Skip to content

8. Embedded Programming

“Embedded programming is the most pain in Fab Academy. Don’t go crazy this week.”

ATtiny412 datasheet

I’m already somewhat familiar with data sheets of various electronic components, so I won’t go listing basic features of ATtiny412. Instead, I try to summarize what new did I learn.

Later I found out that provided datasheet was old and has some errors. Updated datasheet is also now available in my repository. Thanks Erwin Kooi for pointing that out!

I knew that AVR cpu runs on Reduced Instruction Set, but I’m not that familiar with instruction sets, other than the term itself. I was surprised to read that a cpu this small supports 135 instructions! Without any expert knowledge I would’ve guessed the number to be in range of 20-50.

16 MHz and 20 Mhz clock signals are produced by the same oscillator: it can operate at different frequencies. I thought those are produced via multiplier, similarly to desktop PC:s.

Two D-flipflops are used to synchronize inputs, just like I have done in Digital Techniques’ courses exercises.

ATtiny412 has a programmable bandgap voltage reference that is able to operate at 5 different voltages, ranging from 0.55 V to 4.3 V. I didn’t expect that many supported voltages. I expected ATtiny to support one 1.25 V bandgap and scale it by some factor.

UART can operate up to 1/2 of clock rate, and USART can operate up to 1/8 of clock rate. Before Fab Academy UART was just a name of serial communication protocol that I had somehow avoided. I expected it to be slow, from 9600 baud to 115200 baud, but this chip can do up to 2500000 or 10000000 baud (UART/USART).

From Electrical characteristics I learnt that maximum sink or source current of pin group (those PA and PB things) is 100 mA. Pull-up resistor has a typical value of 35 kOhm, minimum and maximum values are 25 and 50 kOhm.

At Conventions section I learnt that 32 kHz means 32000 kHz, but 32 KHz means 32768 Hz.

Program your board

How to program using Arduino IDE

After writing code in Arduino, I check Tools menu so that I have correct Board, Port and Programmer selected, then I can compile and upload my code by pressing Upload button. Arduino IDE

Code I wrote was “blink2” which blinks two leds on my board, varying the delay by pressing on-board button.

Note! In order to program ATtiny412 using Arduino IDE, you must have megaTinyCore board library installed. Installation was done in week06, detailed instructions available there.

Group work - Comparing architectures

For this week’s group work, we will compare AVR, STM32 and ESP32; the speed of ring oscillators they can make, their pinout etc. Group work available at Mona’s page.

AVR - ATtiny412

Later I visited FabLab and wanted to try the group work task on ATtiny412 board, so I wrote simple ring oscillator for the echohello board I made in Electronics design week.

#define IN  2
#define OUT 3

void setup() {
  pinMode(OUT, OUTPUT);
  pinMode(IN,  INPUT);
  digitalWrite(OUT, HIGH);
}

void loop() {
  digitalWrite(OUT, !digitalRead(IN) );
}

I chose the pins 2 and 3 so that I can easily short them with a jumper at the FTDI header since they are next to each other.

ATtiny412 ring oscillator

I also redid the same code on my STM32 Nucleo-64 (source code, binary), and found some inconsistensies.

Many pulses running and overlapping One long pulse found Long pulse measured Regular pulse measured

Somehow there is 2.5 us extra length in some pulses. Probably some STM32-specific overhead related to “housekeeping tasks”.

Further ideas

Some ideas to expand this week: - [ ] I want to try other IDEs than Arduino - [ ] Could I program microcontroller from Visual Studio Code - [ ] I’d like to try Makefiles - [ ] Atmel Studio (Microchip studio) is something that I should expect to work easily - [ ] Atmel Start was mentioned somewhere, check what it is - [ ] Can I program ATtiny on some other programming language? - [ ] Python can doo just anything these days, why not ATtiny? - [ ] Something more exotic, like Rust perhaps? - The Rust Programming Language should be first book to explore - Rust’s embedded page suggests Discovery as first book to embedded developing in general - Don’t be fooled by it’s name, The Embedded Rust book should be read if embedded development is already familiar - Hackaday’s Embedded Rust Hack Chat and it’s event - Embedded Rust GitHub page seems like a really resourceful page for links for further reading


Last update: May 12, 2021