fix: 温度传感器扫描不到。

This commit is contained in:
Ivan Li 2023-03-04 16:24:35 +08:00
parent 4b716751a2
commit bce9548aba
7 changed files with 76 additions and 42 deletions

View File

@ -1,2 +1,2 @@
idf_component_register(SRCS "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" idf_component_register(SRCS "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c"
INCLUDE_DIRS ".") INCLUDE_DIRS ".")

View File

@ -6,15 +6,6 @@
#include "esp_log.h" #include "esp_log.h"
#include "i2c.c" #include "i2c.c"
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */
#define I2C_MASTER_NUM CONFIG_I2C_NUM
#define APDS_9930_ADDRESS 0x39
#define APDS_9930_CMD_REPEATED 0x80 // 命令重复地址 #define APDS_9930_CMD_REPEATED 0x80 // 命令重复地址
#define APDS_9930_CMD_AUTO_INCREMENT 0x90 // 命令自动递增地址 #define APDS_9930_CMD_AUTO_INCREMENT 0x90 // 命令自动递增地址
@ -45,8 +36,6 @@
#define AMBIENT_LIGHT_TAG "ambient-light" #define AMBIENT_LIGHT_TAG "ambient-light"
static int8_t is_apds_9930_online = 0;
esp_err_t apds_9930_write(uint8_t command, uint8_t data) { esp_err_t apds_9930_write(uint8_t command, uint8_t data) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
@ -193,10 +182,8 @@ void ambient_light_auto_fetch() {
} }
void ambient_light_init() { void ambient_light_init() {
if (i2c_check_slave_exists(APDS_9930_ADDRESS)) { if (is_apds_9930_online == 0) {
is_apds_9930_online = 1; ESP_LOGI(AMBIENT_LIGHT_TAG, "AMG8833 is offline");
} else {
is_apds_9930_online = 0;
return; return;
} }
ESP_LOGI(AMBIENT_LIGHT_TAG, "Initializing APDS-9930"); ESP_LOGI(AMBIENT_LIGHT_TAG, "Initializing APDS-9930");

View File

@ -13,15 +13,10 @@
#define X_WIDTH 128 #define X_WIDTH 128
#define Y_WIDTH 64 #define Y_WIDTH 64
#define I2C_ADDRESS 0x78
#define I2C_MASTER_NUM CONFIG_I2C_NUM
static uint8_t is_embedded_display_online = 0;
void i2cWriteByte(uint8_t reg, uint8_t data) { void i2cWriteByte(uint8_t reg, uint8_t data) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
i2c_master_write_byte(cmd, I2C_ADDRESS | I2C_MASTER_WRITE, true); i2c_master_write_byte(cmd, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, reg, true); i2c_master_write_byte(cmd, reg, true);
i2c_master_write_byte(cmd, data, true); i2c_master_write_byte(cmd, data, true);
i2c_master_stop(cmd); i2c_master_stop(cmd);
@ -78,10 +73,8 @@ void display_print8_str(uint8_t x, uint8_t y, char str[]) {
} }
void init_display() { void init_display() {
if (i2c_check_slave_exists(I2C_ADDRESS >> 1)) { if (is_embedded_display_online == 0) {
is_embedded_display_online = 1; ESP_LOGE("display", "display is offline");
} else {
is_embedded_display_online = 0;
return; return;
} }

View File

@ -8,11 +8,27 @@
#define I2C_MASTER_SDA_IO CONFIG_I2C_SDA #define I2C_MASTER_SDA_IO CONFIG_I2C_SDA
#define I2C_MASTER_SCL_IO CONFIG_I2C_SCL #define I2C_MASTER_SCL_IO CONFIG_I2C_SCL
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ #define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
#define I2C_MASTER_NUM CONFIG_I2C_NUM #define I2C_MASTER_NUM CONFIG_I2C_NUM
// 0 1 0 0 A2 A1 A0
#define PCA9555_ADDRESS 0x20
#define GX21M15_ADDRESS 0x48
#define APDS_9930_ADDRESS 0x39
#define I2C_ADDRESS 0x3c
static const char *I2C_TAG = "APP_I2C"; static const char *I2C_TAG = "APP_I2C";
static uint8_t is_temperature_online = 0;
static uint8_t is_pca9555_online = 0;
static uint8_t is_apds_9930_online = 0;
static uint8_t is_embedded_display_online = 0;
void init_i2c() { void init_i2c() {
i2c_config_t conf = { i2c_config_t conf = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
@ -22,10 +38,10 @@ void init_i2c() {
.scl_pullup_en = GPIO_PULLUP_ENABLE, .scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ, .master.clk_speed = I2C_MASTER_FREQ_HZ,
}; };
i2c_param_config(I2C_MASTER_NUM, &conf);
ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode,
I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_RX_BUF_DISABLE,
I2C_MASTER_TX_BUF_DISABLE, 0)); I2C_MASTER_TX_BUF_DISABLE, 0));
i2c_param_config(I2C_MASTER_NUM, &conf);
ESP_LOGI(I2C_TAG, "I2C initialized"); ESP_LOGI(I2C_TAG, "I2C initialized");
} }
@ -34,16 +50,26 @@ uint8_t i2c_check_slave_exists(uint8_t address) {
ESP_LOGI(I2C_TAG, "Checking if slave 0x%2x exists", address); ESP_LOGI(I2C_TAG, "Checking if slave 0x%2x exists", address);
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 1); i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN);
i2c_master_stop(cmd); i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS); esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd); i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) { if (ret == ESP_OK) {
ESP_LOGW(I2C_TAG, "Slave 0x%2x found", address); ESP_LOGW(I2C_TAG, "Slave 0x%2x found", address);
return 1; return 1;
} else if (ret == ESP_ERR_TIMEOUT) {
ESP_LOGW(I2C_TAG, "Slave 0x%2x TIMEOUT", address);
return 1;
} else { } else {
ESP_LOGW(I2C_TAG, "Slave 0x%2x not found", address); ESP_LOGW(I2C_TAG, "Slave 0x%2x not found", address);
return 0; return 0;
} }
}
void i2c_check_slaves() {
is_temperature_online = i2c_check_slave_exists(GX21M15_ADDRESS);
is_pca9555_online = i2c_check_slave_exists(PCA9555_ADDRESS);
is_apds_9930_online = i2c_check_slave_exists(APDS_9930_ADDRESS);
is_embedded_display_online = i2c_check_slave_exists(I2C_ADDRESS);
} }

View File

@ -7,6 +7,7 @@
#include "i2c.c" #include "i2c.c"
#include "light.c" #include "light.c"
#include "mqtt.c" #include "mqtt.c"
#include "pca9555.c"
#include "sdkconfig.h" #include "sdkconfig.h"
#include "temperature.c" #include "temperature.c"
#include "ui_input.c" #include "ui_input.c"
@ -17,12 +18,14 @@ static const char *TAG = "DisplayAmbientLight";
void app_main(void) { void app_main(void) {
light_init_strip(); light_init_strip();
init_i2c(); init_i2c();
i2c_check_slaves();
init_display(); init_display();
display_print8_str(0, 0, "Ambient Light"); display_print8_str(0, 0, "Ambient Light");
ci_03t_init(); ci_03t_init();
ambient_light_init(); ambient_light_init();
ambient_light_auto_fetch(); ambient_light_auto_fetch();
auto_fetch_temperature(); auto_fetch_temperature();
pca9555_init();
ui_input_init(); ui_input_init();
xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10, xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10,
NULL); NULL);

32
main/pca9555.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>
#include "driver/i2c.h"
#include "embedded_display.c"
#include "esp_log.h"
#include "i2c.c"
#define PCA_9555_TAG "PCA9555"
// Register of command byte
#define PCA95555_CMD_INPUT_PORT_0 0x00
#define PCA95555_CMD_INPUT_PORT_1 0x01
#define PCA95555_CMD_OUTPUT_PORT_0 0x02
#define PCA95555_CMD_OUTPUT_PORT_1 0x03
#define PCA95555_CMD_POLARITY_INVERSION_PORT_0 0x04
#define PCA95555_CMD_POLARITY_INVERSION_PORT_1 0x05
#define PCA95555_CMD_CONFIGURATION_PORT_0 0x06
#define PCA95555_CMD_CONFIGURATION_PORT_1 0x07
void pca9555_init() {
if (is_pca9555_online == 0) {
ESP_LOGE(PCA_9555_TAG, "salve offline");
return;
}
ESP_LOGI(PCA_9555_TAG, "Initializing PCA9555");
// esp_log_level_set(PCA_9555_TAG, ESP_LOG_ERROR);
}

View File

@ -11,9 +11,6 @@
#define ACK_VAL 0x0 /*!< I2C ack value */ #define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */ #define NACK_VAL 0x1 /*!< I2C nack value */
#define I2C_MASTER_NUM CONFIG_I2C_NUM
#define GX21M15_ADDRESS 0x90
#define GX21M15_TEMP_POINTER_VALUE 0b00 // 温度寄存器 #define GX21M15_TEMP_POINTER_VALUE 0b00 // 温度寄存器
#define GX21M15_CONF_POINTER_VALUE 0b01 // 配置寄存器 #define GX21M15_CONF_POINTER_VALUE 0b01 // 配置寄存器
#define GX21M15_THYST_POINTER_VALUE 0b10 // 回滞寄存器 #define GX21M15_THYST_POINTER_VALUE 0b10 // 回滞寄存器
@ -22,8 +19,6 @@
#define DEFAULT_TEMPERATURE -999 // 过温关断寄存器 #define DEFAULT_TEMPERATURE -999 // 过温关断寄存器
#define TEMPERATURE_TAG "temperature" #define TEMPERATURE_TAG "temperature"
static uint8_t is_temperature_online = 0;
void fetch_temperature(void* arg) { void fetch_temperature(void* arg) {
esp_err_t error; esp_err_t error;
float temperature = DEFAULT_TEMPERATURE; float temperature = DEFAULT_TEMPERATURE;
@ -33,11 +28,11 @@ void fetch_temperature(void* arg) {
for (;;) { for (;;) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd); i2c_master_start(cmd);
i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_WRITE, i2c_master_write_byte(cmd, GX21M15_ADDRESS << 1 | I2C_MASTER_WRITE,
ACK_CHECK_EN); ACK_CHECK_EN);
i2c_master_write_byte(cmd, 0, ACK_CHECK_EN); i2c_master_write_byte(cmd, 0, ACK_CHECK_EN);
i2c_master_start(cmd); i2c_master_start(cmd);
i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_READ, ACK_VAL); i2c_master_write_byte(cmd, GX21M15_ADDRESS << 1 | I2C_MASTER_READ, ACK_VAL);
i2c_master_read_byte(cmd, &(temperature_buffer[0]), ACK_VAL); i2c_master_read_byte(cmd, &(temperature_buffer[0]), ACK_VAL);
i2c_master_read_byte(cmd, &(temperature_buffer[1]), NACK_VAL); i2c_master_read_byte(cmd, &(temperature_buffer[1]), NACK_VAL);
i2c_master_stop(cmd); i2c_master_stop(cmd);
@ -73,12 +68,10 @@ void fetch_temperature(void* arg) {
} }
void auto_fetch_temperature() { void auto_fetch_temperature() {
// if (i2c_check_slave_exists(GX21M15_ADDRESS >> 1)) { if (is_temperature_online == 0) {
// is_temperature_online = 1; ESP_LOGW(TEMPERATURE_TAG, "temperature is offline");
// } else { return;
// is_temperature_online = 0; }
// return;
// }
ESP_LOGI(TEMPERATURE_TAG, "auto_fetch_temperature"); ESP_LOGI(TEMPERATURE_TAG, "auto_fetch_temperature");
xTaskCreate(fetch_temperature, "temperature", 2048, NULL, 10, NULL); xTaskCreate(fetch_temperature, "temperature", 2048, NULL, 10, NULL);
} }