Skip to content

Commit

Permalink
Merge branch 'master' into feature/esp32_s3_korvo_1
Browse files Browse the repository at this point in the history
  • Loading branch information
pborcin committed Nov 21, 2023
2 parents 83255a6 + af5df95 commit b5b3493
Show file tree
Hide file tree
Showing 30 changed files with 2,599 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/upload_component.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
uses: espressif/upload-components-ci-action@v1
with:
directories: >
bsp/esp32_azure_iot_kit;bsp/esp32_s2_kaluga_kit;bsp/esp_wrover_kit;bsp/esp-box;bsp/esp32_s3_usb_otg;bsp/esp32_s3_eye;bsp/esp32_s3_lcd_ev_board;bsp/esp32_s3_korvo_2;bsp/esp-box-lite;bsp/esp32_lyrat;bsp/esp32_c3_lcdkit;bsp/esp-box-3;bsp/esp32_s3_korvo_1;
bsp/esp32_azure_iot_kit;bsp/esp32_s2_kaluga_kit;bsp/esp_wrover_kit;bsp/esp-box;bsp/esp32_s3_usb_otg;bsp/esp32_s3_eye;bsp/esp32_s3_lcd_ev_board;bsp/esp32_s3_korvo_2;bsp/esp-box-lite;bsp/esp32_lyrat;bsp/esp32_c3_lcdkit;bsp/esp-box-3;bsp/esp_bsp_generic;bsp/esp32_s3_korvo_1;
components/bh1750;components/ds18b20;components/es8311;components/es7210;components/fbm320;components/hts221;components/mag3110;components/mpu6050;components/ssd1306;components/esp_lvgl_port;components/icm42670;
components/lcd_touch/esp_lcd_touch;components/lcd_touch/esp_lcd_touch_ft5x06;components/lcd_touch/esp_lcd_touch_gt911;components/lcd_touch/esp_lcd_touch_tt21100;components/lcd_touch/esp_lcd_touch_gt1151;components/lcd_touch/esp_lcd_touch_cst816s;
components/lcd/esp_lcd_gc9a01;components/lcd/esp_lcd_ili9341;components/lcd/esp_lcd_ra8875;components/lcd_touch/esp_lcd_touch_stmpe610;components/lcd/esp_lcd_sh1107;components/lcd/esp_lcd_st7796;components/lcd/esp_lcd_gc9503;components/lcd/esp_lcd_ssd1681;
Expand Down
15 changes: 12 additions & 3 deletions bsp/esp32_s3_usb_otg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#IDF version is less than IDF5.0
if("${IDF_VERSION_MAJOR}.${IDF_VERSION_MINOR}" VERSION_LESS "5.0")
set(SRC_VER "esp32_s3_usb_otg_idf4.c")
set(REQ "")
else()
set(SRC_VER "esp32_s3_usb_otg_idf5.c")
set(REQ esp_adc)
endif()

idf_component_register(
SRCS "esp32_s3_usb_otg.c"
SRCS "esp32_s3_usb_otg.c" ${SRC_VER}
INCLUDE_DIRS "include"
PRIV_INCLUDE_DIRS "priv_include"
REQUIRES driver
PRIV_REQUIRES fatfs esp_lcd usb esp_adc
REQUIRES driver esp_lcd
PRIV_REQUIRES fatfs usb spiffs ${REQ}
)
28 changes: 27 additions & 1 deletion bsp/esp32_s3_usb_otg/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,34 @@ menu "Board Support Package"
help
Error check assert the application before returning the error code.

menu "SPIFFS - Virtual File System"
config BSP_SPIFFS_FORMAT_ON_MOUNT_FAIL
bool "Format SPIFFS if mounting fails"
default n
help
Format SPIFFS if it fails to mount the filesystem.

config BSP_SPIFFS_MOUNT_POINT
string "SPIFFS mount point"
default "/spiffs"
help
Mount point of SPIFFS in the Virtual File System.

config BSP_SPIFFS_PARTITION_LABEL
string "Partition label of SPIFFS"
default "storage"
help
Partition label which stores SPIFFS.

config BSP_SPIFFS_MAX_FILES
int "Max files supported for SPIFFS VFS"
default 5
help
Supported max files for SPIFFS in the Virtual File System.
endmenu

menu "uSD card - Virtual File System"
config BSP_uSD_FORMAT_ON_MOUNT_FAIL
config BSP_SD_FORMAT_ON_MOUNT_FAIL
bool "Format uSD card if mounting fails"
default n
help
Expand Down
177 changes: 120 additions & 57 deletions bsp/esp32_s3_usb_otg/esp32_s3_usb_otg.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -11,10 +11,7 @@
#include "esp_lcd_panel_ops.h"
#include "esp_log.h"
#include "esp_check.h"

#include "esp_adc/adc_oneshot.h"
#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"
#include "esp_spiffs.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Expand All @@ -32,10 +29,36 @@
static const char *TAG = "USB-OTG";

static TaskHandle_t usb_host_task; // USB Host Library task
static adc_oneshot_unit_handle_t adc1_handle; // ADC1 handle; for USB voltage measurement
static adc_cali_handle_t adc1_cali_handle; // ADC1 calibration handle
sdmmc_card_t *bsp_sdcard = NULL; // Global uSD card handler

static const button_config_t bsp_button_config[BSP_BUTTON_NUM] = {
{
.type = BUTTON_TYPE_GPIO,
.gpio_button_config.gpio_num = BSP_BUTTON_OK_IO,
.gpio_button_config.active_level = 0,
},
{
.type = BUTTON_TYPE_GPIO,
.gpio_button_config.gpio_num = BSP_BUTTON_DW_IO,
.gpio_button_config.active_level = 0,
},
{
.type = BUTTON_TYPE_GPIO,
.gpio_button_config.gpio_num = BSP_BUTTON_UP_IO,
.gpio_button_config.active_level = 0,
},
{
.type = BUTTON_TYPE_GPIO,
.gpio_button_config.gpio_num = BSP_BUTTON_MENU_IO,
.gpio_button_config.active_level = 0,
},
{
.type = BUTTON_TYPE_GPIO,
.gpio_button_config.gpio_num = BSP_USB_OVERCURRENT_IO,
.gpio_button_config.active_level = 0,
},
};

esp_err_t bsp_leds_init(void)
{
const gpio_config_t led_io_config = {
Expand All @@ -55,6 +78,39 @@ esp_err_t bsp_led_set(const bsp_led_t led_io, const bool on)
return ESP_OK;
}

esp_err_t bsp_spiffs_mount(void)
{
esp_vfs_spiffs_conf_t conf = {
.base_path = CONFIG_BSP_SPIFFS_MOUNT_POINT,
.partition_label = CONFIG_BSP_SPIFFS_PARTITION_LABEL,
.max_files = CONFIG_BSP_SPIFFS_MAX_FILES,
#ifdef CONFIG_BSP_SPIFFS_FORMAT_ON_MOUNT_FAIL
.format_if_mount_failed = true,
#else
.format_if_mount_failed = false,
#endif
};

esp_err_t ret_val = esp_vfs_spiffs_register(&conf);

BSP_ERROR_CHECK_RETURN_ERR(ret_val);

size_t total = 0, used = 0;
ret_val = esp_spiffs_info(conf.partition_label, &total, &used);
if (ret_val != ESP_OK) {
ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s)", esp_err_to_name(ret_val));
} else {
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
}

return ret_val;
}

esp_err_t bsp_spiffs_unmount(void)
{
return esp_vfs_spiffs_unregister(CONFIG_BSP_SPIFFS_PARTITION_LABEL);
}

esp_err_t bsp_sdcard_mount(void)
{
const esp_vfs_fat_sdmmc_mount_config_t mount_config = {
Expand Down Expand Up @@ -96,7 +152,7 @@ esp_err_t bsp_sdcard_unmount(void)
esp_err_t bsp_button_init(void)
{
const gpio_config_t btn_io_config = {
.pin_bit_mask = BIT64(BSP_BUTTON_DW) | BIT64(BSP_BUTTON_UP) | BIT64(BSP_BUTTON_OK) | BIT64(BSP_BUTTON_MENU) | BIT64(BSP_USB_OVERCURRENT),
.pin_bit_mask = BIT64(BSP_BUTTON_OK_IO) | BIT64(BSP_BUTTON_DW_IO) | BIT64(BSP_BUTTON_UP_IO) | BIT64(BSP_BUTTON_MENU_IO) | BIT64(BSP_USB_OVERCURRENT_IO),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
Expand All @@ -108,7 +164,52 @@ esp_err_t bsp_button_init(void)

bool bsp_button_get(const bsp_button_t btn)
{
return !(bool)gpio_get_level(btn);
gpio_num_t btn_io;
switch (btn) {
case BSP_BUTTON_OK:
btn_io = BSP_BUTTON_OK_IO;
break;
case BSP_BUTTON_DW:
btn_io = BSP_BUTTON_DW_IO;
break;
case BSP_BUTTON_UP:
btn_io = BSP_BUTTON_UP_IO;
break;
case BSP_BUTTON_MENU:
btn_io = BSP_BUTTON_MENU_IO;
break;
case BSP_USB_OVERCURRENT:
btn_io = BSP_USB_OVERCURRENT_IO;
break;
default:
return ESP_FAIL;
}

return !(bool)gpio_get_level(btn_io);
}

esp_err_t bsp_iot_button_create(button_handle_t btn_array[], int *btn_cnt, int btn_array_size)
{
esp_err_t ret = ESP_OK;
if ((btn_array_size < BSP_BUTTON_NUM) ||
(btn_array == NULL)) {
return ESP_ERR_INVALID_ARG;
}

if (btn_cnt) {
*btn_cnt = 0;
}
for (int i = 0; i < BSP_BUTTON_NUM; i++) {
btn_array[i] = iot_button_create(&bsp_button_config[i]);
if (btn_array[i] == NULL) {
ret = ESP_FAIL;
break;
}
if (btn_cnt) {
(*btn_cnt)++;
}
}
return ret;
}

#define LCD_CMD_BITS (8)
Expand Down Expand Up @@ -256,10 +357,18 @@ static lv_disp_t *bsp_display_lcd_init(void)
}

lv_disp_t *bsp_display_start(void)
{
bsp_display_cfg_t cfg = {
.lvgl_port_cfg = ESP_LVGL_PORT_INIT_CONFIG()
};
return bsp_display_start_with_config(&cfg);
}

lv_disp_t *bsp_display_start_with_config(const bsp_display_cfg_t *cfg)
{
lv_disp_t *disp = NULL;
const lvgl_port_cfg_t lvgl_cfg = ESP_LVGL_PORT_INIT_CONFIG();
BSP_ERROR_CHECK_RETURN_NULL(lvgl_port_init(&lvgl_cfg));
assert(cfg != NULL);
BSP_ERROR_CHECK_RETURN_NULL(lvgl_port_init(&cfg->lvgl_port_cfg));
BSP_NULL_CHECK(disp = bsp_display_lcd_init(), NULL);
return disp;
}
Expand Down Expand Up @@ -394,49 +503,3 @@ esp_err_t bsp_usb_host_stop(void)
}
return bsp_usb_host_power_mode(BSP_USB_HOST_POWER_MODE_OFF, false);
}

esp_err_t bsp_voltage_init(void)
{
// Init ADC1
const adc_oneshot_unit_init_cfg_t init_config1 = {
.unit_id = ADC_UNIT_1,
};
BSP_ERROR_CHECK_RETURN_ERR(adc_oneshot_new_unit(&init_config1, &adc1_handle));

// Init ADC1 channels
const adc_oneshot_chan_cfg_t config = {
.bitwidth = ADC_BITWIDTH_DEFAULT,
.atten = ADC_ATTEN_DB_11,
};
BSP_ERROR_CHECK_RETURN_ERR(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_0, &config));
BSP_ERROR_CHECK_RETURN_ERR(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_1, &config));

// ESP32-S3 supports Curve Fitting calibration scheme
const adc_cali_curve_fitting_config_t cali_config = {
.unit_id = ADC_UNIT_1,
.atten = ADC_ATTEN_DB_11,
.bitwidth = ADC_BITWIDTH_DEFAULT,
};
BSP_ERROR_CHECK_RETURN_ERR(adc_cali_create_scheme_curve_fitting(&cali_config, &adc1_cali_handle));
return ESP_OK;
}

int bsp_voltage_battery_get(void)
{
int voltage, adc_raw;

assert(adc1_handle);
BSP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_1, &adc_raw), -1);
BSP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw, &voltage), -1);
return voltage * BSP_BATTERY_VOLTAGE_DIV;
}

int bsp_voltage_usb_get(void)
{
int voltage, adc_raw;

assert(adc1_handle);
BSP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_0, &adc_raw), -1);
BSP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw, &voltage), -1);
return (float)voltage * BSP_USB_HOST_VOLTAGE_DIV;
}
51 changes: 51 additions & 0 deletions bsp/esp32_s3_usb_otg/esp32_s3_usb_otg_idf4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/

#include "esp_err.h"
#include "bsp/esp32_s3_usb_otg.h"
#include "bsp_err_check.h"
#include "driver/adc.h"
#include "esp_adc_cal.h"

static esp_adc_cal_characteristics_t bsp_adc_chars;

esp_err_t bsp_adc_initialize(void)
{
esp_err_t ret = ESP_OK;
BSP_ERROR_CHECK_RETURN_ERR(esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP_FIT));
esp_adc_cal_characterize(BSP_ADC_UNIT, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &bsp_adc_chars);

/* ADC1 config */
BSP_ERROR_CHECK_RETURN_ERR(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));
return ret;
}

esp_err_t bsp_voltage_init(void)
{
BSP_ERROR_CHECK_RETURN_ERR(bsp_adc_initialize());
BSP_ERROR_CHECK_RETURN_ERR(adc1_config_channel_atten(ADC1_CHANNEL_0, ADC_ATTEN_DB_11));
BSP_ERROR_CHECK_RETURN_ERR(adc1_config_channel_atten(ADC1_CHANNEL_5, ADC_ATTEN_DB_11));

return ESP_OK;
}

int bsp_voltage_battery_get(void)
{
int voltage, adc_raw;

adc_raw = adc1_get_raw(ADC1_CHANNEL_5);
voltage = esp_adc_cal_raw_to_voltage(adc_raw, &bsp_adc_chars);
return voltage * BSP_BATTERY_VOLTAGE_DIV;
}

int bsp_voltage_usb_get(void)
{
int voltage, adc_raw;

adc_raw = adc1_get_raw(ADC_CHANNEL_0);
voltage = esp_adc_cal_raw_to_voltage(adc_raw, &bsp_adc_chars);
return voltage * BSP_USB_HOST_VOLTAGE_DIV;
}
Loading

0 comments on commit b5b3493

Please sign in to comment.