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