irq_arch.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Freie Universität Berlin, Hinnerk van Bruinehsen
3  * 2018 RWTH Aachen, Josua Arndt <jarndt@ias.rwth-aachen.de>
4  * 2020 Otto-von-Guericke-Universität Magdeburg
5  *
6  * This file is subject to the terms and conditions of the GNU Lesser
7  * General Public License v2.1. See the file LICENSE in the top level
8  * directory for more details.
9  */
10 
25 #ifndef IRQ_ARCH_H
26 #define IRQ_ARCH_H
27 
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include "irq.h"
32 #include "cpu.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
41 __attribute__((always_inline)) static inline unsigned int irq_disable(void)
42 {
43  uint8_t mask;
44  __asm__ volatile(
45  "in %[dest], __SREG__" "\n\t"
46  "cli" "\n\t"
47  : [dest] "=r"(mask)
48  : /* no inputs */
49  : "memory"
50  );
51  return mask;
52 }
53 
57 __attribute__((always_inline)) static inline unsigned int irq_enable(void)
58 {
59  uint8_t mask;
60  __asm__ volatile(
61  "in %[dest], __SREG__" "\n\t"
62  "sei" "\n\t"
63  : [dest] "=r"(mask)
64  : /* no inputs */
65  : "memory"
66  );
67  return mask;
68 }
69 
73 __attribute__((always_inline)) static inline void irq_restore(unsigned int _state)
74 {
75  uint8_t state = (uint8_t)_state;
76  /*
77  * Implementation in pseudo-code:
78  *
79  * disable_irqs();
80  * if (state & BIT7) {
81  * enable_irqs();
82  * }
83  *
84  * This takes 3 CPU Cycles if BIT7 is set (IRQs are enabled), otherwise 2.
85  */
86  __asm__ volatile(
87  "cli" "\n\t"
88  "sbrc %[state], 7" "\n\t"
89  "sei" "\n\t"
90  : /* no outputs */
91  : [state] "r"(state)
92  : "memory"
93  );
94 }
95 
99 __attribute__((always_inline)) static inline int irq_is_in(void)
100 {
101  uint8_t state = atmega_get_state();
102  return (state & ATMEGA_STATE_FLAG_ISR);
103 }
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
110 #endif /* IRQ_ARCH_H */
irq_disable
static unsigned int irq_disable(void)
Disable all maskable interrupts.
Definition: irq_arch.h:41
irq_restore
static void irq_restore(unsigned int _state)
Restore the state of the IRQ flags.
Definition: irq_arch.h:73
ATMEGA_STATE_FLAG_ISR
#define ATMEGA_STATE_FLAG_ISR
In ISR.
Definition: cpu.h:60
atmega_get_state
static uint8_t atmega_get_state(void)
Atomically read the state (atmega_state)
Definition: cpu.h:103
irq_is_in
static int irq_is_in(void)
See if the current context is inside an ISR.
Definition: irq_arch.h:99
irq.h
IRQ driver interface.
irq_enable
static unsigned int irq_enable(void)
Enable all maskable interrupts.
Definition: irq_arch.h:57