CuVoodoo STM32F1 firmware template
usart.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 "usart.h" // USART header and definitions
35 #include "global.h" // common methods
36 
40 #define USART_ID 1
43 #define USART_BAUDRATE 1500000
45 /* input and output ring buffer, indexes, and available memory */
46 static uint8_t rx_buffer[USART_BUFFER] = {0};
47 static volatile uint8_t rx_i = 0;
48 static volatile uint8_t rx_used = 0;
49 static uint8_t tx_buffer[USART_BUFFER] = {0};
50 static volatile uint8_t tx_i = 0;
51 static volatile uint8_t tx_used = 0;
53 volatile bool usart_received = false;
54 
55 void usart_setup(void)
56 {
57  /* enable USART I/O peripheral */
58  rcc_periph_clock_enable(USART_PORT_RCC(USART_ID)); // enable clock for USART port peripheral
59  rcc_periph_clock_enable(USART_RCC(USART_ID)); // enable clock for USART peripheral
60  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
61  gpio_set_mode(USART_PORT(USART_ID), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(USART_ID)); // setup GPIO pin USART transmit
62  gpio_set_mode(USART_PORT(USART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(USART_ID)); // setup GPIO pin USART receive
63  gpio_set(USART_PORT(USART_ID), USART_PIN_RX(USART_ID)); // pull up to avoid noise when not connected
64 
65  /* setup USART parameters */
66  usart_set_baudrate(USART(USART_ID), USART_BAUDRATE);
67  usart_set_databits(USART(USART_ID), 8);
68  usart_set_stopbits(USART(USART_ID), USART_STOPBITS_1);
69  usart_set_mode(USART(USART_ID), USART_MODE_TX_RX);
70  usart_set_parity(USART(USART_ID), USART_PARITY_NONE);
71  usart_set_flow_control(USART(USART_ID), USART_FLOWCONTROL_NONE);
72 
73  nvic_enable_irq(USART_IRQ(USART_ID)); // enable the USART interrupt
74  usart_enable_rx_interrupt(USART(USART_ID)); // enable receive interrupt
75  usart_enable(USART(USART_ID)); // enable USART
76 
77  /* reset buffer states */
78  tx_i = 0;
79  tx_used = 0;
80  rx_i = 0;
81  rx_used = 0;
82  usart_received = false;
83 }
84 
86 {
87  usart_flush(); // empty buffer first
88  usart_send_blocking(USART(USART_ID), c); // send character
89 }
90 
91 void usart_flush(void)
92 {
93  while (tx_used) { // idle until buffer is empty
94  __WFI(); // sleep until interrupt
95  }
96  usart_wait_send_ready(USART(USART_ID)); // wait until transmit register is empty (transmission might not be complete)
97 }
98 
99 char usart_getchar(void)
100 {
101  while (!rx_used) { // idle until data is available
102  __WFI(); // sleep until interrupt
103  }
104  char to_return = rx_buffer[rx_i]; // get the next available character
105  usart_disable_rx_interrupt(USART(USART_ID)); // disable receive interrupt to prevent index corruption
106  rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
107  rx_used--; // update used buffer
108  usart_received = (rx_used!=0); // update available data
109  usart_enable_rx_interrupt(USART(USART_ID)); // enable receive interrupt
110  return to_return;
111 }
112 
114 {
115  while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
116  usart_enable_tx_interrupt(USART(USART_ID)); // enable transmit interrupt
117  __WFI(); // sleep until something happened
118  }
119  usart_disable_tx_interrupt(USART(USART_ID)); // disable transmit interrupt to prevent index corruption
120  tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
121  tx_used++; // update used buffer
122  usart_enable_tx_interrupt(USART(USART_ID)); // enable transmit interrupt
123 }
124 
126 void USART_ISR(USART_ID)(void)
127 {
128  if (usart_get_interrupt_source(USART(USART_ID), USART_SR_TXE)) { // data has been transmitted
129  if (!tx_used) { // no data in the buffer to transmit
130  usart_disable_tx_interrupt(USART(USART_ID)); // disable transmit interrupt
131  } else {
132  usart_send(USART(USART_ID),tx_buffer[tx_i]); // put data in transmit register
133  tx_i = (tx_i+1)%LENGTH(rx_buffer); // update location on buffer
134  tx_used--; // update used size
135  }
136  }
137  if (usart_get_interrupt_source(USART(USART_ID), USART_SR_RXNE)) { // data has been received
138  // only save data if there is space in the buffer
139  while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
140  rx_i = (rx_i+1)%LENGTH(rx_buffer); // drop oldest data
141  rx_used--; // update used buffer information
142  }
143  rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usart_recv(USART(USART_ID)); // put character in buffer
144  rx_used++; // update used buffer
145  usart_received = (rx_used!=0); // update available data
146  }
147 }
static uint8_t rx_buffer[USART_BUFFER]
ring buffer for received data
Definition: usart.c:46
volatile bool usart_received
how many bytes available in the received buffer since last read
Definition: usart.c:53
#define USART_ISR(x)
get interrupt service routine for USART based on USART identifier
Definition: global.h:124
static uint8_t tx_buffer[USART_BUFFER]
ring buffer for data to transmit
Definition: usart.c:49
#define USART_ID
USART peripheral.
Definition: usart.c:40
global definitions and methods (API)
#define USART_BUFFER
transmit and receive buffer sizes
Definition: usart.h:24
static volatile uint8_t rx_i
current position of read received data
Definition: usart.c:47
static volatile uint8_t tx_used
how much data needs to be transmitted
Definition: usart.c:51
#define USART_IRQ(x)
get NVIC IRQ for USART based on USART identifier
Definition: global.h:122
#define USART(x)
get USART based on USART identifier
Definition: global.h:118
#define USART_PORT(x)
get port for USART based on USART identifier
Definition: global.h:126
#define USART_PORT_RCC(x)
get RCC for USART port based on USART identifier
Definition: global.h:131
void usart_putchar_nonblocking(char c)
send character over USART (non-blocking)
Definition: usart.c:113
static volatile uint8_t tx_i
current position of transmitted data
Definition: usart.c:50
library for USART communication (API)
static volatile uint8_t rx_used
how much data has been received and not red
Definition: usart.c:48
void usart_flush(void)
ensure all data has been transmitted (blocking)
Definition: usart.c:91
#define USART_BAUDRATE
serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) ...
Definition: usart.c:43
char usart_getchar(void)
get character received over USART (blocking)
Definition: usart.c:99
#define LENGTH(x)
get the length of an array
Definition: global.h:26
#define USART_RCC(x)
get RCC for USART based on USART identifier
Definition: global.h:120
#define USART_PIN_RX(x)
get receive pin for USART based on USART identifier
Definition: global.h:138
void usart_putchar_blocking(char c)
send character over USART (blocking)
Definition: usart.c:85
#define USART_PIN_TX(x)
get transmit pin for USART based on USART identifier
Definition: global.h:136
void usart_setup(void)
setup USART peripheral
Definition: usart.c:55