Entertainment at it's peak. The news is by your side.

Bare-metal programming the tinyAVR 0 microcontrollers


All you will have is a textual recount editor, a makefile and a USB-serial cable.


Are you an 8-bit or a 32-bit programmer?

At OMZLO, we enjoy been mainly focussing our style efforts on more moderen 32-bit Arm-Cortex chips (STM32 and SAMD), which typically provide more RAM, more elope, more peripherals, at a same or lower mark-point than older 8-bit MCUs. Nonetheless 8-bit MCUs are removed from listless. Microchip has particularly launched a novel sequence of chips, collectively branded as the “tinyAVR 0-sequence”, which provide more contemporary peripherals than the older AVR chips at a in point of fact competitive mark point. They look fancy the generous candidate for uncomplicated products that produce no longer need the full substances and gorgeous-tuning capabilities of the more moderen 32-bit MCUs. 8-bit MCUs are moreover considerably more efficient to program, which translates into faster style time.

Due to success of the Arduino UNO, there are hundreds tutorials online that display how to program 8-bit Atmega328 microcontrollers and their cousins fancy the Attiny85 the usage of affirm register safe entry to, without the Arduino language or any vendor IDE reminiscent of Atmel Studio. Lovely google “atmega328 blinky”. All you will have is an AVR C-compiler, a textual recount editor, avrdude, and an AVR programmer. Some sources even characterize how to moreover trace the electronics wished to safe a conventional atmega328 working on a breadboard. On the different hand, it’s exhausting to search out the equivalent knowledge for these more moderen “tinyAVR 0” chips.

Undoubtedly, Microchip presents the full instruments main to program these more moderen “TinyAVR” MCUs with their house windows-only IDE. There are moreover “Arduino cores” for these kinds of more moderen “TinyAVR” MCUs that will allow you to program them with the Arduino IDE. Nonetheless again, while you settle to jot down code for MCUs in “baremetal” style, with your favourite textual recount editor, a makefile, and a c-compiler, there are few sources accessible online.

In this weblog post, we can characterize how to program a blinky firmware on an Attiny406, from the bottom up, the usage of the most effective instruments. Many of the issues described here will also be without problems transposed to various TinyAVR MCUs. Our capacity is continuously guided in the direction of macOS or Linux customers, but have to gentle moreover be appropriate in an MS-Dwelling windows ambiance with about a minor modifications.


We made up our minds to play with the Attiny406, with a study about of the usage of it within the long elope to change the Attiny45 we currently employ on the PiWatcher, our Raspberry-Pi watchdog. The Attiny406 has 4K of flash house, 256 bytes of RAM, and would possibly per chance elope at 20Mhz without an exterior clock source.

One of many preferrred differences between the unique TinyAVR MCUs and the older classic AVR MCU fancy the Attiny85 is that the more moderen chips employ a definite programming protocol called UPDI, which requires only 3 pins, versus the 6-pin ISP on the classic AVRs.

Quite evaluation reveals that programming TinyAVRs with UPDI will also be finished with a straightforward USB-to-serial cable and a resistor, thanks to a python instrument called pyupdi, which implies the following connection contrivance for firmware upload:

                        Vcc                     Vcc
                        +-+                     +-+
                         |                       |
 +---------------------+ |                       | +--------------------+
 | Serial port         +-+                       +-+  AVR system        |
 |                     |      +----------+         |                    |
 |                  TX +------+   4k7    +---------+ UPDI               |
 |                     |      +----------+    |    |                    |
 |                     |                      |    |                    |
 |                  RX +----------------------+    |                    |
 |                     |                           |                    |
 |                     +--+                     +--+                    |
 +---------------------+  |                     |  +--------------------+
                         +-+                   +-+
                         GND                   GND


We created a minimalistic breakout board for the Attiny406. The board will also be powered by 5V by USB or a lower 3.3V by dedicated VCC/GND pins. An LED and a button had been moreover fitted on the board. For testing capabilities, we made up our minds to embed the 4.7K resistor wished for the UPDI programming straight away within the hardware (i.e. resistor R2).
This presents us the following schematic:



The following breakout board is small and suits conveniently on a minute breadboard. The trace info are shared on aisler.procure.

Programming the Attiny406 on the board with a USB-serial cable is done by connecting the headers on the board edge:



We installed pyupdi following the directions offered on their webpage.

We linked our USB-Serial cable to the board with the 4 dedicated UPDI pins accessible on the board. Our USB-Serial converter reveals up as the file /dev/tty.usbserial-FTF5HUAV on a MacOS system.

To check that the programmer acknowledges the Attiny406, that you just might relate a characterize equivalent to the following, adapting the path for the USB-serial converter to your setup:

pyupdi -d small406 -c /dev/tty.usbserial-FTF5HUAV -i

This would possibly per chance presumably gentle lead to the following output if all goes well:

Tool data: {'family': 'tinyAVR', 'nvm': 'P:0', 'ocd': 'D:0', 'osc': '3', 'device_id': '1E9225', 'device_rev': '0.1'}

The C compiler

The normal avr-gcc accessible on macOS with homebrew did no longer appear to acknowledge the Attiny406 as a compiler target, so we went off to install the avr-gcc compiler offered by Microchip, which is accessible here. Downloading the compiler requires you to produce an yarn on the Microchip web website online, which is a little bit disturbing.

AVR toolchain link

As soon as downloaded, we extracted the offered archive in a dedicated itemizing. The bin itemizing within the archive have to gentle be added to the PATH variable to safe your existence simpler. Assuming the downloaded compiler is kept within the itemizing $HOME/Src/avr8-gnu-toolchain-darwin_x86_64, the PATH will also be altered by along with the following line to your .bash_profile file:

export PATH=$PATH:$HOME/Src/avr8-gnu-toolchain-darwin_x86_64/bin/

Newer Attiny MCUs are no longer supported out of the box by the Microchip avc-gcc compiler. It is top to download a dedicated Attiny Tool Pack from their web website online, as shown below:

AVR toolchain link

The following downloaded Tool Pack is named Atmel.ATtiny_DFP.1.6.326.atpack (or same reckoning on versioning). Though the extension is .atpack, the file can be a zipper archive. We modified the extension to .zip and extracted the kit within the itemizing $HOME/Src/Atmel.ATtiny_DFP.1.6.326 subsequent to the compiler info.

C program

We created the following program that blinks the LED on pin PB5 of our Attiny board at a frequency of 1Hz.


int predominant() {
    _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0); // quandary to 20Mhz (assuming fuse 0x02 is quandary to 2)

    PORTB.DIRSET = (1<<5);
    for (;;) {
        PORTB.OUTSET = (1<<5);
        PORTB.OUTCLR = (1<<5);

The code looks very equivalent to what you might notion on a classic AVR “blinky” program. One considered replace is the usage of buildings to safe entry to various registers of the MCU: e.g instead of surroundings bits in PORTB, you safe entry to PORTB.DIRSET.

The assorted considered replace is the clock setup code _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, 0). Out of the box, at reset, the Attiny406 runs at 3.33Mhz, which corresponds to a harmful frequency of 20Mhz with a 6x clock divider utilized. To permit the plump 20Mhz elope, the register CLKCTRL.MCLKCTRLB is cleared. Due to this register wishes to be web towards accidental modifications, the Attiny406 requires a particular programming sequence to alter it. Happily, that is natively supplied by the macro _PROTECTED_WRITE. Extra info are accessible in within the Attiny406 datasheet.

In comparability with an STM32 or a SAMD21, the code is blissfully straightforward.


We choose the following itemizing structure where:

  • Src/Atmel.ATtiny_DFP.1.6.326/ is the role of the Microchip Tool Pack
  • Src/attiny406-check/ is the itemizing where the code above is kept in a file called predominant.c

Compiling the code will also be done by issuing the following characterize interior attiny406-check/ itemizing,:

avr-gcc -mmcu=attiny406 -B ../Atmel.ATtiny_DFP.1.6.326/gcc/dev/attiny406/ -O3 -I ../Atmel.ATtiny_DFP.1.6.326/encompass/ -DF_CPU=20000000L -o attiny406-check.elf predominant.c

An -O optimization flag is required to safe the _delay_ms() perform calls work efficiently, apart from defining the variable F_CPU to contemplate the expected chip clock elope. The leisure of the parameters provide the role of the Attiny406 system-impart info we previously extracted from the Tool Pack.

Uploading the firmware to the MCU requires a conversion to the intel HEX layout and a call to the pyupdi instrument. To handle all these steps, we created a straightforward Makefile.

ELF=$(notdir $(CURDIR)).elf  
HEX=$(notdir $(CURDIR)).hex

CFLAGS=-mmcu=attiny406 -B ../Atmel.ATtiny_DFP.1.6.326/gcc/dev/attiny406/ -O3
CFLAGS+=-I ../Atmel.ATtiny_DFP.1.6.326/encompass/ -DF_CPU=$(F_CPU)
LDFLAGS=-mmcu=attiny406 -B ../Atmel.ATtiny_DFP.1.6.326/gcc/dev/attiny406/

all:    $(HEX)  

$(ELF): $(OBJS)
                $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

$(HEX): $(ELF)
                avr-objcopy -O ihex -R .eeprom $< $@

flash:  $(HEX)
                pyupdi -d small406 -c /dev/tty.usbserial-FTF5HUAV -f attiny406-check.hex

                pyupdi -d small406 -c /dev/tty.usbserial-FTF5HUAV -fr

                rm -rf $(OBJS) $(ELF) $(HEX)

To assemble the code, we merely kind safe. Uploading is done with safe flash. This Makefile will also be additional enhanced as wished.


With the preferrred instruments, baremetal programming on the unique TinyAVR MCUs is as straightforward as on its older AVR cousins.

In the event you enjoy programming pointers for the AVRTiny, please fragment them with us on on Twitter or within the comments below.


No commentary but

Traipse away a commentary

Read More

Leave A Reply

Your email address will not be published.