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