1 /*
2 * Copyright (C) 2017 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 BLUEKITCHEN
24 * GMBH 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_bcm_download_firmware.c"
39
40 // download firmware implementation
41 // requires hci_dump
42 // supports higher baudrate for patch upload
43
44 #include <string.h>
45 #include <stdio.h>
46
47 #ifdef _MSC_VER
48 // map sleep() to Sleep()
49 #include "Windows.h"
sleep(unsigned int seconds)50 unsigned int sleep(unsigned int seconds){
51 _Sleep(seconds * 1000);
52 return 0;
53 }
54 #else
55 #include <unistd.h>
56 #endif
57
58 #include "hci_dump.h"
59 #include "btstack_chipset_bcm.h"
60 #include "btstack_chipset_bcm_download_firmware.h"
61 #include "bluetooth.h"
62 #include "bluetooth_company_id.h"
63 #include "btstack_debug.h"
64 #include "btstack_chipset.h"
65
66 static void bcm_send_next_init_script_command(void);
67 static void bcm_w4_command_complete(void);
68
69 static const btstack_uart_block_t * uart_driver;
70 static const btstack_chipset_t * chipset;
71
72 static uint8_t response_buffer[260];
73 static uint16_t response_buffer_len;
74 static uint8_t command_buffer[260];
75
76 static const int hci_command_complete_len = 7;
77 static const int hci_command_complete_read_local_version = 15;
78 static const uint8_t hci_read_local_version_cmd[] = { 0x01, 0x10, 0x00};
79 static const uint8_t hci_reset_cmd[] = { 0x03, 0x0c, 0x00 };
80 static const uint8_t hci_command_complete_reset[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00};
81
82 static void (*download_complete)(int result);
83
84 static uint32_t baudrate;
85
bcm_hci_dump_event(void)86 static inline void bcm_hci_dump_event(void){
87 uint16_t event_len = 2 + response_buffer[2];
88 hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], event_len);
89 }
90
bcm_send_prepared_command(void)91 static void bcm_send_prepared_command(void){
92 command_buffer[0] = 1;
93 uint16_t command_len = 3 + command_buffer[3];
94 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], command_len);
95 uart_driver->send_block(command_buffer, command_len + 1);
96 }
97
98 // select controller based on { manufacturer / lmp_subversion }
bcm_detect_controller(uint16_t manufacturer,uint16_t lmp_subversion)99 static void bcm_detect_controller(uint16_t manufacturer,
100 uint16_t lmp_subversion) {
101 const char * device_name = NULL;
102 switch (manufacturer){
103 case BLUETOOTH_COMPANY_ID_INFINEON_TECHNOLOGIES_AG:
104 switch (lmp_subversion){
105 case 0x2257:
106 // CYW5557x
107 device_name = "CYW55560A1";
108 break;
109 case 0x2220:
110 // CYW5551x
111 device_name = "CYW55500A1";
112 break;
113 default:
114 break;
115 }
116 break;
117 default:
118 break;
119 }
120 if (device_name == NULL){
121 printf("Unknown device, please update bcm_detect_controller()\n");
122 printf("in btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c\n");
123 } else {
124 printf("Controller: %s\n", device_name);
125 btstack_chipset_bcm_set_device_name(device_name);
126 }
127 }
128
129 // Send / Receive HCI Read Local Version Information
130
bcm_receive_command_command_complete_read_local_version(void)131 static void bcm_receive_command_command_complete_read_local_version(void){
132 const uint8_t * packet = &response_buffer[1];
133 printf("ROM version information:\n");
134 uint16_t hci_version = packet[6];
135 uint16_t hci_revision = little_endian_read_16(packet, 7);
136 uint16_t lmp_version = packet[9];
137 uint16_t manufacturer = little_endian_read_16(packet, 10);
138 uint16_t lmp_subversion = little_endian_read_16(packet, 12);
139 printf("- HCI Version 0x%04x\n", hci_version);
140 printf("- HCI Revision 0x%04x\n", hci_revision);
141 printf("- LMP Version 0x%04x\n", lmp_version);
142 printf("- LMP Subversion 0x%04x\n", lmp_subversion);
143 printf("- Manufacturer 0x%04x\n", manufacturer);
144
145 bcm_detect_controller(manufacturer, lmp_subversion);
146
147 bcm_w4_command_complete();
148 }
149
bcm_send_read_local_version(void)150 static void bcm_send_read_local_version(void){
151 log_info("bcm: send HCI Read Local Version Information");
152 uart_driver->set_block_received(&bcm_receive_command_command_complete_read_local_version);
153 uart_driver->receive_block(response_buffer, hci_command_complete_read_local_version);
154 memcpy(&command_buffer[1], hci_read_local_version_cmd, sizeof(hci_read_local_version_cmd));
155 bcm_send_prepared_command();
156 }
157
158 // Send / Receive HCI Reset
159
160 // Although the Controller just has been reset by the user, there might still be HCI data in the UART driver
161 // which we'll ignore in the receive function
162
bcm_receive_command_complete_reset(void)163 static void bcm_receive_command_complete_reset(void){
164 response_buffer_len++;
165 if (response_buffer_len == hci_command_complete_len){
166 // try to match command complete for HCI Reset
167 if (memcmp(response_buffer, hci_command_complete_reset, hci_command_complete_len) == 0){
168 bcm_hci_dump_event();
169 bcm_send_read_local_version();
170 return;
171 }
172 memmove(&response_buffer[0], &response_buffer[1], response_buffer_len - 1);
173 response_buffer_len--;
174 }
175 uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
176 }
177
bcm_send_hci_reset(void)178 static void bcm_send_hci_reset(void){
179 log_info("bcm: send HCI Reset");
180 response_buffer_len = 0;
181 uart_driver->set_block_received(&bcm_receive_command_complete_reset);
182 uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
183 memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
184 bcm_send_prepared_command();
185 }
186
187 // Other
188
189 #if 0
190 static void bcm_set_local_baudrate(void){
191 bcm_hci_dump_event();
192 uart_driver->set_baudrate(baudrate);
193 uart_driver->set_block_received(&bcm_w4_command_complete);
194 bcm_send_next_init_script_command();
195 }
196 static void bcm_send_hci_baudrate(void){
197 bcm_hci_dump_event();
198 chipset->set_baudrate_command(baudrate, &command_buffer[1]);
199 uart_driver->set_block_received(&bcm_set_local_baudrate);
200 uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
201 log_info("bcm: send baud rate command - %u", baudrate);
202 bcm_send_prepared_command();
203 }
204 #endif
205
bcm_w4_command_complete(void)206 static void bcm_w4_command_complete(void){
207 bcm_hci_dump_event();
208 uart_driver->set_block_received(&bcm_w4_command_complete);
209 bcm_send_next_init_script_command();
210 }
211
bcm_send_next_init_script_command(void)212 static void bcm_send_next_init_script_command(void){
213 int res = chipset->next_command(&command_buffer[1]);
214 switch (res){
215 case BTSTACK_CHIPSET_VALID_COMMAND:
216 uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
217 bcm_send_prepared_command();
218 break;
219 case BTSTACK_CHIPSET_DONE:
220 log_info("bcm: init script done");
221 // disable init script for main startup
222 btstack_chipset_bcm_enable_init_script(0);
223 // reset baudrate to default
224 uart_driver->set_baudrate(115200);
225 // notify main
226 download_complete(0);
227 break;
228 default:
229 break;
230 }
231 }
232
233 /**
234 * @brief Download firmware via uart_driver
235 * @param uart_driver -- already initialized
236 * @param done callback. 0 = Success
237 */
238
btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver,int baudrate_upload,void (* done)(int result))239 void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
240 //
241 uart_driver = the_uart_driver;
242 chipset = btstack_chipset_bcm_instance();
243 baudrate = baudrate_upload;
244 download_complete = done;
245 btstack_chipset_bcm_enable_init_script(1);
246
247 int res = uart_driver->open();
248 if (res) {
249 log_error("uart_block init failed %u", res);
250 download_complete(res);
251 return;
252 }
253
254 // Reset with CTS asserted (low)
255 printf("Please reset Bluetooth Controller, e.g. via RESET button. Firmware download starts in:\n");
256 uint8_t i;
257 for (i = 3; i > 0; i--){
258 printf("%u\n", i);
259 sleep(1);
260 }
261 printf("Firmware download started\n");
262
263 bcm_send_hci_reset();
264 }
265
btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver,int baudrate_upload,void (* done)(int result))266 void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) {
267 btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done);
268 }
269