Close

Preparing Arduino IDE to write sketches for the ATtiny 0- and 1-series

A project log for ATtiny 1-series with Arduino support

Creating a break-out board for the ATtiny1616 where sketches can be uploaded from Arduino with the Arduino UNO or a modified AVR JTAG ICE

sander-van-de-borSander van de Bor 06/04/2019 at 01:361 Comment

Arduino is a great tool for makers and hobbyists to develop code for tons of different micro-controllers. For the user the code always looks very similar and most code can be compiled for different devices. But in order to make this all happen a lot is happening behind the scenes of Arduino, and a lot of different parties are involved.

Different “translations” are done from the sketch in Arduino to the actual machine code which will be uploaded to the micro-controller. The language the micro-controller is speaking differs for each controller and must be provided by the manufacturer. In this case the ATtiny 0- and 1-series are provided by Microchip and Microchip provides the language in a so-called Device Family Pack (DFP).

When you compile a sketch in Arduino with verbose output turned on (in settings) you will get the location of the compiler and the DFP in the Arduino IDE output window. For example, when you compile a sketch for the Arduino UNO you might see lines like:

C:\Users\username\AppData\Local\Arduino15\packages\arduino\tools\avr-gcc\5.4.0-atmel3.6.1-arduino2\lib\gcc\avr\5.4.0

When you browse to that folder it will show all the different devices that are similar to the micro-controller used on the Arduino UNO (ATMEGA328P), with a complete list shown in the folder device-specs. The files in the device-specs folder are loaded, specific for the micro-controller, to tell the compiler where to find libraries for this device.  

Some might notice that the ATtiny1616 is part of the list. Adding the ATtiny 0- and 1-series DFP to Arduino was requested a couple of months ago and they were so kind to make it standard in the latest releases. But just updating the DFP with new packages will not always do the trick. Sometimes new functions are added, and these must be understood by the translator, in this case the compiler.

Arduino is using avr-gcc compiler for the 8-bit microcontrollers. Version 5.4.0 was sufficient for micro-controllers like the ATMEGA328P and the ATtiny series like the ATtiny85 and ATtiny84a. Unfortunately, this compiler version had issues with the new DFP for the 0- and 1-series and a newer compiler version is required (and available).

While the DFP is provided by the manufacturer of the chip, the actual Arduino Core is developed by the Arduino team. It is a collection of libraries which are used to translate the language spoken in Arduino to commands or write to registers described in the DFP. For example, we are all familiar with the delay function. On the Arduino UNO the following function is used (all in the background, so you probably did not even know you were calling out functions):

void delay(unsigned long ms)
{
    uint32_t start = micros();

    while (ms > 0) {
        yield();
        while ( ms > 0 && (micros() - start) >= 1000) {
            ms--;
            start += 1000;
        }
    }
}

Unfortunately, this will not work for the new ATtiny 0- and 1-series, a new core is required.

Luckily enough the Arduino team did come out with a new core and compiler when they released the Arduino UNO WIFI REV2. This new device is using the ATMEGA4809, part of the megaAVR 0-Series, and in the same family as the ATtiny 0- and 1-series. You can see the changes by adding the board from the board manager and just compile an empty sketch with the Arduino Uno WiFi Rev2 board selected. You will notice that the avr-gcc version 7.3.0 is used. The Arduino core is different too, for example the function for delay is now as follows:

void delay(unsigned long ms)
{
    uint32_t start_time = micros(), delay_time = 1000*ms;

    /* Calculate future time to return */
    uint32_t return_time = start_time + delay_time;

    /* If return time overflows */
    if(return_time < delay_time){
        /* Wait until micros overflows */
        while(micros() > return_time);
    }

    /* Wait until return time */
    while(micros() < return_time);
}

With the Arduino core, compiler and DFP in place it should be pretty straight forward to make a variant for the ATtiny series.

Next log will be on creating board variants files.

Discussions

Simon Merrett wrote 06/04/2019 at 19:53 point

Can't wait for the next log! 

  Are you sure? yes | no