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 IRAM_START 0x80000000 58 59 // HCI commands 60 static const uint8_t hci_reset_command[] = { 0x01, 0x03, 0x0c, 0x00 }; 61 static const uint8_t hci_read_local_version_information_command[] = { 0x01, 0x01, 0x10, 0x00 }; 62 static const uint8_t hci_vendor_specific_reset_command[] = { 0x01, 0x55, 0xfc, 0x00 }; 63 64 // prototypes 65 static void atwilc3000_w4_command_complete_reset(void); 66 static void atwilc3000_w4_command_complete_read_local_version_information(void); 67 static void atwilc3000_write_memory(void); 68 static void atwilc3000_vendor_specific_reset(void); 69 static void atwilc3000_done(void); 70 71 // globals 72 static void (*download_complete)(int result); 73 static const btstack_uart_block_t * the_uart_driver; 74 75 static int download_count; 76 static uint8_t event_buffer[15]; 77 static uint8_t command_buffer[260]; 78 static const uint8_t * fw_data; 79 static uint32_t fw_size; 80 static uint32_t fw_offset; 81 82 static void atwilc3000_send_command(const uint8_t * data, uint16_t len){ 83 hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, (uint8_t *) &data[1], len - 1); 84 the_uart_driver->send_block(data, len); 85 } 86 87 static void atwilc3000_log_event(void){ 88 int len = event_buffer[2] + 2; 89 hci_dump_packet(HCI_EVENT_PACKET, 1, &event_buffer[1], len); 90 } 91 92 static void atwilc3000_start(void){ 93 // send HCI Reset 94 the_uart_driver->set_block_received(&atwilc3000_w4_command_complete_reset); 95 the_uart_driver->receive_block(&event_buffer[0], 7); 96 atwilc3000_send_command(&hci_reset_command[0], sizeof(hci_reset_command)); 97 log_info("atwilc3000_start: wait for command complete for HCI Reset"); 98 } 99 100 static void atwilc3000_w4_command_complete_reset(void){ 101 atwilc3000_log_event(); 102 log_info("command complete Reset"); 103 // static uint8_t hci_event_command_complete_reset[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x0c }; 104 // TODO: check if correct event 105 106 // send HCI Read Local Version Information 107 the_uart_driver->receive_block(&event_buffer[0], 15); 108 the_uart_driver->set_block_received(&atwilc3000_w4_command_complete_read_local_version_information); 109 atwilc3000_send_command(&hci_read_local_version_information_command[0], sizeof(hci_read_local_version_information_command)); 110 log_info("atwilc3000_start: wait for command complete for HCI Read Local Version Information"); 111 } 112 113 static void atwilc3000_w4_command_complete_read_local_version_information(void){ 114 atwilc3000_log_event(); 115 log_info("command complete Read Local Version Information"); 116 uint8_t firmware_version = event_buffer[7]; 117 log_info("Firmware version 0x%02x", firmware_version); 118 if (firmware_version != 0xff){ 119 log_info("Firmware already loaded, download complete"); 120 download_complete(0); 121 return; 122 } 123 log_info("Running from ROM, start firmware download"); 124 // TODO: check if firmware download can be skipped 125 atwilc3000_write_memory(); 126 } 127 128 static void atwilc3000_write_memory(void){ 129 atwilc3000_log_event(); 130 131 // done? 132 if (fw_offset >= fw_size){ 133 log_info("DONE!!!"); 134 atwilc3000_vendor_specific_reset(); 135 return; 136 } 137 // bytes to write 138 log_info("Write pos %u", fw_offset); 139 uint16_t bytes_to_write = btstack_min((fw_size - fw_offset), (255-8)); 140 // setup write command 141 command_buffer[0] = 1; 142 command_buffer[1] = 0x52; 143 command_buffer[2] = 0xfc; 144 command_buffer[3] = 8; // NOTE: this is in violation of the Bluetooth Specification, but documented in the Atmel-NNNNN-ATWIL_Linux_Porting_Guide 145 little_endian_store_32(command_buffer, 4, IRAM_START + fw_offset); 146 little_endian_store_32(command_buffer, 8, bytes_to_write); 147 memcpy(&command_buffer[12], &fw_data[fw_offset], bytes_to_write); 148 // 149 fw_offset += bytes_to_write; 150 151 // send write command 152 the_uart_driver->receive_block(&event_buffer[0], 7); 153 the_uart_driver->set_block_received(&atwilc3000_write_memory); 154 atwilc3000_send_command(&command_buffer[0], 12 + bytes_to_write); 155 } 156 157 static void atwilc3000_vendor_specific_reset(void){ 158 // send HCI Vendor Specific Reset 159 // the_uart_driver->receive_block(&event_buffer[0], 7); 160 // the_uart_driver->set_block_received(&atwilc3000_done); 161 the_uart_driver->set_block_sent(&atwilc3000_done); 162 atwilc3000_send_command(&hci_vendor_specific_reset_command[0], sizeof(hci_vendor_specific_reset_command)); 163 } 164 165 static void atwilc3000_done(void){ 166 log_info("done"); 167 download_complete(0); 168 } 169 170 void btstack_chipset_atwilc3000_download_firmware(const btstack_uart_block_t * uart_driver, const uint8_t * da_fw_data, uint32_t da_fw_size, void (*done)(int result)){ 171 172 the_uart_driver = uart_driver; 173 download_complete = done; 174 fw_data = da_fw_data; 175 fw_size = da_fw_size; 176 fw_offset = 0; 177 178 int res = the_uart_driver->open(); 179 180 if (res) { 181 log_error("uart_block init failed %u", res); 182 download_complete(res); 183 } 184 185 download_count = 0; 186 atwilc3000_start(); 187 } 188 189 // not used currently 190 191 static const btstack_chipset_t btstack_chipset_atwilc3000 = { 192 "atwilc3000", 193 NULL, // chipset_init not used 194 NULL, // chipset_next_command not used 195 NULL, // chipset_set_baudrate_command not needed as we're connected via SPI 196 NULL, // chipset_set_bd_addr not provided 197 }; 198 199 // MARK: public API 200 const btstack_chipset_t * btstack_chipset_atwilc3000_instance(void){ 201 return &btstack_chipset_atwilc3000; 202 } 203 204