140 lines
6.6 KiB
C
140 lines
6.6 KiB
C
/* 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/>.
|
|
*
|
|
*/
|
|
/** library for receiving forumslader NMEA-0183 like messages (code)
|
|
* @file sensor_forumslader.c
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
* @date 2017
|
|
* @note peripherals used: USART @ref sensor_forumslader_usart
|
|
*/
|
|
/* standard libraries */
|
|
#include <stdint.h> // standard integer types
|
|
#include <stdio.h> // standard I/O facilities
|
|
#include <stdlib.h> // general utilities
|
|
#include <string.h> // string 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 "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<strlen(message); i++) { // copy message to transmit to internal buffer
|
|
tx_buffer[strlen(message)-1-i] = message[i]; // copy in reverse orders to we can count remaining characters to transmit
|
|
}
|
|
tx_buffer_i = strlen(message); // save number of characters to transmit
|
|
sensor_forumslader_transmitted = false; // reset transmit flag
|
|
usart_enable_tx_interrupt(USART(SENSOR_FORUMSLADER_USART)); // enable transmit interrupt
|
|
return true;
|
|
}
|
|
|
|
|
|
/** USART interrupt service routine called when data has been received */
|
|
void USART_ISR(SENSOR_FORUMSLADER_USART)(void)
|
|
{
|
|
if (usart_get_flag(USART(SENSOR_FORUMSLADER_USART), USART_SR_RXNE)) { // data has been received
|
|
char c = usart_recv(USART(SENSOR_FORUMSLADER_USART)); // put character in buffer (and clear flag)
|
|
// only save data if there is space in the buffer
|
|
if (rx_buffer_i<LENGTH(rx_buffer)) {
|
|
rx_buffer[rx_buffer_i++] = c;
|
|
}
|
|
if ('\n'==c) { // end of message received
|
|
// verify if the message is correctly formatted
|
|
if (rx_buffer_i<1 || '$'!=rx_buffer[0]) {
|
|
goto malformatted;
|
|
} else if (rx_buffer_i<3 || ';'!=rx_buffer[rx_buffer_i-3] || '\r'!=rx_buffer[rx_buffer_i-2]) {
|
|
goto malformatted;
|
|
}
|
|
// copy message to user
|
|
for (uint8_t i = 0; i < rx_buffer_i && i < LENGTH(sensor_forumslader_message); i++) {
|
|
sensor_forumslader_message[i] = rx_buffer[i];
|
|
}
|
|
// terminate string
|
|
if (rx_buffer_i<LENGTH(sensor_forumslader_message)) {
|
|
sensor_forumslader_message[rx_buffer_i] = '\0';
|
|
} else {
|
|
sensor_forumslader_message[LENGTH(sensor_forumslader_message)-1] = '\0';
|
|
}
|
|
sensor_forumslader_received = true; // notify user a message is available
|
|
malformatted:
|
|
rx_buffer_i = 0; // reset buffer
|
|
}
|
|
}
|
|
if (usart_get_flag(USART(SENSOR_FORUMSLADER_USART), USART_SR_TXE) && tx_buffer_i) { // data has been transmitted
|
|
usart_send(USART(SENSOR_FORUMSLADER_USART),tx_buffer[--tx_buffer_i]); // put data in transmit register
|
|
if (0==tx_buffer_i) { // no data in the buffer to transmit
|
|
usart_disable_tx_interrupt(USART(SENSOR_FORUMSLADER_USART)); // disable transmit interrupt
|
|
sensor_forumslader_transmitted = true; // notify user
|
|
}
|
|
}
|
|
}
|