2022-03-21 10:01:11 +10:30
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2022 Greg Davill
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of the TinyUSB stack.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2022-03-21 10:01:11 +10:30
|
|
|
#include "debug_uart.h"
|
|
|
|
#include "ch32v30x.h"
|
|
|
|
|
2024-06-14 12:51:28 +07:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2023-08-03 15:50:52 +07:00
|
|
|
#include "bsp/board_api.h"
|
2023-01-12 15:19:26 +07:00
|
|
|
#include "board.h"
|
2022-03-21 10:01:11 +10:30
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Forward USB interrupt events to TinyUSB IRQ Handler
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
// TODO maybe having FS as port0, HS as port1
|
2022-03-21 10:01:11 +10:30
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
__attribute__((interrupt)) void USBHS_IRQHandler(void) {
|
2024-05-24 11:38:44 +07:00
|
|
|
#if CFG_TUD_WCH_USBIP_USBHS
|
2023-01-12 15:19:26 +07:00
|
|
|
tud_int_handler(0);
|
2024-05-20 17:26:04 +07:00
|
|
|
#endif
|
2022-03-21 10:01:11 +10:30
|
|
|
}
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
__attribute__((interrupt)) void OTG_FS_IRQHandler(void) {
|
2024-05-24 11:38:44 +07:00
|
|
|
#if CFG_TUD_WCH_USBIP_USBFS
|
2024-05-17 16:13:59 +07:00
|
|
|
tud_int_handler(0);
|
2024-05-20 17:26:04 +07:00
|
|
|
#endif
|
2024-05-17 16:13:59 +07:00
|
|
|
}
|
|
|
|
|
2022-03-21 10:01:11 +10:30
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// MACRO TYPEDEF CONSTANT ENUM
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
uint32_t SysTick_Config(uint32_t ticks) {
|
2023-01-12 15:19:26 +07:00
|
|
|
NVIC_EnableIRQ(SysTicK_IRQn);
|
2024-05-20 17:26:04 +07:00
|
|
|
SysTick->CTLR = 0;
|
|
|
|
SysTick->SR = 0;
|
|
|
|
SysTick->CNT = 0;
|
|
|
|
SysTick->CMP = ticks - 1;
|
|
|
|
SysTick->CTLR = 0xF;
|
2023-01-12 15:19:26 +07:00
|
|
|
return 0;
|
2022-03-21 10:01:11 +10:30
|
|
|
}
|
|
|
|
|
|
|
|
void board_init(void) {
|
|
|
|
|
|
|
|
/* Disable interrupts during init */
|
|
|
|
__disable_irq();
|
|
|
|
|
|
|
|
#if CFG_TUSB_OS == OPT_OS_NONE
|
|
|
|
SysTick_Config(SystemCoreClock / 1000);
|
|
|
|
#endif
|
|
|
|
|
2024-05-21 16:08:27 +07:00
|
|
|
usart_printf_init(CFG_BOARD_UART_BAUDRATE);
|
2022-03-21 10:01:11 +10:30
|
|
|
|
2024-05-24 11:38:44 +07:00
|
|
|
#ifdef CH32V30x_D8C
|
|
|
|
// v305/v307: Highspeed USB
|
2022-03-21 10:01:11 +10:30
|
|
|
RCC_USBCLK48MConfig(RCC_USBCLK48MCLKSource_USBPHY);
|
|
|
|
RCC_USBHSPLLCLKConfig(RCC_HSBHSPLLCLKSource_HSE);
|
|
|
|
RCC_USBHSConfig(RCC_USBPLL_Div2);
|
|
|
|
RCC_USBHSPLLCKREFCLKConfig(RCC_USBHSPLLCKREFCLK_4M);
|
|
|
|
RCC_USBHSPHYPLLALIVEcmd(ENABLE);
|
|
|
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USBHS, ENABLE);
|
2024-05-24 11:38:44 +07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Fullspeed USB
|
2024-05-17 16:13:59 +07:00
|
|
|
uint8_t otg_div;
|
|
|
|
switch (SystemCoreClock) {
|
|
|
|
case 48000000: otg_div = RCC_OTGFSCLKSource_PLLCLK_Div1; break;
|
|
|
|
case 96000000: otg_div = RCC_OTGFSCLKSource_PLLCLK_Div2; break;
|
|
|
|
case 144000000: otg_div = RCC_OTGFSCLKSource_PLLCLK_Div3; break;
|
|
|
|
default: TU_ASSERT(0,); break;
|
|
|
|
}
|
|
|
|
RCC_OTGFSCLKConfig(otg_div);
|
|
|
|
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
|
|
|
|
|
2022-03-21 10:01:11 +10:30
|
|
|
GPIO_InitTypeDef GPIO_InitStructure = {0};
|
|
|
|
|
2023-01-12 15:19:26 +07:00
|
|
|
// LED
|
|
|
|
LED_CLOCK_EN();
|
|
|
|
GPIO_InitStructure.GPIO_Pin = LED_PIN;
|
2022-03-21 10:01:11 +10:30
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
2023-01-12 15:19:26 +07:00
|
|
|
GPIO_Init(LED_PORT, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
// Button
|
|
|
|
BUTTON_CLOCK_EN();
|
|
|
|
GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
GPIO_Init(BUTTON_PORT, &GPIO_InitStructure);
|
2022-03-21 10:01:11 +10:30
|
|
|
|
2023-01-08 00:02:17 +07:00
|
|
|
/* Enable interrupts globally */
|
2022-03-21 10:01:11 +10:30
|
|
|
__enable_irq();
|
|
|
|
|
|
|
|
board_delay(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CFG_TUSB_OS == OPT_OS_NONE
|
|
|
|
volatile uint32_t system_ticks = 0;
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
__attribute__((interrupt)) void SysTick_Handler(void) {
|
2023-01-12 15:34:29 +07:00
|
|
|
SysTick->SR = 0;
|
2022-03-21 10:01:11 +10:30
|
|
|
system_ticks++;
|
|
|
|
}
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
uint32_t board_millis(void) {
|
2023-01-12 15:34:29 +07:00
|
|
|
return system_ticks;
|
|
|
|
}
|
2022-03-21 10:01:11 +10:30
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// Board porting API
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
void board_led_write(bool state) {
|
2023-01-12 15:19:26 +07:00
|
|
|
GPIO_WriteBit(LED_PORT, LED_PIN, state);
|
2022-03-21 10:01:11 +10:30
|
|
|
}
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
uint32_t board_button_read(void) {
|
2023-01-12 15:19:26 +07:00
|
|
|
return BUTTON_STATE_ACTIVE == GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN);
|
2022-03-21 10:01:11 +10:30
|
|
|
}
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
int board_uart_read(uint8_t* buf, int len) {
|
2023-01-12 15:19:26 +07:00
|
|
|
(void) buf;
|
|
|
|
(void) len;
|
2022-03-21 10:01:11 +10:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-05-20 17:26:04 +07:00
|
|
|
int board_uart_write(void const* buf, int len) {
|
2022-03-21 10:01:11 +10:30
|
|
|
int txsize = len;
|
2024-05-21 16:08:27 +07:00
|
|
|
const char* bufc = (const char*) buf;
|
2024-05-20 17:26:04 +07:00
|
|
|
while (txsize--) {
|
2024-05-21 16:08:27 +07:00
|
|
|
uart_write(*bufc++);
|
2022-03-21 10:01:11 +10:30
|
|
|
}
|
2024-05-21 16:08:27 +07:00
|
|
|
uart_sync();
|
2022-03-21 10:01:11 +10:30
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
|
|
/**
|
|
|
|
* @brief Reports the name of the source file and the source line number
|
|
|
|
* where the assert_param error has occurred.
|
|
|
|
* @param file: pointer to the source file name
|
|
|
|
* @param line: assert_param error line source number
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
void assert_failed(char* file, uint32_t line) {
|
|
|
|
/* USER CODE BEGIN 6 */
|
|
|
|
/* User can add his own implementation to report the file name and line
|
|
|
|
number,
|
|
|
|
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
|
|
|
|
*/
|
|
|
|
/* USER CODE END 6 */
|
|
|
|
}
|
|
|
|
#endif /* USE_FULL_ASSERT */
|