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>
|
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_atmega_common
|
|
|
|
* @{
|
|
|
|
*
|
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>
|
2014-07-15 12:08:52 +02:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2015-09-14 00:27:27 +02:00
|
|
|
#include <stdio.h>
|
2017-10-20 17:26:10 +02:00
|
|
|
#include "irq.h"
|
2014-07-15 12:08:52 +02:00
|
|
|
#include "cpu.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Macro returns state of the global interrupt register
|
|
|
|
*/
|
|
|
|
static uint8_t __get_interrupt_state(void);
|
|
|
|
static void __set_interrupt_state(uint8_t state);
|
|
|
|
|
2015-09-14 00:27:27 +02:00
|
|
|
volatile uint8_t __in_isr = 0;
|
|
|
|
|
2014-07-15 12:08:52 +02:00
|
|
|
__attribute__((always_inline)) static inline uint8_t __get_interrupt_state(void)
|
|
|
|
{
|
|
|
|
uint8_t sreg;
|
2018-03-20 23:32:08 +01:00
|
|
|
__asm__ volatile( "in __tmp_reg__, __SREG__ \n\t"
|
|
|
|
"mov %0, __tmp_reg__ \n\t"
|
|
|
|
: "=g"(sreg) );
|
2014-07-15 12:08:52 +02:00
|
|
|
return sreg & (1 << 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((always_inline)) inline void __set_interrupt_state(uint8_t state)
|
|
|
|
{
|
2018-03-20 23:32:08 +01:00
|
|
|
__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)
|
2019-04-25 14:07:47 +02:00
|
|
|
: "r15", "r16", "memory");
|
2014-07-15 12:08:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Disable all maskable interrupts
|
|
|
|
*/
|
2017-10-20 17:26:10 +02:00
|
|
|
unsigned int irq_disable(void)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
|
|
|
uint8_t mask = __get_interrupt_state();
|
2019-04-25 14:07:47 +02:00
|
|
|
cli(); /* <-- acts as memory barrier, see doc of avr-libc */
|
2014-07-15 12:08:52 +02:00
|
|
|
return (unsigned int) mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enable all maskable interrupts
|
|
|
|
*/
|
2017-10-20 17:26:10 +02:00
|
|
|
unsigned int irq_enable(void)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
2019-04-25 12:14:18 +02:00
|
|
|
uint8_t mask = __get_interrupt_state();
|
2019-04-25 14:07:47 +02:00
|
|
|
sei(); /* <-- acts as memory barrier, see doc of avr-libc */
|
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
|
|
|
|
*/
|
2017-10-20 17:26:10 +02:00
|
|
|
void irq_restore(unsigned int state)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
|
|
|
__set_interrupt_state(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief See if the current context is inside an ISR
|
|
|
|
*/
|
2017-10-20 17:26:10 +02:00
|
|
|
int irq_is_in(void)
|
2014-07-15 12:08:52 +02:00
|
|
|
{
|
2019-04-25 14:07:47 +02:00
|
|
|
int result = __in_isr;
|
|
|
|
__asm__ volatile("" ::: "memory");
|
|
|
|
return result;
|
2014-07-15 12:08:52 +02:00
|
|
|
}
|