/* 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 NMEA-0183 GPS messages (code) * @file radio_gps.c * @author King Kévin * @date 2017 * @note peripherals used: USART @ref radio_gps_usart */ /* standard libraries */ #include // standard integer types #include // standard I/O facilities #include // general 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 "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