Let Your Data Cross The Bridge

Jack McLeans
4 min readOct 7, 2021

--

“AVR BRIDGE” , a software driven alternative to ASIC GPIO and ADC expanders for I/O constraint MCUs.

ESP-AVR BRIDGE development board

Introduction

The ESP8266 is a very widely used Microcontroller in IoT applications that require WiFi functionality. It’s 32-bit Tensilica Processor, compactness, high durability and power-saving architecture makes it a go-to choice for both beginners and experts. Its compatibility with the Arduino community of boards and software, support for Micropython (We shall cover this interesting feature on a future blog) and low price point has extensively lowered the barrier to entry enabling the design and development of wireless devices by designers of all levels.

AVR-BRIDGE

To resolve this issue I introduce to you “AVR-BRIDGE” (because it is a link between the application software and the peripheral module) a software stack optimised for the Atmega328p to enable it to be used as an expander for both I/O and ADC. It uses “CROSS” commands, similar to “AT” commands used by most wireless modules. The commands are used to configure the Atmega328p peripherals, produce an output from a pin or receive an input. Once a command is received, it is processed and the output transmitted via USART.

Why the “AVR BRIDGE” ?

I started on this after experiencing I/O limitations while working on a project that utilised the ESP8266 and a large number of sensors. Introducing an additional microcontroller with more peripherals provided sufficient design flexibility and also a reduction in cost as compared to using I/O and ADC expander ICs. However, this also meant that some of the application software had to be ported into the new microcontroller. Do you at this point separate the two pieces of application software to different repositories? Do you have a single repository and separate them into different branches? This is a discussion we had to have at this point to properly structure the software.

The “AVR BRIDGE” provides a low level software stack running on the Atmega328p to handle the I/O and ADC peripherals in such a situation. Your application software would remain the same and make requests to the microcontroller peripherals across the “BRIDGE” when needed. For example where a pin was configured as output in the ESP8266 as “pinMode(myPin,OUTPUT);” the same outcome can be achieved on the Atmega328p from the ESP8266 by sending the command “CROSS+OUTPUT_myPin” via USART.

How does it work?

Both configuration and control is attained using the “CROSS ‘’ commands listed in the “commands.h” file (https://github.com/MCLEANS/AVR-BRIDGE-ATMEGA328P/blob/master/commands.h). For example, to toggle an I/O pin on the Atmega328p, the process would be configured as shown below. This sample was implemented using the ESP8266.

/*** This code is uploaded onto the ESP8266*/#include <Arduino.h>void setup(){/* Set communication baudrate */
Serial.begin(9600);
/* Configure Pin B0 as output, The Atmega will respond with "OK" if the process was successful */Serial.println("CROSS+OUTPUT_B0");/* This delay allows time for the Atmega to process the Instruction and respond */delay(5);}void loop(){/* Set the pin High */Serial.println("CROSS+HIGH_B0");delay(1000);Serial.println("CROSS+LOW_B0);delay(1000);}

Receiving an ADC value from a sensor connected to one of the Atmega328p ADC pins also takes an almost similar approach.

/*** This code is uploaded onto the ESP8266*/#include <Arduino.h>void setup(){/* Set communication baudrate */Serial.begin(9600);/* Configure Pin B0 as output, The Atmega will respond with "OK" if the process was successful */Serial.println("CROSS+ADC0");/* This delay allows time for the Atmega to process the Instruction and respond */delay(5);/* Check if there is any serial data available */while(Serial.available() > 0){/* Store the received ADC value */String ADC_value = Serial.readString();}}void loop(){}

The system testing circuit board which contains both the ESP8266 and the Atmega328p was designed using KiCAD can be found at https://github.com/MCLEANS/AVR-BRIDGE-ATMEGA328P-PCB (The hardware is open-source and open to use).

Setting up the compilation environment

Installing Make

Linux$ sudo apt install makeMac OS$ brew install make

Installing avr-gcc

Linux$ sudo apt install gcc-avr binutils avr-libcMac OS$ brew install gcc-avr binutils avr-libc

Installing avrdude

Linux$ sudo apt install avrdudeMac OS$ brew install avrdude

Installing for Windows

The installation procedure for the AVR build environment for windows can be found at https://www.newbiehack.com/MicrocontrollerProgrammingEnvironmentWinAVRInstall.aspx

Compiling the software

$ make

Flashing to the MCU

$ make flash

The software stack is currently under development to add support for SPI and PWM to make the system more scalable across various applications.

Link -: https://github.com/MCLEANS/AVR-BRIDGE-ATMEGA328P

--

--

Jack McLeans

Electronics Engineer | Embedded Systems | Firmware Engineer | Internet of Things | Open Source | KiCAD | EAGLE | C++ Programming | Qt | Python