This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:hardwarespecific [2023/11/13 18:32] – ekontoturbo | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:hardwarespecific [2023/11/23 10:24] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Hardware-specific extensions in programming ==== | + | ====== Hardware-specific extensions in programming ====== |
+ | {{: | ||
Some generic programming techniques and patterns mentioned above require adaptation for different hardware platforms. It may occur whenever hardware-related aspects are in charge, e.g., accessing GPIOs, ADC conversion, timers, interrupts, multitasking (task scheduling and management), | Some generic programming techniques and patterns mentioned above require adaptation for different hardware platforms. It may occur whenever hardware-related aspects are in charge, e.g., accessing GPIOs, ADC conversion, timers, interrupts, multitasking (task scheduling and management), | ||
It is common for hardware vendors to provide rich examples, either in the form of documentation and downloadable samples (e.g. STM) or via Github (Espressif), | It is common for hardware vendors to provide rich examples, either in the form of documentation and downloadable samples (e.g. STM) or via Github (Espressif), | ||
- | === Analog input === | + | ==== Analog input ==== |
Some MCUs use specific setups. Analogue input may work out of the box. Still, low-level control usually brings better results and higher flexibility (e.g. instead of changing the input voltage to reflect the whole measurement range, you can regulate internal amplification and sensitivity. | Some MCUs use specific setups. Analogue input may work out of the box. Still, low-level control usually brings better results and higher flexibility (e.g. instead of changing the input voltage to reflect the whole measurement range, you can regulate internal amplification and sensitivity. | ||
- | == A special note on analogue inputs in ESP32 == | + | ** A special note on analogue inputs in ESP32 **\\ |
Please note implementation varies even between the ESP32 chips family, and not all chips provide all of the functions, so it is essential to refer to the technical documentation ((https:// | Please note implementation varies even between the ESP32 chips family, and not all chips provide all of the functions, so it is essential to refer to the technical documentation ((https:// | ||
Line 24: | Line 25: | ||
<note important> | <note important> | ||
- | === Analog output === | + | ==== Analog output |
PWM frequently controls analogue-style, | PWM frequently controls analogue-style, | ||
PWM uses pulses to change the adequate power delivered to the actuator. | PWM uses pulses to change the adequate power delivered to the actuator. | ||
It applies to motors, LEDs, bulbs, heaters and indirectly to the servos (but that works another way). | It applies to motors, LEDs, bulbs, heaters and indirectly to the servos (but that works another way). | ||
- | == A special note on ESP32 MCUs == | + | ** A special note on ESP32 MCUs **\\ |
The classical '' | The classical '' | ||
ESP32 has up to sixteen (0 to 15) PWM channels (controllers) that can be freely bound to any of the regular GPIOs.\\ The exact number of PWM channels depends on the family member of the ESP chips, e.g. ESP32-S2 and S3 series have only 8 independent PWM channels while ESP32-C3 has only 6. In the Arduino software framework for ESP32, it is referred to as '' | ESP32 has up to sixteen (0 to 15) PWM channels (controllers) that can be freely bound to any of the regular GPIOs.\\ The exact number of PWM channels depends on the family member of the ESP chips, e.g. ESP32-S2 and S3 series have only 8 independent PWM channels while ESP32-C3 has only 6. In the Arduino software framework for ESP32, it is referred to as '' | ||
Line 54: | Line 55: | ||
... | ... | ||
- | ledcSetup(PWM1_Ch, | + | ledcSetup(PWM1_Ch, |
- | ledcAttachPin(RGBLED_R, | + | |
- | ledcWrite(PWM1_Ch, | + | ledcAttachPin(RGBLED_R, |
+ | | ||
+ | ledcWrite(PWM1_Ch, | ||
+ | | ||
... | ... | ||
</ | </ | ||
Line 64: | Line 68: | ||
This technique can be easily adapted to control, e.g. standard and digital servos. PWM signal specification to control servos is presented in the chapter [[en: | This technique can be easily adapted to control, e.g. standard and digital servos. PWM signal specification to control servos is presented in the chapter [[en: | ||
- | === Interrupts === | + | ==== Interrupts |
Arduino boards used to have a limited set of GPIOs to trigger interrupts. In other MCUs, it is a rule of thumb that almost all GPIOs (but those used, e.g. for external SPI flash) can trigger an interrupt; thus, there is much higher flexibility in, e.g., the use of user interface devices such as buttons. | Arduino boards used to have a limited set of GPIOs to trigger interrupts. In other MCUs, it is a rule of thumb that almost all GPIOs (but those used, e.g. for external SPI flash) can trigger an interrupt; thus, there is much higher flexibility in, e.g., the use of user interface devices such as buttons. | ||
- | == A special note on ESP8266 and ESP32 == | + | ** A special note on ESP8266 and ESP32 **\\ |
Suppose the interrupt routine (function handler) uses any variables or access flash memory. In that case, it is necessary to use some tagging of the ISR function because of the specific, low-level memory management. A use of '' | Suppose the interrupt routine (function handler) uses any variables or access flash memory. In that case, it is necessary to use some tagging of the ISR function because of the specific, low-level memory management. A use of '' | ||
<code c> | <code c> | ||
Line 79: | Line 83: | ||
<note warning> | <note warning> | ||
- | === Timers === | + | ==== Timers |
The number of hardware timers, their features, and specific configuration is per MCU. Even single MCU families have different numbers of timers, e.g., in the case of the STM32 chips, the ESP32, and many others. Those differences, | The number of hardware timers, their features, and specific configuration is per MCU. Even single MCU families have different numbers of timers, e.g., in the case of the STM32 chips, the ESP32, and many others. Those differences, | ||
- | == A special note on ESP32 MCUs == | + | ** A special note on ESP32 MCUs **\\ |
The number of hardware timers varies between family members. Most ESP32s have 4, but ESP32-C3 has only two ((https:// | The number of hardware timers varies between family members. Most ESP32s have 4, but ESP32-C3 has only two ((https:// | ||
Special techniques using the critical section, muxes and semaphores are needed when more than one routine writes to the shared variable between processes (usually main code and an interrupt handler). However, It is unnecessary in the scenario where the interrupt handler writes to the variable and some other code (e.g. in the '' | Special techniques using the critical section, muxes and semaphores are needed when more than one routine writes to the shared variable between processes (usually main code and an interrupt handler). However, It is unnecessary in the scenario where the interrupt handler writes to the variable and some other code (e.g. in the '' | ||
Line 90: | Line 94: | ||
#include " | #include " | ||
- | #define LED_GPIO 0 //RED LED on GPIO 0 - vendor-specific | + | #define LED_GPIO 0 |
- | #define PRESCALLER 80 | + | #define PRESCALLER 80 // |
- | #define COUNTER 2000000 //2 million us = 2s | + | #define COUNTER 2000000 |
volatile bool LEDOn = false; | volatile bool LEDOn = false; | ||
Line 98: | Line 102: | ||
void IRAM_ATTR onHBT(){ | void IRAM_ATTR onHBT(){ | ||
- | LEDOn = !LEDOn; | + | LEDOn = !LEDOn; |
+ | //every call | ||
} | } | ||
Line 105: | Line 110: | ||
pinMode(LED_GPIO, | pinMode(LED_GPIO, | ||
- | tHBT = timerBegin(0, | + | tHBT = timerBegin(0, |
- | // Most ESP32s (but ESP32-C3) have 4 timers (0-3), and ESP32-C3 has only two (0-1). | + | |
+ | //Most ESP32s (but ESP32-C3) have 4 timers (0-3), | ||
+ | //and ESP32-C3 has only two (0-1). | ||
if (tHBT==NULL) //Check timer is created OK, NULL otherwise | if (tHBT==NULL) //Check timer is created OK, NULL otherwise | ||
{ | { | ||
Line 113: | Line 120: | ||
ESP.restart(); | ESP.restart(); | ||
} | } | ||
- | timerAttachInterrupt(tHBT, | + | timerAttachInterrupt(tHBT, |
- | timerAlarmWrite(tHBT, | + | |
+ | timerAlarmWrite(tHBT, | ||
+ | | ||
timerAlarmEnable(tHBT); | timerAlarmEnable(tHBT); | ||
| |