1 /* 2 * Copyright (C) 2014 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 #define BTSTACK_FILE__ "btstack_chipset_csr.c" 39 40 /* 41 * btstack_chipset_csr.c 42 * 43 * Adapter to use CSR-based chipsets with BTstack 44 * SCO over HCI doesn't work over H4 connection and BTM805 module from Microchip Bluetooth Audio Developer Kit (CSR8811) 45 */ 46 47 #include "btstack_chipset_csr.h" 48 49 #include <stddef.h> /* NULL */ 50 #include <stdio.h> 51 #include <string.h> /* memcpy */ 52 53 #include "btstack_control.h" 54 #include "btstack_debug.h" 55 #include "btstack_util.h" 56 #include "hci_transport.h" 57 58 enum update_result { 59 OK, 60 SKIP, 61 }; 62 63 // minimal CSR init script to configure PSKEYs and activate them. It uses store 0x0008 = psram. 64 static const uint8_t init_script[] = { 65 66 // 0x01fe: Set ANA_Freq to 26MHz 67 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xfe, 0x01, 0x01, 0x00, 0x08, 0x00, 0x90, 0x65, 68 69 // 0x00f2: Set HCI_NOP_DISABLE 70 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xf2, 0x00, 0x01, 0x00, 0x08, 0x00, 0x01, 0x00, 71 72 #ifdef ENABLE_SCO_OVER_HCI 73 // 0x01ab: Set HOSTIO_MAP_SCO_PCM to 0 74 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xab, 0x01, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 75 // 0x01b0: Set HOSTIO_MAP_SCO_CODEC to 0 76 0x00, 0xFC, 0x13, 0xc2, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x03, 0x70, 0x00, 0x00, 0xb0, 0x01, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 77 // 0x22c9: Set ENABLE_SCO_STREAMS to 0 78 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xc9, 0x22, 0x01, 0x00, 0x08, 0x00, 0x00, 0x00, 79 #endif 80 81 // 0x01bf: Enable RTS/CTS for BCSP (0806 -> 080e) 82 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xbf, 0x01, 0x01, 0x00, 0x08, 0x00, 0x0e, 0x08, 83 84 // 0x01ea: Set UART baudrate to 115200 85 0x00, 0xFC, 0x15, 0xc2, 0x02, 0x00, 0x0a, 0x00, 0x02, 0x00, 0x03, 0x70, 0x00, 0x00, 0xea, 0x01, 0x02, 0x00, 0x08, 0x00, 0x01, 0x00, 0x00, 0xc2, 86 87 // 0x0001: Set Bluetooth address 88 0x00, 0xFC, 0x19, 0xc2, 0x02, 0x00, 0x0A, 0x00, 0x03, 0x00, 0x03, 0x70, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x08, 0x00, 0xf3, 0x00, 0xf5, 0xf4, 0xf2, 0x00, 0xf2, 0xf1, 89 90 // WarmReset 91 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x03, 0x0e, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 92 }; 93 static const uint16_t init_script_size = sizeof(init_script); 94 95 // 96 static uint32_t init_script_offset = 0; 97 static hci_transport_config_uart_t * hci_transport_config_uart = NULL; 98 static int set_bd_addr; 99 static bd_addr_t bd_addr; 100 101 static void chipset_init(const void * config){ 102 init_script_offset = 0; 103 hci_transport_config_uart = NULL; 104 // check for hci_transport_config_uart_t 105 if (!config) return; 106 if (((hci_transport_config_t*)config)->type != HCI_TRANSPORT_CONFIG_UART) return; 107 hci_transport_config_uart = (hci_transport_config_uart_t*) config; 108 } 109 110 // set requested baud rate 111 // @return 0 = OK, 1 = SKIP, 2 = .. 112 static enum update_result update_init_script_command(uint8_t *hci_cmd_buffer){ 113 uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10); 114 if (varid != 0x7003) return OK; 115 uint16_t key = little_endian_read_16(hci_cmd_buffer, 14); 116 log_info("csr: pskey 0x%04x", key); 117 switch (key){ 118 case 0x001: 119 // set bd addr command 120 if (!set_bd_addr) return SKIP; 121 hci_cmd_buffer[20] = bd_addr[3]; 122 hci_cmd_buffer[22] = bd_addr[5]; 123 hci_cmd_buffer[23] = bd_addr[4]; 124 hci_cmd_buffer[24] = bd_addr[2]; 125 hci_cmd_buffer[26] = bd_addr[1]; 126 hci_cmd_buffer[27] = bd_addr[0]; 127 return OK; 128 case 0x01ea: 129 // baud rate command 130 if (!hci_transport_config_uart) return OK; 131 uint32_t baudrate = hci_transport_config_uart->baudrate_main; 132 if (baudrate == 0){ 133 baudrate = hci_transport_config_uart->baudrate_init; 134 } 135 // uint32_t is stored as 2 x uint16_t with most important 16 bits first 136 little_endian_store_16(hci_cmd_buffer, 20, baudrate >> 16); 137 little_endian_store_16(hci_cmd_buffer, 22, baudrate & 0xffff); 138 break; 139 default: 140 break; 141 } 142 return OK; 143 } 144 145 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 146 147 while (true){ 148 149 if (init_script_offset >= init_script_size) { 150 return BTSTACK_CHIPSET_DONE; 151 } 152 153 // copy command header 154 memcpy(&hci_cmd_buffer[0], (uint8_t *) &init_script[init_script_offset], 3); 155 init_script_offset += 3; 156 int payload_len = hci_cmd_buffer[2]; 157 // copy command payload 158 memcpy(&hci_cmd_buffer[3], (uint8_t *) &init_script[init_script_offset], payload_len); 159 160 // support for on-the-fly configuration updates 161 enum update_result res = update_init_script_command(hci_cmd_buffer); 162 init_script_offset += payload_len; 163 164 // support for skipping commands 165 if (res == SKIP) continue; 166 167 // support for warm boot command 168 uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10); 169 if (varid == 0x4002){ 170 return BTSTACK_CHIPSET_WARMSTART_REQUIRED; 171 } 172 173 return BTSTACK_CHIPSET_VALID_COMMAND; 174 } 175 } 176 177 178 static const btstack_chipset_t btstack_chipset_bcm = { 179 "CSR", 180 chipset_init, 181 chipset_next_command, 182 NULL, // chipset_set_baudrate_command, 183 NULL, // chipset_set_bd_addr_command not supported or implemented 184 }; 185 186 // MARK: public API 187 const btstack_chipset_t * btstack_chipset_csr_instance(void){ 188 return &btstack_chipset_bcm; 189 } 190 191 /** 192 * Set BD ADDR before HCI POWER_ON 193 * @param bd_addr 194 */ 195 void btstack_chipset_csr_set_bd_addr(bd_addr_t new_bd_addr){ 196 set_bd_addr = 1; 197 memcpy(bd_addr, new_bd_addr, 6); 198 } 199