33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
6- /*
7- * @File: aht20.c
8- *
9- * @brief: AHT20 driver function definitions
10- *
11- * @Date: May 2, 2025
12- *
13- * @Author: Rohan Jeet <[email protected] > 14- *
15- */
166
177#include <stdio.h>
188
2414
2515static const char * s_TAG = "AHT20" ;
2616
17+ /**
18+ * @brief AHT20 device object
19+ */
20+ typedef struct {
21+ i2c_bus_device_handle_t i2c_dev ; /*!< i2c device handle. */
22+ } aht20_dev_config_t ;
23+
2724/**
2825 *@brief AHT20 raw result
2926 */
@@ -42,7 +39,7 @@ typedef struct {
4239* @param[in] read_size data size to read
4340*
4441*/
45- static esp_err_t aht20_read_reg (aht20_handle_t sensor , uint8_t * read_buffer , uint8_t read_size );
42+ static esp_err_t aht20_read_reg (aht20_dev_config_t * aht20_handle , uint8_t * read_buffer , uint8_t read_size );
4643
4744/**
4845* @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized
@@ -54,15 +51,15 @@ static esp_err_t aht20_read_reg(aht20_handle_t sensor, uint8_t * read_buffer, ui
5451* @param[in] write_size data size to write
5552*
5653*/
57- static esp_err_t aht20_write_reg (aht20_handle_t sensor , uint8_t * cmd , uint8_t write_size );
54+ static esp_err_t aht20_write_reg (aht20_dev_config_t * aht20_handle , uint8_t * cmd , uint8_t write_size );
5855
5956/**
6057* @brief a function used to handle reinitialization of registers of the device, if not found calibrated when initialized
6158*
6259* @param[in] aht20_handle AHT20 device handle
6360*
6461*/
65- static esp_err_t aht20_Start_Init (aht20_handle_t aht20_handle );
62+ static esp_err_t aht20_Start_Init (aht20_dev_config_t * aht20_handle );
6663
6764/**
6865* @brief a function used to handle resetting of registers of the device, if not found calibrated when initialized
@@ -72,7 +69,7 @@ static esp_err_t aht20_Start_Init(aht20_handle_t aht20_handle);
7269* @param[in] addr AHT20 internal register, undocumented in datasheet
7370*
7471*/
75- static esp_err_t aht20_JH_Reset_REG (aht20_handle_t aht20_handle , uint8_t addr );
72+ static esp_err_t aht20_JH_Reset_REG (aht20_dev_config_t * aht20_handle , uint8_t addr );
7673
7774/**
7875* @brief check crc validity of response received
@@ -99,25 +96,26 @@ static uint8_t calc_CRC8(uint8_t *message, uint8_t Num);
9996* - other error codes : failure in reading AHT20 busy status
10097*
10198*/
102- static esp_err_t aht20_busy_status (aht20_handle_t aht20_handle , bool * busy );
99+ static esp_err_t aht20_busy_status (aht20_dev_config_t * aht20_handle , bool * busy );
103100
104- static esp_err_t aht20_read_reg (aht20_handle_t sensor , uint8_t * read_buffer , uint8_t read_size )
101+ static esp_err_t aht20_read_reg (aht20_dev_config_t * aht20_handle , uint8_t * read_buffer , uint8_t read_size )
105102{
106- ESP_RETURN_ON_ERROR (i2c_bus_read_bytes (sensor -> i2c_dev , NULL_I2C_MEM_ADDR , read_size , read_buffer ),
103+
104+ ESP_RETURN_ON_ERROR (i2c_bus_read_bytes (aht20_handle -> i2c_dev , NULL_I2C_MEM_ADDR , read_size , read_buffer ),
107105 s_TAG , "unable to read from aht20" );
108106
109107 return ESP_OK ;
110108}
111109
112- static esp_err_t aht20_write_reg (aht20_handle_t sensor , uint8_t * cmd , uint8_t write_size )
110+ static esp_err_t aht20_write_reg (aht20_dev_config_t * aht20_handle , uint8_t * cmd , uint8_t write_size )
113111{
114- ESP_RETURN_ON_ERROR (i2c_bus_write_bytes (sensor -> i2c_dev , NULL_I2C_MEM_ADDR , write_size , cmd ),
112+ ESP_RETURN_ON_ERROR (i2c_bus_write_bytes (aht20_handle -> i2c_dev , NULL_I2C_MEM_ADDR , write_size , cmd ),
115113 s_TAG , "unable to set mode for AHT20\n" );
116114
117115 return ESP_OK ;
118116}
119117
120- static esp_err_t aht20_busy_status (aht20_handle_t aht20_handle , bool * busy )
118+ static esp_err_t aht20_busy_status (aht20_dev_config_t * aht20_handle , bool * busy )
121119{
122120 ESP_RETURN_ON_FALSE ((aht20_handle != NULL ), ESP_ERR_INVALID_ARG ,
123121 s_TAG , "empty handle, initialize AHT20 handle" );
@@ -137,7 +135,7 @@ static esp_err_t aht20_busy_status(aht20_handle_t aht20_handle, bool *busy)
137135 return ESP_OK ;
138136}
139137
140- static esp_err_t aht20_JH_Reset_REG (aht20_handle_t aht20_handle , uint8_t addr )
138+ static esp_err_t aht20_JH_Reset_REG (aht20_dev_config_t * aht20_handle , uint8_t addr )
141139{
142140
143141 uint8_t reset_cmd [] = {addr , 0x00 , 0x00 }, read_bytes [3 ];
@@ -159,7 +157,7 @@ static esp_err_t aht20_JH_Reset_REG(aht20_handle_t aht20_handle, uint8_t addr)
159157 return ESP_OK ;
160158}
161159
162- static esp_err_t aht20_Start_Init (aht20_handle_t aht20_handle )
160+ static esp_err_t aht20_Start_Init (aht20_dev_config_t * aht20_handle )
163161{
164162 ESP_RETURN_ON_ERROR (aht20_JH_Reset_REG (aht20_handle , 0x1b ), "" , "" );
165163 ESP_RETURN_ON_ERROR (aht20_JH_Reset_REG (aht20_handle , 0x1c ), "" , "" );
@@ -190,7 +188,7 @@ static uint8_t calc_CRC8(uint8_t *message, uint8_t Num)
190188
191189/******************************************** Public *********************************************/
192190
193- esp_err_t aht20_read_raw (aht20_handle_t aht20_handle , aht20_raw_reading_t * raw_read )
191+ static esp_err_t aht20_read_raw (aht20_dev_config_t * aht20_handle , aht20_raw_reading_t * raw_read )
194192{
195193 ESP_RETURN_ON_FALSE ((aht20_handle != NULL ), ESP_ERR_INVALID_ARG , s_TAG , "empty handle, provide a valid AHT20 handle" );
196194
@@ -224,17 +222,25 @@ esp_err_t aht20_read_raw(aht20_handle_t aht20_handle, aht20_raw_reading_t *raw_r
224222 return ESP_OK ;
225223}
226224
227- esp_err_t aht20_read_humidity (aht20_handle_t aht20_handle , float_t * humidity )
225+ esp_err_t aht20_read_humidity (aht20_handle_t aht20 , float_t * humidity )
228226{
227+ ESP_RETURN_ON_FALSE ((aht20 != NULL ), ESP_ERR_INVALID_ARG , s_TAG , "empty handle, initialize AHT20 handle" );
228+
229+ aht20_dev_config_t * aht20_handle = (aht20_dev_config_t * ) aht20 ;
230+
229231 aht20_raw_reading_t raw_read ;
230232 ESP_RETURN_ON_ERROR (aht20_read_raw (aht20_handle , & raw_read ), "" , "" );
231233
232234 * humidity = raw_read .humidity * 100.0 / 1024 / 1024 ; //Calculated humidity value
233235 return ESP_OK ;
234236}
235237
236- esp_err_t aht20_read_temperature (aht20_handle_t aht20_handle , float_t * temperature )
238+ esp_err_t aht20_read_temperature (aht20_handle_t aht20 , float_t * temperature )
237239{
240+ ESP_RETURN_ON_FALSE ((aht20 != NULL ), ESP_ERR_INVALID_ARG , s_TAG , "empty handle, initialize AHT20 handle" );
241+
242+ aht20_dev_config_t * aht20_handle = (aht20_dev_config_t * ) aht20 ;
243+
238244 aht20_raw_reading_t raw_read ;
239245 ESP_RETURN_ON_ERROR (aht20_read_raw (aht20_handle , & raw_read ), "" , "" );
240246
@@ -243,10 +249,11 @@ esp_err_t aht20_read_temperature(aht20_handle_t aht20_handle, float_t *temperatu
243249 return ESP_OK ;
244250}
245251
246- esp_err_t aht20_init (aht20_handle_t aht20_handle )
252+ esp_err_t aht20_init (aht20_handle_t aht20 )
247253{
248- ESP_RETURN_ON_FALSE ((aht20_handle != NULL ), ESP_ERR_INVALID_ARG , s_TAG , "empty handle, initialize AHT20 handle" );
254+ ESP_RETURN_ON_FALSE ((aht20 != NULL ), ESP_ERR_INVALID_ARG , s_TAG , "empty handle, initialize AHT20 handle" );
249255
256+ aht20_dev_config_t * aht20_handle = (aht20_dev_config_t * ) aht20 ;
250257 vTaskDelay (20 / portTICK_PERIOD_MS ); //time for AHT20 SCL to stabilize
251258
252259 /***********************************************************************************/
@@ -281,26 +288,27 @@ esp_err_t aht20_init(aht20_handle_t aht20_handle)
281288aht20_handle_t aht20_create (i2c_bus_handle_t bus_handle , uint8_t aht20_address )
282289{
283290 ESP_LOGI (s_TAG , "adding aht20 as device to bus\n" );
284- i2c_bus_device_handle_t dev_handle = i2c_bus_device_create (bus_handle , aht20_address , CONFIG_AHT20_I2C_CLK_SPEED );
291+ i2c_bus_device_handle_t dev_handle = i2c_bus_device_create (bus_handle , aht20_address , i2c_bus_get_current_clk_speed ( bus_handle ) );
285292 ESP_RETURN_ON_FALSE ((dev_handle != NULL ), NULL , s_TAG , "unable to create device\n" );
286293 ESP_LOGI (s_TAG , "device added to bus\n" );
287294
288- aht20_handle_t my_aht20_handle = malloc (sizeof (aht20_dev_config_t ));
295+ aht20_dev_config_t * my_aht20_handle = malloc (sizeof (aht20_dev_config_t ));
289296
290297 ESP_RETURN_ON_FALSE ((my_aht20_handle != NULL ), NULL , s_TAG , "unable to allocate memory to initialize aht20 handle" );
291298
292299 my_aht20_handle -> i2c_dev = dev_handle ;
293- return my_aht20_handle ;
300+ return ( aht20_handle_t ) my_aht20_handle ;
294301}
295302
296303esp_err_t aht20_remove (aht20_handle_t * aht20ptr )
297304{
298305 if (* aht20ptr == NULL ) {
299306 return ESP_ERR_INVALID_ARG ;
300307 }
301- i2c_bus_device_delete (& ((* aht20ptr )-> i2c_dev ));
302- free (* aht20ptr );
303- * aht20ptr = NULL ; // now AHT20 handle is not a dangling pointer
308+ aht20_dev_config_t * * aht20_handle = (aht20_dev_config_t * * ) aht20ptr ;
309+ i2c_bus_device_delete (& ((* aht20_handle )-> i2c_dev ));
310+ free (* aht20_handle );
311+ * aht20_handle = NULL ; // now AHT20 handle is not a dangling pointer
304312 return ESP_OK ;
305313}
306314
0 commit comments