1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-26 06:53:52 +01:00

Merge pull request #14085 from maribu/atmega_irq

cpu/atmega_common: Update to inlineable IRQ API
This commit is contained in:
Francisco 2020-05-26 16:33:07 +02:00 committed by GitHub
commit 3ef40f3321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 92 deletions

View File

@ -63,6 +63,11 @@ extern "C" {
*/
#define HAVE_HEAP_STATS
/**
* @brief This arch uses the inlined IRQ API.
*/
#define IRQ_API_INLINED (1)
#ifdef __cplusplus
}
#endif

View File

@ -0,0 +1,110 @@
/*
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
* 2018 RWTH Aachen, Josua Arndt <jarndt@ias.rwth-aachen.de>
* 2020 Otto-von-Guericke-Universität Magdeburg
*
* 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_atmega_common
* @{
*
* @file
* @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>
* @author Josua Arndt <jarndt@ias.rwth-aachen.de>
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
*/
#ifndef IRQ_ARCH_H
#define IRQ_ARCH_H
#include <stdint.h>
#include <stdio.h>
#include <stdint.h>
#include "irq.h"
#include "cpu.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Disable all maskable interrupts
*/
__attribute__((always_inline)) static inline unsigned int irq_disable(void)
{
uint8_t mask;
__asm__ volatile(
"in %[dest], __SREG__" "\n\t"
"cli" "\n\t"
: [dest] "=r"(mask)
: /* no inputs */
: "memory"
);
return mask;
}
/**
* @brief Enable all maskable interrupts
*/
__attribute__((always_inline)) static inline unsigned int irq_enable(void)
{
uint8_t mask;
__asm__ volatile(
"in %[dest], __SREG__" "\n\t"
"sei" "\n\t"
: [dest] "=r"(mask)
: /* no inputs */
: "memory"
);
return mask;
}
/**
* @brief Restore the state of the IRQ flags
*/
__attribute__((always_inline)) static inline void irq_restore(unsigned int _state)
{
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"
);
}
/**
* @brief See if the current context is inside an ISR
*/
__attribute__((always_inline)) static inline int irq_is_in(void)
{
uint8_t state = atmega_get_state();
return (state & ATMEGA_STATE_FLAG_ISR);
}
#ifdef __cplusplus
}
#endif
/** @} */
#endif /* IRQ_ARCH_H */

View File

@ -1,92 +0,0 @@
/*
* Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
* 2018 RWTH Aachen, Josua Arndt <jarndt@ias.rwth-aachen.de>
*
* 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_atmega_common
* @{
*
* @file
* @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>
* @author Josua Arndt <jarndt@ias.rwth-aachen.de>
*
* @}
*/
#include <stdint.h>
#include <stdio.h>
#include <stdint.h>
#include "irq.h"
#include "cpu.h"
/**
* @brief Macro returns state of the global interrupt register
*/
static uint8_t atmega_get_interrupt_state(void);
static void atmega_set_interrupt_state(uint8_t state);
__attribute__((always_inline)) static inline uint8_t atmega_get_interrupt_state(void)
{
uint8_t sreg;
__asm__ volatile( "in __tmp_reg__, __SREG__ \n\t"
"mov %0, __tmp_reg__ \n\t"
: "=g"(sreg) );
return sreg & (1 << 7);
}
__attribute__((always_inline)) inline void atmega_set_interrupt_state(uint8_t state)
{
__asm__ volatile( "mov r15,%0 \n\t"
"in r16, __SREG__ \n\t"
"cbr r16,7 \n\t"
"or r15,r16 \n\t"
"out __SREG__, r15 \n\t"
:
: "g"(state)
: "r15", "r16", "memory");
}
/**
* @brief Disable all maskable interrupts
*/
unsigned int irq_disable(void)
{
uint8_t mask = atmega_get_interrupt_state();
cli(); /* <-- acts as memory barrier, see doc of avr-libc */
return (unsigned int) mask;
}
/**
* @brief Enable all maskable interrupts
*/
unsigned int irq_enable(void)
{
uint8_t mask = atmega_get_interrupt_state();
sei(); /* <-- acts as memory barrier, see doc of avr-libc */
return mask;
}
/**
* @brief Restore the state of the IRQ flags
*/
void irq_restore(unsigned int state)
{
atmega_set_interrupt_state(state);
}
/**
* @brief See if the current context is inside an ISR
*/
int irq_is_in(void)
{
uint8_t state = atmega_get_state();
return (state & ATMEGA_STATE_FLAG_ISR);
}