forked from gicking/stm8gal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.c
1793 lines (1484 loc) · 57.1 KB
/
bootloader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
\file bootloader.c
\author G. Icking-Konert
\date 2014-03-14
\version 0.1
\brief implementation of STM8 bootloader routines
implementation of STM8 bootloader routines
*/
#include "bootloader.h"
#include "main.h"
#include "hexfile.h"
#include "serial_comm.h"
#include "spi_spidev_comm.h"
#include "spi_Arduino_comm.h"
#include "misc.h"
#if defined(__ARMEL__) && defined(USE_WIRING)
#include <wiringPi.h>
#endif
// device dependent flash w/e routines (from https://github.com/basilhussain/stm8-bootloader-erase-write)
#include "erase_write_verL_8k_1.0_inc.h"
#include "erase_write_ver_32k_1.0_inc.h"
#include "erase_write_ver_32k_1.2_inc.h"
#include "erase_write_ver_32k_1.3_inc.h"
#include "erase_write_ver_128k_2.0_inc.h"
#include "erase_write_ver_128k_2.1_inc.h"
#include "erase_write_ver_128k_2.2_inc.h"
/**
\fn uint8_t bsl_sync(HANDLE ptrPort, uint8_t physInterface, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] physInterface bootloader interface: 0=UART (default), 1=SPI via Arduino, 2=SPI via SPIDEV
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return synchronization status (0=ok, 1=fail)
synchronize with microcontroller bootloader. For UART synchronize baudrate.
*/
uint8_t bsl_sync(HANDLE ptrPort, uint8_t physInterface, uint8_t verbose)
{
int i, count;
int lenTx, lenRx, len;
char Tx[1000], Rx[1000];
// print message
if (verbose >= SILENT)
printf(" synchronize ... ");
fflush(stdout);
// init receive buffer
for (i=0; i<1000; i++)
Rx[i] = 0;
// check if port is open
if (!ptrPort)
Error("in 'bsl_sync()': port not open");
// purge UART input buffer and set low receive timeout
if (physInterface == UART)
{
flush_port(ptrPort);
set_timeout(ptrPort, 100);
}
// construct SYNC command. Note: SYNC has even parity -> works in all UART modes
lenTx = 1;
Tx[0] = SYNCH;
lenRx = 1;
// synchronize. Repeat until NACK received (problems re-synchronizing with ACK -> wait for NACK)
count = 0;
do
{
// send command
if (physInterface == UART)
len = send_port(ptrPort, 0, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_sync()': sending command failed (expect %d, sent %d)", lenTx, len);
// receive response
if (physInterface == UART)
{
len = receive_port(ptrPort, 0, lenRx, Rx);
if ((len==1) && (Rx[0]== Tx[0]))
{ // check for 1-wire echo
len = receive_port(ptrPort, 0, lenRx, Rx);
}
}
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
// increase retry counter
count++;
// avoid flooding the STM8
SLEEP(10);
} while ((count<50) && ((len!=lenRx) || ((Rx[0]!=ACK) && (Rx[0]!=NACK))));
// check if ok
if ((len==lenRx) && (Rx[0]==ACK))
{
if (verbose == SILENT)
printf("done\n");
else if (verbose > SILENT)
printf("done (ACK)\n");
}
else if ((len==lenRx) && (Rx[0]==NACK))
{
if (verbose == SILENT)
printf("done\n");
else if (verbose > SILENT)
printf("done (NACK)\n");
}
else if (len==lenRx)
Error("in 'bsl_sync()': wrong response 0x%02x from BSL", (uint8_t) (Rx[0]));
else
Error("in 'bsl_sync()': no response from BSL");
fflush(stdout);
// purge PC input buffer
flush_port(ptrPort);
// restore receive timeout
if (physInterface == UART)
set_timeout(ptrPort, TIMEOUT);
// return success
return(0);
} // bsl_sync
/**
\fn uint8_t bsl_getUartMode(HANDLE ptrPort, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return UART mode (0=duplex, 1=1-wire, 2=2-wire reply)
auto-detect UART bootloader mode (see AppNote UM0560). This information is required
to set the correct data parity and determine if local echo is required.
*/
uint8_t bsl_getUartMode(HANDLE ptrPort, uint8_t verbose)
{
int len, lenTx, lenRx;
char Tx[1000], Rx[1000];
uint8_t uartMode = 255;
// print message
if (verbose == CHATTY)
printf(" check UART mode ... ");
fflush(stdout);
// reduce timeout for faster check
set_timeout(ptrPort, 100);
// detect UART mode
set_parity(ptrPort, 2);
lenTx = 2; lenRx = 1;
Tx[0] = 0x00; Tx[1] = (Tx[0] ^ 0xFF); Rx[0] = 0x00;
do
{
len = send_port(ptrPort, 0, lenTx, Tx);
len = receive_port(ptrPort, 0, lenRx, Rx);
//printf("\nmode 1: %d 0x%02x\n", len, (uint8_t) (Rx[0]));
SLEEP(10);
} while (len==0);
// tested empirically...
if (Rx[0] == ACK) // UART mode 0: 2-wire duplex, no SW reply, even parity
{
uartMode = 0;
set_parity(ptrPort, 2);
}
else if (Rx[0] == Tx[0]) // UART mode 1: 1-wire reply, no SW reply, no parity
{
uartMode = 1;
set_parity(ptrPort, 0);
}
else if (Rx[0] == NACK) // UART mode 2: 2-wire reply, SW reply, no parity
{
uartMode = 2;
set_parity(ptrPort, 0);
}
else
Error("in 'bsl_getUartMode()': cannot determine UART mode");
// revert timeout
set_timeout(ptrPort, TIMEOUT);
// purge PC input buffer
flush_port(ptrPort);
// print message
if (verbose == CHATTY)
{
if (uartMode == 0)
printf("done (duplex)\n");
else if (uartMode == 1)
printf("done (1-wire)\n");
else
printf("done (2-wire reply)\n");
}
fflush(stdout);
// return found mode
return(uartMode);
} // bsl_getUartMode
/**
\fn uint8_t bsl_getInfo(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, int *flashsize, uint8_t *vers, uint8_t *family, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] physInterface bootloader interface: 0=UART (default), 1=SPI via Arduino, 2=SPI via SPIDEV
\param[in] uartMode UART bootloader mode: 0=duplex, 1=1-wire, 2=2-wire reply
\param[out] flashsize size of flashsize in kB (required for correct W/E routines)
\param[out] vers BSL version number (required for correct W/E routines)
\param[out] family STM8 family (STM8S=1, STM8L=2)
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return communication status (0=ok, 1=fail)
upload RAM routines required for flash write & erase. Respective RAM routines depend on device family, bootloader version and flash size.
Used RAM routines under OSS license are from https://github.com/basilhussain/stm8-bootloader-erase-write.
Optionally proprietary STM RAM routines are available from http://www.st.com/web/catalog/tools/FM147/CL1794/SC1807/SS1754/PF258008
*/
uint8_t bsl_uploadWriteErase(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, int flashsize, uint8_t versBSL, uint8_t family, uint8_t verbose)
{
char *ptrRAM = NULL; // pointer to array with RAM routines
int lenRAM; // length of RAM array
uint16_t *imageBuf; // RAM image buffer (high byte != 0 indicates value is set)
uint64_t addrStart, addrStop, numData; // image data range
// STM8L >8kB does not need to upload RAM routines -> Skip
if ((family == STM8L) && (flashsize>8))
return(0);
// for STM8S and STM8L 8kB upload device dependent RAM routines
if ((flashsize==8) && (versBSL==0x10))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_8K_verL_1_0_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_verL_8k_1_0_ihx;
lenRAM = bin_erase_write_verL_8k_1_0_ihx_len;
}
else if ((flashsize==32) && (versBSL==0x10))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_0_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_ver_32k_1_0_ihx;
lenRAM = bin_erase_write_ver_32k_1_0_ihx_len;
}
else if ((flashsize==32) && (versBSL==0x12))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_2_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_ver_32k_1_2_ihx;
lenRAM = bin_erase_write_ver_32k_1_2_ihx_len;
}
else if ((flashsize==32) && (versBSL==0x13))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_3_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_ver_32k_1_3_ihx;
lenRAM = bin_erase_write_ver_32k_1_3_ihx_len;
}
else if (((flashsize==64) || (flashsize==128)) && (versBSL==0x20))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_0_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_ver_128k_2_0_ihx;
lenRAM = bin_erase_write_ver_128k_2_0_ihx_len;
}
else if (((flashsize==64) || (flashsize==128)) && (versBSL==0x21))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_1_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_ver_128k_2_1_ihx;
lenRAM = bin_erase_write_ver_128k_2_1_ihx_len;
}
else if (((flashsize==64) || (flashsize==128)) && (versBSL==0x22))
{
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_2_s19 \n");
#endif
ptrRAM = (char*) bin_erase_write_ver_128k_2_2_ihx;
lenRAM = bin_erase_write_ver_128k_2_2_ihx_len;
}
else
Error("unsupported device");
// allocate and init RAM image (>1MByte requires dynamic allocation)
if (!(imageBuf = malloc((LENIMAGEBUF + 1) * sizeof(*imageBuf))))
Error("Cannot allocate image buffer, try reducing LENIMAGEBUF");
memset(imageBuf, 0, (LENIMAGEBUF + 1) * sizeof(*imageBuf));
// convert correct array containing ihx file to RAM image
convert_ihx(ptrRAM, lenRAM, imageBuf, MUTE);
// get image size
get_image_size(imageBuf, 0, LENIMAGEBUF, &addrStart, &addrStop, &numData);
// upload RAM routines to STM8
if (verbose == CHATTY)
printf(" upload RAM routines ... ");
fflush(stdout);
bsl_memWrite(ptrPort, physInterface, uartMode, imageBuf, addrStart, addrStop, MUTE);
if (verbose == CHATTY)
printf("done (%dB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (int) numData, addrStart, addrStop);
fflush(stdout);
// release memory for image
free(imageBuf);
// return success
return(0);
} // bsl_uploadWriteErase
/**
\fn uint8_t bsl_getInfo(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, int *flashsize, uint8_t *versBSL, uint8_t *family, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] physInterface bootloader interface: 0=UART (default), 1=SPI via Arduino, 2=SPI via SPIDEV
\param[in] uartMode UART bootloader mode: 0=duplex, 1=1-wire, 2=2-wire reply
\param[out] flashsize size of flashsize in kB (required for correct W/E routines)
\param[out] versBSL BSL version number (required for correct W/E routines)
\param[out] family STM8 family (STM8S=1, STM8L=2)
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return communication status (0=ok, 1=fail)
query microcontroller type and BSL version info. This information is required
to select correct version of flash write/erase routines
*/
uint8_t bsl_getInfo(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, int *flashsize, uint8_t *versBSL, uint8_t *family, uint8_t verbose)
{
int i;
int lenTx, lenRx, len;
char Tx[1000], Rx[1000];
// print message
if (verbose >= SILENT)
printf(" get device info ... ");
fflush(stdout);
// init receive buffer
for (i=0; i<1000; i++)
Rx[i] = 0;
// check if port is open
if (!ptrPort)
Error("in 'bsl_getInfo()': port not open");
// purge input buffer
flush_port(ptrPort);
/////////
// determine device flash size for selecting w/e routines (flash starts at PFLASH_START)
/////////
// reduce timeout for faster check
if (physInterface == UART)
{
set_timeout(ptrPort, 200);
}
// check address of EEPROM. STM8L starts at 0x1000, STM8S starts at 0x4000
if (bsl_memCheck(ptrPort, physInterface, uartMode, 0x004000, SILENT)) // STM8S
{
*family = STM8S;
#ifdef DEBUG
printf("family STM8S\n");
#endif
}
else if (bsl_memCheck(ptrPort, physInterface, uartMode, 0x00100, SILENT)) // STM8L
{
*family = STM8L;
#ifdef DEBUG
printf("family STM8L\n");
#endif
}
else
Error("in 'bsl_getInfo()': cannot identify family");
// check if adress in flash exists. Check highest flash address to determine size
if (bsl_memCheck(ptrPort, physInterface, uartMode, 0x047FFF, SILENT)) // extreme density (256kB)
*flashsize = 256;
else if (bsl_memCheck(ptrPort, physInterface, uartMode, 0x027FFF, SILENT)) // high density (128kB)
*flashsize = 128;
else if (bsl_memCheck(ptrPort, physInterface, uartMode, 0x00FFFF, SILENT)) // medium density (32kB)
*flashsize = 32;
else if (bsl_memCheck(ptrPort, physInterface, uartMode, 0x009FFF, SILENT)) // low density (8kB)
*flashsize = 8;
else
Error("in 'bsl_getInfo()': cannot identify device");
#ifdef DEBUG
printf("flash size: %d\n", (int) (*flashsize));
#endif
// restore timeout to avoid timeouts during flash operation
if (physInterface == UART)
{
set_timeout(ptrPort, TIMEOUT);
}
/////////
// get BSL version
/////////
// construct command
lenTx = 2;
Tx[0] = GET;
Tx[1] = (Tx[0] ^ 0xFF);
lenRx = 9;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_getInfo()': sending command failed (expect %d, sent %d)", lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
if (len != lenRx)
Error("in 'bsl_getInfo()': ACK timeout (expect %d, received %d)", lenRx, len);
// check 2x ACKs
if (Rx[0]!=ACK)
Error("in 'bsl_getInfo()': start ACK failure (expect 0x%02x, read 0x%02x)", (uint8_t) ACK, (uint8_t) (Rx[0]));
if (Rx[8]!=ACK)
Error("in 'bsl_getInfo()': end ACK failure (expect 0x%02x, read 0x%02x)", (uint8_t) ACK, (uint8_t) (Rx[8]));
// check if command codes are correct (just to be sure)
if (Rx[3] != GET)
Error("in 'bsl_getInfo()': wrong GET code (expect 0x%02x, received 0x%02x)", (uint8_t) GET, (uint8_t) (Rx[3]));
if (Rx[4] != READ)
Error("in 'bsl_getInfo()': wrong READ code (expect 0x%02x, received 0x%02x)", (uint8_t) READ, (uint8_t) (Rx[4]));
if (Rx[5] != GO)
Error("in 'bsl_getInfo()': wrong GO code (expect 0x%02x, received 0x%02x)", (uint8_t) GO, (uint8_t) (Rx[5]));
if (Rx[6] != WRITE)
Error("in 'bsl_getInfo()': wrong WRITE code (expect 0x%02x, received 0x%02x)", (uint8_t) WRITE, (uint8_t) (Rx[6]));
if (Rx[7] != ERASE)
Error("in 'bsl_getInfo()': wrong ERASE code (expect 0x%02x, received 0x%02x)", (uint8_t) ERASE, (uint8_t) (Rx[7]));
// print BSL data
#ifdef DEBUG
printf(" version 0x%02x\n", (uint8_t) (Rx[2]));
printf(" command codes:\n");
printf(" GET 0x%02x\n", (uint8_t) (Rx[3]));
printf(" READ 0x%02x\n", (uint8_t) (Rx[4]));
printf(" GO 0x%02x\n", (uint8_t) (Rx[5]));
printf(" WRITE 0x%02x\n", (uint8_t) (Rx[6]));
printf(" ERASE 0x%02x\n", (uint8_t) (Rx[7]));
fflush(stdout);
#endif
// copy version number
*versBSL = Rx[2];
// print message
if (*family == STM8S)
{
if (verbose == SILENT)
printf("done (STM8S; %dkB)\n", *flashsize);
else if (verbose == INFORM)
printf("done (STM8S; %dkB flash)\n", *flashsize);
else if (verbose == CHATTY)
printf("done (STM8S; %dkB flash; BSL v%x.%x)\n", *flashsize, (((*versBSL)&0xF0)>>4), ((*versBSL) & 0x0F));
}
else
{
if (verbose == SILENT)
printf("done (STM8L; %dkB)\n", *flashsize);
else if (verbose == INFORM)
printf("done (STM8L; %dkB flash)\n", *flashsize);
else if (verbose == CHATTY)
printf("done (STM8L; %dkB flash; BSL v%x.%x)\n", *flashsize, (((*versBSL)&0xF0)>>4), ((*versBSL) & 0x0F));
}
fflush(stdout);
// avoid compiler warnings
return(0);
} // bsl_getInfo
/**
\fn uint8_t bsl_memRead(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, uint64_t addrStart, uint64_t addrStop, uint16_t *imageBuf, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] physInterface bootloader interface: 0=UART (default), 1=SPI via Arduino, 2=SPI via SPIDEV
\param[in] uartMode UART bootloader mode: 0=duplex, 1=1-wire, 2=2-wire reply
\param[in] addrStart first address to read
\param[in] addrStop last address to read
\param[out] imageBuf memory buffer containing read data (16-bit array. HB!=0 indicates content)
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return communication status (0=ok, 1=fail)
read from microcontroller memory via READ command.
*/
uint8_t bsl_memRead(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, uint64_t addrStart, uint64_t addrStop, uint16_t *imageBuf, uint8_t verbose)
{
int i, lenTx, lenRx, len;
char Tx[1000], Rx[1000];
uint64_t addr, addrStep, numBytes, countBytes;
// get number of bytes to read
numBytes = addrStop - addrStart + 1;
// print message
if (verbose == SILENT)
{
if (numBytes > 1024)
printf(" read %1.1fkB ", (float) numBytes/1024.0);
else
printf(" read %dB ", (int) numBytes);
}
else if (verbose == INFORM)
{
if (numBytes > 1024)
printf(" read %1.1fkB ", (float) numBytes/1024.0);
else
printf(" read %dB ", (int) numBytes);
}
else if (verbose == CHATTY)
{
if (numBytes > 1024)
printf(" read %1.1fkB in 0x%" PRIx64 " to 0x%" PRIx64 " ", (float) numBytes/1024.0, addrStart, addrStop);
else
printf(" read %dB in 0x%" PRIx64 " to 0x%" PRIx64 " ", (int) numBytes, addrStart, addrStop);
}
fflush(stdout);
// simple checks of scan window
if (addrStart > addrStop)
Error("start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, addrStart, addrStop);
if (addrStart > LENIMAGEBUF)
Error("start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStart, LENIMAGEBUF);
if (addrStop > LENIMAGEBUF)
Error("end address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStop, LENIMAGEBUF);
// init receive buffer
for (i=0; i<1000; i++)
Rx[i] = 0;
// check if port is open
if (!ptrPort)
Error("in 'bsl_memRead()': port not open");
// init data buffer
for (i=addrStart; i<=addrStop; i++)
imageBuf[i] = 0;
// loop over addresses in 128B steps (required by SPI via Arduino)
countBytes = 0;
addrStep = 128;
for (addr=addrStart; addr<=addrStop; addr+=addrStep)
{
// if addr too close to end of range reduce stepsize
if (addr+128 > addrStop)
addrStep = addrStop - addr + 1;
/////
// send read command
/////
// construct command
lenTx = 2;
Tx[0] = READ;
Tx[1] = (Tx[0] ^ 0xFF);
lenRx = 1;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_memRead()': at 0x%02x sending command failed (expect %d, sent %d)", addr, lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
if (len != lenRx)
Error("in 'bsl_memRead()': at 0x%02x ACK1 timeout", addr);
// check acknowledge
if (Rx[0]!=ACK)
Error("in 'bsl_memRead()': at 0x%02x ACK1 failure (expect 0x%02x, received 0x%02x)", addr,(uint8_t) ACK, (uint8_t) (Rx[0]));
/////
// send address
/////
// construct address + checksum (XOR over address)
lenTx = 5;
Tx[0] = (char) (addr >> 24);
Tx[1] = (char) (addr >> 16);
Tx[2] = (char) (addr >> 8);
Tx[3] = (char) (addr);
Tx[4] = (Tx[0] ^ Tx[1] ^ Tx[2] ^ Tx[3]);
lenRx = 1;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_memRead()': at 0x%02x sending address failed (expect %d, sent %d)", addr, lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
if (len != lenRx)
Error("in 'bsl_memRead()': at 0x%02x ACK2 timeout (expect %d, received %d)", addr, lenRx, len);
// check acknowledge
if (Rx[0]!=ACK)
Error("in 'bsl_memRead()': at 0x%02x ACK2 failure (expect 0x%02x, received 0x%02x)", addr, (uint8_t) ACK, (uint8_t) (Rx[0]));
/////
// send number of bytes
/////
// construct number of bytes + checksum
lenTx = 2;
Tx[0] = addrStep-1; // -1 from BSL
Tx[1] = (Tx[0] ^ 0xFF);
lenRx = addrStep + 1;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_memRead()': at 0x%02x sending range failed (expect %d, sent %d)", addr, lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
{
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
//fprintf(stderr, "%d\n", (int) lenRx);
}
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
{
len = receive_spi_spidev(ptrPort, lenRx, Rx);
//fprintf(stderr, "%d\n", (int) lenRx);
}
#endif
if (len != lenRx)
Error("in 'bsl_memRead()': at 0x%02x data timeout (expect %d, received %d)", addr, lenRx, len);
// check acknowledge
if (Rx[0]!=ACK)
Error("in 'bsl_memRead()': at 0x%02x ACK3 failure (expect 0x%02x, received 0x%02x)", addr, (uint8_t) ACK, (uint8_t) (Rx[0]));
// copy data to buffer. Set HB to indicate data read
for (i=1; i<lenRx; i++)
{
imageBuf[addr+i-1] = ((uint16_t) (Rx[i]) | 0xFF00);
//printf("%d 0x%02x\n", i, (uint8_t) (Rx[i])); fflush(stdout); getchar();
countBytes++;
}
// print progress
if ((countBytes % 1024) == 0)
{
if (verbose == SILENT)
{
printf(".");
if ((countBytes % (10*1024)) == 0)
printf(" ");
}
else if (verbose == INFORM)
{
if (numBytes > 1024)
printf("%c read %1.1fkB / %1.1fkB ", '\r', (float) countBytes/1024.0, (float) numBytes/1024.0);
else
printf("%c read %dB / %dB ", '\r', (int) countBytes, (int) numBytes);
}
else if (verbose == CHATTY)
{
if (numBytes > 1024)
printf("%c read %1.1fkB / %1.1fkB from 0x%" PRIx64 " to 0x%" PRIx64 " ", '\r', (float) countBytes/1024.0, (float) numBytes/1024.0, addrStart, addrStop);
else
printf("%c read %dB / %dB from 0x%" PRIx64 " to 0x%" PRIx64 " ", '\r', (int) countBytes, (int) numBytes, addrStart, addrStop);
}
fflush(stdout);
}
} // loop over address range
// print message
if (verbose == SILENT)
printf(" done\n");
else if (verbose == INFORM)
{
if (numBytes > 1024)
printf("%c read %1.1fkB / %1.1fkB ... done \n", '\r', (float) countBytes/1024.0, (float) numBytes/1024.0);
else
printf("%c read %dB / %dB ... done \n", '\r', (int) countBytes, (int) numBytes);
}
else if (verbose == CHATTY)
{
if (numBytes > 1024)
printf("%c read %1.1fkB / %1.1fkB from 0x%" PRIx64 " to 0x%" PRIx64 " ... done \n", '\r', (float) countBytes/1024.0, (float) numBytes/1024.0, addrStart, addrStop);
else
printf("%c read %dB / %dB from 0x%" PRIx64 " to 0x%" PRIx64 " ... done \n", '\r', (int) countBytes, (int) numBytes, addrStart, addrStop);
}
fflush(stdout);
// avoid compiler warnings
return(0);
} // bsl_memRead
/**
\fn uint8_t bsl_memCheck(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, uint64_t addr, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] physInterface bootloader interface: 0=UART (default), 1=SPI via Arduino, 2=SPI via SPIDEV
\param[in] uartMode UART bootloader mode: 0=duplex, 1=1-wire, 2=2-wire reply
\param[in] addr address to check
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return communication status (0=ok, 1=fail)
check if microcontrolles address exists. Specifically read 1B from microcontroller
memory via READ command. If it fails, memory doesn't exist. Used to get STM8 type
*/
uint8_t bsl_memCheck(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, uint64_t addr, uint8_t verbose)
{
int i, lenTx, lenRx, len;
char Tx[1000], Rx[1000];
// init receive buffer
for (i=0; i<1000; i++)
Rx[i] = 0;
// check if port is open
if (!ptrPort)
Error("in 'bsl_memCheck()': port not open");
/////
// send read command
/////
// construct command
lenTx = 2;
Tx[0] = READ;
Tx[1] = (Tx[0] ^ 0xFF);
lenRx = 1;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_memCheck()': at 0x%02x sending command failed (expect %d, sent %d)", addr, lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
if (len != lenRx)
Error("in 'bsl_memCheck()': at 0x%02x ACK1 timeout (expect %d, received %d)", addr, lenRx, len);
// check acknowledge
if (Rx[0]!=ACK)
Error("in 'bsl_memCheck()': at 0x%02x ACK1 failure (expect 0x%02x, received 0x%02x)", addr, (uint8_t) ACK, (uint8_t) (Rx[0]));
/////
// send address
/////
// construct address + checksum (XOR over address)
lenTx = 5;
Tx[0] = (char) (addr >> 24);
Tx[1] = (char) (addr >> 16);
Tx[2] = (char) (addr >> 8);
Tx[3] = (char) (addr);
Tx[4] = (Tx[0] ^ Tx[1] ^ Tx[2] ^ Tx[3]);
lenRx = 1;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_memCheck()': at 0x%02x sending address failed (expect %d, sent %d)", addr, lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
if (len != lenRx)
Error("in 'bsl_memCheck()': at 0x%02x ACK2 timeout (expect %d, received %d)", addr, lenRx, len);
// check acknowledge -> on NACK memory cannot be read -> return 0
if (Rx[0]!=ACK)
{
return(0);
}
/////
// send number of bytes to read
/////
// construct number of bytes + checksum
lenTx = 2;
Tx[0] = 1-1; // -1 from BSL
Tx[1] = (Tx[0] ^ 0xFF);
lenRx = 2;
// send command
if (physInterface == UART)
len = send_port(ptrPort, uartMode, lenTx, Tx);
else if (physInterface == SPI_ARDUINO)
len = send_spi_Arduino(ptrPort, lenTx, Tx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = send_spi_spidev(ptrPort, lenTx, Tx);
#endif
if (len != lenTx)
Error("in 'bsl_memCheck()': at 0x%02x sending range failed (expect %d, sent %d)", addr, lenTx, len);
// receive response
if (physInterface == UART)
len = receive_port(ptrPort, uartMode, lenRx, Rx);
else if (physInterface == SPI_ARDUINO)
len = receive_spi_Arduino(ptrPort, lenRx, Rx);
#if defined(USE_SPIDEV)
else if (physInterface == SPI_SPIDEV)
len = receive_spi_spidev(ptrPort, lenRx, Rx);
#endif
if (len != lenRx)
Error("in 'bsl_memCheck()': at 0x%02x data timeout (expect %d, received %d)", addr, lenRx, len);
// check acknowledge
if (Rx[0]!=ACK)
Error("in 'bsl_memCheck()': at 0x%02x ACK3 failure (expect 0x%02x, received 0x%02x)", addr, (uint8_t) ACK, (uint8_t) (Rx[0]));
// memory read succeeded -> memory exists
return(1);
} // bsl_memCheck
/**
\fn uint8_t bsl_flashSectorErase(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, uint64_t addr, uint8_t verbose)
\param[in] ptrPort handle to communication port
\param[in] physInterface bootloader interface: 0=UART (default), 1=SPI via Arduino, 2=SPI via SPIDEV
\param[in] uartMode UART bootloader mode: 0=duplex, 1=1-wire, 2=2-wire reply
\param[in] addr adress within 1kB sector to erase
\param[in] verbose verbosity level (0=SILENT, 1=INFORM, 2=CHATTY)
\return communication status (0=ok, 1=fail)
sector erase for microcontroller flash. Use with care!
*/
uint8_t bsl_flashSectorErase(HANDLE ptrPort, uint8_t physInterface, uint8_t uartMode, uint64_t addr, uint8_t verbose)
{
int i;
int lenTx, lenRx, len;
char Tx[1000], Rx[1000];
uint8_t sector;
uint64_t tStart, tStop; // measure time [ms] for erase (for COMM timeout)
// calculate sector code
sector = (addr - PFLASH_START)/PFLASH_BLOCKSIZE;
// print message
if (verbose == SILENT)
{
printf(" erase sector ... ");
}
else if (verbose == INFORM)
{
printf(" erase flash sector %d ... ", (int) sector);
}
else if (verbose == CHATTY)
{
if (addr>0xFFFFFF)
printf(" erase flash sector %d @ 0x%" PRIx64 " ... ", (int) sector, addr);
else if (addr>0xFFFF)
printf(" erase flash sector %d @ 0x%" PRIx64 " ... ", (int) sector, addr);