aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2020-02-19 21:19:54 +0100
committerKing Kévin <kingkevin@cuvoodoo.info>2020-02-19 21:23:05 +0100
commita5a0ae84d1e5358f6f1da712178fc0e5b02ced1d (patch)
tree474c6bec36dce034aa4bb472d68c422fdbfeafe7
parent5f5c9c0b864aec1d19454b7a895786e8e7571e4b (diff)
spp_rx: add library to get UART data from Bluetooth module
-rw-r--r--lib/spp_rx.c102
-rw-r--r--lib/spp_rx.h33
2 files changed, 135 insertions, 0 deletions
diff --git a/lib/spp_rx.c b/lib/spp_rx.c
new file mode 100644
index 0000000..810dd05
--- /dev/null
+++ b/lib/spp_rx.c
@@ -0,0 +1,102 @@
+/* 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 UART communication (code)
+ * @file
+ * @author King Kévin <kingkevin@cuvoodoo.info>
+ * @date 2016-2020
+ * @note peripherals used: USART @ref uart
+ */
+
+/* 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 "spp_rx.h" // own definitions
+#include "global.h" // common methods
+
+/** @defgroup spp_uart USART peripheral used for UART communication
+ * @{
+ */
+#define SPP_UART_ID 3 /**< USART peripheral */
+/** @} */
+
+#define UART_BAUDRATE 115200 /**< serial baud rate, in bits per second (with 8N1 8 bits, no parity bit, 1 stop bit settings) */
+
+volatile bool spp_rx_input_available = false;
+
+/* input ring buffer, indexes, and available memory */
+static volatile uint8_t spp_rx_input_buffer[64] = {0}; /**< ring buffer for received data */
+static volatile uint8_t spp_rx_input_i = 0; /**< current position of read received data */
+static volatile uint8_t spp_rx_input_used = 0; /**< how much data has been received and not red */
+
+void spp_rx_setup(void)
+{
+ /* enable UART I/O peripheral */
+ rcc_periph_clock_enable(RCC_USART_PORT(SPP_UART_ID)); // enable clock for UART port peripheral
+ rcc_periph_clock_enable(RCC_USART(SPP_UART_ID)); // enable clock for UART peripheral
+ rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (UART)
+ gpio_set_mode(USART_RX_PORT(SPP_UART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(SPP_UART_ID)); // setup GPIO pin UART receive
+ gpio_set(USART_RX_PORT(SPP_UART_ID), USART_RX_PIN(SPP_UART_ID)); // pull up to avoid noise when not connected
+
+ /* setup UART parameters */
+ usart_set_baudrate(USART(SPP_UART_ID), UART_BAUDRATE);
+ usart_set_databits(USART(SPP_UART_ID), 8);
+ usart_set_stopbits(USART(SPP_UART_ID), USART_STOPBITS_1);
+ usart_set_mode(USART(SPP_UART_ID), USART_MODE_RX);
+ usart_set_parity(USART(SPP_UART_ID), USART_PARITY_NONE);
+ usart_set_flow_control(USART(SPP_UART_ID), USART_FLOWCONTROL_NONE);
+
+ nvic_enable_irq(USART_IRQ(SPP_UART_ID)); // enable the UART interrupt
+ usart_enable_rx_interrupt(USART(SPP_UART_ID)); // enable receive interrupt
+ usart_enable(USART(SPP_UART_ID)); // enable UART
+
+ /* reset buffer states */
+ spp_rx_input_i = 0;
+ spp_rx_input_used = 0;
+}
+
+char spp_rx_input_get(void)
+{
+ if (0 == spp_rx_input_used) { // no data is available
+ return 0;
+ }
+ volatile char to_return = spp_rx_input_buffer[spp_rx_input_i]; // get the next available character
+ // there is no need to disable receive interrupt since it won't corrupt the index
+ spp_rx_input_i = (spp_rx_input_i + 1) % LENGTH(spp_rx_input_buffer); // update used buffer
+ spp_rx_input_used--; // update used buffer
+ spp_rx_input_available = (spp_rx_input_used != 0); // update available data
+ return to_return;
+}
+
+/** UART interrupt service routine called when data has been transmitted or received */
+void USART_ISR(SPP_UART_ID)(void)
+{
+ while (usart_get_flag(USART(SPP_UART_ID), USART_SR_RXNE)) { // data has been received (repeat while receiving)
+ char c = usart_recv(USART(SPP_UART_ID)); // save character and free UART buffer (also clears flag)
+ if (spp_rx_input_used < LENGTH(spp_rx_input_buffer)) { // only save data if there is space in the buffer
+ spp_rx_input_buffer[(spp_rx_input_i + spp_rx_input_used) % LENGTH(spp_rx_input_buffer)] = c; // put character in buffer
+ spp_rx_input_used++; // update used buffer
+ spp_rx_input_available = true; // update available data
+ }
+ }
+}
diff --git a/lib/spp_rx.h b/lib/spp_rx.h
new file mode 100644
index 0000000..9afe048
--- /dev/null
+++ b/lib/spp_rx.h
@@ -0,0 +1,33 @@
+/* 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 to receive data from a Bluetooth SPP module
+ * @file
+ * @author King Kévin <kingkevin@cuvoodoo.info>
+ * @date 2016-2020
+ * @note peripherals used: USART @ref spp_uart
+ */
+#pragma once
+
+/** flag set when data has been received and is available */
+extern volatile bool spp_rx_input_available;
+
+/** setup UART peripheral */
+void spp_rx_setup(void);
+/** get the data received from Bluetooth SPP module
+ * @return available data
+ * @warning returns 0 when no data is available
+ * @note sets spp_rx_input_available to indicate if more data is available
+ */
+char spp_rx_input_get(void);