/*
{{copyright}} 
*/

/*
{{version}} 
*/

/*
{{license}} 
*/

#pragma once

#include <array>
#include <cstdint>
#include <optional>

namespace sdi_uToolBox::stm32
{
//--------------------------------------------------------------
class CriticalSection final
{
  public:
  CriticalSection();                                                         // Constructor
  ~CriticalSection();                                                        // Destructor
  CriticalSection(const CriticalSection &other)                = delete;     // Copy constructor
  CriticalSection(CriticalSection &&other) noexcept            = delete;     // Move constructor
  CriticalSection &operator=(const CriticalSection &other)     = delete;     // Copy assignment
  CriticalSection &operator=(CriticalSection &&other) noexcept = delete;     // Move assignment

  protected:
  uint32_t m_primask;
};
//--------------------------------------------------------------

//--------------------------------------------------------------
/* Constructor */
inline CriticalSection::CriticalSection()
{
  m_primask = __get_PRIMASK();
  __disable_irq();
}
//--------------------------------------------------------------
/* Destructor */
inline CriticalSection::~CriticalSection()
{
  if ((m_primask & 0x01) == 0)     // Only if interrupts were enabled
    __enable_irq();
}
//--------------------------------------------------------------
}     // namespace sdi_uToolBox::stm32
