try to add esp32 p4 and esp-idf v5.3 support

This commit is contained in:
hathach 2024-09-25 17:18:41 +07:00
parent bb2d1dd0c1
commit 7e472fa3ca
No known key found for this signature in database
GPG Key ID: 26FAB84F615C3C52
4 changed files with 93 additions and 51 deletions

View File

@ -2,7 +2,7 @@ set(hw_dir "${CMAKE_CURRENT_LIST_DIR}/../../../")
idf_component_register(SRCS family.c idf_component_register(SRCS family.c
INCLUDE_DIRS "." ${BOARD} ${hw_dir} INCLUDE_DIRS "." ${BOARD} ${hw_dir}
PRIV_REQUIRES "driver" PRIV_REQUIRES driver usb
REQUIRES led_strip src tinyusb_src) REQUIRES led_strip src tinyusb_src)
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-error=format) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-error=format)

View File

@ -31,20 +31,9 @@
#include "esp_mac.h" #include "esp_mac.h"
#include "hal/gpio_ll.h" #include "hal/gpio_ll.h"
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
#include "hal/usb_hal.h"
#include "soc/usb_periph.h"
static void configure_pins(usb_hal_context_t* usb);
#endif
#include "driver/gpio.h" #include "driver/gpio.h"
#include "driver/uart.h" #include "driver/uart.h"
#include "esp_private/periph_ctrl.h"
#if ESP_IDF_VERSION_MAJOR > 4
#include "esp_private/periph_ctrl.h"
#else
#include "driver/periph_ctrl.h"
#endif
// Note; current code use UART0 can cause device to reset while monitoring // Note; current code use UART0 can cause device to reset while monitoring
#define USE_UART 0 #define USE_UART 0
@ -60,6 +49,7 @@ static led_strip_handle_t led_strip;
static void max3421_init(void); static void max3421_init(void);
#endif #endif
static bool usb_init(void);
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// Implementation // Implementation
@ -111,16 +101,8 @@ void board_init(void) {
gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT); gpio_set_direction(BUTTON_PIN, GPIO_MODE_INPUT);
gpio_set_pull_mode(BUTTON_PIN, BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN_ONLY : GPIO_PULLUP_ONLY); gpio_set_pull_mode(BUTTON_PIN, BUTTON_STATE_ACTIVE ? GPIO_PULLDOWN_ONLY : GPIO_PULLUP_ONLY);
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3) #if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
// USB Controller Hal init usb_init();
periph_module_reset(PERIPH_USB_MODULE);
periph_module_enable(PERIPH_USB_MODULE);
usb_hal_context_t hal = {
.use_external_phy = false // use built-in PHY
};
usb_hal_init(&hal);
configure_pins(&hal);
#endif #endif
#if CFG_TUH_ENABLED && CFG_TUH_MAX3421 #if CFG_TUH_ENABLED && CFG_TUH_MAX3421
@ -129,35 +111,7 @@ void board_init(void) {
} }
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3) #if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
static void configure_pins(usb_hal_context_t* usb) {
/* usb_periph_iopins currently configures USB_OTG as USB Device.
* Introduce additional parameters in usb_hal_context_t when adding support
* for USB Host. */
for (const usb_iopin_dsc_t* iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
esp_rom_gpio_pad_select_gpio(iopin->pin);
if (iopin->is_output) {
esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
} else {
esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
#if ESP_IDF_VERSION_MAJOR > 4
if ((iopin->pin != GPIO_MATRIX_CONST_ZERO_INPUT) && (iopin->pin != GPIO_MATRIX_CONST_ONE_INPUT))
#else
if ((iopin->pin != GPIO_FUNC_IN_LOW) && (iopin->pin != GPIO_FUNC_IN_HIGH))
#endif
{
gpio_ll_input_enable(&GPIO, iopin->pin);
}
}
esp_rom_gpio_pad_unhold(iopin->pin);
}
}
if (!usb->use_external_phy) {
gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
}
}
#endif #endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
@ -204,6 +158,87 @@ int board_getchar(void) {
return board_uart_read(&c, 1) > 0 ? (int) c : (-1); return board_uart_read(&c, 1) > 0 ? (int) c : (-1);
} }
//--------------------------------------------------------------------
// PHY Init
//--------------------------------------------------------------------
#if TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3, OPT_MCU_ESP32P4)
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0)
#include "esp_private/usb_phy.h"
#include "soc/usb_pins.h"
static usb_phy_handle_t phy_hdl;
bool usb_init(void) {
// Configure USB PHY
usb_phy_config_t phy_conf = {
.controller = USB_PHY_CTRL_OTG,
.target = USB_PHY_TARGET_INT,
.otg_mode = USB_OTG_MODE_DEVICE,
};
// OTG IOs config
// const usb_phy_otg_io_conf_t otg_io_conf = USB_PHY_SELF_POWERED_DEVICE(config->vbus_monitor_io);
// if (config->self_powered) {
// phy_conf.otg_io_conf = &otg_io_conf;
// }
// ESP_RETURN_ON_ERROR(usb_new_phy(&phy_conf, &phy_hdl), TAG, "Install USB PHY failed");
usb_new_phy(&phy_conf, &phy_hdl);
return true;
}
#else
#include "esp_private/usb_phy.h"
#include "hal/usb_hal.h"
#include "soc/usb_periph.h"
static void configure_pins(usb_hal_context_t* usb) {
/* usb_periph_iopins currently configures USB_OTG as USB Device.
* Introduce additional parameters in usb_hal_context_t when adding support
* for USB Host. */
for (const usb_iopin_dsc_t* iopin = usb_periph_iopins; iopin->pin != -1; ++iopin) {
if ((usb->use_external_phy) || (iopin->ext_phy_only == 0)) {
esp_rom_gpio_pad_select_gpio(iopin->pin);
if (iopin->is_output) {
esp_rom_gpio_connect_out_signal(iopin->pin, iopin->func, false, false);
} else {
esp_rom_gpio_connect_in_signal(iopin->pin, iopin->func, false);
if ((iopin->pin != GPIO_MATRIX_CONST_ZERO_INPUT) && (iopin->pin != GPIO_MATRIX_CONST_ONE_INPUT)) {
gpio_ll_input_enable(&GPIO, iopin->pin);
}
}
esp_rom_gpio_pad_unhold(iopin->pin);
}
}
if (!usb->use_external_phy) {
gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
}
}
bool usb_init(void) {
// USB Controller Hal init
periph_module_reset(PERIPH_USB_MODULE);
periph_module_enable(PERIPH_USB_MODULE);
usb_hal_context_t hal = {
.use_external_phy = false // use built-in PHY
};
usb_hal_init(&hal);
configure_pins(&hal);
return true;
}
#endif
#endif
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+
// API: SPI transfer with MAX3421E, must be implemented by application // API: SPI transfer with MAX3421E, must be implemented by application
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@ -339,6 +339,12 @@
#define TUP_USBIP_DWC2_ESP32 #define TUP_USBIP_DWC2_ESP32
#define TUP_DCD_ENDPOINT_MAX 6 #define TUP_DCD_ENDPOINT_MAX 6
#elif TU_CHECK_MCU(OPT_MCU_ESP32P4)
#define TUP_USBIP_DWC2
#define TUP_USBIP_DWC2_ESP32
#define TUP_RHPORT_HIGHSPEED 1 // 1 port FS, 1 port HS
#define TUP_DCD_ENDPOINT_MAX 8 // FS 6 ep, HS 8 ep
#elif TU_CHECK_MCU(OPT_MCU_ESP32, OPT_MCU_ESP32C2, OPT_MCU_ESP32C3, OPT_MCU_ESP32C6, OPT_MCU_ESP32H2) #elif TU_CHECK_MCU(OPT_MCU_ESP32, OPT_MCU_ESP32C2, OPT_MCU_ESP32C3, OPT_MCU_ESP32C6, OPT_MCU_ESP32H2)
#if (CFG_TUD_ENABLED || !(defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421)) #if (CFG_TUD_ENABLED || !(defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421))
#error "MCUs are only supported with CFG_TUH_MAX3421 enabled" #error "MCUs are only supported with CFG_TUH_MAX3421 enabled"

View File

@ -123,6 +123,7 @@
#define OPT_MCU_ESP32C6 904 ///< Espressif ESP32-C6 #define OPT_MCU_ESP32C6 904 ///< Espressif ESP32-C6
#define OPT_MCU_ESP32C2 905 ///< Espressif ESP32-C2 #define OPT_MCU_ESP32C2 905 ///< Espressif ESP32-C2
#define OPT_MCU_ESP32H2 906 ///< Espressif ESP32-H2 #define OPT_MCU_ESP32H2 906 ///< Espressif ESP32-H2
#define OPT_MCU_ESP32P4 907 ///< Espressif ESP32-P4
#define TUP_MCU_ESPRESSIF (CFG_TUSB_MCU >= 900 && CFG_TUSB_MCU < 1000) // check if Espressif MCU #define TUP_MCU_ESPRESSIF (CFG_TUSB_MCU >= 900 && CFG_TUSB_MCU < 1000) // check if Espressif MCU
// Dialog // Dialog