xref: /btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c (revision 25d5427a345779b159b63c0d8b197d2dee40cf37)
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"
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 
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 
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 }
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                 default:
110                     break;
111             }
112             break;
113         default:
114             break;
115     }
116     if (device_name == NULL){
117         printf("Unknown device, please update bcm_detect_controller()\n");
118         printf("in btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c\n");
119     } else {
120         printf("Controller: %s\n", device_name);
121         btstack_chipset_bcm_set_device_name(device_name);
122     }
123 }
124 
125 // Send / Receive HCI Read Local Version Information
126 
127 static void bcm_receive_command_command_complete_read_local_version(void){
128     const uint8_t * packet = &response_buffer[1];
129     printf("ROM version information:\n");
130     uint16_t hci_version    = packet[6];
131     uint16_t hci_revision   = little_endian_read_16(packet, 7);
132     uint16_t lmp_version    = packet[9];
133     uint16_t manufacturer   = little_endian_read_16(packet, 10);
134     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
135     printf("- HCI Version    0x%04x\n", hci_version);
136     printf("- HCI Revision   0x%04x\n", hci_revision);
137     printf("- LMP Version    0x%04x\n", lmp_version);
138     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
139     printf("- Manufacturer 0x%04x\n", manufacturer);
140 
141     bcm_detect_controller(manufacturer, lmp_subversion);
142 
143     bcm_w4_command_complete();
144 }
145 
146 static void bcm_send_read_local_version(void){
147     log_info("bcm: send HCI Read Local Version Information");
148     uart_driver->set_block_received(&bcm_receive_command_command_complete_read_local_version);
149     uart_driver->receive_block(response_buffer, hci_command_complete_read_local_version);
150     memcpy(&command_buffer[1], hci_read_local_version_cmd, sizeof(hci_read_local_version_cmd));
151     bcm_send_prepared_command();
152 }
153 
154 // Send / Receive HCI Reset
155 
156 // Although the Controller just has been reset by the user, there might still be HCI data in the UART driver
157 // which we'll ignore in the receive function
158 
159 static void bcm_receive_command_complete_reset(void){
160     response_buffer_len++;
161     if (response_buffer_len == hci_command_complete_len){
162         // try to match command complete for HCI Reset
163         if (memcmp(response_buffer, hci_command_complete_reset, hci_command_complete_len) == 0){
164             bcm_hci_dump_event();
165             bcm_send_read_local_version();
166             return;
167         }
168         memmove(&response_buffer[0], &response_buffer[1], response_buffer_len - 1);
169         response_buffer_len--;
170     }
171     uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
172 }
173 
174 static void bcm_send_hci_reset(void){
175     log_info("bcm: send HCI Reset");
176     response_buffer_len = 0;
177     uart_driver->set_block_received(&bcm_receive_command_complete_reset);
178     uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
179     memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
180     bcm_send_prepared_command();
181 }
182 
183 // Other
184 
185 #if 0
186 static void bcm_set_local_baudrate(void){
187     bcm_hci_dump_event();
188     uart_driver->set_baudrate(baudrate);
189     uart_driver->set_block_received(&bcm_w4_command_complete);
190     bcm_send_next_init_script_command();
191 }
192 static void bcm_send_hci_baudrate(void){
193     bcm_hci_dump_event();
194     chipset->set_baudrate_command(baudrate, &command_buffer[1]);
195     uart_driver->set_block_received(&bcm_set_local_baudrate);
196     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
197     log_info("bcm: send baud rate command - %u", baudrate);
198     bcm_send_prepared_command();
199 }
200 #endif
201 
202 static void bcm_w4_command_complete(void){
203     bcm_hci_dump_event();
204     uart_driver->set_block_received(&bcm_w4_command_complete);
205     bcm_send_next_init_script_command();
206 }
207 
208 static void bcm_send_next_init_script_command(void){
209     int res = chipset->next_command(&command_buffer[1]);
210     switch (res){
211         case BTSTACK_CHIPSET_VALID_COMMAND:
212             uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
213             bcm_send_prepared_command();
214             break;
215         case BTSTACK_CHIPSET_DONE:
216             log_info("bcm: init script done");
217             // disable init script for main startup
218             btstack_chipset_bcm_enable_init_script(0);
219             // reset baudrate to default
220             uart_driver->set_baudrate(115200);
221             // notify main
222             download_complete(0);
223             break;
224         default:
225             break;
226     }
227 }
228 
229 /**
230  * @brief Download firmware via uart_driver
231  * @param uart_driver -- already initialized
232  * @param done callback. 0 = Success
233  */
234 
235 void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
236     //
237     uart_driver = the_uart_driver;
238     chipset     = btstack_chipset_bcm_instance();
239     baudrate    = baudrate_upload;
240     download_complete = done;
241     btstack_chipset_bcm_enable_init_script(1);
242 
243     int res = uart_driver->open();
244     if (res) {
245         log_error("uart_block init failed %u", res);
246         download_complete(res);
247         return;
248     }
249 
250     // Reset with CTS asserted (low)
251     printf("Please reset Bluetooth Controller, e.g. via RESET button. Firmware download starts in:\n");
252     uint8_t i;
253     for (i = 3; i > 0; i--){
254         printf("%u\n", i);
255         sleep(1);
256     }
257     printf("Firmware download started\n");
258 
259     bcm_send_hci_reset();
260 }
261 
262 void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) {
263     btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done);
264 }
265