forked from gicking/stm8gal
/
main.c
848 lines (720 loc) · 28.3 KB
/
main.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
/**
\file main.c
\author G. Icking-Konert
\date 2014-03-14
\version 0.1
\brief implementation of main routine
this is the main file containing browsing the input parameters,
calling the import, programming, and check routines.
\note program not yet fully tested!
*/
// include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
// OS specific: Win32
#if defined(WIN32)
#include <windows.h>
#include <malloc.h>
// OS specific: Posix
#elif defined(__APPLE__) || defined(__unix__)
#ifndef HANDLE
#define HANDLE int // comm port handler is int
#endif
#include <fcntl.h> // File control definitions
#include <termios.h> // Posix terminal control definitions
#include <getopt.h>
#include <errno.h> /* Error number definitions */
#include <dirent.h>
#include <sys/ioctl.h>
#if defined(__ARMEL__) && defined(USE_WIRING)
#include <wiringPi.h> // for reset via GPIO
#endif // __ARMEL__ && USE_WIRING
#else
#error OS not supported
#endif
#define _MAIN_
#include "globals.h"
#undef _MAIN_
#include "main.h"
#include "misc.h"
#include "serial_comm.h"
#include "spi_spidev_comm.h"
#include "spi_Arduino_comm.h"
#include "bootloader.h"
#include "hexfile.h"
#include "version.h"
// device dependent flash w/e routines
#include "E_W_ROUTINEs_8K_verL_1.0.h"
#include "E_W_ROUTINEs_32K_ver_1.0.h"
#include "E_W_ROUTINEs_32K_ver_1.2.h"
#include "E_W_ROUTINEs_32K_ver_1.3.h"
#include "E_W_ROUTINEs_32K_ver_1.4.h"
//#include "E_W_ROUTINEs_32K_verL_1.0.h" // empty
#include "E_W_ROUTINEs_128K_ver_2.0.h"
#include "E_W_ROUTINEs_128K_ver_2.1.h"
#include "E_W_ROUTINEs_128K_ver_2.2.h"
#include "E_W_ROUTINEs_128K_ver_2.4.h"
#include "E_W_ROUTINEs_256K_ver_1.0.h"
// buffer sizes
#define STRLEN 1000
#define BUFSIZE 10000000
/**
\fn int main(int argc, char *argv[])
\brief main routine
\param argc number of commandline arguments + 1
\param argv string array containing commandline arguments (argv[0] contains name of executable)
\return dummy return code (not used)
Main routine for import, programming, and check routines
*/
int main(int argc, char ** argv) {
char *appname; // name of application without path
uint8_t physInterface; // bootloader interface: 0=UART (default), 1=SPI via spidev, 2=SPI via Arduino
uint8_t uartMode; // UART bootloader mode: 0=duplex, 1=1-wire reply, 2=2-wire reply
char portname[STRLEN]; // name of communication port
HANDLE ptrPort; // handle to communication port
int baudrate; // communication baudrate [Baud]
uint8_t resetSTM8; // reset STM8: 0=skip, 1=manual, 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin 8, 5=Raspi pin 12 (default: manual)
uint8_t enableBSL; // don't enable ROM bootloader after upload (caution!)
uint8_t flashErase; // erase P-flash and D-flash prior to upload
uint8_t jumpFlash; // jump to flash after upload
uint8_t verifyUpload; // verify memory after upload
char *ptr=NULL; // pointer to memory
int i, j; // generic variables
char buf[1000]; // misc buffer
//char Tx[100], Rx[100]; // debug: buffer for tests
// STM8 propoerties
int flashsize; // size of flash (kB) for w/e routines
uint8_t versBSL; // BSL version for w/e routines
uint8_t family; // device family, currently STM8S and STM8L
// for upload to flash
char fileIn[STRLEN]; // name of file to upload to STM8
char *fileBufIn; // buffer for hexfiles
char *imageIn; // memory buffer for upload hexfile
uint32_t imageInStart; // starting address of imageIn
uint32_t imageInBytes; // number of bytes in imageIn
// for download from flash
char fileOut[STRLEN]; // name of file to download from STM8
char *imageOut; // memory buffer for download hexfile
uint32_t imageOutStart; // starting address of imageOut
uint32_t imageOutBytes; // number of bytes in imageOut
// initialize global variables
g_verbose = 2; // high verbosity
g_pauseOnExit = 0; // no wait for <return> before terminating
g_backgroungOperation = 0; // by default assume foreground application
// initialize default arguments
portname[0] = '\0'; // no default port name
physInterface = 0; // bootloader interface: 0=UART (default), 1=SPI via spidev, 2=SPI via Arduino
uartMode = 0; // UART bootloader mode: 0=duplex, 1=1-wire reply, 2=2-wire reply
baudrate = 230400; // default baudrate
resetSTM8 = 1; // manual reset of STM8
flashErase = 0; // erase P-flash and D-flash prior to upload
jumpFlash = 1; // jump to flash after uploade
enableBSL = 1; // enable bootloader after upload
verifyUpload = 1; // verify memory content after upload
fileIn[0] = '\0'; // no default file to upload to flash
fileOut[0] = '\0'; // no default file to download from flash
// required for strncpy()
portname[STRLEN-1] = '\0';
fileIn[STRLEN-1] = '\0';
fileOut[STRLEN-1] = '\0';
// allocate buffers (can't be static for large buffers)
imageIn = (char*) malloc(BUFSIZE);
imageOut = (char*) malloc(BUFSIZE);
fileBufIn = (char*) malloc(BUFSIZE);
if ((!imageIn) || (!imageOut) || (!fileBufIn)) {
setConsoleColor(PRM_COLOR_RED);
fprintf(stderr, "\n\nerror: cannot allocate memory buffers, exit!\n\n");
Exit(1, 1);
}
// debug: print arguments
/*
printf("\n\narguments:\n");
for (i=0; i<argc; i++) {
//printf(" %d: '%s'\n", (int) i, argv[i]);
printf("%s ", argv[i]);
}
printf("\n\n");
exit(1);
*/
////////
// parse commandline arguments
////////
for (i=1; i<argc; i++) {
// interface type: 0=UART (default); 1=SPI via spidev, 2=SPI via Arduino
if (!strcmp(argv[i], "-i")) {
if (i<argc-1) {
sscanf(argv[++i], "%d", &j);
physInterface = j;
}
}
// name of communication port
else if (!strcmp(argv[i], "-p")) {
if (i<argc-1)
strncpy(portname, argv[++i], STRLEN-1);
}
// communication baudrate
else if (!strcmp(argv[i], "-b")) {
if (i<argc-1)
sscanf(argv[++i],"%d",&baudrate);
}
// UART mode: 0=duplex, 1=1-wire reply, 2=2-wire reply (default: duplex)\n");
else if (!strcmp(argv[i], "-u")) {
if (i<argc-1) {
sscanf(argv[++i], "%d", &j);
uartMode = j;
}
}
// reset STM8 method: 0=skip, 1=manual; 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin 8, 5=Raspi pin 12
else if (!strcmp(argv[i], "-R")) {
if (i<argc-1) {
sscanf(argv[++i], "%d", &j);
resetSTM8 = j;
}
}
// erase P-flash and D-flash prior to upload
else if (!strcmp(argv[i], "-e")) {
flashErase = 1;
}
// name of file to upload
else if (!strcmp(argv[i], "-w")) {
if (i<argc-1)
strncpy(fileIn, argv[++i], STRLEN-1);
}
// don't enable ROM bootloader after upload (caution!)
else if (!strcmp(argv[i], "-x")) {
enableBSL = 0;
}
// skip verify memory content after upload
else if (!strcmp(argv[i], "-v")) {
verifyUpload = 0;
}
// memory range to read and file to save to
else if (!strcmp(argv[i], "-r")) {
if (i<argc-1) {
sscanf(argv[++i],"%x",&j);
imageOutStart = j;
}
if (i<argc-1) {
sscanf(argv[++i],"%x",&j);
imageOutBytes = j - imageOutStart + 1;
}
if (i<argc-1)
strncpy(fileOut, argv[++i], STRLEN-1);
}
// don't jump to address after upload
else if (!strcmp(argv[i], "-j")) {
jumpFlash = 0;
}
// verbosity level (0..2)
else if (!strcmp(argv[i], "-V")) {
if (i<argc-1)
sscanf(argv[++i],"%d",&g_verbose);
if (g_verbose < 0) g_verbose = 0;
if (g_verbose > 2) g_verbose = 2;
}
// optimize for background operation, e.g. skip prompts and colors
else if (!strcmp(argv[i], "-B")) {
g_backgroungOperation = 1;
}
// prompt for <return> prior to exit
else if (!strcmp(argv[i], "-q")) {
g_pauseOnExit = 1;
}
// else print list of commandline arguments and language commands
else {
if (strrchr(argv[0],'\\'))
appname = strrchr(argv[0],'\\')+1; // windows
else if (strrchr(argv[0],'/'))
appname = strrchr(argv[0],'/')+1; // Posix
else
appname = argv[0];
printf("\n");
printf("usage: %s [-h] [-i interface] [-p port] [-b rate] [-u mode] [-R ch] [-e] [-w infile] [-x] [-v] [-r start stop outfile] [-j] [-V verbose] [-B] [-q]\n", appname);
printf(" -h print this help\n");
#ifdef USE_SPIDEV
printf(" -i interface communication interface: 0=UART, 1=SPI via spidev, 2=SPI via Arduino (default: UART)\n");
#else
printf(" -i interface communication interface: 0=UART, 2=SPI via Arduino (default: UART)\n");
#endif
printf(" -p port name of communication port (default: list available ports)\n");
printf(" -b rate communication baudrate in Baud (default: 230400)\n");
printf(" -u mode UART mode: 0=duplex, 1=1-wire reply, 2=2-wire reply (default: duplex). For details see setion 2 in UM0560 AppNote\n");
#if defined(__ARMEL__) && defined(USE_WIRING)
printf(" -R ch reset STM8: 0=skip, 1=manual, 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin 8, 5=Raspi pin 12 (default: manual)\n");
#else
printf(" -R ch reset STM8: 0=skip, 1=manual, 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin 8 (default: manual)\n");
#endif
printf(" -e erase P-flash and D-flash prior to upload (default: skip)\n");
printf(" -w infile upload s19 or intel-hex file to flash (default: skip)\n");
printf(" -x don't enable ROM bootloader after upload (default: enable)\n");
printf(" -v don't verify code in flash after upload (default: verify)\n");
printf(" -r start stop outfile read memory range (in hex) to s19 file or table (default: skip)\n");
printf(" -j don't jump to flash before exit (default: jump to flash)\n");
printf(" -V verbosity level 0..2 (default: 2)\n");
printf(" -B optimize for background operation, e.g. skip prompts and colors (default: forefront use)\n");
printf(" -q prompt for <return> prior to exit (default: no prompt)\n");
printf("\n");
Exit(0, 0);
}
} // process commandline arguments
////////
// some parameter post-processing
////////
if ((physInterface == 1) || (physInterface == 2)) uartMode = 0; // echo mode is n/a for SPI
if ((physInterface == 1) || (physInterface == 2)) verifyUpload = 0; // read back after writing doesn't work for SPI (don't know why)
if (g_backgroungOperation) g_pauseOnExit = 0; // for background operation avoid prompt on exit
////////
// reset console color (needs to be called once for Win32)
////////
setConsoleColor(PRM_COLOR_DEFAULT);
////////
// print app name & version, and change console title
////////
get_app_name(argv[0], VERSION, buf);
printf("\n%s\n", buf);
setConsoleTitle(buf);
////////
// if no port name is given, list all available ports and query
////////
if (strlen(portname) == 0) {
if (!g_backgroungOperation) {
printf(" enter comm port name ( ");
list_ports();
printf(" ): ");
scanf("%s", portname);
getchar();
}
else {
printf(" available comm ports ( ");
list_ports();
printf(" ), exit!");
Exit(1, 0);
}
} // if no comm port name
// If specified import hexfile - do it early here to be able to report file read errors before others
if (strlen(fileIn) > 0) {
// strip path from filename for readability
#if defined(WIN32)
const char *shortname = strrchr(fileIn, '\\');
#else
const char *shortname = strrchr(fileIn, '/');
#endif
if (!shortname)
shortname = fileIn;
else
shortname++;
// convert to memory image, depending on file type
const char *dot = strrchr (fileIn, '.');
if (dot && (!strcmp(dot, ".s19") || !strcmp(dot, ".S19"))) {
if (g_verbose == 1)
printf(" load file '%s' ... ", shortname);
else if (g_verbose == 2)
printf(" load Motorola S-record file '%s' ... ", shortname);
fflush(stdout);
load_hexfile(fileIn, fileBufIn, BUFSIZE);
convert_s19(fileBufIn, &imageInStart, &imageInBytes, imageIn);
}
else if (dot && (!strcmp(dot, ".hex") || !strcmp(dot, ".HEX") || !strcmp(dot, ".ihx") || !strcmp(dot, ".IHX"))) {
if (g_verbose == 1)
printf(" load file '%s' ... ", shortname);
else if (g_verbose == 2)
printf(" load Intel hex file '%s' ... ", shortname);
fflush(stdout);
load_hexfile(fileIn, fileBufIn, BUFSIZE);
convert_hex(fileBufIn, &imageInStart, &imageInBytes, imageIn);
}
else {
if (g_verbose == 1)
printf(" load file '%s' ... ", shortname);
else if (g_verbose == 2)
printf(" load binary file '%s' ... ", shortname);
fflush(stdout);
load_binfile(fileIn, imageIn, &imageInStart, &imageInBytes, BUFSIZE);
}
// print size of imported data
if (g_verbose > 0) {
if (imageInBytes>2048)
printf("ok (%1.1fkB)\n", (float) imageInBytes/1024.0);
else if (imageInBytes>0)
printf("ok (%dB)\n", imageInBytes);
else
printf("ok, no data read\n");
fflush(stdout);
}
} // import hexfile
////////
// reset STM8
// Note: prior to opening port to avoid flushing issue under Linux, see https://stackoverflow.com/questions/13013387/clearing-the-serial-ports-buffer
////////
// skip reset of STM8
if (resetSTM8 == 0) {
}
// manually reset STM8
else if (resetSTM8 == 1) {
if (!g_backgroungOperation) {
printf(" reset STM8 and press <return>");
fflush(stdout);
fflush(stdin);
getchar();
}
else {
printf(" reset STM8 now\n");
fflush(stdout);
}
}
// HW reset STM8 using DTR line (USB/RS232)
else if (resetSTM8 == 2) {
printf(" reset via DTR ... ");
fflush(stdout);
pulse_DTR(ptrPort, 10);
printf("ok\n");
fflush(stdout);
SLEEP(20); // allow BSL to initialize
}
// SW reset STM8 via command 'Re5eT!' at 115.2kBaud (requires respective STM8 SW)
else if (resetSTM8 == 3) {
set_baudrate(ptrPort, 115200); // expect STM8 SW to receive at 115.2kBaud
printf(" reset via UART command ... ");
fflush(stdout);
sprintf(buf, "Re5eT!"); // reset command (same as in STM8 SW!)
for (i=0; i<6; i++) {
send_port(ptrPort, uartMode, 1, buf+i); // send reset command bytewise to account for possible slow handling on STM8 side
SLEEP(10);
}
printf("ok\n");
fflush(stdout);
set_baudrate(ptrPort, baudrate); // restore specified baudrate
SLEEP(20); // allow BSL to initialize
}
// HW reset STM8 using Arduino pin 8
else if (resetSTM8 == 4) {
printf(" reset via Arduino pin %d ... ", ARDUINO_RESET_PIN);
fflush(stdout);
setPin_Arduino(ptrPort, ARDUINO_RESET_PIN, 0);
SLEEP(1);
setPin_Arduino(ptrPort, ARDUINO_RESET_PIN, 1);
printf("ok\n");
fflush(stdout);
SLEEP(20); // allow BSL to initialize
}
// HW reset STM8 using header pin 12 (only Raspberry Pi!)
#if defined(__ARMEL__) && defined(USE_WIRING)
else if (resetSTM8 == 5) {
printf(" reset via Raspi pin 12 ... ");
fflush(stdout);
pulse_GPIO(12, 20);
printf("ok\n");
fflush(stdout);
SLEEP(20); // allow BSL to initialize
}
#endif // __ARMEL__ && USE_WIRING
// unknown reset method -> error
else {
setConsoleColor(PRM_COLOR_RED);
#ifdef __ARMEL__
fprintf(stderr, "\n\nerror: reset method %d not supported (0=skip, 1=manual, 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin 8), 5=Raspi pin 12, exit!\n\n", resetSTM8);
#else
fprintf(stderr, "\n\nerror: reset method %d not supported (0=skip, 1=manual, 2=DTR line (RS232), 3=send 'Re5eT!' @ 115.2kBaud, 4=Arduino pin 8), exit!\n\n", resetSTM8);
#endif
Exit(1, g_pauseOnExit);
}
////////
// open port with given properties
////////
// UART interface (default)
if (physInterface == 0) {
if (g_verbose == 2)
printf(" open serial port '%s' with %gkBaud ... ", portname, (float) baudrate / 1000.0);
fflush(stdout);
if (uartMode == 0)
ptrPort = init_port(portname, baudrate, 1000, 8, 2, 1, 0, 0); // use even parity
else
ptrPort = init_port(portname, baudrate, 1000, 8, 0, 1, 0, 0); // use no parity
if (g_verbose == 2)
printf("ok\n");
fflush(stdout);
// flush receive buffer
flush_port(ptrPort);
} // UART
// SPI via spidev
#if defined(USE_SPIDEV)
else if (physInterface == 1) {
if (g_verbose == 1)
printf(" open SPI '%s' ... ", portname);
else if (g_verbose == 2) {
if (baudrate < 1000000.0)
printf(" open SPI '%s' with %gkBaud ... ", portname, (float) baudrate / 1000.0);
else
printf(" open SPI '%s' with %gMBaud ... ", portname, (float) baudrate / 1000000.0);
}
fflush(stdout);
ptrPort = init_spi_spidev(portname, baudrate);
if (g_verbose >= 0)
printf("ok\n");
fflush(stdout);
} // SPI via spidev
#endif // USE_SPIDEV
// SPI via Arduino
else if (physInterface == 2) {
// open port
if (g_verbose == 1)
printf(" open Arduino port '%s' ... ", portname);
else if (g_verbose == 2)
printf(" open Arduino port '%s' with %gkBaud SPI ... ", portname, (float) ARDUINO_BAUDRATE / 1000.0);
fflush(stdout);
ptrPort = init_port(portname, ARDUINO_BAUDRATE, 100, 8, 0, 1, 0, 0);
if (g_verbose > 0)
printf("ok\n");
fflush(stdout);
// wait until after Arduino bootloader
if (g_verbose == 2)
printf(" wait for Arduino bootloader ... ");
fflush(stdout);
SLEEP(2000);
if (g_verbose == 2)
printf("ok\n");
fflush(stdout);
// init SPI interface and set NSS pin to high
if (g_verbose == 2) {
if (baudrate < 1000000L)
printf(" init SPI with %gkBaud... ", (float) baudrate / 1000.0);
else
printf(" init SPI with %gMBaud... ", (float) baudrate / 1000000.0);
}
fflush(stdout);
setPin_Arduino(ptrPort, ARDUINO_CSN_PIN, 1);
configSPI_Arduino(ptrPort, baudrate, ARDUINO_MSBFIRST, ARDUINO_SPI_MODE0);
if (g_verbose == 2)
printf("ok\n");
fflush(stdout);
} // SPI via Arduino
// unknown interface -> error
else {
setConsoleColor(PRM_COLOR_RED);
#if defined(USE_SPIDEV)
fprintf(stderr, "\n\nerror: interface %d not supported (0=UART, 1=SPI via spidev, 2=SPI via Arduino), exit!\n\n", physInterface);
#else
fprintf(stderr, "\n\nerror: interface %d not supported (0=UART, 2=SPI via Arduino), exit!\n\n", physInterface);
#endif
Exit(1, g_pauseOnExit);
}
// debug: communication test (echo+1 test-SW on STM8)
/*
printf("open: %d\n", ptrPort);
for (i=0; i<254; i++) {
Tx[0] = i;
send_port(ptrPort, 1, Tx);
receive_port(ptrPort, 1, Rx);
printf("%d %d\n", (int) Tx[0], (int) Rx[0]);
}
printf("ok\n");
Exit(1,0);
*/
////////
// communicate with STM8 bootloader
////////
//required to make flush work, for some reason
usleep(200000);
flush_port(ptrPort);
// synchronize baudrate
bsl_sync(ptrPort, physInterface, uartMode);
// get bootloader info for selecting RAM w/e routines for flash
bsl_getInfo(ptrPort, physInterface, uartMode, &flashsize, &versBSL, &family);
// for STM8S and 8kB STM8L upload RAM routines, else skip
if ((family == STM8S) || (flashsize==8)) {
// select device dependent flash routines for upload
if ((flashsize==8) && (versBSL==0x10)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_8K_verL_1_0_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_8K_verL_1_0_s19;
ptr[STM8_Routines_E_W_ROUTINEs_8K_verL_1_0_s19_len]=0;
}
else if ((flashsize==32) && (versBSL==0x10)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_0_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_0_s19;
ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_0_s19_len]=0;
}
else if ((flashsize==32) && (versBSL==0x12)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_2_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_2_s19;
ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_2_s19_len]=0;
}
else if ((flashsize==32) && (versBSL==0x13)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_3_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_3_s19;
ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_3_s19_len]=0;
}
else if ((flashsize==32) && (versBSL==0x14)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_ver_1_4_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_ver_1_4_s19;
ptr[STM8_Routines_E_W_ROUTINEs_32K_ver_1_4_s19_len]=0;
}
else if ((flashsize==128) && (versBSL==0x20)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_0_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_0_s19;
ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_0_s19_len]=0;
}
/*
else if ((flashsize==128) && (versBSL==0x20)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_32K_verL_1_0_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_32K_verL_1_0_s19;
ptr[STM8_Routines_E_W_ROUTINEs_32K_verL_1_0_s19_len]=0;
}
*/
else if ((flashsize==128) && (versBSL==0x21)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_1_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_1_s19;
ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_1_s19_len]=0;
}
else if ((flashsize==128) && (versBSL==0x22)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_2_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_2_s19;
ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_2_s19_len]=0;
}
else if ((flashsize==128) && (versBSL==0x24)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_128K_ver_2_4_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_128K_ver_2_4_s19;
ptr[STM8_Routines_E_W_ROUTINEs_128K_ver_2_4_s19_len]=0;
}
else if ((flashsize==256) && (versBSL==0x10)) {
#ifdef DEBUG
printf("header STM8_Routines_E_W_ROUTINEs_256K_ver_1_0_s19 \n");
#endif
ptr = (char*) STM8_Routines_E_W_ROUTINEs_256K_ver_1_0_s19;
ptr[STM8_Routines_E_W_ROUTINEs_256K_ver_1_0_s19_len]=0;
}
else {
setConsoleColor(PRM_COLOR_RED);
fprintf(stderr, "\n\nerror: unsupported device, exit!\n\n");
Exit(1, g_pauseOnExit);
}
// upload respective RAM routines to STM8
{
char ramImage[8192];
uint32_t ramImageStart;
uint32_t numRamBytes;
convert_s19(ptr, &ramImageStart, &numRamBytes, ramImage);
if (g_verbose == 2)
printf(" Uploading RAM routines ... ");
fflush(stdout);
bsl_memWrite(ptrPort, physInterface, uartMode, ramImageStart, numRamBytes, ramImage, -1);
if (g_verbose == 2)
printf("ok (%dB from 0x%04x)\n", numRamBytes, ramImageStart);
fflush(stdout);
}
} // if STM8S or low-density STM8L -> upload RAM code
// if flash mass erase
if (flashErase)
bsl_flashMassErase(ptrPort, physInterface, uartMode);
// if upload file to flash
if (strlen(fileIn)>0) {
// upload memory image to STM8
//export_txt("write.txt", imageIn, imageInStart, imageInBytes); // debug
bsl_memWrite(ptrPort, physInterface, uartMode, imageInStart, imageInBytes, imageIn, g_verbose);
// optionally verify upload
if (verifyUpload==1) {
bsl_memRead(ptrPort, physInterface, uartMode, imageInStart, imageInBytes, imageOut);
//export_txt("read.txt", imageOut, imageInStart, imageInBytes); // debug
printf(" verify memory ... ");
for (i=0; i<imageInBytes; i++) {
if (imageIn[i] != imageOut[i]) {
setConsoleColor(PRM_COLOR_RED);
printf("\nfailed at address 0x%04x (0x%02x vs 0x%02x), exit!\n", (uint32_t) (imageInStart+i), (uint8_t) (imageIn[i]), (uint8_t) (imageOut[i]));
Exit(1, g_pauseOnExit);
}
}
printf("ok\n");
}
// enable ROM bootloader after upload (option bytes always on same address)
if (enableBSL==1) {
if (g_verbose == 2)
printf(" activate bootloader ... ");
fflush(stdout);
bsl_memWrite(ptrPort, physInterface, uartMode, 0x487E, 2, (char*)"\x55\xAA", -1);
if (g_verbose == 2)
printf("ok\n");
fflush(stdout);
}
} // if file upload to flash
////////////////////
// read memory and dump to file
////////////////////
if (strlen(fileOut)>0) {
const char *shortname = strrchr(fileOut, '/');
if (!shortname)
shortname = fileOut;
// read memory
bsl_memRead(ptrPort, physInterface, uartMode, imageOutStart, imageOutBytes, imageOut);
// save to file, depending on file type
const char *dot = strrchr (fileOut, '.');
if (dot && !strcmp(dot, ".s19")) {
printf(" save as Motorola S-record file '%s' ... ", shortname);
fflush(stdout);
export_s19(fileOut, imageOut, imageOutStart, imageOutBytes);
printf("ok\n");
fflush(stdout);
}
else if (dot && !strcmp(dot, ".txt")) {
printf(" save as plain file to '%s' ... ", shortname);
fflush(stdout);
export_txt(fileOut, imageOut, imageOutStart, imageOutBytes);
printf("ok\n");
fflush(stdout);
}
else {
setConsoleColor(PRM_COLOR_RED);
fprintf(stderr, "\n\nerror: unsupported export type '%s', exit!\n\n", dot);
Exit(1, g_pauseOnExit);
}
}
// jump to flash start address after done (reset vector always on same address)
if (jumpFlash) {
// don't know why, but seems to be required for SPI
if ((physInterface==1) || (physInterface==2))
SLEEP(500);
// jumpt to application
if (g_verbose == 2)
printf(" jump to address 0x%04x ... ", (int) PFLASH_START);
fflush(stdout);
bsl_jumpTo(ptrPort, physInterface, uartMode, PFLASH_START);
if (g_verbose == 2)
printf("ok\n");
fflush(stdout);
} // jump to flash
////////
// clean up and exit
////////
close_port(&ptrPort);
printf("done with program\n");
Exit(0, g_pauseOnExit);
// avoid compiler warnings
return(0);
} // main