/* 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 .
*
*/
/** library for receiving forumslader NMEA-0183 like messages (code)
* @file sensor_forumslader.c
* @author King Kévin
* @date 2017
* @note peripherals used: USART @ref sensor_forumslader_usart
*/
/* standard libraries */
#include // standard integer types
#include // standard I/O facilities
#include // general utilities
#include // string utilities
/* STM32 (including CM3) libraries */
#include // real-time control clock library
#include // general purpose input output library
#include // universal synchronous asynchronous receiver transmitter library
#include // interrupt handler
#include // Cortex M3 utilities
#include "sensor_forumslader.h" // forumslader header and definitions
#include "global.h" // common methods
/** @defgroup sensor_forumslader_usart USART peripheral used for communication with the forumslader
* @{
*/
#define SENSOR_FORUMSLADER_USART 1 /**< USART peripheral */
/** @} */
#define SENSOR_FORUMSLADER_BAUDRATE 9600 /**< USART baudrate for the forumslader communication, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */
static volatile char rx_buffer[82] = {0}; /**< buffer for received data (82 chars is the maximum NMEA-0183 message length) */
static volatile uint8_t rx_buffer_i = 0; /**< current position of read received data */
static volatile char tx_buffer[82] = {0}; /**< buffer to transmit data */
static volatile uint8_t tx_buffer_i = 0; /**< number or remaining characters to transmit */
volatile bool sensor_forumslader_received = false;
volatile bool sensor_forumslader_transmitted = false;
volatile char sensor_forumslader_message[82+1] = {0};
void sensor_forumslader_setup(void)
{
/* enable USART I/O peripheral */
rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_FORUMSLADER_USART)); // enable clock for USART port peripheral
rcc_periph_clock_enable(USART_RCC(SENSOR_FORUMSLADER_USART)); // enable clock for USART peripheral
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
gpio_set_mode(USART_PORT(SENSOR_FORUMSLADER_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(SENSOR_FORUMSLADER_USART)); // setup GPIO pin USART transmit
gpio_set_mode(USART_PORT(SENSOR_FORUMSLADER_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(SENSOR_FORUMSLADER_USART)); // setup GPIO pin USART receive
gpio_set(USART_PORT(SENSOR_FORUMSLADER_USART), USART_PIN_RX(SENSOR_FORUMSLADER_USART)); // pull up to avoid noise when not connected
/* setup USART parameters */
usart_set_baudrate(USART(SENSOR_FORUMSLADER_USART), SENSOR_FORUMSLADER_BAUDRATE); // set baud rate
usart_set_databits(USART(SENSOR_FORUMSLADER_USART), 8); // set data bits
usart_set_stopbits(USART(SENSOR_FORUMSLADER_USART), USART_STOPBITS_1); // set stop bit
usart_set_mode(USART(SENSOR_FORUMSLADER_USART), USART_MODE_TX_RX); // enable transmit and receive
usart_set_parity(USART(SENSOR_FORUMSLADER_USART), USART_PARITY_NONE); // set parity
usart_set_flow_control(USART(SENSOR_FORUMSLADER_USART), USART_FLOWCONTROL_NONE); // set flow control
nvic_enable_irq(USART_IRQ(SENSOR_FORUMSLADER_USART)); // enable the USART interrupt
usart_enable_rx_interrupt(USART(SENSOR_FORUMSLADER_USART)); // enable receive interrupt
usart_enable(USART(SENSOR_FORUMSLADER_USART)); // enable USART
/* reset buffer states */
sensor_forumslader_received = false;
rx_buffer_i = 0;
sensor_forumslader_transmitted = false;
tx_buffer_i = 0;
}
bool sensor_forumslader_transmit(const char* message)
{
if (strlen(message)>LENGTH(tx_buffer)) { // message to transmit is too long
return false;
}
while (tx_buffer_i) { // idle until buffer is empty
__WFI(); // sleep until interrupt
}
for (uint8_t i=0; i