From abe631b1ebf8b5465d0e23a8b368e85d0bffcae8 Mon Sep 17 00:00:00 2001 From: bsongis Date: Sat, 31 Jan 2015 14:20:56 +0100 Subject: [PATCH] BT driver added (I only converted Adela's one to STM32F2 and got rid of FreeRTOS) --- radio/src/Makefile | 1 + radio/src/fifo.h | 6 +- .../STM32F2xx_StdPeriph_Driver/inc/misc.h | 2 +- .../src/targets/taranis/bluetooth_driver.cpp | 167 ++++++++++++++++++ radio/src/targets/taranis/board_taranis.h | 5 +- 5 files changed, 176 insertions(+), 5 deletions(-) create mode 100755 radio/src/targets/taranis/bluetooth_driver.cpp diff --git a/radio/src/Makefile b/radio/src/Makefile index 1c59a2b28..780deb7e4 100644 --- a/radio/src/Makefile +++ b/radio/src/Makefile @@ -772,6 +772,7 @@ ifeq ($(PCB), TARANIS) CPPDEFS += -DLCD_DUAL_BUFFER endif HAPTIC = YES + EXTRABOARDSRC += targets/taranis/bluetooth_driver.cpp else ifeq ($(PCBREV), REVPLUS) FLAVOUR = taranis-plus CPPDEFS = -DREVPLUS diff --git a/radio/src/fifo.h b/radio/src/fifo.h index bb18dab09..dae8032fa 100644 --- a/radio/src/fifo.h +++ b/radio/src/fifo.h @@ -34,8 +34,8 @@ * */ -#ifndef fifo_h -#define fifo_h +#ifndef _FIFO_H_ +#define _FIFO_H_ template class Fifo @@ -65,9 +65,11 @@ class Fifo return true; } } + bool empty() { return (ridx == widx); } + void flush() { while (!empty()) {}; } diff --git a/radio/src/targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/misc.h b/radio/src/targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/misc.h index aec4c361b..1f18fc8dd 100644 --- a/radio/src/targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/misc.h +++ b/radio/src/targets/taranis/STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/misc.h @@ -35,7 +35,7 @@ #endif /* Includes ------------------------------------------------------------------*/ -#include "stm32f2xx.h" +#include "../../CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h" /** @addtogroup STM32F2xx_StdPeriph_Driver * @{ diff --git a/radio/src/targets/taranis/bluetooth_driver.cpp b/radio/src/targets/taranis/bluetooth_driver.cpp new file mode 100755 index 000000000..3ab736fcc --- /dev/null +++ b/radio/src/targets/taranis/bluetooth_driver.cpp @@ -0,0 +1,167 @@ +/** + * @file bluetooth_driver.c + * @version V1.0.0 + * @date 2014-11-10 + * @brief bluetooth driver for Tananis. + * @author - Adela + * - Robert Zhang + * - + */ + +#include "board_taranis.h" +#include "string.h" +#include "../../fifo.h" + +#define GPIO_EN_PIN GPIO_Pin_10 +#define GPIO_EN_PORT GPIOG + +#define GPIO_EN_GPIO_CLK RCC_AHB1Periph_GPIOG + +#define GPIO_BRTS_GPIO_PIN GPIO_Pin_10 +#define GPIO_BRTS_GPIO_PORT GPIOB +#define GPIO_BRTS_GPIO_CLK RCC_AHB1Periph_GPIOB + +#define GPIO_BCTS_GPIO_PIN GPIO_Pin_11 +#define GPIO_BCTS_GPIO_PORT GPIOB +#define GPIO_BCTS_GPIO_CLK RCC_AHB1Periph_GPIOB + +#define BT_UART USART1 +#define BT_UART_CLK RCC_APB2Periph_USART1 +#define BT_UART_AF GPIO_AF_USART1 +#define BT_UART_IRQn USART1_IRQn + +#define BT_UART_GPIO_PIN_TX GPIO_Pin_6 +#define BT_UART_GPIO_PIN_RX GPIO_Pin_7 +#define BT_UART_GPIO_TX_PinSource GPIO_PinSource6 +#define BT_UART_GPIO_RX_PinSource GPIO_PinSource7 +#define BT_UART_GPIO_PORT GPIOB + +Fifo<200> btTxFifo; +Fifo<200> btRxFifo; + +int bt_open() +{ + GPIO_InitTypeDef GPIO_InitStructure; + USART_InitTypeDef USART_InitStructure; + + RCC_AHB1PeriphClockCmd( GPIO_BRTS_GPIO_CLK, ENABLE); + RCC_AHB1PeriphClockCmd( GPIO_EN_GPIO_CLK, ENABLE); + RCC_APB2PeriphClockCmd(BT_UART_CLK, ENABLE); + + //PG10:as bluetooth EN + GPIO_InitStructure.GPIO_Pin = GPIO_EN_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(GPIO_EN_PORT, &GPIO_InitStructure); + + GPIO_SetBits(GPIO_EN_PORT, GPIO_EN_PIN); + + //PB10,PB11 + GPIO_InitStructure.GPIO_Pin = GPIO_BRTS_GPIO_PIN | GPIO_BCTS_GPIO_PIN; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(GPIO_BRTS_GPIO_PORT, &GPIO_InitStructure); + + //PB6:as TXD + //PB7:as RXD + GPIO_PinAFConfig(BT_UART_GPIO_PORT, BT_UART_GPIO_TX_PinSource, BT_UART_AF); + GPIO_PinAFConfig(BT_UART_GPIO_PORT, BT_UART_GPIO_RX_PinSource, BT_UART_AF); + + GPIO_InitStructure.GPIO_Pin = BT_UART_GPIO_PIN_TX; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(BT_UART_GPIO_PORT, &GPIO_InitStructure); + + GPIO_InitStructure.GPIO_Pin = BT_UART_GPIO_PIN_RX; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; + GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(BT_UART_GPIO_PORT, &GPIO_InitStructure); + + USART_InitStructure.USART_BaudRate = 115200; + USART_InitStructure.USART_Parity = USART_Parity_No; + USART_InitStructure.USART_StopBits = USART_StopBits_1; + USART_InitStructure.USART_WordLength = USART_WordLength_8b; + USART_InitStructure.USART_HardwareFlowControl = + USART_HardwareFlowControl_None; + USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; + USART_Init(BT_UART, &USART_InitStructure); + + USART_Cmd(BT_UART, ENABLE); + USART_ITConfig(BT_UART, USART_IT_RXNE, ENABLE); + + GPIO_ResetBits(GPIO_EN_PORT, GPIO_EN_PIN); //open bluetooth + + NVIC_InitTypeDef NVIC_InitStructure; + NVIC_InitStructure.NVIC_IRQChannel = BT_UART_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 8; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + return 0; +} + +extern "C" void USART1_IRQHandler(void) +{ +#if 1 + if (USART_GetITStatus(BT_UART, USART_IT_RXNE) != RESET) { + USART_ClearITPendingBit(BT_UART, USART_IT_RXNE); + uint8_t byte = USART_ReceiveData(BT_UART); + btRxFifo.push(byte); + } +#endif + +#if 1 + if (USART_GetITStatus(BT_UART, USART_IT_TXE) != RESET) { + uint8_t byte; + bool result = btTxFifo.pop(byte); + if (result) { + USART_SendData(BT_UART, byte); + } + else { + USART_ITConfig(BT_UART, USART_IT_TXE, DISABLE); + } + } +#endif +} + +int bt_write(void *buffer, int len) +{ + uint8_t *data = (uint8_t *)buffer; + for (int i=0; i= len) + return result; + } +} + +int bt_close() +{ + GPIO_SetBits(GPIO_EN_PORT, GPIO_EN_PIN); + return 0; +} diff --git a/radio/src/targets/taranis/board_taranis.h b/radio/src/targets/taranis/board_taranis.h index 3dce1d90b..8f58ee867 100644 --- a/radio/src/targets/taranis/board_taranis.h +++ b/radio/src/targets/taranis/board_taranis.h @@ -34,8 +34,8 @@ * */ -#ifndef board_taranis_h -#define board_taranis_h +#ifndef _BOARD_TARANIS_H_ +#define _BOARD_TARANIS_H_ #include "stddef.h" @@ -51,6 +51,7 @@ extern "C" { #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_dma.h" #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/stm32f2xx_usart.h" #include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/CMSIS/Device/ST/STM32F2xx/Include/stm32f2xx.h" +#include "STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries/STM32F2xx_StdPeriph_Driver/inc/misc.h" #if !defined(SIMU) #include "STM32_USB-Host-Device_Lib_V2.1.0/Libraries/STM32_USB_Device_Library/Class/msc/inc/usbd_msc_core.h"