CuVoodoo STM32F1 firmware template
rs232.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 <stdlib.h> // general utilities
25 
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/stm32/rcc.h> // real-time control clock library
28 #include <libopencm3/stm32/gpio.h> // general purpose input output library
29 #include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
30 #include <libopencm3/cm3/nvic.h> // interrupt handler
31 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
32 
33 #include "rs232.h" // RS-232 header and definitions
34 #include "global.h" // common methods
35 
36 #include "print.h"
37 
41 #define RS232_USART 2
47 #define RS232_EN_PORT B
48 #define RS232_EN_PIN 5
49 #define RS232_SHDN_PORT C
50 #define RS232_SHDN_PIN 15
51 #define RS232_RTS_PORT A
52 #define RS232_RTS_PIN 1
53 #define RS232_CTS_PORT A
54 #define RS232_CTS_PIN 0
55 
58 #define RS232_BAUDRATE 1200
60 /* input and output ring buffer, indexes, and available memory */
61 //static uint8_t rx_buffer[RS_BUFFER] = {0}; /**< ring buffer for received data */
62 //static volatile uint8_t rx_i = 0; /**< current position of read received data */
63 //static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
64 //static uint8_t tx_buffer[RS_BUFFER] = {0}; /**< ring buffer for data to transmit */
65 //static volatile uint8_t tx_i = 0; /**< current position of transmitted data */
66 //static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
67 
68 //volatile bool usart_received = false;
69 
70 void rs232_setup(void)
71 {
72 
73  // put RS-232 transceiver output pins in high impedance (disable transmitter and receiver)
74  rcc_periph_clock_enable(RCC_GPIO(RS232_EN_PORT)); // enable clock for GPIO domain
75  gpio_set(GPIO(RS232_EN_PORT), GPIO(RS232_EN_PIN)); // set high to disable receiver
76  gpio_set_mode(GPIO(RS232_EN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(RS232_EN_PIN)); // set pin as output (open-drain pulled high to disable receiver)
77  rcc_periph_clock_enable(RCC_GPIO(RS232_SHDN_PORT)); // enable clock for GPIO domain
78  gpio_clear(GPIO(RS232_SHDN_PORT), GPIO(RS232_SHDN_PIN)); // set low to disable transmitter
79  gpio_set_mode(GPIO(RS232_SHDN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(RS232_SHDN_PIN)); // set pin as output (push-pull pulled low to disable transmitter)
80 
81  // enable UART I/O peripheral connected to RS-485 transceiver
82  rcc_periph_clock_enable(USART_PORT_RCC(RS232_USART)); // enable clock for USART port peripheral
83  rcc_periph_clock_enable(USART_RCC(RS232_USART)); // enable clock for USART peripheral
84  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
85  gpio_set_mode(USART_PORT(RS232_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(RS232_USART)); // setup GPIO pin USART transmit
86  gpio_set_mode(USART_PORT(RS232_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(RS232_USART)); // setup GPIO pin USART receive
87  gpio_set(USART_PORT(RS232_USART), USART_PIN_RX(RS232_USART)); // pull up to avoid noise when not connected
88 
89  // setup UART parameters
90  usart_set_baudrate(USART(RS232_USART), RS232_BAUDRATE);
91  usart_set_databits(USART(RS232_USART), 8);
92  usart_set_stopbits(USART(RS232_USART), USART_STOPBITS_1);
93  usart_set_mode(USART(RS232_USART), USART_MODE_TX_RX);
94  usart_set_parity(USART(RS232_USART), USART_PARITY_NONE);
95  usart_set_flow_control(USART(RS232_USART), USART_FLOWCONTROL_NONE);
96 // nvic_enable_irq(RS_IRQ(RS232_USART)); // enable the USART interrupt
97 // usart_enable_rx_interrupt(USART(RS232_USART)); // enable receive interrupt
98  usart_enable(USART(RS232_USART)); // enable USART
99 
100 /*
101  gpio_set(GPIO(RS232_SHDN_PORT), GPIO(RS232_SHDN_PIN)); // enable RS-232 transceiver driver output
102  while (true) {
103  usart_send_blocking(USART(RS232_USART), 'a'); // send character
104  }
105 */
106 
107 /*
108  gpio_clear(GPIO(RS232_EN_PORT), GPIO(RS232_EN_PIN)); // enable RS-485 receiver output
109  while (true) {
110  char c = usart_recv_blocking(USART(RS232_USART));
111  printf("%c", c);
112  }
113 */
114 
115 /*
116  // put RS-232 transceiver output pins in high impedance (disable transmitter and receiver)
117  rcc_periph_clock_enable(RCC_GPIO(RS232_RTS_PORT)); // enable clock for GPIO domain
118  gpio_set_mode(GPIO(RS232_RTS_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(RS232_RTS_PIN)); // set pin as output (open-drain pulled high to disable receiver)
119  gpio_set(GPIO(RS232_SHDN_PORT), GPIO(RS232_SHDN_PIN)); // enable RS-232 transceiver driver output
120  while (true) {
121  gpio_toggle(GPIO(RS232_RTS_PORT), GPIO(RS232_RTS_PIN));
122  sleep_ms(1);
123  }
124 */
125 
126  rcc_periph_clock_enable(RCC_GPIO(RS232_CTS_PORT)); // enable clock for GPIO domain
127  gpio_set_mode(GPIO(RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(RS232_CTS_PIN)); // set pin as output (push-dull pulled low to disable transmitter)
128  gpio_clear(GPIO(RS232_EN_PORT), GPIO(RS232_EN_PIN)); // enable RS-485 receiver output
129  while (true) {
130  if (gpio_get(GPIO(RS232_CTS_PORT), GPIO(RS232_CTS_PIN))) {
131  printf("1");
132  } else {
133  printf("0");
134  }
135  }
136 
137  /* reset buffer states */
138 // tx_i = 0;
139 // tx_used = 0;
140 // rx_i = 0;
141 // rx_used = 0;
142 // usart_received = false;
143 }
144 
145 /*
146 void usart_putchar_blocking(char c)
147 {
148  usart_flush(); // empty buffer first
149  usart_send_blocking(USART(RS232_USART), c); // send character
150 }
151 
152 void usart_flush(void)
153 {
154  while (tx_used) { // idle until buffer is empty
155  __WFI(); // sleep until interrupt
156  }
157  usart_wait_send_ready(USART(RS232_USART)); // wait until transmit register is empty (transmission might not be complete)
158 }
159 
160 char usart_getchar(void)
161 {
162  while (!rx_used) { // idle until data is available
163  __WFI(); // sleep until interrupt
164  }
165  char to_return = rx_buffer[rx_i]; // get the next available character
166  usart_disable_rx_interrupt(USART(RS232_USART)); // disable receive interrupt to prevent index corruption
167  rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
168  rx_used--; // update used buffer
169  usart_received = (rx_used!=0); // update available data
170  usart_enable_rx_interrupt(USART(RS232_USART)); // enable receive interrupt
171  return to_return;
172 }
173 
174 void usart_putchar_nonblocking(char c)
175 {
176  while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
177  usart_enable_tx_interrupt(USART(RS232_USART)); // enable transmit interrupt
178  __WFI(); // sleep until something happened
179  }
180  usart_disable_tx_interrupt(USART(RS232_USART)); // disable transmit interrupt to prevent index corruption
181  tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
182  tx_used++; // update used buffer
183  usart_enable_tx_interrupt(USART(RS232_USART)); // enable transmit interrupt
184 }
185 */
186 
188 /*
189 void RS_ISR(RS232_USART)(void)
190 {
191  if (usart_get_flag(USART(RS232_USART), RS_SR_TXE)) { // data has been transmitted
192  if (!tx_used) { // no data in the buffer to transmit
193  usart_disable_tx_interrupt(USART(RS232_USART)); // disable transmit interrupt
194  } else {
195  usart_send(USART(RS232_USART),tx_buffer[tx_i]); // put data in transmit register
196  tx_i = (tx_i+1)%LENGTH(rx_buffer); // update location on buffer
197  tx_used--; // update used size
198  }
199  }
200  if (usart_get_flag(USART(RS232_USART), RS_SR_RXNE)) { // data has been received
201  // only save data if there is space in the buffer
202  while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
203  rx_i = (rx_i+1)%LENGTH(rx_buffer); // drop oldest data
204  rx_used--; // update used buffer information
205  }
206  rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usart_recv(USART(RS232_USART)); // put character in buffer
207  rx_used++; // update used buffer
208  usart_received = (rx_used!=0); // update available data
209  }
210 }
211 */
library for RS-232 communication (API)
#define RS232_SHDN_PORT
RS-232 pin to enable transmitter (active high, pulled low)
Definition: rs232.c:49
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
#define RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
Definition: rs232.c:48
#define RS232_USART
USART peripheral.
Definition: rs232.c:41
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
#define USART_PORT(x)
get port for USART based on USART identifier
Definition: global.h:190
#define USART_PORT_RCC(x)
get RCC for USART port based on USART identifier
Definition: global.h:195
void rs232_setup(void)
transmit and receive buffer sizes
Definition: rs232.c:70
#define RS232_EN_PORT
RS-232 pin to enable receiver (active low, pulled up)
Definition: rs232.c:47
#define USART_RCC(x)
get RCC for USART based on USART identifier
Definition: global.h:184
#define RS232_SHDN_PIN
RS-232 pin to enable transmitter (active high, pulled low)
Definition: rs232.c:50
#define USART_PIN_RX(x)
get receive pin for USART based on USART identifier
Definition: global.h:202
#define USART_PIN_TX(x)
get transmit pin for USART based on USART identifier
Definition: global.h:200
#define RS232_BAUDRATE
serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) ...
Definition: rs232.c:58