| Both sides previous revisionPrevious revisionNext revision | Previous revision |
| en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:hardwarespecific [2023/11/21 22:28] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:hardwarespecific [2025/10/06 17:00] (current) – [Interrupts] pczekalski |
|---|
| ====== Hardware-specific extensions in programming ====== | ====== Hardware-specific extensions in programming ====== |
| | {{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_m.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ |
| 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), multicore management, power saving extensions and most of all, integrated communication capabilities (if any). It can be different for almost every single MCU or MCU family.\\ | 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), multicore management, power saving extensions and most of all, integrated communication capabilities (if any). It can be different for almost every single MCU or MCU family.\\ |
| 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), presenting specific C/C++ code for microcontrollers. | 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), presenting specific C/C++ code for microcontrollers. |
| |
| ** 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 ''IRAM_ATTR'' is necessary (part of the code present in [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts|]]: | Suppose the interrupt routine (function handler) uses any variables or accesses flash memory. In that case, it is necessary to use some form of tagging for the ISR function due to the specific, low-level memory management. A use of ''IRAM_ATTR'' is necessary (part of the code present in [[en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:interrupts|]]): |
| <code c> | <code c> |
| void IRAM_ATTR ButtonIRS() { //IRS function | void IRAM_ATTR ButtonIRS() { //IRS function |
| </code> | </code> |
| |
| <note important>If the ISR and some other process write to the memory (variable), providing exclusive access to the variable is important. This may be achieved with so-called Muxes, Semaphores and critical sections to ensure no deadlock will occur. However, it is unnecessary if ISR writes to the variable and some other process is reading it. The use of ''volatile'' for the variable should be enough.</note> | <note important>If the ISR and some other process write to the memory (variable), providing exclusive access to the variable is important. This can be achieved using so-called Mutexes, Semaphores, and critical sections to ensure that no deadlock will occur. However, it is unnecessary if ISR writes to the variable and some other process is reading it. The use of ''volatile'' for the variable should be sufficient.</note> |
| |
| <note warning>Without an advanced configuration, using the ''float'' type (hardware accelerated floating point) will cause the application to hang, throwing a panic error and immediate restart of the MCU. It is due to the specific construction of the MCU and FPU. Do not use the ''float'' type in interrupt handling. If floating point operations are needed, use ''double'' as this one is calculated the software way.</note> | <note warning>Without an advanced configuration, using the ''float'' type (hardware accelerated floating point) will cause the application to hang, throwing a panic error and immediate restart of the MCU. It is due to the specific construction of the MCU and FPU. Do not use the ''float'' type in interrupt handling. If floating point operations are needed, use ''double'' as this one is calculated the software way.</note> |