xref: /btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c (revision e957bdcbde2a6d5e7c299e917526a07144b00003)
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 <printf.h>
46 #include <unistd.h>
47 
48 #include "hci_dump.h"
49 #include "btstack_chipset_bcm.h"
50 #include "btstack_chipset_bcm_download_firmware.h"
51 #include "bluetooth.h"
52 #include "btstack_debug.h"
53 #include "btstack_chipset.h"
54 
55 static void bcm_send_hci_baudrate(void);
56 static void bcm_send_next_init_script_command(void);
57 static void bcm_set_local_baudrate(void);
58 static void bcm_w4_command_complete(void);
59 
60 static const btstack_uart_block_t * uart_driver;
61 static const btstack_chipset_t * chipset;
62 
63 static uint8_t response_buffer[260];
64 static uint16_t response_buffer_len;
65 static uint8_t command_buffer[260];
66 
67 static const int hci_command_complete_len = 7;
68 static const uint8_t hci_reset_cmd[] = { 0x03, 0x0c, 0x00 };
69 static const uint8_t hci_command_complete_reset[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00};
70 
71 static void (*download_complete)(int result);
72 static uint32_t baudrate;
73 
74 static void bcm_send_prepared_command(void){
75     int size = 1 + 3 + command_buffer[3];
76     command_buffer[0] = 1;
77     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], size-1);
78     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
79     uart_driver->send_block(command_buffer, size);
80 }
81 
82 // Although the Controller just has been reset by the user, there might still be HCI data in the UART driver
83 // which we'll ignore in the receive function
84 
85 static void bcm_receive_command_complete_reset(void){
86     response_buffer_len++;
87     if (response_buffer_len == hci_command_complete_len){
88         // try to match command complete for HCI Reset
89         if (memcmp(response_buffer, hci_command_complete_reset, hci_command_complete_len) == 0){
90             bcm_w4_command_complete();
91             return;
92         }
93         memmove(&response_buffer[0], &response_buffer[1], response_buffer_len - 1);
94         response_buffer_len--;
95     }
96     uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
97 }
98 
99 static void bcm_send_hci_reset(void){
100     log_info("bcm: send HCI Reset");
101     response_buffer_len = 0;
102     uart_driver->set_block_received(&bcm_receive_command_complete_reset);
103     uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
104 
105     command_buffer[0] = 1;
106     memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
107     uint16_t size = sizeof(hci_reset_cmd);
108     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], size);
109     uart_driver->send_block(command_buffer, size + 1);
110 }
111 
112 static void bcm_send_hci_baudrate(void){
113     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
114     chipset->set_baudrate_command(baudrate, &command_buffer[1]);
115     uart_driver->set_block_received(&bcm_set_local_baudrate);
116     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
117     log_info("bcm: send baud rate command - %u", baudrate);
118     bcm_send_prepared_command();
119 }
120 
121 static void bcm_set_local_baudrate(void){
122     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
123     uart_driver->set_baudrate(baudrate);
124     uart_driver->set_block_received(&bcm_w4_command_complete);
125     bcm_send_next_init_script_command();
126 }
127 
128 static void bcm_w4_command_complete(void){
129     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
130     uart_driver->set_block_received(&bcm_w4_command_complete);
131     bcm_send_next_init_script_command();
132 }
133 
134 static void bcm_send_next_init_script_command(void){
135     int res = chipset->next_command(&command_buffer[1]);
136     switch (res){
137         case BTSTACK_CHIPSET_VALID_COMMAND:
138             bcm_send_prepared_command();
139             break;
140         case BTSTACK_CHIPSET_DONE:
141             log_info("bcm: init script done");
142             // disable init script for main startup
143             btstack_chipset_bcm_enable_init_script(0);
144             // reset baudrate to default
145             uart_driver->set_baudrate(115200);
146             // notify main
147             download_complete(0);
148             break;
149         default:
150             break;
151     }
152 }
153 
154 /**
155  * @brief Download firmware via uart_driver
156  * @param uart_driver -- already initialized
157  * @param done callback. 0 = Success
158  */
159 
160 void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
161     //
162     uart_driver = the_uart_driver;
163     chipset     = btstack_chipset_bcm_instance();
164     baudrate    = baudrate_upload;
165     download_complete = done;
166     btstack_chipset_bcm_enable_init_script(1);
167 
168     int res = uart_driver->open();
169     if (res) {
170         log_error("uart_block init failed %u", res);
171         download_complete(res);
172         return;
173     }
174 
175     // Reset with CTS asserted (low)
176     printf("Please reset Bluetooth Controller, e.g. via RESET button. Firmware download starts in:\n");
177     uint8_t i;
178     for (i = 3; i > 0; i--){
179         printf("%u\n", i);
180         sleep(1);
181     }
182     printf("Firmware download started\n");
183 
184     bcm_send_hci_reset();
185 }
186 
187 void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) {
188     btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done);
189 }
190