add library for receiving NMEA-0183 GPS messages
This commit is contained in:
parent
87b788b19e
commit
6c0c5bb7be
|
@ -0,0 +1,127 @@
|
|||
/* 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 NMEA-0183 GPS messages (code)
|
||||
* @file radio_gps.c
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: USART @ref radio_gps_usart
|
||||
*/
|
||||
|
||||
/* standard libraries */
|
||||
#include <stdint.h> // standard integer types
|
||||
#include <stdio.h> // standard I/O facilities
|
||||
#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 "radio_gps.h" // GPS header and definitions
|
||||
#include "global.h" // common methods
|
||||
|
||||
/** @defgroup radio_gps_usart USART peripheral used for communication with the GPS
|
||||
* @{
|
||||
*/
|
||||
#define RADIO_GPS_USART 3 /**< USART peripheral */
|
||||
/** @} */
|
||||
|
||||
#define RADIO_GPS_BAUDRATE 9600 /**< USART baudrate for the GPS communication, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */
|
||||
|
||||
static volatile char radio_gps_buffer[82] = {0}; /**< buffer for received data (82 chars is the maximum NMEA-0183 message length) */
|
||||
static volatile uint8_t radio_gps_buffer_i = 0; /**< current position of read received data */
|
||||
|
||||
volatile bool radio_gps_received = false;
|
||||
volatile char radio_gps_message[82+1] = {0};
|
||||
|
||||
void radio_gps_setup(void)
|
||||
{
|
||||
/* enable USART I/O peripheral */
|
||||
rcc_periph_clock_enable(USART_PORT_RCC(RADIO_GPS_USART)); // enable clock for USART port peripheral
|
||||
rcc_periph_clock_enable(USART_RCC(RADIO_GPS_USART)); // enable clock for USART peripheral
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
|
||||
gpio_set_mode(USART_PORT(RADIO_GPS_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(RADIO_GPS_USART)); // setup GPIO pin USART receive
|
||||
gpio_set(USART_PORT(RADIO_GPS_USART), USART_PIN_RX(RADIO_GPS_USART)); // pull up to avoid noise when not connected
|
||||
|
||||
/* setup USART parameters */
|
||||
usart_set_baudrate(USART(RADIO_GPS_USART), RADIO_GPS_BAUDRATE);
|
||||
usart_set_databits(USART(RADIO_GPS_USART), 8);
|
||||
usart_set_stopbits(USART(RADIO_GPS_USART), USART_STOPBITS_1);
|
||||
usart_set_mode(USART(RADIO_GPS_USART), USART_MODE_RX); // we only need to receive messages
|
||||
usart_set_parity(USART(RADIO_GPS_USART), USART_PARITY_NONE);
|
||||
usart_set_flow_control(USART(RADIO_GPS_USART), USART_FLOWCONTROL_NONE);
|
||||
|
||||
nvic_enable_irq(USART_IRQ(RADIO_GPS_USART)); // enable the USART interrupt
|
||||
usart_enable_rx_interrupt(USART(RADIO_GPS_USART)); // enable receive interrupt
|
||||
usart_enable(USART(RADIO_GPS_USART)); // enable USART
|
||||
|
||||
/* reset buffer states */
|
||||
radio_gps_received = false;
|
||||
radio_gps_buffer_i = 0;
|
||||
}
|
||||
|
||||
/** USART interrupt service routine called when data has been received */
|
||||
void USART_ISR(RADIO_GPS_USART)(void)
|
||||
{
|
||||
if (usart_get_flag(USART(RADIO_GPS_USART), USART_SR_RXNE)) { // data has been received
|
||||
char c = usart_recv(USART(RADIO_GPS_USART)); // put character in buffer (and clear flag)
|
||||
// only save data if there is space in the buffer
|
||||
if (radio_gps_buffer_i<LENGTH(radio_gps_buffer)) {
|
||||
radio_gps_buffer[radio_gps_buffer_i++] = c;
|
||||
}
|
||||
if ('\n'==c) { // end of message received
|
||||
// verify if the message is correctly formatted
|
||||
if (radio_gps_buffer_i<1 || '$'!=radio_gps_buffer[0]) {
|
||||
goto malformatted;
|
||||
} else if (radio_gps_buffer_i<2 || '\r'!=radio_gps_buffer[radio_gps_buffer_i-2]) {
|
||||
goto malformatted;
|
||||
} else { // verify checksum when available
|
||||
uint8_t checksum_calc = 0;
|
||||
uint8_t checksum_i = 0;
|
||||
bool checkum_present = false;
|
||||
for (checksum_i = 1; checksum_i < radio_gps_buffer_i-2; checksum_i++) {
|
||||
if ('*'==radio_gps_buffer[checksum_i]) {
|
||||
checkum_present = true;
|
||||
break;
|
||||
} else {
|
||||
checksum_calc ^= radio_gps_buffer[checksum_i];
|
||||
}
|
||||
}
|
||||
if (checkum_present) {
|
||||
if (checksum_i!=radio_gps_buffer_i-5) { // only 2 checksum character should be remaining
|
||||
goto malformatted;
|
||||
} else if (checksum_calc-(radio_gps_buffer[checksum_i+1]-'0')*0x10-(radio_gps_buffer[checksum_i+2]-'0')) { // checksum does not match
|
||||
goto malformatted;
|
||||
}
|
||||
}
|
||||
}
|
||||
// copy message to user
|
||||
for (uint8_t i = 0; i < radio_gps_buffer_i && i < LENGTH(radio_gps_message); i++) {
|
||||
radio_gps_message[i] = radio_gps_buffer[i];
|
||||
}
|
||||
// terminate string
|
||||
if (radio_gps_buffer_i<LENGTH(radio_gps_message)) {
|
||||
radio_gps_message[radio_gps_buffer_i] = '\0';
|
||||
} else {
|
||||
radio_gps_message[LENGTH(radio_gps_message)-1] = '\0';
|
||||
}
|
||||
radio_gps_received = true; // notify user a message is available
|
||||
malformatted:
|
||||
radio_gps_buffer_i = 0; // reset buffer
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/* 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 NMEA-0183 GPS messages (API)
|
||||
* @file radio_gps.h
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2017
|
||||
* @note peripherals used: USART @ref radio_gps_usart
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
/** has a NMEA-0183 GPS message been received */
|
||||
extern volatile bool radio_gps_received;
|
||||
/** last valid NMEA-0183 GPS message received (including \r\n ending and \0) */
|
||||
extern volatile char radio_gps_message[82+1];
|
||||
|
||||
/** setup USART peripheral for GPS communication */
|
||||
void radio_gps_setup(void);
|
Loading…
Reference in New Issue