[Taranis X9E] Bluetooth

- baudrate configured automatically to 115200bauds (factory
reset=9600bauds)
- device name is now "Taranis"
- Telemetry is mirrored
This commit is contained in:
bsongis
2015-07-06 23:55:58 +02:00
parent 7f65075af2
commit a56f75f3d9
9 changed files with 160 additions and 83 deletions
+1 -1
View File
@@ -197,7 +197,7 @@ void perMain()
#endif
#if defined(REV9E) && !defined(SIMU)
bt_wakeup();
bluetoothWakeup();
#endif
#if defined(PCBTARANIS)
+122 -40
View File
@@ -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;
}
+2 -2
View File
@@ -132,7 +132,7 @@ void boardInit()
init2MhzTimer();
init5msTimer();
__enable_irq();
eepromInit();
i2cInit();
usbInit();
#if defined(HAPTIC)
@@ -140,7 +140,7 @@ void boardInit()
#endif
#if defined(REV9E)
bt_open();
bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE);
#endif
#if defined(DEBUG)
+10 -7
View File
@@ -288,8 +288,8 @@ void usbStop(void);
}
#endif
// EEPROM driver
void eepromInit(void);
// I2C driver: EEPROM + Audio Volume
void i2cInit(void);
void eepromReadBlock(uint8_t * buffer, uint16_t address, uint16_t size);
void eepromWriteBlock(uint8_t * buffer, uint16_t address, uint16_t size);
@@ -321,7 +321,7 @@ void hapticOff(void);
#endif
// SERIAL_USART driver
#define DEBUG_BAUDRATE 115200
#define DEBUG_BAUDRATE 115200
void uart3Init(unsigned int mode, unsigned int protocol);
void uart3Putc(const char c);
#define telemetrySecondPortInit(protocol) uart3Init(UART_MODE_TELEMETRY, protocol)
@@ -329,10 +329,13 @@ void uart3SbusInit(void);
void uart3Stop(void);
// BT driver
int bt_open(void);
int bt_write(const void *buffer, int len);
int bt_read(void *buffer, int len);
void bt_wakeup(void);
#define BLUETOOTH_DEFAULT_BAUDRATE 115200
uint8_t bluetoothReady();
void bluetoothInit(uint32_t baudrate);
void bluetoothWrite(const void * buffer, int len);
void bluetoothWriteString(const char * str);
int bluetoothRead(void * buffer, int len);
void bluetoothWakeup(void);
// Top LCD driver
#if defined(REV9E)
+1 -24
View File
@@ -123,7 +123,6 @@ TCHAR backupFilename[60];
uint32_t Master_frequency;
volatile uint8_t Tenms;
uint8_t EE_timer;
FIL FlashFile;
DIR Dj;
@@ -423,18 +422,12 @@ int main()
lcd_putsLeft(0, BOOTLOADER_TITLE);
lcd_invert_line(0);
lcdRefresh();
#if defined(PCBSKY9X)
OptrexDisplay = 0;
lcdRefresh();
#endif
#if defined(PCBTARANIS)
#if defined(REVPLUS)
turnBacklightOn(0, 0);
#endif
keysInit();
eepromInit();
#endif
i2cInit();
__enable_irq();
init10msTimer();
@@ -444,13 +437,6 @@ int main()
init_spi();
#endif
#if defined(PCBSKY9X)
LockBits = readLockBits();
if (LockBits) {
clearLockBits();
}
#endif
#if defined(PCBTARANIS)
// SD card detect pin
sdInit();
@@ -462,15 +448,6 @@ int main()
wdt_reset();
if (Tenms) {
if (EE_timer) {
if (--EE_timer == 0) {
#if defined(PCBSKY9X)
writeBlock();
#endif
}
}
Tenms = 0;
lcdRefreshWait();
+4 -2
View File
@@ -549,8 +549,10 @@
#define BT_GPIO_PIN_RX GPIO_Pin_9 // PG.09
#define BT_GPIO_EN GPIOD
#define BT_GPIO_PIN_EN GPIO_Pin_11 // PD.11
#define BT_GPIO_BRTS GPIOB
#define BT_GPIO_PIN_BRTS GPIO_Pin_10
#define BT_GPIO_BRTS GPIOE
#define BT_GPIO_PIN_BRTS GPIO_Pin_12 // PE.12
#define BT_GPIO_BCTS GPIOG
#define BT_GPIO_PIN_BCTS GPIO_Pin_6 // PG.06
#define BT_GPIO_PinSource_TX GPIO_PinSource14
#define BT_GPIO_PinSource_RX GPIO_PinSource9
#endif
+4 -4
View File
@@ -3,7 +3,7 @@
void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite);
void eepromWaitEepromStandbyState(void);
void eepromInit(void)
void i2cInit()
{
I2C_DeInit(I2C);
@@ -113,7 +113,7 @@ bool I2C_EE_ReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRea
void eepromReadBlock(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
{
while (!I2C_EE_ReadBlock(pBuffer, ReadAddr, NumByteToRead)) {
eepromInit();
i2cInit();
}
}
@@ -192,7 +192,7 @@ bool I2C_EE_PageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWri
void eepromPageWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t NumByteToWrite)
{
while (!I2C_EE_PageWrite(pBuffer, WriteAddr, NumByteToWrite)) {
eepromInit();
i2cInit();
}
}
@@ -218,7 +218,7 @@ bool I2C_EE_WaitEepromStandbyState(void)
void eepromWaitEepromStandbyState(void)
{
while (!I2C_EE_WaitEepromStandbyState()) {
eepromInit();
i2cInit();
}
}
+13
View File
@@ -170,6 +170,19 @@ NOINLINE void processSerialData(uint8_t data)
}
#endif
#if defined(PCBTARANIS) && defined(REV9E)
#define BLUETOOTH_BUFFER_LENGTH 20
static uint8_t bluetoothBuffer[BLUETOOTH_BUFFER_LENGTH];
static uint8_t bluetoothIndex = 0;
bluetoothBuffer[bluetoothIndex++] = data;
if (bluetoothIndex == BLUETOOTH_BUFFER_LENGTH) {
if (bluetoothReady()) {
bluetoothWrite(bluetoothBuffer, BLUETOOTH_BUFFER_LENGTH);
}
bluetoothIndex = 0;
}
#endif
switch (dataState)
{
case STATE_DATA_START:
+3 -3
View File
@@ -511,11 +511,11 @@ void sportFirmwareUpdate(ModuleIndex module, const char *filename)
{
bool result = sportUpdatePowerOn(module);
if (result)
result = sportUpdateReqVersion();
result = sportUpdateReqVersion();
if (result)
result = sportUpdateUploadFile(filename);
result = sportUpdateUploadFile(filename);
if (result)
result = sportUpdateEnd();
result = sportUpdateEnd();
if (result == false) {
POPUP_WARNING("Firmware Update Error");