xref: /btstack/chipset/em9301/btstack_chipset_em9301.c (revision d080547ccfb05a5e0344289a19c6fbf8bfd202d2)
1 /*
2  * Copyright (C) 2014 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 #define __BTSTACK_FILE__ "btstack_chipset_em9301.c"
39 
40 /*
41  *  btstack_chipset_em9301.c
42  *
43  *  Adapter to use em9301-based chipsets with BTstack
44  *
45  *  Allows to set public BD ADDR
46  */
47 
48 #include "btstack_config.h"
49 #include "btstack_chipset_em9301.h"
50 #include "btstack_debug.h"
51 
52 #include <stddef.h>   /* NULL */
53 #include <string.h>   /* memcpy */
54 #include "hci.h"
55 
56 #include "crc32.h"
57 
58 // should go to some common place
59 #define OPCODE(ogf, ocf) (ocf | ogf << 10)
60 
61 #define HCI_OPCODE_EM_WRITE_PATCH_START        (0xFC27)
62 #define HCI_OPCODE_EM_WRITE_PATCH_CONTINUE     (0xFC28)
63 #define HCI_OPCODE_EM_WRITE_PATCH_ABORT        (0xFC29)
64 #define HCI_OPCODE_EM_CPU_RESET                (0xFC32)
65 #define HCI_OPCODE_EM_PATCH_QUERY              (0xFC34)
66 
67 static const uint32_t baudrates[] = {
68 	      0,
69 	      0,
70 	      0,
71 	   9600,
72 	  14400,
73 	  19200,
74 	  28800,
75 	  38400,
76 	  57600,
77 	  76800,
78 	 115200,
79 	 230400,
80 	 460800,
81 	 921600,
82 	1843200,
83 };
84 
85 #if 0
86 // crc32 from 802.3 without table lookup to minimize code
87 uint32_t crc32(const uint8_t *data, uint16_t len) {
88    int i, j;
89    uint32_t byte, crc, mask;
90    crc = 0xFFFFFFFF;
91    for (i=0;i<len;i++){
92       byte = data[i];
93       crc = crc ^ byte;
94       for (j = 7; j >= 0; j--) {
95          mask = -(crc & 1);
96          crc = (crc >> 1) ^ (0xEDB88320 & mask);
97       }
98       i = i + 1;
99    }
100    return ~crc;
101 }
102 #endif
103 
104 #ifdef HAVE_EM9304_PATCH_CONTAINER
105 
106 extern const uint8_t   container_blob_data[];
107 extern const uint32_t  container_blob_size;
108 
109 static uint32_t container_blob_offset  = 0;
110 static uint32_t container_end;	// current container
111 static uint16_t patch_sequence_number;
112 static int      em_cpu_reset_sent;
113 
114 static enum {
115 	UPLOAD_IDLE,
116 	UPLOAD_ACTIVE,
117 } upload_state;
118 
119 #endif
120 
121 
122 static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer){
123     little_endian_store_16(hci_cmd_buffer, 0, OPCODE(OGF_VENDOR, 0x02));
124     hci_cmd_buffer[2] = 0x06;
125     reverse_bd_addr(addr, &hci_cmd_buffer[3]);
126 }
127 
128 static void chipset_set_baudrate_command(uint32_t baudrate, uint8_t *hci_cmd_buffer){
129 	// lookup baudrates
130 	int i;
131 	int found = 0;
132 	for (i=0 ; i < sizeof(baudrates)/sizeof(uint32_t) ; i++){
133 		if (baudrates[i] == baudrate){
134 			found = i;
135 			break;
136 		}
137 	}
138 	if (!found){
139 		log_error("Baudrate %u not found in table", baudrate);
140 		return;
141 	}
142     little_endian_store_16(hci_cmd_buffer, 0, OPCODE(OGF_VENDOR, 0x07));
143     hci_cmd_buffer[2] = 0x01;
144     hci_cmd_buffer[3] = i;
145 }
146 
147 #ifdef HAVE_EM9304_PATCH_CONTAINER
148 static void chipset_init(const void * config){
149 	UNUSED(config);
150 	container_blob_offset = 0;
151 	em_cpu_reset_sent = 0;
152 	upload_state = UPLOAD_IDLE;
153 }
154 
155 static btstack_chipset_result_t chipset_next_command(uint8_t * hci_cmd_buffer){
156 	log_info("pos %u, container end %u, blob size %u", container_blob_offset, container_end, container_blob_size);
157 
158     if (container_blob_offset >= container_blob_size) {
159     	if (0 == em_cpu_reset_sent){
160     		// send EM CPU Reset
161 		    little_endian_store_16(hci_cmd_buffer, 0, HCI_OPCODE_EM_CPU_RESET);
162 		    hci_cmd_buffer[2] = 0;
163 		    em_cpu_reset_sent = 1;
164 		    return BTSTACK_CHIPSET_VALID_COMMAND;
165     	} else {
166 	        return BTSTACK_CHIPSET_DONE;
167     	}
168     }
169 
170     uint32_t tag;
171     uint16_t bytes_to_upload;
172     uint32_t crc;
173 
174 	switch (upload_state){
175 		case UPLOAD_IDLE:
176 			// check for 'em93' tag
177 			tag = little_endian_read_32(container_blob_data, container_blob_offset);
178 			if (0x656d3933 != tag) {
179 				log_error("Expected 0x656d3933 ('em934') but got %08x", tag);
180 				return BTSTACK_CHIPSET_DONE;
181 			}
182 			// fetch info for current container
183 			container_end = container_blob_offset + little_endian_read_32(container_blob_data, container_blob_offset + 4);
184 			// start uploading (<= 59 bytes)
185 			patch_sequence_number = 1;
186 			bytes_to_upload = btstack_min(59, container_end - container_blob_offset);
187 			crc = crc32(&container_blob_data[container_blob_offset], bytes_to_upload);
188 			// build command
189 		    little_endian_store_16(hci_cmd_buffer, 0, HCI_OPCODE_EM_WRITE_PATCH_START);
190 		    hci_cmd_buffer[2] = 5 + bytes_to_upload;
191 		    hci_cmd_buffer[3] = 0;	// upload to iRAM1
192 		    little_endian_store_32(hci_cmd_buffer, 4, crc);
193 		    memcpy(&hci_cmd_buffer[8], &container_blob_data[container_blob_offset], bytes_to_upload);
194 		    container_blob_offset += bytes_to_upload;
195 		    if (container_blob_offset < container_end){
196 		    	upload_state = UPLOAD_ACTIVE;
197 		    }
198 		    return BTSTACK_CHIPSET_VALID_COMMAND;
199 		case UPLOAD_ACTIVE:
200 			// Upload next segement
201 			bytes_to_upload = btstack_min(58, container_end - container_blob_offset);
202 			crc = crc32(&container_blob_data[container_blob_offset], bytes_to_upload);
203 			// build command
204 		    little_endian_store_16(hci_cmd_buffer, 0, HCI_OPCODE_EM_WRITE_PATCH_CONTINUE);
205 		    hci_cmd_buffer[2] = 6 + bytes_to_upload;
206 		    little_endian_store_16(hci_cmd_buffer, 3, patch_sequence_number++);
207 		    little_endian_store_32(hci_cmd_buffer, 5, crc);
208 		    memcpy(&hci_cmd_buffer[9], &container_blob_data[container_blob_offset], bytes_to_upload);
209 		    container_blob_offset += bytes_to_upload;
210 		    if (container_blob_offset >= container_end){
211 		    	log_info("container done maybe another one");
212 		    	upload_state = UPLOAD_IDLE;
213 		    }
214 		    return BTSTACK_CHIPSET_VALID_COMMAND;
215 	}
216 	return BTSTACK_CHIPSET_DONE;
217 }
218 #endif
219 
220 static const btstack_chipset_t btstack_chipset_em9301 = {
221     "EM9301",
222 #ifdef HAVE_EM9304_PATCH_CONTAINER
223     chipset_init,
224     chipset_next_command,
225 #else
226     NULL,
227     NULL,
228 #endif
229     chipset_set_baudrate_command,
230     chipset_set_bd_addr_command,
231 };
232 
233 // MARK: public API
234 const btstack_chipset_t * btstack_chipset_em9301_instance(void){
235     return &btstack_chipset_em9301;
236 }
237