1 /* 2 * Copyright (C) 2009-2012 by Matthias Ringwald 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 MATTHIAS RINGWALD 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 [email protected] 34 * 35 */ 36 37 /* 38 * bt_control_bcm.c 39 * 40 * Adapter to use Broadcom-based chipsets with BTstack 41 */ 42 43 44 #include "btstack_config.h" 45 46 #include <stddef.h> /* NULL */ 47 #include <stdio.h> 48 #include <string.h> /* memcpy */ 49 50 #include "btstack_control.h" 51 #include "btstack_debug.h" 52 53 // actual init script provided by separate bt_firmware_image.c from WICED SDK 54 extern const uint8_t brcm_patchram_buf[]; 55 extern const int brcm_patch_ram_length; 56 extern const char brcm_patch_version[]; 57 58 // 59 static uint32_t init_script_offset; 60 static int send_download_command; 61 62 static void chipset_init(void * config){ 63 log_info("Broadcom init script %s, len %u", brcm_patch_version, brcm_patch_ram_length); 64 init_script_offset = 0; 65 send_download_command = 1; 66 } 67 68 // @note: Broadcom chips require higher UART clock for baud rate > 3000000 -> limit baud rate in hci.c 69 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 70 hci_cmd_buffer[0] = 0x18; 71 hci_cmd_buffer[1] = 0xfc; 72 hci_cmd_buffer[2] = 0x06; 73 hci_cmd_buffer[3] = 0x00; 74 hci_cmd_buffer[4] = 0x00; 75 bt_store_32(hci_cmd_buffer, 5, baudrate); 76 } 77 78 // @note: bd addr has to be set after sending init script (it might just get re-set) 79 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 80 hci_cmd_buffer[0] = 0x01; 81 hci_cmd_buffer[1] = 0xfc; 82 hci_cmd_buffer[2] = 0x06; 83 bt_flip_addr(&hci_cmd_buffer[3], addr); 84 } 85 86 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 87 // send download firmware command 88 if (send_download_command){ 89 send_download_command = 0; 90 hci_cmd_buffer[0] = 0x2e; 91 hci_cmd_buffer[1] = 0xfc; 92 hci_cmd_buffer[2] = 0x00; 93 return BTSTACK_CHIPSET_VALID_COMMAND; 94 } 95 96 if (init_script_offset >= brcm_patch_ram_length) { 97 return BTSTACK_CHIPSET_DONE; 98 } 99 100 int cmd_len = 3 + brcm_patchram_buf[init_script_offset+2]; 101 memcpy(&hci_cmd_buffer[0], &brcm_patchram_buf[init_script_offset], cmd_len); 102 init_script_offset += cmd_len; 103 return BTSTACK_CHIPSET_VALID_COMMAND; 104 } 105 106 107 static const btstack_chipset_t btstack_chipset_bcm = { 108 "BCM", 109 chipset_init, 110 chipset_next_command, 111 chipset_set_baudrate_command, 112 chipset_set_bd_addr_command, 113 }; 114 115 // MARK: public API 116 const btstack_chipset_t * btstack_chipset_bcm_instance(void){ 117 return &btstack_chipset_bcm; 118 } 119