diff --git a/main/APP/app_task_sensors/app_task_sensors.c b/main/APP/app_task_sensors/app_task_sensors.c new file mode 100644 index 0000000..3a11c32 --- /dev/null +++ b/main/APP/app_task_sensors/app_task_sensors.c @@ -0,0 +1,104 @@ +#include "app_task_sensors.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "driver/i2c.h" + +#include "drv_bmp280_3v3.h" +#include +#include +#include + +#define SENSOR_DRV_I2C_MASTER_SCL_IO 33 +#define SENSOR_DRV_I2C_MASTER_SDA_IO 34 +#define SENSOR_DRV_I2C_MASTER_FREQ_HZ 100000 +#define SENSOR_DRV_I2C_CONTROLLER_PORT 0 +#define SENSOR_DRV_I2C_MASTER_TIMEOUT_MS 1000 +#define SENSOR_DRV_I2C_MASTER_RX_BUF_DISABLE 0 +#define SENSOR_DRV_I2C_MASTER_TX_BUF_DISABLE 0 + +static i2c_config_t sg_drv_sensor_i2c_conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = SENSOR_DRV_I2C_MASTER_SDA_IO, + .scl_io_num = SENSOR_DRV_I2C_MASTER_SCL_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .scl_pullup_en = GPIO_PULLUP_ENABLE, + .master.clk_speed = SENSOR_DRV_I2C_MASTER_FREQ_HZ, +}; + +static drv_bmp280_3v3_t* sg_drv_bmp280_handle = NULL; + +static uint8_t drv_i2c_read(uint8_t chip_addr, uint8_t reg_addr, uint8_t *data, uint16_t data_len) +{ + int ret = i2c_master_write_read_device(SENSOR_DRV_I2C_CONTROLLER_PORT, chip_addr , ®_addr, 1, data, data_len, SENSOR_DRV_I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS); + + if (ret) { + printf("I2C read error = %d\r\n", ret); + } + return 0; +} + +static uint8_t drv_i2c_write(uint8_t chip_addr, uint8_t reg_addr, uint8_t data) +{ + int ret; + uint8_t write_buf[2] = {reg_addr, data}; + + ret = i2c_master_write_to_device(SENSOR_DRV_I2C_CONTROLLER_PORT, chip_addr, write_buf, sizeof(write_buf), SENSOR_DRV_I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS); + + if (ret) { + printf("I2C write error = %d\r\n", ret); + } + return 0; +} + +static uint8_t _app_task_sensors_drv_init() +{ + sg_drv_bmp280_handle = malloc(sizeof(drv_bmp280_3v3_t)); + memset(sg_drv_bmp280_handle, 0, sizeof(drv_bmp280_3v3_t)); + sg_drv_bmp280_handle->drv_i2c_read = drv_i2c_read; + sg_drv_bmp280_handle->drv_i2c_write = drv_i2c_write; + drv_bmp280_3v3_init(sg_drv_bmp280_handle); + return 0; +} + +static uint8_t _app_task_sensors_drv_deinit() +{ + + return 0; +} + +static esp_err_t _app_task_sensors_esp_i2c_init() +{ + i2c_param_config(SENSOR_DRV_I2C_CONTROLLER_PORT, &sg_drv_sensor_i2c_conf); + + return i2c_driver_install(SENSOR_DRV_I2C_CONTROLLER_PORT, sg_drv_sensor_i2c_conf.mode, SENSOR_DRV_I2C_MASTER_RX_BUF_DISABLE, SENSOR_DRV_I2C_MASTER_TX_BUF_DISABLE, 0); +} + +static void _app_task_sensors_run() +{ + int ret = 0; + uint32_t presure = 0; + ret = _app_task_sensors_esp_i2c_init(); + if (ret) { + printf("sensor i2c init faild = %d\r\n", ret); + return; + } + + _app_task_sensors_drv_init(); + + while(1) { + drv_bmp280_3v3_get_pressure(sg_drv_bmp280_handle, &presure); + vTaskDelay(100); + } +} + +uint8_t app_task_sensors_start() +{ + vTaskDelay(10); + xTaskCreatePinnedToCore(_app_task_sensors_run, "sensor task", 4096, NULL, 7, NULL, tskNO_AFFINITY); + return 0; +} + +uint8_t app_task_sensors_stop() +{ + return 0; +} diff --git a/main/APP/app_task_sensors/app_task_sensors.h b/main/APP/app_task_sensors/app_task_sensors.h new file mode 100644 index 0000000..dcef346 --- /dev/null +++ b/main/APP/app_task_sensors/app_task_sensors.h @@ -0,0 +1,17 @@ +#ifndef __APP_TASK_SENSORS_H__ +#define __APP_TASK_SENSORS_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +uint8_t app_task_sensors_start(); + +uint8_t app_task_sensors_stop(); + +#ifdef __cplusplus +} +#endif +#endif//__APP_TASK_SENSORS_H__ \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 3444f47..010c4d4 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -4,9 +4,13 @@ idf_component_register( "APP/app_task_hello" "APP/app_task_wifi" "APP/app_task_mqtt" + "APP/app_task_sensors" + "drivers/drv_bmp280_3v3" INCLUDE_DIRS "." "APP/app_task_hello" "APP/app_task_wifi" "APP/app_task_mqtt" + "APP/app_task_sensors" + "drivers/drv_bmp280_3v3" ) diff --git a/main/app_main.c b/main/app_main.c index 94893f5..2a48bb5 100644 --- a/main/app_main.c +++ b/main/app_main.c @@ -17,6 +17,7 @@ #include "app_task_hello.h" #include "app_task_wifi.h" #include "app_task_mqtt.h" +#include "app_task_sensors.h" #include "app_main.h" @@ -44,6 +45,7 @@ void app_main(void) // app_hello_task_start(); app_task_wifi_start(); app_task_mqtt_start(); + app_task_sensors_start(); /* Print chip information */ esp_chip_info_t chip_info; diff --git a/main/drivers/drv_bmp280_3v3/drv_bmp280_3v3.c b/main/drivers/drv_bmp280_3v3/drv_bmp280_3v3.c new file mode 100644 index 0000000..c780bf7 --- /dev/null +++ b/main/drivers/drv_bmp280_3v3/drv_bmp280_3v3.c @@ -0,0 +1,39 @@ +#include "drv_bmp280_3v3.h" +#include + +#define DRV_BMP280_3V3_CHIP_ADDR 0x76 + +uint8_t drv_bmp280_3v3_init(drv_bmp280_3v3_t *handle) +{ + if (!handle || !handle->drv_i2c_read || !handle->drv_i2c_write) { + return 1; + } + handle->chip_addr = DRV_BMP280_3V3_CHIP_ADDR; + handle->is_drv_init = 1; + handle->drv_state = 1; + return 0; +} + +uint8_t drv_bmp280_3v3_get_pressure(drv_bmp280_3v3_t *handle, uint32_t *presure) +{ + if (!handle) { + return 1; + } + + uint8_t data = 0; + handle->drv_i2c_read(handle->chip_addr, 0x88, &data, 1); + printf("%d\r\n", data); + + return 0; +} + +uint8_t drv_bmp280_3v3_get_tempreture(drv_bmp280_3v3_t *handle, uint32_t *temperature) +{ + return 0; +} + +uint8_t drv_bmp280_3v3_deinit(drv_bmp280_3v3_t *handle) +{ + handle->is_drv_init = 0; + return 0; +} diff --git a/main/drivers/drv_bmp280_3v3/drv_bmp280_3v3.h b/main/drivers/drv_bmp280_3v3/drv_bmp280_3v3.h new file mode 100644 index 0000000..47451a7 --- /dev/null +++ b/main/drivers/drv_bmp280_3v3/drv_bmp280_3v3.h @@ -0,0 +1,27 @@ +#ifndef __DRV_BMP280_3V3_H__ +#define __DRV_BMP280_3V3_H__ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _drv_bmp280_3v3_t_ +{ + uint8_t is_drv_init; + uint8_t drv_state; + uint8_t chip_addr; + uint8_t (*drv_i2c_read)(uint8_t chip_addr, uint8_t reg_addr, uint8_t *data, uint16_t data_len); + uint8_t (*drv_i2c_write)(uint8_t chip_addr, uint8_t reg_addr, uint8_t data); +} drv_bmp280_3v3_t; + +uint8_t drv_bmp280_3v3_init(drv_bmp280_3v3_t *handle); +uint8_t drv_bmp280_3v3_get_pressure(drv_bmp280_3v3_t *handle, uint32_t *presure); +uint8_t drv_bmp280_3v3_get_tempreture(drv_bmp280_3v3_t *handle, uint32_t *temperature); +uint8_t drv_bmp280_3v3_deinit(drv_bmp280_3v3_t *handle); + +#ifdef __cplusplus +} +#endif +#endif//__DRV_BMP280_3V3_H__ \ No newline at end of file