xref: /btstack/chipset/csr/btstack_chipset_csr.c (revision ca93b47d3c0a65b91d7a5dc8586f4c82a3b3e1c2)
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     //  Set UART baudrate to 115200
80     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,
81 
82     //  WarmReset
83     0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x03, 0x0e, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 };
85 static const uint16_t init_script_size = sizeof(init_script);
86 
87 //
88 static uint32_t init_script_offset  = 0;
89 static hci_transport_config_uart_t * hci_transport_config_uart = NULL;
90 
91 static void chipset_init(const void * config){
92     init_script_offset = 0;
93     hci_transport_config_uart = NULL;
94     // check for hci_transport_config_uart_t
95     if (!config) return;
96     if (((hci_transport_config_t*)config)->type != HCI_TRANSPORT_CONFIG_UART) return;
97     hci_transport_config_uart = (hci_transport_config_uart_t*) config;
98 }
99 
100 // set requested baud rate
101 static void update_init_script_command(uint8_t *hci_cmd_buffer){
102     uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10);
103     if (varid != 0x7003) return;
104     uint16_t key = little_endian_read_16(hci_cmd_buffer, 14);
105     log_info("csr: pskey 0x%04x", key);
106     if (key != 0x01ea) return;
107 
108     // check for baud rate
109     if (!hci_transport_config_uart) return;
110     uint32_t baudrate = hci_transport_config_uart->baudrate_main;
111     if (baudrate == 0){
112         baudrate = hci_transport_config_uart->baudrate_init;
113     }
114     // uint32_t is stored as 2 x uint16_t with most important 16 bits first
115     little_endian_store_16(hci_cmd_buffer, 20, baudrate >> 16);
116     little_endian_store_16(hci_cmd_buffer, 22, baudrate &  0xffff);
117 }
118 
119 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
120 
121     if (init_script_offset >= init_script_size) {
122         return BTSTACK_CHIPSET_DONE;
123     }
124 
125     // init script is stored with the HCI Command Packet Type
126     init_script_offset++;
127     // copy command header
128     memcpy(&hci_cmd_buffer[0], (uint8_t *) &init_script[init_script_offset], 3);
129     init_script_offset += 3;
130     int payload_len = hci_cmd_buffer[2];
131     // copy command payload
132     memcpy(&hci_cmd_buffer[3], (uint8_t *) &init_script[init_script_offset], payload_len);
133 
134     // support for on-the-fly configuration updates
135     update_init_script_command(hci_cmd_buffer);
136 
137     init_script_offset += payload_len;
138 
139     // support for warm boot command
140     uint16_t varid = little_endian_read_16(hci_cmd_buffer, 10);
141     if (varid == 0x4002){
142         return BTSTACK_CHIPSET_WARMSTART_REQUIRED;
143     }
144 
145     return BTSTACK_CHIPSET_VALID_COMMAND;
146 }
147 
148 
149 static const btstack_chipset_t btstack_chipset_bcm = {
150     "CSR",
151     chipset_init,
152     chipset_next_command,
153     NULL, // chipset_set_baudrate_command,
154     NULL, // chipset_set_bd_addr_command not supported or implemented
155 };
156 
157 // MARK: public API
158 const btstack_chipset_t * btstack_chipset_csr_instance(void){
159     return &btstack_chipset_bcm;
160 }
161