mbed library
Fork of mbed by
TARGET_RBLAB_NRF51822/TARGET_NORDIC/TARGET_MCU_NRF51822/Lib/nrf-sdk/sd_common/app_util_platform.h@89:552587b429a1, 2014-09-12 (annotated)
- Committer:
- bogdanm
- Date:
- Fri Sep 12 16:41:52 2014 +0100
- Revision:
- 89:552587b429a1
Release 89 of the mbed library
Main changes:
- low power optimizations for Nordic targets
- code structure changes for Freescale K64F targets
- bug fixes in various backends
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 89:552587b429a1 | 1 | /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved. |
bogdanm | 89:552587b429a1 | 2 | * |
bogdanm | 89:552587b429a1 | 3 | * The information contained herein is property of Nordic Semiconductor ASA. |
bogdanm | 89:552587b429a1 | 4 | * Terms and conditions of usage are described in detail in NORDIC |
bogdanm | 89:552587b429a1 | 5 | * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT. |
bogdanm | 89:552587b429a1 | 6 | * |
bogdanm | 89:552587b429a1 | 7 | * Licensees are granted free, non-transferable use of the information. NO |
bogdanm | 89:552587b429a1 | 8 | * WARRANTY of ANY KIND is provided. This heading must NOT be removed from |
bogdanm | 89:552587b429a1 | 9 | * the file. |
bogdanm | 89:552587b429a1 | 10 | * |
bogdanm | 89:552587b429a1 | 11 | */ |
bogdanm | 89:552587b429a1 | 12 | |
bogdanm | 89:552587b429a1 | 13 | /**@file |
bogdanm | 89:552587b429a1 | 14 | * |
bogdanm | 89:552587b429a1 | 15 | * @defgroup app_util_platform Utility Functions and Definitions (Platform) |
bogdanm | 89:552587b429a1 | 16 | * @{ |
bogdanm | 89:552587b429a1 | 17 | * @ingroup app_common |
bogdanm | 89:552587b429a1 | 18 | * |
bogdanm | 89:552587b429a1 | 19 | * @brief Various types and definitions available to all applications when using SoftDevice. |
bogdanm | 89:552587b429a1 | 20 | */ |
bogdanm | 89:552587b429a1 | 21 | |
bogdanm | 89:552587b429a1 | 22 | #ifndef APP_UTIL_PLATFORM_H__ |
bogdanm | 89:552587b429a1 | 23 | #define APP_UTIL_PLATFORM_H__ |
bogdanm | 89:552587b429a1 | 24 | |
bogdanm | 89:552587b429a1 | 25 | #include <stdint.h> |
bogdanm | 89:552587b429a1 | 26 | #include "compiler_abstraction.h" |
bogdanm | 89:552587b429a1 | 27 | #include "nrf51.h" |
bogdanm | 89:552587b429a1 | 28 | #include "app_error.h" |
bogdanm | 89:552587b429a1 | 29 | |
bogdanm | 89:552587b429a1 | 30 | /**@brief The interrupt priorities available to the application while the SoftDevice is active. */ |
bogdanm | 89:552587b429a1 | 31 | typedef enum |
bogdanm | 89:552587b429a1 | 32 | { |
bogdanm | 89:552587b429a1 | 33 | APP_IRQ_PRIORITY_HIGH = 1, |
bogdanm | 89:552587b429a1 | 34 | APP_IRQ_PRIORITY_LOW = 3 |
bogdanm | 89:552587b429a1 | 35 | } app_irq_priority_t; |
bogdanm | 89:552587b429a1 | 36 | |
bogdanm | 89:552587b429a1 | 37 | #define NRF_APP_PRIORITY_THREAD 4 /**< "Interrupt level" when running in Thread Mode. */ |
bogdanm | 89:552587b429a1 | 38 | |
bogdanm | 89:552587b429a1 | 39 | /**@cond NO_DOXYGEN */ |
bogdanm | 89:552587b429a1 | 40 | #define EXTERNAL_INT_VECTOR_OFFSET 16 |
bogdanm | 89:552587b429a1 | 41 | /**@endcond */ |
bogdanm | 89:552587b429a1 | 42 | |
bogdanm | 89:552587b429a1 | 43 | #define PACKED(TYPE) __packed TYPE |
bogdanm | 89:552587b429a1 | 44 | |
bogdanm | 89:552587b429a1 | 45 | /**@brief Macro for entering a critical region. |
bogdanm | 89:552587b429a1 | 46 | * |
bogdanm | 89:552587b429a1 | 47 | * @note Due to implementation details, there must exist one and only one call to |
bogdanm | 89:552587b429a1 | 48 | * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located |
bogdanm | 89:552587b429a1 | 49 | * in the same scope. |
bogdanm | 89:552587b429a1 | 50 | */ |
bogdanm | 89:552587b429a1 | 51 | #define CRITICAL_REGION_ENTER() \ |
bogdanm | 89:552587b429a1 | 52 | { \ |
bogdanm | 89:552587b429a1 | 53 | uint8_t IS_NESTED_CRITICAL_REGION = 0; \ |
bogdanm | 89:552587b429a1 | 54 | uint32_t CURRENT_INT_PRI = current_int_priority_get(); \ |
bogdanm | 89:552587b429a1 | 55 | if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ |
bogdanm | 89:552587b429a1 | 56 | { \ |
bogdanm | 89:552587b429a1 | 57 | uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION); \ |
bogdanm | 89:552587b429a1 | 58 | if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ |
bogdanm | 89:552587b429a1 | 59 | { \ |
bogdanm | 89:552587b429a1 | 60 | __disable_irq(); \ |
bogdanm | 89:552587b429a1 | 61 | } \ |
bogdanm | 89:552587b429a1 | 62 | else \ |
bogdanm | 89:552587b429a1 | 63 | { \ |
bogdanm | 89:552587b429a1 | 64 | APP_ERROR_CHECK(ERR_CODE); \ |
bogdanm | 89:552587b429a1 | 65 | } \ |
bogdanm | 89:552587b429a1 | 66 | } |
bogdanm | 89:552587b429a1 | 67 | |
bogdanm | 89:552587b429a1 | 68 | /**@brief Macro for leaving a critical region. |
bogdanm | 89:552587b429a1 | 69 | * |
bogdanm | 89:552587b429a1 | 70 | * @note Due to implementation details, there must exist one and only one call to |
bogdanm | 89:552587b429a1 | 71 | * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located |
bogdanm | 89:552587b429a1 | 72 | * in the same scope. |
bogdanm | 89:552587b429a1 | 73 | */ |
bogdanm | 89:552587b429a1 | 74 | #define CRITICAL_REGION_EXIT() \ |
bogdanm | 89:552587b429a1 | 75 | if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \ |
bogdanm | 89:552587b429a1 | 76 | { \ |
bogdanm | 89:552587b429a1 | 77 | uint32_t ERR_CODE; \ |
bogdanm | 89:552587b429a1 | 78 | __enable_irq(); \ |
bogdanm | 89:552587b429a1 | 79 | ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION); \ |
bogdanm | 89:552587b429a1 | 80 | if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \ |
bogdanm | 89:552587b429a1 | 81 | { \ |
bogdanm | 89:552587b429a1 | 82 | APP_ERROR_CHECK(ERR_CODE); \ |
bogdanm | 89:552587b429a1 | 83 | } \ |
bogdanm | 89:552587b429a1 | 84 | } \ |
bogdanm | 89:552587b429a1 | 85 | } |
bogdanm | 89:552587b429a1 | 86 | |
bogdanm | 89:552587b429a1 | 87 | /**@brief Function for finding the current interrupt level. |
bogdanm | 89:552587b429a1 | 88 | * |
bogdanm | 89:552587b429a1 | 89 | * @return Current interrupt level. |
bogdanm | 89:552587b429a1 | 90 | * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level. |
bogdanm | 89:552587b429a1 | 91 | * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level. |
bogdanm | 89:552587b429a1 | 92 | * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode. |
bogdanm | 89:552587b429a1 | 93 | */ |
bogdanm | 89:552587b429a1 | 94 | static __INLINE uint8_t current_int_priority_get(void) |
bogdanm | 89:552587b429a1 | 95 | { |
bogdanm | 89:552587b429a1 | 96 | uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk); |
bogdanm | 89:552587b429a1 | 97 | if (isr_vector_num > 0) |
bogdanm | 89:552587b429a1 | 98 | { |
bogdanm | 89:552587b429a1 | 99 | int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET); |
bogdanm | 89:552587b429a1 | 100 | return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF); |
bogdanm | 89:552587b429a1 | 101 | } |
bogdanm | 89:552587b429a1 | 102 | else |
bogdanm | 89:552587b429a1 | 103 | { |
bogdanm | 89:552587b429a1 | 104 | return NRF_APP_PRIORITY_THREAD; |
bogdanm | 89:552587b429a1 | 105 | } |
bogdanm | 89:552587b429a1 | 106 | } |
bogdanm | 89:552587b429a1 | 107 | |
bogdanm | 89:552587b429a1 | 108 | #endif // APP_UTIL_PLATFORM_H__ |
bogdanm | 89:552587b429a1 | 109 | |
bogdanm | 89:552587b429a1 | 110 | /** @} */ |