Skip to content

Commit 9263bfa

Browse files
committed
Added updiprog
1 parent d1b24fd commit 9263bfa

13 files changed

Lines changed: 1688 additions & 0 deletions

File tree

src/updiprog/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/updiprog
2+
/build

src/updiprog/LICENSE

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
BSD 2-Clause License
2+
3+
Copyright (c) 2018, Alex Kiselev
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

src/updiprog/Makefile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
TARGET=updiprog
2+
3+
SRC := $(wildcard src/*.c)
4+
OBJ := $(patsubst src/%.c,build/%.o,$(SRC))
5+
6+
CFLAGS += -O3 -Wall -Werror -Isrc
7+
CFLAGS += -MD -MP -MT $@ -MF build/dep/$(@F).d
8+
9+
10+
all: $(TARGET)
11+
12+
$(TARGET): $(OBJ)
13+
$(CC) -o $@ $(OBJ) $(LDFLAGS)
14+
15+
build/%.o: src/%.c
16+
@mkdir -p $(shell dirname $@)
17+
$(CC) $(CFLAGS) -c -o $@ $<
18+
19+
tidy:
20+
rm -f *~ \#*
21+
22+
clean: tidy
23+
rm -rf build $(TARGET)
24+
25+
.PHONY: all tidy clean
26+
27+
# Dependencies
28+
-include $(shell mkdir -p build/dep) $(wildcard build/dep/*.d)

src/updiprog/README.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# updiprog
2+
3+
This is UPDI programming software for AVR microcontrollers. It was based on the
4+
code at https://github.com/Polarisru/updiprog but has undergone a lot of
5+
restructuring and has speed and reliability improvements and additional features.
6+
However, this version does not work on Windows.
7+
8+
```
9+
Vcc Vcc
10+
+-+ +-+
11+
| |
12+
+---------------------+ | | +--------------------+
13+
| Serial port +-+ +-+ AVR device |
14+
| | +----------+ | |
15+
| TX +------+ 1k +---------+ UPDI |
16+
| | +----------+ | | |
17+
| | | | |
18+
| RX +----------------------+ | |
19+
| | | |
20+
| +--+ +--+ |
21+
+---------------------+ | | +--------------------+
22+
+-+ +-+
23+
GND GND
24+
```
25+
26+
# Features
27+
- Read/write flash memory
28+
- Read/write fuses
29+
- Chip erase
30+
- Chip reset
31+
- Read/write up to 1Gbps
32+
- CRC checking
33+
34+
## CRC Check
35+
You can enable CRC checking with the ``-x`` option. With this option enabled
36+
during a FLASH write updiprog will do the following:
37+
38+
* Read the HEX file
39+
* Compute a CRC-16-CCITT
40+
* Read the last 2-bytes of FLASH
41+
* If the last 2-bytes match the CRC, stop
42+
* Otherwise program the FLASH
43+
* Write the CRC to the last 2-bytes of FLASH
44+
45+
The end result is that with the ``-x`` option updiprog will not reprogram the
46+
chip if the CRC indicates that the programmed firmware is the same.
47+
48+
The programmed CRC can also be used with the AVR's CRCSCAN feature. With
49+
CRCSCAN enabled, the chip will not run if the CRC at the end of FLASH does not
50+
match the contents of FLASH memory.
51+
52+
# Options
53+
```
54+
updiprog [OPTIONS]
55+
56+
OPTIONS:
57+
-b BAUDRATE - Serial port speed (default=115200)
58+
-d DEVICE - Target device (e.g. tiny321x)
59+
-c PORT - Serial port (e.g. /dev/ttyUSB0)
60+
-e - Chip erase
61+
-R - Chip reset
62+
-F - Read all fuses
63+
-f FUSE VALUE - Write fuse
64+
-r FILE.HEX - Hex file to read FLASH into
65+
-w FILE.HEX - Hex file to write to FLASH
66+
-x - Enable CRC checks
67+
-v - Increase log level. Errors -> Warnings -> All
68+
-h - Show this help screen and exit
69+
```
70+
71+
# Supported devices
72+
73+
```
74+
mega480x mega320x mega160x mega80x
75+
tiny321x tiny160x tiny161x tiny80x
76+
tiny81x tiny40x tiny41x tiny20x
77+
tiny21x
78+
```
79+
80+
# Examples
81+
82+
## Chip Erase
83+
updiprog -c /dev/ttyUSB0 -d tiny321x -e
84+
85+
## Program FLASH from HEX file
86+
updiprog -c /dev/ttyUSB0 -d tiny321x -w firmware.hex
87+
88+
## Program FLASH from HEX file at 1G BAUD
89+
updiprog -c /dev/ttyUSB0 -d tiny321x -w firmware.hex -b 1000000
90+
91+
## Read FLASH to HEX file
92+
updiprog -c /dev/ttyUSB0 -d tiny321x -r tiny_fw.hex
93+
94+
## Read all fuses:
95+
updiprog -c /dev/ttyUSB0 -d tiny321x -F
96+
97+
## Write 0x04 to fuse number 1 and 0x1b to fuse number 5
98+
updiprog -c /dev/ttyUSB0 -d tiny321x -f 1 0x04 -f 5 0x1b
99+
100+
## Program FLASH from HEX file at 1G BAUD with CRC check
101+
updiprog -c /dev/ttyUSB0 -d tiny321x -w firmware.hex -b 1000000 -x
102+
103+
## Erase the chip then program FLASH from HEX file at 1G BAUD
104+
updiprog -c /dev/ttyUSB0 -d tiny321x -e -w firmware.hex -b 1000000
105+
106+
## Chip Reset
107+
updiprog -c /dev/ttyUSB0 -d tiny321x -R

src/updiprog/src/devices.c

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
#include "devices.h"
2+
3+
#include <string.h>
4+
5+
6+
device_t devices[] = {
7+
{
8+
"mega480x",
9+
0x4000,
10+
48 * 1024,
11+
128,
12+
0x0f00,
13+
0x1000,
14+
0x1100,
15+
0x1280,
16+
0x1300,
17+
11
18+
}, {
19+
"mega320x",
20+
0x4000,
21+
32 * 1024,
22+
128,
23+
0x0f00,
24+
0x1000,
25+
0x1100,
26+
0x1280,
27+
0x1300,
28+
11
29+
}, {
30+
"mega160x",
31+
0x4000,
32+
16 * 1024,
33+
64,
34+
0x0f00,
35+
0x1000,
36+
0x1100,
37+
0x1280,
38+
0x1300,
39+
11
40+
}, {
41+
"mega80x",
42+
0x4000,
43+
8 * 1024,
44+
64,
45+
0x0f00,
46+
0x1000,
47+
0x1100,
48+
0x1280,
49+
0x1300,
50+
11
51+
}, {
52+
"tiny321x",
53+
0x8000,
54+
32 * 1024,
55+
128,
56+
0x0f00,
57+
0x1000,
58+
0x1100,
59+
0x1280,
60+
0x1300,
61+
9
62+
}, {
63+
"tiny160x",
64+
0x8000,
65+
16 * 1024,
66+
64,
67+
0x0f00,
68+
0x1000,
69+
0x1100,
70+
0x1280,
71+
0x1300,
72+
9
73+
}, {
74+
"tiny161x",
75+
0x8000,
76+
16 * 1024,
77+
64,
78+
0x0f00,
79+
0x1000,
80+
0x1100,
81+
0x1280,
82+
0x1300,
83+
9
84+
}, {
85+
"tiny80x",
86+
0x8000,
87+
8 * 1024,
88+
64,
89+
0x0f00,
90+
0x1000,
91+
0x1100,
92+
0x1280,
93+
0x1300,
94+
9
95+
}, {
96+
"tiny81x",
97+
0x8000,
98+
8 * 1024,
99+
64,
100+
0x0f00,
101+
0x1000,
102+
0x1100,
103+
0x1280,
104+
0x1300,
105+
9
106+
}, {
107+
"tiny40x",
108+
0x8000,
109+
4 * 1024,
110+
64,
111+
0x0f00,
112+
0x1000,
113+
0x1100,
114+
0x1280,
115+
0x1300,
116+
9
117+
}, {
118+
"tiny41x",
119+
0x8000,
120+
4 * 1024,
121+
64,
122+
0x0f00,
123+
0x1000,
124+
0x1100,
125+
0x1280,
126+
0x1300,
127+
11
128+
}, {
129+
"tiny20x",
130+
0x8000,
131+
2 * 1024,
132+
64,
133+
0x0f00,
134+
0x1000,
135+
0x1100,
136+
0x1280,
137+
0x1300,
138+
9
139+
}, {
140+
"tiny21x",
141+
0x8000,
142+
2 * 1024,
143+
64,
144+
0x0f00,
145+
0x1000,
146+
0x1100,
147+
0x1280,
148+
0x1300,
149+
9
150+
},
151+
{0} // Sentinel
152+
};
153+
154+
155+
device_t *devices_find(char *name) {
156+
for (int i = 0; devices[i].name; i++)
157+
if (strcmp(name, devices[i].name) == 0)
158+
return &devices[i];
159+
160+
return 0;
161+
}

src/updiprog/src/devices.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include <stdint.h>
4+
5+
6+
typedef struct {
7+
const char *name;
8+
uint16_t flash_start;
9+
uint16_t flash_size;
10+
uint16_t page_size;
11+
uint16_t syscfg_address;
12+
uint16_t nvmctrl_address;
13+
uint16_t sigrow_address;
14+
uint16_t fuses_address;
15+
uint16_t userrow_address;
16+
uint8_t num_fuses;
17+
} device_t;
18+
19+
20+
extern device_t devices[];
21+
22+
device_t *devices_find(char *name);

0 commit comments

Comments
 (0)