xref: /btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c (revision cd5f23a3250874824c01a2b3326a9522fea3f99f)
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 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 #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 
46 #include "hci_dump.h"
47 #include "btstack_chipset_bcm.h"
48 #include "btstack_chipset_bcm_download_firmware.h"
49 #include "bluetooth.h"
50 #include "btstack_debug.h"
51 #include "btstack_chipset.h"
52 
53 static void bcm_send_hci_baudrate(void);
54 static void bcm_send_next_init_script_command(void);
55 static void bcm_set_local_baudrate(void);
56 static void bcm_w4_command_complete(void);
57 
58 static const btstack_uart_block_t * uart_driver;
59 static const btstack_chipset_t * chipset;
60 
61 static uint8_t response_buffer[260];
62 static uint8_t command_buffer[260];
63 
64 static const int hci_command_complete_len = 7;
65 static const uint8_t hci_reset_cmd[] = { 0x03, 0x0c, 0x00 };
66 // static const uint8_t hci_update_baud_rate[] = { 0x01, 0x18, 0xfc, 0x06, 0x00, 0x00,0x00, 0x00, 0x00, 0x00 };
67 static void (*download_complete)(int result);
68 static int baudrate;
69 
70 static void bcm_send_prepared_command(void){
71     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
72     int size = 1 + 3 + command_buffer[3];
73     command_buffer[0] = 1;
74     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], size-1);
75     uart_driver->send_block(command_buffer, size);
76 }
77 
78 static void bcm_send_hci_reset(void){
79     if (baudrate == 0 || baudrate == 115200){
80         uart_driver->set_block_received(&bcm_w4_command_complete);
81     } else {
82         uart_driver->set_block_received(&bcm_send_hci_baudrate);
83     }
84     log_info("bcm: send HCI Reset");
85     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
86     memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
87     bcm_send_prepared_command();
88 }
89 
90 static void bcm_send_hci_baudrate(void){
91     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
92     chipset->set_baudrate_command(baudrate, &command_buffer[1]);
93     uart_driver->set_block_received(&bcm_set_local_baudrate);
94     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
95     log_info("bcm: send baud rate command");
96     bcm_send_prepared_command();
97 }
98 
99 static void bcm_set_local_baudrate(void){
100     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
101     uart_driver->set_baudrate(baudrate);
102     uart_driver->set_block_received(&bcm_w4_command_complete);
103     bcm_send_next_init_script_command();
104 }
105 
106 static void bcm_w4_command_complete(void){
107     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
108     bcm_send_next_init_script_command();
109 }
110 
111 static void bcm_send_next_init_script_command(void){
112     int res = chipset->next_command(&command_buffer[1]);
113     switch (res){
114         case BTSTACK_CHIPSET_VALID_COMMAND:
115             bcm_send_prepared_command();
116             break;
117         case BTSTACK_CHIPSET_DONE:
118             log_info("bcm: init script done");
119             // disable init script for main startup
120             btstack_chipset_bcm_enable_init_script(0);
121             // reset baudreate to default
122             uart_driver->set_baudrate(115200);
123             // notify main
124             download_complete(0);
125             break;
126         default:
127             break;
128     }
129 }
130 
131 /**
132  * @brief Download firmware via uart_driver
133  * @param uart_driver -- already initialized
134  * @param done callback. 0 = Success
135  */
136 
137 void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
138     //
139     uart_driver = the_uart_driver;
140     chipset     = btstack_chipset_bcm_instance();
141     baudrate    = baudrate_upload;
142     download_complete = done;
143     btstack_chipset_bcm_enable_init_script(1);
144 
145     int res = uart_driver->open();
146     if (res) {
147         log_error("uart_block init failed %u", res);
148         download_complete(res);
149         return;
150     }
151 
152     bcm_send_hci_reset();
153 }
154 void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) {
155     btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done);
156 }
157