xref: /btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c (revision 2364ad0c608cae46bf5e3f74e3a9715d4b7e1aed)
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 uint8_t command_buffer[260];
65 
66 static const int hci_command_complete_len = 7;
67 static const uint8_t hci_reset_cmd[] = { 0x03, 0x0c, 0x00 };
68 // static const uint8_t hci_update_baud_rate[] = { 0x01, 0x18, 0xfc, 0x06, 0x00, 0x00,0x00, 0x00, 0x00, 0x00 };
69 static void (*download_complete)(int result);
70 static int baudrate;
71 
72 static void bcm_send_prepared_command(void){
73     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
74     int size = 1 + 3 + command_buffer[3];
75     command_buffer[0] = 1;
76     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], size-1);
77     uart_driver->send_block(command_buffer, size);
78 }
79 
80 static void bcm_send_hci_reset(void){
81     log_info("bcm: send HCI Reset");
82     bool send_baudrate = (baudrate != 0) && (baudrate != 115200);
83     uart_driver->set_block_received(send_baudrate ? &bcm_send_hci_baudrate : &bcm_w4_command_complete);
84     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
85     memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
86     bcm_send_prepared_command();
87 }
88 
89 static void bcm_send_hci_baudrate(void){
90     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
91     chipset->set_baudrate_command(baudrate, &command_buffer[1]);
92     uart_driver->set_block_received(&bcm_set_local_baudrate);
93     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
94     log_info("bcm: send baud rate command - %u", baudrate);
95     bcm_send_prepared_command();
96 }
97 
98 static void bcm_set_local_baudrate(void){
99     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
100     uart_driver->set_baudrate(baudrate);
101     uart_driver->set_block_received(&bcm_w4_command_complete);
102     bcm_send_next_init_script_command();
103 }
104 
105 static void bcm_w4_command_complete(void){
106     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
107     bcm_send_next_init_script_command();
108 }
109 
110 static void bcm_send_next_init_script_command(void){
111     int res = chipset->next_command(&command_buffer[1]);
112     switch (res){
113         case BTSTACK_CHIPSET_VALID_COMMAND:
114             bcm_send_prepared_command();
115             break;
116         case BTSTACK_CHIPSET_DONE:
117             log_info("bcm: init script done");
118             // disable init script for main startup
119             btstack_chipset_bcm_enable_init_script(0);
120             // reset baudrate to default
121             uart_driver->set_baudrate(115200);
122             // notify main
123             download_complete(0);
124             break;
125         default:
126             break;
127     }
128 }
129 
130 /**
131  * @brief Download firmware via uart_driver
132  * @param uart_driver -- already initialized
133  * @param done callback. 0 = Success
134  */
135 
136 void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
137     //
138     uart_driver = the_uart_driver;
139     chipset     = btstack_chipset_bcm_instance();
140     baudrate    = baudrate_upload;
141     download_complete = done;
142     btstack_chipset_bcm_enable_init_script(1);
143 
144     int res = uart_driver->open();
145     if (res) {
146         log_error("uart_block init failed %u", res);
147         download_complete(res);
148         return;
149     }
150 
151     // Reset with CTS asserted (low)
152     printf("Please reset Bluetooth Controller, e.g. via RESET button. Firmware download starts in:\n");
153     uint8_t i;
154     for (i = 3; i > 0; i--){
155         printf("%u\n", i);
156         sleep(1);
157     }
158     printf("Firmware download started\n");
159 
160     bcm_send_hci_reset();
161 }
162 
163 void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) {
164     btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done);
165 }
166