xref: /btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c (revision 7f03330a55d25ecd68b12ae915192984980597c7)
199e8f095SMatthias Ringwald /*
299e8f095SMatthias Ringwald  * Copyright (C) 2017 BlueKitchen GmbH
399e8f095SMatthias Ringwald  *
499e8f095SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
599e8f095SMatthias Ringwald  * modification, are permitted provided that the following conditions
699e8f095SMatthias Ringwald  * are met:
799e8f095SMatthias Ringwald  *
899e8f095SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
999e8f095SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1099e8f095SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1199e8f095SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1299e8f095SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1399e8f095SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1499e8f095SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1599e8f095SMatthias Ringwald  *    from this software without specific prior written permission.
1699e8f095SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1799e8f095SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1899e8f095SMatthias Ringwald  *    monetary gain.
1999e8f095SMatthias Ringwald  *
2099e8f095SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2199e8f095SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2299e8f095SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2599e8f095SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2699e8f095SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2799e8f095SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2899e8f095SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2999e8f095SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3099e8f095SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3199e8f095SMatthias Ringwald  * SUCH DAMAGE.
3299e8f095SMatthias Ringwald  *
3399e8f095SMatthias Ringwald  * Please inquire about commercial licensing options at
3499e8f095SMatthias Ringwald  * [email protected]
3599e8f095SMatthias Ringwald  *
3699e8f095SMatthias Ringwald  */
3799e8f095SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "btstack_chipset_bcm_download_firmware.c"
3999e8f095SMatthias Ringwald 
4099e8f095SMatthias Ringwald // download firmware implementation
4199e8f095SMatthias Ringwald // requires hci_dump
4299e8f095SMatthias Ringwald // supports higher baudrate for patch upload
4399e8f095SMatthias Ringwald 
4499e8f095SMatthias Ringwald #include <string.h>
45c6dda3a5SMatthias Ringwald #include <stdio.h>
46889634f3SMatthias Ringwald 
47889634f3SMatthias Ringwald #ifdef _MSC_VER
48889634f3SMatthias Ringwald // map sleep() to Sleep()
49889634f3SMatthias Ringwald #include "Windows.h"
sleep(unsigned int seconds)50889634f3SMatthias Ringwald unsigned int sleep(unsigned int seconds){
51889634f3SMatthias Ringwald     _Sleep(seconds * 1000);
52889634f3SMatthias Ringwald     return 0;
53889634f3SMatthias Ringwald }
54889634f3SMatthias Ringwald #else
550df9df8cSMatthias Ringwald #include <unistd.h>
56889634f3SMatthias Ringwald #endif
5799e8f095SMatthias Ringwald 
5899e8f095SMatthias Ringwald #include "hci_dump.h"
5999e8f095SMatthias Ringwald #include "btstack_chipset_bcm.h"
6099e8f095SMatthias Ringwald #include "btstack_chipset_bcm_download_firmware.h"
6199e8f095SMatthias Ringwald #include "bluetooth.h"
622150e30cSMatthias Ringwald #include "bluetooth_company_id.h"
6399e8f095SMatthias Ringwald #include "btstack_debug.h"
6499e8f095SMatthias Ringwald #include "btstack_chipset.h"
6599e8f095SMatthias Ringwald 
6699e8f095SMatthias Ringwald static void bcm_send_next_init_script_command(void);
6799e8f095SMatthias Ringwald static void bcm_w4_command_complete(void);
6899e8f095SMatthias Ringwald 
6999e8f095SMatthias Ringwald static const btstack_uart_block_t * uart_driver;
7099e8f095SMatthias Ringwald static const btstack_chipset_t * chipset;
7199e8f095SMatthias Ringwald 
7299e8f095SMatthias Ringwald static uint8_t response_buffer[260];
73e957bdcbSMatthias Ringwald static uint16_t response_buffer_len;
7499e8f095SMatthias Ringwald static uint8_t command_buffer[260];
7599e8f095SMatthias Ringwald 
7699e8f095SMatthias Ringwald static const int hci_command_complete_len = 7;
7786243214SMatthias Ringwald static const int hci_command_complete_read_local_version = 15;
7886243214SMatthias Ringwald static const uint8_t hci_read_local_version_cmd[] = { 0x01, 0x10, 0x00};
7999e8f095SMatthias Ringwald static const uint8_t hci_reset_cmd[] =              { 0x03, 0x0c, 0x00 };
80e957bdcbSMatthias Ringwald static const uint8_t hci_command_complete_reset[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00};
81e957bdcbSMatthias Ringwald 
8299e8f095SMatthias Ringwald static void (*download_complete)(int result);
832150e30cSMatthias Ringwald 
84e957bdcbSMatthias Ringwald static uint32_t baudrate;
8599e8f095SMatthias Ringwald 
bcm_hci_dump_event(void)8686243214SMatthias Ringwald static inline void bcm_hci_dump_event(void){
8786243214SMatthias Ringwald     uint16_t event_len = 2 + response_buffer[2];
8886243214SMatthias Ringwald     hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], event_len);
8999e8f095SMatthias Ringwald }
9099e8f095SMatthias Ringwald 
bcm_send_prepared_command(void)9186243214SMatthias Ringwald static void bcm_send_prepared_command(void){
9286243214SMatthias Ringwald     command_buffer[0] = 1;
9386243214SMatthias Ringwald     uint16_t command_len = 3 + command_buffer[3];
9486243214SMatthias Ringwald     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], command_len);
9586243214SMatthias Ringwald     uart_driver->send_block(command_buffer, command_len + 1);
9686243214SMatthias Ringwald }
9786243214SMatthias Ringwald 
982150e30cSMatthias Ringwald // select controller based on { manufacturer / lmp_subversion }
bcm_detect_controller(uint16_t manufacturer,uint16_t lmp_subversion)992150e30cSMatthias Ringwald static void bcm_detect_controller(uint16_t manufacturer,
1002150e30cSMatthias Ringwald                                   uint16_t lmp_subversion) {
1012150e30cSMatthias Ringwald     const char * device_name = NULL;
1022150e30cSMatthias Ringwald     switch (manufacturer){
1032150e30cSMatthias Ringwald         case BLUETOOTH_COMPANY_ID_INFINEON_TECHNOLOGIES_AG:
1042150e30cSMatthias Ringwald             switch (lmp_subversion){
1052150e30cSMatthias Ringwald                 case 0x2257:
1062150e30cSMatthias Ringwald                     // CYW5557x
1072150e30cSMatthias Ringwald                     device_name = "CYW55560A1";
1082150e30cSMatthias Ringwald                     break;
109*7f03330aSMatthias Ringwald                 case 0x2220:
110*7f03330aSMatthias Ringwald                     // CYW5551x
111*7f03330aSMatthias Ringwald                     device_name = "CYW55500A1";
112*7f03330aSMatthias Ringwald                     break;
1132150e30cSMatthias Ringwald                 default:
1142150e30cSMatthias Ringwald                     break;
1152150e30cSMatthias Ringwald             }
1162150e30cSMatthias Ringwald             break;
1172150e30cSMatthias Ringwald         default:
1182150e30cSMatthias Ringwald             break;
1192150e30cSMatthias Ringwald     }
1202150e30cSMatthias Ringwald     if (device_name == NULL){
1212150e30cSMatthias Ringwald         printf("Unknown device, please update bcm_detect_controller()\n");
1222150e30cSMatthias Ringwald         printf("in btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c\n");
1232150e30cSMatthias Ringwald     } else {
1242150e30cSMatthias Ringwald         printf("Controller: %s\n", device_name);
1252150e30cSMatthias Ringwald         btstack_chipset_bcm_set_device_name(device_name);
1262150e30cSMatthias Ringwald     }
1272150e30cSMatthias Ringwald }
1282150e30cSMatthias Ringwald 
12986243214SMatthias Ringwald // Send / Receive HCI Read Local Version Information
13086243214SMatthias Ringwald 
bcm_receive_command_command_complete_read_local_version(void)13186243214SMatthias Ringwald static void bcm_receive_command_command_complete_read_local_version(void){
13286243214SMatthias Ringwald     const uint8_t * packet = &response_buffer[1];
13386243214SMatthias Ringwald     printf("ROM version information:\n");
13486243214SMatthias Ringwald     uint16_t hci_version    = packet[6];
13586243214SMatthias Ringwald     uint16_t hci_revision   = little_endian_read_16(packet, 7);
13686243214SMatthias Ringwald     uint16_t lmp_version    = packet[9];
13786243214SMatthias Ringwald     uint16_t manufacturer   = little_endian_read_16(packet, 10);
13886243214SMatthias Ringwald     uint16_t lmp_subversion = little_endian_read_16(packet, 12);
13986243214SMatthias Ringwald     printf("- HCI Version    0x%04x\n", hci_version);
14086243214SMatthias Ringwald     printf("- HCI Revision   0x%04x\n", hci_revision);
14186243214SMatthias Ringwald     printf("- LMP Version    0x%04x\n", lmp_version);
14286243214SMatthias Ringwald     printf("- LMP Subversion 0x%04x\n", lmp_subversion);
14386243214SMatthias Ringwald     printf("- Manufacturer 0x%04x\n", manufacturer);
1442150e30cSMatthias Ringwald 
1452150e30cSMatthias Ringwald     bcm_detect_controller(manufacturer, lmp_subversion);
1462150e30cSMatthias Ringwald 
14786243214SMatthias Ringwald     bcm_w4_command_complete();
14886243214SMatthias Ringwald }
14986243214SMatthias Ringwald 
bcm_send_read_local_version(void)15086243214SMatthias Ringwald static void bcm_send_read_local_version(void){
15186243214SMatthias Ringwald     log_info("bcm: send HCI Read Local Version Information");
15286243214SMatthias Ringwald     uart_driver->set_block_received(&bcm_receive_command_command_complete_read_local_version);
15386243214SMatthias Ringwald     uart_driver->receive_block(response_buffer, hci_command_complete_read_local_version);
15486243214SMatthias Ringwald     memcpy(&command_buffer[1], hci_read_local_version_cmd, sizeof(hci_read_local_version_cmd));
15586243214SMatthias Ringwald     bcm_send_prepared_command();
15686243214SMatthias Ringwald }
15786243214SMatthias Ringwald 
15886243214SMatthias Ringwald // Send / Receive HCI Reset
15986243214SMatthias Ringwald 
160e957bdcbSMatthias Ringwald // Although the Controller just has been reset by the user, there might still be HCI data in the UART driver
161e957bdcbSMatthias Ringwald // which we'll ignore in the receive function
162e957bdcbSMatthias Ringwald 
bcm_receive_command_complete_reset(void)163e957bdcbSMatthias Ringwald static void bcm_receive_command_complete_reset(void){
164e957bdcbSMatthias Ringwald     response_buffer_len++;
165e957bdcbSMatthias Ringwald     if (response_buffer_len == hci_command_complete_len){
166e957bdcbSMatthias Ringwald         // try to match command complete for HCI Reset
167e957bdcbSMatthias Ringwald         if (memcmp(response_buffer, hci_command_complete_reset, hci_command_complete_len) == 0){
16886243214SMatthias Ringwald             bcm_hci_dump_event();
16986243214SMatthias Ringwald             bcm_send_read_local_version();
170e957bdcbSMatthias Ringwald             return;
171e957bdcbSMatthias Ringwald         }
172e957bdcbSMatthias Ringwald         memmove(&response_buffer[0], &response_buffer[1], response_buffer_len - 1);
173e957bdcbSMatthias Ringwald         response_buffer_len--;
174e957bdcbSMatthias Ringwald     }
175e957bdcbSMatthias Ringwald     uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
176e957bdcbSMatthias Ringwald }
177e957bdcbSMatthias Ringwald 
bcm_send_hci_reset(void)17899e8f095SMatthias Ringwald static void bcm_send_hci_reset(void){
17999e8f095SMatthias Ringwald     log_info("bcm: send HCI Reset");
180e957bdcbSMatthias Ringwald     response_buffer_len = 0;
181e957bdcbSMatthias Ringwald     uart_driver->set_block_received(&bcm_receive_command_complete_reset);
182e957bdcbSMatthias Ringwald     uart_driver->receive_block(&response_buffer[response_buffer_len], 1);
18399e8f095SMatthias Ringwald     memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd));
18486243214SMatthias Ringwald     bcm_send_prepared_command();
18599e8f095SMatthias Ringwald }
18699e8f095SMatthias Ringwald 
18786243214SMatthias Ringwald // Other
18886243214SMatthias Ringwald 
1897d6c5059SMatthias Ringwald #if 0
1907d6c5059SMatthias Ringwald static void bcm_set_local_baudrate(void){
1917d6c5059SMatthias Ringwald     bcm_hci_dump_event();
1927d6c5059SMatthias Ringwald     uart_driver->set_baudrate(baudrate);
1937d6c5059SMatthias Ringwald     uart_driver->set_block_received(&bcm_w4_command_complete);
1947d6c5059SMatthias Ringwald     bcm_send_next_init_script_command();
1957d6c5059SMatthias Ringwald }
19699e8f095SMatthias Ringwald static void bcm_send_hci_baudrate(void){
19786243214SMatthias Ringwald     bcm_hci_dump_event();
19899e8f095SMatthias Ringwald     chipset->set_baudrate_command(baudrate, &command_buffer[1]);
19999e8f095SMatthias Ringwald     uart_driver->set_block_received(&bcm_set_local_baudrate);
20099e8f095SMatthias Ringwald     uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
2010df9df8cSMatthias Ringwald     log_info("bcm: send baud rate command - %u", baudrate);
20299e8f095SMatthias Ringwald     bcm_send_prepared_command();
20399e8f095SMatthias Ringwald }
2047d6c5059SMatthias Ringwald #endif
20599e8f095SMatthias Ringwald 
bcm_w4_command_complete(void)20699e8f095SMatthias Ringwald static void bcm_w4_command_complete(void){
20786243214SMatthias Ringwald     bcm_hci_dump_event();
208e957bdcbSMatthias Ringwald     uart_driver->set_block_received(&bcm_w4_command_complete);
20999e8f095SMatthias Ringwald     bcm_send_next_init_script_command();
21099e8f095SMatthias Ringwald }
21199e8f095SMatthias Ringwald 
bcm_send_next_init_script_command(void)21299e8f095SMatthias Ringwald static void bcm_send_next_init_script_command(void){
21399e8f095SMatthias Ringwald     int res = chipset->next_command(&command_buffer[1]);
21499e8f095SMatthias Ringwald     switch (res){
21599e8f095SMatthias Ringwald         case BTSTACK_CHIPSET_VALID_COMMAND:
21686243214SMatthias Ringwald             uart_driver->receive_block(&response_buffer[0], hci_command_complete_len);
21799e8f095SMatthias Ringwald             bcm_send_prepared_command();
21899e8f095SMatthias Ringwald             break;
21999e8f095SMatthias Ringwald         case BTSTACK_CHIPSET_DONE:
22099e8f095SMatthias Ringwald             log_info("bcm: init script done");
221646a1850SMatthias Ringwald             // disable init script for main startup
222646a1850SMatthias Ringwald             btstack_chipset_bcm_enable_init_script(0);
2230df9df8cSMatthias Ringwald             // reset baudrate to default
22499e8f095SMatthias Ringwald             uart_driver->set_baudrate(115200);
225646a1850SMatthias Ringwald             // notify main
22699e8f095SMatthias Ringwald             download_complete(0);
22799e8f095SMatthias Ringwald             break;
22899e8f095SMatthias Ringwald         default:
22999e8f095SMatthias Ringwald             break;
23099e8f095SMatthias Ringwald     }
23199e8f095SMatthias Ringwald }
23299e8f095SMatthias Ringwald 
23399e8f095SMatthias Ringwald /**
23499e8f095SMatthias Ringwald  * @brief Download firmware via uart_driver
23599e8f095SMatthias Ringwald  * @param uart_driver -- already initialized
23699e8f095SMatthias Ringwald  * @param done callback. 0 = Success
23799e8f095SMatthias Ringwald  */
23899e8f095SMatthias Ringwald 
btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver,int baudrate_upload,void (* done)(int result))23978a48aa8SMatthias Ringwald void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){
24099e8f095SMatthias Ringwald     //
24199e8f095SMatthias Ringwald     uart_driver = the_uart_driver;
24299e8f095SMatthias Ringwald     chipset     = btstack_chipset_bcm_instance();
24399e8f095SMatthias Ringwald     baudrate    = baudrate_upload;
24499e8f095SMatthias Ringwald     download_complete = done;
2458ac52328SMatthias Ringwald     btstack_chipset_bcm_enable_init_script(1);
24699e8f095SMatthias Ringwald 
24799e8f095SMatthias Ringwald     int res = uart_driver->open();
24899e8f095SMatthias Ringwald     if (res) {
24999e8f095SMatthias Ringwald         log_error("uart_block init failed %u", res);
25099e8f095SMatthias Ringwald         download_complete(res);
25199e8f095SMatthias Ringwald         return;
25299e8f095SMatthias Ringwald     }
25399e8f095SMatthias Ringwald 
2540df9df8cSMatthias Ringwald     // Reset with CTS asserted (low)
2550df9df8cSMatthias Ringwald     printf("Please reset Bluetooth Controller, e.g. via RESET button. Firmware download starts in:\n");
2560df9df8cSMatthias Ringwald     uint8_t i;
2570df9df8cSMatthias Ringwald     for (i = 3; i > 0; i--){
2580df9df8cSMatthias Ringwald         printf("%u\n", i);
2590df9df8cSMatthias Ringwald         sleep(1);
2600df9df8cSMatthias Ringwald     }
2610df9df8cSMatthias Ringwald     printf("Firmware download started\n");
2620df9df8cSMatthias Ringwald 
26399e8f095SMatthias Ringwald     bcm_send_hci_reset();
26499e8f095SMatthias Ringwald }
2650df9df8cSMatthias Ringwald 
btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver,int baudrate_upload,void (* done)(int result))26678a48aa8SMatthias Ringwald void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) {
26778a48aa8SMatthias Ringwald     btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done);
26878a48aa8SMatthias Ringwald }
269