Embedded Programming

  • group assignment: compare the performance and development workflows for other architectures

    #ArduinoMEGA#NucleoSTM32#ArduinnoIDE#Mbed

  • individual assignment: read a microcontroller data sheet program your board to do something, with as many different programming languages and programming environments as possible

    #ATTINY 412 echo-hello world board#Arduino IDE

Group assignment


Group assignment for this week is to compare the performance and development workflows for other architectures. Our group (Zhengya Gong, Yazan Barhoush, Noora Nyberg and Xinhui Hu) tested STM32 Nucleo-64 development board with STM32F303RE MCU and Arduino Mega board. For the both boards we used LED blinking test, as the LED is the simplest peripheral output.

Arduino MEGA 2560 is designed for projects that require more I/O pins, sketch memory and RAM than Arduino UNO, it is the biggest Arduino microcontroller. The Mega 2560 board can be programmed with the Arduino Software (IDE).

STM32 Nucleo-64 is a development platform with STM32F303RE MCU. Datasheet STM32F303RE. The STM32 Nucleo boards integrate the ST-LINK/V2-1 debugger and programmer. The ARDUINO® Uno V3 connectivity support and the ST morpho headers provide an easy means of expanding the functionality of the Nucleo open development platform with a wide choice of specialized shields.

Arduino are the most popular boards for the begginers with a full functionality for the embedded programming. STM32 Nucleo boards, however, have the better performance in general, more inputs/outputs and debugging options available. But again, STM32 Nucleo boards don't have EEPROM for storing the permanent varibales for a system reboot, as the 4 KB internal EEPROM of Arduino MEGA.

The comparison table of the main features of the tested boards and performance:

Properties Arduino Mega STM32 Nucleo-64
Manufacture Atmel Corporation STMicroelectronics
Microcontroller ATmega2560 STM32 microcontroller in LQFP64 package
Processor 8-bit RISC processor core ARM®32-bit Cortex®-M4 CPU with FPU, RISC
Architecture Harvard Harvard
Flash Memory 256 KB 512 KB
SRAM 8 KB 64 KB
EEPROM N/A 4 KB
Operating voltage 5 V 2 to 3.6 V
Communication interface 54 Digital I/O Pins, 15 Digital I/O Pins with PWM, 16 Analog Input Pins, 4 UARTs Up to 115 fast I/O Pins, Up to 5 USART/UARTs, Up to 4 SPIs

Performance

CPU frequency 16 MHz 72 MHz
CPU Speed 16 DMIPS 90 DMIPS
...

Fig 1. Arduino Mega board. Image credit

Blink test for Arduino Mega with Arduino IDE:

  • Connect board with connector USB A to B
  • In Arduino IDE -> Choose Tools and port as follows below in the figure:
  • We used the simple sample sketch from Arduino IDE, which will make pin 13 LED on the board start to blink. File -> Examples -> Basics -> Blink Download sketch Blink test for Arduino Mega
...

Fig 1. Arduino IDE settings for programming the Arduino Mega board.

Arduino Mega board built-in LED is blinking with 1000 ms interval.

...

Fig 2. NUCLEO-F303RE board and pinout. Image credit

LED blink test with Arduino IDE for NUCLEO-F303RE:

  • Connect board mini B to USB cable
  • We installed Board Manager for the Nucleo board: File -> Preferences -> Additional Boards Manager URLs -> https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json. Then Tools -> Boards Manager -> Search for STM32 Cores -> Install
  • Tools and port as follows below in the figure:
  • We found the LED blink test on the page of the previous year students (link). File -> Open -> Fabblinking -> Upload the sketch Download sketch Fabblinking for NUCLEO-F303RE
...

Fig 3. NUCLEO-F303RE programming in Arduino IDE. Image credit

LED blink test with Mbed for NUCLEO-F303RE :

  • Create an account on Mbed
  • Connect board mini B to USB cable
  • Search for NUCLEO-F303RE -> Open Mbed Compiler
  • New -> Create new programm -> Platform: NUCLEO-F303RE -> Template: mbed-os-example-mbed5-blinky -> Program name ->
  • main.cpp file containes the main code, we put an interval 500 ms
  • Compile -> Save file -> Copy file into the Node_F303RE
... ... ... ...

Fig 4. NUCLEO-F303RE board programming with Mbed

NUCLEO-F303RE built-in LED is blinking with 500 ms interval.

Individual assignment


ATTINTY412 microchip datasheet


Descriptions and specifications of ATTINTY412 can be found on a webpage of the product.

ATTINTY412 is a 8-bit AVR® RISC (Reduced Instruction Set Computing) architecture microcontroller. Some of the main features:

  • AVR® 8-bit CPU running at up to 20MHz
  • 2/4KB In-system self-programmable Flash Memory
  • 64/128B EEPROM
  • 128/256B SRAM
  • Single Pin Unified Program Debug Interface (UPDI)
  • 6 Programmable I/O Lines and 8-pin SOIC150
  • 128/256B SRAM

...

Fig 1. ATTINY412 pinout

UPDI - Unified Program and Debug Interface

Programming and debugging is done through the UPDI Physical interface (UPDI), which is a 1-wire UART based interface using the RESET pin for data reception and transmission. Clocking of UPDI is done by an internal oscillator. (Features, Overview, Block diagram, etc. from p. 379)

Communication through the UPDI is based on standard UART serial communication (Universal Synchronous and Asynchronous Receiver-Transmitter).

Communication lines of USART are:

Receiver - RX (PA1) and the Transmitter TX (PA2).

UPDI can access the entire I/O and data space of the device

In my echo-board, LED is connected to PA7, which is in ATTINY412 is pin number 3
and the BUTTON to PA6, which is pin number 2.

...

Fig 2. Schematic of an echo-board

Programming ATtiny 412 with Arduino IDE


For the beginners like me, Arduino IDE would be the best choice to start programming. There are many examples with comprehensive explanations available. I started with using programs, made by the students last year.

With this knowledge I'm ready to proceed:

  • Simply connect programmer with 3 pin female connector of the board
  • I took the Arduino sketch example from Arash's page, to make LED blinking the number of times the BUTTON is pressed. I changed the pin's number for BUTTON and LED.
  • Settings in Arduino IDE:
    ...

    Fig 3. Arduino IDE to upload the sketch on the board.

Unfortunatly, nothing have happened. Therefore, I decided to simplify the program, just to check if my LED works. I used the simplest available Arduino sketch:


void setup() {
// put your setup code here, to run once:
pinMode(3, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(3, HIGH);
delay(100);
digitalWrite(3, LOW);
delay(100);
}

Again, nothing happened, even though sketch uploading was successful. To ensure, that programming is happening, I tried the previous echo program with Putty, which worked. After I uploaded the new program, I checked Putty, and it stopped responding, meaning, that programming is happening succesfully. Eventually, HUGE THANKS to Gleb, and our intructor Ari, who sent us help just few minutes later, after I succeed with a programming.


The ISSUE is that the pinout of Attiny412 is different from the pinout in Arduino IDE megaTinyCore. This Gitlab page reports: "WARNING: On pre 2.0.0 versions of megaTinyCore, the UART defaulted to the alternate positions This matches the megaTinyCore 412/402 Rev. - and Rev. A breakout boards below. In 2.0.0 and later, this is no longer the case! You must call Serial.swap(1) prior to Serial.begin() to move it to the alt pins (or connect your adapter to pins 0 and 1 instead of 2 and 3)."

...

Fig 4. The correct Attiny412 pinout.

  • VDD: Supply voltage.
  • GND: Ground.
  • Digital pins: PA0, PA1, PA2, PA3, PA4, PA5, PA6.
  • Analog pins: PA1, PA2, PA3, PA4, PA5, PA6.
  • UPDI Programming: PA0 (physical pin number 6).
  • External Clock: PA3.

Therefore, the pins in the sketch should be defined as:

Output LED pin number is 1
and the Input BUTTON pin is 0.

I'm trying the LED pin set to 1, and it works now!

Another example, when the button is pressed, the light is turned off:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 0; // the number of the pushbutton pin
const int ledPin = 1; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int lastButtonState = 0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState != lastButtonState) {
if (buttonState == HIGH){
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}

}
lastButtonState = buttonState;
}
  
  

One more code, which joins the serial monitor with LED function: typing 1 in a serial monitor the LED will light up, and typing 0 with turn it off.
int outPin = 1;
void setup(){

pinMode(outPin,OUTPUT);
Serial.begin(112500);
Serial.println("Enter 1 or 0");
}
void loop(){
if(Serial.available()>0)
{
char ch = Serial.read();
if (ch == '1')
{
digitalWrite(outPin,1);
}
else if (ch == '0')
{
digitalWrite(outPin,LOW);
}
}
}

...

Fig 5. Sending 1 through the Serial lighten up the LED.

Arduino sketches:

Reflection

Programming requiers not only coding knowledge, but also some extra datasheet reading for the electronics. Quite challenging, but starting with simple Arduino IDE examples and tutorials will ease the pain of embedded programming for the beginners.