xref: /btstack/chipset/atwilc3000/btstack_chipset_atwilc3000.c (revision 6ca9a99a26ce79466b491fa168ec7ff4551c0f8d)
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 #define FIRMWARE_CHUNK_SIZE 4096
59 
60 // HCI commands
61 static const uint8_t hci_reset_command[] = { 0x01, 0x03, 0x0c, 0x00 };
62 static const uint8_t hci_read_local_version_information_command[] = { 0x01, 0x01, 0x10, 0x00 };
63 static const uint8_t hci_vendor_specific_reset_command[] = { 0x01, 0x55, 0xfc, 0x00 };
64 
65 // prototypes
66 static void atwilc3000_w4_command_complete_reset(void);
67 static void atwilc3000_w4_command_complete_read_local_version_information(void);
68 static void atwilc3000_write_memory(void);
69 static void atwilc3000_write_firmware(void);
70 static void atwilc3000_vendor_specific_reset(void);
71 static void atwilc3000_done(void);
72 static void atwilc3000_update_uart_params(void);
73 static void atwilc3000_w4_baudrate_update(void);
74 
75 // globals
76 static void (*download_complete)(int result);
77 static const btstack_uart_block_t * the_uart_driver;
78 
79 static int     download_count;
80 static uint8_t event_buffer[15];
81 static uint8_t command_buffer[12];
82 static const uint8_t * fw_data;
83 static uint32_t        fw_size;
84 static uint32_t        fw_offset;
85 static uint32_t        fw_baudrate;
86 
87 static void dummy(void){}
88 
89 static void atwilc3000_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
90     hci_cmd_buffer[0] = 0x53;
91     hci_cmd_buffer[1] = 0xfc;
92     hci_cmd_buffer[2] = 5;
93     little_endian_store_32(hci_cmd_buffer, 3, fw_baudrate);
94     hci_cmd_buffer[7] = 0;  // No flow control
95 }
96 
97 static void atwilc3000_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
98     hci_cmd_buffer[0] = 0x54;
99     hci_cmd_buffer[1] = 0xFC;
100     hci_cmd_buffer[2] = 0x06;
101     reverse_bd_addr(addr, &hci_cmd_buffer[3]);
102 }
103 
104 static void atwilc3000_send_command(const uint8_t * data, uint16_t len){
105     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, (uint8_t *) &data[1], len - 1);
106     the_uart_driver->send_block(data, len);
107 }
108 
109 static void atwilc3000_log_event(void){
110     int len = event_buffer[2] + 2;
111     hci_dump_packet(HCI_EVENT_PACKET, 1, &event_buffer[1], len);
112 }
113 
114 static void atwilc3000_start(void){
115     // send HCI Reset
116     the_uart_driver->set_block_received(&atwilc3000_w4_command_complete_reset);
117     the_uart_driver->receive_block(&event_buffer[0], 7);
118     atwilc3000_send_command(&hci_reset_command[0], sizeof(hci_reset_command));
119     log_info("atwilc3000_start: wait for command complete for HCI Reset");
120 }
121 
122 static void atwilc3000_w4_command_complete_reset(void){
123     atwilc3000_log_event();
124     log_info("command complete Reset");
125     // static uint8_t hci_event_command_complete_reset[] = { 0x04, 0x0e, 0x04, 0x01, 0x03, 0x0c, 0x0c };
126     // TODO: check if correct event
127 
128     // send HCI Read Local Version Information
129     the_uart_driver->receive_block(&event_buffer[0], 15);
130     the_uart_driver->set_block_received(&atwilc3000_w4_command_complete_read_local_version_information);
131     atwilc3000_send_command(&hci_read_local_version_information_command[0], sizeof(hci_read_local_version_information_command));
132     log_info("atwilc3000_start: wait for command complete for HCI Read Local Version Information");
133 }
134 
135 static void atwilc3000_w4_command_complete_read_local_version_information(void){
136     atwilc3000_log_event();
137     log_info("command complete Read Local Version Information");
138     uint8_t firmware_version = event_buffer[7];
139     log_info("Firmware version 0x%02x", firmware_version);
140     if (firmware_version != 0xff){
141         log_info("Firmware already loaded, download complete");
142         download_complete(0);
143         return;
144     }
145     log_info("Running from ROM, start firmware download");
146     if (fw_baudrate){
147         atwilc3000_update_uart_params();
148     } else {
149         atwilc3000_write_memory();
150     }
151 }
152 
153 static void atwilc3000_update_uart_params(void){
154     command_buffer[0] = 1;
155     atwilc3000_set_baudrate_command(fw_baudrate, &command_buffer[1]);
156     the_uart_driver->receive_block(&event_buffer[0], 7);
157     the_uart_driver->set_block_received(&atwilc3000_w4_baudrate_update);
158     atwilc3000_send_command(&command_buffer[0], 9);
159 }
160 
161 static void atwilc3000_w4_baudrate_update(void){
162     atwilc3000_log_event();
163     the_uart_driver->set_baudrate(fw_baudrate);
164     atwilc3000_write_memory();
165 }
166 
167 
168 static void atwilc3000_write_memory(void){
169     atwilc3000_log_event();
170 
171     // done?
172     if (fw_offset >= fw_size){
173         log_info("DONE!!!");
174         atwilc3000_vendor_specific_reset();
175         return;
176     }
177 
178     // bytes to write
179     log_info("Write pos %u", fw_offset);
180     uint16_t bytes_to_write = btstack_min((fw_size - fw_offset), FIRMWARE_CHUNK_SIZE);
181     // setup write command
182     command_buffer[0] = 1;
183     command_buffer[1] = 0x52;
184     command_buffer[2] = 0xfc;
185     command_buffer[3] = 8; // NOTE: this is in violation of the Bluetooth Specification, but documented in the Atmel-NNNNN-ATWIL_Linux_Porting_Guide
186     little_endian_store_32(command_buffer, 4, IRAM_START + fw_offset);
187     little_endian_store_32(command_buffer, 8, bytes_to_write);
188 
189     // send write command - only log write command without the firmware blob
190     hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, (uint8_t *) &command_buffer[1], 12 - 1);
191     the_uart_driver->set_block_sent(&atwilc3000_write_firmware);
192     the_uart_driver->send_block(&command_buffer[0], 12);
193 }
194 
195 static void atwilc3000_write_firmware(void){
196 
197     the_uart_driver->set_block_received(&atwilc3000_write_memory);
198     the_uart_driver->receive_block(&event_buffer[0], 7);
199 
200     uint16_t bytes_to_write = btstack_min((fw_size - fw_offset), FIRMWARE_CHUNK_SIZE);
201 
202     uint32_t offset = fw_offset;
203     fw_offset += bytes_to_write;
204 
205     the_uart_driver->set_block_sent(&dummy);
206     the_uart_driver->send_block(&fw_data[offset], bytes_to_write);
207 }
208 
209 static void atwilc3000_vendor_specific_reset(void){
210     // send HCI Vendor Specific Reset
211     // the_uart_driver->receive_block(&event_buffer[0], 7);
212     // the_uart_driver->set_block_received(&atwilc3000_done);
213     the_uart_driver->set_block_sent(&atwilc3000_done);
214     atwilc3000_send_command(&hci_vendor_specific_reset_command[0], sizeof(hci_vendor_specific_reset_command));
215 }
216 
217 static void atwilc3000_done(void){
218     log_info("done");
219     // reset baud rate
220     if (fw_baudrate){
221         the_uart_driver->set_baudrate(115200);
222     }
223     download_complete(0);
224 }
225 
226 void btstack_chipset_atwilc3000_download_firmware(const btstack_uart_block_t * uart_driver, uint32_t baudrate, const uint8_t * da_fw_data, uint32_t da_fw_size, void (*done)(int result)){
227 
228 	the_uart_driver   = uart_driver;
229     download_complete = done;
230     fw_data = da_fw_data;
231     fw_size = da_fw_size;
232     fw_offset = 0;
233     fw_baudrate = baudrate;
234 
235     int res = the_uart_driver->open();
236 
237     if (res) {
238     	log_error("uart_block init failed %u", res);
239     	download_complete(res);
240     }
241 
242     download_count = 0;
243     atwilc3000_start();
244 }
245 
246 // not used currently
247 
248 static const btstack_chipset_t btstack_chipset_atwilc3000 = {
249     "atwilc3000",
250     NULL, // chipset_init not used
251     NULL, // chipset_next_command not used
252     atwilc3000_set_baudrate_command,
253     atwilc3000_set_bd_addr_command,
254 };
255 
256 // MARK: public API
257 const btstack_chipset_t * btstack_chipset_atwilc3000_instance(void){
258     return &btstack_chipset_atwilc3000;
259 }
260 
261