| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2024-06-14 12:51:28 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // https://github.com/openwch/ch32v307/pull/90
 | 
					
						
							|  |  |  | // https://github.com/openwch/ch32v20x/pull/12
 | 
					
						
							|  |  |  | #ifdef __GNUC__
 | 
					
						
							|  |  |  | #pragma GCC diagnostic push
 | 
					
						
							|  |  |  | #pragma GCC diagnostic ignored "-Wstrict-prototypes"
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | #include "ch32v20x.h"
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 12:51:28 +07:00
										 |  |  | #ifdef __GNUC__
 | 
					
						
							|  |  |  | #pragma GCC diagnostic pop
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | #include "bsp/board_api.h"
 | 
					
						
							|  |  |  | #include "board.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  | /* CH32v203 depending on variants can support 2 USB IPs: FSDEV and USBFS.
 | 
					
						
							|  |  |  |  * By default, we use FSDEV, but you can explicitly select by define: | 
					
						
							|  |  |  |  * - CFG_TUD_WCH_USBIP_FSDEV | 
					
						
							|  |  |  |  * - CFG_TUD_WCH_USBIP_USBFS | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // USBFS
 | 
					
						
							|  |  |  | __attribute__((interrupt)) __attribute__((used)) | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | void USBHD_IRQHandler(void) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   #if CFG_TUD_WCH_USBIP_USBFS
 | 
					
						
							|  |  |  |   tud_int_handler(0); | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __attribute__((interrupt)) __attribute__((used)) | 
					
						
							|  |  |  | void USBHDWakeUp_IRQHandler(void) { | 
					
						
							|  |  |  |   #if CFG_TUD_WCH_USBIP_USBFS
 | 
					
						
							|  |  |  |   tud_int_handler(0); | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // USBD (fsdev)
 | 
					
						
							|  |  |  | __attribute__((interrupt)) __attribute__((used)) | 
					
						
							|  |  |  | void USB_LP_CAN1_RX0_IRQHandler(void) { | 
					
						
							|  |  |  |   #if CFG_TUD_WCH_USBIP_FSDEV
 | 
					
						
							|  |  |  |   tud_int_handler(0); | 
					
						
							|  |  |  |   #endif
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  | __attribute__((interrupt)) __attribute__((used)) | 
					
						
							|  |  |  | void USB_HP_CAN1_TX_IRQHandler(void) { | 
					
						
							|  |  |  |   #if CFG_TUD_WCH_USBIP_FSDEV
 | 
					
						
							|  |  |  |   tud_int_handler(0); | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __attribute__((interrupt)) __attribute__((used)) | 
					
						
							|  |  |  | void USBWakeUp_IRQHandler(void) { | 
					
						
							|  |  |  |   #if CFG_TUD_WCH_USBIP_FSDEV
 | 
					
						
							|  |  |  |   tud_int_handler(0); | 
					
						
							|  |  |  |   #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | volatile uint32_t system_ticks = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | __attribute__((interrupt)) | 
					
						
							|  |  |  | void SysTick_Handler(void) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   SysTick->SR = 0; | 
					
						
							|  |  |  |   system_ticks++; | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t SysTick_Config(uint32_t ticks) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   NVIC_EnableIRQ(SysTicK_IRQn); | 
					
						
							|  |  |  |   SysTick->CTLR = 0; | 
					
						
							|  |  |  |   SysTick->SR = 0; | 
					
						
							|  |  |  |   SysTick->CNT = 0; | 
					
						
							|  |  |  |   SysTick->CMP = ticks - 1; | 
					
						
							|  |  |  |   SysTick->CTLR = 0xF; | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t board_millis(void) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   return system_ticks; | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void board_init(void) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   __disable_irq(); | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if CFG_TUSB_OS == OPT_OS_NONE
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   SysTick_Config(SystemCoreClock / 1000); | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   GPIO_InitTypeDef GPIO_InitStructure = { | 
					
						
							|  |  |  |     .GPIO_Pin = LED_PIN, | 
					
						
							|  |  |  |     .GPIO_Mode = GPIO_Mode_Out_OD, | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  |     .GPIO_Speed = GPIO_Speed_10MHz, | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   }; | 
					
						
							|  |  |  |   GPIO_Init(LED_PORT, &GPIO_InitStructure); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  | #ifdef UART_DEV
 | 
					
						
							|  |  |  |   UART_CLOCK_EN(); | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   GPIO_InitTypeDef usart_init = { | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  |     .GPIO_Pin = UART_TX_PIN, | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |     .GPIO_Speed = GPIO_Speed_50MHz, | 
					
						
							|  |  |  |     .GPIO_Mode = GPIO_Mode_AF_PP, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   GPIO_Init(GPIOA, &usart_init); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   USART_InitTypeDef usart = { | 
					
						
							|  |  |  |     .USART_BaudRate = 115200, | 
					
						
							|  |  |  |     .USART_WordLength = USART_WordLength_8b, | 
					
						
							|  |  |  |     .USART_StopBits = USART_StopBits_1, | 
					
						
							|  |  |  |     .USART_Parity = USART_Parity_No, | 
					
						
							|  |  |  |     .USART_Mode = USART_Mode_Tx, | 
					
						
							|  |  |  |     .USART_HardwareFlowControl = USART_HardwareFlowControl_None, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  |   USART_Init(UART_DEV, &usart); | 
					
						
							|  |  |  |   USART_Cmd(UART_DEV, ENABLE); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // USB init
 | 
					
						
							|  |  |  |   uint8_t usb_div; | 
					
						
							|  |  |  |   switch (SystemCoreClock) { | 
					
						
							|  |  |  |     case 48000000: usb_div = RCC_USBCLKSource_PLLCLK_Div1; break; | 
					
						
							|  |  |  |     case 96000000: usb_div = RCC_USBCLKSource_PLLCLK_Div2; break; | 
					
						
							|  |  |  |     case 144000000: usb_div = RCC_USBCLKSource_PLLCLK_Div3; break; | 
					
						
							|  |  |  |     default: TU_ASSERT(0,); break; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   RCC_USBCLKConfig(usb_div); | 
					
						
							|  |  |  |   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);  // FSDEV
 | 
					
						
							|  |  |  |   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE); // USB FS
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   __enable_irq(); | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-05 15:40:02 +07:00
										 |  |  | 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) { }
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | void board_led_write(bool state) { | 
					
						
							| 
									
										
										
										
											2024-06-06 15:49:20 +07:00
										 |  |  |   GPIO_WriteBit(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint32_t board_button_read(void) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   return false; | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int board_uart_read(uint8_t *buf, int len) { | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   (void) buf; | 
					
						
							|  |  |  |   (void) len; | 
					
						
							|  |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int board_uart_write(void const *buf, int len) { | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  | #ifdef UART_DEV
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   const char *bufc = (const char *) buf; | 
					
						
							|  |  |  |   for (int i = 0; i < len; i++) { | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  |     while (USART_GetFlagStatus(UART_DEV, USART_FLAG_TC) == RESET); | 
					
						
							|  |  |  |     USART_SendData(UART_DEV, *bufc++); | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2024-06-21 16:08:37 +07:00
										 |  |  | #else
 | 
					
						
							|  |  |  |   (void) buf; (void) len; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2024-05-24 13:58:44 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return len; | 
					
						
							| 
									
										
										
										
											2023-11-19 18:26:40 -08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-07-05 15:40:02 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------
 | 
					
						
							|  |  |  | // Neopixel
 | 
					
						
							|  |  |  | //--------------------------------------------------------------------
 |