BSPACM  20150113
Board Support Package for ARM Cortex-M Microcontrollers
device.h
Go to the documentation of this file.
1 /* Copyright 2014, Peter A. Bigot
2  *
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * * Neither the name of the software nor the names of its contributors may be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
60 #ifndef BSPACM_DEVICE_NRF51_H
61 #define BSPACM_DEVICE_NRF51_H
62 
63 #if ! (BSPACM_DEVICE_SERIES_NRF51 - 0)
64 #error NRF51 device header in non-NRF51 device
65 #endif /* BSPACM_DEVICE_SERIES_NRF51 */
66 
67 /* We need the peripheral structures for inline use in headers, but
68  * try not to contaminate the visible identifiers with everything
69  * that's in nrf51_bitfields.h */
70 #include "nrf51.h"
71 
72 #ifndef BSPACM_NRF_USE_SD
73 
75 #define BSPACM_NRF_USE_SD 0
76 #endif /* BSPACM_NRF_USE_SD */
77 
78 #if (BSPACM_NRF_USE_SD - 0)
79 /* And of course this pulls in nrf51_bitfields.h, dammit. */
80 #include "nrf_soc.h"
81 #endif /* BSPACM_NRF_USE_SD */
82 
105 __STATIC_INLINE void
107  bool high)
108 {
109 #if (BSPACM_NRF_USE_SD - 0)
110  /* NB: Use non-sd interface for unrestricted peripherals */
111  NVIC_SetPriority(irqn, high ? 1 : 3);
112 #endif /* BSPACM_NRF_USE_SD */
113 }
114 
116 __STATIC_INLINE void
118 {
119 #define NOSD_BODY() do { \
120  NVIC_EnableIRQ(irqn); \
121  } while (0)
122 
123 #if (BSPACM_NRF_USE_SD - 0)
124  uint32_t ec = sd_nvic_EnableIRQ(irqn);
125  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
126  NOSD_BODY();
127  } else if (NRF_SUCCESS != ec) {
128  while (1) {
129  /* deadlock to indicate invalid use */
130  }
131  }
132 #else /* BSPACM_NRF_USE_SD */
133  NOSD_BODY();
134 #endif /* BSPACM_NRF_USE_SD */
135 
136 #undef NOSD_BODY
137 }
138 
140 __STATIC_INLINE void
142 {
143 #define NOSD_BODY() do { \
144  NVIC_DisableIRQ(irqn); \
145  } while (0)
146 
147 #if (BSPACM_NRF_USE_SD - 0)
148  uint32_t ec = sd_nvic_DisableIRQ(irqn);
149  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
150  NOSD_BODY();
151  } else if (NRF_SUCCESS != ec) {
152  while (1) {
153  /* deadlock to indicate invalid use */
154  }
155  }
156 #else /* BSPACM_NRF_USE_SD */
157  NOSD_BODY();
158 #endif /* BSPACM_NRF_USE_SD */
159 
160 #undef NOSD_BODY
161 }
162 
164 __STATIC_INLINE uint32_t
166 {
167  uint32_t pending_irq;
168 
169 #define NOSD_BODY() do { \
170  pending_irq = NVIC_GetPendingIRQ(irqn); \
171  } while (0)
172 
173 #if (BSPACM_NRF_USE_SD - 0)
174  uint32_t ec = sd_nvic_GetPendingIRQ(irqn, &pending_irq);
175  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
176  NOSD_BODY();
177  } else if (NRF_SUCCESS != ec) {
178  while (1) {
179  /* deadlock to indicate invalid use */
180  }
181  }
182 #else /* BSPACM_NRF_USE_SD */
183  NOSD_BODY();
184 #endif /* BSPACM_NRF_USE_SD */
185  return pending_irq;
186 
187 #undef NOSD_BODY
188 }
189 
191 __STATIC_INLINE void
193 {
194 #define NOSD_BODY() do { \
195  NVIC_SetPendingIRQ(irqn); \
196  } while (0)
197 
198 #if (BSPACM_NRF_USE_SD - 0)
199  uint32_t ec = sd_nvic_SetPendingIRQ(irqn);
200  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
201  NOSD_BODY();
202  } else if (NRF_SUCCESS != ec) {
203  while (1) {
204  /* deadlock to indicate invalid use */
205  }
206  }
207 #else /* BSPACM_NRF_USE_SD */
208  NOSD_BODY();
209 #endif /* BSPACM_NRF_USE_SD */
210 
211 #undef NOSD_BODY
212 }
213 
215 __STATIC_INLINE void
217 {
218 #define NOSD_BODY() do { \
219  NVIC_ClearPendingIRQ(irqn); \
220  } while (0)
221 
222 #if (BSPACM_NRF_USE_SD - 0)
223  uint32_t ec = sd_nvic_ClearPendingIRQ(irqn);
224  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
225  NOSD_BODY();
226  } else if (NRF_SUCCESS != ec) {
227  while (1) {
228  /* deadlock to indicate invalid use */
229  }
230  }
231 #else /* BSPACM_NRF_USE_SD */
232  NOSD_BODY();
233 #endif /* BSPACM_NRF_USE_SD */
234 
235 #undef NOSD_BODY
236 }
237 
239 __STATIC_INLINE void
241  uint32_t priority)
242 {
243 #define NOSD_BODY() do { \
244  NVIC_SetPriority(irqn, priority); \
245  } while (0)
246 
247 #if (BSPACM_NRF_USE_SD - 0)
248  uint32_t ec = sd_nvic_SetPriority(irqn, priority);
249  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
250  NOSD_BODY();
251  } else if (NRF_SUCCESS != ec) {
252  while (1) {
253  /* deadlock to indicate invalid use */
254  }
255  }
256 #else /* BSPACM_NRF_USE_SD */
257  NOSD_BODY();
258 #endif /* BSPACM_NRF_USE_SD */
259 
260 #undef NOSD_BODY
261 }
262 
264 __STATIC_INLINE uint32_t
266 {
267 #define NOSD_FN() NVIC_GetPriority(irqn)
268 
269 #if (BSPACM_NRF_USE_SD - 0)
270  nrf_app_irq_priority_t priority;
271  uint32_t ec = sd_nvic_GetPriority(irqn, &priority);
272  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
273  priority = NOSD_FN();
274  } else if (NRF_SUCCESS != ec) {
275  while (1) {
276  /* deadlock to indicate invalid use */
277  }
278  }
279  return priority;
280 #else /* BSPACM_NRF_USE_SD */
281  return NOSD_FN();
282 #endif /* BSPACM_NRF_USE_SD */
283 
284 #undef NOSD_FN
285 }
286 
288 __STATIC_INLINE void
290 {
291 #define NOSD_BODY() do { \
292  NVIC_SystemReset(); \
293  } while (0)
294 
295 #if (BSPACM_NRF_USE_SD - 0)
296  uint32_t ec = sd_nvic_SystemReset();
297  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
298  NOSD_BODY();
299  } else {
300  while (1) {
301  /* deadlock to indicate invalid use */
302  }
303  }
304 #else /* BSPACM_NRF_USE_SD */
305  NOSD_BODY();
306 #endif /* BSPACM_NRF_USE_SD */
307 
308 #undef NOSD_BODY
309 }
310 
312 __STATIC_INLINE void
313 vBSPACMnrf51_PPI_CH (uint8_t channel_num,
314  const volatile void * evt_endpoint,
315  const volatile void * task_endpoint)
316 {
317 #define NOSD_BODY() do { \
318  NRF_PPI->CH[channel_num].EEP = (uintptr_t)evt_endpoint; \
319  NRF_PPI->CH[channel_num].TEP = (uintptr_t)task_endpoint; \
320  } while (0)
321 
322 #if (BSPACM_NRF_USE_SD - 0)
323  uint32_t ec = sd_ppi_channel_assign(channel_num, evt_endpoint, task_endpoint);
324  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
325  NOSD_BODY();
326  } else if (NRF_SUCCESS != ec) {
327  while (1) {
328  /* deadlock to indicate invalid use */
329  }
330  }
331 #else /* BSPACM_NRF_USE_SD */
332  NOSD_BODY();
333 #endif /* BSPACM_NRF_USE_SD */
334 
335 #undef NOSD_BODY
336 }
337 
339 __STATIC_INLINE void
341 {
342 #define NOSD_BODY() do { \
343  NRF_PPI->CHENCLR = mask; \
344  } while (0)
345 
346 #if (BSPACM_NRF_USE_SD - 0)
347  uint32_t ec = sd_ppi_channel_enable_clr(mask);
348  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
349  NOSD_BODY();
350  } else if (NRF_SUCCESS != ec) {
351  while (1) {
352  /* deadlock to indicate invalid use */
353  }
354  }
355 #else /* BSPACM_NRF_USE_SD */
356  NOSD_BODY();
357 #endif /* BSPACM_NRF_USE_SD */
358 
359 #undef NOSD_BODY
360 }
361 
363 __STATIC_INLINE void
365 {
366 #define NOSD_BODY() do { \
367  NRF_PPI->CHENSET = mask; \
368  } while (0)
369 
370 #if (BSPACM_NRF_USE_SD - 0)
371  uint32_t ec = sd_ppi_channel_enable_set(mask);
372  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
373  NOSD_BODY();
374  } else if (NRF_SUCCESS != ec) {
375  while (1) {
376  /* deadlock to indicate invalid use */
377  }
378  }
379 #else /* BSPACM_NRF_USE_SD */
380  NOSD_BODY();
381 #endif /* BSPACM_NRF_USE_SD */
382 
383 #undef NOSD_BODY
384 }
385 
387 __STATIC_INLINE uint32_t
389 {
390 #define NOSD_FN() NRF_PPI->CHEN
391 
392 #if (BSPACM_NRF_USE_SD - 0)
393  uint32_t chen;
394  uint32_t ec = sd_ppi_channel_enable_get(&chen);
395  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
396  chen = NOSD_FN();
397  } else if (NRF_SUCCESS != ec) {
398  while (1) {
399  /* deadlock to indicate invalid use */
400  }
401  }
402  return chen;
403 #else /* BSPACM_NRF_USE_SD */
404  return NOSD_FN();
405 #endif /* BSPACM_NRF_USE_SD */
406 
407 #undef NOSD_FN
408 }
409 
411 __STATIC_INLINE void
413 {
414 #define NOSD_BODY() do { \
415  NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; \
416  NRF_CLOCK->TASKS_HFCLKSTART = 1; \
417  while (! NRF_CLOCK->EVENTS_HFCLKSTARTED) { \
418  } \
419  } while (0)
420 
421 #if (BSPACM_NRF_USE_SD - 0)
422  uint32_t ec = sd_clock_hfclk_request();
423 
424  if (NRF_ERROR_SOFTDEVICE_NOT_ENABLED == ec) {
425  NOSD_BODY();
426  } else if (NRF_SUCCESS == ec) {
427  uint32_t is_running;
428  do {
429  if (NRF_SUCCESS != sd_clock_hfclk_is_running(&is_running))
430  while (1) {
431  /* deadlock to indicate invalid use */
432  }
433  } while (! is_running);
434  } else {
435  while (1) {
436  /* deadlock to indicate invalid use */
437  }
438  }
439 #else /* BSPACM_NRF_USE_SD */
440  NOSD_BODY();
441 #endif /* BSPACM_NRF_USE_SD */
442 
443 #undef NOSD_BODY
444 }
445 
446 #endif /* BSPACM_DEVICE_NRF51_H */
__STATIC_INLINE void vBSPACMnrf51_NVIC_EnableIRQ(IRQn_Type irqn)
Definition: device.h:117
__STATIC_INLINE void vBSPACMnrf51_NVIC_DisableIRQ(IRQn_Type irqn)
Definition: device.h:141
__STATIC_INLINE uint32_t uiBSPACMnrf51_NVIC_GetPriority(IRQn_Type irqn)
Definition: device.h:265
__STATIC_INLINE void vBSPACMnrf51_PPI_CH(uint8_t channel_num, const volatile void *evt_endpoint, const volatile void *task_endpoint)
Definition: device.h:313
__STATIC_INLINE void vBSPACMnrf51_NVIC_SystemReset(void)
Definition: device.h:289
__STATIC_INLINE uint32_t uiBSPACMnrf51_NVIC_GetPendingIRQ(IRQn_Type irqn)
Definition: device.h:165
__STATIC_INLINE void vBSPACMnrf51_HFCLKSTART()
Definition: device.h:412
__STATIC_INLINE void vBSPACMnrf51_NVIC_ClearPendingIRQ(IRQn_Type irqn)
Definition: device.h:216
__STATIC_INLINE void vBSPACMnrf51_NVIC_SetPriority(IRQn_Type irqn, uint32_t priority)
Definition: device.h:240
__STATIC_INLINE void vBSPACMnrf51_PPI_CHENSET(uint32_t mask)
Definition: device.h:364
__STATIC_INLINE void vBSPACMnrf51_NVIC_SetPendingIRQ(IRQn_Type irqn)
Definition: device.h:192
__STATIC_INLINE uint32_t uiBSPACMnrf51_PPI_CHEN()
Definition: device.h:388
__STATIC_INLINE void vBSPACMnrf51_PPI_CHENCLR(uint32_t mask)
Definition: device.h:340
__STATIC_INLINE void vBSPACMnrf51NVICsetApplicationPriority(IRQn_Type irqn, bool high)
Definition: device.h:106