xref: /btstack/platform/freertos/btstack_uart_block_freertos.c (revision 017c2996ac90e47dcc79147aa4883ee09cb3810b)
1 /*
2  * Copyright (C) 2016 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "btstack_uart_block_freertos.c"
39 
40 /*
41  *  btstack_uart_block_freertos.c
42  *
43  *  Adapter to IRQ-driven hal_uart_dma.h with FreeRTOS BTstack Run Loop and
44  *  Callbacks are executed on main thread via data source and btstack_run_loop_freertos_trigger_from_isr
45  *
46  */
47 
48 #include "btstack_debug.h"
49 #include "btstack_uart_block.h"
50 #include "btstack_run_loop_freertos.h"
51 #include "hal_uart_dma.h"
52 
53 #ifdef HAVE_FREERTOS_INCLUDE_PREFIX
54 #include "freertos/FreeRTOS.h"
55 #else
56 #include "FreeRTOS.h"
57 #endif
58 
59 #if (INCLUDE_xEventGroupSetBitFromISR != 1) && !defined(HAVE_FREERTOS_TASK_NOTIFICATIONS)
60 #error "The BTstack HAL UART Run Loop integration (btstack_uart_block_freertos) needs to trigger Run Loop iterations from ISR context," \
61 "but neither 'INCLUDE_xEventGroupSetBitFromISR' is enabled in your FreeRTOS configuration nor HAVE_FREERTOS_TASK_NOTIFICATIONS is enabled in " \
62 "btstack_config.h. Please enable INCLUDE_xEventGroupSetBitFromISR or HAVE_FREERTOS_TASK_NOTIFICATIONS."
63 #endif
64 
65 // uart config
66 static const btstack_uart_config_t * uart_config;
67 
68 // data source for integration with BTstack Runloop
69 static btstack_data_source_t transport_data_source;
70 
71 // callbacks
72 static void (*block_sent)(void);
73 static void (*block_received)(void);
74 
75 static int send_complete;
76 static int receive_complete;
77 
78 static void btstack_uart_block_freertos_received_isr(void){
79     receive_complete = 1;
80     btstack_run_loop_freertos_trigger_from_isr();
81 }
82 
83 static void btstack_uart_block_freertos_sent_isr(void){
84     send_complete = 1;
85     btstack_run_loop_freertos_trigger_from_isr();
86 }
87 
88 static void btstack_uart_block_freertos_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) {
89     switch (callback_type){
90         case DATA_SOURCE_CALLBACK_POLL:
91             if (send_complete){
92                 send_complete = 0;
93                 if (block_sent){
94                     block_sent();
95                 }
96             }
97             if (receive_complete){
98                 receive_complete = 0;
99                 if (block_received){
100                     block_received();
101                 }
102             }
103             break;
104         default:
105             break;
106     }
107 }
108 
109 //
110 static int btstack_uart_block_freertos_init(const btstack_uart_config_t * config){
111     uart_config = config;
112     hal_uart_dma_set_block_received(&btstack_uart_block_freertos_received_isr);
113     hal_uart_dma_set_block_sent(&btstack_uart_block_freertos_sent_isr);
114 
115     // set up polling data_source
116     btstack_run_loop_set_data_source_handler(&transport_data_source, &btstack_uart_block_freertos_process);
117     btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
118     btstack_run_loop_add_data_source(&transport_data_source);
119     return 0;
120 }
121 
122 static int btstack_uart_block_freertos_open(void){
123     hal_uart_dma_init();
124     hal_uart_dma_set_baud(uart_config->baudrate);
125     return 0;
126 }
127 
128 static int btstack_uart_block_freertos_close(void){
129 
130     // close device
131     // ...
132     return 0;
133 }
134 
135 static void btstack_uart_block_freertos_set_block_received( void (*block_handler)(void)){
136     block_received = block_handler;
137 }
138 
139 static void btstack_uart_block_freertos_set_block_sent( void (*block_handler)(void)){
140     block_sent = block_handler;
141 }
142 
143 static int btstack_uart_block_freertos_set_parity(int parity){
144     return 0;
145 }
146 
147 // static void btstack_uart_block_set_sleep(uint8_t sleep){
148 // }
149 
150 // static void btstack_uart_block_freertos_set_csr_irq_handler( void (*csr_irq_handler)(void)){
151 // }
152 
153 static const btstack_uart_block_t btstack_uart_block_freertos = {
154     /* int  (*init)(hci_transport_config_uart_t * config); */         &btstack_uart_block_freertos_init,
155     /* int  (*open)(void); */                                         &btstack_uart_block_freertos_open,
156     /* int  (*close)(void); */                                        &btstack_uart_block_freertos_close,
157     /* void (*set_block_received)(void (*handler)(void)); */          &btstack_uart_block_freertos_set_block_received,
158     /* void (*set_block_sent)(void (*handler)(void)); */              &btstack_uart_block_freertos_set_block_sent,
159     /* int  (*set_baudrate)(uint32_t baudrate); */                    &hal_uart_dma_set_baud,
160     /* int  (*set_parity)(int parity); */                             &btstack_uart_block_freertos_set_parity,
161 #ifdef HAVE_UART_DMA_SET_FLOWCONTROL
162     /* int  (*set_flowcontrol)(int flowcontrol); */                   &hal_uart_dma_set_flowcontrol,
163 #else
164     /* int  (*set_flowcontrol)(int flowcontrol); */                   NULL,
165 #endif
166     /* void (*receive_block)(uint8_t *buffer, uint16_t len); */       &hal_uart_dma_receive_block,
167     /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &hal_uart_dma_send_block,
168     /* int (*get_supported_sleep_modes); */                           NULL,
169     /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */    NULL,
170     /* void (*set_wakeup_handler)(void (*wakeup_handler)(void)); */   NULL,
171     NULL, NULL, NULL, NULL,
172 };
173 
174 const btstack_uart_block_t * btstack_uart_block_freertos_instance(void){
175 	return &btstack_uart_block_freertos;
176 }
177