OpenMPTL - ARM Cortex (common)
C++ Microprocessor Template Library
nvic.hpp
Go to the documentation of this file.
1 /*
2  * OpenMPTL - C++ Microprocessor Template Library
3  *
4  * Copyright (C) 2012-2017 Axel Burri <axel@tty0.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef ARM_CORTEX_COMMON_NVIC_HPP_INCLUDED
22 #define ARM_CORTEX_COMMON_NVIC_HPP_INCLUDED
23 
24 #include <arch/scb.hpp>
25 #include "reg/nvic.hpp"
26 #include <isr.hpp>
27 #include <type_traits>
28 
29 namespace mptl {
30 
31 //////////////////// irq_base ////////////////////
32 
33 
34 template<int _irqn>
35 struct irq_base {
36  static constexpr int irqn = _irqn;
37 };
38 
39 
40 //////////////////// core_exception ////////////////////
41 
42 
43 template<int irqn>
44 class core_exception : public irq_base<irqn> {
45  static_assert(irqn < 0 && irqn > -16, "illegal core exception interrupt number");
46 
47 #if 0
48 public:
49 
50  static constexpr bool priority_available = irqn > -13;
51 
52  static typename std::enable_if<priority_available>::type set_priority(uint32_t priority) {
53  scb::set_priority<irqn>(priority);
54  }
55 
56  static typename std::enable_if<priority_available, uint32_t>::type get_priority(void) {
57  return scb::set_priority<irqn>();
58  }
59 #endif
60 };
61 
62 
63 //////////////////// irq_channel ////////////////////
64 
65 
66 template<int irqn>
67 class irq_channel : public irq_base<irqn> {
68  static_assert(irqn >= 0, "illegal irq channel interrupt number");
69 
70  static constexpr unsigned reg_index = (uint32_t)irqn >> 5;
71  static constexpr unsigned irq_bit = 1 << ((uint32_t)irqn & 0x1F);
72 
78  using IPRx = NVIC::IPR<irqn>;
79 
80 public:
81 
82  static void enable(void) {
83  ISERx::store(irq_bit);
84  }
85  static void disable(void) {
86  ICERx::store(irq_bit);
87  }
88 
89  static bool is_pending(void) {
90  return ISPRx::load() & irq_bit;
91  }
92  static void set_pending(void) {
93  ISPRx::store(irq_bit);
94  }
95 
96  static void clear_pending(void) {
97  ICPRx::store(irq_bit);
98  }
99  static bool is_active(void) {
100  return IABRx::load() & (irq_bit);
101  }
102 
103 #if 0
104  static void set_priority(uint32_t priority) {
105  IPRx::store((priority << (8 - priority_bits)) & 0xff);
106  }
107 
108  static uint32_t get_priority(void) {
109  return((uint32_t)(IPRx::load() >> (8 - priority_bits)));
110  }
111 #endif
112 };
113 
114 
115 //////////////////// irq typedefs ////////////////////
116 
117 
118 namespace irq {
119 
120 /* Fixed core exceptions */
121 using reset = core_exception<-15>; // NOTE: priority is fixed to -3
122 using nmi = core_exception<-14>; // NOTE: priority is fixed to -2
123 using hard_fault = core_exception<-13>; // NOTE: priority is fixed to -1
124 
125 /* Settable core exceptions */
133 
134 static inline constexpr bool reserved_irqn(int irqn) {
135  return ((irqn == -3) ||
136  (irqn == -6) ||
137  (irqn == -7) ||
138  (irqn == -8) ||
139  (irqn == -9));
140 }
141 
142 } // namespace irq
143 
144 } // namespace mptl
145 
146 #endif // ARM_CORTEX_COMMON_NVIC_HPP_INCLUDED
Interrupt Set-Pending Registers.
Definition: nvic.hpp:54
static void clear_pending(void)
Definition: nvic.hpp:96
static constexpr bool reserved_irqn(int irqn)
Definition: nvic.hpp:134
Definition: nvic.hpp:44
static void enable(void)
Definition: nvic.hpp:82
Interrupt Set-Enable Registers.
Definition: nvic.hpp:49
static void disable(void)
Definition: nvic.hpp:85
Interrupt Clear-Pending Registers.
Definition: nvic.hpp:59
static void set_pending(void)
Definition: nvic.hpp:92
static constexpr int irqn
Definition: nvic.hpp:36
Definition: nvic.hpp:35
static bool is_active(void)
Definition: nvic.hpp:99
Definition: nvic.hpp:67
Interrupt Active Bit Register.
Definition: nvic.hpp:64
static bool is_pending(void)
Definition: nvic.hpp:89
Interrupt Priority Register.
Definition: nvic.hpp:69
Interrupt Set-Enable Registers.
Definition: nvic.hpp:44