1 /* 2 * Copyright (C) 2023 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 BLUEKITCHEN 24 * GMBH 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_nxp.c" 39 40 #include "btstack_chipset_nxp.h" 41 #include "btstack_debug.h" 42 #include "btstack_event.h" 43 44 #include <stdio.h> 45 46 #ifdef _MSC_VER 47 // ignore deprecated warning for fopen 48 #pragma warning(disable : 4996) 49 #endif 50 51 // Firmware download protocol constants 52 #define NXP_V1_FW_REQ_PKT 0xa5 53 #define NXP_V1_CHIP_VER_PKT 0xaa 54 #define NXP_V3_FW_REQ_PKT 0xa7 55 #define NXP_V3_CHIP_VER_PKT 0xab 56 57 #define NXP_ACK_V1 0x5a 58 #define NXP_NAK_V1 0xbf 59 #define NXP_ACK_V3 0x7a 60 #define NXP_NAK_V3 0x7b 61 #define NXP_CRC_ERROR_V3 0x7c 62 63 // chip ids 64 #define NXP_CHIP_ID_W9098 0x5c03 65 #define NXP_CHIP_ID_IW416 0x7201 66 #define NXP_CHIP_ID_IW612 0x7601 67 68 #define NXP_MAX_RESEND_COUNT 5 69 70 // prototypes 71 static void nxp_w4_fw_req(void); 72 static void nxp_done(void); 73 static void nxp_prepare_chunk(void); 74 static void nxp_send_chunk(btstack_timer_source_t * ts); 75 static void nxp_done_with_status(uint8_t status); 76 77 // globals 78 static void (*nxp_download_complete)(uint8_t status); 79 static const btstack_uart_t * nxp_uart_driver; 80 static btstack_timer_source_t nxp_timer; 81 static bool nxp_have_firmware; 82 83 static const uint8_t * nxp_fw_data; 84 static uint32_t nxp_fw_size; 85 static uint32_t nxp_fw_offset; 86 87 static uint8_t nxp_response_buffer[5]; 88 static const uint8_t nxp_ack_buffer_v1[] = {NXP_ACK_V1 }; 89 static const uint8_t nxp_ack_buffer_v3[] = {NXP_ACK_V3, 0x92 }; 90 static uint16_t nxp_fw_request_len; 91 static uint8_t nxp_fw_resend_count; 92 static uint8_t nxp_output_buffer[2048 + 1]; 93 94 #ifdef HAVE_POSIX_FILE_IO 95 static char nxp_firmware_path[1000]; 96 static FILE * nxp_firmware_file; 97 98 static void nxp_load_firmware(void) { 99 if (nxp_firmware_file == NULL){ 100 log_info("chipset-bcm: open file %s", nxp_firmware_path); 101 nxp_firmware_file = fopen(nxp_firmware_path, "rb"); 102 if (nxp_firmware_file != NULL){ 103 nxp_have_firmware = true; 104 } 105 } 106 107 } 108 static uint16_t nxp_read_firmware(uint16_t bytes_to_read, uint8_t * buffer) { 109 size_t bytes_read = fread(buffer, 1, bytes_to_read, nxp_firmware_file); 110 return bytes_read; 111 } 112 113 static void nxp_unload_firmware(void) { 114 btstack_assert(nxp_firmware_file != NULL); 115 fclose(nxp_firmware_file); 116 nxp_firmware_file = NULL; 117 } 118 #else 119 void nxp_load_firmware(void){ 120 nxp_have_firmware = true; 121 } 122 123 // read bytes from firmware file 124 static uint16_t nxp_read_firmware(uint16_t bytes_to_read, uint8_t * buffer){ 125 if (nxp_fw_request_len > nxp_fw_size - nxp_fw_offset){ 126 printf("fw_request_len %u > remaining file len %u\n", nxp_fw_request_len, nxp_fw_size - nxp_fw_offset); 127 return nxp_fw_size - nxp_fw_offset; 128 } 129 memcpy(buffer, &nxp_fw_data[nxp_fw_offset], bytes_to_read); 130 nxp_fw_offset += nxp_fw_request_len; 131 return bytes_to_read; 132 } 133 static void nxp_unload_firmware(void) { 134 } 135 #endif 136 137 // first two uint16_t should xor to 0xffff 138 static bool nxp_valid_packet(void){ 139 switch (nxp_response_buffer[0]){ 140 case NXP_V1_FW_REQ_PKT: 141 case NXP_V1_CHIP_VER_PKT: 142 return ((nxp_response_buffer[1] ^ nxp_response_buffer[3]) == 0xff) && ((nxp_response_buffer[2] ^ nxp_response_buffer [4]) == 0xff); 143 case NXP_V3_FW_REQ_PKT: 144 case NXP_V3_CHIP_VER_PKT: 145 // TODO: check crc-7 146 return true; 147 default: 148 return false; 149 } 150 } 151 152 static void nxp_start(){ 153 // start to read 154 nxp_fw_resend_count = 0; 155 nxp_fw_offset = 0; 156 nxp_have_firmware = false; 157 nxp_uart_driver->set_block_received(&nxp_w4_fw_req); 158 nxp_uart_driver->receive_block(nxp_response_buffer, 5); 159 log_info("nxp_start: wait for 0x%02x", NXP_V1_FW_REQ_PKT); 160 } 161 162 static void nxp_send_ack_v1(void (*done_handler)(void)){ 163 nxp_uart_driver->set_block_sent(done_handler); 164 nxp_uart_driver->send_block(nxp_ack_buffer_v1, sizeof(nxp_ack_buffer_v1)); 165 } 166 167 static void nxp_send_ack_v3(void (*done_handler)(void)){ 168 nxp_uart_driver->set_block_sent(done_handler); 169 nxp_uart_driver->send_block(nxp_ack_buffer_v3, sizeof(nxp_ack_buffer_v3)); 170 } 171 172 static void nxp_prepare_chunk(void){ 173 // delay chunk send by 5 ms 174 btstack_run_loop_set_timer_handler(&nxp_timer, nxp_send_chunk); 175 btstack_run_loop_set_timer(&nxp_timer, 5); 176 btstack_run_loop_add_timer(&nxp_timer); 177 } 178 179 static void nxp_dummy(void){ 180 } 181 182 static void nxp_w4_fw_req(void){ 183 // validate checksum 184 if (nxp_valid_packet()){ 185 printf("RECV: "); 186 printf_hexdump(nxp_response_buffer, sizeof(nxp_response_buffer)); 187 switch (nxp_response_buffer[0]){ 188 case NXP_V1_FW_REQ_PKT: 189 // get firmware 190 if (nxp_have_firmware == false){ 191 nxp_load_firmware(); 192 } 193 if (nxp_have_firmware == false){ 194 printf("No firmware found, abort\n"); 195 break; 196 } 197 nxp_fw_request_len = little_endian_read_16(nxp_response_buffer, 1); 198 printf("RECV: NXP_V1_FW_REQ_PKT, len %u\n", nxp_fw_request_len); 199 if (nxp_fw_request_len == 0){ 200 printf("last chunk sent!\n"); 201 nxp_unload_firmware(); 202 nxp_send_ack_v1(nxp_done); 203 } else { 204 nxp_send_ack_v1(nxp_prepare_chunk); 205 } 206 return; 207 case NXP_V1_CHIP_VER_PKT: 208 printf("RECV: NXP_V1_CHIP_VER_PKT, id = 0x%x02, revision = 0x%02x\n", nxp_response_buffer[0], nxp_response_buffer[1]); 209 nxp_send_ack_v1(nxp_dummy); 210 break; 211 case NXP_V3_CHIP_VER_PKT: 212 printf("RECV: NXP_V3_CHIP_VER_PKT , id = 0x%04x, loader 0x%02x\n", little_endian_read_16(nxp_response_buffer, 1), nxp_response_buffer[3]); 213 nxp_send_ack_v3(nxp_dummy); 214 break; 215 default: 216 printf("RECV: unknown packet type 0x%02x\n", nxp_response_buffer[0]); 217 break; 218 } 219 nxp_start(); 220 } 221 // drop byte and read another byte 222 memmove(&nxp_response_buffer[0], &nxp_response_buffer[1], 4); 223 nxp_uart_driver->receive_block(&nxp_response_buffer[4], 1); 224 } 225 226 static void nxp_send_chunk(btstack_timer_source_t * ts){ 227 if ((nxp_fw_request_len & 1) == 0){ 228 // update sttate 229 nxp_fw_offset += nxp_fw_request_len; 230 nxp_fw_resend_count = 0; 231 // read next firmware chunk 232 uint16_t bytes_read = nxp_read_firmware(nxp_fw_request_len, nxp_output_buffer); 233 if (bytes_read < nxp_fw_request_len){ 234 printf("only %u of %u bytes available, abort.\n", bytes_read, nxp_fw_request_len); 235 nxp_done_with_status(ERROR_CODE_HARDWARE_FAILURE); 236 return; 237 } 238 } else { 239 // resend last chunk if request len is odd 240 if (nxp_fw_resend_count >= NXP_MAX_RESEND_COUNT){ 241 printf("Resent last block %u times, abort.", nxp_fw_resend_count); 242 nxp_done_with_status(ERROR_CODE_HARDWARE_FAILURE); 243 return; 244 } 245 nxp_fw_resend_count++; 246 } 247 248 printf("SEND: firmware %08x - %u bytes (%u. try)\n", nxp_fw_offset, nxp_fw_request_len, nxp_fw_resend_count + 1); 249 nxp_uart_driver->set_block_received(&nxp_w4_fw_req); 250 nxp_uart_driver->receive_block(nxp_response_buffer, 5); 251 nxp_uart_driver->set_block_sent(nxp_dummy); 252 nxp_uart_driver->send_block(nxp_output_buffer, nxp_fw_request_len); 253 } 254 255 static void nxp_done_with_status(uint8_t status){ 256 printf("DONE!\n"); 257 (*nxp_download_complete)(status); 258 } 259 260 static void nxp_done(void){ 261 nxp_done_with_status(ERROR_CODE_SUCCESS); 262 } 263 264 void btstack_chipset_nxp_set_v1_firmware_path(const char * firmware_path){ 265 btstack_strcpy(nxp_firmware_path, sizeof(nxp_firmware_path), firmware_path); 266 } 267 268 void btstack_chipset_nxp_set_firmware(const uint8_t * fw_data, uint32_t fw_size){ 269 nxp_fw_data = fw_data; 270 nxp_fw_size = fw_size; 271 } 272 273 void btstack_chipset_nxp_download_firmware_with_uart(const btstack_uart_t *uart_driver, void (*done)(uint8_t status)) { 274 nxp_uart_driver = uart_driver; 275 nxp_download_complete = done; 276 277 int res = nxp_uart_driver->open(); 278 279 if (res) { 280 log_error("uart_block init failed %u", res); 281 nxp_download_complete(res); 282 } 283 284 nxp_start(); 285 } 286 287 288 static void chipset_init(const void *transport_config){ 289 UNUSED(transport_config); 290 } 291 292 static btstack_chipset_t btstack_chipset_nxp = { 293 .name = "NXP", 294 .init = chipset_init, 295 .next_command = NULL, 296 .set_baudrate_command = NULL, 297 .set_bd_addr_command = NULL 298 }; 299 300 const btstack_chipset_t *btstack_chipset_nxp_instance(void){ 301 return &btstack_chipset_nxp; 302 } 303