1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/cpu/avr8_common/include/irq_arch.h

112 lines
2.6 KiB
C
Raw Normal View History

2014-07-15 12:08:52 +02:00
/*
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
2018-03-20 23:32:08 +01:00
* 2018 RWTH Aachen, Josua Arndt <jarndt@ias.rwth-aachen.de>
* 2020 Otto-von-Guericke-Universität Magdeburg
* 2021 Gerson Fernando Budke
2014-07-15 12:08:52 +02:00
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_avr8_common
2014-07-15 12:08:52 +02:00
* @{
*
* @file
2014-07-15 12:08:52 +02:00
* @brief Implementation of the kernels irq interface
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Hinnerk van Bruinehsen <h.v.bruinehsen@fu-berlin.de>
2018-03-20 23:32:08 +01:00
* @author Josua Arndt <jarndt@ias.rwth-aachen.de>
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
* @author Gerson Fernando Budke <nandojve@gmail.com>
2014-07-15 12:08:52 +02:00
*
*/
#ifndef IRQ_ARCH_H
#define IRQ_ARCH_H
2014-07-15 12:08:52 +02:00
#include <stdint.h>
2015-09-14 00:27:27 +02:00
#include <stdio.h>
#include <stdint.h>
2014-07-15 12:08:52 +02:00
#include "cpu.h"
#ifdef __cplusplus
extern "C" {
#endif
2014-07-15 12:08:52 +02:00
/**
* @brief Disable all maskable interrupts
*/
__attribute__((always_inline)) static inline unsigned int irq_disable(void)
2014-07-15 12:08:52 +02:00
{
uint8_t mask;
__asm__ volatile(
"in %[dest], __SREG__" "\n\t"
"cli" "\n\t"
: [dest] "=r"(mask)
: /* no inputs */
: "memory"
);
return mask;
2014-07-15 12:08:52 +02:00
}
/**
* @brief Enable all maskable interrupts
*/
__attribute__((always_inline)) static inline unsigned int irq_enable(void)
2014-07-15 12:08:52 +02:00
{
uint8_t mask;
__asm__ volatile(
"in %[dest], __SREG__" "\n\t"
"sei" "\n\t"
: [dest] "=r"(mask)
: /* no inputs */
: "memory"
);
return mask;
2014-07-15 12:08:52 +02:00
}
/**
* @brief Restore the state of the IRQ flags
*/
__attribute__((always_inline)) static inline void irq_restore(unsigned int _state)
2014-07-15 12:08:52 +02:00
{
uint8_t state = (uint8_t)_state;
/*
* Implementation in pseudo-code:
*
* disable_irqs();
* if (state & BIT7) {
* enable_irqs();
* }
*
* This takes 3 CPU Cycles if BIT7 is set (IRQs are enabled), otherwise 2.
*/
__asm__ volatile(
"cli" "\n\t"
"sbrc %[state], 7" "\n\t"
"sei" "\n\t"
: /* no outputs */
: [state] "r"(state)
: "memory"
);
2014-07-15 12:08:52 +02:00
}
/**
* @brief See if the current context is inside an ISR
*/
__attribute__((always_inline)) static inline int irq_is_in(void)
2014-07-15 12:08:52 +02:00
{
uint8_t state = avr8_get_state();
return (state & AVR8_STATE_FLAG_ISR);
2014-07-15 12:08:52 +02:00
}
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* IRQ_ARCH_H */