Nordic stack and drivers for the mbed BLE API. Version to work around build bug.

Dependents:   microbit_rubber_ducky microbit_mouse_BLE microbit_mouse_BLE_daybreak_version microbit_presenter

Fork of nRF51822 by Nordic Semiconductor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers app_util_platform.h Source File

app_util_platform.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Nordic Semiconductor ASA
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without modification,
00006  * are permitted provided that the following conditions are met:
00007  *
00008  *   1. Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  *
00011  *   2. Redistributions in binary form must reproduce the above copyright notice, this
00012  *   list of conditions and the following disclaimer in the documentation and/or
00013  *   other materials provided with the distribution.
00014  *
00015  *   3. Neither the name of Nordic Semiconductor ASA nor the names of other
00016  *   contributors to this software may be used to endorse or promote products
00017  *   derived from this software without specific prior written permission.
00018  *
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00021  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
00024  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00027  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  */
00032 
00033 /**@file
00034  *
00035  * @defgroup app_util_platform Utility Functions and Definitions (Platform)
00036  * @{
00037  * @ingroup app_common
00038  *
00039  * @brief Various types and definitions available to all applications when using SoftDevice.
00040  */
00041 
00042 #ifndef APP_UTIL_PLATFORM_H__
00043 #define APP_UTIL_PLATFORM_H__
00044 
00045 #include <stdint.h>
00046 #include "compiler_abstraction.h"
00047 #include "nrf.h"
00048 #ifdef SOFTDEVICE_PRESENT
00049 #include "nrf_soc.h"
00050 #include "app_error.h "
00051 #endif
00052 /**@brief The interrupt priorities available to the application while the SoftDevice is active. */
00053 typedef enum
00054 {
00055 #ifndef SOFTDEVICE_PRESENT
00056     APP_IRQ_PRIORITY_HIGHEST = 0,
00057 #endif
00058     APP_IRQ_PRIORITY_HIGH    = 1,
00059 #ifndef SOFTDEVICE_PRESENT
00060     APP_IRQ_PRIORITY_MID     = 2,
00061 #endif
00062     APP_IRQ_PRIORITY_LOW     = 3
00063 } app_irq_priority_t;
00064 
00065 #define NRF_APP_PRIORITY_THREAD    4                    /**< "Interrupt level" when running in Thread Mode. */
00066 
00067 /**@cond NO_DOXYGEN */
00068 #define EXTERNAL_INT_VECTOR_OFFSET 16
00069 /**@endcond */
00070 
00071 #define PACKED(TYPE) __packed TYPE
00072 
00073 void critical_region_enter (void);
00074 void critical_region_exit (void);
00075 
00076 /**@brief Macro for entering a critical region.
00077  *
00078  * @note Due to implementation details, there must exist one and only one call to
00079  *       CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
00080  *       in the same scope.
00081  */
00082 #ifdef SOFTDEVICE_PRESENT
00083 #define CRITICAL_REGION_ENTER()                                                             \
00084     {                                                                                       \
00085         uint8_t IS_NESTED_CRITICAL_REGION = 0;                                              \
00086         uint32_t CURRENT_INT_PRI = current_int_priority_get();                              \
00087         if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH)                                       \
00088         {                                                                                   \
00089             uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION);  \
00090             if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED)                               \
00091             {                                                                               \
00092                 __disable_irq();                                                            \
00093             }                                                                               \
00094             else                                                                            \
00095             {                                                                               \
00096                 APP_ERROR_CHECK(ERR_CODE);                                                  \
00097             }                                                                               \
00098         }        
00099 #else
00100 #define CRITICAL_REGION_ENTER() critical_region_enter()
00101 #endif
00102 
00103 /**@brief Macro for leaving a critical region.
00104  *
00105  * @note Due to implementation details, there must exist one and only one call to
00106  *       CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
00107  *       in the same scope.
00108  */
00109 #ifdef SOFTDEVICE_PRESENT
00110 #define CRITICAL_REGION_EXIT()                                                              \
00111         if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH)                                       \
00112         {                                                                                   \
00113             uint32_t ERR_CODE;                                                              \
00114             __enable_irq();                                                                 \
00115             ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION);             \
00116             if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED)                               \
00117             {                                                                               \
00118                 APP_ERROR_CHECK(ERR_CODE);                                                  \
00119             }                                                                               \
00120         }                                                                                   \
00121     }
00122 #else
00123 #define CRITICAL_REGION_EXIT() critical_region_exit()
00124 #endif 
00125        
00126 /**@brief Function for finding the current interrupt level.
00127  *
00128  * @return   Current interrupt level.
00129  * @retval   APP_IRQ_PRIORITY_HIGH    We are running in Application High interrupt level.
00130  * @retval   APP_IRQ_PRIORITY_LOW     We are running in Application Low interrupt level.
00131  * @retval   APP_IRQ_PRIORITY_THREAD  We are running in Thread Mode.
00132  */
00133 static __INLINE uint8_t current_int_priority_get(void)
00134 {
00135     uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);
00136     if (isr_vector_num > 0)
00137     {
00138         int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET);
00139         return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF);
00140     }
00141     else
00142     {
00143         return NRF_APP_PRIORITY_THREAD;
00144     }
00145 }
00146 
00147 #endif // APP_UTIL_PLATFORM_H__
00148 
00149 /** @} */