|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2022 Greg Davill |
| 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 | +#include "debug_uart.h" |
| 28 | +#include <ch32v30x.h> |
| 29 | + |
| 30 | + |
| 31 | +#define UART_RINGBUFFER_SIZE_TX 64 |
| 32 | +#define UART_RINGBUFFER_MASK_TX (UART_RINGBUFFER_SIZE_TX-1) |
| 33 | + |
| 34 | +static char tx_buf[UART_RINGBUFFER_SIZE_TX]; |
| 35 | +static unsigned int tx_produce; |
| 36 | +static volatile unsigned int tx_consume; |
| 37 | + |
| 38 | +void USART1_IRQHandler(void) __attribute__((naked)); |
| 39 | +void USART1_IRQHandler(void) { |
| 40 | + __asm volatile ("call USART1_IRQHandler_impl; mret"); |
| 41 | +} |
| 42 | + |
| 43 | +__attribute__((used)) void USART1_IRQHandler_impl(void) |
| 44 | +{ |
| 45 | + if(USART_GetITStatus(USART1, USART_IT_TC) != RESET) |
| 46 | + { |
| 47 | + USART_ClearITPendingBit(USART1, USART_IT_TC); |
| 48 | + |
| 49 | + if(tx_consume != tx_produce) { |
| 50 | + USART_SendData(USART1, tx_buf[tx_consume]); |
| 51 | + tx_consume = (tx_consume + 1) & UART_RINGBUFFER_MASK_TX; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +void uart_write(char c) |
| 58 | +{ |
| 59 | + unsigned int tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX; |
| 60 | + |
| 61 | + NVIC_DisableIRQ(USART1_IRQn); |
| 62 | + if((tx_consume != tx_produce) || (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)) { |
| 63 | + tx_buf[tx_produce] = c; |
| 64 | + tx_produce = tx_produce_next; |
| 65 | + } else { |
| 66 | + USART_SendData(USART1, c); |
| 67 | + } |
| 68 | + NVIC_EnableIRQ(USART1_IRQn); |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +void uart_sync(void) |
| 73 | +{ |
| 74 | + while(tx_consume != tx_produce); |
| 75 | +} |
| 76 | + |
| 77 | + |
| 78 | +void usart_printf_init(uint32_t baudrate) |
| 79 | +{ |
| 80 | + GPIO_InitTypeDef GPIO_InitStructure; |
| 81 | + USART_InitTypeDef USART_InitStructure; |
| 82 | + |
| 83 | + tx_produce = 0; |
| 84 | + tx_consume = 0; |
| 85 | + |
| 86 | + RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); |
| 87 | + |
| 88 | + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; |
| 89 | + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
| 90 | + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
| 91 | + GPIO_Init(GPIOA, &GPIO_InitStructure); |
| 92 | + |
| 93 | + USART_InitStructure.USART_BaudRate = baudrate; |
| 94 | + USART_InitStructure.USART_WordLength = USART_WordLength_8b; |
| 95 | + USART_InitStructure.USART_StopBits = USART_StopBits_1; |
| 96 | + USART_InitStructure.USART_Parity = USART_Parity_No; |
| 97 | + USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; |
| 98 | + USART_InitStructure.USART_Mode = USART_Mode_Tx; |
| 99 | + |
| 100 | + USART_Init(USART1, &USART_InitStructure); |
| 101 | + USART_ITConfig(USART1, USART_IT_TC, ENABLE); |
| 102 | + USART_Cmd(USART1, ENABLE); |
| 103 | + |
| 104 | + NVIC_InitTypeDef NVIC_InitStructure = { 0 }; |
| 105 | + NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; |
| 106 | + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; |
| 107 | + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; |
| 108 | + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; |
| 109 | + NVIC_Init(&NVIC_InitStructure); |
| 110 | +} |
0 commit comments