Coder Social home page Coder Social logo

twiboot's Introduction

twiboot - a TWI / I2C bootloader for AVR MCUs

twiboot is a simple/small bootloader for AVR MCUs written in C. It uses the integrated TWI or USI peripheral of the controller to implement a I2C slave. It was originally created to update I2C controlled BLMCs (Brushless Motor Controller) without an AVR ISP adapter.

twiboot acts as a slave device on a TWI/I2C bus and allows reading/writing of the internal flash memory. As a compile time option (EEPROM_SUPPORT) twiboot also allows reading/writing of the whole internal EEPROM memory. The bootloader is not able to update itself (only application flash memory region accessible).

Currently the following AVR MCUs are supported:

AVR MCU Flash bytes used (.text + .data) Bootloader region size
attiny85 954 (0x3BA) 512 words
atmega8 786 (0x312) 512 words
atmega88 810 (0x32A) 512 words
atmega168 810 (0x32A) 512 words
atmega328p 810 (0x32A) 512 words

(Compiled on Ubuntu 18.04 LTS (gcc 5.4.0 / avr-libc 2.0.0) with EEPROM and LED support)

Operation

twiboot is installed in the bootloader section and executed directly after reset (BOOTRST fuse is programmed). For MCUs without bootloader section see Virtual bootloader section below.

While running, twiboot configures the TWI/USI peripheral as slave device and waits for valid protocol messages directed to its address on the TWI/I2C bus. The slave address is configured during compile time of twiboot. When receiving no messages for 1000ms after reset, the bootloader exits and executes the main application at address 0x0000.

A TWI/I2C master can use the protocol to

  • abort the boot timeout
  • query information about the device (bootloader version, AVR signature bytes, flash/eeprom size, flash page size)
  • read internal flash / eeprom memory (byte wise)
  • write the internal flash (page wise)
  • write the internal eeprom (byte wise)
  • exit the bootloader and start the application

As a compile time option (LED_SUPPORT) twiboot can output its state with two LEDs. One LED will flash with a frequency of 20Hz while twiboot is active (including boot wait time). A second LED will flash when the bootloader is addressed on the TWI/I2C bus.

Virtual Bootloader Section

For MCUs without bootloader section twiboot will patch the vector table on the fly during flash programming to stay active. The reset vector is patched to execute twiboot instead of the application code.

Another vector entry will be patched to store the original entry point of the application. This vector entry is overridden and MUST NOT be used by the application. twiboot uses this vector to start the application after the initial timeout.

This live patching changes the content of the vector table, which would result in a verification error after programming. To counter this kind of error, twiboot caches the original vector table entries in RAM and return those on a read command. The real content of the vector table is only returned after a reset.

Build and install twiboot

twiboot uses gcc, avr-libc and GNU Make for building, avrdude is used for flashing the MCU. The build and install procedures are only tested under linux.

The selection of the target MCU and the programming interface can be found in the Makefile, TWI/I2C slave address and optional components (EEPROM / LED support) are configured in the main.c source.

To build twiboot for the selected target:

$ make

To install (flash download) twiboot with avrdude on the target:

$ make install

Set AVR fuses with avrdude on the target (internal RC-Osz, enable BOD, enable BOOTRST):

$ make fuses

TWI/I2C Protocol

A TWI/I2C master can use the following protocol for accessing the bootloader.

Function TWI/I2C data Comment
Abort boot timeout SLA+W, 0x00, STO
Show bootloader version SLA+W, 0x01, SLA+R, {16 bytes}, STO ASCII, not null terminated
Start application SLA+W, 0x01, 0x80, STO
Read chip info SLA+W, 0x02, 0x00, 0x00, 0x00, SLA+R, {8 bytes}, STO 3byte signature, 1byte page size, 2byte flash size, 2byte eeprom size
Read 1+ flash bytes SLA+W, 0x02, 0x01, addrh, addrl, SLA+R, {* bytes}, STO
Read 1+ eeprom bytes SLA+W, 0x02, 0x02, addrh, addrl, SLA+R, {* bytes}, STO
Write one flash page SLA+W, 0x02, 0x01, addrh, addrl, {* bytes}, STO page size as indicated in chip info
Write 1+ eeprom bytes SLA+W, 0x02, 0x02, addrh, addrl, {* bytes}, STO write 0 < n < page size bytes at once

SLA+R means Start Condition, Slave Address, Read Access

SLA+W means Start Condition, Slave Address, Write Access

STO means Stop Condition

A flash page / eeprom write is only triggered after the Stop Condition. During the write process twiboot will NOT acknowledge its slave address.

The multiboot_tool repository contains a simple linux application that uses this protocol to access the bootloader over linux i2c device.

The ispprog programming adapter can also be used as a avr910/butterfly to twiboot protocol bridge.

TWI/I2C Clockstretching

While a write is in progress twiboot will not respond on the TWI/I2C bus and the TWI/I2C master needs to retry/poll the slave address until the write has completed.

As a compile time option (USE_CLOCKSTRETCH) the previous behavior of twiboot can be restored: TWI/I2C Clockstretching is then used to inform the master of the duration of the write. Please note that there are some TWI/I2C masters that do not support clockstretching.

Development

Issue reports, feature requests, patches or simply success stories are much appreciated.

twiboot's People

Contributors

jlefley avatar orempel 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  avatar

twiboot's Issues

Support for Attiny85?

Awesome work on twiboot! Any chance offering support for Tiny85? I changed the MCU in the Makefile and it seems some registers are Tiny85-specific..

twiboot timeout issue

Hi,
i ported twiboot to my Tiny84 target.
So far, it is working. I am able to download the code and start a small demo application via the I2C command.

Unfortunately, it is not starting without any I2C communication (timeout).
Any hint?

For debugging, i added some fast LED toggling just before jump_to_app();
The problem, is, that my app code is just not working, when it is started by timeout.

Additional, i think there is a concept error with the appvect_save variable.
I think, the appvect_save & rstvect_save are initialized with the wrong value in main.
When I check/compare without writing it, then i get a mismatch with the original code.

I think, it should be:
rstvect_save[0] = buf[APPVECT_PAGE_OFFSET];
rstvect_save[1] = buf[APPVECT_PAGE_OFFSET + 1];
appvect_save[0] = ?;
appvect_save[1] = ?;

I added a simple CRC16 checksum query. This is very useful doing a quick code compare without read the entire flash to the master.

Later, i like to add support for the AT Tiny841 too. This device has a different I2C slave implementation.

Shall i provide a pull request for this?

Maik

License

I'd like to use this code in a product I am developing, but I cannot accept the GPL 2 license attached.

Are you open to changing the license? LGPL?

Customizable CPU frequencies

Currently, my ATmega328p runs at 16MHz on an external clock (I set the fuse bits manually in a copy of the Makefile you provide). However, it would be nice to utilize a custom CPU frequency that doesn't require me manually editing the Makefile. This isn't something important, as it doesn't impact my project much (just that I have to be faster when stopping the abort boot timeout ๐Ÿ˜). However, it would be nice to have.

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.