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 /* 39 * btstack_chipset_atwilc3000.c 40 * 41 * Adapter to use atwilc3000-based chipsets with BTstack 42 * 43 */ 44 45 #define __BTSTACK_FILE__ "btstack_chipset_atwilc3000.c" 46 47 #include "btstack_config.h" 48 #include "btstack_chipset_atwilc3000.h" 49 #include "btstack_debug.h" 50 51 52 #include <stddef.h> /* NULL */ 53 #include <stdio.h> 54 #include <string.h> /* memcpy */ 55 #include "hci.h" 56 57 #define HCI_DEFAULT_BAUDRATE 115200 58 59 // Address to load firmware 60 #define IRAM_START 0x80000000 61 62 // Larger blocks (e.g. 8192) cause hang 63 #define FIRMWARE_CHUNK_SIZE 4096 64 65 // works with 200 ms, so use 250 ms to stay on the safe side 66 #define ATWILC3000_RESET_TIME_MS 250 67 68 // HCI commands used for firmware upload 69 static const uint8_t hci_reset_command[] = { 0x01, 0x03, 0x0c, 0x00 }; 70 static const uint8_t hci_read_local_version_information_command[] = { 0x01, 0x01, 0x10, 0x00 }; 71 static const uint8_t hci_vendor_specific_reset_command[] = { 0x01, 0x55, 0xfc, 0x00 }; 72 73 // prototypes 74 static void atwilc3000_configure_uart(btstack_timer_source_t * ts); 75 static void atwilc3000_done(void); 76 static void atwilc3000_update_uart_params(void); 77 static void atwilc3000_vendor_specific_reset(void); 78 static void atwilc3000_w4_baudrate_update(void); 79 static void atwilc3000_w4_command_complete_read_local_version_information(void); 80 static void atwilc3000_w4_command_complete_reset(void); 81 static void atwilc3000_wait_for_reset_completed(void); 82 static void atwilc3000_write_firmware(void); 83 static void atwilc3000_write_memory(void); 84 85 // globals 86 static void (*download_complete)(int result); 87 static const btstack_uart_block_t * the_uart_driver; 88 static btstack_timer_source_t reset_timer; 89 90 static int download_count; 91 static uint8_t event_buffer[15]; 92 static uint8_t command_buffer[12]; 93 static const uint8_t * fw_data; 94 static uint32_t fw_size; 95 static uint32_t fw_offset; 96 97 // baudrate for firmware upload 98 static uint32_t fw_baudrate; 99 100 // flow control requested 101 static int fw_flowtcontrol; 102 103 // flow control active 104 static int flowcontrol; 105 106 static void atwilc3000_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){ 107 hci_cmd_buffer[0] = 0x53; 108 hci_cmd_buffer[1] = 0xfc; 109 hci_cmd_buffer[2] = 5; 110 little_endian_store_32(hci_cmd_buffer, 3, baudrate); 111 hci_cmd_buffer[7] = flowcontrol; // use global state 112 } 113 114 static void atwilc3000_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){ 115 hci_cmd_buffer[0] = 0x54; 116 hci_cmd_buffer[1] = 0xFC; 117 hci_cmd_buffer[2] = 0x06; 118 reverse_bd_addr(addr, &hci_cmd_buffer[3]); 119 } 120 121 static void atwilc3000_send_command(const uint8_t * data, uint16_t len){ 122 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, (uint8_t *) &data[1], len - 1); 123 the_uart_driver->send_block(data, len); 124 } 125 126 static void atwilc3000_log_event(void){ 127 int len = event_buffer[2] + 2; 128 hci_dump_packet(HCI_EVENT_PACKET, 1, &event_buffer[1], len); 129 } 130 131 static void atwilc3000_start(void){ 132 // default after power up 133 flowcontrol = 0; 134 135 // send HCI Reset 136 the_uart_driver->set_block_received(&atwilc3000_w4_command_complete_reset); 137 the_uart_driver->receive_block(&event_buffer[0], 7); 138 atwilc3000_send_command(&hci_reset_command[0], sizeof(hci_reset_command)); 139 } 140 141 static void atwilc3000_w4_command_complete_reset(void){ 142 atwilc3000_log_event(); 143 // send HCI Read Local Version Information 144 the_uart_driver->receive_block(&event_buffer[0], 15); 145 the_uart_driver->set_block_received(&atwilc3000_w4_command_complete_read_local_version_information); 146 atwilc3000_send_command(&hci_read_local_version_information_command[0], sizeof(hci_read_local_version_information_command)); 147 } 148 149 static void atwilc3000_w4_command_complete_read_local_version_information(void){ 150 atwilc3000_log_event(); 151 uint8_t firmware_version = event_buffer[7]; 152 if (firmware_version != 0xff){ 153 log_info("Firmware version 0x%02x already loaded, download complete", firmware_version); 154 download_complete(0); 155 return; 156 } 157 log_info("Running from ROM, start firmware download"); 158 if (fw_baudrate){ 159 atwilc3000_update_uart_params(); 160 } else { 161 atwilc3000_write_memory(); 162 } 163 } 164 165 static void atwilc3000_update_uart_params(void){ 166 command_buffer[0] = 1; 167 atwilc3000_set_baudrate_command(fw_baudrate, &command_buffer[1]); 168 the_uart_driver->receive_block(&event_buffer[0], 7); 169 the_uart_driver->set_block_received(&atwilc3000_w4_baudrate_update); 170 atwilc3000_send_command(&command_buffer[0], 9); 171 } 172 173 static void atwilc3000_w4_baudrate_update(void){ 174 atwilc3000_log_event(); 175 the_uart_driver->set_baudrate(fw_baudrate); 176 atwilc3000_write_memory(); 177 } 178 179 static void atwilc3000_write_memory(void){ 180 atwilc3000_log_event(); 181 182 // done? 183 if (fw_offset >= fw_size){ 184 log_info("Firmware upload complete!!!"); 185 atwilc3000_vendor_specific_reset(); 186 return; 187 } 188 189 // bytes to write 190 log_info("Write pos %u", fw_offset); 191 uint16_t bytes_to_write = btstack_min((fw_size - fw_offset), FIRMWARE_CHUNK_SIZE); 192 // setup write command 193 command_buffer[0] = 1; 194 command_buffer[1] = 0x52; 195 command_buffer[2] = 0xfc; 196 command_buffer[3] = 8; // NOTE: this is in violation of the Bluetooth Specification, but documented in the Atmel-NNNNN-ATWIL_Linux_Porting_Guide 197 little_endian_store_32(command_buffer, 4, IRAM_START + fw_offset); 198 little_endian_store_32(command_buffer, 8, bytes_to_write); 199 200 // send write command - only log write command without the firmware blob 201 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, (uint8_t *) &command_buffer[1], 12 - 1); 202 the_uart_driver->set_block_sent(&atwilc3000_write_firmware); 203 the_uart_driver->send_block(&command_buffer[0], 12); 204 } 205 206 static void atwilc3000_write_firmware(void){ 207 the_uart_driver->set_block_received(&atwilc3000_write_memory); 208 the_uart_driver->receive_block(&event_buffer[0], 7); 209 210 uint16_t bytes_to_write = btstack_min((fw_size - fw_offset), FIRMWARE_CHUNK_SIZE); 211 212 uint32_t offset = fw_offset; 213 fw_offset += bytes_to_write; 214 215 the_uart_driver->set_block_sent(NULL); 216 the_uart_driver->send_block(&fw_data[offset], bytes_to_write); 217 } 218 219 static void atwilc3000_vendor_specific_reset(void){ 220 log_info("Trigger MCU reboot and wait "); 221 // send HCI Vendor Specific Reset 222 the_uart_driver->set_block_sent(&atwilc3000_wait_for_reset_completed); 223 atwilc3000_send_command(&hci_vendor_specific_reset_command[0], sizeof(hci_vendor_specific_reset_command)); 224 } 225 226 static void atwilc3000_wait_for_reset_completed(void){ 227 the_uart_driver->set_block_sent(NULL); 228 btstack_run_loop_set_timer_handler(&reset_timer, &atwilc3000_configure_uart); 229 btstack_run_loop_set_timer(&reset_timer, ATWILC3000_RESET_TIME_MS); 230 btstack_run_loop_add_timer(&reset_timer); 231 } 232 233 static void atwilc3000_configure_uart(btstack_timer_source_t * ts){ 234 // reset baud rate if higher baud rate was requested before 235 if (fw_baudrate){ 236 the_uart_driver->set_baudrate(HCI_DEFAULT_BAUDRATE); 237 } 238 // send baudrate command to enable flow control (using current baud rate) if requested and supported 239 if (fw_flowtcontrol && the_uart_driver->set_flowcontrol){ 240 log_info("Send baudrate command (%u) to enable flow control", HCI_DEFAULT_BAUDRATE); 241 flowcontrol = 1; 242 command_buffer[0] = 1; 243 atwilc3000_set_baudrate_command(HCI_DEFAULT_BAUDRATE, &command_buffer[1]); 244 the_uart_driver->set_block_received(&atwilc3000_done); 245 the_uart_driver->receive_block(&event_buffer[0], 7); 246 atwilc3000_send_command(&command_buffer[0], 9); 247 } else { 248 atwilc3000_done(); 249 } 250 } 251 252 static void atwilc3000_done(void){ 253 atwilc3000_log_event(); 254 // enable our flow control 255 if (flowcontrol){ 256 the_uart_driver->set_flowcontrol(flowcontrol); 257 } 258 // done 259 download_complete(0); 260 } 261 262 void btstack_chipset_atwilc3000_download_firmware(const btstack_uart_block_t * uart_driver, uint32_t baudrate, int flowcontrol, const uint8_t * da_fw_data, uint32_t da_fw_size, void (*done)(int result)){ 263 264 the_uart_driver = uart_driver; 265 download_complete = done; 266 fw_data = da_fw_data; 267 fw_size = da_fw_size; 268 fw_offset = 0; 269 fw_baudrate = baudrate; 270 fw_flowtcontrol = flowcontrol; 271 272 int res = the_uart_driver->open(); 273 if (res) { 274 log_error("uart_block init failed %u", res); 275 download_complete(res); 276 return; 277 } 278 279 download_count = 0; 280 atwilc3000_start(); 281 } 282 283 static const btstack_chipset_t btstack_chipset_atwilc3000 = { 284 "atwilc3000", 285 NULL, // chipset_init not used 286 NULL, // chipset_next_command not used 287 atwilc3000_set_baudrate_command, 288 atwilc3000_set_bd_addr_command, 289 }; 290 291 // MARK: public API 292 const btstack_chipset_t * btstack_chipset_atwilc3000_instance(void){ 293 return &btstack_chipset_atwilc3000; 294 } 295 296