/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /** library for UART communication (code) * @file * @author King Kévin * @date 2016-2020 * @note peripherals used: USART @ref uart */ /* standard libraries */ #include // standard integer types #include // standard I/O facilities #include // general utilities /* STM32 (including CM3) libraries */ #include // real-time control clock library #include // general purpose input output library #include // universal synchronous asynchronous receiver transmitter library #include // interrupt handler #include // Cortex M3 utilities #include "spp_rx.h" // own definitions #include "global.h" // common methods /** @defgroup spp_uart USART peripheral used for UART communication * @{ */ #define SPP_UART_ID 3 /**< USART peripheral */ /** @} */ #define UART_BAUDRATE 115200 /**< serial baud rate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */ volatile bool spp_rx_input_available = false; /* input ring buffer, indexes, and available memory */ static volatile uint8_t spp_rx_input_buffer[64] = {0}; /**< ring buffer for received data */ static volatile uint8_t spp_rx_input_i = 0; /**< current position of read received data */ static volatile uint8_t spp_rx_input_used = 0; /**< how much data has been received and not red */ void spp_rx_setup(void) { /* enable UART I/O peripheral */ rcc_periph_clock_enable(RCC_USART_PORT(SPP_UART_ID)); // enable clock for UART port peripheral rcc_periph_clock_enable(RCC_USART(SPP_UART_ID)); // enable clock for UART peripheral rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (UART) gpio_set_mode(USART_RX_PORT(SPP_UART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(SPP_UART_ID)); // setup GPIO pin UART receive gpio_set(USART_RX_PORT(SPP_UART_ID), USART_RX_PIN(SPP_UART_ID)); // pull up to avoid noise when not connected /* setup UART parameters */ usart_set_baudrate(USART(SPP_UART_ID), UART_BAUDRATE); usart_set_databits(USART(SPP_UART_ID), 8); usart_set_stopbits(USART(SPP_UART_ID), USART_STOPBITS_1); usart_set_mode(USART(SPP_UART_ID), USART_MODE_RX); usart_set_parity(USART(SPP_UART_ID), USART_PARITY_NONE); usart_set_flow_control(USART(SPP_UART_ID), USART_FLOWCONTROL_NONE); nvic_enable_irq(USART_IRQ(SPP_UART_ID)); // enable the UART interrupt usart_enable_rx_interrupt(USART(SPP_UART_ID)); // enable receive interrupt usart_enable(USART(SPP_UART_ID)); // enable UART /* reset buffer states */ spp_rx_input_i = 0; spp_rx_input_used = 0; } char spp_rx_input_get(void) { if (0 == spp_rx_input_used) { // no data is available return 0; } volatile char to_return = spp_rx_input_buffer[spp_rx_input_i]; // get the next available character // there is no need to disable receive interrupt since it won't corrupt the index spp_rx_input_i = (spp_rx_input_i + 1) % LENGTH(spp_rx_input_buffer); // update used buffer spp_rx_input_used--; // update used buffer spp_rx_input_available = (spp_rx_input_used != 0); // update available data return to_return; } /** UART interrupt service routine called when data has been transmitted or received */ void USART_ISR(SPP_UART_ID)(void) { while (usart_get_flag(USART(SPP_UART_ID), USART_SR_RXNE)) { // data has been received (repeat while receiving) char c = usart_recv(USART(SPP_UART_ID)); // save character and free UART buffer (also clears flag) if (spp_rx_input_used < LENGTH(spp_rx_input_buffer)) { // only save data if there is space in the buffer spp_rx_input_buffer[(spp_rx_input_i + spp_rx_input_used) % LENGTH(spp_rx_input_buffer)] = c; // put character in buffer spp_rx_input_used++; // update used buffer spp_rx_input_available = true; // update available data } } }