Skip to content

Commit 8d0f916

Browse files
committed
simplified api implemented
1 parent 3038eb3 commit 8d0f916

File tree

6 files changed

+64
-77
lines changed

6 files changed

+64
-77
lines changed

Core/Inc/AHT20.h renamed to Core/Inc/aht20.h

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,7 @@
1919

2020
#pragma once
2121
#include "main.h"
22-
23-
/*
24-
* enum for status returns
25-
*/
26-
typedef enum {
27-
AHT20_STATUS_OK = 1,
28-
AHT20_STATUS_NOT_TRANSMITTED,
29-
AHT20_STATUS_NOT_RECEIVED,
30-
AHT20_STATUS_NOT_CALIBRATED,
31-
AHT20_STATUS_NOT_MEASURED,
32-
} aht20_status_t;
22+
#include "aht20_api.h"
3323

3424
/*
3525
* struct for holding measurment data
@@ -41,29 +31,15 @@ typedef struct {
4131
float temperature_f;
4232
} aht20_data_t;
4333

34+
extern const aht20_sensor_api_t aht20_api;
35+
4436
/*
4537
* sends reads status_word for further calibration verification
4638
*
4739
* Datasheet: AHT20 Product manuals
4840
* 5.3 Send command
4941
*/
50-
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, uint8_t *status_word, uint16_t status_word_size);
51-
52-
/*
53-
* checks the 3rd bit of a received variable
54-
*
55-
* Datasheet: AHT20 Product manuals
56-
* 5.4 Sensor reading process, paragraph 1
57-
*/
58-
aht20_status_t aht20_check_calibration(uint8_t status_word);
59-
60-
/*
61-
* sends an array of integers to trigger sensor calibration
62-
*
63-
* Datasheet: AHT20 Product manuals
64-
* 5.4 Sensor reading process, paragraph 1
65-
*/
66-
aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word);
42+
aht20_status_t aht20_validate_calibration(I2C_HandleTypeDef *hi2c);
6743

6844
/*
6945
* sends an array of integers to trigger sensor measurment

Core/Inc/aht20_api.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* aht20_driver_stm32f446ret
3+
* driver for aht20 temperature and humidity sensor
4+
* Copyright (C) 2025 Andrew Kushyk
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Affero General Public License as published
8+
* by the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Affero General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Affero General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#pragma once
21+
22+
/*
23+
* enum for status returns
24+
*/
25+
typedef enum {
26+
AHT20_STATUS_OK = 1,
27+
AHT20_STATUS_NOT_TRANSMITTED,
28+
AHT20_STATUS_NOT_RECEIVED,
29+
AHT20_STATUS_NOT_MEASURED,
30+
} aht20_status_t;
31+
32+
/*
33+
* api for aht20 sensor
34+
*/
35+
typedef struct {
36+
aht20_status_t (*aht20_validate_calibration) (I2C_HandleTypeDef *hi2c);
37+
aht20_status_t (*measure) (I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size);
38+
void (*calculate_measurments) (uint8_t *measured_data, float *humidity, float *temp_c, float *temp_f);
39+
aht20_status_t (*soft_reset) (I2C_HandleTypeDef *hi2c);
40+
} aht20_sensor_api_t;

Core/Inc/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919

2020
#pragma once
21+
#include <aht20.h>
2122
#include "main.h"
22-
#include "AHT20.h"
2323

2424
/*
2525
* prints error message via UART

Core/Src/AHT20.c renamed to Core/Src/aht20.c

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919

20-
#include "AHT20.h"
20+
#include <aht20.h>
2121
#include <stdio.h>
2222
#include <string.h>
2323
#include <assert.h>
@@ -73,6 +73,13 @@ static uint8_t ACK_CMD = 0x06;
7373
*/
7474
static uint8_t NACK_CMD = 0x15;
7575

76+
const aht20_sensor_api_t aht20_api = {
77+
.aht20_validate_calibration = aht20_validate_calibration,
78+
.measure = aht20_measure,
79+
.calculate_measurments = aht20_calculate_measurments,
80+
.soft_reset = aht20_soft_reset,
81+
};
82+
7683
/*
7784
* calculates crc8 for given data
7885
*/
@@ -84,48 +91,28 @@ static uint8_t calculate_crc(uint8_t *data);
8491
* Datasheet: AHT20 Product manuals
8592
* 5.3 Send command
8693
*/
87-
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, uint8_t *status_word, uint16_t status_word_size) {
94+
aht20_status_t aht20_validate_calibration(I2C_HandleTypeDef *hi2c) {
8895
assert(hi2c != NULL);
89-
assert(status_word != NULL);
96+
uint8_t status_word = 0;
97+
98+
HAL_Delay(40);
9099

91100
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &GET_STATUS_CMD, (uint16_t)sizeof(GET_STATUS_CMD), HAL_MAX_DELAY)) {
92101
return AHT20_STATUS_NOT_TRANSMITTED;
93102
}
94103

95-
if (HAL_OK != HAL_I2C_Master_Receive(hi2c, DEVICE_ADDRESS, status_word, status_word_size, HAL_MAX_DELAY)) {
104+
if (HAL_OK != HAL_I2C_Master_Receive(hi2c, DEVICE_ADDRESS, &status_word, (uint16_t)sizeof(status_word), HAL_MAX_DELAY)) {
96105
return AHT20_STATUS_NOT_RECEIVED;
97106
}
98107

99-
return AHT20_STATUS_OK;
100-
}
101-
102-
/*
103-
* checks the 3rd bit of a received variable
104-
*
105-
* Datasheet: AHT20 Product manuals
106-
* 5.4 Sensor reading process, paragraph 1
107-
*/
108-
aht20_status_t aht20_check_calibration(uint8_t status_word) {
109108
if (status_word & (1 << 3)) {
110109
return AHT20_STATUS_OK;
111110
} else {
112-
return AHT20_STATUS_NOT_CALIBRATED;
113-
}
114-
}
115-
116-
/*
117-
* sends an array of integers to trigger sensor calibration
118-
*
119-
* Datasheet: AHT20 Product manuals
120-
* 5.4 Sensor reading process, paragraph 1
121-
*/
122-
aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
123-
assert(hi2c != NULL);
124-
125-
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, INIT_CMD, (uint16_t)sizeof(INIT_CMD), HAL_MAX_DELAY)) {
126-
return AHT20_STATUS_NOT_TRANSMITTED;
111+
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, INIT_CMD, (uint16_t)sizeof(INIT_CMD), HAL_MAX_DELAY)) {
112+
return AHT20_STATUS_NOT_TRANSMITTED;
113+
}
114+
HAL_Delay(10);
127115
}
128-
129116
return AHT20_STATUS_OK;
130117
}
131118

Core/Src/main.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
/* USER CODE END Header */
2323
/* Includes ------------------------------------------------------------------*/
24+
#include <aht20.h>
2425
#include "main.h"
2526

2627
/* Private includes ----------------------------------------------------------*/
2728
/* USER CODE BEGIN Includes */
28-
#include "AHT20.h"
2929
#include "utils.h"
3030
#include <stdio.h>
3131
#include <string.h>
@@ -114,27 +114,13 @@ int main(void)
114114
* Datasheet: AHT20 Product manuals
115115
* 5.4 Sensor reading process, paragraph 1
116116
*/
117-
HAL_Delay(40);
118-
119-
uint8_t status_word = 0;
120117
aht20_status_t status = AHT20_STATUS_OK;
121118

122119
/* getting info about sensor calibration */
123-
status = aht20_get_calibration_status(&hi2c1, &status_word, (uint16_t)sizeof(status_word));
120+
status = aht20_validate_calibration(&hi2c1);
124121
if (status != AHT20_STATUS_OK) {
125122
print_error(&huart2, status);
126123
return 1;
127-
} else {
128-
/* checks if calibration is valid */
129-
status = aht20_check_calibration(status_word);
130-
if (status != AHT20_STATUS_OK) {
131-
/* calibrates if not calibrated*/
132-
status = aht20_calibrate(&hi2c1, status_word);
133-
if (status != AHT20_STATUS_OK) {
134-
print_error(&huart2, status);
135-
return 2;
136-
}
137-
}
138124
}
139125

140126
/* USER CODE END 2 */

Core/Src/utils.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ void print_error(UART_HandleTypeDef *huart, aht20_status_t status) {
3030
sprintf(debug_msg, "I2C initialization transmit error: 0x71\r\n");
3131
} else if (status == AHT20_STATUS_NOT_RECEIVED) {
3232
sprintf(debug_msg, "I2C initialization receive error: status_word\r\n");
33-
} else if (status == AHT20_STATUS_NOT_CALIBRATED) {
34-
sprintf(debug_msg, "I2C device calibration error\r\n");
3533
} else if (status == AHT20_STATUS_NOT_MEASURED) {
3634
sprintf(debug_msg, "I2C device couldn't perform measuring\r\n");
3735
} else if (status == AHT20_STATUS_OK) {

0 commit comments

Comments
 (0)