/* 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 like messages over a Bluetooth module (code) * @file radio_bluetooth.c * @author King Kévin * @date 2017 * @note peripherals used: USART @ref radio_bluetooth_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 "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