CuVoodoo STM32F1 firmware template
rs485.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 "rs485.h" // RS-485 header and definitions
34 #include "global.h" // common methods
35 
36 #include "print.h"
37 
41 #define RS485_USART 2
47 #define RS485_RE_PORT C
48 #define RS485_RE_PIN 13
49 #define RS485_DE_PORT C
50 #define RS485_DE_PIN 14
53 #define RS485_BAUDRATE 1200
55 /* input and output ring buffer, indexes, and available memory */
56 //static uint8_t rx_buffer[RS_BUFFER] = {0}; /**< ring buffer for received data */
57 //static volatile uint8_t rx_i = 0; /**< current position of read received data */
58 //static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
59 //static uint8_t tx_buffer[RS_BUFFER] = {0}; /**< ring buffer for data to transmit */
60 //static volatile uint8_t tx_i = 0; /**< current position of transmitted data */
61 //static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
62 
63 //volatile bool usart_received = false;
64 
65 void rs485_setup(void)
66 {
67 
68  // put RS-485 transceiver output pins in high impedance (disable driver and receiver)
69  rcc_periph_clock_enable(RCC_GPIO(RS485_RE_PORT)); // enable clock for GPIO domain
70  gpio_set(GPIO(RS485_RE_PORT), GPIO(RS485_RE_PIN)); // set high disable receiver
71  gpio_set_mode(GPIO(RS485_RE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(RS485_RE_PIN)); // set pin as output (open-drain pulled high to disable receiver)
72  rcc_periph_clock_enable(RCC_GPIO(RS485_DE_PORT)); // enable clock for GPIO domain
73  gpio_clear(GPIO(RS485_DE_PORT), GPIO(RS485_DE_PIN)); // set low disable driver
74  gpio_set_mode(GPIO(RS485_DE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(RS485_DE_PIN)); // set pin as output (push-dull pulled low to disable driver)
75 
76  // enable UART I/O peripheral connected to RS-485 transceiver
77  rcc_periph_clock_enable(USART_PORT_RCC(RS485_USART)); // enable clock for USART port peripheral
78  rcc_periph_clock_enable(USART_RCC(RS485_USART)); // enable clock for USART peripheral
79  rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
80  gpio_set_mode(USART_PORT(RS485_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(RS485_USART)); // setup GPIO pin USART transmit
81  gpio_set_mode(USART_PORT(RS485_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(RS485_USART)); // setup GPIO pin USART receive
82  gpio_set(USART_PORT(RS485_USART), USART_PIN_RX(RS485_USART)); // pull up to avoid noise when not connected
83 
84  // setup UART parameters
85  usart_set_baudrate(USART(RS485_USART), RS485_BAUDRATE);
86  usart_set_databits(USART(RS485_USART), 8);
87  usart_set_stopbits(USART(RS485_USART), USART_STOPBITS_1);
88  usart_set_mode(USART(RS485_USART), USART_MODE_TX_RX);
89  usart_set_parity(USART(RS485_USART), USART_PARITY_NONE);
90  usart_set_flow_control(USART(RS485_USART), USART_FLOWCONTROL_NONE);
91 // nvic_enable_irq(RS_IRQ(RS485_USART)); // enable the USART interrupt
92 // usart_enable_rx_interrupt(USART(RS485_USART)); // enable receive interrupt
93  usart_enable(USART(RS485_USART)); // enable USART
94 
95  gpio_set(GPIO(RS485_DE_PORT), GPIO(RS485_DE_PIN)); // enable RS-485 transceiver driver output
96  while (true) {
97  usart_send_blocking(USART(RS485_USART), 'a'); // send character
98  }
99 
100  gpio_clear(GPIO(RS485_RE_PORT), GPIO(RS485_RE_PIN)); // enable RS-485 receiver output
101  while (true) {
102  char c = usart_recv_blocking(USART(RS485_USART));
103  printf("%c", c);
104  }
105  /* reset buffer states */
106 // tx_i = 0;
107 // tx_used = 0;
108 // rx_i = 0;
109 // rx_used = 0;
110 // usart_received = false;
111 }
112 
113 /*
114 void usart_putchar_blocking(char c)
115 {
116  usart_flush(); // empty buffer first
117  usart_send_blocking(USART(RS485_USART), c); // send character
118 }
119 
120 void usart_flush(void)
121 {
122  while (tx_used) { // idle until buffer is empty
123  __WFI(); // sleep until interrupt
124  }
125  usart_wait_send_ready(USART(RS485_USART)); // wait until transmit register is empty (transmission might not be complete)
126 }
127 
128 char usart_getchar(void)
129 {
130  while (!rx_used) { // idle until data is available
131  __WFI(); // sleep until interrupt
132  }
133  char to_return = rx_buffer[rx_i]; // get the next available character
134  usart_disable_rx_interrupt(USART(RS485_USART)); // disable receive interrupt to prevent index corruption
135  rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
136  rx_used--; // update used buffer
137  usart_received = (rx_used!=0); // update available data
138  usart_enable_rx_interrupt(USART(RS485_USART)); // enable receive interrupt
139  return to_return;
140 }
141 
142 void usart_putchar_nonblocking(char c)
143 {
144  while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
145  usart_enable_tx_interrupt(USART(RS485_USART)); // enable transmit interrupt
146  __WFI(); // sleep until something happened
147  }
148  usart_disable_tx_interrupt(USART(RS485_USART)); // disable transmit interrupt to prevent index corruption
149  tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
150  tx_used++; // update used buffer
151  usart_enable_tx_interrupt(USART(RS485_USART)); // enable transmit interrupt
152 }
153 */
154 
156 /*
157 void RS_ISR(RS485_USART)(void)
158 {
159  if (usart_get_flag(USART(RS485_USART), RS_SR_TXE)) { // data has been transmitted
160  if (!tx_used) { // no data in the buffer to transmit
161  usart_disable_tx_interrupt(USART(RS485_USART)); // disable transmit interrupt
162  } else {
163  usart_send(USART(RS485_USART),tx_buffer[tx_i]); // put data in transmit register
164  tx_i = (tx_i+1)%LENGTH(rx_buffer); // update location on buffer
165  tx_used--; // update used size
166  }
167  }
168  if (usart_get_flag(USART(RS485_USART), RS_SR_RXNE)) { // data has been received
169  // only save data if there is space in the buffer
170  while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
171  rx_i = (rx_i+1)%LENGTH(rx_buffer); // drop oldest data
172  rx_used--; // update used buffer information
173  }
174  rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usart_recv(USART(RS485_USART)); // put character in buffer
175  rx_used++; // update used buffer
176  usart_received = (rx_used!=0); // update available data
177  }
178 }
179 */
library for RS-485/RS-422 communication (API)
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
#define RS485_RE_PORT
RS-485 pin to enable receiver (active low, pulled up)
Definition: rs485.c:47
global definitions and methods (API)
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
#define RS485_DE_PORT
RS-485 pin to enable driver (active high, pulled low)
Definition: rs485.c:49
#define RS485_RE_PIN
RS-485 pin to enable receiver (active low, pulled up)
Definition: rs485.c:48
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
#define RS485_DE_PIN
RS-485 pin to enable driver (active high, pulled low)
Definition: rs485.c:50
#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
#define RS485_USART
USART peripheral.
Definition: rs485.c:41
#define RS485_BAUDRATE
serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) ...
Definition: rs485.c:53
#define USART_RCC(x)
get RCC for USART based on USART identifier
Definition: global.h:184
#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
void rs485_setup(void)
transmit and receive buffer sizes
Definition: rs485.c:65