|
| 1 | +#include <stdio.h> |
| 2 | + |
| 3 | +// https://github.com/openwch/ch32v307/pull/90 |
| 4 | +// https://github.com/openwch/ch32v20x/pull/12 |
| 5 | +#ifdef __GNUC__ |
| 6 | +#pragma GCC diagnostic push |
| 7 | +#pragma GCC diagnostic ignored "-Wstrict-prototypes" |
| 8 | +#endif |
| 9 | + |
| 10 | +#include "ch32v10x.h" |
| 11 | + |
| 12 | +#ifdef __GNUC__ |
| 13 | +#pragma GCC diagnostic pop |
| 14 | +#endif |
| 15 | + |
| 16 | +#include "bsp/board_api.h" |
| 17 | +#include "board.h" |
| 18 | + |
| 19 | +__attribute__((interrupt)) __attribute__((used)) |
| 20 | +void USBHD_IRQHandler(void) { |
| 21 | + #if CFG_TUD_WCH_USBIP_USBFS |
| 22 | + tud_int_handler(0); |
| 23 | + #endif |
| 24 | +} |
| 25 | + |
| 26 | +__attribute__((interrupt)) __attribute__((used)) |
| 27 | +void USBWakeUp_IRQHandler(void) { |
| 28 | + #if CFG_TUD_WCH_USBIP_USBFS |
| 29 | + tud_int_handler(0); |
| 30 | + #endif |
| 31 | +} |
| 32 | + |
| 33 | +#if CFG_TUSB_OS == OPT_OS_NONE |
| 34 | +volatile uint32_t system_ticks = 0; |
| 35 | + |
| 36 | +__attribute__((interrupt)) __attribute__((used)) |
| 37 | +void SysTick_Handler(void) { |
| 38 | + SysTick->CNTL0 = SysTick->CNTL1 = SysTick->CNTL2 = SysTick->CNTL3 = 0; |
| 39 | + SysTick->CNTH0 = SysTick->CNTH1 = SysTick->CNTH2 = SysTick->CNTH3 = 0; |
| 40 | + system_ticks++; |
| 41 | +} |
| 42 | + |
| 43 | +uint32_t SysTick_Config(uint32_t ticks) { |
| 44 | + NVIC_EnableIRQ(SysTicK_IRQn); |
| 45 | + SysTick->CTLR = 0; |
| 46 | + SysTick->CNTL0 = SysTick->CNTL1 = SysTick->CNTL2 = SysTick->CNTL3 = 0; |
| 47 | + SysTick->CNTH0 = SysTick->CNTH1 = SysTick->CNTH2 = SysTick->CNTH3 = 0; |
| 48 | + |
| 49 | + SysTick->CMPLR0 = (u8)(ticks & 0xFF); |
| 50 | + SysTick->CMPLR1 = (u8)(ticks >> 8); |
| 51 | + SysTick->CMPLR2 = (u8)(ticks >> 16); |
| 52 | + SysTick->CMPLR3 = (u8)(ticks >> 24); |
| 53 | + |
| 54 | + SysTick->CMPHR0 = SysTick->CMPHR1 = SysTick->CMPHR2 = SysTick->CMPHR3 = 0; |
| 55 | + SysTick->CTLR = 1; |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +uint32_t board_millis(void) { |
| 60 | + return system_ticks; |
| 61 | +} |
| 62 | +#endif |
| 63 | + |
| 64 | +void board_init(void) { |
| 65 | + __disable_irq(); |
| 66 | + |
| 67 | +#if CFG_TUSB_OS == OPT_OS_NONE |
| 68 | + SysTick_Config(SystemCoreClock / 1000); |
| 69 | +#endif |
| 70 | + |
| 71 | + RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); |
| 72 | + |
| 73 | + EXTEN->EXTEN_CTR |= EXTEN_USBFS_IO_EN; |
| 74 | + uint8_t usb_div; |
| 75 | + switch (SystemCoreClock) { |
| 76 | + case 48000000: usb_div = RCC_USBCLKSource_PLLCLK_Div1; break; |
| 77 | + case 72000000: usb_div = RCC_USBCLKSource_PLLCLK_1Div5; break; |
| 78 | + default: TU_ASSERT(0,); break; |
| 79 | + } |
| 80 | + RCC_USBCLKConfig(usb_div); |
| 81 | + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USBFS, ENABLE); |
| 82 | + |
| 83 | + #ifdef LED_PIN |
| 84 | + GPIO_InitTypeDef led_init = { |
| 85 | + .GPIO_Pin = LED_PIN, |
| 86 | + .GPIO_Mode = GPIO_Mode_Out_OD, |
| 87 | + .GPIO_Speed = GPIO_Speed_50MHz, |
| 88 | + }; |
| 89 | + GPIO_Init(LED_PORT, &led_init); |
| 90 | + #endif |
| 91 | + |
| 92 | + #ifdef BUTTON_PIN |
| 93 | + GPIO_InitTypeDef button_init = { |
| 94 | + .GPIO_Pin = BUTTON_PIN, |
| 95 | + .GPIO_Mode = GPIO_Mode_IPU, |
| 96 | + .GPIO_Speed = GPIO_Speed_50MHz, |
| 97 | + }; |
| 98 | + GPIO_Init(BUTTON_PORT, &button_init); |
| 99 | + #endif |
| 100 | + |
| 101 | + // UART TX is PA9 |
| 102 | + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); |
| 103 | + GPIO_InitTypeDef usart_init = { |
| 104 | + .GPIO_Pin = GPIO_Pin_9, |
| 105 | + .GPIO_Speed = GPIO_Speed_50MHz, |
| 106 | + .GPIO_Mode = GPIO_Mode_AF_PP, |
| 107 | + }; |
| 108 | + GPIO_Init(GPIOA, &usart_init); |
| 109 | + |
| 110 | + USART_InitTypeDef usart = { |
| 111 | + .USART_BaudRate = 115200, |
| 112 | + .USART_WordLength = USART_WordLength_8b, |
| 113 | + .USART_StopBits = USART_StopBits_1, |
| 114 | + .USART_Parity = USART_Parity_No, |
| 115 | + .USART_Mode = USART_Mode_Tx, |
| 116 | + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, |
| 117 | + }; |
| 118 | + USART_Init(USART1, &usart); |
| 119 | + USART_Cmd(USART1, ENABLE); |
| 120 | + |
| 121 | + __enable_irq(); |
| 122 | + |
| 123 | + board_led_write(true); |
| 124 | +} |
| 125 | + |
| 126 | +void board_led_write(bool state) { |
| 127 | + GPIO_WriteBit(LED_PORT, LED_PIN, state ? LED_STATE_ON : (1-LED_STATE_ON)); |
| 128 | +} |
| 129 | + |
| 130 | +uint32_t board_button_read(void) { |
| 131 | + return BUTTON_STATE_ACTIVE == GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN); |
| 132 | +} |
| 133 | + |
| 134 | +int board_uart_read(uint8_t *buf, int len) { |
| 135 | + (void) buf; |
| 136 | + (void) len; |
| 137 | + return 0; |
| 138 | +} |
| 139 | + |
| 140 | +int board_uart_write(void const *buf, int len) { |
| 141 | + const char *bufc = (const char *) buf; |
| 142 | + for (int i = 0; i < len; i++) { |
| 143 | + while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); |
| 144 | + USART_SendData(USART1, *bufc++); |
| 145 | + } |
| 146 | + |
| 147 | + return len; |
| 148 | +} |
0 commit comments