1 /* 2 * Copyright (C) 2015 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 /* 39 * hci_h4_transport_wiced.c 40 * 41 * HCI Transport API implementation for basic H4 protocol for use with btstack_run_loop_wiced.c 42 */ 43 44 #define BTSTACK_FILE__ "btstack_uart_block_wiced.c" 45 46 #include "btstack_config.h" 47 #include "btstack_run_loop_wiced.h" 48 49 #include "btstack_debug.h" 50 #include "hci.h" 51 #include "hci_transport.h" 52 #include "platform_bluetooth.h" 53 54 #include "wiced.h" 55 56 #include <stdio.h> 57 #include <string.h> 58 59 // priority higher than WIFI to make sure RTS is set 60 #define WICED_BT_UART_THREAD_PRIORITY (WICED_NETWORK_WORKER_PRIORITY - 2) 61 #define WICED_BT_UART_THREAD_STACK_SIZE 300 62 63 // assert pre-buffer for packet type is available 64 #if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0) 65 #error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h 66 #endif 67 68 // Default of 512 bytes should be fine. Only needed with BTSTACK_FLOW_CONTROL_UART 69 #ifndef RX_RING_BUFFER_SIZE 70 #define RX_RING_BUFFER_SIZE 512 71 #endif 72 73 // Use BTSTACK_FLOW_CONTROL_MANUAL is used when Bluetooth RTS/CTS are not connected to UART RTS/CTS pins 74 // E.g. on RedBear Duo - WICED_BT_UART_MANUAL_CTS_RTS is defined 75 76 static enum { 77 BTSTACK_FLOW_CONTROL_OFF, 78 BTSTACK_FLOW_CONTROL_UART, 79 BTSTACK_FLOW_CONTROL_MANUAL, 80 } btstack_flow_control_mode; 81 82 static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * arg); 83 84 static wiced_worker_thread_t tx_worker_thread; 85 static const uint8_t * tx_worker_data_buffer; 86 static uint16_t tx_worker_data_size; 87 88 static wiced_worker_thread_t rx_worker_thread; 89 static uint8_t * rx_worker_read_buffer; 90 static uint16_t rx_worker_read_size; 91 92 static wiced_ring_buffer_t rx_ring_buffer; 93 static uint8_t rx_data[RX_RING_BUFFER_SIZE]; 94 95 // uart config 96 static const btstack_uart_config_t * uart_config; 97 98 // callbacks 99 static void (*block_sent)(void); 100 static void (*block_received)(void); 101 102 // executed on main run loop 103 static wiced_result_t btstack_uart_block_wiced_main_notify_block_send(void *arg){ 104 if (block_sent){ 105 block_sent(); 106 } 107 return WICED_SUCCESS; 108 } 109 110 // executed on main run loop 111 static wiced_result_t btstack_uart_block_wiced_main_notify_block_read(void *arg){ 112 if (block_received){ 113 block_received(); 114 } 115 return WICED_SUCCESS; 116 } 117 118 // executed on tx worker thread 119 static wiced_result_t btstack_uart_block_wiced_tx_worker_send_block(void * arg){ 120 // wait for CTS to become low in manual flow control mode 121 if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL && wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){ 122 while (platform_gpio_input_get(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]) == WICED_TRUE){ 123 wiced_rtos_delay_milliseconds(10); 124 } 125 } 126 127 // blocking send 128 platform_uart_transmit_bytes(wiced_bt_uart_driver, tx_worker_data_buffer, tx_worker_data_size); 129 130 // let transport know 131 btstack_run_loop_wiced_execute_code_on_main_thread(&btstack_uart_block_wiced_main_notify_block_send, NULL); 132 return WICED_SUCCESS; 133 } 134 135 // executed on rx worker thread 136 static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * arg){ 137 138 if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL && wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){ 139 platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]); 140 } 141 142 #ifdef WICED_UART_READ_DOES_NOT_RETURN_BYTES_READ 143 // older API passes in number of bytes to read (checked in 3.3.1 and 3.4.0) 144 platform_uart_receive_bytes(wiced_bt_uart_driver, rx_worker_read_buffer, rx_worker_read_size, WICED_NEVER_TIMEOUT); 145 #else 146 // newer API uses pointer to return number of read bytes 147 uint32_t bytes = rx_worker_read_size; 148 platform_uart_receive_bytes(wiced_bt_uart_driver, rx_worker_read_buffer, &bytes, WICED_NEVER_TIMEOUT); 149 // assumption: bytes = bytes_to_read as timeout is never 150 #endif 151 152 if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL && wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){ 153 platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]); 154 } 155 156 // let transport know 157 btstack_run_loop_wiced_execute_code_on_main_thread(&btstack_uart_block_wiced_main_notify_block_read, NULL); 158 return WICED_SUCCESS; 159 } 160 161 static int btstack_uart_block_wiced_init(const btstack_uart_config_t * config){ 162 uart_config = config; 163 164 // determine flow control mode based on hardware config and uart config 165 if (uart_config->flowcontrol){ 166 #ifdef WICED_BT_UART_MANUAL_CTS_RTS 167 btstack_flow_control_mode = BTSTACK_FLOW_CONTROL_MANUAL; 168 #else 169 btstack_flow_control_mode = BTSTACK_FLOW_CONTROL_UART; 170 #endif 171 } else { 172 btstack_flow_control_mode = BTSTACK_FLOW_CONTROL_OFF; 173 } 174 return 0; 175 } 176 177 static int btstack_uart_block_wiced_open(void){ 178 179 // UART config 180 wiced_uart_config_t wiced_uart_config = 181 { 182 .baud_rate = uart_config->baudrate, 183 .data_width = DATA_WIDTH_8BIT, 184 .parity = NO_PARITY, 185 .stop_bits = STOP_BITS_1, 186 }; 187 188 if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_UART){ 189 wiced_uart_config.flow_control = FLOW_CONTROL_CTS_RTS; 190 } else { 191 wiced_uart_config.flow_control = FLOW_CONTROL_DISABLED; 192 } 193 wiced_ring_buffer_t * ring_buffer = NULL; 194 195 // configure HOST and DEVICE WAKE PINs 196 platform_gpio_init(wiced_bt_control_pins[WICED_BT_PIN_HOST_WAKE], INPUT_HIGH_IMPEDANCE); 197 platform_gpio_init(wiced_bt_control_pins[WICED_BT_PIN_DEVICE_WAKE], OUTPUT_PUSH_PULL); 198 platform_gpio_output_low(wiced_bt_control_pins[WICED_BT_PIN_DEVICE_WAKE]); 199 200 /* Configure Reg Enable pin to output. Set to HIGH */ 201 if (wiced_bt_control_pins[ WICED_BT_PIN_POWER ]){ 202 platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_POWER ], OUTPUT_OPEN_DRAIN_PULL_UP ); 203 platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] ); 204 } 205 206 wiced_rtos_delay_milliseconds( 100 ); 207 208 // Configure RTS 209 if (wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]){ 210 switch (btstack_flow_control_mode){ 211 case BTSTACK_FLOW_CONTROL_OFF: 212 // configure RTS pin as output and set to low - always on 213 platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS], OUTPUT_PUSH_PULL); 214 platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]); 215 break; 216 case BTSTACK_FLOW_CONTROL_UART: 217 // configuration done by platform_uart_init 218 break; 219 case BTSTACK_FLOW_CONTROL_MANUAL: 220 // configure RTS pin as output and set to high - controlled by btstack_uart_block_wiced_rx_worker_receive_block 221 platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS], OUTPUT_PUSH_PULL); 222 platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]); 223 break; 224 } 225 } 226 227 // Configure CTS 228 if (wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){ 229 switch (btstack_flow_control_mode){ 230 case BTSTACK_FLOW_CONTROL_OFF: 231 // don't care 232 break; 233 case BTSTACK_FLOW_CONTROL_UART: 234 // configuration done by platform_uart_init 235 break; 236 case BTSTACK_FLOW_CONTROL_MANUAL: 237 // configure CTS to input, pull-up 238 platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS], INPUT_PULL_UP); 239 break; 240 } 241 } 242 243 // use ring buffer to allow to receive RX_RING_BUFFER_SIZE/2 addition bytes - not needed with hardware UART 244 if (btstack_flow_control_mode != BTSTACK_FLOW_CONTROL_UART){ 245 ring_buffer_init((wiced_ring_buffer_t *) &rx_ring_buffer, (uint8_t*) rx_data, sizeof( rx_data ) ); 246 ring_buffer = (wiced_ring_buffer_t *) &rx_ring_buffer; 247 } 248 249 platform_uart_init( wiced_bt_uart_driver, wiced_bt_uart_peripheral, &wiced_uart_config, ring_buffer ); 250 251 252 // Reset Bluetooth via RESET line. Fallback to toggling POWER otherwise 253 if ( wiced_bt_control_pins[ WICED_BT_PIN_RESET ]){ 254 platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_RESET ], OUTPUT_PUSH_PULL ); 255 platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] ); 256 257 platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] ); 258 wiced_rtos_delay_milliseconds( 100 ); 259 platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] ); 260 } 261 else if ( wiced_bt_control_pins[ WICED_BT_PIN_POWER ]){ 262 platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] ); 263 wiced_rtos_delay_milliseconds( 100 ); 264 platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] ); 265 } 266 267 // wait for Bluetooth to start up 268 wiced_rtos_delay_milliseconds( 500 ); 269 270 // create worker threads for rx/tx. only single request is posted to their queues 271 wiced_rtos_create_worker_thread(&tx_worker_thread, WICED_BT_UART_THREAD_PRIORITY, WICED_BT_UART_THREAD_STACK_SIZE, 1); 272 wiced_rtos_create_worker_thread(&rx_worker_thread, WICED_BT_UART_THREAD_PRIORITY, WICED_BT_UART_THREAD_STACK_SIZE, 1); 273 274 // tx is ready 275 tx_worker_data_size = 0; 276 return 0; 277 } 278 279 static int btstack_uart_block_wiced_close(void){ 280 // not implemented 281 return 0; 282 } 283 284 static void btstack_uart_block_wiced_set_block_received( void (*block_handler)(void)){ 285 block_received = block_handler; 286 } 287 288 static void btstack_uart_block_wiced_set_block_sent( void (*block_handler)(void)){ 289 block_sent = block_handler; 290 } 291 292 static int btstack_uart_block_wiced_set_baudrate(uint32_t baudrate){ 293 294 #if defined(_STM32F205RGT6_) || defined(STM32F40_41xxx) || defined(STM32F411xE) || (STM32F412xG) 295 296 // directly use STM peripheral functions to change baud rate dynamically 297 298 // set TX to high 299 log_info("set baud %u", (int) baudrate); 300 const platform_gpio_t* gpio = wiced_bt_uart_pins[WICED_BT_PIN_UART_TX]; 301 platform_gpio_output_high(gpio); 302 303 // reconfigure TX pin as GPIO 304 GPIO_InitTypeDef gpio_init_structure; 305 gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz; 306 gpio_init_structure.GPIO_Mode = GPIO_Mode_OUT; 307 gpio_init_structure.GPIO_OType = GPIO_OType_PP; 308 gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL; 309 gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << gpio->pin_number ); 310 GPIO_Init( gpio->port, &gpio_init_structure ); 311 312 // disable USART 313 USART_Cmd( wiced_bt_uart_peripheral->port, DISABLE ); 314 315 // setup init structure 316 USART_InitTypeDef uart_init_structure; 317 uart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; 318 uart_init_structure.USART_BaudRate = baudrate; 319 uart_init_structure.USART_WordLength = USART_WordLength_8b; 320 uart_init_structure.USART_StopBits = USART_StopBits_1; 321 uart_init_structure.USART_Parity = USART_Parity_No; 322 323 if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_UART){ 324 uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; 325 } else { 326 uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 327 } 328 USART_Init(wiced_bt_uart_peripheral->port, &uart_init_structure); 329 330 // enable USART again 331 USART_Cmd( wiced_bt_uart_peripheral->port, ENABLE ); 332 333 // set TX pin as USART again 334 gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; 335 GPIO_Init( gpio->port, &gpio_init_structure ); 336 337 #else 338 log_error("btstack_uart_block_wiced_set_baudrate not implemented for this WICED Platform"); 339 #endif 340 return 0; 341 } 342 343 static int btstack_uart_block_wiced_set_parity(int parity){ 344 log_error("btstack_uart_block_wiced_set_parity not implemented"); 345 return 0; 346 } 347 348 static void btstack_uart_block_wiced_send_block(const uint8_t *buffer, uint16_t length){ 349 // store in request 350 tx_worker_data_buffer = buffer; 351 tx_worker_data_size = length; 352 wiced_rtos_send_asynchronous_event(&tx_worker_thread, &btstack_uart_block_wiced_tx_worker_send_block, NULL); 353 } 354 355 static void btstack_uart_block_wiced_receive_block(uint8_t *buffer, uint16_t len){ 356 rx_worker_read_buffer = buffer; 357 rx_worker_read_size = len; 358 wiced_rtos_send_asynchronous_event(&rx_worker_thread, &btstack_uart_block_wiced_rx_worker_receive_block, NULL); 359 } 360 361 362 // static void btstack_uart_block_wiced_set_sleep(uint8_t sleep){ 363 // } 364 // static void btstack_uart_block_wiced_set_csr_irq_handler( void (*csr_irq_handler)(void)){ 365 // } 366 367 static const btstack_uart_block_t btstack_uart_block_wiced = { 368 /* int (*init)(hci_transport_config_uart_t * config); */ &btstack_uart_block_wiced_init, 369 /* int (*open)(void); */ &btstack_uart_block_wiced_open, 370 /* int (*close)(void); */ &btstack_uart_block_wiced_close, 371 /* void (*set_block_received)(void (*handler)(void)); */ &btstack_uart_block_wiced_set_block_received, 372 /* void (*set_block_sent)(void (*handler)(void)); */ &btstack_uart_block_wiced_set_block_sent, 373 /* int (*set_baudrate)(uint32_t baudrate); */ &btstack_uart_block_wiced_set_baudrate, 374 /* int (*set_parity)(int parity); */ &btstack_uart_block_wiced_set_parity, 375 /* int (*set_flowcontrol)(int flowcontrol); */ NULL, 376 /* void (*receive_block)(uint8_t *buffer, uint16_t len); */ &btstack_uart_block_wiced_receive_block, 377 /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_block_wiced_send_block, 378 /* int (*get_supported_sleep_modes); */ NULL, 379 /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */ NULL, 380 /* void (*set_wakeup_handler)(void (*handler)(void)); */ NULL, 381 NULL, NULL, NULL, NULL, 382 }; 383 384 const btstack_uart_block_t * btstack_uart_block_wiced_instance(void){ 385 return &btstack_uart_block_wiced; 386 } 387