chipset-bcm: support .hcd files in posix-h4 port

This commit is contained in:
Matthias Ringwald 2016-11-14 17:40:06 +01:00
parent 0e290b474c
commit 64eccac7d9
4 changed files with 225 additions and 18 deletions

View File

@ -51,21 +51,24 @@
#include "btstack_debug.h"
#include "btstack_chipset_bcm.h"
#ifdef HAVE_POSIX_FILE_IO
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#endif
static int send_download_command;
static uint32_t init_script_offset;
// Embedded == non posix systems
// actual init script provided by separate bt_firmware_image.c from WICED SDK
extern const uint8_t brcm_patchram_buf[];
extern const int brcm_patch_ram_length;
extern const char brcm_patch_version[];
//
static uint32_t init_script_offset;
static int send_download_command;
static void chipset_init(const void * config){
log_info("Broadcom init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
init_script_offset = 0;
send_download_command = 1;
}
// @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c
static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
hci_cmd_buffer[0] = 0x18;
@ -84,6 +87,170 @@ static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer)
reverse_bd_addr(addr, &hci_cmd_buffer[3]);
}
#ifdef HAVE_POSIX_FILE_IO
static const char * hcd_file_path;
static const char * hcd_folder_path = ".";
static int hcd_fd;
static char matched_file[1000];
static void chipset_init(const void * config){
if (hcd_file_path){
log_info("chipset-bcm: init file %s", hcd_file_path);
} else {
log_info("chipset-bcm: init folder %s", hcd_folder_path);
}
send_download_command = 1;
init_script_offset = 0;
hcd_fd = -1;
}
static const uint8_t download_command[] = {0x2e, 0xfc, 0x00};
static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
if (hcd_fd < 0){
log_info("chipset-bcm: open file %s", hcd_file_path);
hcd_fd = open(hcd_file_path, O_RDONLY);
if (hcd_fd < 0){
log_error("chipset-bcm: can't open file %s", hcd_file_path);
return BTSTACK_CHIPSET_DONE;
}
}
// send download firmware command
if (send_download_command){
send_download_command = 0;
memcpy(hci_cmd_buffer, download_command, sizeof(download_command));
return BTSTACK_CHIPSET_VALID_COMMAND;
}
// read next command, but skip download command
do {
// read command
int res = read(hcd_fd, hci_cmd_buffer, 3);
if (res == 0){
log_info("chipset-bcm: end of file, size %u", init_script_offset);
close(hcd_fd);
// wait for firmware patch to be applied - shorter delay possible
sleep(1);
return BTSTACK_CHIPSET_DONE;
}
if (res < 0){
log_error("chipset-bcm: read error at %u", init_script_offset);
return BTSTACK_CHIPSET_DONE;
}
init_script_offset += 3;
// read parameters
int param_len = hci_cmd_buffer[2];
if (param_len){
res = read(hcd_fd, &hci_cmd_buffer[3], param_len);
}
if (res < 0){
log_error("chipset-bcm: read error at %u", init_script_offset);
return BTSTACK_CHIPSET_DONE;
}
init_script_offset += param_len;
} while (memcmp(hci_cmd_buffer, download_command, sizeof(download_command)) == 1);
return BTSTACK_CHIPSET_VALID_COMMAND;
}
void btstack_chipset_bcm_set_hcd_file_path(const char * path){
hcd_file_path = path;
}
void btstack_chipset_bcm_set_hcd_folder_path(const char * path){
hcd_folder_path = path;
}
static int equal_ignore_case(const char *str1, const char *str2){
if (!str1 || !str2) return (1);
int i = 0;
while (1){
if (!str1[i] && !str2[i]) return 1;
if (tolower(str1[i]) != tolower(str2[i])) return 0;
if (!str1[i] || !str2[i]) return 0;
i++;
}
}
// assumption starts with BCM or bcm
#define MAX_DEVICE_NAME_LEN 15
void btstack_chipset_bcm_set_device_name(const char * device_name){
// ignore if file path already set
if (hcd_file_path) {
log_error("chipset-bcm: set device name called %s although path %s already set", device_name, hcd_file_path);
return;
}
// construct filename for long variant
if (strlen(device_name) > MAX_DEVICE_NAME_LEN){
log_error("chipset-bcm: device name %s too long", device_name);
return;
}
char filename_complete[MAX_DEVICE_NAME_LEN+5];
strcpy(filename_complete, device_name);
strcat(filename_complete, ".hcd");
// construct short variant without revision info
char filename_short[MAX_DEVICE_NAME_LEN+5];
strcpy(filename_short, device_name);
int len = strlen(filename_short);
while (len > 3){
char c = filename_short[len-1];
if (isdigit(c) == 0) break;
len--;
}
if (len > 3){
filename_short[len-1] = 0;
}
strcat(filename_short, ".hcd");
log_info("chipset-bcm: looking for %s and %s", filename_short, filename_complete);
// find in folder
DIR *dirp = opendir(hcd_folder_path);
int match_short = 0;
int match_complete = 0;
if (!dirp){
log_error("chipset-bcm: could not get directory for %s", hcd_folder_path);
return;
}
while (1){
struct dirent *dp = readdir(dirp);
if (!dp) break;
if (equal_ignore_case(filename_complete, dp->d_name)){
match_complete = 1;
continue;
}
if (equal_ignore_case(filename_short, dp->d_name)){
match_short = 1;
}
}
closedir(dirp);
if (match_complete){
sprintf(matched_file, "%s/%s", hcd_folder_path, filename_complete);
hcd_file_path = matched_file;
return;
}
if (match_short){
sprintf(matched_file, "%s/%s", hcd_folder_path, filename_short);
hcd_file_path = matched_file;
return;
}
log_error("chipset-bcm: could not find %s or %s, please provide .hcd file in %s", filename_complete, filename_short, hcd_folder_path);
}
#else
static void chipset_init(const void * config){
log_info("chipset-bcm: init script %s, len %u", brcm_patch_version, brcm_patch_ram_length);
init_script_offset = 0;
send_download_command = 1;
}
static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
// send download firmware command
if (send_download_command){
@ -103,6 +270,7 @@ static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
init_script_offset += cmd_len;
return BTSTACK_CHIPSET_VALID_COMMAND;
}
#endif
static const btstack_chipset_t btstack_chipset_bcm = {

View File

@ -52,6 +52,27 @@ extern "C" {
const btstack_chipset_t * btstack_chipset_bcm_instance(void);
// Support for loading .hcd init files on POSIX systems
/**
* @brief Set path to .hcd init file
* @param path
*/
void btstack_chipset_bcm_set_hcd_file_path(const char * path);
/**
* @brief Set folder to look for .hcd init files
* @param path
*/
void btstack_chipset_bcm_set_hcd_folder_path(const char * path);
/**
* @brief Look for .hcd init file based on device name
* @param device_name e.g. BCM43430A1
*/
void btstack_chipset_bcm_set_device_name(const char * path);
#if defined __cplusplus
}
#endif

View File

@ -3,6 +3,7 @@ BTSTACK_ROOT = ../..
CORE += \
bluetooth_init_cc2564B_1.4_BT_Spec_4.1.c \
btstack_chipset_bcm.c \
btstack_chipset_cc256x.c \
btstack_chipset_csr.c \
btstack_chipset_em9301.c \
@ -16,7 +17,6 @@ CORE += \
main.c \
stdin_support.c \
# bluetooth_init_cc2564_2.14.c \
# btstack_chipset_bcm.c \
# TI-WL183x requires TIInit_11.8.32.c

View File

@ -66,6 +66,8 @@
#include "btstack_chipset_stlc2500d.h"
#include "btstack_chipset_tc3566x.h"
int is_bcm;
int btstack_main(int argc, const char * argv[]);
static hci_transport_config_uart_t config = {
@ -80,9 +82,24 @@ static btstack_packet_callback_registration_t hci_event_callback_registration;
static void packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
if (packet_type != HCI_EVENT_PACKET) return;
if (hci_event_packet_get_type(packet) != BTSTACK_EVENT_STATE) return;
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) return;
printf("BTstack up and running.\n");
switch (hci_event_packet_get_type(packet)){
case BTSTACK_EVENT_STATE:
if (btstack_event_state_get_state(packet) != HCI_STATE_WORKING) break;
printf("BTstack up and running.\n");
break;
case HCI_EVENT_COMMAND_COMPLETE:
if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_local_name)){
// terminate, name 248 chars
packet[6+248] = 0;
printf("Local name: %s\n", &packet[6]);
if (is_bcm){
btstack_chipset_bcm_set_device_name((const char *)&packet[6]);
}
}
break;
default:
break;
}
}
static void sigint_handler(int param){
@ -131,7 +148,7 @@ static void local_version_information_callback(uint8_t * packet){
printf("- Manufacturer 0x%04x\n", manufacturer);
switch (manufacturer){
case COMPANY_ID_CAMBRIDGE_SILICON_RADIO:
printf("Cambridge Silicon Radio CSR chipset.\n");
printf("Cambridge Silicon Radio - CSR chipset.\n");
use_fast_uart();
hci_set_chipset(btstack_chipset_csr_instance());
break;
@ -146,8 +163,9 @@ static void local_version_information_callback(uint8_t * packet){
#endif
break;
case COMPANY_ID_BROADCOM_CORPORATION:
printf("Broadcom chipset. Not supported yet\n");
// hci_set_chipset(btstack_chipset_bcm_instance());
printf("Broadcom - using BCM driver.\n");
hci_set_chipset(btstack_chipset_bcm_instance());
is_bcm = 1;
break;
case COMPANY_ID_ST_MICROELECTRONICS:
printf("ST Microelectronics - using STLC2500d driver.\n");
@ -159,7 +177,7 @@ static void local_version_information_callback(uint8_t * packet){
hci_set_chipset(btstack_chipset_em9301_instance());
break;
case COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA:
printf("Nordic Semicondutor ASA nRF5 chipset.\n");
printf("Nordic Semiconductor nRF5 chipset.\n");
break;
default:
printf("Unknown manufacturer / manufacturer not supported yet.\n");
@ -177,7 +195,7 @@ int main(int argc, const char * argv[]){
hci_dump_open("/tmp/hci_dump.pklg", HCI_DUMP_PACKETLOGGER);
// pick serial port
config.device_name = "/dev/tty.usbserial-A900K0VK";
config.device_name = "/dev/tty.usbserial-A9GN71D5";
// init HCI
const btstack_uart_block_t * uart_driver = btstack_uart_block_posix_instance();