rs232.c
Go to the documentation of this file.
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* standard libraries */
#include <stdint.h> // standard integer types
#include <stdlib.h> // general utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/usart.h> // universal synchronous asynchronous receiver transmitter library
#include <libopencm3/cm3/nvic.h> // interrupt handler
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
#include "rs232.h" // RS-232 header and definitions
#include "global.h" // common methods
#include "print.h"
#define RS232_USART 2
#define RS232_EN_PORT B
#define RS232_EN_PIN 5
#define RS232_SHDN_PORT C
#define RS232_SHDN_PIN 15
#define RS232_RTS_PORT A
#define RS232_RTS_PIN 1
#define RS232_CTS_PORT A
#define RS232_CTS_PIN 0
#define RS232_BAUDRATE 1200
/* input and output ring buffer, indexes, and available memory */
//static uint8_t rx_buffer[RS_BUFFER] = {0}; /**< ring buffer for received data */
//static volatile uint8_t rx_i = 0; /**< current position of read received data */
//static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
//static uint8_t tx_buffer[RS_BUFFER] = {0}; /**< ring buffer for data to transmit */
//static volatile uint8_t tx_i = 0; /**< current position of transmitted data */
//static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
//volatile bool usart_received = false;
void rs232_setup(void)
{
// put RS-232 transceiver output pins in high impedance (disable transmitter and receiver)
rcc_periph_clock_enable(RCC_GPIO(RS232_EN_PORT)); // enable clock for GPIO domain
gpio_set(GPIO(RS232_EN_PORT), GPIO(RS232_EN_PIN)); // set high to disable receiver
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)
rcc_periph_clock_enable(RCC_GPIO(RS232_SHDN_PORT)); // enable clock for GPIO domain
gpio_clear(GPIO(RS232_SHDN_PORT), GPIO(RS232_SHDN_PIN)); // set low to disable transmitter
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)
// enable UART I/O peripheral connected to RS-485 transceiver
rcc_periph_clock_enable(USART_PORT_RCC(RS232_USART)); // enable clock for USART port peripheral
rcc_periph_clock_enable(USART_RCC(RS232_USART)); // enable clock for USART peripheral
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
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
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
gpio_set(USART_PORT(RS232_USART), USART_PIN_RX(RS232_USART)); // pull up to avoid noise when not connected
// setup UART parameters
usart_set_baudrate(USART(RS232_USART), RS232_BAUDRATE);
usart_set_databits(USART(RS232_USART), 8);
usart_set_stopbits(USART(RS232_USART), USART_STOPBITS_1);
usart_set_mode(USART(RS232_USART), USART_MODE_TX_RX);
usart_set_parity(USART(RS232_USART), USART_PARITY_NONE);
usart_set_flow_control(USART(RS232_USART), USART_FLOWCONTROL_NONE);
// nvic_enable_irq(RS_IRQ(RS232_USART)); // enable the USART interrupt
// usart_enable_rx_interrupt(USART(RS232_USART)); // enable receive interrupt
usart_enable(USART(RS232_USART)); // enable USART
/*
gpio_set(GPIO(RS232_SHDN_PORT), GPIO(RS232_SHDN_PIN)); // enable RS-232 transceiver driver output
while (true) {
usart_send_blocking(USART(RS232_USART), 'a'); // send character
}
*/
/*
gpio_clear(GPIO(RS232_EN_PORT), GPIO(RS232_EN_PIN)); // enable RS-485 receiver output
while (true) {
char c = usart_recv_blocking(USART(RS232_USART));
printf("%c", c);
}
*/
/*
// put RS-232 transceiver output pins in high impedance (disable transmitter and receiver)
rcc_periph_clock_enable(RCC_GPIO(RS232_RTS_PORT)); // enable clock for GPIO domain
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)
gpio_set(GPIO(RS232_SHDN_PORT), GPIO(RS232_SHDN_PIN)); // enable RS-232 transceiver driver output
while (true) {
gpio_toggle(GPIO(RS232_RTS_PORT), GPIO(RS232_RTS_PIN));
sleep_ms(1);
}
*/
rcc_periph_clock_enable(RCC_GPIO(RS232_CTS_PORT)); // enable clock for GPIO domain
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)
gpio_clear(GPIO(RS232_EN_PORT), GPIO(RS232_EN_PIN)); // enable RS-485 receiver output
while (true) {
if (gpio_get(GPIO(RS232_CTS_PORT), GPIO(RS232_CTS_PIN))) {
printf("1");
} else {
printf("0");
}
}
/* reset buffer states */
// tx_i = 0;
// tx_used = 0;
// rx_i = 0;
// rx_used = 0;
// usart_received = false;
}
/*
void usart_putchar_blocking(char c)
{
usart_flush(); // empty buffer first
usart_send_blocking(USART(RS232_USART), c); // send character
}
void usart_flush(void)
{
while (tx_used) { // idle until buffer is empty
__WFI(); // sleep until interrupt
}
usart_wait_send_ready(USART(RS232_USART)); // wait until transmit register is empty (transmission might not be complete)
}
char usart_getchar(void)
{
while (!rx_used) { // idle until data is available
__WFI(); // sleep until interrupt
}
char to_return = rx_buffer[rx_i]; // get the next available character
usart_disable_rx_interrupt(USART(RS232_USART)); // disable receive interrupt to prevent index corruption
rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
rx_used--; // update used buffer
usart_received = (rx_used!=0); // update available data
usart_enable_rx_interrupt(USART(RS232_USART)); // enable receive interrupt
return to_return;
}
void usart_putchar_nonblocking(char c)
{
while (tx_used>=LENGTH(tx_buffer)) { // idle until buffer has some space
usart_enable_tx_interrupt(USART(RS232_USART)); // enable transmit interrupt
__WFI(); // sleep until something happened
}
usart_disable_tx_interrupt(USART(RS232_USART)); // disable transmit interrupt to prevent index corruption
tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
tx_used++; // update used buffer
usart_enable_tx_interrupt(USART(RS232_USART)); // enable transmit interrupt
}
*/
/*
void RS_ISR(RS232_USART)(void)
{
if (usart_get_flag(USART(RS232_USART), RS_SR_TXE)) { // data has been transmitted
if (!tx_used) { // no data in the buffer to transmit
usart_disable_tx_interrupt(USART(RS232_USART)); // disable transmit interrupt
} else {
usart_send(USART(RS232_USART),tx_buffer[tx_i]); // put data in transmit register
tx_i = (tx_i+1)%LENGTH(rx_buffer); // update location on buffer
tx_used--; // update used size
}
}
if (usart_get_flag(USART(RS232_USART), RS_SR_RXNE)) { // data has been received
// only save data if there is space in the buffer
while (rx_used>=LENGTH(rx_buffer)) { // if buffer is full
rx_i = (rx_i+1)%LENGTH(rx_buffer); // drop oldest data
rx_used--; // update used buffer information
}
rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usart_recv(USART(RS232_USART)); // put character in buffer
rx_used++; // update used buffer
usart_received = (rx_used!=0); // update available data
}
}
*/
rs232.h
library for RS-232 communication (API)
RS232_SHDN_PORT
#define RS232_SHDN_PORT
RS-232 pin to enable transmitter (active high, pulled low)
Definition: rs232.c:49
RCC_GPIO
#define RCC_GPIO(x)
get RCC for GPIO based on GPIO identifier
Definition: global.h:105
global.h
global definitions and methods (API)
GPIO
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
RS232_EN_PIN
#define RS232_EN_PIN
RS-232 pin to enable receiver (active low, pulled up)
Definition: rs232.c:48
RS232_USART
#define RS232_USART
USART peripheral.
Definition: rs232.c:41
USART
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
USART_PORT
#define USART_PORT(x)
get port for USART based on USART identifier
Definition: global.h:190
USART_PORT_RCC
#define USART_PORT_RCC(x)
get RCC for USART port based on USART identifier
Definition: global.h:195
rs232_setup
void rs232_setup(void)
transmit and receive buffer sizes
Definition: rs232.c:70
printf
size_t printf(const char *format,...)
print format string on user output
Definition: print.c:445
print.h
printing utilities to replace the large printf from the standard library (API)
RS232_EN_PORT
#define RS232_EN_PORT
RS-232 pin to enable receiver (active low, pulled up)
Definition: rs232.c:47
USART_RCC
#define USART_RCC(x)
get RCC for USART based on USART identifier
Definition: global.h:184
RS232_SHDN_PIN
#define RS232_SHDN_PIN
RS-232 pin to enable transmitter (active high, pulled low)
Definition: rs232.c:50
USART_PIN_RX
#define USART_PIN_RX(x)
get receive pin for USART based on USART identifier
Definition: global.h:202
USART_PIN_TX
#define USART_PIN_TX(x)
get transmit pin for USART based on USART identifier
Definition: global.h:200
RS232_BAUDRATE
#define RS232_BAUDRATE
serial baudrate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) ...
Definition: rs232.c:58