1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Glenn Ruben Bakke
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #ifndef NRFX_GLUE_H
28 #define NRFX_GLUE_H
29 
30 #include "py/mpconfig.h"
31 #include "py/misc.h"
32 
33 #include <soc/nrfx_irqs.h>
34 
35 #ifndef ARRAY_SIZE
36 #define ARRAY_SIZE MP_ARRAY_SIZE
37 #endif
38 
39 #define NRFX_STATIC_ASSERT(expression)
40 
41 #define NRFX_ASSERT(expression)  do { bool res = expression; (void)res; } while (0)
42 
43 void mp_hal_delay_us(mp_uint_t us);
44 #define NRFX_DELAY_US            mp_hal_delay_us
45 
46 #if BLUETOOTH_SD
47 
48 #if NRF51
49 #include "nrf_soc.h"
50 #else
51 #include "nrf_nvic.h"
52 #endif
53 
54 #include "ble_drv.h"
55 
56 #if (BLUETOOTH_SD == 110)
57 #define NRFX_IRQ_ENABLE(irq_number) \
58     do { \
59         if (ble_drv_stack_enabled() == 1) \
60         { \
61             sd_nvic_EnableIRQ(irq_number); \
62         } else { \
63             NVIC_EnableIRQ(irq_number); \
64         } \
65     } while (0)
66 #else
67 #define NRFX_IRQ_ENABLE(irq_number) sd_nvic_EnableIRQ(irq_number)
68 #endif
69 
70 #if (BLUETOOTH_SD == 110)
71 #define NRFX_IRQ_DISABLE(irq_number) \
72     do { \
73         if (ble_drv_stack_enabled() == 1) \
74         { \
75             sd_nvic_DisableIRQ(irq_number);  \
76         } else { \
77             NVIC_DisableIRQ(irq_number); \
78         } \
79     } while (0)
80 #else
81 #define NRFX_IRQ_DISABLE(irq_number) sd_nvic_DisableIRQ(irq_number)
82 #endif
83 
84 #if (BLUETOOTH_SD == 110)
85 #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) \
86     do { \
87         if (ble_drv_stack_enabled() == 1) \
88         { \
89             sd_nvic_SetPriority(irq_number, priority); \
90         } else { \
91             NVIC_SetPriority(irq_number, priority); \
92         } \
93     } while (0)
94 #else
95 #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) sd_nvic_SetPriority(irq_number, priority)
96 #endif
97 
98 #if (BLUETOOTH_SD == 110)
99 #define NRFX_IRQ_PENDING_SET(irq_number) \
100     do { \
101         if (ble_drv_stack_enabled() == 1) \
102         { \
103             sd_nvic_SetPendingIRQ(irq_number); \
104         } else { \
105             NVIC_SetPendingIRQ(irq_number); \
106         } \
107     } while (0)
108 #else
109 #define NRFX_IRQ_PENDING_SET(irq_number) sd_nvic_SetPendingIRQ(irq_number)
110 #endif
111 
112 #if (BLUETOOTH_SD == 110)
113 #define NRFX_IRQ_PENDING_CLEAR(irq_number) \
114     do { \
115         if (ble_drv_stack_enabled() == 1) \
116         { \
117             sd_nvic_ClearPendingIRQ(irq_number); \
118         } else { \
119             NVIC_ClearPendingIRQ(irq_number)(irq_number); \
120         } \
121     } while (0)
122 #else
123 #define NRFX_IRQ_PENDING_CLEAR(irq_number) sd_nvic_ClearPendingIRQ(irq_number)
124 #endif
125 
126 #define NRFX_CRITICAL_SECTION_ENTER() \
127     { \
128         uint8_t _is_nested_critical_region; \
129         sd_nvic_critical_region_enter(&_is_nested_critical_region);
130 
131 #define NRFX_CRITICAL_SECTION_EXIT() \
132     sd_nvic_critical_region_exit(_is_nested_critical_region); \
133 }
134 
135 #else // BLUETOOTH_SD
136 
137 #define NRFX_IRQ_ENABLE(irq_number) NVIC_EnableIRQ(irq_number)
138 #define NRFX_IRQ_DISABLE(irq_number) NVIC_DisableIRQ(irq_number)
139 #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) NVIC_SetPriority(irq_number, priority)
140 #define NRFX_IRQ_PENDING_SET(irq_number) NVIC_SetPendingIRQ(irq_number)
141 #define NRFX_IRQ_PENDING_CLEAR(irq_number) NVIC_ClearPendingIRQ(irq_number)
142 
143 // Source:
144 // https://devzone.nordicsemi.com/f/nordic-q-a/8572/disable-interrupts-and-enable-interrupts-if-they-where-enabled/31347#31347
145 #define NRFX_CRITICAL_SECTION_ENTER() { int _old_primask = __get_PRIMASK(); __disable_irq();
146 #define NRFX_CRITICAL_SECTION_EXIT() __set_PRIMASK(_old_primask); }
147 
148 #endif // !BLUETOOTH_SD
149 
150 #define NRFX_IRQ_IS_ENABLED(irq_number) (0 != (NVIC->ISER[irq_number / 32] & (1UL << (irq_number % 32))))
151 
152 #endif // NRFX_GLUE_H
153