Skip to content

Commit c54c73b

Browse files
committed
integrate files from stm8flash repository at https://github.com/vdudouyt/stm8flash.git
2 parents 6bc779c + 6be8ecf commit c54c73b

19 files changed

Lines changed: 2886 additions & 0 deletions

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
*~
2+
.*.swp
23
*.o
4+
*.bin
5+
*.ihx
6+
*.hex
7+
*.exe
8+
tags

tools/stm8flash/COPYING

Lines changed: 340 additions & 0 deletions
Large diffs are not rendered by default.

tools/stm8flash/Makefile

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# stm8flash makefile
2+
#
3+
# Multiplatform support
4+
# - Linux (x86, Raspian)
5+
# - MacOS (Darwin)
6+
# - Windows (e.g. mingw-w64-x86 with libusb1.0)
7+
8+
9+
PLATFORM=$(shell uname -s)
10+
11+
# Pass RELEASE=anything to build without debug symbols
12+
ifneq (,$(strip $(RELEASE)))
13+
BASE_CFLAGS := -O1
14+
else
15+
BASE_CFLAGS := -g -O0
16+
endif
17+
18+
# Pass LIBUSB_QUIET=anything to Make to silence debug output from libusb.
19+
ifneq (,$(strip $(LIBUSB_QUIET)))
20+
BASE_CFLAGS += -DSTM8FLASH_LIBUSB_QUIET
21+
endif
22+
23+
BASE_CFLAGS += --std=gnu99 --pedantic
24+
25+
ifeq ($(PLATFORM),Linux)
26+
LIBS = `pkg-config --libs libusb-1.0`
27+
LIBUSB_CFLAGS = `pkg-config --cflags libusb-1.0`
28+
else ifeq ($(PLATFORM),Darwin)
29+
LIBS = $(shell pkg-config --libs libusb-1.0)
30+
LIBUSB_CFLAGS = $(shell pkg-config --cflags libusb-1.0)
31+
MacOSSDK=$(shell xcrun --show-sdk-path)
32+
BASE_CFLAGS += -I$(MacOSSDK)/usr/include/ -I$(MacOSSDK)/usr/include/sys -I$(MacOSSDK)/usr/include/machine
33+
else
34+
# Generic case is Windows
35+
36+
LIBS = -lusb-1.0
37+
LIBUSB_CFLAGS =
38+
CC ?= GCC
39+
BIN_SUFFIX =.exe
40+
endif
41+
42+
# Respect user-supplied cflags, if any - just put ours in front.
43+
override CFLAGS := $(BASE_CFLAGS) $(LIBUSB_CFLAGS) $(CFLAGS)
44+
45+
46+
BIN =stm8flash
47+
OBJECTS =stlink.o stlinkv2.o main.o byte_utils.o ihex.o stm8.o
48+
49+
50+
.PHONY: all clean install
51+
52+
$(BIN)$(BIN_SUFFIX): $(OBJECTS)
53+
$(CC) $(CFLAGS) $(OBJECTS) $(LIBS) -o $(BIN)$(BIN_SUFFIX)
54+
55+
all: $(BIN)$(BIN_SUFFIX)
56+
57+
58+
clean:
59+
-rm -f $(OBJECTS) $(BIN)$(BIN_SUFFIX)
60+
61+
install:
62+
cp $(BIN)$(BIN_SUFFIX) $(DESTDIR)/usr/bin/

tools/stm8flash/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
stm8flash
2+
=========
3+
4+
This is a free and opensource software distributed under the terms of GNU General Public License v2.
5+
6+
It also seems to be the only program that's able to communicate through the SWIM interface of ST-LINKs under Linux as for March, 2014.
7+
8+
9+
Synopsis
10+
--------
11+
12+
```
13+
stm8flash -c <stlink|stlinkv2> -p <partname> [-s flash|eeprom|0x8000] [-r|-w|-v] <filename>
14+
```
15+
16+
Flash examples:
17+
```nohighlight
18+
./stm8flash -c stlink -p stm8s003f3 -w blinky.bin
19+
./stm8flash -c stlink -p stm8s003f3 -w blinky.ihx
20+
./stm8flash -c stlinkv2 -p stm8s003f3 -w blinky.ihx
21+
./stm8flash -c stlink -p stm8s105c6 -w blinky.bin
22+
./stm8flash -c stlinkv2 -p stm8l150 -w blinky.bin
23+
```
24+
25+
EEPROM examples:
26+
```nohighlight
27+
./stm8flash -c stlinkv2 -p stm8s003f3 -s eeprom -r ee.bin
28+
./stm8flash -c stlinkv2 -p stm8s003f3 -s eeprom -w ee.bin
29+
./stm8flash -c stlinkv2 -p stm8s003f3 -s eeprom -v ee.bin
30+
```
31+
32+
Support table
33+
-------------
34+
35+
* ST-Link V1: flash/eeprom/opt
36+
* ST-Link V2: flash2/eeprom2/opt2
37+
38+
| MCU | flash | eeprom | opt | flash2 | eeprom2 | opt2 |
39+
|-------------|-------|--------|------|--------|---------|-------|
40+
| stlux???a | ? | ? | ? | ? | ? | ? |
41+
| stm8af526? | ? | ? | ? | ? | ? | ? |
42+
| stm8af528? | ? | ? | ? | 32 | ? | ? |
43+
| stm8af52a? | ? | ? | ? | ? | ? | ? |
44+
| stm8af6213 | ? | ? | ? | ? | ? | ? |
45+
| stm8af6223 | ? | ? | ? | ? | ? | ? |
46+
| stm8af6223a | ? | ? | ? | ? | ? | ? |
47+
| stm8af6226 | ? | ? | ? | ? | ? | ? |
48+
| stm8af624? | ? | ? | ? | ? | ? | ? |
49+
| stm8af6266 | ? | ? | ? | ? | ? | ? |
50+
| stm8af6268 | ? | ? | ? | ? | ? | ? |
51+
| stm8af6269 | ? | ? | ? | ? | ? | ? |
52+
| stm8af628? | ? | ? | ? | ? | ? | ? |
53+
| stm8af62a? | ? | ? | ? | ? | ? | ? |
54+
| stm8al313? | ? | ? | ? | ? | ? | ? |
55+
| stm8al314? | ? | ? | ? | ? | ? | ? |
56+
| stm8al316? | ? | ? | ? | ? | ? | ? |
57+
| stm8al3l4? | ? | ? | ? | ? | ? | ? |
58+
| stm8al3l6? | ? | ? | ? | ok | ? | ? |
59+
| stm8l051f3 | ok | ? | ? | ? | ? | ? |
60+
| stm8l052c6 | ok | ? | ? | ? | ? | ? |
61+
| stm8l052r8 | ok | ? | ? | ? | ? | ? |
62+
| stm8l101f1 | ? | no | ? | ? | no | ? |
63+
| stm8l101?2 | ? | no | ? | ? | no | ? |
64+
| stm8l101?3 | ? | no | ? | ok | no | ? |
65+
| stm8l151?2 | ? | ? | ? | ? | ? | ? |
66+
| stm8l151?3 | ? | ? | ? | ? | ? | ? |
67+
| stm8l151?4 | ? | ? | ? | ? | ? | ? |
68+
| stm8l151?6 | ? | ? | ? | ? | ? | ? |
69+
| stm8l151?8 | ? | ? | ? | ? | ? | ? |
70+
| stm8l152?4 | ? | ? | ? | ? | ? | ? |
71+
| stm8l152?6 | ok | FAIL | ? | ok | ok | ? |
72+
| stm8l152?8 | ? | ? | ? | ? | ? | ? |
73+
| stm8l162?8 | ? | ? | ? | ? | ? | ? |
74+
| stm8s003?3 | ok | FAIL | ? | ok | ok | ? |
75+
| stm8s005?6 | ok | ? | ok | ? | ? | ? |
76+
| stm8s007c8 | ? | ? | ? | ? | ? | ? |
77+
| stm8s103f2 | ? | ? | ? | ? | ? | ? |
78+
| stm8s103?3 | ? | ? | ? | ok | ? | ? |
79+
| stm8s105?4 | ok | FAIL | ? | ok | ok | ? |
80+
| stm8s105?6 | ok | ? | ? | ok | ? | ? |
81+
| stm8s207c8 | ? | ? | ? | ? | ? | ? |
82+
| stm8s207cb | ? | ? | ? | ? | ? | ? |
83+
| stm8s207k8 | ? | ? | ? | ? | ? | ? |
84+
| stm8s207m8 | ? | ? | ? | ? | ? | ? |
85+
| stm8s207mb | ? | ? | ? | ? | ? | ? |
86+
| stm8s207r8 | ? | ? | ? | ? | ? | ? |
87+
| stm8s207rb | ? | ? | ? | ? | ? | ? |
88+
| stm8s207s8 | ? | ? | ? | 32 | ? | ? |
89+
| stm8s207sb | ? | ? | ? | ? | ? | ? |
90+
| stm8s207?6 | ? | ? | ? | ? | ? | ? |
91+
| stm8s208c6 | ? | ? | ? | ok | ? | ? |
92+
| stm8s208s6 | ? | ? | ? | ? | ? | ? |
93+
| stm8s208?8 | ? | ? | ? | ? | ? | ? |
94+
| stm8s208?b | ? | ? | ? | 32 | ? | ? |
95+
| stm8s903?3 | ? | ? | ? | ? | ? | ? |
96+
| stm8splnb1 | ? | ? | ? | ? | ? | ? |
97+
| stm8tl5??4 | ? | no | ? | ? | no | ? |
98+
| stnrg???a | ? | ? | ? | ? | ? | ? |
99+
100+
Legend:
101+
102+
* `ok` - Fully supported.
103+
* `no` - Not supported.
104+
* `?` - Not tested.
105+
* `FÁIL` - Not working. Needs fix.
106+
* `32` - Lower 32K of flash works, upper doesn't.
107+

tools/stm8flash/byte_utils.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "byte_utils.h"
2+
3+
void format_int(unsigned char *out, unsigned int in, unsigned char length, unsigned char endianess) {
4+
int i, idx;
5+
for(i = 0; i < length; i++) {
6+
idx = endianess == MP_LITTLE_ENDIAN ? i : length - 1 - i;
7+
out[i] = (in & 0xFF << idx*8) >> idx*8;
8+
}
9+
}
10+
11+
int load_int(unsigned char *buf, unsigned char length, unsigned char endianess) {
12+
int i, idx, result = 0;
13+
for(i = 0; i < length; i++) {
14+
idx = endianess == MP_LITTLE_ENDIAN ? i : length - 1 - i;
15+
result |= (buf[i] << idx*8);
16+
}
17+
return(result);
18+
}

tools/stm8flash/byte_utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef __BYTE_UTILS_H
2+
#define __BYTE_UTILS_H
3+
4+
#define MP_LITTLE_ENDIAN 0
5+
#define MP_BIG_ENDIAN 1
6+
7+
#define HI(x) (((x) & 0xff00) >> 8)
8+
#define LO(x) ((x) & 0xff)
9+
10+
void format_int(unsigned char *out, unsigned int in, unsigned char length, unsigned char endianess);
11+
int load_int(unsigned char *buf, unsigned char length, unsigned char endianess);
12+
13+
#endif

tools/stm8flash/error.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
// Fixes warning
4+
#undef ERROR
5+
6+
#define ERROR(s) do { fprintf(stderr, "%s\n", (s)); exit(-1); } while(0)
7+
#define ERROR2(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while(0)
8+
#define PERROR(s) do { perror((s)); exit(-1); } while(0)
9+
#define USAGE_ERROR(s) do { fprintf(stderr, "%s\n", (s)); print_help_and_exit(argv[0]); } while(0)

tools/stm8flash/ihex.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* stlink/v2 stm8 memory programming utility
2+
(c) Valentin Dudouyt, 2012 - 2014 */
3+
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <string.h>
7+
#include "ihex.h"
8+
#include "error.h"
9+
#include "byte_utils.h"
10+
11+
char line[256];
12+
13+
static unsigned char checksum(unsigned char *buf, unsigned int length, int chunk_len, int chunk_addr, int chunk_type) {
14+
int sum = chunk_len + (LO(chunk_addr)) + (HI(chunk_addr)) + chunk_type;
15+
int i;
16+
for(i = 0; i < length; i++) {
17+
sum += buf[i];
18+
}
19+
20+
int complement = (~sum + 1);
21+
return complement & 0xff;
22+
}
23+
24+
int ihex_read(FILE *pFile, unsigned char *buf, unsigned int start, unsigned int end) {
25+
fseek(pFile, 0, SEEK_SET);
26+
unsigned int chunk_len, chunk_addr, chunk_type, i, byte, line_no = 0, greatest_addr = 0;
27+
while(fgets(line, sizeof(line), pFile)) {
28+
line_no++;
29+
// Reading chunk header
30+
if(sscanf(line, ":%02x%04x%02x", &chunk_len, &chunk_addr, &chunk_type) != 3) {
31+
free(buf);
32+
ERROR2("Error while parsing IHEX at line %d\n", line_no);
33+
}
34+
// Reading chunk data
35+
for(i = 9; i < strlen(line) - 1; i +=2) {
36+
if(sscanf(&(line[i]), "%02x", &byte) != 1) {
37+
free(buf);
38+
ERROR2("Error while parsing IHEX at line %d byte %d\n", line_no, i);
39+
}
40+
if(chunk_type != 0x00) {
41+
// The only data records have to be processed
42+
continue;
43+
}
44+
if((i - 9) / 2 >= chunk_len) {
45+
// Respect chunk_len and do not capture checksum as data
46+
break;
47+
}
48+
if(chunk_addr < start) {
49+
free(buf);
50+
ERROR2("Address %04x is out of range at line %d\n", chunk_addr, line_no);
51+
}
52+
if(chunk_addr + chunk_len > end) {
53+
free(buf);
54+
ERROR2("Address %04x + %d is out of range at line %d\n", chunk_addr, chunk_len, line_no);
55+
}
56+
if(chunk_addr + chunk_len > greatest_addr) {
57+
greatest_addr = chunk_addr + chunk_len;
58+
}
59+
buf[chunk_addr - start + (i - 9) / 2] = byte;
60+
}
61+
}
62+
63+
return(greatest_addr - start);
64+
}
65+
66+
void ihex_write(FILE *pFile, unsigned char *buf, unsigned int start, unsigned int end) {
67+
unsigned int chunk_len, chunk_start, chunk_type, i, byte, line_no = 0, greatest_addr = 0;
68+
chunk_start = start;
69+
while(chunk_start < end)
70+
{
71+
chunk_len = end - chunk_start;
72+
if (chunk_len > 32)
73+
{
74+
chunk_len = 32;
75+
}
76+
fprintf(pFile, ":%02X%04X00",chunk_len,chunk_start);
77+
for(i = chunk_start - start; i < (chunk_start + chunk_len - start); i++)
78+
{
79+
fprintf(pFile, "%02X",buf[i]);
80+
}
81+
fprintf(pFile, "%02X\n", checksum( &buf[chunk_start - start], chunk_len, chunk_len, chunk_start, 0));
82+
83+
chunk_start += chunk_len;
84+
}
85+
fprintf(pFile,":00000001FF\n");
86+
}

tools/stm8flash/ihex.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef __IHEX_H
2+
#define __IHEX_H
3+
4+
int ihex_read(FILE *pFile, unsigned char *buf, unsigned int start, unsigned int end);
5+
6+
void ihex_write(FILE *pFile, unsigned char *buf, unsigned int start, unsigned int end);
7+
8+
#endif

0 commit comments

Comments
 (0)