LED clock
 All Files Functions Variables Macros Groups Pages
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 
39 #define USART USART1
40 #define USART_RCC RCC_USART1
41 #define USART_IRQ NVIC_USART1_IRQ
42 #define USART_ISR usart1_isr
43 #define USART_PORT GPIOA
44 #define USART_PORT_RCC RCC_GPIOA
45 #define USART_PIN_TX GPIO_USART1_TX
46 #define USART_PIN_RX GPIO_USART1_RX
49 #define USART_BAUDRATE 115200
51 /* input and output ring buffer, indexes, and available memory */
52 static uint8_t rx_buffer[USART_BUFFER] = {0};
53 static volatile uint8_t rx_i = 0;
54 static volatile uint8_t rx_used = 0;
55 static uint8_t tx_buffer[USART_BUFFER] = {0};
56 static volatile uint8_t tx_i = 0;
57 static volatile uint8_t tx_used = 0;
58 volatile uint8_t usart_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it
59 
60 void usart_setup(void)
61 {
62  /* enable USART I/O peripheral */
63  rcc_periph_clock_enable(USART_PORT_RCC); // enable clock for USART port peripheral
64  rcc_periph_clock_enable(USART_RCC); // enable clock for USART peripheral
65  gpio_set_mode(USART_PORT, GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX); // setup GPIO pin USART transmit
66  gpio_set_mode(USART_PORT, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX); // setup GPIO pin USART receive
67  gpio_set(USART_PORT, USART_PIN_RX); // pull up to avoid noise when not connected
68 
69  /* setup USART parameters */
70  usart_set_baudrate(USART, USART_BAUDRATE);
71  usart_set_databits(USART, 8);
72  usart_set_stopbits(USART, USART_STOPBITS_1);
73  usart_set_mode(USART, USART_MODE_TX_RX);
74  usart_set_parity(USART, USART_PARITY_NONE);
75  usart_set_flow_control(USART, USART_FLOWCONTROL_NONE);
76 
77  nvic_enable_irq(USART_IRQ); // enable the USART interrupt
78  usart_enable_rx_interrupt(USART); // enable receive interrupt
79  usart_enable(USART); // enable USART
80 
81  /* reset buffer states */
82  tx_i = 0;
83  tx_used = 0;
84  rx_i = 0;
85  rx_used = 0;
86  usart_received = 0;
87 }
88 
90 {
91  usart_flush(); // empty buffer first
92  usart_send_blocking(USART, c); // send character
93 }
94 
95 void usart_flush(void)
96 {
97  while (tx_used) { // idle until buffer is empty
98  __WFI(); // sleep until interrupt
99  }
100  usart_wait_send_ready(USART); // wait until transmit register is empty (transmission might not be complete)
101 }
102 
103 char usart_getchar(void)
104 {
105  while (!rx_used) { // idle until data is available
106  __WFI(); // sleep until interrupt;
107  }
108  char to_return = rx_buffer[rx_i]; // get the next available character
109  usart_disable_rx_interrupt(USART); // disable receive interrupt to prevent index corruption
110  rx_i = (rx_i+1)%sizeof(rx_buffer); // update used buffer
111  rx_used--; // update used buffer
112  usart_received = rx_used; // update available data
113  usart_enable_rx_interrupt(USART); // enable receive interrupt
114  return to_return;
115 }
116 
118 {
119  while (tx_used>=sizeof(tx_buffer)) { // idle until buffer has some space
120  usart_enable_tx_interrupt(USART); // enable transmit interrupt
121  __WFI(); // sleep until something happened
122  }
123  usart_disable_tx_interrupt(USART); // disable transmit interrupt to prevent index corruption
124  tx_buffer[(tx_i+tx_used)%sizeof(tx_buffer)] = c; // put character in buffer
125  tx_used++; // update used buffer
126  usart_enable_tx_interrupt(USART); // enable transmit interrupt
127 }
128 
130 void USART_ISR(void)
131 {
132  if (usart_get_interrupt_source(USART, USART_SR_TXE)) { // data has been transmitted
133  if (!tx_used) { // no data in the buffer to transmit
134  usart_disable_tx_interrupt(USART); // disable transmit interrupt
135  } else {
136  usart_send(USART,tx_buffer[tx_i]); // put data in transmit register
137  tx_i = (tx_i+1)%sizeof(rx_buffer); // update location on buffer
138  tx_used--; // update used size
139  }
140  }
141  if (usart_get_interrupt_source(USART, USART_SR_RXNE)) { // data has been received
142  // only save data if there is space in the buffer
143  if (rx_used>=sizeof(rx_buffer)) {
144  usart_recv(USART); // read to clear interrupt
145  } else {
146  rx_buffer[(rx_i+rx_used)%sizeof(rx_buffer)] = usart_recv(USART); // put character in buffer
147  rx_used++; // update used buffer
148  usart_received = rx_used; // update available data
149  }
150  }
151 }
static uint8_t rx_buffer[USART_BUFFER]
Definition: usart.c:52
#define USART
Definition: usart.c:39
static uint8_t tx_buffer[USART_BUFFER]
Definition: usart.c:55
#define USART_PIN_TX
Definition: usart.c:45
#define USART_IRQ
Definition: usart.c:41
volatile uint8_t usart_received
Definition: usart.c:58
#define USART_ISR
Definition: usart.c:42
#define USART_BUFFER
Definition: usart.h:24
#define USART_PORT_RCC
Definition: usart.c:44
static volatile uint8_t rx_i
Definition: usart.c:53
#define USART_PORT
Definition: usart.c:43
static volatile uint8_t tx_used
Definition: usart.c:57
#define USART_PIN_RX
Definition: usart.c:46
void usart_putchar_nonblocking(char c)
send character over USART (non-blocking)
Definition: usart.c:117
static volatile uint8_t tx_i
Definition: usart.c:56
library for USART communication (API)
static volatile uint8_t rx_used
Definition: usart.c:54
#define USART_RCC
Definition: usart.c:40
void usart_flush(void)
ensure all data has been transmitted (blocking)
Definition: usart.c:95
#define USART_BAUDRATE
Definition: usart.c:49
char usart_getchar(void)
get character received over USART (blocking)
Definition: usart.c:103
void usart_putchar_blocking(char c)
send character over USART (blocking)
Definition: usart.c:89
void usart_setup(void)
setup USART peripheral
Definition: usart.c:60