hydrotower-mainboard/src/common/common.h

57 lines
1.5 KiB
C

// Copyright (C) 2025 Thies Lennart Alff
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
// USA
#pragma once
#include <sam.h>
#include <stdbool.h>
#ifndef NULL
#define NULL ((void*)0)
#endif
#ifdef DEBUG
#include "SEGGER_RTT.h"
#define DEBUG_PRINT(...) SEGGER_RTT_printf(0, "DEBUG: " __VA_ARGS__)
#else
#define DEBUG_PRINT(...) do {} while(0)
#endif
static volatile uint32_t critical_section_counter = 0;
static volatile bool critical_section_previous_state = false;
static inline void interrupts_enable() {
__DMB();
__enable_irq();
}
static inline void interrupts_disable() {
__disable_irq();
__DMB();
}
static inline bool interrupts_are_enabled() {
return __get_PRIMASK() == 0;
}
static inline uint32_t interrupts_save() {
volatile uint32_t flags = interrupts_are_enabled();
interrupts_disable();
return flags;
}
void enter_critical();
void leave_ciritical();