Nordic stack and drivers for the mbed BLE API

Fork of nRF51822 by Nordic Semiconductor

Committer:
Vincent Coubard
Date:
Wed Sep 14 14:39:43 2016 +0100
Revision:
640:c90ae1400bf2
Sync with bdab10dc0f90748b6989c8b577771bb403ca6bd8 from ARMmbed/mbed-os.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vincent Coubard 640:c90ae1400bf2 1 /*
Vincent Coubard 640:c90ae1400bf2 2 * Copyright (c) Nordic Semiconductor ASA
Vincent Coubard 640:c90ae1400bf2 3 * All rights reserved.
Vincent Coubard 640:c90ae1400bf2 4 *
Vincent Coubard 640:c90ae1400bf2 5 * Redistribution and use in source and binary forms, with or without modification,
Vincent Coubard 640:c90ae1400bf2 6 * are permitted provided that the following conditions are met:
Vincent Coubard 640:c90ae1400bf2 7 *
Vincent Coubard 640:c90ae1400bf2 8 * 1. Redistributions of source code must retain the above copyright notice, this
Vincent Coubard 640:c90ae1400bf2 9 * list of conditions and the following disclaimer.
Vincent Coubard 640:c90ae1400bf2 10 *
Vincent Coubard 640:c90ae1400bf2 11 * 2. Redistributions in binary form must reproduce the above copyright notice, this
Vincent Coubard 640:c90ae1400bf2 12 * list of conditions and the following disclaimer in the documentation and/or
Vincent Coubard 640:c90ae1400bf2 13 * other materials provided with the distribution.
Vincent Coubard 640:c90ae1400bf2 14 *
Vincent Coubard 640:c90ae1400bf2 15 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
Vincent Coubard 640:c90ae1400bf2 16 * contributors to this software may be used to endorse or promote products
Vincent Coubard 640:c90ae1400bf2 17 * derived from this software without specific prior written permission.
Vincent Coubard 640:c90ae1400bf2 18 *
Vincent Coubard 640:c90ae1400bf2 19 *
Vincent Coubard 640:c90ae1400bf2 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
Vincent Coubard 640:c90ae1400bf2 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Vincent Coubard 640:c90ae1400bf2 22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Vincent Coubard 640:c90ae1400bf2 23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
Vincent Coubard 640:c90ae1400bf2 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
Vincent Coubard 640:c90ae1400bf2 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
Vincent Coubard 640:c90ae1400bf2 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
Vincent Coubard 640:c90ae1400bf2 27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Vincent Coubard 640:c90ae1400bf2 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Vincent Coubard 640:c90ae1400bf2 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Vincent Coubard 640:c90ae1400bf2 30 *
Vincent Coubard 640:c90ae1400bf2 31 */
Vincent Coubard 640:c90ae1400bf2 32
Vincent Coubard 640:c90ae1400bf2 33 /**@file
Vincent Coubard 640:c90ae1400bf2 34 *
Vincent Coubard 640:c90ae1400bf2 35 * @defgroup app_util_platform Utility Functions and Definitions (Platform)
Vincent Coubard 640:c90ae1400bf2 36 * @{
Vincent Coubard 640:c90ae1400bf2 37 * @ingroup app_common
Vincent Coubard 640:c90ae1400bf2 38 *
Vincent Coubard 640:c90ae1400bf2 39 * @brief Various types and definitions available to all applications when using SoftDevice.
Vincent Coubard 640:c90ae1400bf2 40 */
Vincent Coubard 640:c90ae1400bf2 41
Vincent Coubard 640:c90ae1400bf2 42 #ifndef APP_UTIL_PLATFORM_H__
Vincent Coubard 640:c90ae1400bf2 43 #define APP_UTIL_PLATFORM_H__
Vincent Coubard 640:c90ae1400bf2 44
Vincent Coubard 640:c90ae1400bf2 45 #include <stdint.h>
Vincent Coubard 640:c90ae1400bf2 46 #include "compiler_abstraction.h"
Vincent Coubard 640:c90ae1400bf2 47 #include "nrf.h"
Vincent Coubard 640:c90ae1400bf2 48 #ifdef SOFTDEVICE_PRESENT
Vincent Coubard 640:c90ae1400bf2 49 #include "nrf_soc.h"
Vincent Coubard 640:c90ae1400bf2 50 #include "app_error.h"
Vincent Coubard 640:c90ae1400bf2 51 #endif
Vincent Coubard 640:c90ae1400bf2 52 /**@brief The interrupt priorities available to the application while the SoftDevice is active. */
Vincent Coubard 640:c90ae1400bf2 53 typedef enum
Vincent Coubard 640:c90ae1400bf2 54 {
Vincent Coubard 640:c90ae1400bf2 55 #ifndef SOFTDEVICE_PRESENT
Vincent Coubard 640:c90ae1400bf2 56 APP_IRQ_PRIORITY_HIGHEST = 0,
Vincent Coubard 640:c90ae1400bf2 57 #endif
Vincent Coubard 640:c90ae1400bf2 58 APP_IRQ_PRIORITY_HIGH = 1,
Vincent Coubard 640:c90ae1400bf2 59 #ifndef SOFTDEVICE_PRESENT
Vincent Coubard 640:c90ae1400bf2 60 APP_IRQ_PRIORITY_MID = 2,
Vincent Coubard 640:c90ae1400bf2 61 #endif
Vincent Coubard 640:c90ae1400bf2 62 APP_IRQ_PRIORITY_LOW = 3
Vincent Coubard 640:c90ae1400bf2 63 } app_irq_priority_t;
Vincent Coubard 640:c90ae1400bf2 64
Vincent Coubard 640:c90ae1400bf2 65 #define NRF_APP_PRIORITY_THREAD 4 /**< "Interrupt level" when running in Thread Mode. */
Vincent Coubard 640:c90ae1400bf2 66
Vincent Coubard 640:c90ae1400bf2 67 /**@cond NO_DOXYGEN */
Vincent Coubard 640:c90ae1400bf2 68 #define EXTERNAL_INT_VECTOR_OFFSET 16
Vincent Coubard 640:c90ae1400bf2 69 /**@endcond */
Vincent Coubard 640:c90ae1400bf2 70
Vincent Coubard 640:c90ae1400bf2 71 #define PACKED(TYPE) __packed TYPE
Vincent Coubard 640:c90ae1400bf2 72
Vincent Coubard 640:c90ae1400bf2 73 void critical_region_enter (void);
Vincent Coubard 640:c90ae1400bf2 74 void critical_region_exit (void);
Vincent Coubard 640:c90ae1400bf2 75
Vincent Coubard 640:c90ae1400bf2 76 /**@brief Macro for entering a critical region.
Vincent Coubard 640:c90ae1400bf2 77 *
Vincent Coubard 640:c90ae1400bf2 78 * @note Due to implementation details, there must exist one and only one call to
Vincent Coubard 640:c90ae1400bf2 79 * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
Vincent Coubard 640:c90ae1400bf2 80 * in the same scope.
Vincent Coubard 640:c90ae1400bf2 81 */
Vincent Coubard 640:c90ae1400bf2 82 #ifdef SOFTDEVICE_PRESENT
Vincent Coubard 640:c90ae1400bf2 83 #define CRITICAL_REGION_ENTER() \
Vincent Coubard 640:c90ae1400bf2 84 { \
Vincent Coubard 640:c90ae1400bf2 85 uint8_t IS_NESTED_CRITICAL_REGION = 0; \
Vincent Coubard 640:c90ae1400bf2 86 uint32_t CURRENT_INT_PRI = current_int_priority_get(); \
Vincent Coubard 640:c90ae1400bf2 87 if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \
Vincent Coubard 640:c90ae1400bf2 88 { \
Vincent Coubard 640:c90ae1400bf2 89 uint32_t ERR_CODE = sd_nvic_critical_region_enter(&IS_NESTED_CRITICAL_REGION); \
Vincent Coubard 640:c90ae1400bf2 90 if (ERR_CODE == NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \
Vincent Coubard 640:c90ae1400bf2 91 { \
Vincent Coubard 640:c90ae1400bf2 92 __disable_irq(); \
Vincent Coubard 640:c90ae1400bf2 93 } \
Vincent Coubard 640:c90ae1400bf2 94 else \
Vincent Coubard 640:c90ae1400bf2 95 { \
Vincent Coubard 640:c90ae1400bf2 96 APP_ERROR_CHECK(ERR_CODE); \
Vincent Coubard 640:c90ae1400bf2 97 } \
Vincent Coubard 640:c90ae1400bf2 98 }
Vincent Coubard 640:c90ae1400bf2 99 #else
Vincent Coubard 640:c90ae1400bf2 100 #define CRITICAL_REGION_ENTER() critical_region_enter()
Vincent Coubard 640:c90ae1400bf2 101 #endif
Vincent Coubard 640:c90ae1400bf2 102
Vincent Coubard 640:c90ae1400bf2 103 /**@brief Macro for leaving a critical region.
Vincent Coubard 640:c90ae1400bf2 104 *
Vincent Coubard 640:c90ae1400bf2 105 * @note Due to implementation details, there must exist one and only one call to
Vincent Coubard 640:c90ae1400bf2 106 * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
Vincent Coubard 640:c90ae1400bf2 107 * in the same scope.
Vincent Coubard 640:c90ae1400bf2 108 */
Vincent Coubard 640:c90ae1400bf2 109 #ifdef SOFTDEVICE_PRESENT
Vincent Coubard 640:c90ae1400bf2 110 #define CRITICAL_REGION_EXIT() \
Vincent Coubard 640:c90ae1400bf2 111 if (CURRENT_INT_PRI != APP_IRQ_PRIORITY_HIGH) \
Vincent Coubard 640:c90ae1400bf2 112 { \
Vincent Coubard 640:c90ae1400bf2 113 uint32_t ERR_CODE; \
Vincent Coubard 640:c90ae1400bf2 114 __enable_irq(); \
Vincent Coubard 640:c90ae1400bf2 115 ERR_CODE = sd_nvic_critical_region_exit(IS_NESTED_CRITICAL_REGION); \
Vincent Coubard 640:c90ae1400bf2 116 if (ERR_CODE != NRF_ERROR_SOFTDEVICE_NOT_ENABLED) \
Vincent Coubard 640:c90ae1400bf2 117 { \
Vincent Coubard 640:c90ae1400bf2 118 APP_ERROR_CHECK(ERR_CODE); \
Vincent Coubard 640:c90ae1400bf2 119 } \
Vincent Coubard 640:c90ae1400bf2 120 } \
Vincent Coubard 640:c90ae1400bf2 121 }
Vincent Coubard 640:c90ae1400bf2 122 #else
Vincent Coubard 640:c90ae1400bf2 123 #define CRITICAL_REGION_EXIT() critical_region_exit()
Vincent Coubard 640:c90ae1400bf2 124 #endif
Vincent Coubard 640:c90ae1400bf2 125
Vincent Coubard 640:c90ae1400bf2 126 /**@brief Function for finding the current interrupt level.
Vincent Coubard 640:c90ae1400bf2 127 *
Vincent Coubard 640:c90ae1400bf2 128 * @return Current interrupt level.
Vincent Coubard 640:c90ae1400bf2 129 * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level.
Vincent Coubard 640:c90ae1400bf2 130 * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level.
Vincent Coubard 640:c90ae1400bf2 131 * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode.
Vincent Coubard 640:c90ae1400bf2 132 */
Vincent Coubard 640:c90ae1400bf2 133 static __INLINE uint8_t current_int_priority_get(void)
Vincent Coubard 640:c90ae1400bf2 134 {
Vincent Coubard 640:c90ae1400bf2 135 uint32_t isr_vector_num = (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk);
Vincent Coubard 640:c90ae1400bf2 136 if (isr_vector_num > 0)
Vincent Coubard 640:c90ae1400bf2 137 {
Vincent Coubard 640:c90ae1400bf2 138 int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET);
Vincent Coubard 640:c90ae1400bf2 139 return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF);
Vincent Coubard 640:c90ae1400bf2 140 }
Vincent Coubard 640:c90ae1400bf2 141 else
Vincent Coubard 640:c90ae1400bf2 142 {
Vincent Coubard 640:c90ae1400bf2 143 return NRF_APP_PRIORITY_THREAD;
Vincent Coubard 640:c90ae1400bf2 144 }
Vincent Coubard 640:c90ae1400bf2 145 }
Vincent Coubard 640:c90ae1400bf2 146
Vincent Coubard 640:c90ae1400bf2 147 #endif // APP_UTIL_PLATFORM_H__
Vincent Coubard 640:c90ae1400bf2 148
Vincent Coubard 640:c90ae1400bf2 149 /** @} */