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