我有一个基于 esp32s3 的带有 LED 的自定义板。我的第一个任务是运行简单的程序,该程序可以通过 I2C 总线连接到 LED 驱动器IS31FL3236A 和 MCU。 我在互联网上发现了一些在 Arduino 上编写的示例,并尝试使用 std esp32 库将其转换为 c/c++ 来处理 I2C
https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/i2c.html
实际上我不知道我做错了什么,因为我的LED不想工作(在Arduino上写的可以工作)。
配置引脚的连接和配置与 Arduino 上的相同,并且必须良好。 (SDA、SCL、LED)
请看我的程序,还有监视器的日志
节目:
#include "esp_log.h"
#include "driver/i2c.h"
static const char *TAG = "i2c-simple-example";
#define I2C_MASTER_SCL_IO CONFIG_I2C_MASTER_SCL
#define I2C_MASTER_SDA_IO CONFIG_I2C_MASTER_SDA
#define I2C_MASTER_NUM 0
#define I2C_MASTER_FREQ_HZ 400000
#define OUT_CURRENT 0x02
#define LED_DRIVER_ADDR 0x3C
#define IS31FL3236A_UPDATE_REG 0x25
#define IS31FL3236A_SHUTDOWN_REG 0x00
esp_err_t i2c_master_init(void)
{
int i2c_master_port = I2C_MASTER_NUM;
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
i2c_param_config(i2c_master_port, &conf);
return i2c_driver_install(i2c_master_port, conf.mode, 0, 0, 0);
}
void send_data(uint8_t ledDriverRegister, unsigned char data)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
ESP_ERROR_CHECK(i2c_master_start(cmd));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, LED_DRIVER_ADDR, true));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, ledDriverRegister, true));
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, data, true));
ESP_ERROR_CHECK(i2c_master_stop(cmd));
i2c_cmd_link_delete(cmd);
}
void update_shutdown_register_to_normal_operation()
{
send_data(IS31FL3236A_SHUTDOWN_REG, 0x01);
}
void update_register()
{
send_data(IS31FL3236A_UPDATE_REG, 0x00);
}
void init()
{
ESP_LOGI(TAG, "Init current for leds");
for (uint8_t i = 0x26; i <= 0x49; i++)
{
send_data(i, ((OUT_CURRENT << 1) | 0x01)); // enable all leds
}
for (uint8_t i = 0x01; i <= 0x24; i++)
{
send_data(i, 0x00); // set PWM for all leds to 0
}
ESP_LOGI(TAG, "Updated register");
update_register();
update_shutdown_register_to_normal_operation();
}
_Noreturn void app_main(void)
{
ESP_ERROR_CHECK(i2c_master_init());
ESP_LOGI(TAG, "I2C initialized successfully GPIO SDA %d SCL %d", I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO);
init();
send_data(0x8, 0x63); // I my board LED is on OUT on port 8 and value 63 max
update_register();
ESP_LOGI(TAG, "End Test");
vTaskDelay(3000 / portTICK_RATE_MS);
}
日志:
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x1 (POWERON),boot:0x9 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3fce3808,len:0x1664
load:0x403c9700,len:0xbb8
load:0x403cc700,len:0x2e8c
entry 0x403c9954
I (29) boot: ESP-IDF v4.4.3-dirty 2nd stage bootloader
I (29) boot: compile time 22:27:17
I (30) boot: chip revision: 0
I (32) boot.esp32s3: Boot SPI Speed : 40MHz
I (36) boot.esp32s3: SPI Mode : DIO
I (41) boot.esp32s3: SPI Flash Size : 8MB
I (46) boot: Enabling RNG early entropy source...
I (51) boot: Partition Table:
I (55) boot: ## Label Usage Type ST Offset Length
I (62) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (70) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (77) boot: 2 factory factory app 00 00 00010000 00100000
I (84) boot: End of partition table
I (89) esp_image: segment 0: paddr=00010020 vaddr=3c020020 size=088c8h ( 35016) map
I (106) esp_image: segment 1: paddr=000188f0 vaddr=3fc91f90 size=02614h ( 9748) load
I (109) esp_image: segment 2: paddr=0001af0c vaddr=40374000 size=0510ch ( 20748) load
I (120) esp_image: segment 3: paddr=00020020 vaddr=42000020 size=182f8h ( 99064) map
I (147) esp_image: segment 4: paddr=00038320 vaddr=4037910c size=08e84h ( 36484) load
I (158) esp_image: segment 5: paddr=000411ac vaddr=50000000 size=00010h ( 16) load
I (164) boot: Loaded app from partition at offset 0x10000
I (164) boot: Disabling RNG early entropy source...
I (178) cpu_start: Pro cpu up.
I (178) cpu_start: Starting app cpu, entry point is 0x403751e4
0x403751e4: call_start_cpu1 at /home/rafal/esp/esp-idf/components/esp_system/port/cpu_start.c:148
I (0) cpu_start: App cpu up.
I (192) cpu_start: Pro cpu start user code
I (192) cpu_start: cpu freq: 160000000
I (192) cpu_start: Application information:
I (195) cpu_start: Project name: i2c-simple
I (200) cpu_start: App version: v4.4.3-dirty
I (206) cpu_start: Compile time: Nov 25 2022 22:18:32
I (212) cpu_start: ELF file SHA256: a7bcfcfcba18ab90...
I (218) cpu_start: ESP-IDF: v4.4.3-dirty
I (223) heap_init: Initializing. RAM available for dynamic allocation:
I (230) heap_init: At 3FC94F98 len 00054778 (337 KiB): D/IRAM
I (237) heap_init: At 3FCE9710 len 00005724 (21 KiB): STACK/DRAM
I (243) heap_init: At 3FCF0000 len 00008000 (32 KiB): DRAM
I (250) heap_init: At 600FE000 len 00002000 (8 KiB): RTCRAM
I (257) spi_flash: detected chip: generic
I (261) spi_flash: flash io: dio
I (266) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (286) i2c-simple-example: I2C initialized successfully GPIO SDA 10 SCL 9
I (286) i2c-simple-example: Init current for leds
I (296) i2c-simple-example: Updated register
I (296) i2c-simple-example: Set min value for pwm
I (306) i2c-simple-example: End Test