From 8d5dbb957766921eba78b8762a458e6518521b66 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 5 Jul 2024 15:40:02 +0700 Subject: [PATCH] add board_reset_to_bootloader(), try to implement that for ch32v203 but not working yet --- examples/device/cdc_dual_ports/src/main.c | 20 ++++++++++++ hw/bsp/board_api.h | 3 ++ .../ch32v20x/boards/ch32v203c_r0_1v0/board.h | 2 +- .../ch32v20x/boards/ch32v203g_r0_1v0/board.h | 2 +- hw/bsp/ch32v20x/family.c | 32 +++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/examples/device/cdc_dual_ports/src/main.c b/examples/device/cdc_dual_ports/src/main.c index 1167a5d50..ef12186f2 100644 --- a/examples/device/cdc_dual_ports/src/main.c +++ b/examples/device/cdc_dual_ports/src/main.c @@ -119,6 +119,26 @@ static void cdc_task(void) { } } +// Invoked when cdc when line state changed e.g connected/disconnected +// Use to reset to DFU when disconnect with 1200 bps +void tud_cdc_line_state_cb(uint8_t instance, bool dtr, bool rts) { + (void)rts; + + // DTR = false is counted as disconnected + if (!dtr) { + // touch1200 only with first CDC instance (Serial) + if (instance == 0) { + cdc_line_coding_t coding; + tud_cdc_get_line_coding(&coding); + if (coding.bit_rate == 1200) { + if (board_reset_to_bootloader) { + board_reset_to_bootloader(); + } + } + } + } +} + //--------------------------------------------------------------------+ // BLINKING TASK //--------------------------------------------------------------------+ diff --git a/hw/bsp/board_api.h b/hw/bsp/board_api.h index eee9ed9c5..774deca24 100644 --- a/hw/bsp/board_api.h +++ b/hw/bsp/board_api.h @@ -72,6 +72,9 @@ void board_init(void); // Init board after tinyusb is initialized void board_init_after_tusb(void) TU_ATTR_WEAK; +// Jump to bootloader +void board_reset_to_bootloader(void) TU_ATTR_WEAK; + // Turn LED on or off void board_led_write(bool state); diff --git a/hw/bsp/ch32v20x/boards/ch32v203c_r0_1v0/board.h b/hw/bsp/ch32v20x/boards/ch32v203c_r0_1v0/board.h index 64eaf931e..692cf11bf 100644 --- a/hw/bsp/ch32v20x/boards/ch32v203c_r0_1v0/board.h +++ b/hw/bsp/ch32v20x/boards/ch32v203c_r0_1v0/board.h @@ -6,7 +6,7 @@ extern "C" { #endif #define LED_PORT GPIOA -#define LED_PIN GPIO_Pin_15 +#define LED_PIN GPIO_Pin_0 #define LED_STATE_ON 0 #define UART_DEV USART1 diff --git a/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.h b/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.h index d6c3a64c8..783831edd 100644 --- a/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.h +++ b/hw/bsp/ch32v20x/boards/ch32v203g_r0_1v0/board.h @@ -7,7 +7,7 @@ extern "C" { #define LED_PORT GPIOA #define LED_PIN GPIO_Pin_0 -#define LED_STATE_ON 1 +#define LED_STATE_ON 0 #define UART_DEV USART2 #define UART_CLOCK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) diff --git a/hw/bsp/ch32v20x/family.c b/hw/bsp/ch32v20x/family.c index ea98a5e19..e3075757e 100644 --- a/hw/bsp/ch32v20x/family.c +++ b/hw/bsp/ch32v20x/family.c @@ -139,6 +139,34 @@ void board_init(void) { __enable_irq(); } +void board_reset_to_bootloader(void) { + board_led_write(true); + + __disable_irq(); + +#if CFG_TUD_ENABLED + tud_deinit(0); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USB, ENABLE); + RCC_APB1PeriphResetCmd(RCC_APB1Periph_USB, DISABLE); +#endif + + SysTick->CTLR = 0; + for (int i = WWDG_IRQn; i< DMA1_Channel8_IRQn; i++) { + NVIC_DisableIRQ(i); + } + + __enable_irq(); + + // define function pointer to BOOT ROM address + void (*bootloader_entry)(void) = (void (*)(void))0x1FFF8000; + + bootloader_entry(); + + board_led_write(false); + + // while(1) { } +} + void board_led_write(bool state) { GPIO_WriteBit(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); } @@ -166,3 +194,7 @@ int board_uart_write(void const *buf, int len) { return len; } + +//-------------------------------------------------------------------- +// Neopixel +//--------------------------------------------------------------------