Skip to content

9. Embedded programming

Assignment

Group task

  • Compare the performance and development workflows for other architectures.

Individual task

  • Read a ATTiny Data sheet.
  • Program my board to do something, with as many different programming languages and programming environments as possible.

Group Assignment

For this test, I did it with Alok and as a group, we also did other tests. More details for the other tests can be found in Jobin’s and Marjo’s pages, for example. The steps for this are in Alok’s page and below I provide a short summary.

We tested the NUCLEO-F429ZI board with STM32CubeMX, a graphical tool that allows the easy configuration of microcontrollers and microprocessors. The STM32 Nucleo-144 board provides an affordable and flexible way for users to try out new concepts and build prototypes with the STM32 microcontrollers, choosing from the various combinations of performance, power consumption and features 1.

The board support package came from STM itself and we used FreeRTOS as a scheduler to blink a couple of LEDs. Eclipse was used as an IDE and Openocd was used for debugging. We just tested the process for blinking the LEDS on the board.

The first step is creating a new project and selecting the microcontroller. In our case, it was F429ZI board.

Then the settings for the selected board are defined and based on these settings, the code for testing can be generated.

Below are snippet of the code and video of the blinking LED.

 void StartDefaultTask(void const * argument)
{

  /* USER CODE BEGIN 5 */
  /* Infinite loop */
  for(;;)
  {
    HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_RESET);
    osDelay(300);
    HAL_GPIO_WritePin(GPIOB, LD3_Pin|LD2_Pin, GPIO_PIN_SET);
    osDelay(300);
  }
  /* USER CODE END 5 */ 
}


void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(0)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

LED Video

STM32 is a family of 32-bit microcontroller integrated circuits based on the RISC (reduced instruction set computer) architecture developed by Advanced RISC Machines (ARM). The STM32 F4-series, compared to earlier versions, has higher clock speed, 64 KB CCM static RAM, full duplex, improved real-time clock, and faster ADCs. All devices offer one 12-bit ADC, a low-power RTC, six general-purpose 16-bit timers including one PWM timer for motor control, two general-purpose 32-bit timers. They also feature standard and advanced communication interfaces.

ARM can be used as both a microcontroller (when combined with ROM, RAM and other peripherals to a single chip like LPC2148 or the NUCLEO-F429ZI board we tested above) and a microprocessor whereas AVR are only microcontrollers. ARM has higher processing power and capability compared to AVR and supports more communication protocols. ARM has a bus width (or data pipe) of 32-bit, but also available in 64-bit whereas AVR has an 8/32-bit bus width. Typically, microcontrollers with higher bus width are faster as the the number of cycles required to perform tasks decrease. Both ARM and AVR have low power consumption 2.

Individual assignment

Read ATtiny data sheet

I skimmed through the over 200-page Data sheet for the ATTiny to get an overall idea of the architecture and the key features which mostly went over my head. In addition to skimming through the data sheet, I also went through the following sites:

The data sheet has information about the pin configurations, the core of the CPU, memories, clock system, system control and reset, I/O ports, analog to digital converters amongst other features.

Overview The high-performance, low-power microcontroller combines 4KB ISP flash memory, 256-Byte EEPROM, 256B SRAM, 12 general purpose I/O lines, 32 general purpose working registers, an 8-bit timer/counter with two PWM channels, a 16-bit timer/counter with two PWM channels, internal and external interrupts, an 8-channel 10-bit A/D converter, a programmable gain stage (1x, 20x) for 12 differential ADC channel pairs, programmable watchdog timer with internal oscillator, a internal calibrated oscillator, and three software selectable power saving modes. By executing powerful instructions in a single clock cycle, the device achieves throughputs approaching 1 MIPS per MHz, balancing power consumption and processing speed 3.


Pin configurations

Programming the hello button + LED board

Below is a repeat of programming the board that I did in Electronics Design week.

As everything needed to program the board was installed in Week 5, following these instructions, I downloaded the below files and saved them in the folder with the programmer files.

First, I ran the command to compile the code:

make -f hello.ftdi.44.echo.c.make

Then, I connected the programmer from Week 5 to the new board designed this week. Before connecting the two boards, I checked that the programmer was recognised by the computer when plugged into the USB port by running the command ls /dev/tty* in Mac. This can also be checked by the command python -m serial.tools.list_ports and in Mac through Utilities> System information> Hardware> USB. When connecting the two boards, it is important to check the orientation of the pins and that the GND of one pin is aligned to the GND of the other. I used an ISP cable to connect the two boards and a USB/FTDI cable to connect the new board to the computer’s USB port.

After this, I prepared the new board for programming by setting the fuses:

make -f hello.ftdi.44.echo.c.make program-usbtiny-fuses

Finally, I flashed the program to the microcontroller’s memory.

make -f hello.ftdi.44.echo.c.make program-usbtiny

Testing the board

After programming the new board, I tested it to check that it was working.

I installed pyserial, a Python Serial Port Extension for different platforms and distributions, in the mkdocs virtual environment on my computer.

sudo python -m pip install pyserial

Then, I ran the python application connected to the serial port of the board with the following command:

python term.py /dev/tty.usbserial-FT9P2WH8 115200

To know the serial port number to reference, use the tab key after tty in the command above or before entering it, type ls /dev/tty* on the command line and connections to the USB port will be displayed.

After running the application, the python application window opened but it was mostly unresponsive. Pressing Ctrl + C on the keyboard resulted to the following error in the terminal and the python window displayed the serial port number and speed.

Also testing the board with Arduino IDE’s serial monitor did not work. There was no feedback after typing into the text entry area and pressing send/enter.

The test works on a PC, but for some reason cannot yet get it to work on MAC.

Update: I later tried the test on the board I made for the Networking and Communications assignment. There was a response when something was typed in the text entry area of the Arduino IDE’s serial monitor.

With python, it was still unresponsive without displaying any errors in the terminal.

Programming with Arduino

Before ATtiny can work in Arduino, the ATtinyCore should be installed using the Arduino IDE boards manager. File -> Preferences, enter the URL in “Additional Boards Manager URLs” shown in the image below.

After the URL is added, the ATtinyCore can be installed in the boards manager Tools -> Board -> Boards Manager.

Then in Tools -> Boards, you select ATtiny24/44/84, Chip -> ATtiny44, set the Clock to 20MHz and select USBtinyISP as the Programmer. Then I clicked on Burn Bootloader which configures the fuse bits of the microcontroller.

To program the board, the board should be plugged into the computer via the FTDI cable and the FabISP connected to the 6-pin header on the Hello Button + LED board.

I opened the Button sketch from examples menu. File > Examples > Digital > Button. Then I changed the pin numbers for Button and LED to correspond to the pins on my Hello board using the below diagram from ATtinyCore.

Below is the code I modified.

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 8 on the ATTiny, when pressing a pushbutton attached to pin 7.

  The circuit:
  - LED attached from pin 8 to ground
  - pushbutton attached to pin 7 from +5V
  - 10K resistor attached to pin 7 from ground

    created 2005
  by DojoDave <http://www.0j0.org>
  modified 14 March 2019
  by Michael O

  This example code is in the public domain.

   Modified button and LED pins for the ATTiny based on 
  http://www.arduino.cc/en/Tutorial/Button
*/

//I changed these numbers from 2 (button) and (13) ledPin to correspond to those on the ATtiny44
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 7;     // the number of the pushbutton pin
const int ledPin =  8;      // the number of the LED pin

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

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 == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

The default mode when the sketch is uploaded to the board is that the LED glows automatically and when the button is pressed it turns off. I switched the buttonState so that the LED only glows when the button is pressed.

I also tried other Sketches in Arduino and Megumi’s (originally by Iván) LED blinks three times by button press and they worked as expected.

Reflection

Reviewing the datasheet was difficult as I am not familiar with the topic or embedded programming in general and only got an obligatory introduction to C about 10 years ago. This was the last time I learnt or used the language and it was not the easiest programming language to learn for someone coming from another background.

The data sheet had a lot of information from the architectural overview, to memory, clock system, I/O pins and their operations and many more. Reviewing these just made me think about all the things (from serial communication, clock cycles, pin configurations and the functions of different pins, Bitwise operations in C, etc.) I need to or should learn when working with different microcontrollers. Plus the different settings required when working with various components.

Initially when I went through the data sheet, I didn’t understand much, but while working on other assignments it started to register. I am slowly learning more about memory (program and data), clock sources for different microcontrollers, the function of different pins and which are the pulse width modulation (PWM) pins etc and their functions.

Even though I am not very familiar with embedded programming, the task for the week was relatively easy as we worked with Arduino. In Arduino, there are good examples that are easy to follow. As long as you know the pin mapping from Arduino to ATtiny or any other microcontroller, with the same working code it should be easy to operate different microcontrollers.

Files

Arduino