-
-
Notifications
You must be signed in to change notification settings - Fork 379
/
Copy pathbluetooth_driver.cpp
142 lines (122 loc) · 3.45 KB
/
bluetooth_driver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (C) EdgeTX
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include "bluetooth_driver.h"
#include "stm32_hal_ll.h"
#include "board.h"
#include "debug.h"
#if !defined(BOOT)
#include "stm32_serial_driver.h"
#define BT_USART_IRQ_PRIORITY 6
static const stm32_usart_t btUSART = {
.USARTx = BT_USART,
.GPIOx = BT_USART_GPIO,
.GPIO_Pin = BT_TX_GPIO_PIN | BT_RX_GPIO_PIN,
.IRQn = BT_USART_IRQn,
.IRQ_Prio = BT_USART_IRQ_PRIORITY,
.txDMA = nullptr,
.txDMA_Stream = 0,
.txDMA_Channel = 0,
.rxDMA = nullptr,
.rxDMA_Stream = 0,
.rxDMA_Channel = 0,
};
DEFINE_STM32_SERIAL_PORT(BTModule, btUSART, BT_RX_FIFO_SIZE, BT_TX_FIFO_SIZE);
#if defined(BLUETOOTH_PROBE)
volatile uint8_t btChipPresent = 0;
#endif
// const etx_serial_driver_t* _bt_usart_drv = nullptr;
void* _bt_usart_ctx = nullptr;
#endif
void bluetoothInit(uint32_t baudrate, bool enable)
{
LL_GPIO_InitTypeDef pinInit;
#if defined(BT_EN_GPIO_PIN)
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = BT_EN_GPIO_PIN;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
pinInit.Pull = LL_GPIO_PULL_NO;
LL_GPIO_Init(BT_EN_GPIO, &pinInit);
#endif
#if defined(BT_PWR_GPIO)
LL_GPIO_StructInit(&pinInit);
pinInit.Pin = BT_PWR_GPIO_PIN;
pinInit.Mode = LL_GPIO_MODE_OUTPUT;
pinInit.Pull = LL_GPIO_PULL_UP;
LL_GPIO_Init(BT_PWR_GPIO, &pinInit);
#endif
#if !defined(BOOT)
etx_serial_init cfg = {
.baudrate = baudrate,
.encoding = ETX_Encoding_8N1,
.direction = ETX_Dir_TX_RX,
.polarity = ETX_Pol_Normal,
};
if (!_bt_usart_ctx) {
auto hw_def = REF_STM32_SERIAL_PORT(BTModule);
_bt_usart_ctx = STM32SerialDriver.init(hw_def, &cfg);
} else {
STM32SerialDriver.setBaudrate(_bt_usart_ctx, baudrate);
}
#endif
#if defined(BT_EN_GPIO_PIN)
if (enable) {
LL_GPIO_ResetOutputPin(BT_EN_GPIO, BT_EN_GPIO_PIN);
} else {
LL_GPIO_SetOutputPin(BT_EN_GPIO, BT_EN_GPIO_PIN);
}
#endif
#if defined(BT_PWR_GPIO)
LL_GPIO_SetOutputPin(BT_PWR_GPIO, BT_PWR_GPIO_PIN);
#endif
}
#if !defined(BOOT)
void bluetoothDisable()
{
#if defined(BT_EN_GPIO_PIN)
// close bluetooth (recent modules will go to bootloader mode)
LL_GPIO_SetOutputPin(BT_EN_GPIO, BT_EN_GPIO_PIN);
#endif
#if defined(BT_PWR_GPIO)
LL_GPIO_ResetOutputPin(BT_PWR_GPIO, BT_PWR_GPIO_PIN);
#endif
if (_bt_usart_ctx) {
STM32SerialDriver.deinit(_bt_usart_ctx);
_bt_usart_ctx = nullptr;
}
}
void bluetoothWrite(const void* buffer, uint32_t len)
{
if (!_bt_usart_ctx) return;
STM32SerialDriver.sendBuffer(_bt_usart_ctx, (const uint8_t*)buffer, len);
}
int bluetoothRead(uint8_t* data)
{
if (!_bt_usart_ctx) return 0;
return STM32SerialDriver.getByte(_bt_usart_ctx, data);
}
uint8_t bluetoothIsWriting(void)
{
if (!_bt_usart_ctx)
return false;
return !STM32SerialDriver.txCompleted(_bt_usart_ctx);
}
#endif // !BOOT