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