Skip to content

Commit

Permalink
Added updiprog
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoffland committed Jun 30, 2021
1 parent d1b24fd commit 9263bfa
Show file tree
Hide file tree
Showing 13 changed files with 1,688 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/updiprog/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/updiprog
/build
25 changes: 25 additions & 0 deletions src/updiprog/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2018, Alex Kiselev
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 changes: 28 additions & 0 deletions src/updiprog/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
TARGET=updiprog

SRC := $(wildcard src/*.c)
OBJ := $(patsubst src/%.c,build/%.o,$(SRC))

CFLAGS += -O3 -Wall -Werror -Isrc
CFLAGS += -MD -MP -MT $@ -MF build/dep/$(@F).d


all: $(TARGET)

$(TARGET): $(OBJ)
$(CC) -o $@ $(OBJ) $(LDFLAGS)

build/%.o: src/%.c
@mkdir -p $(shell dirname $@)
$(CC) $(CFLAGS) -c -o $@ $<

tidy:
rm -f *~ \#*

clean: tidy
rm -rf build $(TARGET)

.PHONY: all tidy clean

# Dependencies
-include $(shell mkdir -p build/dep) $(wildcard build/dep/*.d)
107 changes: 107 additions & 0 deletions src/updiprog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# updiprog

This is UPDI programming software for AVR microcontrollers. It was based on the
code at https://github.com/Polarisru/updiprog but has undergone a lot of
restructuring and has speed and reliability improvements and additional features.
However, this version does not work on Windows.

```
Vcc Vcc
+-+ +-+
| |
+---------------------+ | | +--------------------+
| Serial port +-+ +-+ AVR device |
| | +----------+ | |
| TX +------+ 1k +---------+ UPDI |
| | +----------+ | | |
| | | | |
| RX +----------------------+ | |
| | | |
| +--+ +--+ |
+---------------------+ | | +--------------------+
+-+ +-+
GND GND
```

# Features
- Read/write flash memory
- Read/write fuses
- Chip erase
- Chip reset
- Read/write up to 1Gbps
- CRC checking

## CRC Check
You can enable CRC checking with the ``-x`` option. With this option enabled
during a FLASH write updiprog will do the following:

* Read the HEX file
* Compute a CRC-16-CCITT
* Read the last 2-bytes of FLASH
* If the last 2-bytes match the CRC, stop
* Otherwise program the FLASH
* Write the CRC to the last 2-bytes of FLASH

The end result is that with the ``-x`` option updiprog will not reprogram the
chip if the CRC indicates that the programmed firmware is the same.

The programmed CRC can also be used with the AVR's CRCSCAN feature. With
CRCSCAN enabled, the chip will not run if the CRC at the end of FLASH does not
match the contents of FLASH memory.

# Options
```
updiprog [OPTIONS]
OPTIONS:
-b BAUDRATE - Serial port speed (default=115200)
-d DEVICE - Target device (e.g. tiny321x)
-c PORT - Serial port (e.g. /dev/ttyUSB0)
-e - Chip erase
-R - Chip reset
-F - Read all fuses
-f FUSE VALUE - Write fuse
-r FILE.HEX - Hex file to read FLASH into
-w FILE.HEX - Hex file to write to FLASH
-x - Enable CRC checks
-v - Increase log level. Errors -> Warnings -> All
-h - Show this help screen and exit
```

# Supported devices

```
mega480x mega320x mega160x mega80x
tiny321x tiny160x tiny161x tiny80x
tiny81x tiny40x tiny41x tiny20x
tiny21x
```

# Examples

## Chip Erase
updiprog -c /dev/ttyUSB0 -d tiny321x -e

## Program FLASH from HEX file
updiprog -c /dev/ttyUSB0 -d tiny321x -w firmware.hex

## Program FLASH from HEX file at 1G BAUD
updiprog -c /dev/ttyUSB0 -d tiny321x -w firmware.hex -b 1000000

## Read FLASH to HEX file
updiprog -c /dev/ttyUSB0 -d tiny321x -r tiny_fw.hex

## Read all fuses:
updiprog -c /dev/ttyUSB0 -d tiny321x -F

## Write 0x04 to fuse number 1 and 0x1b to fuse number 5
updiprog -c /dev/ttyUSB0 -d tiny321x -f 1 0x04 -f 5 0x1b

## Program FLASH from HEX file at 1G BAUD with CRC check
updiprog -c /dev/ttyUSB0 -d tiny321x -w firmware.hex -b 1000000 -x

## Erase the chip then program FLASH from HEX file at 1G BAUD
updiprog -c /dev/ttyUSB0 -d tiny321x -e -w firmware.hex -b 1000000

## Chip Reset
updiprog -c /dev/ttyUSB0 -d tiny321x -R
161 changes: 161 additions & 0 deletions src/updiprog/src/devices.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include "devices.h"

#include <string.h>


device_t devices[] = {
{
"mega480x",
0x4000,
48 * 1024,
128,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
11
}, {
"mega320x",
0x4000,
32 * 1024,
128,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
11
}, {
"mega160x",
0x4000,
16 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
11
}, {
"mega80x",
0x4000,
8 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
11
}, {
"tiny321x",
0x8000,
32 * 1024,
128,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny160x",
0x8000,
16 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny161x",
0x8000,
16 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny80x",
0x8000,
8 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny81x",
0x8000,
8 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny40x",
0x8000,
4 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny41x",
0x8000,
4 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
11
}, {
"tiny20x",
0x8000,
2 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
}, {
"tiny21x",
0x8000,
2 * 1024,
64,
0x0f00,
0x1000,
0x1100,
0x1280,
0x1300,
9
},
{0} // Sentinel
};


device_t *devices_find(char *name) {
for (int i = 0; devices[i].name; i++)
if (strcmp(name, devices[i].name) == 0)
return &devices[i];

return 0;
}
22 changes: 22 additions & 0 deletions src/updiprog/src/devices.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include <stdint.h>


typedef struct {
const char *name;
uint16_t flash_start;
uint16_t flash_size;
uint16_t page_size;
uint16_t syscfg_address;
uint16_t nvmctrl_address;
uint16_t sigrow_address;
uint16_t fuses_address;
uint16_t userrow_address;
uint8_t num_fuses;
} device_t;


extern device_t devices[];

device_t *devices_find(char *name);
Loading

0 comments on commit 9263bfa

Please sign in to comment.