stm32f1/lib/radio_bluetooth.c

140 lines
6.4 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 NMEA-0183 like messages over a Bluetooth module (code)
* @file radio_bluetooth.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2017
* @note peripherals used: USART @ref radio_bluetooth_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 "radio_bluetooth.h" // forumslader header and definitions
#include "global.h" // common methods
/** @defgroup radio_bluetooth_usart USART peripheral used for communication with the Bluetooth module
* @{
*/
#define RADIO_BLUETOOTH_USART 2 /**< USART peripheral */
/** @} */
#define RADIO_BLUETOOTH_BAUDRATE 115200 /**< USART baud rate for the Bluetooth 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 radio_bluetooth_received = false;
volatile bool radio_bluetooth_transmitted = false;
volatile char radio_bluetooth_message[82+1] = {0};
void radio_bluetooth_setup(void)
{
/* enable USART I/O peripheral */
rcc_periph_clock_enable(USART_PORT_RCC(RADIO_BLUETOOTH_USART)); // enable clock for USART port peripheral
rcc_periph_clock_enable(USART_RCC(RADIO_BLUETOOTH_USART)); // enable clock for USART peripheral
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
gpio_set_mode(USART_PORT(RADIO_BLUETOOTH_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(RADIO_BLUETOOTH_USART)); // setup GPIO pin USART transmit
gpio_set_mode(USART_PORT(RADIO_BLUETOOTH_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(RADIO_BLUETOOTH_USART)); // setup GPIO pin USART receive
gpio_set(USART_PORT(RADIO_BLUETOOTH_USART), USART_PIN_RX(RADIO_BLUETOOTH_USART)); // pull up to avoid noise when not connected
/* setup USART parameters */
usart_set_baudrate(USART(RADIO_BLUETOOTH_USART), RADIO_BLUETOOTH_BAUDRATE); // set baud rate
usart_set_databits(USART(RADIO_BLUETOOTH_USART), 8); // set data bits
usart_set_stopbits(USART(RADIO_BLUETOOTH_USART), USART_STOPBITS_1); // set stop bit
usart_set_mode(USART(RADIO_BLUETOOTH_USART), USART_MODE_TX_RX); // enable transmit and receive
usart_set_parity(USART(RADIO_BLUETOOTH_USART), USART_PARITY_NONE); // set parity
usart_set_flow_control(USART(RADIO_BLUETOOTH_USART), USART_FLOWCONTROL_NONE); // set flow control
nvic_enable_irq(USART_IRQ(RADIO_BLUETOOTH_USART)); // enable the USART interrupt
usart_enable_rx_interrupt(USART(RADIO_BLUETOOTH_USART)); // enable receive interrupt
usart_enable(USART(RADIO_BLUETOOTH_USART)); // enable USART
/* reset buffer states */
radio_bluetooth_received = false;
rx_buffer_i = 0;
radio_bluetooth_transmitted = false;
tx_buffer_i = 0;
}
bool radio_bluetooth_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
radio_bluetooth_transmitted = false; // reset transmit flag
usart_enable_tx_interrupt(USART(RADIO_BLUETOOTH_USART)); // enable transmit interrupt
return true;
}
/** USART interrupt service routine called when data has been received */
void USART_ISR(RADIO_BLUETOOTH_USART)(void)
{
if (usart_get_flag(USART(RADIO_BLUETOOTH_USART), USART_SR_RXNE)) { // data has been received
char c = usart_recv(USART(RADIO_BLUETOOTH_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<2 || '\r'!=rx_buffer[rx_buffer_i-2]) {
goto malformatted;
}
// copy message to user
for (uint8_t i = 0; i < rx_buffer_i && i < LENGTH(radio_bluetooth_message); i++) {
radio_bluetooth_message[i] = rx_buffer[i];
}
// terminate string
if (rx_buffer_i<LENGTH(radio_bluetooth_message)) {
radio_bluetooth_message[rx_buffer_i] = '\0';
} else {
radio_bluetooth_message[LENGTH(radio_bluetooth_message)-1] = '\0';
}
radio_bluetooth_received = true; // notify user a message is available
malformatted:
rx_buffer_i = 0; // reset buffer
}
}
if (usart_get_flag(USART(RADIO_BLUETOOTH_USART), USART_SR_TXE) && tx_buffer_i) { // data has been transmitted
usart_send(USART(RADIO_BLUETOOTH_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(RADIO_BLUETOOTH_USART)); // disable transmit interrupt
radio_bluetooth_transmitted = true; // notify user
}
}
}