CuVoodoo STM32F1 firmware template
uart.c
Go to the documentation of this file.
1 /* This program is free software: you can redistribute it and/or modify
2  * it under the terms of the GNU General Public License as published by
3  * the Free Software Foundation, either version 3 of the License, or
4  * (at your option) any later version.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public License
12  * along with this program. If not, see <http://www.gnu.org/licenses/>.
13  *
14  */
22 /* standard libraries */
23 #include <stdint.h> // standard integer types
24 #include <stdio.h> // standard I/O facilities
25 #include <stdlib.h> // general utilities
26 
27 /* STM32 (including CM3) libraries */
28 #include <libopencm3/stm32/rcc.h> // real-time control clock library
29 #include <libopencm3/stm32/gpio.h> // general purpose input output library
30 #include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
31 #include <libopencm3/cm3/nvic.h> // interrupt handler
32 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
33 
34 #include "uart.h" // UART header and definitions
35 #include "global.h" // common methods
36 
40 #define UART_ID 1
43 #define UART_BAUDRATE 921600
45 /* output ring buffer, indexes, and available memory */
46 static volatile uint8_t tx_buffer[64] = {0};
47 static volatile uint8_t tx_i = 0;
48 static volatile uint8_t tx_used = 0;
50 void uart_setup(void)
51 {
52  /* enable UART I/O peripheral */
53  rcc_periph_clock_enable(RCC_USART_PORT(UART_ID)); // enable clock for UART port peripheral
54  rcc_periph_clock_enable(RCC_USART(UART_ID)); // enable clock for UART peripheral
55  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (UART)
56  gpio_set_mode(USART_TX_PORT(UART_ID), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_TX_PIN(UART_ID)); // setup GPIO pin UART transmit
57  gpio_set_mode(USART_RX_PORT(UART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(UART_ID)); // setup GPIO pin UART receive
58  gpio_set(USART_RX_PORT(UART_ID), USART_RX_PIN(UART_ID)); // pull up to avoid noise when not connected
59 
60  /* setup UART parameters */
61  usart_set_baudrate(USART(UART_ID), UART_BAUDRATE);
62  usart_set_databits(USART(UART_ID), 8);
63  usart_set_stopbits(USART(UART_ID), USART_STOPBITS_1);
64  usart_set_mode(USART(UART_ID), USART_MODE_TX_RX);
65  usart_set_parity(USART(UART_ID), USART_PARITY_NONE);
66  usart_set_flow_control(USART(UART_ID), USART_FLOWCONTROL_NONE);
67 
68  nvic_enable_irq(USART_IRQ(UART_ID)); // enable the UART interrupt
69  usart_enable_rx_interrupt(USART(UART_ID)); // enable receive interrupt
70  usart_enable(USART(UART_ID)); // enable UART
71 
72  /* reset buffer states */
73  tx_i = 0;
74  tx_used = 0;
75 }
76 
78 {
79  uart_flush(); // empty buffer first
80  usart_send_blocking(USART(UART_ID), c); // send character
81 }
82 
83 void uart_flush(void)
84 {
85  while (tx_used) { // idle until buffer is empty
86  __WFI(); // sleep until interrupt
87  }
88  usart_wait_send_ready(USART(UART_ID)); // wait until transmit register is empty (transmission might not be complete)
89 }
90 
92 {
93  while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
94  usart_enable_tx_interrupt(USART(UART_ID)); // enable transmit interrupt
95  __WFI(); // sleep until something happened
96  }
97  usart_disable_tx_interrupt(USART(UART_ID)); // disable transmit interrupt to prevent index corruption
98  tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
99  tx_used++; // update used buffer
100  usart_enable_tx_interrupt(USART(UART_ID)); // enable transmit interrupt
101 }
102 
104 void USART_ISR(UART_ID)(void)
105 {
106  if (usart_get_flag(USART(UART_ID), USART_SR_TXE)) { // data has been transmitted
107  if (!tx_used) { // no data in the buffer to transmit
108  usart_disable_tx_interrupt(USART(UART_ID)); // disable transmit interrupt
109  } else {
110  usart_send(USART(UART_ID),tx_buffer[tx_i]); // put data in transmit register
111  tx_i = (tx_i+1)%LENGTH(tx_buffer); // update location on buffer
112  tx_used--; // update used size
113  }
114  }
115  while (usart_get_flag(USART(UART_ID), USART_SR_RXNE)) { // data has been received (repeat while receiving)
116  char c = usart_recv(USART(UART_ID)); // save character and free UART buffer
117  user_input_store(c); // store data
118  }
119 }
#define USART_TX_PORT(x)
get port for USART transmit pin based on USART identifier
Definition: global.h:190
#define RCC_USART(x)
get RCC for USART based on USART identifier
Definition: global.h:184
#define UART_BAUDRATE
serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) ...
Definition: uart.c:43
library for UART communication (API)
void uart_putchar_blocking(char c)
send character over UART (blocking)
Definition: uart.c:77
#define USART_ISR(x)
get interrupt service routine for USART based on USART identifier
Definition: global.h:188
#define RCC_USART_PORT(x)
get RCC for USART port based on USART identifier
Definition: global.h:206
void user_input_store(char c)
store user input
Definition: global.c:199
void uart_putchar_nonblocking(char c)
send character over UART (non-blocking)
Definition: uart.c:91
global definitions and methods (API)
#define UART_ID
USART peripheral.
Definition: uart.c:40
#define USART_RX_PIN(x)
get pin for USART receive pin based on USART identifier
Definition: global.h:200
#define USART_TX_PIN(x)
get pin for USART transmit pin based on USART identifier
Definition: global.h:198
#define USART_IRQ(x)
get NVIC IRQ for USART based on USART identifier
Definition: global.h:186
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
void uart_flush(void)
ensure all data has been transmitted (blocking)
Definition: uart.c:83
#define USART_RX_PORT(x)
get port for USART receive pin based on USART identifier
Definition: global.h:192
static volatile uint8_t tx_used
how much data needs to be transmitted
Definition: uart.c:48
static volatile uint8_t tx_i
current position of transmitted data
Definition: uart.c:47
#define LENGTH(x)
get the length of an array
Definition: global.h:26
static volatile uint8_t tx_buffer[64]
ring buffer for data to transmit
Definition: uart.c:46
void uart_setup(void)
setup UART peripheral
Definition: uart.c:50