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 */
7374static 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 */
132125aht20_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 */
169165void 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 */
184185aht20_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 */
196199static 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
0 commit comments