xref: /btstack/platform/freertos/btstack_uart_block_freertos.c (revision 2fca4dad957cd7b88f4657ed51e89c12615dda72)
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 BLUEKITCHEN
24  * GMBH 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
44  *  Callbacks are executed on main thread via data source and btstack_run_loop_poll_data_sources_from_irq
45  *
46  */
47 
48 #include "btstack_debug.h"
49 #include "btstack_uart_block.h"
50 #include "btstack_run_loop.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 // uart config
60 static const btstack_uart_config_t * uart_config;
61 
62 // data source for integration with BTstack Runloop
63 static btstack_data_source_t transport_data_source;
64 
65 // callbacks
66 static void (*block_sent)(void);
67 static void (*block_received)(void);
68 
69 static int send_complete;
70 static int receive_complete;
71 
btstack_uart_block_freertos_received_isr(void)72 static void btstack_uart_block_freertos_received_isr(void){
73     receive_complete = 1;
74     btstack_run_loop_poll_data_sources_from_irq();
75 }
76 
btstack_uart_block_freertos_sent_isr(void)77 static void btstack_uart_block_freertos_sent_isr(void){
78     send_complete = 1;
79     btstack_run_loop_poll_data_sources_from_irq();
80 }
81 
btstack_uart_block_freertos_process(btstack_data_source_t * ds,btstack_data_source_callback_type_t callback_type)82 static void btstack_uart_block_freertos_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) {
83     switch (callback_type){
84         case DATA_SOURCE_CALLBACK_POLL:
85             if (send_complete){
86                 send_complete = 0;
87                 if (block_sent){
88                     block_sent();
89                 }
90             }
91             if (receive_complete){
92                 receive_complete = 0;
93                 if (block_received){
94                     block_received();
95                 }
96             }
97             break;
98         default:
99             break;
100     }
101 }
102 
103 //
btstack_uart_block_freertos_init(const btstack_uart_config_t * config)104 static int btstack_uart_block_freertos_init(const btstack_uart_config_t * config){
105     uart_config = config;
106     hal_uart_dma_set_block_received(&btstack_uart_block_freertos_received_isr);
107     hal_uart_dma_set_block_sent(&btstack_uart_block_freertos_sent_isr);
108 
109     // set up polling data_source
110     btstack_run_loop_set_data_source_handler(&transport_data_source, &btstack_uart_block_freertos_process);
111     btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
112     btstack_run_loop_add_data_source(&transport_data_source);
113     return 0;
114 }
115 
btstack_uart_block_freertos_open(void)116 static int btstack_uart_block_freertos_open(void){
117     hal_uart_dma_init();
118     hal_uart_dma_set_baud(uart_config->baudrate);
119     return 0;
120 }
121 
btstack_uart_block_freertos_close(void)122 static int btstack_uart_block_freertos_close(void){
123 
124     // close device
125     // ...
126     return 0;
127 }
128 
btstack_uart_block_freertos_set_block_received(void (* block_handler)(void))129 static void btstack_uart_block_freertos_set_block_received( void (*block_handler)(void)){
130     block_received = block_handler;
131 }
132 
btstack_uart_block_freertos_set_block_sent(void (* block_handler)(void))133 static void btstack_uart_block_freertos_set_block_sent( void (*block_handler)(void)){
134     block_sent = block_handler;
135 }
136 
btstack_uart_block_freertos_set_parity(int parity)137 static int btstack_uart_block_freertos_set_parity(int parity){
138     return 0;
139 }
140 
141 // static void btstack_uart_block_set_sleep(uint8_t sleep){
142 // }
143 
144 // static void btstack_uart_block_freertos_set_csr_irq_handler( void (*csr_irq_handler)(void)){
145 // }
146 
147 static const btstack_uart_block_t btstack_uart_block_freertos = {
148     /* int  (*init)(hci_transport_config_uart_t * config); */         &btstack_uart_block_freertos_init,
149     /* int  (*open)(void); */                                         &btstack_uart_block_freertos_open,
150     /* int  (*close)(void); */                                        &btstack_uart_block_freertos_close,
151     /* void (*set_block_received)(void (*handler)(void)); */          &btstack_uart_block_freertos_set_block_received,
152     /* void (*set_block_sent)(void (*handler)(void)); */              &btstack_uart_block_freertos_set_block_sent,
153     /* int  (*set_baudrate)(uint32_t baudrate); */                    &hal_uart_dma_set_baud,
154     /* int  (*set_parity)(int parity); */                             &btstack_uart_block_freertos_set_parity,
155 #ifdef HAVE_UART_DMA_SET_FLOWCONTROL
156     /* int  (*set_flowcontrol)(int flowcontrol); */                   &hal_uart_dma_set_flowcontrol,
157 #else
158     /* int  (*set_flowcontrol)(int flowcontrol); */                   NULL,
159 #endif
160     /* void (*receive_block)(uint8_t *buffer, uint16_t len); */       &hal_uart_dma_receive_block,
161     /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &hal_uart_dma_send_block,
162     /* int (*get_supported_sleep_modes); */                           NULL,
163     /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */    NULL,
164     /* void (*set_wakeup_handler)(void (*wakeup_handler)(void)); */   NULL,
165     NULL, NULL, NULL, NULL,
166 };
167 
btstack_uart_block_freertos_instance(void)168 const btstack_uart_block_t * btstack_uart_block_freertos_instance(void){
169 	return &btstack_uart_block_freertos;
170 }
171