Skip to content

Commit 1aa3e7a

Browse files
authored
Merge pull request #5 from git-user-cpp/development
v2.0.0
2 parents 32d677b + 8d0f916 commit 1aa3e7a

File tree

6 files changed

+150
-99
lines changed

6 files changed

+150
-99
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, UART_HandleTypeDef *huart, 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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
#include <aht20.h>
22+
#include "main.h"
23+
24+
/*
25+
* prints error message via UART
26+
*/
27+
void print_error(UART_HandleTypeDef *huart, aht20_status_t status);

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

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
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>
23-
#include <math.h>
23+
#include <assert.h>
24+
#include <stddef.h>
2425

2526
/*
2627
* device address for AHT20
@@ -72,6 +73,13 @@ static uint8_t ACK_CMD = 0x06;
7273
*/
7374
static uint8_t NACK_CMD = 0x15;
7475

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+
7583
/*
7684
* calculates crc8 for given data
7785
*/
@@ -83,43 +91,28 @@ static uint8_t calculate_crc(uint8_t *data);
8391
* Datasheet: AHT20 Product manuals
8492
* 5.3 Send command
8593
*/
86-
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart, uint8_t *status_word, uint16_t status_word_size) {
94+
aht20_status_t aht20_validate_calibration(I2C_HandleTypeDef *hi2c) {
95+
assert(hi2c != NULL);
96+
uint8_t status_word = 0;
97+
98+
HAL_Delay(40);
99+
87100
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &GET_STATUS_CMD, (uint16_t)sizeof(GET_STATUS_CMD), HAL_MAX_DELAY)) {
88101
return AHT20_STATUS_NOT_TRANSMITTED;
89102
}
90103

91-
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)) {
92105
return AHT20_STATUS_NOT_RECEIVED;
93106
}
94107

95-
return AHT20_STATUS_OK;
96-
}
97-
98-
/*
99-
* checks the 3rd bit of a received variable
100-
*
101-
* Datasheet: AHT20 Product manuals
102-
* 5.4 Sensor reading process, paragraph 1
103-
*/
104-
aht20_status_t aht20_check_calibration(uint8_t status_word) {
105108
if (status_word & (1 << 3)) {
106109
return AHT20_STATUS_OK;
107110
} else {
108-
return AHT20_STATUS_NOT_CALIBRATED;
109-
}
110-
}
111-
112-
/*
113-
* sends an array of integers to trigger sensor calibration
114-
*
115-
* Datasheet: AHT20 Product manuals
116-
* 5.4 Sensor reading process, paragraph 1
117-
*/
118-
aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
119-
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, INIT_CMD, (uint16_t)sizeof(INIT_CMD), HAL_MAX_DELAY)) {
120-
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);
121115
}
122-
123116
return AHT20_STATUS_OK;
124117
}
125118

@@ -130,6 +123,9 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
130123
* 5.4 Sensor reading process, paragraph 2
131124
*/
132125
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size) {
126+
assert(hi2c != NULL);
127+
assert(measured_data != NULL);
128+
133129
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, MEASURE_CMD, (uint16_t)sizeof(MEASURE_CMD), HAL_MAX_DELAY)) {
134130
return AHT20_STATUS_NOT_TRANSMITTED;
135131
}
@@ -167,6 +163,11 @@ aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, ui
167163
* 6.2 Temperature transformation
168164
*/
169165
void aht20_calculate_measurments(uint8_t *measured_data, float *humidity, float *temp_c, float *temp_f) {
166+
assert(measured_data != NULL);
167+
assert(humidity != NULL);
168+
assert(temp_c != NULL);
169+
assert(temp_f != NULL);
170+
170171
uint32_t raw_humidity = ((measured_data[1] << 12) | (measured_data[2] << 4) | (measured_data[3] >> 4));
171172
uint32_t raw_temperature = (((measured_data[3] & 0x0F) << 16) | (measured_data[4] << 8) | measured_data[5]);
172173

@@ -182,6 +183,8 @@ void aht20_calculate_measurments(uint8_t *measured_data, float *humidity, float
182183
* 5.5 Soft reset
183184
*/
184185
aht20_status_t aht20_soft_reset(I2C_HandleTypeDef *hi2c) {
186+
assert(hi2c != NULL);
187+
185188
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &SOFT_RESET_CMD, (uint16_t)sizeof(SOFT_RESET_CMD), HAL_MAX_DELAY)) {
186189
return AHT20_STATUS_NOT_TRANSMITTED;
187190
}
@@ -194,6 +197,8 @@ aht20_status_t aht20_soft_reset(I2C_HandleTypeDef *hi2c) {
194197
* calculates crc8 for given data
195198
*/
196199
static uint8_t calculate_crc(uint8_t *data) {
200+
assert(data != NULL);
201+
197202
uint8_t crc = 0xFF;
198203
uint8_t i = 0, j = 0;
199204

Core/Src/main.c

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
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"
29+
#include "utils.h"
2930
#include <stdio.h>
3031
#include <string.h>
3132
/* USER CODE END Includes */
@@ -69,31 +70,6 @@ void UART_Send_String(const char* str)
6970
HAL_UART_Transmit(&huart2, (uint8_t*)str, strlen(str), HAL_MAX_DELAY);
7071
}
7172

72-
/* prints error message to UART based on received status */
73-
void print_error(aht20_status_t status) {
74-
char debug_msg[64] = "\0";
75-
76-
if(status == AHT20_STATUS_NOT_TRANSMITTED) {
77-
sprintf(debug_msg, "I2C initialization transmit error: 0x71\r\n");
78-
UART_Send_String(debug_msg);
79-
} else if (status == AHT20_STATUS_NOT_RECEIVED) {
80-
sprintf(debug_msg, "I2C initialization recieve error: status_word\r\n");
81-
UART_Send_String(debug_msg);
82-
} else if (status == AHT20_STATUS_NOT_CALIBRATED) {
83-
sprintf(debug_msg, "I2C device calibration error\r\n");
84-
UART_Send_String(debug_msg);
85-
} else if (status == AHT20_STATUS_NOT_MEASURED) {
86-
sprintf(debug_msg, "I2C device couldn't perform measuring\r\n");
87-
UART_Send_String(debug_msg);
88-
} else if (status == AHT20_STATUS_OK) {
89-
/* doing nothing if ok */
90-
}
91-
else {
92-
sprintf(debug_msg, "Unknown error\r\n");
93-
UART_Send_String(debug_msg);
94-
}
95-
}
96-
9773
/* transmits data to UART */
9874
void transmit_data(float humidity, float temperature_c, float temperature_f) {
9975
char data_to_display[60] = "\0";
@@ -138,27 +114,13 @@ int main(void)
138114
* Datasheet: AHT20 Product manuals
139115
* 5.4 Sensor reading process, paragraph 1
140116
*/
141-
HAL_Delay(40);
142-
143-
uint8_t status_word = 0;
144117
aht20_status_t status = AHT20_STATUS_OK;
145118

146119
/* getting info about sensor calibration */
147-
status = aht20_get_calibration_status(&hi2c1, &huart2, &status_word, (uint16_t)sizeof(status_word));
120+
status = aht20_validate_calibration(&hi2c1);
148121
if (status != AHT20_STATUS_OK) {
149-
print_error(status);
122+
print_error(&huart2, status);
150123
return 1;
151-
} else {
152-
/* checks if calibration is valid */
153-
status = aht20_check_calibration(status_word);
154-
if (status != AHT20_STATUS_OK) {
155-
/* calibrates if not calibrated*/
156-
status = aht20_calibrate(&hi2c1, status_word);
157-
if (status != AHT20_STATUS_OK) {
158-
print_error(status);
159-
return 2;
160-
}
161-
}
162124
}
163125

164126
/* USER CODE END 2 */
@@ -172,7 +134,7 @@ int main(void)
172134
/* triggering measuring */
173135
status = aht20_measure(&hi2c1, sensor_data.measured_data, (uint16_t)sizeof(sensor_data.measured_data));
174136
if (status != AHT20_STATUS_OK) {
175-
print_error(status);
137+
print_error(&huart2, status);
176138
aht20_soft_reset(&hi2c1);
177139
continue;
178140
}

Core/Src/utils.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#include "utils.h"
21+
#include <stdio.h>
22+
#include <string.h>
23+
24+
/*
25+
* prints error message via UART
26+
*/
27+
void print_error(UART_HandleTypeDef *huart, aht20_status_t status) {
28+
char debug_msg[64] = "\0";
29+
if (status == AHT20_STATUS_NOT_TRANSMITTED) {
30+
sprintf(debug_msg, "I2C initialization transmit error: 0x71\r\n");
31+
} else if (status == AHT20_STATUS_NOT_RECEIVED) {
32+
sprintf(debug_msg, "I2C initialization receive error: status_word\r\n");
33+
} else if (status == AHT20_STATUS_NOT_MEASURED) {
34+
sprintf(debug_msg, "I2C device couldn't perform measuring\r\n");
35+
} else if (status == AHT20_STATUS_OK) {
36+
return; // No action needed
37+
} else {
38+
sprintf(debug_msg, "Unknown error\r\n");
39+
}
40+
HAL_UART_Transmit(huart, (uint8_t*)debug_msg, strlen(debug_msg), HAL_MAX_DELAY);
41+
}

0 commit comments

Comments
 (0)