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 // minimal CSR init script to configure PSKEYs and activate them 59 static const uint8_t init_script[] = { 60 61 // Set ANA_Freq to 26MHz 62 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xfe, 0x01, 0x01, 0x00, 0x00, 0x00, 0x90, 0x65, 63 64 // Set HCI_NOP_DISABLE 65 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xf2, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 66 67 #ifdef ENABLE_SCO_OVER_HCI 68 // Set HOSTIO_MAP_SCO_PCM to 0 69 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xab, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 70 // Set HOSTIO_MAP_SCO_CODEC to 0 71 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x00, 0x00, 0x09, 0x00, 0x03, 0x00, 0x03, 0x70, 0x00, 0x00, 0xb0, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 72 // Set ENABLE_SCO_STREAMS to 0 73 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xc9, 0x22, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 74 #endif 75 76 // Enable RTS/CTS for BCSP (0806 -> 080e) 77 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xbf, 0x01, 0x01, 0x00, 0x00, 0x00, 0x0e, 0x08, 78 79 // Enable RTS/CTS for H5 (1806 -> 180e, even parity still on) 80 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xc1, 0x01, 0x01, 0x00, 0x00, 0x00, 0x0e, 0x18, 81 82 // Set UART baudrate to 115200 83 0x01, 0x00, 0xFC, 0x15, 0xc2, 0x02, 0x00, 0x0a, 0x00, 0x02, 0x00, 0x03, 0x70, 0x00, 0x00, 0xea, 0x01, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0xc2, 84 85 // WarmReset 86 0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x03, 0x0e, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 87 }; 88 static const uint16_t init_script_size = sizeof(init_script); 89 90 // 91 static uint32_t init_script_offset = 0; 92 static hci_transport_config_uart_t * hci_transport_config_uart = NULL; 93 94 static void chipset_init(const void * config){ 95 init_script_offset = 0; 96 hci_transport_config_uart = NULL; 97 // check for hci_transport_config_uart_t 98 if (!config) return; 99 if (((hci_transport_config_t*)config)->type != HCI_TRANSPORT_CONFIG_UART) return; 100 hci_transport_config_uart = (hci_transport_config_uart_t*) config; 101 } 102 103 // set requested baud rate 104 static void update_init_script_command(uint8_t *hci_cmd_buffer){ 105 uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10); 106 if (varid != 0x7003) return; 107 uint16_t key = little_endian_read_16(hci_cmd_buffer, 14); 108 log_info("csr: pskey 0x%04x", key); 109 if (key != 0x01ea) return; 110 111 // check for baud rate 112 if (!hci_transport_config_uart) return; 113 uint32_t baudrate = hci_transport_config_uart->baudrate_main; 114 if (baudrate == 0){ 115 baudrate = hci_transport_config_uart->baudrate_init; 116 } 117 // uint32_t is stored as 2 x uint16_t with most important 16 bits first 118 little_endian_store_16(hci_cmd_buffer, 20, baudrate >> 16); 119 little_endian_store_16(hci_cmd_buffer, 22, baudrate & 0xffff); 120 } 121 122 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){ 123 124 if (init_script_offset >= init_script_size) { 125 return BTSTACK_CHIPSET_DONE; 126 } 127 128 // init script is stored with the HCI Command Packet Type 129 init_script_offset++; 130 // copy command header 131 memcpy(&hci_cmd_buffer[0], (uint8_t *) &init_script[init_script_offset], 3); 132 init_script_offset += 3; 133 int payload_len = hci_cmd_buffer[2]; 134 // copy command payload 135 memcpy(&hci_cmd_buffer[3], (uint8_t *) &init_script[init_script_offset], payload_len); 136 137 // support for on-the-fly configuration updates 138 update_init_script_command(hci_cmd_buffer); 139 140 init_script_offset += payload_len; 141 142 // support for warm boot command 143 uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10); 144 if (varid == 0x4002){ 145 return BTSTACK_CHIPSET_WARMSTART_REQUIRED; 146 } 147 148 return BTSTACK_CHIPSET_VALID_COMMAND; 149 } 150 151 152 static const btstack_chipset_t btstack_chipset_bcm = { 153 "CSR", 154 chipset_init, 155 chipset_next_command, 156 NULL, // chipset_set_baudrate_command, 157 NULL, // chipset_set_bd_addr_command not supported or implemented 158 }; 159 160 // MARK: public API 161 const btstack_chipset_t * btstack_chipset_csr_instance(void){ 162 return &btstack_chipset_bcm; 163 } 164