xref: /btstack/platform/embedded/btstack_uart_block_embedded.c (revision d3384105566dae64613a70cefce542b67dcb5daa)
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_embedded.c"
39 
40 /*
41  *  btstack_uart_block_embedded.c
42  *
43  *  Adapter to IRQ-driven hal_uart_dma.h with Embedded 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 #include "btstack_debug.h"
48 #include "btstack_uart_block.h"
49 #include "btstack_run_loop.h"
50 #include "hal_uart_dma.h"
51 
52 // NULL
53 #include <stddef.h>
54 
55 // uart config
56 static const btstack_uart_config_t * btstack_uart_block_configuration;
57 
58 // data source for integration with BTstack Runloop
59 static btstack_data_source_t transport_data_source;
60 
61 static int send_complete;
62 static int receive_complete;
63 static int wakeup_event;
64 
65 // callbacks
66 static void (*block_sent)(void);
67 static void (*block_received)(void);
68 static void (*wakeup_handler)(void);
69 
70 
71 static void btstack_uart_block_received(void){
72     receive_complete = 1;
73     btstack_run_loop_poll_data_sources_from_irq();
74 }
75 
76 static void btstack_uart_block_sent(void){
77     send_complete = 1;
78     btstack_run_loop_poll_data_sources_from_irq();
79 }
80 
81 static void btstack_uart_cts_pulse(void){
82     wakeup_event = 1;
83     btstack_run_loop_poll_data_sources_from_irq();
84 }
85 
86 static int btstack_uart_embedded_init(const btstack_uart_config_t * config){
87     btstack_uart_block_configuration = config;
88     hal_uart_dma_set_block_received(&btstack_uart_block_received);
89     hal_uart_dma_set_block_sent(&btstack_uart_block_sent);
90     return 0;
91 }
92 
93 static void btstack_uart_embedded_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) {
94     switch (callback_type){
95         case DATA_SOURCE_CALLBACK_POLL:
96             if (send_complete){
97                 send_complete = 0;
98                 if (block_sent){
99                     block_sent();
100                 }
101             }
102             if (receive_complete){
103                 receive_complete = 0;
104                 if (block_received){
105                     block_received();
106                 }
107             }
108             if (wakeup_event){
109                 wakeup_event = 0;
110                 if (wakeup_handler){
111                     wakeup_handler();
112                 }
113             }
114             break;
115         default:
116             break;
117     }
118 }
119 
120 static int btstack_uart_embedded_open(void){
121     hal_uart_dma_init();
122     hal_uart_dma_set_baud(btstack_uart_block_configuration->baudrate);
123 
124     // set up polling data_source
125     btstack_run_loop_set_data_source_handler(&transport_data_source, &btstack_uart_embedded_process);
126     btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
127     btstack_run_loop_add_data_source(&transport_data_source);
128     return 0;
129 }
130 
131 static int btstack_uart_embedded_close(void){
132 
133     // remove data source
134     btstack_run_loop_disable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL);
135     btstack_run_loop_remove_data_source(&transport_data_source);
136 
137     // close device
138     // ...
139     return 0;
140 }
141 
142 static void btstack_uart_embedded_set_block_received( void (*block_handler)(void)){
143     block_received = block_handler;
144 }
145 
146 static void btstack_uart_embedded_set_block_sent( void (*block_handler)(void)){
147     block_sent = block_handler;
148 }
149 
150 static void btstack_uart_embedded_set_wakeup_handler( void (*the_wakeup_handler)(void)){
151     wakeup_handler = the_wakeup_handler;
152 }
153 
154 static int btstack_uart_embedded_set_parity(int parity){
155     return 0;
156 }
157 
158 static void btstack_uart_embedded_send_block(const uint8_t *data, uint16_t size){
159     hal_uart_dma_send_block(data, size);
160 }
161 
162 static void btstack_uart_embedded_receive_block(uint8_t *buffer, uint16_t len){
163     hal_uart_dma_receive_block(buffer, len);
164 }
165 
166 static int btstack_uart_embedded_get_supported_sleep_modes(void){
167 #ifdef HAVE_HAL_UART_DMA_SLEEP_MODES
168 	return hal_uart_dma_get_supported_sleep_modes();
169 #else
170 	return BTSTACK_UART_SLEEP_MASK_RTS_HIGH_WAKE_ON_CTS_PULSE;
171 #endif
172 }
173 
174 static void btstack_uart_embedded_set_sleep(btstack_uart_sleep_mode_t sleep_mode){
175 	log_info("set sleep %u", sleep_mode);
176 	if (sleep_mode == BTSTACK_UART_SLEEP_RTS_HIGH_WAKE_ON_CTS_PULSE){
177 		hal_uart_dma_set_csr_irq_handler(&btstack_uart_cts_pulse);
178 	} else {
179 		hal_uart_dma_set_csr_irq_handler(NULL);
180 	}
181 
182 #ifdef HAVE_HAL_UART_DMA_SLEEP_MODES
183 	hal_uart_dma_set_sleep_mode(sleep_mode);
184 #else
185 	hal_uart_dma_set_sleep(sleep_mode != BTSTACK_UART_SLEEP_OFF);
186 #endif
187 	log_info("done");
188 }
189 
190 static const btstack_uart_block_t btstack_uart_embedded = {
191     /* int  (*init)(hci_transport_config_uart_t * config); */         &btstack_uart_embedded_init,
192     /* int  (*open)(void); */                                         &btstack_uart_embedded_open,
193     /* int  (*close)(void); */                                        &btstack_uart_embedded_close,
194     /* void (*set_block_received)(void (*handler)(void)); */          &btstack_uart_embedded_set_block_received,
195     /* void (*set_block_sent)(void (*handler)(void)); */              &btstack_uart_embedded_set_block_sent,
196     /* int  (*set_baudrate)(uint32_t baudrate); */                    &hal_uart_dma_set_baud,
197     /* int  (*set_parity)(int parity); */                             &btstack_uart_embedded_set_parity,
198 #ifdef HAVE_UART_DMA_SET_FLOWCONTROL
199     /* int  (*set_flowcontrol)(int flowcontrol); */                   &hal_uart_dma_set_flowcontrol,
200 #else
201     /* int  (*set_flowcontrol)(int flowcontrol); */                   NULL,
202 #endif
203     /* void (*receive_block)(uint8_t *buffer, uint16_t len); */       &btstack_uart_embedded_receive_block,
204     /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_embedded_send_block,
205 	/* int (*get_supported_sleep_modes); */                           &btstack_uart_embedded_get_supported_sleep_modes,
206     /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */    &btstack_uart_embedded_set_sleep,
207     /* void (*set_wakeup_handler)(void (*handler)(void)); */          &btstack_uart_embedded_set_wakeup_handler,
208     NULL, NULL, NULL, NULL,
209 };
210 
211 const btstack_uart_block_t * btstack_uart_block_embedded_instance(void){
212 	return &btstack_uart_embedded;
213 }
214