mirror of
https://github.com/Show-maket/IR-protocol.git
synced 2026-09-21 20:39:35 +00:00
archive: freeze IR-protocol WIP before stepwise integration
This commit is contained in:
51
IrInterruptGuard.h
Normal file
51
IrInterruptGuard.h
Normal file
@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#if defined(__AVR__)
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/io.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Nest-safe interrupt guard for the short ISR/main shared-state sections used
|
||||
* by IR-protocol. Unlike a noInterrupts()/interrupts() pair it restores the
|
||||
* previous state and therefore never enables interrupts from inside an ISR.
|
||||
*/
|
||||
class IrInterruptGuard final
|
||||
{
|
||||
public:
|
||||
IrInterruptGuard()
|
||||
{
|
||||
#if defined(__arm__) || defined(__thumb__) || defined(ARDUINO_ARCH_STM32)
|
||||
state_ = __get_PRIMASK();
|
||||
__disable_irq();
|
||||
#elif defined(__AVR__)
|
||||
state_ = SREG;
|
||||
cli();
|
||||
#else
|
||||
noInterrupts();
|
||||
#endif
|
||||
}
|
||||
|
||||
~IrInterruptGuard()
|
||||
{
|
||||
#if defined(__arm__) || defined(__thumb__) || defined(ARDUINO_ARCH_STM32)
|
||||
if ((state_ & 1U) == 0U)
|
||||
__enable_irq();
|
||||
#elif defined(__AVR__)
|
||||
SREG = static_cast<uint8_t>(state_);
|
||||
#else
|
||||
interrupts();
|
||||
#endif
|
||||
}
|
||||
|
||||
IrInterruptGuard(const IrInterruptGuard&) = delete;
|
||||
IrInterruptGuard& operator=(const IrInterruptGuard&) = delete;
|
||||
|
||||
private:
|
||||
#if defined(__arm__) || defined(__thumb__) || defined(ARDUINO_ARCH_STM32) || \
|
||||
defined(__AVR__)
|
||||
uint32_t state_ = 0U;
|
||||
#endif
|
||||
};
|
||||
Reference in New Issue
Block a user