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-05-15 09:25:08 +02:00
|
|
|
* 2020 Otto-von-Guericke-Universität Magdeburg
|
2023-06-27 22:41:18 +02:00
|
|
|
* 2021-2023 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-02 18:40:07 +01:00
|
|
|
* @ingroup cpu_avr8_common
|
2014-07-15 12:08:52 +02:00
|
|
|
* @{
|
|
|
|
*
|
2015-05-22 07:34:41 +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>
|
2020-05-15 09:25:08 +02:00
|
|
|
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
|
2021-01-02 18:40:07 +01:00
|
|
|
* @author Gerson Fernando Budke <nandojve@gmail.com>
|
2014-07-15 12:08:52 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2020-05-15 09:15:16 +02:00
|
|
|
#ifndef IRQ_ARCH_H
|
|
|
|
#define IRQ_ARCH_H
|
|
|
|
|
2021-12-08 15:53:15 +01:00
|
|
|
#include <stdbool.h>
|
2014-07-15 12:08:52 +02:00
|
|
|
#include <stdint.h>
|
2015-09-14 00:27:27 +02:00
|
|
|
#include <stdio.h>
|
2020-02-06 12:21:48 +01:00
|
|
|
#include <stdint.h>
|
2014-07-15 12:08:52 +02:00
|
|
|
#include "cpu.h"
|
|
|
|
|
2020-05-15 09:15:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-07-15 12:08:52 +02:00
|
|
|
/**
|
|
|
|
* @brief Disable all maskable interrupts
|
|
|
|
*/
|
2020-05-15 09:15:16 +02:00
|
|
|
__attribute__((always_inline)) static inline unsigned int irq_disable(void)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
2020-05-15 09:25:08 +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
|
|
|
|
*/
|
2020-05-15 09:15:16 +02:00
|
|
|
__attribute__((always_inline)) static inline unsigned int irq_enable(void)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
2020-05-15 09:25:08 +02:00
|
|
|
uint8_t mask;
|
|
|
|
__asm__ volatile(
|
|
|
|
"in %[dest], __SREG__" "\n\t"
|
|
|
|
"sei" "\n\t"
|
|
|
|
: [dest] "=r"(mask)
|
|
|
|
: /* no inputs */
|
|
|
|
: "memory"
|
|
|
|
);
|
2019-04-25 12:14:18 +02:00
|
|
|
return mask;
|
2014-07-15 12:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Restore the state of the IRQ flags
|
|
|
|
*/
|
2020-05-15 09:25:08 +02:00
|
|
|
__attribute__((always_inline)) static inline void irq_restore(unsigned int _state)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
2020-05-15 09:25:08 +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
|
|
|
|
*/
|
2021-12-08 15:53:15 +01:00
|
|
|
__attribute__((always_inline)) static inline bool irq_is_in(void)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
2023-06-27 22:41:18 +02:00
|
|
|
bool is_in = avr8_state_irq_count > 0;
|
|
|
|
|
|
|
|
__asm__ volatile ("" : : : "memory");
|
|
|
|
|
|
|
|
return is_in;
|
2014-07-15 12:08:52 +02:00
|
|
|
}
|
2020-05-15 09:15:16 +02:00
|
|
|
|
2019-03-06 18:03:09 +01:00
|
|
|
/**
|
|
|
|
* @brief Test if interrupts are currently enabled
|
|
|
|
*/
|
2021-12-08 15:53:15 +01:00
|
|
|
__attribute__((always_inline)) static inline bool irq_is_enabled(void)
|
2019-03-06 18:03:09 +01:00
|
|
|
{
|
|
|
|
uint8_t mask;
|
|
|
|
__asm__ volatile(
|
|
|
|
"in %[dest], __SREG__" "\n\t"
|
|
|
|
: [dest] "=r"(mask)
|
|
|
|
: /* no inputs */
|
|
|
|
: "memory"
|
|
|
|
);
|
|
|
|
return mask & (1 << 7);
|
|
|
|
}
|
|
|
|
|
2023-06-29 17:18:21 +02:00
|
|
|
/**
|
|
|
|
* @brief Define an AVR-8 ISR
|
|
|
|
*
|
|
|
|
* This macro hides all RIOT-OS port internal details from user implementation.
|
|
|
|
* The user should use this macro passing at least two parameters. The first is
|
|
|
|
* the interrupt vector and the second one is the function to be called. Zero or
|
|
|
|
* more optional parameters can be passed the function one by one using the
|
|
|
|
* variable length argument list.
|
|
|
|
*
|
|
|
|
* It is recommended that user define a `static inline void` function to
|
|
|
|
* the implement the interrupt. The function can accept argument list based on
|
|
|
|
* implementation details.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
*
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.c}
|
|
|
|
* static inline void _my_isr_handler(int index)
|
|
|
|
* {
|
|
|
|
* do_something(index);
|
|
|
|
* }
|
|
|
|
* AVR8_ISR(PERIPHERAL_INSTANCE_0_ISR, _my_isr_handler, 0);
|
|
|
|
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
*/
|
|
|
|
#define AVR8_ISR(vector, function, ...) \
|
|
|
|
ISR(vector, ISR_BLOCK) \
|
|
|
|
{ \
|
|
|
|
avr8_enter_isr(); \
|
|
|
|
function(__VA_ARGS__); \
|
|
|
|
avr8_exit_isr(); \
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:15:16 +02:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
#endif /* IRQ_ARCH_H */
|