[Taranis X9E] Bluetooth driver
This commit is contained in:
@@ -203,4 +203,8 @@ void perMain()
|
||||
topLcdRefreshEnd();
|
||||
#endif
|
||||
|
||||
#if defined(REV9E) && !defined(SIMU)
|
||||
bt_wakeup();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,16 @@
|
||||
#define BT_UART_AF GPIO_AF_USART6
|
||||
#define BT_UART_IRQn USART6_IRQn
|
||||
|
||||
#define BT_UART_GPIO_PORT GPIOG
|
||||
#define BT_UART_GPIO_CLK_TXRX RCC_AHB1Periph_GPIOG
|
||||
#define BT_UART_GPIO_PORT_TXRX GPIOG
|
||||
#define BT_UART_GPIO_PIN_TX GPIO_Pin_14
|
||||
#define BT_UART_GPIO_PIN_RX GPIO_Pin_9
|
||||
#define BT_UART_GPIO_CLK_EN RCC_AHB1Periph_GPIOD
|
||||
#define BT_UART_GPIO_PORT_EN GPIOD
|
||||
#define BT_UART_GPIO_PIN_EN GPIO_Pin_11
|
||||
#define BT_UART_GPIO_CLK_BRTS RCC_AHB1Periph_GPIOB
|
||||
#define BT_UART_GPIO_PORT_BRTS GPIOB
|
||||
#define BT_UART_GPIO_PIN_BRTS GPIO_Pin_10
|
||||
#define BT_UART_GPIO_TX_PinSource GPIO_PinSource14
|
||||
#define BT_UART_GPIO_RX_PinSource GPIO_PinSource9
|
||||
|
||||
@@ -30,25 +37,34 @@ int bt_open()
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
USART_InitTypeDef USART_InitStructure;
|
||||
|
||||
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(BT_UART_GPIO_CLK_TXRX, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(BT_UART_GPIO_CLK_EN, ENABLE);
|
||||
RCC_AHB1PeriphClockCmd(BT_UART_GPIO_CLK_BRTS, ENABLE);
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);
|
||||
|
||||
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_Pin = BT_UART_GPIO_PIN_EN;
|
||||
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_UART_GPIO_PORT, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_UART_GPIO_PIN_RX;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_Init(BT_UART_GPIO_PORT_EN, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_UART_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_UART_GPIO_PORT, &GPIO_InitStructure);
|
||||
GPIO_Init(BT_UART_GPIO_PORT_BRTS, &GPIO_InitStructure);
|
||||
|
||||
GPIO_InitStructure.GPIO_Pin = BT_UART_GPIO_PIN_TX|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_UP;
|
||||
GPIO_Init(BT_UART_GPIO_PORT_TXRX, &GPIO_InitStructure);
|
||||
|
||||
GPIO_PinAFConfig(BT_UART_GPIO_PORT_TXRX, BT_UART_GPIO_TX_PinSource, BT_UART_AF);
|
||||
GPIO_PinAFConfig(BT_UART_GPIO_PORT_TXRX, BT_UART_GPIO_RX_PinSource, BT_UART_AF);
|
||||
|
||||
USART_InitStructure.USART_BaudRate = 115200;
|
||||
USART_InitStructure.USART_Parity = USART_Parity_No;
|
||||
@@ -68,9 +84,14 @@ int bt_open()
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
GPIO_SetBits(BT_UART_GPIO_PORT_BRTS, BT_UART_GPIO_PIN_BRTS);
|
||||
GPIO_ResetBits(BT_UART_GPIO_PORT_EN, BT_UART_GPIO_PIN_EN); // open bluetooth
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t bt_state = 0;
|
||||
|
||||
extern "C" void USART6_IRQHandler(void)
|
||||
{
|
||||
#if 1
|
||||
@@ -90,6 +111,8 @@ extern "C" void USART6_IRQHandler(void)
|
||||
}
|
||||
else {
|
||||
USART_ITConfig(BT_UART, USART_IT_TXE, DISABLE);
|
||||
GPIO_SetBits(BT_UART_GPIO_PORT_BRTS, BT_UART_GPIO_PIN_BRTS);
|
||||
bt_state = 0;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -101,10 +124,23 @@ int bt_write(const void *buffer, int len)
|
||||
for (int i=0; i<len; ++i) {
|
||||
btTxFifo.push(data[i]);
|
||||
}
|
||||
USART_ITConfig(BT_UART, USART_IT_TXE, ENABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bt_wakeup(void)
|
||||
{
|
||||
if (bt_state < 2 && !btTxFifo.empty()) {
|
||||
if (bt_state == 0) {
|
||||
GPIO_ResetBits(BT_UART_GPIO_PORT_BRTS, BT_UART_GPIO_PIN_BRTS);
|
||||
bt_state = 1;
|
||||
}
|
||||
else {
|
||||
USART_ITConfig(BT_UART, USART_IT_TXE, ENABLE);
|
||||
bt_state = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int bt_read(void *buffer, int len)
|
||||
{
|
||||
int result = 0;
|
||||
@@ -121,5 +157,6 @@ int bt_read(void *buffer, int len)
|
||||
|
||||
int bt_close()
|
||||
{
|
||||
GPIO_SetBits(BT_UART_GPIO_PORT_EN, BT_UART_GPIO_PIN_EN); // close bluetooth
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -316,6 +316,7 @@ void uart3Stop(void);
|
||||
int bt_open(void);
|
||||
int bt_write(const void *buffer, int len);
|
||||
int bt_read(void *buffer, int len);
|
||||
void bt_wakeup(void);
|
||||
|
||||
// Top LCD driver
|
||||
#if defined(REV9E)
|
||||
|
||||
Reference in New Issue
Block a user