wiced: introduce btstack_flow_control_mode and reduce #ifdefs

This commit is contained in:
Matthias Ringwald 2017-05-02 10:00:58 +02:00
parent 197ab0eba1
commit 0e02617b40

View File

@ -70,6 +70,15 @@
#define RX_RING_BUFFER_SIZE 512
#endif
// Use BTSTACK_FLOW_CONTROL_MANUAL is used when Bluetooth RTS/CTS are not connected to UART RTS/CTS pins
// E.g. on RedBear Duo - WICED_BT_UART_MANUAL_CTS_RTS is defined
static enum {
BTSTACK_FLOW_CONTROL_OFF,
BTSTACK_FLOW_CONTROL_UART,
BTSTACK_FLOW_CONTROL_MANUAL,
} btstack_flow_control_mode;
static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * arg);
static wiced_worker_thread_t tx_worker_thread;
@ -110,11 +119,12 @@ static wiced_result_t btstack_uart_block_wiced_main_notify_block_read(void *arg)
// executed on tx worker thread
static wiced_result_t btstack_uart_block_wiced_tx_worker_send_block(void * arg){
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
while (platform_gpio_input_get(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]) == WICED_TRUE){
wiced_rtos_delay_milliseconds(100);
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL){
while (platform_gpio_input_get(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]) == WICED_TRUE){
wiced_rtos_delay_milliseconds(100);
}
}
#endif
// blocking send
platform_uart_transmit_bytes(wiced_bt_uart_driver, tx_worker_data_buffer, tx_worker_data_size);
// let transport know
@ -125,9 +135,9 @@ static wiced_result_t btstack_uart_block_wiced_tx_worker_send_block(void * arg){
// executed on rx worker thread
static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * arg){
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
#endif
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL){
platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
}
#ifdef WICED_UART_READ_DOES_NOT_RETURN_BYTES_READ
// older API passes in number of bytes to read (checked in 3.3.1 and 3.4.0)
@ -139,9 +149,9 @@ static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * ar
// assumption: bytes = bytes_to_read as timeout is never
#endif
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL){
platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
#endif
}
// let transport know
btstack_run_loop_wiced_execute_code_on_main_thread(&btstack_uart_block_wiced_main_notify_block_read, NULL);
@ -150,20 +160,36 @@ static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * ar
static int btstack_uart_block_wiced_init(const btstack_uart_config_t * config){
uart_config = config;
// determine flow control mode based on hardware config and uart config
if (uart_config->flowcontrol){
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
btstack_flow_control_mode = BTSTACK_FLOW_CONTROL_MANUAL;
#else
btstack_flow_control_mode = BTSTACK_FLOW_CONTROL_UART;
#endif
} else {
btstack_flow_control_mode = BTSTACK_FLOW_CONTROL_OFF;
}
return 0;
}
static int btstack_uart_block_wiced_open(void){
// UART config
wiced_uart_config_t uart_config =
wiced_uart_config_t wiced_uart_config =
{
.baud_rate = 115200,
.baud_rate = uart_config->baudrate,
.data_width = DATA_WIDTH_8BIT,
.parity = NO_PARITY,
.stop_bits = STOP_BITS_1,
.flow_control = FLOW_CONTROL_CTS_RTS,
};
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_UART){
wiced_uart_config.flow_control = FLOW_CONTROL_CTS_RTS;
} else {
wiced_uart_config.flow_control = FLOW_CONTROL_DISABLED;
}
wiced_ring_buffer_t * ring_buffer = NULL;
// configure HOST and DEVICE WAKE PINs
@ -192,11 +218,8 @@ static int btstack_uart_block_wiced_open(void){
// casts avoid warnings because of volatile qualifier
ring_buffer_init((wiced_ring_buffer_t *) &rx_ring_buffer, (uint8_t*) rx_data, sizeof( rx_data ) );
ring_buffer = (wiced_ring_buffer_t *) &rx_ring_buffer;
// don't try
uart_config.flow_control = FLOW_CONTROL_DISABLED;
#endif
platform_uart_init( wiced_bt_uart_driver, wiced_bt_uart_peripheral, &uart_config, ring_buffer );
platform_uart_init( wiced_bt_uart_driver, wiced_bt_uart_peripheral, &wiced_uart_config, ring_buffer );
// Reset Bluetooth via RESET line. Fallback to toggling POWER otherwise
@ -246,7 +269,7 @@ static int btstack_uart_block_wiced_set_baudrate(uint32_t baudrate){
// directly use STM peripheral functions to change baud rate dynamically
// set TX to high
log_info("h4_set_baudrate %u", (int) baudrate);
log_info("set baud %u", (int) baudrate);
const platform_gpio_t* gpio = wiced_bt_uart_pins[WICED_BT_PIN_UART_TX];
platform_gpio_output_high(gpio);
@ -269,10 +292,12 @@ static int btstack_uart_block_wiced_set_baudrate(uint32_t baudrate){
uart_init_structure.USART_WordLength = USART_WordLength_8b;
uart_init_structure.USART_StopBits = USART_StopBits_1;
uart_init_structure.USART_Parity = USART_Parity_No;
uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
#endif
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_UART){
uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
} else {
uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
}
USART_Init(wiced_bt_uart_peripheral->port, &uart_init_structure);
// enable USART again