@@ -20,59 +20,56 @@ manufacturer: WCH
2020#include "bsp/board_api.h"
2121#include "board.h"
2222
23- /* CH32v203 depending on variants can support 2 USB IPs: FSDEV and USBFS.
23+ /* CH32v203 depending on variants can support 2 USB IPs: FSDEV (port0) and USBFS (port1) .
2424 * By default, we use FSDEV, but you can explicitly select by define:
2525 * - CFG_TUD_WCH_USBIP_FSDEV
2626 * - CFG_TUD_WCH_USBIP_USBFS
2727 */
2828
29- // USBFS
30- __attribute__((interrupt )) __attribute__((used ))
31- void USBHD_IRQHandler (void ) {
32- #if CFG_TUD_WCH_USBIP_USBFS
29+ // Port0: USBD (fsdev)
30+ __attribute__((interrupt )) __attribute__((used )) void USB_LP_CAN1_RX0_IRQHandler (void ) {
31+ #if CFG_TUD_WCH_USBIP_FSDEV
3332 tud_int_handler (0 );
3433 #endif
35- #if defined(CFG_TUH_WCH_USBIP_USBFS ) && CFG_TUH_WCH_USBIP_USBFS
36- tuh_int_handler (0 );
37- #endif
3834}
3935
40- __attribute__((interrupt )) __attribute__((used ))
41- void USBHDWakeUp_IRQHandler (void ) {
42- #if CFG_TUD_WCH_USBIP_USBFS
36+ __attribute__((interrupt )) __attribute__((used )) void USB_HP_CAN1_TX_IRQHandler (void ) {
37+ #if CFG_TUD_WCH_USBIP_FSDEV
4338 tud_int_handler (0 );
4439 #endif
40+
4541}
4642
47- // USBD (fsdev)
48- __attribute__((interrupt )) __attribute__((used ))
49- void USB_LP_CAN1_RX0_IRQHandler (void ) {
43+ __attribute__((interrupt )) __attribute__((used )) void USBWakeUp_IRQHandler (void ) {
5044 #if CFG_TUD_WCH_USBIP_FSDEV
5145 tud_int_handler (0 );
5246 #endif
5347}
5448
55- __attribute__(( interrupt )) __attribute__(( used ))
56- void USB_HP_CAN1_TX_IRQHandler (void ) {
57- #if CFG_TUD_WCH_USBIP_FSDEV
58- tud_int_handler (0 );
49+ // Port1: USBFS
50+ __attribute__(( interrupt )) __attribute__(( used )) void USBHD_IRQHandler (void ) {
51+ #if CFG_TUD_ENABLED && CFG_TUD_WCH_USBIP_USBFS
52+ tud_int_handler (1 );
5953 #endif
6054
55+ #if CFG_TUH_ENABLED
56+ tuh_int_handler (1 );
57+ #endif
6158}
6259
63- __attribute__((interrupt )) __attribute__((used ))
64- void USBWakeUp_IRQHandler (void ) {
65- #if CFG_TUD_WCH_USBIP_FSDEV
60+ __attribute__((interrupt )) __attribute__((used )) void USBHDWakeUp_IRQHandler (void ) {
61+ #if CFG_TUD_WCH_USBIP_USBFS
6662 tud_int_handler (0 );
6763 #endif
6864}
6965
70-
66+ //--------------------------------------------------------------------+
67+ // Board API
68+ //--------------------------------------------------------------------+
7169#if CFG_TUSB_OS == OPT_OS_NONE
7270volatile uint32_t system_ticks = 0 ;
7371
74- __attribute__((interrupt ))
75- void SysTick_Handler (void ) {
72+ __attribute__((interrupt )) void SysTick_Handler (void ) {
7673 SysTick -> SR = 0 ;
7774 system_ticks ++ ;
7875}
@@ -111,7 +108,7 @@ void board_init(void) {
111108#ifdef UART_DEV
112109 UART_CLOCK_EN ();
113110 GPIO_InitTypeDef usart_init = {
114- .GPIO_Pin = UART_TX_PIN ,
111+ .GPIO_Pin = UART_TX_PIN | UART_RX_PIN ,
115112 .GPIO_Speed = GPIO_Speed_50MHz ,
116113 .GPIO_Mode = GPIO_Mode_AF_PP ,
117114 };
@@ -122,7 +119,7 @@ void board_init(void) {
122119 .USART_WordLength = USART_WordLength_8b ,
123120 .USART_StopBits = USART_StopBits_1 ,
124121 .USART_Parity = USART_Parity_No ,
125- .USART_Mode = USART_Mode_Tx ,
122+ .USART_Mode = USART_Mode_Tx | USART_Mode_Rx ,
126123 .USART_HardwareFlowControl = USART_HardwareFlowControl_None ,
127124 };
128125 USART_Init (UART_DEV , & usart );
@@ -192,9 +189,19 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) {
192189}
193190
194191int board_uart_read (uint8_t * buf , int len ) {
195- (void ) buf ;
196- (void ) len ;
192+ #ifdef UART_DEV
193+ int count ;
194+ for (count = 0 ; count < len ; count ++ ) {
195+ if (USART_GetFlagStatus (UART_DEV , USART_FLAG_RXNE ) == RESET ) {
196+ break ;
197+ }
198+ buf [count ] = USART_ReceiveData (UART_DEV );
199+ }
200+ return count ;
201+ #else
202+ (void ) buf ; (void ) len ;
197203 return 0 ;
204+ #endif
198205}
199206
200207int board_uart_write (void const * buf , int len ) {
@@ -210,7 +217,3 @@ int board_uart_write(void const *buf, int len) {
210217
211218 return len ;
212219}
213-
214- //--------------------------------------------------------------------
215- // Neopixel
216- //--------------------------------------------------------------------
0 commit comments