SG RFID nRF51822 fork

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 /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
00002  *
00003  * The information contained herein is property of Nordic Semiconductor ASA.
00004  * Terms and conditions of usage are described in detail in NORDIC
00005  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
00006  *
00007  * Licensees are granted free, non-transferable use of the information. NO
00008  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
00009  * the file.
00010  *
00011  */
00012 
00013 /**@file
00014  *
00015  * @defgroup app_util_platform Utility Functions and Definitions (Platform)
00016  * @{
00017  * @ingroup app_common
00018  *
00019  * @brief Various types and definitions available to all applications when using SoftDevice.
00020  */
00021 
00022 #ifndef APP_UTIL_PLATFORM_H__
00023 #define APP_UTIL_PLATFORM_H__
00024 
00025 #include <stdint.h>
00026 #include "compiler_abstraction.h"
00027 #include "nrf51.h"
00028 #include "app_error.h "
00029 
00030 /**@brief The interrupt priorities available to the application while the SoftDevice is active. */
00031 typedef enum
00032 {
00033     APP_IRQ_PRIORITY_HIGH = 1,
00034     APP_IRQ_PRIORITY_LOW  = 3
00035 } app_irq_priority_t;
00036 
00037 #define NRF_APP_PRIORITY_THREAD    4                    /**< "Interrupt level" when running in Thread Mode. */
00038 
00039 /**@cond NO_DOXYGEN */
00040 #define EXTERNAL_INT_VECTOR_OFFSET 16
00041 /**@endcond */
00042 
00043 #define PACKED(TYPE) __packed TYPE
00044 
00045 /**@brief Macro for entering a critical region.
00046  *
00047  * @note Due to implementation details, there must exist one and only one call to
00048  *       CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
00049  *       in the same scope.
00050  */
00051 #define CRITICAL_REGION_ENTER()                                                             \
00052     {                                                                                       \
00053         uint8_t IS_NESTED_CRITICAL_REGION = 0;                                              \
00054         uint32_t CURRENT_INT_PRI = current_int_priority_get();                              \
00055         if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH)                                       \
00056         {                                                                                   \
00057             uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION);  \
00058             if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED)                               \
00059             {                                                                               \
00060                 __disable_irq();                                                            \
00061             }                                                                               \
00062             else                                                                            \
00063             {                                                                               \
00064                 APP_ERROR_CHECK(ERR_CODE);                                                  \
00065             }                                                                               \
00066         }
00067 
00068 /**@brief Macro for leaving a critical region.
00069  *
00070  * @note Due to implementation details, there must exist one and only one call to
00071  *       CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
00072  *       in the same scope.
00073  */
00074 #define CRITICAL_REGION_EXIT()                                                              \
00075         if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH)                                       \
00076         {                                                                                   \
00077             uint32_t ERR_CODE;                                                              \
00078             __enable_irq();                                                                 \
00079             ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION);             \
00080             if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED)                               \
00081             {                                                                               \
00082                 APP_ERROR_CHECK(ERR_CODE);                                                  \
00083             }                                                                               \
00084         }                                                                                   \
00085     }
00086 
00087 /**@brief Function for finding the current interrupt level.
00088  *
00089  * @return   Current interrupt level.
00090  * @retval   APP_IRQ_PRIORITY_HIGH    We are running in Application High interrupt level.
00091  * @retval   APP_IRQ_PRIORITY_LOW     We are running in Application Low interrupt level.
00092  * @retval   APP_IRQ_PRIORITY_THREAD  We are running in Thread Mode.
00093  */
00094 static __INLINE uint8_t current_int_priority_get(void)
00095 {
00096     uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);
00097     if (isr_vector_num > 0)
00098     {
00099         int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET);
00100         return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF);
00101     }
00102     else
00103     {
00104         return NRF_APP_PRIORITY_THREAD;
00105     }
00106 }
00107 
00108 #endif // APP_UTIL_PLATFORM_H__
00109 
00110 /** @} */