This is an old revision of the document!


ADV1: Using timers to execute code asynchronously

It is advised to use timers that periodically execute a function to handle repeating tasks. Hardware timers work parallel to the CPU and consume few CPU resources. ESP32-S3 has 4 hardware timers, but each timer can execute multiple handlers. The idea of using the timer is to encapsulate a piece of compact code that can be run virtually asynchronously and executed is a precisely-defined time manner. In this scenario, we use a timer to update the LCD screen periodically. We choose a dummy example where Timer 1 is used to increment a value of the byte type, and Timer 2 reads this value and writes it on the LCD. Naturally, a collision may occur whenever two processes are to access a single memory block (variable). It is critical when both processes (tasks, asynchronous functions) are writing to it. However, in our example, the handler executed by Timer 1 only writes to the variable, while Timer 2 only reads from it. In this scenario, there is no need to use mutexes (semaphores). A scenario with detailed description of when to use mutexes is beyond the scope of this example.

Prerequisites

To implement this scenario, it is necessary to get familiar with at least one of the following scenarios first:

A standard LCD handling library is attached to the platformio.ini, and it is the only library needed. Timers are integrated into the Arduino framework for ESP32.

lib_deps = adafruit/Adafruit LiquidCrystal@^2.0.2 

Suggested Readings and Knowledge Resources

Hands-on Lab Scenario

Task to be implemented

Present on the LCD current value of the byte variable; update the LCD every 3 seconds using one timer. Use another timer to increase this variable every second.

Start

Check LCD visibility in the camera FOV. You may also disable LED if it interferes with the camera video stream, as described in the scenario EMB9A: Use of RGB LEDs.

Steps

We used to separate tasks, but for this case, complete code is provided in chunks, including LCD handling. It presents relations on where particular parts of the code should be located when using timers and how timing relates between components.

It is important e.g. when LCD had to be set up and configured before asynchronous handlers execute writing to it, so set up order is meaningful!

Step 1

Include libraries:

#include <Arduino.h>
#include <Adafruit_LiquidCrystal.h>

#define LCD_RS 2 #define LCD_ENABLE 1 #define LCD_D4 39 #define LCD_D5 40 #define LCD_D6 41 #define LCD_D7 42

volatile byte i = 0; hw_timer_t *Timer1 = NULL; hw_timer_t *Timer2 = NULL; const int baseFrequency = 80; MHz const int interval1 = 1000000; Interval in microseconds = 1s const int interval2 = 3000000; Interval in microseconds = 3s static Adafruit_LiquidCrystal lcd(LCD_RS, LCD_ENABLE, LCD_D4, LCD_D5, LCD_D6, LCD_D7); void IRAM_ATTR onTimer1() { i++; } void IRAM_ATTR onTimer2() { lcd.clear(); lcd.setCursor(0,0); lcd.print(i); } void setup(){ Timer1 config

Timer1 = timerBegin(0, baseFrequency, true);
timerAttachInterrupt(Timer1, &onTimer1, true);
timerAlarmWrite(Timer1, interval1, true);
timerAlarmEnable(Timer1);

//Timer2 config
Timer2 = timerBegin(1, baseFrequency, true);
timerAttachInterrupt(Timer2, &onTimer2, true);
timerAlarmWrite(Timer2, interval2, true);
timerAlarmEnable(Timer2);

//start LCD
lcd.begin(16,2);
lcd.clear();
//start both timers
timerAlarmEnable(Timer1);
timerAlarmEnable(Timer2);

}

void loop() {

}

Step 2

Step n

Describe activities done in Step n.

Result validation

On the LCD screen, you should see values starting from number 3, then 6, 9, 12 and so on (=3 every 3 seconds). Note, as byte has a capacity of 256 (0…255), the sequence changes in the following increments once it overflows.

FAQ

How many timers can I use?: ESP32-S3 has 4 hardware timers. You may trick this limitation by using smart handlers that have, e.g., an internal counter and internally execute every N-th cycle. This helps to simulate more timers in a software way.

Project information


This Intellectual Output was implemented under the Erasmus+ KA2.
Project IOT-OPEN.EU Reloaded – Education-based strengthening of the European universities, companies and labour force in the global IoT market.
Project number: 2022-1-PL01-KA220-HED-000085090.

Erasmus+ Disclaimer
This project has been funded with support from the European Commission.
This publication reflects the views of only the author, and the Commission cannot be held responsible for any use that may be made of the information contained therein.

Copyright Notice
This content was created by the IOT-OPEN.EU Reloaded consortium, 2022,2024.
The content is Copyrighted and distributed under CC BY-NC Creative Commons Licence, free for Non-Commercial use.

en/iot-open/practical/hardware/sut/esp32/adv1_1.1711388344.txt.gz · Last modified: 2024/03/25 17:39 by pczekalski
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0