-
Notifications
You must be signed in to change notification settings - Fork 689
Expand file tree
/
Copy pathbtstack_uart_block_freertos.c
More file actions
211 lines (183 loc) · 7.64 KB
/
btstack_uart_block_freertos.c
File metadata and controls
211 lines (183 loc) · 7.64 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "btstack_uart_block_freertos.c"
/*
* btstack_uart_block_freertos.c
*
* Adapter to IRQ-driven hal_uart_dma.h with FreeRTOS BTstack Run Loop
* Callbacks are executed on main thread via data source and btstack_run_loop_poll_data_sources_from_irq
*
*/
#include "btstack_debug.h"
#include "btstack_uart_block.h"
#include "btstack_run_loop.h"
#include "hal_uart_dma.h"
#ifdef HAVE_FREERTOS_INCLUDE_PREFIX
#include "freertos/FreeRTOS.h"
#else
#include "FreeRTOS.h"
#endif
// uart config
static const btstack_uart_config_t * uart_config;
// data source for integration with BTstack Runloop
static btstack_data_source_t transport_data_source;
// callbacks
static void (*block_sent)(void);
static void (*block_received)(void);
static void (*wakeup_handler)(void);
static int send_complete;
static int receive_complete;
static int wakeup_event;
static void btstack_uart_block_freertos_received_isr(void){
receive_complete = 1;
btstack_run_loop_poll_data_sources_from_irq();
}
static void btstack_uart_block_freertos_sent_isr(void){
send_complete = 1;
btstack_run_loop_poll_data_sources_from_irq();
}
static void btstack_uart_block_freertos_cts_pulse_isr(void){
wakeup_event = 1;
btstack_run_loop_poll_data_sources_from_irq();
}
static void btstack_uart_block_freertos_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) {
switch (callback_type){
case DATA_SOURCE_CALLBACK_POLL:
if (send_complete){
send_complete = 0;
if (block_sent){
block_sent();
}
}
if (receive_complete){
receive_complete = 0;
if (block_received){
block_received();
}
}
if (wakeup_event){
wakeup_event = 0;
if (wakeup_handler){
wakeup_handler();
}
}
break;
default:
break;
}
}
//
static int btstack_uart_block_freertos_init(const btstack_uart_config_t * config){
uart_config = config;
hal_uart_dma_set_block_received(&btstack_uart_block_freertos_received_isr);
hal_uart_dma_set_block_sent(&btstack_uart_block_freertos_sent_isr);
// set up polling data_source
btstack_run_loop_set_data_source_handler(&transport_data_source, &btstack_uart_block_freertos_process);
btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
btstack_run_loop_add_data_source(&transport_data_source);
return 0;
}
static int btstack_uart_block_freertos_open(void){
hal_uart_dma_init();
hal_uart_dma_set_baud(uart_config->baudrate);
return 0;
}
static int btstack_uart_block_freertos_close(void){
// close device
// ...
return 0;
}
static void btstack_uart_block_freertos_set_block_received( void (*block_handler)(void)){
block_received = block_handler;
}
static void btstack_uart_block_freertos_set_block_sent( void (*block_handler)(void)){
block_sent = block_handler;
}
static void btstack_uart_block_freertos_set_wakeup_handler( void (*the_wakeup_handler)(void)){
wakeup_handler = the_wakeup_handler;
}
static int btstack_uart_block_freertos_set_parity(int parity){
return 0;
}
// static void btstack_uart_block_set_sleep(uint8_t sleep){
// }
// static void btstack_uart_block_freertos_set_csr_irq_handler( void (*csr_irq_handler)(void)){
// }
static int btstack_uart_block_freertos_get_supported_sleep_modes(void){
#ifdef HAVE_HAL_UART_DMA_SLEEP_MODES
return hal_uart_dma_get_supported_sleep_modes();
#else
return BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE;
#endif
}
static void btstack_uart_block_freertos_set_sleep(btstack_uart_sleep_mode_t sleep_mode){
log_info("set sleep %u", sleep_mode);
if (sleep_mode == BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE){
hal_uart_dma_set_csr_irq_handler(&btstack_uart_block_freertos_cts_pulse_isr);
} else {
hal_uart_dma_set_csr_irq_handler(NULL);
}
#ifdef HAVE_HAL_UART_DMA_SLEEP_MODES
hal_uart_dma_set_sleep_mode(sleep_mode);
#else
hal_uart_dma_set_sleep(sleep_mode != BTSTACK_UART_SLEEP_OFF);
#endif
log_info("done");
}
static const btstack_uart_block_t btstack_uart_block_freertos = {
/* int (*init)(hci_transport_config_uart_t * config); */ &btstack_uart_block_freertos_init,
/* int (*open)(void); */ &btstack_uart_block_freertos_open,
/* int (*close)(void); */ &btstack_uart_block_freertos_close,
/* void (*set_block_received)(void (*handler)(void)); */ &btstack_uart_block_freertos_set_block_received,
/* void (*set_block_sent)(void (*handler)(void)); */ &btstack_uart_block_freertos_set_block_sent,
/* int (*set_baudrate)(uint32_t baudrate); */ &hal_uart_dma_set_baud,
/* int (*set_parity)(int parity); */ &btstack_uart_block_freertos_set_parity,
#ifdef HAVE_UART_DMA_SET_FLOWCONTROL
/* int (*set_flowcontrol)(int flowcontrol); */ &hal_uart_dma_set_flowcontrol,
#else
/* int (*set_flowcontrol)(int flowcontrol); */ NULL,
#endif
/* void (*receive_block)(uint8_t *buffer, uint16_t len); */ &hal_uart_dma_receive_block,
/* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &hal_uart_dma_send_block,
/* int (*get_supported_sleep_modes); */ &btstack_uart_block_freertos_get_supported_sleep_modes,
/* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */ &btstack_uart_block_freertos_set_sleep,
/* void (*set_wakeup_handler)(void (*wakeup_handler)(void)); */ &btstack_uart_block_freertos_set_wakeup_handler,
NULL, NULL, NULL, NULL,
};
const btstack_uart_block_t * btstack_uart_block_freertos_instance(void){
return &btstack_uart_block_freertos;
}