David Johnson-Davies' New Universal TinyI2C Routines Can Slash Your AVR Project's RAM Requirements

Now compatible with "just about any" AVR chip, TinyI2C lowers resource usage while offering unlimited transmission and other advantages.

Gareth Halfacree
3 years agoHW101

Embedded engineer David Johnson-Davies has released an update to his popular TinyI2C routines, offering "universal" support for "just about any Microchip/Atmel AVR processor" — and taking up just a fifth the RAM required by default libraries.

"The main difference between these routines and the standard Arduino Wire library is that these don't use buffers," Johnson-Davies explains of the next-generation version of the routines originally released back in 2018, "so have much smaller memory requirements and don't impose a limit on transmissions."

While the original release was entirely functional, the new version aims to address a key issue: Fragmentation of I2C peripherals within the AVR ecosystem. "Over the years different generations of AVR chips have featured three different, incompatible peripherals to handle I2C," Johnson-Davies explains. "[The] Universal Serial Interface (USI) peripheral; 2-Wire Serial Interface (TWI) peripheral; [and the] Two-Wire Interface (TWI) peripheral," the latter being a new TWI variant available in the company's latest parts.

The new universal release of the TinyI2C routines works around this incompatibility in a simple way: Providing three separate blocks of code, selected during compilation via #if defined statements. Just set up your development environment correctly, and the routines will handle the rest.

For those who had previously been using Johnson-Davies' TinyI2C routines, the new release effectively replaces the original TinyI2C as well as the newer TinyMegaI2C while bringing major benefits over the Arduino Wire I2C routines: No need to assign buffers, meaning RAM usage is considerably lower; no limits on transmission length; a flexible read routine; and support for polling rather than interrupts.

To test the new release out, Johnson-Davies put together two sample programs. One uses a pair of LED matrices as a display for an I2C bus scanner; the other uses them to display thermometer data. The latter also serves as a benchmark: "Depending on the platform," Johnson-Davies finds, "the TinyI2C code is typically about half the size of the Arduino Wire code, with 5 per cent of the RAM usage."

A full write-up of the routines is available on Johnson-Davies' website, with source code available on the project's GitHub repository under the permissive Creative Commons-Attribution 4.0 International license.

Gareth Halfacree
Freelance journalist, technical author, hacker, tinkerer, erstwhile sysadmin. For hire: freelance@halfacree.co.uk.
Latest articles
Sponsored articles
Related articles
Latest articles
Read more
Related articles