wiced: move common files into platform/wiced

This commit is contained in:
Matthias Ringwald 2017-05-02 19:06:57 +02:00
parent 8e24486a4a
commit e25b4a2f90
13 changed files with 14 additions and 1324 deletions

View File

@ -12,6 +12,7 @@ GLOBAL_INCLUDES += \
. \
../../src \
../../platform/embedded \
../../platform/wiced \
../../chipset/bcm \
../../../../
@ -54,11 +55,11 @@ $(NAME)_SOURCES += \
# WICED port incl. support for Broadcom chipset
$(NAME)_SOURCES += \
main.c \
btstack_link_key_db_wiced_dct.c \
btstack_run_loop_wiced.c \
btstack_uart_block_wiced.c \
../../chipset/bcm/btstack_chipset_bcm.c \
main.c \
../../platform/wiced/btstack_link_key_db_wiced_dct.c \
../../platform/wiced/btstack_run_loop_wiced.c \
../../platform/wiced/btstack_uart_block_wiced.c \
../../chipset/bcm/btstack_chipset_bcm.c \
ifeq ($(BT_CHIP_XTAL_FREQUENCY),)
$(NAME)_SOURCES += ../../../drivers/bluetooth/firmware/$(BT_CHIP)$(BT_CHIP_REVISION)/bt_firmware_image.c

View File

@ -1,228 +0,0 @@
/*
* Copyright (C) 2016 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define __BTSTACK_FILE__ "btstack_link_key_db_wiced_dct.c"
/*
* btstack_link_key_db_wiced_dct.c
*
* Persistent Link Key implemenetation for WICED using DCT mechanism
*/
#include "classic/btstack_link_key_db.h"
#include "stdint.h"
#include "string.h"
#include "btstack_debug.h"
#include "btstack_util.h"
#include "wiced.h"
// Link Key Magic
#define LINK_KEY_MAGIC ((uint32_t) 'B' << 24 | 'T' << 16 | 'L' << 8 | 'K')
typedef struct link_key_nvm {
uint32_t magic;
uint32_t seq_nr; // used for "last recently stored" eviction strategy
bd_addr_t bd_addr;
link_key_t link_key;
link_key_type_t link_key_type;
} link_key_nvm_t;
static char link_key_to_str_buffer[LINK_KEY_STR_LEN+1]; // 11223344556677889900112233445566\0
static char *link_key_to_str(link_key_t link_key){
char * p = link_key_to_str_buffer;
int i;
for (i = 0; i < LINK_KEY_LEN ; i++) {
*p++ = char_for_nibble((link_key[i] >> 4) & 0x0F);
*p++ = char_for_nibble((link_key[i] >> 0) & 0x0F);
}
*p = 0;
return (char *) link_key_to_str_buffer;
}
static void link_key_db_init(void){
log_info("Link Key DB initialized for DCT\n");
}
static void link_key_db_close(void){
}
// @return valid
static int link_key_read(int index, link_key_nvm_t * out_entry){
// calculate address
uint32_t address = index * sizeof(link_key_nvm_t);
// read lock
link_key_nvm_t * entry;
wiced_dct_read_lock((void*) &entry, WICED_FALSE, DCT_APP_SECTION, address, sizeof(link_key_nvm_t));
if (entry->magic == LINK_KEY_MAGIC){
memcpy(out_entry, entry, sizeof(link_key_nvm_t));
} else {
memset(out_entry, 0, sizeof(link_key_nvm_t));
}
// read unlock
wiced_dct_read_unlock((void*) entry, WICED_FALSE);
return out_entry->magic == LINK_KEY_MAGIC;
}
static void link_key_write(int index, link_key_nvm_t * entry){
// calculate address
uint32_t address = index * sizeof(link_key_nvm_t);
// write block
wiced_dct_write((void*)entry, DCT_APP_SECTION, address, sizeof(link_key_nvm_t));
}
// returns entry index or -1
static int link_key_find_entry_for_address(bd_addr_t address){
link_key_nvm_t item;
int i;
for (i=0;i<NVM_NUM_LINK_KEYS;i++){
link_key_read(i, &item);
if (item.magic != LINK_KEY_MAGIC) continue;
if (memcmp(address, &item.bd_addr, 6) != 0) continue;
return i;
}
return -1;
}
// returns index
static int link_key_find_free_entry(void){
link_key_nvm_t item;
int i;
uint32_t seq_nr = 0;
int lowest_index = -1;
for (i=0;i<NVM_NUM_LINK_KEYS;i++){
link_key_read(i, &item);
if (item.magic != LINK_KEY_MAGIC) return i;
if ((lowest_index < 0) || (item.seq_nr < seq_nr)){
lowest_index = i;
seq_nr= item.seq_nr;
}
}
return lowest_index;
}
static uint32_t link_key_highest_seq_nr(void){
link_key_nvm_t item;
int i;
uint32_t seq_nr = 0;
for (i=0;i<NVM_NUM_LINK_KEYS;i++){
link_key_read(i, &item);
if (item.magic != LINK_KEY_MAGIC) continue;
if (item.seq_nr < seq_nr) continue;
seq_nr = item.seq_nr;
}
return seq_nr;
}
// returns 1 if found
static int link_key_db_get_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * link_key_type) {
int index = link_key_find_entry_for_address(bd_addr);
if (index < 0) {
log_info("link_key_db_get_link_key for %s -> not found\n", bd_addr_to_str(bd_addr));
return 0;
}
link_key_nvm_t item;
link_key_read(index, &item);
memcpy(link_key, item.link_key, LINK_KEY_LEN);
if (link_key_type) {
*link_key_type = item.link_key_type;
}
log_info("link_key_db_get_link_key for %s -> found %s\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key));
return 1;
}
static void link_key_db_delete_link_key(bd_addr_t bd_addr){
int index = link_key_find_entry_for_address(bd_addr);
if (index < 0) return;
link_key_nvm_t item;
memset(&item, 0, sizeof(item));
link_key_write(index, &item);
}
static void link_key_db_put_link_key(bd_addr_t bd_addr, link_key_t link_key, link_key_type_t link_key_type){
// check for existing record
int index = link_key_find_entry_for_address(bd_addr);
// if not found, use new entry
if (index < 0) {
index = link_key_find_free_entry();
}
log_info("link_key_db_put_link_key for %s - key %s - at index %u\n", bd_addr_to_str(bd_addr), link_key_to_str(link_key), index);
link_key_nvm_t item;
item.magic = LINK_KEY_MAGIC;
item.seq_nr = link_key_highest_seq_nr() + 1;
memcpy(item.bd_addr, bd_addr, sizeof(bd_addr_t));
memcpy(item.link_key, link_key, LINK_KEY_LEN);
item.link_key_type = link_key_type;
link_key_write(index, &item);
}
static void link_key_db_set_local_bd_addr(bd_addr_t bd_addr){
}
const btstack_link_key_db_t btstack_link_key_db_wiced_dct = {
link_key_db_init,
link_key_db_set_local_bd_addr,
link_key_db_close,
link_key_db_get_link_key,
link_key_db_put_link_key,
link_key_db_delete_link_key,
};
// custom function
void btstack_link_key_db_wiced_dct_delete_all(void){
int i;
link_key_nvm_t entry;
memset(&entry, 0, sizeof(link_key_nvm_t));
for (i=0;i<NVM_NUM_LINK_KEYS;i++){
link_key_write(i, &entry);
}
}
const btstack_link_key_db_t * btstack_link_key_db_wiced_dct_instance(void){
return &btstack_link_key_db_wiced_dct;
}

View File

@ -1,70 +0,0 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_link_key_db_wiced_dct.c
*
* Persistent Link Key implemenetation for WICED using DCT mechanism
*/
#ifndef __BTSTACK_LINK_KEY_WICED_DCT_H
#define __BTSTACK_LINK_KEY_WICED_DCT_H
#include "btstack_config.h"
#include "classic/btstack_link_key_db.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Get btstack_link_key_db_t instance
*/
const btstack_link_key_db_t * btstack_link_key_db_wiced_dct_instance(void);
/*
* @brief Delete all link keys
*/
void btstack_link_key_db_wiced_dct_delete_all(void);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // __BTSTACK_LINK_KEY_DB_MEMORY_H

View File

@ -1,181 +0,0 @@
/*
* Copyright (C) 2015 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define __BTSTACK_FILE__ "btstack_run_loop_wiced.c"
/*
* btstack_run_loop_wiced.c
*
* Run loop for Broadcom WICED SDK which currently supports FreeRTOS and ThreadX
* WICED 3.3.1 does not support Event Flags on FreeRTOS so a queue is used instead
*/
#include "wiced.h"
#include <stddef.h> // NULL
#include "btstack_linked_list.h"
#include "btstack_debug.h"
#include "btstack_run_loop.h"
#include "btstack_run_loop_wiced.h"
typedef struct function_call {
wiced_result_t (*fn)(void * arg);
void * arg;
} function_call_t;
static const btstack_run_loop_t btstack_run_loop_wiced;
static wiced_queue_t btstack_run_loop_queue;
// the run loop
static btstack_linked_list_t timers;
static uint32_t btstack_run_loop_wiced_get_time_ms(void){
wiced_time_t time;
wiced_time_get_time(&time);
return time;
}
// set timer
static void btstack_run_loop_wiced_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){
ts->timeout = btstack_run_loop_wiced_get_time_ms() + timeout_in_ms + 1;
}
/**
* Add timer to run_loop (keep list sorted)
*/
static void btstack_run_loop_wiced_add_timer(btstack_timer_source_t *ts){
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){
// don't add timer that's already in there
if ((btstack_timer_source_t *) it->next == ts){
log_error( "btstack_run_loop_timer_add error: timer to add already in list!");
return;
}
if (ts->timeout < ((btstack_timer_source_t *) it->next)->timeout) {
break;
}
}
ts->item.next = it->next;
it->next = (btstack_linked_item_t *) ts;
}
/**
* Remove timer from run loop
*/
static int btstack_run_loop_wiced_remove_timer(btstack_timer_source_t *ts){
return btstack_linked_list_remove(&timers, (btstack_linked_item_t *) ts);
}
static void btstack_run_loop_wiced_dump_timer(void){
#ifdef ENABLE_LOG_INFO
btstack_linked_item_t *it;
int i = 0;
for (it = (btstack_linked_item_t *) timers; it ; it = it->next){
btstack_timer_source_t *ts = (btstack_timer_source_t*) it;
log_info("timer %u, timeout %u\n", i, (unsigned int) ts->timeout);
}
#endif
}
// schedules execution similar to wiced_rtos_send_asynchronous_event for worker threads
void btstack_run_loop_wiced_execute_code_on_main_thread(wiced_result_t (*fn)(void *arg), void * arg){
function_call_t message;
message.fn = fn;
message.arg = arg;
wiced_rtos_push_to_queue(&btstack_run_loop_queue, &message, WICED_NEVER_TIMEOUT);
}
/**
* Execute run_loop
*/
static void btstack_run_loop_wiced_execute(void) {
while (1) {
// get next timeout
uint32_t timeout_ms = WICED_NEVER_TIMEOUT;
if (timers) {
btstack_timer_source_t * ts = (btstack_timer_source_t *) timers;
uint32_t now = btstack_run_loop_wiced_get_time_ms();
if (ts->timeout < now){
// remove timer before processing it to allow handler to re-register with run loop
btstack_run_loop_remove_timer(ts);
// printf("RL: timer %p\n", ts->process);
ts->process(ts);
continue;
}
timeout_ms = ts->timeout - now;
}
// pop function call
function_call_t message = { NULL, NULL };
wiced_rtos_pop_from_queue( &btstack_run_loop_queue, &message, timeout_ms);
if (message.fn){
// execute code on run loop
// printf("RL: execute %p\n", message.fn);
message.fn(message.arg);
}
}
}
static void btstack_run_loop_wiced_btstack_run_loop_init(void){
timers = NULL;
// queue to receive events: up to 2 calls from transport, up to 3 for app
wiced_rtos_init_queue(&btstack_run_loop_queue, "BTstack Run Loop", sizeof(function_call_t), 5);
}
/**
* @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
*/
const btstack_run_loop_t * btstack_run_loop_wiced_get_instance(void){
return &btstack_run_loop_wiced;
}
static const btstack_run_loop_t btstack_run_loop_wiced = {
&btstack_run_loop_wiced_btstack_run_loop_init,
NULL,
NULL,
NULL,
NULL,
&btstack_run_loop_wiced_set_timer,
&btstack_run_loop_wiced_add_timer,
&btstack_run_loop_wiced_remove_timer,
&btstack_run_loop_wiced_execute,
&btstack_run_loop_wiced_dump_timer,
&btstack_run_loop_wiced_get_time_ms,
};

View File

@ -1,71 +0,0 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* btstack_run_loop_wiced.h
*
* Functions relevant for BTstack WICED port
*/
#ifndef __btstack_run_loop_WICED_H
#define __btstack_run_loop_WICED_H
#include "btstack_config.h"
#include "btstack_run_loop.h"
#include "wiced.h"
#if defined __cplusplus
extern "C" {
#endif
/**
* @brief Provide btstack_run_loop_posix instance for use with btstack_run_loop_init
*/
const btstack_run_loop_t * btstack_run_loop_wiced_get_instance(void);
/*
* @brief Execute code on BTstack run loop. Can be used to control BTstack from a different thread
*/
void btstack_run_loop_wiced_execute_code_on_main_thread(wiced_result_t (*fn)(void *arg), void * arg);
/* API_END */
#if defined __cplusplus
}
#endif
#endif // __btstack_run_loop_WICED_H

View File

@ -1,384 +0,0 @@
/*
* Copyright (C) 2015 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* hci_h4_transport_wiced.c
*
* HCI Transport API implementation for basic H4 protocol for use with btstack_run_loop_wiced.c
*/
#define __BTSTACK_FILE__ "hci_transport_h4_wiced.c"
#include "btstack_config.h"
#include "btstack_run_loop_wiced.h"
#include "btstack_debug.h"
#include "hci.h"
#include "hci_transport.h"
#include "platform_bluetooth.h"
#include "wiced.h"
#include <stdio.h>
#include <string.h>
// priority higher than WIFI to make sure RTS is set
#define WICED_BT_UART_THREAD_PRIORITY (WICED_NETWORK_WORKER_PRIORITY - 2)
#define WICED_BT_UART_THREAD_STACK_SIZE 300
// assert pre-buffer for packet type is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
#endif
// Default of 512 bytes should be fine. Only needed with BTSTACK_FLOW_CONTROL_UART
#ifndef RX_RING_BUFFER_SIZE
#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;
static const uint8_t * tx_worker_data_buffer;
static uint16_t tx_worker_data_size;
static wiced_worker_thread_t rx_worker_thread;
static uint8_t * rx_worker_read_buffer;
static uint16_t rx_worker_read_size;
static wiced_ring_buffer_t rx_ring_buffer;
static uint8_t rx_data[RX_RING_BUFFER_SIZE];
// uart config
static const btstack_uart_config_t * uart_config;
// callbacks
static void (*block_sent)(void);
static void (*block_received)(void);
// executed on main run loop
static wiced_result_t btstack_uart_block_wiced_main_notify_block_send(void *arg){
if (block_sent){
block_sent();
}
return WICED_SUCCESS;
}
// executed on main run loop
static wiced_result_t btstack_uart_block_wiced_main_notify_block_read(void *arg){
if (block_received){
block_received();
}
return WICED_SUCCESS;
}
// executed on tx worker thread
static wiced_result_t btstack_uart_block_wiced_tx_worker_send_block(void * arg){
// wait for CTS to become low in manual flow control mode
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL && wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){
while (platform_gpio_input_get(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]) == WICED_TRUE){
wiced_rtos_delay_milliseconds(10);
}
}
// blocking send
platform_uart_transmit_bytes(wiced_bt_uart_driver, tx_worker_data_buffer, tx_worker_data_size);
// let transport know
btstack_run_loop_wiced_execute_code_on_main_thread(&btstack_uart_block_wiced_main_notify_block_send, NULL);
return WICED_SUCCESS;
}
// executed on rx worker thread
static wiced_result_t btstack_uart_block_wiced_rx_worker_receive_block(void * arg){
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL && wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){
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)
platform_uart_receive_bytes(wiced_bt_uart_driver, rx_worker_read_buffer, rx_worker_read_size, WICED_NEVER_TIMEOUT);
#else
// newer API uses pointer to return number of read bytes
uint32_t bytes = rx_worker_read_size;
platform_uart_receive_bytes(wiced_bt_uart_driver, rx_worker_read_buffer, &bytes, WICED_NEVER_TIMEOUT);
// assumption: bytes = bytes_to_read as timeout is never
#endif
if (btstack_flow_control_mode == BTSTACK_FLOW_CONTROL_MANUAL && wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){
platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
}
// let transport know
btstack_run_loop_wiced_execute_code_on_main_thread(&btstack_uart_block_wiced_main_notify_block_read, NULL);
return WICED_SUCCESS;
}
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 wiced_uart_config =
{
.baud_rate = uart_config->baudrate,
.data_width = DATA_WIDTH_8BIT,
.parity = NO_PARITY,
.stop_bits = STOP_BITS_1,
};
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
platform_gpio_init(wiced_bt_control_pins[WICED_BT_PIN_HOST_WAKE], INPUT_HIGH_IMPEDANCE);
platform_gpio_init(wiced_bt_control_pins[WICED_BT_PIN_DEVICE_WAKE], OUTPUT_PUSH_PULL);
platform_gpio_output_low(wiced_bt_control_pins[WICED_BT_PIN_DEVICE_WAKE]);
/* Configure Reg Enable pin to output. Set to HIGH */
if (wiced_bt_control_pins[ WICED_BT_PIN_POWER ]){
platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_POWER ], OUTPUT_OPEN_DRAIN_PULL_UP );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] );
}
wiced_rtos_delay_milliseconds( 100 );
// Configure RTS
if (wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]){
switch (btstack_flow_control_mode){
case BTSTACK_FLOW_CONTROL_OFF:
// configure RTS pin as output and set to low - always on
platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS], OUTPUT_PUSH_PULL);
platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
break;
case BTSTACK_FLOW_CONTROL_UART:
// configuration done by platform_uart_init
break;
case BTSTACK_FLOW_CONTROL_MANUAL:
// configure RTS pin as output and set to high - controlled by btstack_uart_block_wiced_rx_worker_receive_block
platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS], OUTPUT_PUSH_PULL);
platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
break;
}
}
// Configure CTS
if (wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS]){
switch (btstack_flow_control_mode){
case BTSTACK_FLOW_CONTROL_OFF:
// don't care
break;
case BTSTACK_FLOW_CONTROL_UART:
// configuration done by platform_uart_init
break;
case BTSTACK_FLOW_CONTROL_MANUAL:
// configure CTS to input, pull-up
platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS], INPUT_PULL_UP);
break;
}
}
// use ring buffer to allow to receive RX_RING_BUFFER_SIZE/2 addition bytes - not needed with hardware UART
if (btstack_flow_control_mode != BTSTACK_FLOW_CONTROL_UART){
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;
}
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
if ( wiced_bt_control_pins[ WICED_BT_PIN_RESET ]){
platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_RESET ], OUTPUT_PUSH_PULL );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] );
platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] );
wiced_rtos_delay_milliseconds( 100 );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] );
}
else if ( wiced_bt_control_pins[ WICED_BT_PIN_POWER ]){
platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] );
wiced_rtos_delay_milliseconds( 100 );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] );
}
// wait for Bluetooth to start up
wiced_rtos_delay_milliseconds( 500 );
// create worker threads for rx/tx. only single request is posted to their queues
wiced_rtos_create_worker_thread(&tx_worker_thread, WICED_BT_UART_THREAD_PRIORITY, WICED_BT_UART_THREAD_STACK_SIZE, 1);
wiced_rtos_create_worker_thread(&rx_worker_thread, WICED_BT_UART_THREAD_PRIORITY, WICED_BT_UART_THREAD_STACK_SIZE, 1);
// tx is ready
tx_worker_data_size = 0;
return 0;
}
static int btstack_uart_block_wiced_close(void){
// not implemented
return 0;
}
static void btstack_uart_block_wiced_set_block_received( void (*block_handler)(void)){
block_received = block_handler;
}
static void btstack_uart_block_wiced_set_block_sent( void (*block_handler)(void)){
block_sent = block_handler;
}
static int btstack_uart_block_wiced_set_baudrate(uint32_t baudrate){
#if defined(_STM32F205RGT6_) || defined(STM32F40_41xxx)
// directly use STM peripheral functions to change baud rate dynamically
// set TX to high
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);
// reconfigure TX pin as GPIO
GPIO_InitTypeDef gpio_init_structure;
gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init_structure.GPIO_Mode = GPIO_Mode_OUT;
gpio_init_structure.GPIO_OType = GPIO_OType_PP;
gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << gpio->pin_number );
GPIO_Init( gpio->port, &gpio_init_structure );
// disable USART
USART_Cmd( wiced_bt_uart_peripheral->port, DISABLE );
// setup init structure
USART_InitTypeDef uart_init_structure;
uart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
uart_init_structure.USART_BaudRate = 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;
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
USART_Cmd( wiced_bt_uart_peripheral->port, ENABLE );
// set TX pin as USART again
gpio_init_structure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init( gpio->port, &gpio_init_structure );
#else
log_error("btstack_uart_block_wiced_set_baudrate not implemented for this WICED Platform");
#endif
return 0;
}
static int btstack_uart_block_wiced_set_parity(int parity){
log_error("btstack_uart_block_wiced_set_parity not implemented");
return 0;
}
static void btstack_uart_block_wiced_send_block(const uint8_t *buffer, uint16_t length){
// store in request
tx_worker_data_buffer = buffer;
tx_worker_data_size = length;
wiced_rtos_send_asynchronous_event(&tx_worker_thread, &btstack_uart_block_wiced_tx_worker_send_block, NULL);
}
static void btstack_uart_block_wiced_receive_block(uint8_t *buffer, uint16_t len){
rx_worker_read_buffer = buffer;
rx_worker_read_size = len;
wiced_rtos_send_asynchronous_event(&rx_worker_thread, &btstack_uart_block_wiced_rx_worker_receive_block, NULL);
}
// static void btstack_uart_block_wiced_set_sleep(uint8_t sleep){
// }
// static void btstack_uart_block_wiced_set_csr_irq_handler( void (*csr_irq_handler)(void)){
// }
static const btstack_uart_block_t btstack_uart_block_wiced = {
/* int (*init)(hci_transport_config_uart_t * config); */ &btstack_uart_block_wiced_init,
/* int (*open)(void); */ &btstack_uart_block_wiced_open,
/* int (*close)(void); */ &btstack_uart_block_wiced_close,
/* void (*set_block_received)(void (*handler)(void)); */ &btstack_uart_block_wiced_set_block_received,
/* void (*set_block_sent)(void (*handler)(void)); */ &btstack_uart_block_wiced_set_block_sent,
/* int (*set_baudrate)(uint32_t baudrate); */ &btstack_uart_block_wiced_set_baudrate,
/* int (*set_parity)(int parity); */ &btstack_uart_block_wiced_set_parity,
/* void (*receive_block)(uint8_t *buffer, uint16_t len); */ &btstack_uart_block_wiced_receive_block,
/* void (*send_block)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_block_wiced_send_block,
/* int (*get_supported_sleep_modes); */ NULL,
/* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */ NULL,
/* void (*set_wakeup_handler)(void (*handler)(void)); */ NULL,
};
const btstack_uart_block_t * btstack_uart_block_wiced_instance(void){
return &btstack_uart_block_wiced;
}

View File

@ -1,378 +0,0 @@
/*
* Copyright (C) 2015 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
/*
* hci_h4_transport_wiced.c
*
* HCI Transport API implementation for basic H4 protocol for use with btstack_run_loop_wiced.c
*/
#define __BTSTACK_FILE__ "hci_transport_h4_wiced.c"
#include "btstack_config.h"
#include "btstack_run_loop_wiced.h"
#include "btstack_debug.h"
#include "hci.h"
#include "hci_transport.h"
#include "platform_bluetooth.h"
#include "wiced.h"
#include <stdio.h>
#include <string.h>
// priority higher than WIFI to make sure RTS is set
#define WICED_BT_UART_THREAD_PRIORITY (WICED_NETWORK_WORKER_PRIORITY - 2)
#define WICED_BT_UART_THREAD_STACK_SIZE 300
// assert pre-buffer for packet type is available
#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE == 0)
#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined. Please update hci.h
#endif
/* Default of 512 bytes should be fine. Only needed if WICED_BT_UART_MANUAL_CTS_RTS */
#ifndef RX_RING_BUFFER_SIZE
#define RX_RING_BUFFER_SIZE 512
#endif
static wiced_result_t h4_rx_worker_receive_packet(void * arg);
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size);
typedef struct hci_transport_h4 {
hci_transport_t transport;
btstack_data_source_t *ds;
int uart_fd; // different from ds->fd for HCI reader thread
/* power management support, e.g. used by iOS */
btstack_timer_source_t sleep_timer;
} hci_transport_h4_t;
// single instance
static hci_transport_h4_t * hci_transport_h4 = NULL;
static hci_transport_config_uart_t * hci_transport_config_uart = NULL;
static void (*packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = dummy_handler;
static wiced_worker_thread_t tx_worker_thread;
static const uint8_t * tx_worker_data_buffer;
static uint16_t tx_worker_data_size;
static wiced_worker_thread_t rx_worker_thread;
static int rx_worker_read_pos;
static uint8_t hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + 1 + HCI_PACKET_BUFFER_SIZE]; // packet type + max(acl header + acl payload, event header + event data)
static uint8_t * hci_packet = &hci_packet_with_pre_buffer[HCI_INCOMING_PRE_BUFFER_SIZE];
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
static volatile wiced_ring_buffer_t rx_ring_buffer;
static volatile uint8_t rx_data[RX_RING_BUFFER_SIZE];
#endif
// executed on main run loop
static wiced_result_t h4_main_deliver_packet(void *arg){
// deliver packet
packet_handler(hci_packet[0], &hci_packet[1], rx_worker_read_pos-1);
// trigger receive of next packet
wiced_rtos_send_asynchronous_event(&rx_worker_thread, &h4_rx_worker_receive_packet, NULL);
return WICED_SUCCESS;
}
// executed on main run loop
static wiced_result_t h4_main_notify_packet_send(void *arg){
// prepare for next packet
tx_worker_data_size = 0;
// notify upper stack that it might be possible to send again
uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0};
packet_handler(HCI_EVENT_PACKET, &event[0], sizeof(event));
return WICED_SUCCESS;
}
// executed on rx worker thread
static void h4_rx_worker_receive_bytes(int bytes_to_read){
#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)
platform_uart_receive_bytes(wiced_bt_uart_driver, &hci_packet[rx_worker_read_pos], bytes_to_read, WICED_NEVER_TIMEOUT);
#else
// newer API uses pointer to return number of read bytes
uint32_t bytes = bytes_to_read;
platform_uart_receive_bytes(wiced_bt_uart_driver, &hci_packet[rx_worker_read_pos], &bytes, WICED_NEVER_TIMEOUT);
// assumption: bytes = bytes_to_rad as timeout is never
#endif
rx_worker_read_pos += bytes_to_read;
}
static wiced_result_t h4_rx_worker_receive_packet(void * arg){
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
platform_gpio_output_low(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
#endif
while (1){
rx_worker_read_pos = 0;
h4_rx_worker_receive_bytes(1);
switch (hci_packet[0]){
case HCI_EVENT_PACKET:
h4_rx_worker_receive_bytes(HCI_EVENT_HEADER_SIZE);
h4_rx_worker_receive_bytes(hci_packet[2]);
break;
case HCI_ACL_DATA_PACKET:
h4_rx_worker_receive_bytes(HCI_ACL_HEADER_SIZE);
h4_rx_worker_receive_bytes(little_endian_read_16( hci_packet, 3));
break;
case HCI_SCO_DATA_PACKET:
h4_rx_worker_receive_bytes(HCI_SCO_HEADER_SIZE);
h4_rx_worker_receive_bytes(hci_packet[3]);
break;
default:
// try again
log_error("h4_rx_worker_receive_packet: invalid packet type 0x%02x", hci_packet[0]);
continue;
}
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
#endif
// deliver packet on main thread
btstack_run_loop_wiced_execute_code_on_main_thread(&h4_main_deliver_packet, NULL);
return WICED_SUCCESS;
}
}
// executed on tx worker thread
static wiced_result_t h4_tx_worker_send_packet(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);
}
#endif
// blocking send
platform_uart_transmit_bytes(wiced_bt_uart_driver, tx_worker_data_buffer, tx_worker_data_size);
// let stack know
btstack_run_loop_wiced_execute_code_on_main_thread(&h4_main_notify_packet_send, NULL);
return WICED_SUCCESS;
}
static int h4_set_baudrate(uint32_t baudrate){
#if defined(_STM32F205RGT6_) || defined(STM32F40_41xxx)
// directly use STM peripheral functions to change baud rate dynamically
// set TX to high
log_info("h4_set_baudrate %u", (int) baudrate);
const platform_gpio_t* gpio = wiced_bt_uart_pins[WICED_BT_PIN_UART_TX];
platform_gpio_output_high(gpio);
// reconfigure TX pin as GPIO
GPIO_InitTypeDef gpio_init_structure;
gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
gpio_init_structure.GPIO_Mode = GPIO_Mode_OUT;
gpio_init_structure.GPIO_OType = GPIO_OType_PP;
gpio_init_structure.GPIO_PuPd = GPIO_PuPd_NOPULL;
gpio_init_structure.GPIO_Pin = (uint32_t) ( 1 << gpio->pin_number );
GPIO_Init( gpio->port, &gpio_init_structure );
// disable USART
USART_Cmd( wiced_bt_uart_peripheral->port, DISABLE );
// setup init structure
USART_InitTypeDef uart_init_structure;
uart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
uart_init_structure.USART_BaudRate = 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
USART_Init(wiced_bt_uart_peripheral->port, &uart_init_structure);
// enable USART again
USART_Cmd( wiced_bt_uart_peripheral->port, ENABLE );
// set TX pin as USART again
gpio_init_structure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init( gpio->port, &gpio_init_structure );
#else
log_error("h4_set_baudrate not implemented for this WICED Platform");
#endif
return 0;
}
static void h4_init(const void * transport_config){
// check for hci_transport_config_uart_t
if (!transport_config) {
log_error("hci_transport_h4_wiced: no config!");
return;
}
if (((hci_transport_config_t*)transport_config)->type != HCI_TRANSPORT_CONFIG_UART) {
log_error("hci_transport_h4_wiced: config not of type != HCI_TRANSPORT_CONFIG_UART!");
return;
}
hci_transport_config_uart = (hci_transport_config_uart_t*) transport_config;
}
static int h4_open(void){
// UART config
wiced_uart_config_t uart_config =
{
.baud_rate = 115200,
.data_width = DATA_WIDTH_8BIT,
.parity = NO_PARITY,
.stop_bits = STOP_BITS_1,
.flow_control = FLOW_CONTROL_CTS_RTS,
};
wiced_ring_buffer_t * ring_buffer = NULL;
// configure HOST and DEVICE WAKE PINs
platform_gpio_init(wiced_bt_control_pins[WICED_BT_PIN_HOST_WAKE], INPUT_HIGH_IMPEDANCE);
platform_gpio_init(wiced_bt_control_pins[WICED_BT_PIN_DEVICE_WAKE], OUTPUT_PUSH_PULL);
platform_gpio_output_low(wiced_bt_control_pins[WICED_BT_PIN_DEVICE_WAKE]);
/* Configure Reg Enable pin to output. Set to HIGH */
if (wiced_bt_control_pins[ WICED_BT_PIN_POWER ]){
platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_POWER ], OUTPUT_OPEN_DRAIN_PULL_UP );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] );
}
wiced_rtos_delay_milliseconds( 100 );
// -- init UART
#ifdef WICED_BT_UART_MANUAL_CTS_RTS
// configure RTS pin as output and set to high
platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS], OUTPUT_PUSH_PULL);
platform_gpio_output_high(wiced_bt_uart_pins[WICED_BT_PIN_UART_RTS]);
// configure CTS to input, pull-up
platform_gpio_init(wiced_bt_uart_pins[WICED_BT_PIN_UART_CTS], INPUT_PULL_UP);
// use ring buffer to allow to receive RX_RING_BUFFER_SIZE/2 addition bytes before raising RTS
// 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 );
// Reset Bluetooth via RESET line. Fallback to toggling POWER otherwise
if ( wiced_bt_control_pins[ WICED_BT_PIN_RESET ]){
platform_gpio_init( wiced_bt_control_pins[ WICED_BT_PIN_RESET ], OUTPUT_PUSH_PULL );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] );
platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] );
wiced_rtos_delay_milliseconds( 100 );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_RESET ] );
}
else if ( wiced_bt_control_pins[ WICED_BT_PIN_POWER ]){
platform_gpio_output_low( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] );
wiced_rtos_delay_milliseconds( 100 );
platform_gpio_output_high( wiced_bt_control_pins[ WICED_BT_PIN_POWER ] );
}
// wait for Bluetooth to start up
wiced_rtos_delay_milliseconds( 500 );
// create worker threads for rx/tx. only single request is posted to their queues
wiced_rtos_create_worker_thread(&tx_worker_thread, WICED_BT_UART_THREAD_PRIORITY, WICED_BT_UART_THREAD_STACK_SIZE, 1);
wiced_rtos_create_worker_thread(&rx_worker_thread, WICED_BT_UART_THREAD_PRIORITY, WICED_BT_UART_THREAD_STACK_SIZE, 1);
// start receiving packet
wiced_rtos_send_asynchronous_event(&rx_worker_thread, &h4_rx_worker_receive_packet, NULL);
// tx is ready
tx_worker_data_size = 0;
return 0;
}
static int h4_close(void){
// not implementd
return 0;
}
static int h4_send_packet(uint8_t packet_type, uint8_t * data, int size){
// store packet type before actual data and increase size
size++;
data--;
*data = packet_type;
// store in request
tx_worker_data_buffer = data;
tx_worker_data_size = size;
// send packet as single block
wiced_rtos_send_asynchronous_event(&tx_worker_thread, &h4_tx_worker_send_packet, NULL);
return 0;
}
static void h4_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){
packet_handler = handler;
}
static int h4_can_send_packet_now(uint8_t packet_type){
return tx_worker_data_size == 0;
}
static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){
}
// get h4 singleton
const hci_transport_t * hci_transport_h4_instance(const btstack_uart_block_t * uart_driver) {
if (hci_transport_h4 == NULL) {
hci_transport_h4 = (hci_transport_h4_t*)malloc( sizeof(hci_transport_h4_t));
memset(hci_transport_h4, 0, sizeof(hci_transport_h4_t));
hci_transport_h4->ds = NULL;
hci_transport_h4->transport.name = "H4_WICED";
hci_transport_h4->transport.init = h4_init;
hci_transport_h4->transport.open = h4_open;
hci_transport_h4->transport.close = h4_close;
hci_transport_h4->transport.register_packet_handler = h4_register_packet_handler;
hci_transport_h4->transport.can_send_packet_now = h4_can_send_packet_now;
hci_transport_h4->transport.send_packet = h4_send_packet;
hci_transport_h4->transport.set_baudrate = h4_set_baudrate;
}
return (const hci_transport_t *) hci_transport_h4;
}

View File

@ -12,6 +12,7 @@ GLOBAL_INCLUDES += \
. \
../../src \
../../platform/embedded \
../../platform/wiced \
../../chipset/bcm \
../../../../
@ -55,13 +56,13 @@ $(NAME)_SOURCES += \
# WICED port incl. support for Broadcom chipset
$(NAME)_SOURCES += \
main.c \
btstack_link_key_db_wiced_dct.c \
btstack_run_loop_wiced.c \
btstack_uart_block_wiced.c \
btstack_aes128_wiced.c \
../../chipset/bcm/btstack_chipset_bcm.c \
../../chipset/bcm/btstack_chipset_bcm_download_firmware.c \
main.c \
btstack_aes128_wiced.c \
../../platform/wiced/btstack_link_key_db_wiced_dct.c \
../../platform/wiced/btstack_run_loop_wiced.c \
../../platform/wiced/btstack_uart_block_wiced.c \
../../chipset/bcm/btstack_chipset_bcm.c \
../../chipset/bcm/btstack_chipset_bcm_download_firmware.c \
ifeq ($(BT_CHIP_XTAL_FREQUENCY),)
$(NAME)_SOURCES += ../../../drivers/bluetooth/firmware/$(BT_CHIP)$(BT_CHIP_REVISION)/bt_firmware_image.c