xref: /btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c (revision 99e8f095f58cf17ea9e1b418a9bc021e57c83981)
1*99e8f095SMatthias Ringwald /*
2*99e8f095SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
3*99e8f095SMatthias Ringwald  *
4*99e8f095SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*99e8f095SMatthias Ringwald  * modification, are permitted provided that the following conditions
6*99e8f095SMatthias Ringwald  * are met:
7*99e8f095SMatthias Ringwald  *
8*99e8f095SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*99e8f095SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*99e8f095SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*99e8f095SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*99e8f095SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*99e8f095SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*99e8f095SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*99e8f095SMatthias Ringwald  *    from this software without specific prior written permission.
16*99e8f095SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*99e8f095SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*99e8f095SMatthias Ringwald  *    monetary gain.
19*99e8f095SMatthias Ringwald  *
20*99e8f095SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*99e8f095SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*99e8f095SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*99e8f095SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*99e8f095SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*99e8f095SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*99e8f095SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*99e8f095SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*99e8f095SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*99e8f095SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*99e8f095SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*99e8f095SMatthias Ringwald  * SUCH DAMAGE.
32*99e8f095SMatthias Ringwald  *
33*99e8f095SMatthias Ringwald  * Please inquire about commercial licensing options at
34*99e8f095SMatthias Ringwald  * [email protected]
35*99e8f095SMatthias Ringwald  *
36*99e8f095SMatthias Ringwald  */
37*99e8f095SMatthias Ringwald 
38*99e8f095SMatthias Ringwald #define __BTSTACK_FILE__ "btstack_chipset_bcm_download_firmware.c"
39*99e8f095SMatthias Ringwald 
40*99e8f095SMatthias Ringwald // download firmware implementation
41*99e8f095SMatthias Ringwald // requires hci_dump
42*99e8f095SMatthias Ringwald // supports higher baudrate for patch upload
43*99e8f095SMatthias Ringwald 
44*99e8f095SMatthias Ringwald #include <string.h>
45*99e8f095SMatthias Ringwald 
46*99e8f095SMatthias Ringwald #include "hci_dump.h"
47*99e8f095SMatthias Ringwald #include "btstack_chipset_bcm.h"
48*99e8f095SMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h"
49*99e8f095SMatthias Ringwald #include "bluetooth.h"
50*99e8f095SMatthias Ringwald #include "btstack_debug.h"
51*99e8f095SMatthias Ringwald #include "btstack_chipset.h"
52*99e8f095SMatthias Ringwald 
53*99e8f095SMatthias Ringwald static void bcm_send_hci_baudrate(void);
54*99e8f095SMatthias Ringwald static void bcm_send_next_init_script_command(void);
55*99e8f095SMatthias Ringwald static void bcm_set_local_baudrate(void);
56*99e8f095SMatthias Ringwald static void bcm_w4_command_complete(void);
57*99e8f095SMatthias Ringwald 
58*99e8f095SMatthias Ringwald static const btstack_uart_block_t * uart_driver;
59*99e8f095SMatthias Ringwald static const btstack_chipset_t * chipset;
60*99e8f095SMatthias Ringwald 
61*99e8f095SMatthias Ringwald static uint8_t response_buffer[260];
62*99e8f095SMatthias Ringwald static uint8_t command_buffer[260];
63*99e8f095SMatthias Ringwald 
64*99e8f095SMatthias Ringwald static const int hci_command_complete_len = 7;
65*99e8f095SMatthias Ringwald static const uint8_t hci_reset_cmd[] = { 0x03, 0x0c, 0x00 };
66*99e8f095SMatthias Ringwald // static const uint8_t hci_update_baud_rate[] = { 0x01, 0x18, 0xfc, 0x06, 0x00, 0x00,0x00, 0x00, 0x00, 0x00 };
67*99e8f095SMatthias Ringwald static void (*download_complete)(int result);
68*99e8f095SMatthias Ringwald static int baudrate;
69*99e8f095SMatthias Ringwald 
70*99e8f095SMatthias Ringwald static void bcm_send_prepared_command(void){
71*99e8f095SMatthias Ringwald     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
72*99e8f095SMatthias Ringwald     int size = 1 + 3 + command_buffer[3];
73*99e8f095SMatthias Ringwald     command_buffer[0] = 1;
74*99e8f095SMatthias Ringwald     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], size-1);
75*99e8f095SMatthias Ringwald     uart_driver->send_block(command_buffer, size);
76*99e8f095SMatthias Ringwald }
77*99e8f095SMatthias Ringwald 
78*99e8f095SMatthias Ringwald static void bcm_send_hci_reset(void){
79*99e8f095SMatthias Ringwald     if (baudrate == 0 || baudrate == 115200){
80*99e8f095SMatthias Ringwald         uart_driver->set_block_received(&bcm_w4_command_complete);
81*99e8f095SMatthias Ringwald     } else {
82*99e8f095SMatthias Ringwald         uart_driver->set_block_received(&bcm_send_hci_baudrate);
83*99e8f095SMatthias Ringwald     }
84*99e8f095SMatthias Ringwald     log_info("bcm: send HCI Reset");
85*99e8f095SMatthias Ringwald     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
86*99e8f095SMatthias Ringwald     memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
87*99e8f095SMatthias Ringwald     bcm_send_prepared_command();
88*99e8f095SMatthias Ringwald }
89*99e8f095SMatthias Ringwald 
90*99e8f095SMatthias Ringwald static void bcm_send_hci_baudrate(void){
91*99e8f095SMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
92*99e8f095SMatthias Ringwald     chipset->set_baudrate_command(baudrate, &command_buffer[1]);
93*99e8f095SMatthias Ringwald     uart_driver->set_block_received(&bcm_set_local_baudrate);
94*99e8f095SMatthias Ringwald     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
95*99e8f095SMatthias Ringwald     log_info("bcm: send baud rate command");
96*99e8f095SMatthias Ringwald     bcm_send_prepared_command();
97*99e8f095SMatthias Ringwald }
98*99e8f095SMatthias Ringwald 
99*99e8f095SMatthias Ringwald static void bcm_set_local_baudrate(void){
100*99e8f095SMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
101*99e8f095SMatthias Ringwald     uart_driver->set_baudrate(baudrate);
102*99e8f095SMatthias Ringwald     uart_driver->set_block_received(&bcm_w4_command_complete);
103*99e8f095SMatthias Ringwald     bcm_send_next_init_script_command();
104*99e8f095SMatthias Ringwald }
105*99e8f095SMatthias Ringwald 
106*99e8f095SMatthias Ringwald static void bcm_w4_command_complete(void){
107*99e8f095SMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], hci_command_complete_len-1);
108*99e8f095SMatthias Ringwald     bcm_send_next_init_script_command();
109*99e8f095SMatthias Ringwald }
110*99e8f095SMatthias Ringwald 
111*99e8f095SMatthias Ringwald static void bcm_send_next_init_script_command(void){
112*99e8f095SMatthias Ringwald     int res = chipset->next_command(&command_buffer[1]);
113*99e8f095SMatthias Ringwald     switch (res){
114*99e8f095SMatthias Ringwald         case BTSTACK_CHIPSET_VALID_COMMAND:
115*99e8f095SMatthias Ringwald             bcm_send_prepared_command();
116*99e8f095SMatthias Ringwald             break;
117*99e8f095SMatthias Ringwald         case BTSTACK_CHIPSET_DONE:
118*99e8f095SMatthias Ringwald             log_info("bcm: init script done");
119*99e8f095SMatthias Ringwald             uart_driver->set_baudrate(115200);
120*99e8f095SMatthias Ringwald             download_complete(0);
121*99e8f095SMatthias Ringwald             break;
122*99e8f095SMatthias Ringwald         default:
123*99e8f095SMatthias Ringwald             break;
124*99e8f095SMatthias Ringwald     }
125*99e8f095SMatthias Ringwald }
126*99e8f095SMatthias Ringwald 
127*99e8f095SMatthias Ringwald /**
128*99e8f095SMatthias Ringwald  * @brief Download firmware via uart_driver
129*99e8f095SMatthias Ringwald  * @param uart_driver -- already initialized
130*99e8f095SMatthias Ringwald  * @param done callback. 0 = Success
131*99e8f095SMatthias Ringwald  */
132*99e8f095SMatthias Ringwald 
133*99e8f095SMatthias Ringwald void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
134*99e8f095SMatthias Ringwald     //
135*99e8f095SMatthias Ringwald     uart_driver = the_uart_driver;
136*99e8f095SMatthias Ringwald     chipset     = btstack_chipset_bcm_instance();
137*99e8f095SMatthias Ringwald     baudrate    = baudrate_upload;
138*99e8f095SMatthias Ringwald     download_complete = done;
139*99e8f095SMatthias Ringwald 
140*99e8f095SMatthias Ringwald     int res = uart_driver->open();
141*99e8f095SMatthias Ringwald     if (res) {
142*99e8f095SMatthias Ringwald         log_error("uart_block init failed %u", res);
143*99e8f095SMatthias Ringwald         download_complete(res);
144*99e8f095SMatthias Ringwald         return;
145*99e8f095SMatthias Ringwald     }
146*99e8f095SMatthias Ringwald 
147*99e8f095SMatthias Ringwald     bcm_send_hci_reset();
148*99e8f095SMatthias Ringwald }
149