7
7
#include <stdlib.h>
8
8
#include <string.h>
9
9
#include <stdbool.h>
10
+ #include <stdint.h>
11
+
12
+ #define le_uint16_t le_uint16_t
13
+ #define le_uint32_t le_uint32_t
14
+ #define le_int32_t le_int32_t
15
+
16
+ typedef struct { uint8_t v [2 ]; } le_uint16_t ;
17
+ typedef struct { uint8_t v [4 ]; } le_uint32_t ;
18
+ typedef le_uint32_t le_int32_t ;
10
19
11
20
#include "picoboot_connection.h"
12
21
@@ -25,6 +34,16 @@ static bool verbose;
25
34
#define PRODUCT_ID_PICOPROBE 0x0004u
26
35
#define PRODUCT_ID_MICROPYTHON 0x0005u
27
36
37
+ static uint32_t le32_to_host (le_uint32_t le )
38
+ {
39
+ return le .v [0 ] | (le .v [1 ] << 8 ) | (le .v [2 ] << 16 ) | (le .v [3 ] << 24 );
40
+ }
41
+
42
+ static le_uint32_t host_to_le32 (uint32_t host )
43
+ {
44
+ return (le_uint32_t ){{ host , host >>8 , host >>16 , host >>24 }};
45
+ }
46
+
28
47
uint32_t crc32_for_byte (uint32_t remainder ) {
29
48
const uint32_t POLYNOMIAL = 0x4C11DB7 ;
30
49
remainder <<= 24u ;
@@ -200,8 +219,8 @@ int picoboot_cmd(libusb_device_handle *usb_device, struct picoboot_cmd *cmd, uin
200
219
int ret ;
201
220
202
221
static int token = 1 ;
203
- cmd -> dMagic = PICOBOOT_MAGIC ;
204
- cmd -> dToken = token ++ ;
222
+ cmd -> dMagic = host_to_le32 ( PICOBOOT_MAGIC ) ;
223
+ cmd -> dToken = host_to_le32 ( token ++ ) ;
205
224
ret = libusb_bulk_transfer (usb_device , out_ep , (uint8_t * ) cmd , sizeof (struct picoboot_cmd ), & sent , 3000 );
206
225
207
226
if (ret != 0 || sent != sizeof (struct picoboot_cmd )) {
@@ -214,22 +233,22 @@ int picoboot_cmd(libusb_device_handle *usb_device, struct picoboot_cmd *cmd, uin
214
233
timeout = one_time_bulk_timeout ;
215
234
one_time_bulk_timeout = 0 ;
216
235
}
217
- if (cmd -> dTransferLength != 0 ) {
218
- assert (buf_size >= cmd -> dTransferLength );
236
+ if (le32_to_host ( cmd -> dTransferLength ) != 0 ) {
237
+ assert (buf_size >= le32_to_host ( cmd -> dTransferLength ) );
219
238
if (cmd -> bCmdId & 0x80u ) {
220
- if (verbose ) output (" receive %d...\n" , cmd -> dTransferLength );
239
+ if (verbose ) output (" receive %d...\n" , ( int ) le32_to_host ( cmd -> dTransferLength ) );
221
240
int received = 0 ;
222
- ret = libusb_bulk_transfer (usb_device , in_ep , buffer , cmd -> dTransferLength , & received , timeout );
223
- if (ret != 0 || received != (int ) cmd -> dTransferLength ) {
224
- output (" ...failed to receive data %d %d/%d\n" , ret , received , cmd -> dTransferLength );
241
+ ret = libusb_bulk_transfer (usb_device , in_ep , buffer , le32_to_host ( cmd -> dTransferLength ) , & received , timeout );
242
+ if (ret != 0 || received != (int ) le32_to_host ( cmd -> dTransferLength ) ) {
243
+ output (" ...failed to receive data %d %d/%d\n" , ret , received , ( int ) le32_to_host ( cmd -> dTransferLength ) );
225
244
if (!ret ) ret = 1 ;
226
245
return ret ;
227
246
}
228
247
} else {
229
- if (verbose ) output (" send %d...\n" , cmd -> dTransferLength );
230
- ret = libusb_bulk_transfer (usb_device , out_ep , buffer , cmd -> dTransferLength , & sent , timeout );
231
- if (ret != 0 || sent != (int ) cmd -> dTransferLength ) {
232
- output (" ...failed to send data %d %d/%d\n" , ret , sent , cmd -> dTransferLength );
248
+ if (verbose ) output (" send %d...\n" , ( int ) le32_to_host ( cmd -> dTransferLength ) );
249
+ ret = libusb_bulk_transfer (usb_device , out_ep , buffer , le32_to_host ( cmd -> dTransferLength ) , & sent , timeout );
250
+ if (ret != 0 || sent != (int ) le32_to_host ( cmd -> dTransferLength ) ) {
251
+ output (" ...failed to send data %d %d/%d\n" , ret , sent , ( int ) le32_to_host ( cmd -> dTransferLength ) );
233
252
if (!ret ) ret = 1 ;
234
253
picoboot_cmd_status_verbose (usb_device , NULL , true);
235
254
return ret ;
@@ -242,10 +261,10 @@ int picoboot_cmd(libusb_device_handle *usb_device, struct picoboot_cmd *cmd, uin
242
261
uint8_t spoon [64 ];
243
262
if (cmd -> bCmdId & 0x80u ) {
244
263
if (verbose ) output ("zero length out\n" );
245
- ret = libusb_bulk_transfer (usb_device , out_ep , spoon , 1 , & received , cmd -> dTransferLength == 0 ? timeout : 3000 );
264
+ ret = libusb_bulk_transfer (usb_device , out_ep , spoon , 1 , & received , le32_to_host ( cmd -> dTransferLength ) == 0 ? timeout : 3000 );
246
265
} else {
247
266
if (verbose ) output ("zero length in\n" );
248
- ret = libusb_bulk_transfer (usb_device , in_ep , spoon , 1 , & received , cmd -> dTransferLength == 0 ? timeout : 3000 );
267
+ ret = libusb_bulk_transfer (usb_device , in_ep , spoon , 1 , & received , le32_to_host ( cmd -> dTransferLength ) == 0 ? timeout : 3000 );
249
268
}
250
269
return ret ;
251
270
}
@@ -256,7 +275,7 @@ int picoboot_exclusive_access(libusb_device_handle *usb_device, uint8_t exclusiv
256
275
cmd .bCmdId = PC_EXCLUSIVE_ACCESS ;
257
276
cmd .exclusive_cmd .bExclusive = exclusive ;
258
277
cmd .bCmdSize = sizeof (struct picoboot_exclusive_cmd );
259
- cmd .dTransferLength = 0 ;
278
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
260
279
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
261
280
}
262
281
@@ -265,7 +284,7 @@ int picoboot_exit_xip(libusb_device_handle *usb_device) {
265
284
if (verbose ) output ("EXIT_XIP\n" );
266
285
cmd .bCmdId = PC_EXIT_XIP ;
267
286
cmd .bCmdSize = 0 ;
268
- cmd .dTransferLength = 0 ;
287
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
269
288
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
270
289
}
271
290
@@ -274,7 +293,7 @@ int picoboot_enter_cmd_xip(libusb_device_handle *usb_device) {
274
293
if (verbose ) output ("ENTER_CMD_XIP\n" );
275
294
cmd .bCmdId = PC_ENTER_CMD_XIP ;
276
295
cmd .bCmdSize = 0 ;
277
- cmd .dTransferLength = 0 ;
296
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
278
297
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
279
298
}
280
299
@@ -283,10 +302,10 @@ int picoboot_reboot(libusb_device_handle *usb_device, uint32_t pc, uint32_t sp,
283
302
if (verbose ) output ("REBOOT %08x %08x %u\n" , (uint ) pc , (uint ) sp , (uint ) delay_ms );
284
303
cmd .bCmdId = PC_REBOOT ;
285
304
cmd .bCmdSize = sizeof (cmd .reboot_cmd );
286
- cmd .dTransferLength = 0 ;
287
- cmd .reboot_cmd .dPC = pc ;
288
- cmd .reboot_cmd .dSP = sp ;
289
- cmd .reboot_cmd .dDelayMS = delay_ms ;
305
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
306
+ cmd .reboot_cmd .dPC = host_to_le32 ( pc ) ;
307
+ cmd .reboot_cmd .dSP = host_to_le32 ( sp ) ;
308
+ cmd .reboot_cmd .dDelayMS = host_to_le32 ( delay_ms ) ;
290
309
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
291
310
}
292
311
@@ -297,8 +316,8 @@ int picoboot_exec(libusb_device_handle *usb_device, uint32_t addr) {
297
316
if (verbose ) output ("EXEC %08x\n" , (uint ) addr );
298
317
cmd .bCmdId = PC_EXEC ;
299
318
cmd .bCmdSize = sizeof (cmd .address_only_cmd );
300
- cmd .dTransferLength = 0 ;
301
- cmd .address_only_cmd .dAddr = addr ;
319
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
320
+ cmd .address_only_cmd .dAddr = host_to_le32 ( addr ) ;
302
321
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
303
322
}
304
323
@@ -307,9 +326,9 @@ int picoboot_flash_erase(libusb_device_handle *usb_device, uint32_t addr, uint32
307
326
if (verbose ) output ("FLASH_ERASE %08x+%08x\n" , (uint ) addr , (uint ) len );
308
327
cmd .bCmdId = PC_FLASH_ERASE ;
309
328
cmd .bCmdSize = sizeof (cmd .range_cmd );
310
- cmd .range_cmd .dAddr = addr ;
311
- cmd .range_cmd .dSize = len ;
312
- cmd .dTransferLength = 0 ;
329
+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
330
+ cmd .range_cmd .dSize = host_to_le32 ( len ) ;
331
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
313
332
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
314
333
}
315
334
@@ -318,8 +337,8 @@ int picoboot_vector(libusb_device_handle *usb_device, uint32_t addr) {
318
337
if (verbose ) output ("VECTOR %08x\n" , (uint ) addr );
319
338
cmd .bCmdId = PC_VECTORIZE_FLASH ;
320
339
cmd .bCmdSize = sizeof (cmd .address_only_cmd );
321
- cmd .range_cmd .dAddr = addr ;
322
- cmd .dTransferLength = 0 ;
340
+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
341
+ cmd .dTransferLength = host_to_le32 ( 0 ) ;
323
342
return picoboot_cmd (usb_device , & cmd , NULL , 0 );
324
343
}
325
344
@@ -328,8 +347,8 @@ int picoboot_write(libusb_device_handle *usb_device, uint32_t addr, uint8_t *buf
328
347
if (verbose ) output ("WRITE %08x+%08x\n" , (uint ) addr , (uint ) len );
329
348
cmd .bCmdId = PC_WRITE ;
330
349
cmd .bCmdSize = sizeof (cmd .range_cmd );
331
- cmd .range_cmd .dAddr = addr ;
332
- cmd .range_cmd .dSize = cmd .dTransferLength = len ;
350
+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
351
+ cmd .range_cmd .dSize = cmd .dTransferLength = host_to_le32 ( len ) ;
333
352
return picoboot_cmd (usb_device , & cmd , buffer , len );
334
353
}
335
354
@@ -339,8 +358,8 @@ int picoboot_read(libusb_device_handle *usb_device, uint32_t addr, uint8_t *buff
339
358
struct picoboot_cmd cmd ;
340
359
cmd .bCmdId = PC_READ ;
341
360
cmd .bCmdSize = sizeof (cmd .range_cmd );
342
- cmd .range_cmd .dAddr = addr ;
343
- cmd .range_cmd .dSize = cmd .dTransferLength = len ;
361
+ cmd .range_cmd .dAddr = host_to_le32 ( addr ) ;
362
+ cmd .range_cmd .dSize = cmd .dTransferLength = host_to_le32 ( len ) ;
344
363
int ret = picoboot_cmd (usb_device , & cmd , buffer , len );
345
364
if (!ret && len < 256 && verbose ) {
346
365
for (uint32_t i = 0 ; i < len ; i += 32 ) {
@@ -422,4 +441,4 @@ int picoboot_peek(libusb_device_handle *usb_device, uint32_t addr, uint32_t *dat
422
441
return ret ;
423
442
return picoboot_read (usb_device , PEEK_POKE_CODE_LOC + picoboot_peek_cmd_len , (uint8_t * ) data , sizeof (uint32_t ));
424
443
}
425
- #endif
444
+ #endif
0 commit comments