117 lines
3.7 KiB
C
117 lines
3.7 KiB
C
#include "esp32_s3_cam_ovxxxx.h"
|
|
#include "esp_log.h"
|
|
#include "driver/i2c_master.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
|
|
static const char *TAG = "BSP_BOARD";
|
|
static esp_io_expander_handle_t io_expander = NULL;
|
|
static i2c_master_bus_handle_t i2c_bus_handle = NULL;
|
|
|
|
// تعریف قطعی پینهای I2C طبق شماتیک برای جلوگیری از خطای ماکروها
|
|
#define ACTUAL_I2C_SDA 8
|
|
#define ACTUAL_I2C_SCL 7
|
|
|
|
static void i2c_scan(void)
|
|
{
|
|
ESP_LOGI("I2C_SCAN", "Scanning I2C bus...");
|
|
|
|
i2c_master_bus_handle_t bus = bsp_i2c_get_handle();
|
|
|
|
for (uint8_t addr = 1; addr < 127; addr++) {
|
|
|
|
esp_err_t ret = i2c_master_probe(
|
|
bus,
|
|
addr,
|
|
100
|
|
);
|
|
|
|
if (ret == ESP_OK) {
|
|
ESP_LOGI("I2C_SCAN", "Found device at 0x%02X", addr);
|
|
}
|
|
}
|
|
|
|
ESP_LOGI("I2C_SCAN", "I2C scan complete");
|
|
}
|
|
|
|
esp_err_t bsp_i2c_init(void)
|
|
{
|
|
ESP_LOGW(TAG, "--- 1. Starting I2C Master Init ---");
|
|
ESP_LOGI(TAG, "Configuring I2C on SDA: %d, SCL: %d", ACTUAL_I2C_SDA, ACTUAL_I2C_SCL);
|
|
|
|
i2c_master_bus_config_t i2c_conf = {
|
|
.i2c_port = 0,
|
|
.sda_io_num = ACTUAL_I2C_SDA,
|
|
.scl_io_num = ACTUAL_I2C_SCL,
|
|
.clk_source = I2C_CLK_SRC_DEFAULT,
|
|
.glitch_ignore_cnt = 7,
|
|
.flags.enable_internal_pullup = true,
|
|
};
|
|
|
|
esp_err_t ret = i2c_new_master_bus(&i2c_conf, &i2c_bus_handle);
|
|
if (ret == ESP_OK) {
|
|
ESP_LOGI(TAG, "I2C Bus Init: SUCCESS");
|
|
i2c_scan();
|
|
} else {
|
|
ESP_LOGE(TAG, "I2C Bus Init: FAILED (%s)", esp_err_to_name(ret));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
esp_err_t bsp_io_expander_init(void)
|
|
{
|
|
ESP_LOGW(TAG, "--- 2. Starting CH32V003 Expander Init ---");
|
|
if (i2c_bus_handle == NULL) return ESP_FAIL;
|
|
|
|
esp_err_t ret = custom_io_expander_new_i2c_ch32v003(i2c_bus_handle, BSP_IO_EXPANDER_I2C_ADDRESS_CH32V003, &io_expander);
|
|
if (ret != ESP_OK) return ret;
|
|
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_3, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_3, 0);
|
|
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_0, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_0, 1);
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_1, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_1, 1);
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_2, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_2, 1);
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_4, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_4, 1);
|
|
|
|
// ۳. قفل تغذیه باتری
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_5, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_5, 1);
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_6, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_6, 1);
|
|
esp_io_expander_set_dir(io_expander, IO_EXPANDER_PIN_NUM_7, IO_EXPANDER_OUTPUT);
|
|
esp_io_expander_set_level(io_expander, IO_EXPANDER_PIN_NUM_7, 1);
|
|
|
|
// force delay for the audio ics booting
|
|
vTaskDelay(pdMS_TO_TICKS(150));
|
|
|
|
ESP_LOGI(TAG, "Hardware Awake & Power Latch applied.");
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t bsp_i2c_deinit(void)
|
|
{
|
|
if (i2c_bus_handle) {
|
|
return i2c_del_master_bus(i2c_bus_handle);
|
|
}
|
|
return ESP_OK;
|
|
}
|
|
|
|
i2c_master_bus_handle_t bsp_i2c_get_handle(void)
|
|
{
|
|
return i2c_bus_handle;
|
|
}
|
|
|
|
esp_io_expander_handle_t bsp_get_io_expander_handle(void)
|
|
{
|
|
return io_expander;
|
|
}
|
|
|
|
uint16_t bsp_get_io_expander_adc(void)
|
|
{
|
|
return 0;
|
|
} |