add board_reset_to_bootloader(), try to implement that for ch32v203 but not working yet

This commit is contained in:
hathach
2024-07-05 15:40:02 +07:00
parent 13dedddd19
commit 8d5dbb9577
5 changed files with 57 additions and 2 deletions

View File

@@ -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 // BLINKING TASK
//--------------------------------------------------------------------+ //--------------------------------------------------------------------+

View File

@@ -72,6 +72,9 @@ void board_init(void);
// Init board after tinyusb is initialized // Init board after tinyusb is initialized
void board_init_after_tusb(void) TU_ATTR_WEAK; 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 // Turn LED on or off
void board_led_write(bool state); void board_led_write(bool state);

View File

@@ -6,7 +6,7 @@ extern "C" {
#endif #endif
#define LED_PORT GPIOA #define LED_PORT GPIOA
#define LED_PIN GPIO_Pin_15 #define LED_PIN GPIO_Pin_0
#define LED_STATE_ON 0 #define LED_STATE_ON 0
#define UART_DEV USART1 #define UART_DEV USART1

View File

@@ -7,7 +7,7 @@ extern "C" {
#define LED_PORT GPIOA #define LED_PORT GPIOA
#define LED_PIN GPIO_Pin_0 #define LED_PIN GPIO_Pin_0
#define LED_STATE_ON 1 #define LED_STATE_ON 0
#define UART_DEV USART2 #define UART_DEV USART2
#define UART_CLOCK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) #define UART_CLOCK_EN() RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE)

View File

@@ -139,6 +139,34 @@ void board_init(void) {
__enable_irq(); __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) { void board_led_write(bool state) {
GPIO_WriteBit(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); 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; return len;
} }
//--------------------------------------------------------------------
// Neopixel
//--------------------------------------------------------------------