Skip to content

Commit 2f4b471

Browse files
committed
added parameters validation and cosmetic changes
1 parent 32d677b commit 2f4b471

File tree

5 files changed

+96
-31
lines changed

5 files changed

+96
-31
lines changed

Core/Inc/AHT20.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ typedef struct {
4747
* Datasheet: AHT20 Product manuals
4848
* 5.3 Send command
4949
*/
50-
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart, uint8_t *status_word, uint16_t status_word_size);
50+
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, uint8_t *status_word, uint16_t status_word_size);
5151

5252
/*
5353
* checks the 3rd bit of a received variable

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 "main.h"
22+
#include "AHT20.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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <stdio.h>
2222
#include <string.h>
2323
#include <math.h>
24+
#include <assert.h>
25+
#include <stddef.h>
2426

2527
/*
2628
* device address for AHT20
@@ -83,7 +85,10 @@ static uint8_t calculate_crc(uint8_t *data);
8385
* Datasheet: AHT20 Product manuals
8486
* 5.3 Send command
8587
*/
86-
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, UART_HandleTypeDef *huart, uint8_t *status_word, uint16_t status_word_size) {
88+
aht20_status_t aht20_get_calibration_status(I2C_HandleTypeDef *hi2c, uint8_t *status_word, uint16_t status_word_size) {
89+
assert(hi2c != NULL);
90+
assert(status_word != NULL);
91+
8792
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &GET_STATUS_CMD, (uint16_t)sizeof(GET_STATUS_CMD), HAL_MAX_DELAY)) {
8893
return AHT20_STATUS_NOT_TRANSMITTED;
8994
}
@@ -116,6 +121,8 @@ aht20_status_t aht20_check_calibration(uint8_t status_word) {
116121
* 5.4 Sensor reading process, paragraph 1
117122
*/
118123
aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
124+
assert(hi2c != NULL);
125+
119126
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, INIT_CMD, (uint16_t)sizeof(INIT_CMD), HAL_MAX_DELAY)) {
120127
return AHT20_STATUS_NOT_TRANSMITTED;
121128
}
@@ -130,6 +137,9 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
130137
* 5.4 Sensor reading process, paragraph 2
131138
*/
132139
aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, uint16_t measured_data_size) {
140+
assert(hi2c != NULL);
141+
assert(measured_data != NULL);
142+
133143
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, MEASURE_CMD, (uint16_t)sizeof(MEASURE_CMD), HAL_MAX_DELAY)) {
134144
return AHT20_STATUS_NOT_TRANSMITTED;
135145
}
@@ -167,6 +177,11 @@ aht20_status_t aht20_measure(I2C_HandleTypeDef *hi2c, uint8_t *measured_data, ui
167177
* 6.2 Temperature transformation
168178
*/
169179
void aht20_calculate_measurments(uint8_t *measured_data, float *humidity, float *temp_c, float *temp_f) {
180+
assert(measured_data != NULL);
181+
assert(humidity != NULL);
182+
assert(temp_c != NULL);
183+
assert(temp_f != NULL);
184+
170185
uint32_t raw_humidity = ((measured_data[1] << 12) | (measured_data[2] << 4) | (measured_data[3] >> 4));
171186
uint32_t raw_temperature = (((measured_data[3] & 0x0F) << 16) | (measured_data[4] << 8) | measured_data[5]);
172187

@@ -182,6 +197,8 @@ void aht20_calculate_measurments(uint8_t *measured_data, float *humidity, float
182197
* 5.5 Soft reset
183198
*/
184199
aht20_status_t aht20_soft_reset(I2C_HandleTypeDef *hi2c) {
200+
assert(hi2c != NULL);
201+
185202
if (HAL_OK != HAL_I2C_Master_Transmit(hi2c, DEVICE_ADDRESS, &SOFT_RESET_CMD, (uint16_t)sizeof(SOFT_RESET_CMD), HAL_MAX_DELAY)) {
186203
return AHT20_STATUS_NOT_TRANSMITTED;
187204
}
@@ -194,6 +211,8 @@ aht20_status_t aht20_soft_reset(I2C_HandleTypeDef *hi2c) {
194211
* calculates crc8 for given data
195212
*/
196213
static uint8_t calculate_crc(uint8_t *data) {
214+
assert(data != NULL);
215+
197216
uint8_t crc = 0xFF;
198217
uint8_t i = 0, j = 0;
199218

Core/Src/main.c

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
/* Private includes ----------------------------------------------------------*/
2727
/* USER CODE BEGIN Includes */
2828
#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";
@@ -144,9 +120,9 @@ int main(void)
144120
aht20_status_t status = AHT20_STATUS_OK;
145121

146122
/* getting info about sensor calibration */
147-
status = aht20_get_calibration_status(&hi2c1, &huart2, &status_word, (uint16_t)sizeof(status_word));
123+
status = aht20_get_calibration_status(&hi2c1, &status_word, (uint16_t)sizeof(status_word));
148124
if (status != AHT20_STATUS_OK) {
149-
print_error(status);
125+
print_error(&huart2, status);
150126
return 1;
151127
} else {
152128
/* checks if calibration is valid */
@@ -155,7 +131,7 @@ int main(void)
155131
/* calibrates if not calibrated*/
156132
status = aht20_calibrate(&hi2c1, status_word);
157133
if (status != AHT20_STATUS_OK) {
158-
print_error(status);
134+
print_error(&huart2, status);
159135
return 2;
160136
}
161137
}
@@ -172,7 +148,7 @@ int main(void)
172148
/* triggering measuring */
173149
status = aht20_measure(&hi2c1, sensor_data.measured_data, (uint16_t)sizeof(sensor_data.measured_data));
174150
if (status != AHT20_STATUS_OK) {
175-
print_error(status);
151+
print_error(&huart2, status);
176152
aht20_soft_reset(&hi2c1);
177153
continue;
178154
}

Core/Src/utils.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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_CALIBRATED) {
34+
sprintf(debug_msg, "I2C device calibration error\r\n");
35+
} else if (status == AHT20_STATUS_NOT_MEASURED) {
36+
sprintf(debug_msg, "I2C device couldn't perform measuring\r\n");
37+
} else if (status == AHT20_STATUS_OK) {
38+
return; // No action needed
39+
} else {
40+
sprintf(debug_msg, "Unknown error\r\n");
41+
}
42+
HAL_UART_Transmit(huart, (uint8_t*)debug_msg, strlen(debug_msg), HAL_MAX_DELAY);
43+
}

0 commit comments

Comments
 (0)