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