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