Skip to content

Commit 22cd170

Browse files
committed
Merge branch 'update_aht20'
2 parents f97e3c8 + addf33f commit 22cd170

File tree

11 files changed

+88
-96
lines changed

11 files changed

+88
-96
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# ChangeLog
2+
3+
## v0.2.0 - 2024-05-22
4+
5+
### Enhancements:
6+
7+
* Add config ADC_MIC_APPLY_GAIN to apply gain to the ADC mic.
8+
9+
## v0.1.0 - 2024-05-22
10+
11+
### Enhancements:
12+
13+
* Initial version.

components/audio/adc_mic/Kconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
menu "ADC Mic"
2+
3+
config ADC_MIC_APPLY_GAIN
4+
int "Apply Gain"
5+
default 3
6+
range 0 4
7+
help
8+
Apply gain to the ADC mic.
9+
0: No gain applied
10+
1: Left shift 1 bit (2x gain)
11+
2: Left shift 2 bits (4x gain)
12+
3: Left shift 3 bits (8x gain)
13+
4: Left shift 4 bits (16x gain)
14+
15+
config ADC_MIC_OFFSET
16+
int
17+
default 2048 if ADC_MIC_APPLY_GAIN = 0
18+
default 4095 if ADC_MIC_APPLY_GAIN = 1
19+
default 8190 if ADC_MIC_APPLY_GAIN = 2
20+
default 16380 if ADC_MIC_APPLY_GAIN = 3
21+
default 32760 if ADC_MIC_APPLY_GAIN = 4
22+
23+
endmenu

components/audio/adc_mic/adc_mic.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static int _adc_data_read(const audio_codec_data_if_t *h, uint8_t *data, int siz
138138
for (int i = 0; i < item_count; i++) {
139139
uint16_t raw_value = buffer[i].val;
140140
// Left shift to amplify audio.
141-
p[i] = (raw_value << 4) - 32768;
141+
p[i] = (raw_value << CONFIG_ADC_MIC_APPLY_GAIN) - CONFIG_ADC_MIC_OFFSET;
142142
}
143143

144144
cnt += ret_num;
@@ -154,7 +154,7 @@ static int _adc_data_read(const audio_codec_data_if_t *h, uint8_t *data, int siz
154154
for (int i = 0; i < item_count; i++) {
155155
uint16_t raw_value = buffer[i].val & 0xFFFF;
156156
// Left shift to amplify audio.
157-
p[i] = (raw_value << 4) - 32768;
157+
p[i] = (raw_value << CONFIG_ADC_MIC_APPLY_GAIN) - CONFIG_ADC_MIC_OFFSET;
158158
}
159159
cnt += ret_num / 2;
160160
left_size -= ret_num / 2;

components/audio/adc_mic/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "0.1.0"
1+
version: "0.2.0"
22
description: ADC mic input driver for ESP32 Series socs
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/audio/adc_mic
44
repository: https://github.com/espressif/esp-iot-solution.git

components/sensors/humiture/aht20/Kconfig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,4 @@ menu "AHT20 : CONFIGURATION"
66
CRC check to be performed on results or not?.
77
default n
88

9-
10-
config AHT20_I2C_CLK_SPEED
11-
int "I2C clock speed"
12-
default 100000
13-
range 1 400000
14-
help
15-
Clock speed used for i2c communication by AHT20 device.
16-
Limited to maximum of 400KHZ.
17-
189
endmenu

components/sensors/humiture/aht20/README.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,17 @@ Following are the general guidelines.
7373
printf("Temperature = %.2f°C\n", temperature);
7474
```
7575
76-
## Senosr Hub
7776
78-
Following are the general guidelines.
79-
```
80-
81-
/*create a sensor with specific sensor_id and configurations*/
82-
iot_sensor_create("aht20", &sensor_config, &sensor_handle);
83-
84-
/*register handler with sensor's handle*/
85-
iot_sensor_handler_register(sensor_handle, sensor_event_handler, NULL);
86-
87-
/*start the sensor, data ready events will be posted once data is acquired successfully*/
88-
iot_sensor_start(sensor_handle);
89-
90-
```
91-
92-
## How to Configure CRC and I2C clock speed
77+
## How to Configure CRC
9378
Additionally, select in menuconfig under Component Config → AHT20; to use CRC(default is not used)
9479
or change the clock speed of device (default is 100KHz).
9580
9681
Note : It is recommended to use clock speeds in upper ranges of 100kHz to 200kHz.
9782
Higher clock speeds may cause occasional data inconsistencies depending on your board layout and wiring.
9883
99-
![image](https://github.com/user-attachments/assets/fc8680fb-1567-477c-92f8-52dd126e6f9d)
84+
![image](https://github.com/user-attachments/assets/58a07cc9-5d87-4afe-9675-637b3e776faa)
85+
10086
10187
or
10288
In sdkconfig under Component Config → AHT20,
103-
![image](https://github.com/user-attachments/assets/1f9612df-8d73-4ad1-bec7-75cbe6ed327a)
89+

components/sensors/humiture/aht20/aht20.c

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
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

@@ -24,6 +14,13 @@
2414

2515
static 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)
281288
aht20_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

296303
esp_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

components/sensors/humiture/aht20/include/aht20.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,7 @@
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
/*
7-
* @File: aht20.h
8-
*
9-
* @brief: AHT20 driver function declarations and typedefinitons
10-
*
11-
* @Date: May 2, 2025
12-
*
13-
* @Author: Rohan Jeet <[email protected]>
14-
*
15-
*/
6+
167
#ifndef AHT20_H
178
#define AHT20_H
189

@@ -42,17 +33,10 @@ extern "C" {
4233
#define AHT20_INIT_REG 0XBE /*!< initialize the AHT20 */
4334
#define AHT20_MEASURE_CYC 0xAC /*!< trigger measurement in cycle mode */
4435

45-
/**
46-
* @brief AHT20 device object
47-
*/
48-
typedef struct {
49-
i2c_bus_device_handle_t i2c_dev; /*!< i2c device handle. */
50-
} aht20_dev_config_t;
51-
5236
/**
5337
* @brief AHT20 device handle
5438
*/
55-
typedef aht20_dev_config_t *aht20_handle_t;
39+
typedef void * aht20_handle_t;
5640

5741
/**
5842
* @brief soft reset AHT20

components/sensors/humiture/aht20/test_apps/aht20_driver_test/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
cmake_minimum_required(VERSION 3.5)
44

55
set(COMPONENTS main)
6-
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components")
6+
set(EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/unit-test-app/components"
7+
"../../../aht20")
78

89
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
910
project(aht20_test)

components/sensors/humiture/aht20/test_apps/aht20_driver_test/main/aht20_test_app.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
6-
/*-
7-
* @File: aht20_test_app.c
8-
*
9-
* @brief: AHT20 driver unity test app
10-
*
11-
* @Date: May 2, 2025
12-
*
13-
* @Author: Rohan Jeet <[email protected]>
14-
*
15-
*/
166

177
#include <stdio.h>
188
#include "unity.h"

0 commit comments

Comments
 (0)