[Taranis X9E] Bluetooth
- baudrate configured automatically to 115200bauds (factory reset=9600bauds) - device name is now "Taranis" - Telemetry is mirrored
This commit is contained in:
@@ -12,26 +12,33 @@
|
||||
#include "string.h"
|
||||
#include "../../fifo.h"
|
||||
|
||||
Fifo<200> btTxFifo;
|
||||
Fifo<200> btRxFifo;
|
||||
Fifo<64> btTxFifo;
|
||||
Fifo<64> btRxFifo;
|
||||
|
||||
int bt_open()
|
||||
void bluetoothInit(uint32_t baudrate)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
|
||||
USART_DeInit(BT_USART);
|
||||
|
||||
RCC_AHB1PeriphClockCmd(BT_RCC_AHB1Periph, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_EN;
|
||||
GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_BRTS;
|
||||
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(BT_GPIO_EN, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_BRTS;
|
||||
GPIO_Init(BT_GPIO_BRTS, &GPIO_InitStructure);
|
||||
GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_EN;
|
||||
GPIO_Init(BT_GPIO_EN, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_BCTS;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
||||
GPIO_Init(BT_GPIO_BCTS, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_GPIO_PIN_TX|BT_GPIO_PIN_RX;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
@@ -41,7 +48,7 @@ int bt_open()
|
||||
GPIO_PinAFConfig(BT_GPIO_TXRX, BT_GPIO_PinSource_TX, BT_GPIO_AF);
|
||||
GPIO_PinAFConfig(BT_GPIO_TXRX, BT_GPIO_PinSource_RX, BT_GPIO_AF);
|
||||
|
||||
USART_InitStructure.USART_BaudRate = 115200;
|
||||
USART_InitStructure.USART_BaudRate = baudrate;
|
||||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
||||
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
||||
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
||||
@@ -59,25 +66,36 @@ int bt_open()
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS);
|
||||
GPIO_ResetBits(BT_GPIO_EN, BT_GPIO_PIN_EN); // open bluetooth
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t bt_state = 0;
|
||||
enum BluetoothState
|
||||
{
|
||||
BLUETOOTH_INIT,
|
||||
BLUETOOTH_WAIT_TTM,
|
||||
BLUETOOTH_WAIT_BAUDRATE_CHANGE,
|
||||
BLUETOOTH_OK,
|
||||
};
|
||||
|
||||
enum BluetoothWriteState
|
||||
{
|
||||
BLUETOOTH_WRITE_IDLE,
|
||||
BLUETOOTH_WRITE_INIT,
|
||||
BLUETOOTH_WRITING,
|
||||
BLUETOOTH_WRITE_DONE
|
||||
};
|
||||
|
||||
volatile uint8_t bluetoothState = BLUETOOTH_INIT;
|
||||
volatile uint8_t bluetoothWriteState = BLUETOOTH_WRITE_IDLE;
|
||||
|
||||
extern "C" void USART6_IRQHandler(void)
|
||||
{
|
||||
#if 1
|
||||
if (USART_GetITStatus(BT_USART, USART_IT_RXNE) != RESET) {
|
||||
USART_ClearITPendingBit(BT_USART, USART_IT_RXNE);
|
||||
uint8_t byte = USART_ReceiveData(BT_USART);
|
||||
btRxFifo.push(byte);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if 1
|
||||
if (USART_GetITStatus(BT_USART, USART_IT_TXE) != RESET) {
|
||||
uint8_t byte;
|
||||
bool result = btTxFifo.pop(byte);
|
||||
@@ -86,52 +104,116 @@ extern "C" void USART6_IRQHandler(void)
|
||||
}
|
||||
else {
|
||||
USART_ITConfig(BT_USART, USART_IT_TXE, DISABLE);
|
||||
GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS);
|
||||
bt_state = 0;
|
||||
bluetoothWriteState = BLUETOOTH_WRITE_DONE;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int bt_write(const void *buffer, int len)
|
||||
void bluetoothWrite(const void * buffer, int len)
|
||||
{
|
||||
uint8_t *data = (uint8_t *)buffer;
|
||||
uint8_t * data = (uint8_t *)buffer;
|
||||
for (int i=0; i<len; ++i) {
|
||||
btTxFifo.push(data[i]);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bt_wakeup(void)
|
||||
void bluetoothWriteString(const char * str)
|
||||
{
|
||||
if (bt_state < 2 && !btTxFifo.empty()) {
|
||||
if (bt_state == 0) {
|
||||
GPIO_ResetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS);
|
||||
bt_state = 1;
|
||||
}
|
||||
else {
|
||||
USART_ITConfig(BT_USART, USART_IT_TXE, ENABLE);
|
||||
bt_state = 2;
|
||||
}
|
||||
while (*str != 0) {
|
||||
btTxFifo.push(*str++);
|
||||
}
|
||||
}
|
||||
|
||||
int bt_read(void *buffer, int len)
|
||||
void bluetoothWriteWakeup(void)
|
||||
{
|
||||
if (bluetoothWriteState == BLUETOOTH_WRITE_IDLE) {
|
||||
if (!btTxFifo.empty()) {
|
||||
bluetoothWriteState = BLUETOOTH_WRITE_INIT;
|
||||
GPIO_ResetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS);
|
||||
}
|
||||
}
|
||||
else if (bluetoothWriteState == BLUETOOTH_WRITE_INIT) {
|
||||
bluetoothWriteState = BLUETOOTH_WRITING;
|
||||
USART_ITConfig(BT_USART, USART_IT_TXE, ENABLE);
|
||||
}
|
||||
else if (bluetoothWriteState == BLUETOOTH_WRITE_DONE) {
|
||||
bluetoothWriteState = BLUETOOTH_WRITE_IDLE;
|
||||
GPIO_SetBits(BT_GPIO_BRTS, BT_GPIO_PIN_BRTS);
|
||||
}
|
||||
}
|
||||
|
||||
void bluetoothWakeup(void)
|
||||
{
|
||||
if (bluetoothState != BLUETOOTH_OK) {
|
||||
static tmr10ms_t waitEnd = 0;
|
||||
|
||||
if (bluetoothState == BLUETOOTH_INIT) {
|
||||
const char btMessage[] = "TTM:REN-Taranis";
|
||||
bluetoothWriteString(btMessage);
|
||||
bluetoothState = BLUETOOTH_WAIT_TTM;
|
||||
waitEnd = get_tmr10ms() + 25/*250*s*/;
|
||||
}
|
||||
else if (bluetoothState == BLUETOOTH_WAIT_TTM) {
|
||||
if (get_tmr10ms() > waitEnd) {
|
||||
char ttm[] = "TTM:REN";
|
||||
int index = 0;
|
||||
uint8_t c;
|
||||
bool found = false;
|
||||
while (btRxFifo.pop(c)) {
|
||||
if (c == ttm[index]) {
|
||||
index++;
|
||||
if (index == sizeof(ttm)-1) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
bluetoothState = BLUETOOTH_OK;
|
||||
}
|
||||
else {
|
||||
bluetoothInit(9600);
|
||||
const char btMessage[] = "TTM:BPS-115200";
|
||||
bluetoothWriteString(btMessage);
|
||||
bluetoothState = BLUETOOTH_WAIT_BAUDRATE_CHANGE;
|
||||
waitEnd = get_tmr10ms() + 250;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (bluetoothState == BLUETOOTH_WAIT_BAUDRATE_CHANGE) {
|
||||
if (get_tmr10ms() > waitEnd) {
|
||||
bluetoothInit(115200);
|
||||
bluetoothState = BLUETOOTH_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bluetoothWriteWakeup();
|
||||
}
|
||||
|
||||
uint8_t bluetoothReady()
|
||||
{
|
||||
return (bluetoothState == BLUETOOTH_OK);
|
||||
}
|
||||
|
||||
int bluetoothRead(void * buffer, int len)
|
||||
{
|
||||
int result = 0;
|
||||
uint8_t *data = (uint8_t *)buffer;
|
||||
while (1) {
|
||||
uint8_t * data = (uint8_t *)buffer;
|
||||
while (result < len) {
|
||||
uint8_t byte;
|
||||
if (!btRxFifo.pop(byte))
|
||||
return result;
|
||||
if (!btRxFifo.pop(byte)) {
|
||||
break;
|
||||
}
|
||||
data[result++] = byte;
|
||||
if (result >= len)
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int bt_close()
|
||||
void bluetoothDone()
|
||||
{
|
||||
GPIO_SetBits(BT_GPIO_EN, BT_GPIO_PIN_EN); // close bluetooth
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user