xref: /btstack/chipset/csr/btstack_chipset_csr.c (revision faa6c1f625806f77d213afda856e452e3e27fee8)
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  *  bt_control_csr.c
40  *
41  *  Adapter to use CSR-based chipsets with BTstack
42  *
43  */
44 
45 #include "bt_control_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     //  Set ANA_Freq PSKEY to 26MHz
59     0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xfe, 0x01, 0x01, 0x00, 0x00, 0x00, 0x90, 0x65,
60     //  Set HCI_NOP_DISABLE
61     0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x01, 0x00, 0x03, 0x70, 0x00, 0x00, 0xf2, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00,
62     //  Set UART baudrate to 115200
63     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,
64     //  WarmReset
65     0x01, 0x00, 0xFC, 0x13, 0xc2, 0x02, 0x00, 0x09, 0x00, 0x03, 0x0e, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 };
67 static const uint16_t init_script_size = sizeof(init_script);
68 
69 //
70 static uint32_t init_script_offset  = 0;
71 static hci_transport_config_uart_t * hci_transport_config_uart = NULL;
72 
73 static void chipset_init(void * config){
74     init_script_offset = 0;
75     hci_transport_config_uart = NULL;
76     // check for hci_transport_config_uart_t
77     if (!config) return;
78     if (((hci_transport_config_t*)config)->type != HCI_TRANSPORT_CONFIG_UART) return;
79     hci_transport_config_uart = (hci_transport_config_uart_t*) config;
80 }
81 
82 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
83 }
84 
85 // set requested baud rate
86 static void update_init_script_command(uint8_t *hci_cmd_buffer){
87     uint16_t varid = READ_BT_16(hci_cmd_buffer, 10);
88     if (varid != 0x7003) return;
89     uint16_t key = READ_BT_16(hci_cmd_buffer, 14);
90     if (key != 0x01ea) return;
91 
92     // check for baud rate
93     if (!hci_transport_config_uart) return;
94     uint32_t baudrate = hci_transport_config_uart->baudrate_main;
95     if (baudrate == 0){
96         baudrate = hci_transport_config_uart->baudrate_init;
97     }
98     // uint32_t is stored as 2 x uint16_t with most important 16 bits first
99     bt_store_16(hci_cmd_buffer, 20, baudrate >> 16);
100     bt_store_16(hci_cmd_buffer, 22, baudrate &  0xffff);
101 }
102 
103 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
104 
105     if (init_script_offset >= init_script_size) {
106         return BTSTACK_CHIPSET_DONE;
107     }
108 
109     // init script is stored with the HCI Command Packet Type
110     init_script_offset++;
111     // copy command header
112     memcpy(&hci_cmd_buffer[0], (uint8_t *) &init_script[init_script_offset], 3);
113     init_script_offset += 3;
114     int payload_len = hci_cmd_buffer[2];
115     // copy command payload
116     memcpy(&hci_cmd_buffer[3], (uint8_t *) &init_script[init_script_offset], payload_len);
117 
118     // support for on-the-fly configuration updates
119     update_init_script_command(hci_cmd_buffer);
120 
121     init_script_offset += payload_len;
122 
123     // support for warm boot command
124     uint16_t varid = READ_BT_16(hci_cmd_buffer, 10);
125     log_info("csr: varid 0x%04x", varid);
126     if (varid == 0x4002){
127         return BTSTACK_CHIPSET_WARMSTART_REQUIRED;
128     }
129 
130     return BTSTACK_CHIPSET_VALID_COMMAND;
131 }
132 
133 
134 static const btstack_chipset_t btstack_chipset_bcm = {
135     "BCM",
136     chipset_init,
137     chipset_next_command,
138     chipset_set_baudrate_command,
139     NULL, // chipset_set_bd_addr_command not supported or implemented
140 };
141 
142 // MARK: public API
143 const btstack_chipset_t * btstack_chipset_csr_instance(void){
144     return &btstack_chipset_bcm;
145 }
146