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 <stdio.h> 46 47 #ifdef _MSC_VER 48 // map sleep() to Sleep() 49 #include "Windows.h" 50 unsigned int sleep(unsigned int seconds){ 51 _Sleep(seconds * 1000); 52 return 0; 53 } 54 #else 55 #include <unistd.h> 56 #endif 57 58 #include "hci_dump.h" 59 #include "btstack_chipset_bcm.h" 60 #include "btstack_chipset_bcm_download_firmware.h" 61 #include "bluetooth.h" 62 #include "bluetooth_company_id.h" 63 #include "btstack_debug.h" 64 #include "btstack_chipset.h" 65 66 static void bcm_send_hci_baudrate(void); 67 static void bcm_send_next_init_script_command(void); 68 static void bcm_set_local_baudrate(void); 69 static void bcm_w4_command_complete(void); 70 71 static const btstack_uart_block_t * uart_driver; 72 static const btstack_chipset_t * chipset; 73 74 static uint8_t response_buffer[260]; 75 static uint16_t response_buffer_len; 76 static uint8_t command_buffer[260]; 77 78 static const int hci_command_complete_len = 7; 79 static const int hci_command_complete_read_local_version = 15; 80 static const uint8_t hci_read_local_version_cmd[] = { 0x01, 0x10, 0x00}; 81 static const uint8_t hci_reset_cmd[] = { 0x03, 0x0c, 0x00 }; 82 static const uint8_t hci_command_complete_reset[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x00}; 83 84 static void (*download_complete)(int result); 85 86 static uint32_t baudrate; 87 88 static inline void bcm_hci_dump_event(void){ 89 uint16_t event_len = 2 + response_buffer[2]; 90 hci_dump_packet(HCI_EVENT_PACKET, 0, &response_buffer[1], event_len); 91 } 92 93 static void bcm_send_prepared_command(void){ 94 command_buffer[0] = 1; 95 uint16_t command_len = 3 + command_buffer[3]; 96 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, &command_buffer[1], command_len); 97 uart_driver->send_block(command_buffer, command_len + 1); 98 } 99 100 // select controller based on { manufacturer / lmp_subversion } 101 static void bcm_detect_controller(uint16_t manufacturer, 102 uint16_t lmp_subversion) { 103 const char * device_name = NULL; 104 switch (manufacturer){ 105 case BLUETOOTH_COMPANY_ID_INFINEON_TECHNOLOGIES_AG: 106 switch (lmp_subversion){ 107 case 0x2257: 108 // CYW5557x 109 device_name = "CYW55560A1"; 110 break; 111 default: 112 break; 113 } 114 break; 115 default: 116 break; 117 } 118 if (device_name == NULL){ 119 printf("Unknown device, please update bcm_detect_controller()\n"); 120 printf("in btstack/chipset/bcm/btstack_chipset_bcm_download_firmware.c\n"); 121 } else { 122 printf("Controller: %s\n", device_name); 123 btstack_chipset_bcm_set_device_name(device_name); 124 } 125 } 126 127 // Send / Receive HCI Read Local Version Information 128 129 static void bcm_receive_command_command_complete_read_local_version(void){ 130 const uint8_t * packet = &response_buffer[1]; 131 printf("ROM version information:\n"); 132 uint16_t hci_version = packet[6]; 133 uint16_t hci_revision = little_endian_read_16(packet, 7); 134 uint16_t lmp_version = packet[9]; 135 uint16_t manufacturer = little_endian_read_16(packet, 10); 136 uint16_t lmp_subversion = little_endian_read_16(packet, 12); 137 printf("- HCI Version 0x%04x\n", hci_version); 138 printf("- HCI Revision 0x%04x\n", hci_revision); 139 printf("- LMP Version 0x%04x\n", lmp_version); 140 printf("- LMP Subversion 0x%04x\n", lmp_subversion); 141 printf("- Manufacturer 0x%04x\n", manufacturer); 142 143 bcm_detect_controller(manufacturer, lmp_subversion); 144 145 bcm_w4_command_complete(); 146 } 147 148 static void bcm_send_read_local_version(void){ 149 log_info("bcm: send HCI Read Local Version Information"); 150 uart_driver->set_block_received(&bcm_receive_command_command_complete_read_local_version); 151 uart_driver->receive_block(response_buffer, hci_command_complete_read_local_version); 152 memcpy(&command_buffer[1], hci_read_local_version_cmd, sizeof(hci_read_local_version_cmd)); 153 bcm_send_prepared_command(); 154 } 155 156 // Send / Receive HCI Reset 157 158 // Although the Controller just has been reset by the user, there might still be HCI data in the UART driver 159 // which we'll ignore in the receive function 160 161 static void bcm_receive_command_complete_reset(void){ 162 response_buffer_len++; 163 if (response_buffer_len == hci_command_complete_len){ 164 // try to match command complete for HCI Reset 165 if (memcmp(response_buffer, hci_command_complete_reset, hci_command_complete_len) == 0){ 166 bcm_hci_dump_event(); 167 bcm_send_read_local_version(); 168 return; 169 } 170 memmove(&response_buffer[0], &response_buffer[1], response_buffer_len - 1); 171 response_buffer_len--; 172 } 173 uart_driver->receive_block(&response_buffer[response_buffer_len], 1); 174 } 175 176 static void bcm_send_hci_reset(void){ 177 log_info("bcm: send HCI Reset"); 178 response_buffer_len = 0; 179 uart_driver->set_block_received(&bcm_receive_command_complete_reset); 180 uart_driver->receive_block(&response_buffer[response_buffer_len], 1); 181 memcpy(&command_buffer[1], hci_reset_cmd, sizeof(hci_reset_cmd)); 182 bcm_send_prepared_command(); 183 } 184 185 // Other 186 187 static void bcm_send_hci_baudrate(void){ 188 bcm_hci_dump_event(); 189 chipset->set_baudrate_command(baudrate, &command_buffer[1]); 190 uart_driver->set_block_received(&bcm_set_local_baudrate); 191 uart_driver->receive_block(&response_buffer[0], hci_command_complete_len); 192 log_info("bcm: send baud rate command - %u", baudrate); 193 bcm_send_prepared_command(); 194 } 195 196 static void bcm_set_local_baudrate(void){ 197 bcm_hci_dump_event(); 198 uart_driver->set_baudrate(baudrate); 199 uart_driver->set_block_received(&bcm_w4_command_complete); 200 bcm_send_next_init_script_command(); 201 } 202 203 static void bcm_w4_command_complete(void){ 204 bcm_hci_dump_event(); 205 uart_driver->set_block_received(&bcm_w4_command_complete); 206 bcm_send_next_init_script_command(); 207 } 208 209 static void bcm_send_next_init_script_command(void){ 210 int res = chipset->next_command(&command_buffer[1]); 211 switch (res){ 212 case BTSTACK_CHIPSET_VALID_COMMAND: 213 uart_driver->receive_block(&response_buffer[0], hci_command_complete_len); 214 bcm_send_prepared_command(); 215 break; 216 case BTSTACK_CHIPSET_DONE: 217 log_info("bcm: init script done"); 218 // disable init script for main startup 219 btstack_chipset_bcm_enable_init_script(0); 220 // reset baudrate to default 221 uart_driver->set_baudrate(115200); 222 // notify main 223 download_complete(0); 224 break; 225 default: 226 break; 227 } 228 } 229 230 /** 231 * @brief Download firmware via uart_driver 232 * @param uart_driver -- already initialized 233 * @param done callback. 0 = Success 234 */ 235 236 void btstack_chipset_bcm_download_firmware_with_uart(const btstack_uart_t * the_uart_driver, int baudrate_upload, void (*done)(int result)){ 237 // 238 uart_driver = the_uart_driver; 239 chipset = btstack_chipset_bcm_instance(); 240 baudrate = baudrate_upload; 241 download_complete = done; 242 btstack_chipset_bcm_enable_init_script(1); 243 244 int res = uart_driver->open(); 245 if (res) { 246 log_error("uart_block init failed %u", res); 247 download_complete(res); 248 return; 249 } 250 251 // Reset with CTS asserted (low) 252 printf("Please reset Bluetooth Controller, e.g. via RESET button. Firmware download starts in:\n"); 253 uint8_t i; 254 for (i = 3; i > 0; i--){ 255 printf("%u\n", i); 256 sleep(1); 257 } 258 printf("Firmware download started\n"); 259 260 bcm_send_hci_reset(); 261 } 262 263 void btstack_chipset_bcm_download_firmware(const btstack_uart_block_t * the_uart_driver, int baudrate_upload, void (*done)(int result)) { 264 btstack_chipset_bcm_download_firmware_with_uart((const btstack_uart_t *) the_uart_driver, baudrate_upload, done); 265 } 266