|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2020 MM32 SE TEAM |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + * |
| 24 | + * This file is part of the TinyUSB stack. |
| 25 | + */ |
| 26 | + |
| 27 | +/* WeAct BluePillPlus with MM32F3273G6P */ |
| 28 | + |
| 29 | +#include "mm32_device.h" |
| 30 | +#include "hal_conf.h" |
| 31 | +#include "tusb.h" |
| 32 | +#include "../board.h" |
| 33 | + |
| 34 | +//--------------------------------------------------------------------+ |
| 35 | +// Forward USB interrupt events to TinyUSB IRQ Handler |
| 36 | +//--------------------------------------------------------------------+ |
| 37 | +void OTG_FS_IRQHandler (void) |
| 38 | +{ |
| 39 | + tud_int_handler(0); |
| 40 | + |
| 41 | +} |
| 42 | +void USB_DeviceClockInit (void) |
| 43 | +{ |
| 44 | + /* Select USBCLK source */ |
| 45 | + // RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_Div1); |
| 46 | + RCC->CFGR &= ~(0x3 << 22); |
| 47 | + RCC->CFGR |= (0x1 << 22); |
| 48 | + |
| 49 | + /* Enable USB clock */ |
| 50 | + RCC->AHB2ENR |= 0x1 << 7; |
| 51 | +} |
| 52 | +//--------------------------------------------------------------------+ |
| 53 | +// MACRO TYPEDEF CONSTANT ENUM DECLARATION |
| 54 | +//--------------------------------------------------------------------+ |
| 55 | +// LED |
| 56 | + |
| 57 | +extern u32 SystemCoreClock; |
| 58 | +const int baudrate = 115200; |
| 59 | + |
| 60 | +void board_init (void) |
| 61 | +{ |
| 62 | +// usb clock |
| 63 | + USB_DeviceClockInit(); |
| 64 | + |
| 65 | + if ( SysTick_Config(SystemCoreClock / 1000) ) |
| 66 | + { |
| 67 | + while ( 1 ) |
| 68 | + ; |
| 69 | + } |
| 70 | + NVIC_SetPriority(SysTick_IRQn, 0x0); |
| 71 | + |
| 72 | + // LED on PB2 |
| 73 | + GPIO_InitTypeDef GPIO_InitStruct; |
| 74 | + RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE); |
| 75 | + GPIO_StructInit(&GPIO_InitStruct); |
| 76 | + |
| 77 | + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; |
| 78 | + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; |
| 79 | + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; |
| 80 | + GPIO_Init(GPIOB, &GPIO_InitStruct); |
| 81 | + |
| 82 | + board_led_write(true); |
| 83 | + |
| 84 | + // KEY on PA0 |
| 85 | + RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE); |
| 86 | + GPIO_StructInit(&GPIO_InitStruct); |
| 87 | + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0; |
| 88 | + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; |
| 89 | + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPD; |
| 90 | + GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 91 | + |
| 92 | + // UART |
| 93 | + UART_InitTypeDef UART_InitStruct; |
| 94 | + |
| 95 | + RCC_APB2PeriphClockCmd(RCC_APB2ENR_UART1, ENABLE); //enableUART1,GPIOAclock |
| 96 | + RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE); // |
| 97 | + //UART initialset |
| 98 | + |
| 99 | + GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_7); |
| 100 | + GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_7); |
| 101 | + |
| 102 | + UART_StructInit(&UART_InitStruct); |
| 103 | + UART_InitStruct.UART_BaudRate = baudrate; |
| 104 | + UART_InitStruct.UART_WordLength = UART_WordLength_8b; |
| 105 | + UART_InitStruct.UART_StopBits = UART_StopBits_1; //one stopbit |
| 106 | + UART_InitStruct.UART_Parity = UART_Parity_No; //none odd-even verify bit |
| 107 | + UART_InitStruct.UART_HardwareFlowControl = UART_HardwareFlowControl_None; //No hardware flow control |
| 108 | + UART_InitStruct.UART_Mode = UART_Mode_Rx | UART_Mode_Tx; // receive and sent mode |
| 109 | + |
| 110 | + UART_Init(UART1, &UART_InitStruct); //initial uart 1 |
| 111 | + UART_Cmd(UART1, ENABLE); //enable uart 1 |
| 112 | + |
| 113 | + //UART1_TX GPIOA.9 |
| 114 | + GPIO_StructInit(&GPIO_InitStruct); |
| 115 | + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; |
| 116 | + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; |
| 117 | + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; |
| 118 | + GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 119 | + |
| 120 | + //UART1_RX GPIOA.10 |
| 121 | + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; |
| 122 | + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; |
| 123 | + GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +//--------------------------------------------------------------------+ |
| 129 | +// Board porting API |
| 130 | +//--------------------------------------------------------------------+ |
| 131 | + |
| 132 | +void board_led_write (bool state) |
| 133 | +{ |
| 134 | + state ? (GPIO_ResetBits(GPIOB, GPIO_Pin_2)) : (GPIO_SetBits(GPIOB, GPIO_Pin_2)); |
| 135 | +} |
| 136 | + |
| 137 | +uint32_t board_button_read (void) |
| 138 | +{ |
| 139 | + uint32_t key = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_SET; |
| 140 | + return key; |
| 141 | +} |
| 142 | + |
| 143 | +int board_uart_read (uint8_t *buf, int len) |
| 144 | +{ |
| 145 | + (void) buf; |
| 146 | + (void) len; |
| 147 | + return 0; |
| 148 | +} |
| 149 | + |
| 150 | +int board_uart_write (void const *buf, int len) |
| 151 | +{ |
| 152 | + const char *buff = buf; |
| 153 | + while ( len ) |
| 154 | + { |
| 155 | + while ( (UART1->CSR & UART_IT_TXIEN) == 0 ) |
| 156 | + ; //The loop is sent until it is finished |
| 157 | + UART1->TDR = (*buff & 0xFF); |
| 158 | + buff++; |
| 159 | + len--; |
| 160 | + } |
| 161 | + return len; |
| 162 | +} |
| 163 | + |
| 164 | +#if CFG_TUSB_OS == OPT_OS_NONE |
| 165 | +volatile uint32_t system_ticks = 0; |
| 166 | +void SysTick_Handler (void) |
| 167 | +{ |
| 168 | + system_ticks++; |
| 169 | +} |
| 170 | + |
| 171 | +uint32_t board_millis (void) |
| 172 | +{ |
| 173 | + return system_ticks; |
| 174 | +} |
| 175 | +#endif |
| 176 | + |
| 177 | +// Required by __libc_init_array in startup code if we are compiling using |
| 178 | +// -nostdlib/-nostartfiles. |
| 179 | +void _init(void) |
| 180 | +{ |
| 181 | + |
| 182 | +} |
0 commit comments