busvoodoo_rs485.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> // standard utilities
#include <string.h> // string utilities
/* STM32 (including CM3) libraries */
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/usart.h> // USART utilities
/* own libraries */
#include "global.h" // board definitions
#include "print.h" // printing utilities
#include "menu.h" // menu definitions
#include "busvoodoo_global.h" // BusVoodoo definitions
#include "busvoodoo_oled.h" // OLED utilities
#include "busvoodoo_rs485.h" // own definitions
#define BUSVOODOO_RS485_USART 2
static enum busvoodoo_rs485_setting_t {
BUSVOODOO_RS485_SETTING_NONE,
BUSVOODOO_RS485_SETTING_BAUDRATE,
BUSVOODOO_RS485_SETTING_DATABITS,
BUSVOODOO_RS485_SETTING_PARITY,
BUSVOODOO_RS485_SETTING_STOPBITS,
BUSVOODOO_RS485_SETTING_DONE,
} busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_NONE;
static uint32_t busvoodoo_rs485_baudrate = 115200;
static uint8_t busvoodoo_rs485_databits = 8;
static uint32_t busvoodoo_rs485_parity = USART_PARITY_NONE;
static uint32_t busvoodoo_rs485_stopbits = USART_STOPBITS_1;
static bool busvoodoo_rs485_setup(char** prefix, const char* line)
{
bool complete = false; // is the setup complete
if (NULL==line) { // first call
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_NONE; // re-start configuration
}
switch (busvoodoo_rs485_setting) {
case BUSVOODOO_RS485_SETTING_NONE:
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "baud rate in bps (1-2000000) [%u]", busvoodoo_rs485_baudrate);
*prefix = busvoodoo_global_string; // ask for baud rate
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_BAUDRATE;
break;
case BUSVOODOO_RS485_SETTING_BAUDRATE:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DATABITS; // go to next setting
} else { // setting provided
uint32_t baudrate = atoi(line); // parse setting
if (baudrate>0 && baudrate<=2000000) { // check setting
busvoodoo_rs485_baudrate = baudrate; // remember setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DATABITS; // go to next setting
}
}
if (BUSVOODOO_RS485_SETTING_DATABITS==busvoodoo_rs485_setting) { // if next setting
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "data bits (8-9) [%u]", busvoodoo_rs485_databits); // prepare next setting
*prefix = busvoodoo_global_string; // display next setting
}
break;
case BUSVOODOO_RS485_SETTING_DATABITS:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_PARITY; // go to next setting
} else { // setting provided
uint8_t databits = atoi(line); // parse setting
if (8==databits || 9==databits) { // check setting
busvoodoo_rs485_databits = databits; // remember setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_PARITY; // go to next setting
}
}
if (BUSVOODOO_RS485_SETTING_PARITY==busvoodoo_rs485_setting) { // if next setting
printf("1) none\n");
printf("2) even\n");
printf("3) odd\n");
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "parity (1,2,3) [%c]", USART_PARITY_NONE==busvoodoo_rs485_parity ? '1' : (USART_PARITY_EVEN==busvoodoo_rs485_parity ? '2' : '3')); // prepare next setting
*prefix = busvoodoo_global_string; // display next setting
}
break;
case BUSVOODOO_RS485_SETTING_PARITY:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_STOPBITS; // go to next setting
} else if (1==strlen(line)) { // setting provided
if ('1'==line[0]) { // no parity
busvoodoo_rs485_parity = USART_PARITY_NONE;
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_STOPBITS; // go to next setting
} else if ('2'==line[0]) { // even parity
busvoodoo_rs485_parity = USART_PARITY_EVEN;
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_STOPBITS; // go to next setting
} else if ('3'==line[0]) { // odd parity
busvoodoo_rs485_parity = USART_PARITY_ODD;
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_STOPBITS; // go to next setting
}
}
if (BUSVOODOO_RS485_SETTING_STOPBITS==busvoodoo_rs485_setting) { // if next setting
printf("1) 0.5\n");
printf("2) 1\n");
printf("3) 1.5\n");
printf("4) 2\n");
snprintf(busvoodoo_global_string, LENGTH(busvoodoo_global_string), "stop bits (1,2,3,4) [%s]", USART_STOPBITS_0_5==busvoodoo_rs485_stopbits ? "0.5" : (USART_STOPBITS_1==busvoodoo_rs485_stopbits ? "1" : (USART_STOPBITS_1_5==busvoodoo_rs485_stopbits ? "1.5" : "2.0"))); // prepare next setting
*prefix = busvoodoo_global_string; // display next setting
}
break;
case BUSVOODOO_RS485_SETTING_STOPBITS:
if (NULL==line || 0==strlen(line)) { // use default setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DONE; // go to next setting
} else if (1==strlen(line)) { // setting provided
if ('1'==line[0]) { // 0.5 stop bits
busvoodoo_rs485_stopbits = USART_STOPBITS_0_5; // remember setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DONE; // go to next setting
} else if ('2'==line[0]) { // 1 stop bits
busvoodoo_rs485_stopbits = USART_STOPBITS_1; // remember setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DONE; // go to next setting
} else if ('3'==line[0]) { // 1.5 stop bits
busvoodoo_rs485_stopbits = USART_STOPBITS_1_5; // remember setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DONE; // go to next setting
} else if ('4'==line[0]) { // 2 stop bits
busvoodoo_rs485_stopbits = USART_STOPBITS_2; // remember setting
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_DONE; // go to next setting
}
}
if (BUSVOODOO_RS485_SETTING_DONE==busvoodoo_rs485_setting) { // we have all settings, configure RS-485
rcc_periph_clock_enable(RCC_AFIO); // enable clock for USART alternate function
rcc_periph_clock_enable(RCC_USART(BUSVOODOO_RS485_USART)); // enable clock for USART peripheral
usart_set_baudrate(USART(BUSVOODOO_RS485_USART), busvoodoo_rs485_baudrate); // set baud rate
usart_set_databits(USART(BUSVOODOO_RS485_USART), busvoodoo_rs485_databits); // set data bits
usart_set_parity(USART(BUSVOODOO_RS485_USART), busvoodoo_rs485_parity); // set parity
usart_set_stopbits(USART(BUSVOODOO_RS485_USART), busvoodoo_rs485_stopbits); // set stop bits
usart_set_flow_control(USART(BUSVOODOO_RS485_USART), USART_FLOWCONTROL_NONE); // set no flow control
usart_set_mode(USART(BUSVOODOO_RS485_USART), USART_MODE_TX_RX); // full-duplex communication
rcc_periph_clock_enable(RCC_USART_PORT(BUSVOODOO_RS485_USART)); // enable clock for USART GPIO peripheral
gpio_set_mode(USART_TX_PORT(BUSVOODOO_RS485_USART), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_TX_PIN(BUSVOODOO_RS485_USART)); // setup GPIO pin USART transmit
gpio_set_mode(USART_RX_PORT(BUSVOODOO_RS485_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, USART_RX_PIN(BUSVOODOO_RS485_USART)); // setup GPIO pin USART receive (no need to pull since the transceiver will drive it)
gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable RS-485 transceiver drive
gpio_set_mode(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_DE_PIN)); // setup RS-485 Drive Enable GPIO as output (pulled low to disable by default)
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set RS-485 transceiver Receive Enable pin high to disable received
gpio_set_mode(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO_MODE_OUTPUT_10_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_RS485_RE_PIN)); // setup RS-485 Receive Enable GPIO as output (pulled high to disable by default)
usart_enable(USART(BUSVOODOO_RS485_USART)); // enable USART
led_off(); // disable LED because there is no activity
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_NONE; // restart settings next time
*prefix = "RS-485"; // display mode
busvoodoo_oled_text_left(*prefix); // set mode title on OLED display
const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, NULL, NULL, NULL, NULL, NULL}; // RS-485 mode I/O pinout
for (uint8_t i=0; i<LENGTH(pinout_io) && i<LENGTH(busvoodoo_global_pinout_io); i++) {
busvoodoo_global_pinout_io[i] = pinout_io[i]; // set pin names
}
#if BUSVOODOO_HARDWARE_VERSION==0
const char* pinout_rscan[5] = {"HV", NULL, NULL, "B", "A"}; // RS-485 mode RS/CAN pinout for hardware version 0
#else
const char* pinout_rscan[5] = {"HV", NULL, "A", "B", NULL}; // RS-485 mode RS/CAN pinout
#endif
for (uint8_t i=0; i<LENGTH(pinout_rscan) && i<LENGTH(busvoodoo_global_pinout_rscan); i++) {
busvoodoo_global_pinout_rscan[i] = pinout_rscan[i]; // set pin names
}
const char* pinout[10] = {pinout_rscan[0], pinout_io[0], pinout_rscan[1], pinout_io[2], pinout_rscan[2], pinout_io[4], pinout_rscan[3], pinout_io[6], pinout_rscan[4], pinout_io[8]}; // pinout to display
busvoodoo_oled_text_pinout((const char**)pinout, false); // set pinout on display
busvoodoo_oled_update(); // update display to show text and pinout
complete = true; // configuration is complete
}
break;
default: // unknown case
busvoodoo_rs485_setting = BUSVOODOO_RS485_SETTING_NONE; // restart settings next time
break;
}
return complete;
}
static void busvoodoo_rs485_exit(void)
{
usart_disable(USART(BUSVOODOO_RS485_USART)); // disable USART
rcc_periph_clock_disable(RCC_USART(BUSVOODOO_RS485_USART)); // disable domain clock for USART peripheral
gpio_set_mode(USART_TX_PORT(BUSVOODOO_RS485_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, USART_TX_PIN(BUSVOODOO_RS485_USART)); // set pin back to floating input
gpio_set_mode(USART_RX_PORT(BUSVOODOO_RS485_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, USART_RX_PIN(BUSVOODOO_RS485_USART)); // set pin back to floating input
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable RS-485 transceiver receiver
gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable RS-485 transceiver driver
}
static void busvoodoo_rs485_write(uint16_t value)
{
while ((0==(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty (or user to interrupt)
if (USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TXE) { // we can send a character
// remove unused bits
if (USART_PARITY_NONE==busvoodoo_rs485_parity) { // no parity bit in frame
if (8==busvoodoo_rs485_databits) { // 8-bit frame
value &= 0xff;
} else { // 9-bit frame
value &= 0x1ff;
}
} else { // MSb is parity bit
if (8==busvoodoo_rs485_databits) { // 8-bit frame
value &= 0x7f;
} else { // 9-bit frame
value &= 0xff;
}
}
// send data
busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show transmission
gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set high to enable RS-485 transceiver driver
usart_send(USART(BUSVOODOO_RS485_USART), value); // transmit character
// display data send
printf("write: '%c'/0x", value);
if ((USART_PARITY_NONE==busvoodoo_rs485_parity) && 9==busvoodoo_rs485_databits) { // case where the final data is 9 bits long
printf("%03x\n", value);
} else {
printf("%02x\n", value);
}
while (0==(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TC)); // wait for transmission to be complete
gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable RS-485 transceiver driver
}
}
static void busvoodoo_rs485_read(void)
{
printf("read: ");
gpio_clear(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to low enable RS-485 transceiver receiver
while (!(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_RXNE) && !user_input_available); // wait for incoming data to be available (or user input to exit)
if ((USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_RXNE)) { // verify if data has been received
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // enable blue LED to show reception
// get the errors
bool error_noise = (0!=(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_NE)); // read noise error flag
bool error_framing = (0!=(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_FE)); // read frame error flag
bool error_parity = (0!=(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_PE)); // read parity error flag
uint16_t c = usart_recv(USART(BUSVOODOO_RS485_USART)); // read received character (also clears the error flags)
// remove unused bits
if (USART_PARITY_NONE==busvoodoo_rs485_parity) { // no parity bit in frame
if (8==busvoodoo_rs485_databits) { // 8-bit frame
c &= 0xff;
} else { // 9-bit frame
c &= 0x1ff;
}
} else { // MSb is parity bit
if (8==busvoodoo_rs485_databits) { // 8-bit frame
c &= 0x7f;
} else { // 9-bit frame
c &= 0xff;
}
}
// display data
printf("'%c'/0x", c);
if ((USART_PARITY_NONE==busvoodoo_rs485_parity) && 9==busvoodoo_rs485_databits) { // case where the final data is 9 bits long
printf("%03x ", c);
} else {
printf("%02x ", c);
}
// display errors
printf("(");
if (error_noise) {
printf("noise");
} else if (error_framing) {
printf("framing");
} else if (error_parity) {
printf("parity");
} else {
printf("no");
}
printf(" error)");
}
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable RS-485 transceiver receiver
printf("\n");
}
static bool busvoodoo_rs485_action(const char* action, uint32_t repetition, bool perform)
{
uint32_t length = strlen(action); // remember length since it will be used a number of times
if (NULL==action || 0==length) { // there is nothing to do
return true;
}
if (1==length && 'r'==action[0]) { // read data
if (!perform) {
return true;
}
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_rs485_read(); // read from RS-485
}
} else if (1==length && 'u'==action[0]) { // sleep us
if (!perform) {
return true;
}
printf("wait for %u us\n", repetition);
sleep_us(repetition); // sleep
} else if (1==length && 'm'==action[0]) { // sleep ms
if (!perform) {
return true;
}
printf("wait for %u ms\n", repetition);
sleep_ms(repetition); // sleep
} else if ('0'==action[0]) { // send digit
if (1==length) { // just send 0
if (!perform) {
return true;
}
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_rs485_write(0); // write to RS-485
}
} else if ('x'==action[1] || 'b'==action[1]) { // send hex/binary
return busvoodoo_rs485_action(action+1, repetition, perform); // just retry without leading 0
} else if (action[1]>='0' && action[1]<='9') { // send decimal
return busvoodoo_rs485_action(action+1, repetition, perform); // just retry without leading 0
} else { // malformed action
return false;
}
} else if ('x'==action[0] && length>1) { // send hexadecimal value
for (uint32_t i=1; i<length; i++) { // check string
if (!((action[i]>='0' && action[i]<='9') || (action[i]>='a' && action[i]<='f') || (action[i]>='A' && action[i]<='F'))) { // check for hexadecimal character
return false; // not an hexadecimal string
}
}
if (!perform) {
return true;
}
uint32_t value = strtol(&action[1], NULL, 16); // get hex value
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_rs485_write(value); // write to SPI
}
} else if ('b'==action[0] && length>1) { // send binary value
for (uint32_t i=1; i<length; i++) { // check string
if (action[i]<'0' || action[i]>'1') { // check for binary character
return false; // not a binary string
}
}
if (!perform) {
return true;
}
uint32_t value = strtol(&action[1], NULL, 2); // get binary value
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_rs485_write(value); // write to SPI
}
} else if (action[0]>='1' && action[0]<='9') { // send decimal value
for (uint32_t i=1; i<length; i++) { // check string
if (action[i]<'0' || action[i]>'9') { // check for decimal character
return false; // not a decimal string
}
}
if (!perform) {
return true;
}
uint32_t value = strtol(&action[0], NULL, 10); // get decimal value
for (uint32_t i=0; i<repetition; i++) {
busvoodoo_rs485_write(value); // write to SPI
}
} else if (length>=2 && ('"'==action[0] || '\''==action[0]) && (action[length-1]==action[0])) { // send ASCII character
if (!perform) {
return true;
}
for (uint32_t r=0; r<repetition; r++) {
for (uint32_t i=1; i<length-1; i++) { // go through string
busvoodoo_rs485_write(action[i]); // write to SPI
}
}
} else { // malformed action
return false;
}
return true; // all went well
}
// command handlers
static void busvoodoo_rs485_command_actions(void* argument)
{
if (NULL==argument || 0==strlen(argument)) {
printf("available actions (separated by space or ,):\n");
printf("0\twrite decimal value\n");
printf("0x0\twrite hexadecimal value\n");
printf("0b0\twrite binary value\n");
printf("\"a\"/'a'\twrite ASCII characters\n");
printf("r\tread value\n");
printf("u/m\twait 1 us/ms\n");
printf(":n\trepeat action n times\n");
return;
}
// copy argument since it will be modified
char* copy = calloc(strlen(argument)+1, sizeof(char));
if (!copy) {
while (true);
}
strncpy(copy, argument, strlen(argument)+1);
// verify and perform actions
if (!busvoodoo_global_actions(copy, false, &busvoodoo_rs485_action)) { // verify actions
printf("malformed action(s)\n");
} else { // action are ok
printf("press any key to exit\n");
busvoodoo_global_actions(argument, true, &busvoodoo_rs485_action); // perform action
if (user_input_available) { // user interrupted flow
user_input_get(); // discard user input
}
}
free(copy); // release memory
}
static void busvoodoo_rs485_command_transmit(void* argument)
{
if (NULL==argument || 0==strlen(argument)) { // nothing to transmit
argument = "\r\n"; // transmit CR+LF
}
printf("press any key to exit\n");
gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set high to enable RS-485 transceiver driver
for (uint16_t i=0; ((char*)(argument))[i] && !user_input_available; i++) {
while ((0==(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty
if (USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TXE) { // we can send a character
printf("%c", ((char*)(argument))[i]); // echo character to transmit
busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // pulse red LED to show transmission
usart_send(USART(BUSVOODOO_RS485_USART), ((char*)(argument))[i]); // transmit character
}
}
if (user_input_available) { // user interrupted flow
user_input_get(); // discard user input
} else {
while (0==(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TC)); // wait for transmission to be complete
}
gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable RS-485 transceiver driver
if (strcmp(argument, "\r\n")) {
printf("\n");
}
}
static void busvoodoo_rs485_command_receive(void* argument)
{
bool display_hex = false; // display in hex
bool display_bin = false; // display in bin
if (NULL!=argument && strlen(argument)>0) {
if (0==strcmp(argument, "h") || 0==strcmp(argument, "hex")) { // user wants hexadecimal display
display_hex = true; // remember to display in hexadecimal
} else if (0==strcmp(argument, "b") || 0==strcmp(argument, "bin")) { // user wants binary display
display_bin = true; // remember to display in binary
} else {
printf("malformed argument\n");
return;
}
}
printf("press any key to exit\n");
gpio_clear(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to low enable RS-485 transceiver receiver
while (!user_input_available) { // check for user input to exit
if ((USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_RXNE)) { // verify if data has been received
uint16_t c = usart_recv(USART(BUSVOODOO_RS485_USART)); // receive character
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // enable blue LED to show reception
// remove unused bits (ignore parity bit)
if (USART_PARITY_NONE==busvoodoo_rs485_parity) { // no parity bit in frame
if (8==busvoodoo_rs485_databits) { // 8-bit frame
c &= 0xff;
} else { // 9-bit frame
c &= 0x1ff;
}
} else { // MSb is parity bit
if (8==busvoodoo_rs485_databits) { // 8-bit frame
c &= 0x7f;
} else { // 9-bit frame
c &= 0xff;
}
}
if (display_hex) { // display data in hex
if ((USART_PARITY_NONE==busvoodoo_rs485_parity) && 9==busvoodoo_rs485_databits) { // case where the final data is 9 bits long
printf("%03x ", c);
} else {
printf("%02x ", c);
}
} else if (display_bin) { // display data in binary
if (USART_PARITY_NONE==busvoodoo_rs485_parity) {
if (8==busvoodoo_rs485_databits) { // 8-bit frame
printf("%08b ", c);
} else { // 9-bit frame
printf("%09b ", c);
}
} else { // one bit is a parity bit
if (8==busvoodoo_rs485_databits) { // 8-bit frame
printf("%07b ", c);
} else { // 9-bit frame
printf("%08b ", c);
}
}
} else { // display in ASCII
printf("%c", c); // print received character
}
}
}
user_input_get(); // discard user input
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable RS-485 transceiver receiver
printf("\n"); // get to next line
}
static void busvoodoo_rs485_command_transceive(void* argument)
{
(void)argument; // we won't use the argument
printf("press 5 times escape to exit\n");
char last_c = 0; // last user character received
uint8_t esc_count = 0; // number of times escape has press received
gpio_clear(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to low enable RS-485 transceiver receiver
while (true) { // check for escape sequence
if (user_input_available) { // check if user wants to transmit something
char c = user_input_get(); // get user input
if (0x1b==c) { // user pressed escape
if (0x1b!=last_c) { // this is the first escape press
esc_count = 0;
}
esc_count++; // increment escape count
}
last_c = c; // remember key press
if (esc_count<5) { // check for escape sequence
while ((0==(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TXE) && !user_input_available)); // wait for transmit buffer to be empty
if (USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TXE) { // we can send a character
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable RS-485 transceiver receiver
gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set high to enable RS-485 transceiver driver
usart_send_blocking(USART(BUSVOODOO_RS485_USART), c); // send user character
busvoodoo_led_red_pulse(BUSVOODOO_LED_PULSE); // enable red LED to show transmission
while (0==(USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_TC)); // wait for transmission to be complete
gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable RS-485 transceiver driver
gpio_clear(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to low enable RS-485 transceiver receiver
}
} else { // user wants to exit
break; // exit infinite loop
}
}
if ((USART_SR(USART(BUSVOODOO_RS485_USART)) & USART_SR_RXNE)) { // verify if data has been received
char c = usart_recv(USART(BUSVOODOO_RS485_USART)); // receive character
busvoodoo_led_blue_pulse(BUSVOODOO_LED_PULSE); // enable blue LED to show reception
printf("%c", c); // print received character
}
}
gpio_set(GPIO(BUSVOODOO_RS485_RE_PORT), GPIO(BUSVOODOO_RS485_RE_PIN)); // set high to disable RS-485 transceiver receiver
printf("\n"); // get to next line
}
static void busvoodoo_rs485_command_error(void* argument)
{
(void)argument; // argument not used
printf("press any key to exit\n");
while (!user_input_available) { // wait until user interrupt
busvoodoo_rs485_read(); // read incoming data (this also checks for errors
}
user_input_get(); // discard user input
}
static const struct menu_command_t busvoodoo_rs485_commands[] = {
{
.shortcut = 'a',
.name = "action",
.command_description = "perform protocol actions",
.argument = MENU_ARGUMENT_STRING,
.argument_description = "[actions]",
.command_handler = &busvoodoo_rs485_command_actions,
},
{
.shortcut = 'r',
.name = "receive",
.command_description = "show incoming data [in hexadecimal or binary]",
.argument = MENU_ARGUMENT_STRING,
.argument_description = "[hex|bin]",
.command_handler = &busvoodoo_rs485_command_receive,
},
{
.shortcut = 't',
.name = "transmit",
.command_description = "transmit ASCII text (empty for CR+LF)",
.argument = MENU_ARGUMENT_STRING,
.argument_description = "[text]",
.command_handler = &busvoodoo_rs485_command_transmit,
},
{
.shortcut = 'x',
.name = "transceive",
.command_description = "transmit and receive data",
.argument = MENU_ARGUMENT_NONE,
.argument_description = NULL,
.command_handler = &busvoodoo_rs485_command_transceive,
},
{
.shortcut = 'e',
.name = "error",
.command_description = "verify incoming transmission for errors",
.argument = MENU_ARGUMENT_NONE,
.argument_description = NULL,
.command_handler = &busvoodoo_rs485_command_error,
},
};
const struct busvoodoo_mode_t busvoodoo_rs485_mode = {
.name = "rs485",
.description = "Recommended Standard 485/422",
.full_only = true,
.setup = &busvoodoo_rs485_setup,
.commands = busvoodoo_rs485_commands,
.commands_nb = LENGTH(busvoodoo_rs485_commands),
.exit = &busvoodoo_rs485_exit,
};
USART_TX_PORT
#define USART_TX_PORT(x)
get port for USART transmit pin based on USART identifier
Definition: global.h:190
RCC_USART
#define RCC_USART(x)
get RCC for USART based on USART identifier
Definition: global.h:184
busvoodoo_rs485_databits
static uint8_t busvoodoo_rs485_databits
RS-485 data bits.
Definition: busvoodoo_rs485.c:57
menu_command_t
command menu entry
Definition: menu.h:31
BUSVOODOO_LED_PULSE
#define BUSVOODOO_LED_PULSE
recommended duration in ms for pulsing LEDs to show activity
Definition: busvoodoo_global.h:110
busvoodoo_global.h
BusVoodoo global definitions and methods (API)
busvoodoo_oled_text_pinout
void busvoodoo_oled_text_pinout(const char *pins[10], bool io_connector)
draw pin names on bottom (blue) part in display buffer
Definition: busvoodoo_oled.c:406
busvoodoo_led_red_pulse
void busvoodoo_led_red_pulse(uint16_t ms)
pulse red LED for short duration
Definition: busvoodoo_global.c:400
busvoodoo_mode_t::name
const char * name
name of the mode (i.e.
Definition: busvoodoo_global.h:114
busvoodoo_rs485_commands
static const struct menu_command_t busvoodoo_rs485_commands[]
RS-485 menu commands.
Definition: busvoodoo_rs485.c:597
led_off
void led_off(void)
switch off board LED
Definition: global.c:82
RCC_USART_PORT
#define RCC_USART_PORT(x)
get RCC for USART port based on USART identifier
Definition: global.h:206
busvoodoo_oled_update
void busvoodoo_oled_update(void)
update OLED display RAM with current display buffer
Definition: busvoodoo_oled.c:589
busvoodoo_rs485_write
static void busvoodoo_rs485_write(uint16_t value)
write to RS-485
Definition: busvoodoo_rs485.c:216
busvoodoo_global_pinout_rscan
const char * busvoodoo_global_pinout_rscan[5]
RS/CAN connector pinout.
Definition: busvoodoo_global.c:57
global.h
global definitions and methods (API)
GPIO
#define GPIO(x)
get GPIO based on GPIO identifier
Definition: global.h:103
busvoodoo_led_blue_pulse
void busvoodoo_led_blue_pulse(uint16_t ms)
pulse blue LED for short duration
Definition: busvoodoo_global.c:381
sleep_us
void sleep_us(uint32_t duration)
go to sleep for some microseconds
Definition: global.c:144
busvoodoo_rs485_stopbits
static uint32_t busvoodoo_rs485_stopbits
RS-485 stop bits setting.
Definition: busvoodoo_rs485.c:61
BUSVOODOO_RS485_RE_PORT
#define BUSVOODOO_RS485_RE_PORT
RS-485 pin to enable receiver (active low, pulled up)
Definition: busvoodoo_global.h:79
busvoodoo_rs485_command_transmit
static void busvoodoo_rs485_command_transmit(void *argument)
command to transmit a string
Definition: busvoodoo_rs485.c:445
busvoodoo_rs485_setup
static bool busvoodoo_rs485_setup(char **prefix, const char *line)
setup RS-485 mode
Definition: busvoodoo_rs485.c:68
USART_RX_PIN
#define USART_RX_PIN(x)
get pin for USART receive pin based on USART identifier
Definition: global.h:200
USART_TX_PIN
#define USART_TX_PIN(x)
get pin for USART transmit pin based on USART identifier
Definition: global.h:198
USART
#define USART(x)
get USART based on USART identifier
Definition: global.h:182
BUSVOODOO_RS485_DE_PORT
#define BUSVOODOO_RS485_DE_PORT
RS-485 pin to enable transmitter (active high, pulled low)
Definition: busvoodoo_global.h:81
menu_command_t::shortcut
char shortcut
short command code (0 if not available)
Definition: menu.h:32
busvoodoo_rs485_command_receive
static void busvoodoo_rs485_command_receive(void *argument)
command to receive data
Definition: busvoodoo_rs485.c:474
busvoodoo_rs485_read
static void busvoodoo_rs485_read(void)
read from RS-485
Definition: busvoodoo_rs485.c:252
busvoodoo_global_pinout_io
const char * busvoodoo_global_pinout_io[10]
I/O connector pinout.
Definition: busvoodoo_global.c:56
user_input_get
char user_input_get(void)
get user input
Definition: global.c:187
busvoodoo_rs485_action
static bool busvoodoo_rs485_action(const char *action, uint32_t repetition, bool perform)
perform RS-485 action
Definition: busvoodoo_rs485.c:308
busvoodoo_global_string
char busvoodoo_global_string[64]
shared string buffer, i.e.
Definition: busvoodoo_global.c:66
snprintf
size_t snprintf(char *str, size_t size, const char *format,...)
print format string on string or user output
Definition: print.c:458
busvoodoo_global_actions
bool busvoodoo_global_actions(char *actions, bool perform, bool(*action_handler)(const char *action, uint32_t repetition, bool perform))
parse and perform actions
Definition: busvoodoo_global.c:419
sleep_ms
void sleep_ms(uint32_t duration)
go to sleep for some milliseconds
Definition: global.c:159
USART_RX_PORT
#define USART_RX_PORT(x)
get port for USART receive pin based on USART identifier
Definition: global.h:192
busvoodoo_rs485_setting
static enum busvoodoo_rs485_setting_t busvoodoo_rs485_setting
current mode setup stage
printf
size_t printf(const char *format,...)
print format string on user output
Definition: print.c:448
busvoodoo_rs485_command_actions
static void busvoodoo_rs485_command_actions(void *argument)
command to perform actions
Definition: busvoodoo_rs485.c:409
busvoodoo_rs485_baudrate
static uint32_t busvoodoo_rs485_baudrate
RS-485 baud rate (in bps)
Definition: busvoodoo_rs485.c:55
print.h
printing utilities to replace the large printf from the standard library (API)
BUSVOODOO_RS485_DE_PIN
#define BUSVOODOO_RS485_DE_PIN
RS-485 pin to enable transmitter (active high, pulled low)
Definition: busvoodoo_global.h:82
busvoodoo_oled_text_left
void busvoodoo_oled_text_left(char *text)
draw mode text on top (yellow) left side in display buffer
Definition: busvoodoo_oled.c:356
busvoodoo_rs485_command_error
static void busvoodoo_rs485_command_error(void *argument)
command to verify incoming transmission for error
Definition: busvoodoo_rs485.c:586
LENGTH
#define LENGTH(x)
get the length of an array
Definition: global.h:26
busvoodoo_rs485_command_transceive
static void busvoodoo_rs485_command_transceive(void *argument)
command to transmit and receive data
Definition: busvoodoo_rs485.c:541
BUSVOODOO_RS485_RE_PIN
#define BUSVOODOO_RS485_RE_PIN
RS-485 pin to enable receiver (active low, pulled up)
Definition: busvoodoo_global.h:80
busvoodoo_oled.h
library to show BusVoodoo mode information on SSD1306 OLED display: name, activity, pinout (API)
user_input_available
volatile bool user_input_available
flag set when user input is available
Definition: global.c:37
busvoodoo_mode_t
BusVoodoo mode interface.
Definition: busvoodoo_global.h:113
busvoodoo_rs485.h
BusVoodoo RS-485/422 mode (API)
busvoodoo_rs485_exit
static void busvoodoo_rs485_exit(void)
exit RS-485 mode
Definition: busvoodoo_rs485.c:203
busvoodoo_rs485_parity
static uint32_t busvoodoo_rs485_parity
RS-485 parity setting.
Definition: busvoodoo_rs485.c:59
BUSVOODOO_RS485_USART
#define BUSVOODOO_RS485_USART
USART peripheral.
Definition: busvoodoo_rs485.c:42
busvoodoo_rs485_mode
const struct busvoodoo_mode_t busvoodoo_rs485_mode
RS-485 mode interface definition.
Definition: busvoodoo_rs485.c:640