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