Giter Site home page Giter Site logo

updiprog's Introduction

updiprog

This is C version of UPDI interface utility with improvements, you could refer to the Python version: pyupdi

pyupdi is a Python utility for programming AVR devices with UPDI interface using a standard TTL serial port.

The main purpose is the possibility to use UPDI to flash the new TinyAVR at any PC. No external libraries were used, so no dependencies. I have some problems during pyupdi installation because of absence of the Internet connection, so I had to copy all necessary packages and wheels first and to install them manually. Yes, I know about the possibility to compile the executable from the Python script, but pyupdi has also some disadvantages like lackage of reading the flash content, slow speed of programming and probably no error handling at all. So I have decided to improve it and get it working with standard C language.

Code::Blocks IDE was used to write code and compile it.

I have tried to write it portable for Windows and Linux but can't really test it on Linux because of the driver bug for CH340 USB to serial converter, it can't work with parity bits.

I am using CH340 USB to TTL converter to program Atmel Tiny devices, you just need to connect TX and RX lines, they are actually already have a 1.5k output resistor on my connector, so just connect them and don't forget to connect GND of the converter with GND of the PCB with Tiny MCU. There is also a possibility to supply Tiny with the voltage from the CH340 converter, so this adapter seems to be a good choice to start with programming of AVR devices.

                        Vcc                     Vcc
                        +-+                     +-+
                         |                       |
 +---------------------+ |                       | +--------------------+
 | CH340 converter     +-+                       +-+  AVR Tiny device   |
 |                     |                           |                    |
 |                  TX +---+-----------------------+ UPDI               |
 |                     |   |                       |                    |
 |                     |   |                       |                    |
 |                  RX +---+                       |                    |
 |                     |                           |                    |
 |                     +---+-----------------------+                    |
 +---------------------+   |                       +--------------------+
                          +-+
                         GND

Warning!!! If you don't have any output resistors on your CH340 board, please mount them!

Additional features were added:

- reading content of the flash memory from the MCU
- reading fuses
- different levels of logging
- faster programming/reading (about 6 seconds for the whole Tiny1616)
- many additional error messages
- locking/unlocking MCU

A brief description of all available options.

-b BAUDRATE - set COM baudrate (default=115200)
-d DEVICE   - target device (tinyXXX)
-c COM_PORT - COM port to use (Win: COMx | *nix: /dev/ttyX)
-e          - erase device
-fw X:0xYY  - write fuses (X - fuse number, 0xYY - hex value)
-fr         - read all fuses
-h          - show this help screen
-ls         - lock device
-lr         - unlock device
-mX         - set logging level (0-all/1-warnings/2-errors)
-r FILE.HEX - Hex file to read MCU flash into
-w FILE.HEX - Hex file to write to MCU flash

Examples:

Erase Flash memory:
    updiprog.exe -c COM10 -d tiny81x -e

Program Flash memory from file tiny_fw.hex:
    updiprog.exe -c COM10 -d tiny81x -w tiny_fw.hex
	
Read Flash memory to file tiny_fw.hex:
    updiprog.exe -c COM10 -d tiny81x -r tiny_fw.hex
	
Read all fuses:
	updiprog.exe -c COM10 -d tiny81x -fr
	
Write 0x04 to fuse number 1 and 0x1b to fuse number 5:
	updiprog.exe -c COM10 -d tiny81x -fw 1:0x04 5:0x1b

updiprog's People

Contributors

2ni avatar ansemjo avatar brain5lug avatar davidflowerday avatar lhondareyte avatar nieldk avatar polarisru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

updiprog's Issues

Serial read not working correctly on Linux

I'm trying the program on Arch Linux and it seems the Linux COM_Read read function is not working correctly. From debugging it with strace, I could see that the read() call only returns one byte instead of three and the remaining spill over to the next write/read pair.

write(1, "INFO: STCS to 0x03\n", 19INFO: STCS to 0x03
)    = 19
write(3, "U\303\10", 3)                 = 3
read(3, "U", 3)                         = 1
write(1, "INFO: STCS to 0x02\n", 19INFO: STCS to 0x02
)    = 19
write(3, "U\302\200", 3)                = 3
read(3, "\303", 3)                      = 1
write(1, "INFO: LDCS from 0x00\n", 21INFO: LDCS from 0x00
)  = 21
write(3, "U\200", 2)                    = 2
read(3, "\10", 2)                       = 1
read(3, "U", 1)                         = 1
write(1, "INFO: UPDI init OK\n", 19INFO: UPDI init OK
)    = 19

read() can return less than the given length of bytes, so the read call must be run in a loop until the expected bytes are received or an error occurs or the timeout expires.

I'm still testing but if I get everything working, I will try to create a pull request.

Cannot flash due to "Problem reading Hex file"

Description

I'm trying to flash a simple blink example to an ATmega4809, which was compiled with PlatformIO. updiprog cannot read the hex file, however.

After adding a few debug statements, the culprit appears to be the sanity check in IHEX_ReadFile:

updiprog/ihex.c

Lines 148 to 149 in 3741ea7

if (len * 2 + IHEX_MIN_STRING != strlen(str))
return IHEX_ERROR_FMT;

The left side returns 44 while strlen(str) returns 45.

If I just add another + 1 on the left side as an override, the program is flashed correctly and my LED blinks. Is strlen() handling the CRLF (see below) incorrectly?

I'm on Linux 5.5.4-arch1-1 x86_64.

Sources

src/blink.c:

#define F_CPU 3333333UL

#include <avr/io.h>
#include <util/delay.h>

#define LED 7

int main (void)
{
    PORTA.DIR |= (1 << LED);

    while (1)
    {
        PORTA.OUT ^= (1 << LED);
        _delay_ms(500);
    }
}

.pio/build/mega/firmware.hex:

:100000000C9450000C945A000C945A000C945A0012
:100010000C945A000C945A000C945A000C945A00F8
:100020000C945A000C945A000C945A000C945A00E8
:100030000C945A000C945A000C945A000C945A00D8
:100040000C945A000C945A000C945A000C945A00C8
:100050000C945A000C945A000C945A000C945A00B8
:100060000C945A000C945A000C945A000C945A00A8
:100070000C945A000C945A000C945A000C945A0098
:100080000C945A000C945A000C945A000C945A0088
:100090000C945A000C945A000C945A000C945A0078
:1000A00011241FBECFEFCDBFDFE3DEBF0E945C0097
:1000B0000C946E000C940000809100048068809382
:1000C00000048091040480588093040425E186E1B3
:1000D00095E0215080409040E1F7F3CFF894FFCFB6
:00000001FF

The hexdump shows that lines are terminated with \x0d\x0a, i.e. "CRLF":

$ xxd .pio/build/mega/firmware.hex | head -4
00000000: 3a31 3030 3030 3030 3030 4339 3435 3030  :100000000C94500
00000010: 3030 4339 3435 4130 3030 4339 3435 4130  00C945A000C945A0
00000020: 3030 4339 3435 4130 3031 320d 0a3a 3130  00C945A0012..:10  <<<
00000030: 3030 3130 3030 3043 3934 3541 3030 3043  0010000C945A000C

Исправления com.c для Linux

Исли вам будет интересно, внес некоторые изменения в файл com.c для быстрой и надежной работы с последовательным портом под Linux (тестировал только под OpenSUSE и Ubuntu). Добавил также кое-что по-мелочи в ваш код, адаптировал код под сборку динамической библиотеки, добавил пример ее использования в GUI-приложении.

https://github.com/iLya2IK/updiprog

С уважением,

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.