BLE_Nano nRF51 Central heart rate

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers softdevice_handler.h Source File

softdevice_handler.h

Go to the documentation of this file.
00001 /* Copyright (c) 2013 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 softdevice_handler SoftDevice Event Handler
00016  * @{
00017  * @ingroup  app_common
00018  * @brief    API for initializing and disabling the SoftDevice
00019  *
00020  * @details  This API contains the functions and defines exposed by the @ref lib_softdevice_handler.
00021  *           For more information on the library and how the application should use it, please refer
00022  *           @ref lib_softdevice_handler.
00023  *
00024  * @note     Use the USE_SCHEDULER parameter of the SOFTDEVICE_HANDLER_INIT() macro to select if
00025  *           the @ref app_scheduler is to be used or not.
00026  *
00027  * @note     Even if the scheduler is not used, softdevice_handler.h will include app_scheduler.h.
00028  *           So when compiling, app_scheduler.h must be available in one of the compiler include
00029  *           paths.
00030  */
00031 
00032 #ifndef SOFTDEVICE_HANDLER_H__
00033 #define SOFTDEVICE_HANDLER_H__
00034 
00035 #include <stdlib.h>
00036 #include "nordic_common.h"
00037 #include "nrf_sdm.h"
00038 #include "app_error.h "
00039 #include "app_util.h "
00040 #include "ble_stack_handler_types.h "
00041 #include "ant_stack_handler_types.h "
00042 #if defined(BLE_STACK_SUPPORT_REQD)
00043     #include "ble.h"
00044 #endif
00045 #include "app_ram_base.h"
00046 #define SOFTDEVICE_SCHED_EVT_SIZE       0                                                 /**< Size of button events being passed through the scheduler (is to be used for computing the maximum size of scheduler events). For SoftDevice events, this size is 0, since the events are being pulled in the event handler. */
00047 #define SYS_EVT_MSG_BUF_SIZE            sizeof(uint32_t)                                  /**< Size of System (SOC) event message buffer. */
00048 
00049 
00050 #define CHECK_RAM_START_ADDR_INTERN(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT)              \
00051     do{                                                                                     \
00052         uint32_t app_ram_start_addr = APP_RAM_BASE_CENTRAL_LINKS_##CENTRAL_LINK_COUNT##_PERIPH_LINKS_##PERIPHERAL_LINK_COUNT##_SEC_COUNT_0_MID_BW; \
00053         err_code = sd_check_ram_start(app_ram_start_addr);                                  \
00054         APP_ERROR_CHECK(err_code);                                                          \
00055     } while (0)
00056 
00057 /** @brief Macro for checking the RAM requirement of the SoftDevice */
00058 #define CHECK_RAM_START_ADDR(C_LINK_CNT, P_LINK_CNT) CHECK_RAM_START_ADDR_INTERN(C_LINK_CNT, P_LINK_CNT)
00059 
00060 
00061 /**@brief     Function for checking the RAM requirement of the SoftDevice.
00062  *
00063  * @details   Call this function to check if the project settings have the correct RAM start address in respect to what the SoftDevice requires.
00064  *
00065  * @note      This function is called using the CHECK_RAM_START_ADDR_INTERN macro and should not be called directly.
00066  */
00067 uint32_t sd_check_ram_start(uint32_t sd_req_ram_start);
00068 
00069 /**@brief Type of function for passing events from the stack handler module to the scheduler. */
00070 typedef uint32_t (*softdevice_evt_schedule_func_t) (void);
00071 
00072 /**@brief Application System (SOC) event handler type. */
00073 typedef void (*sys_evt_handler_t) (uint32_t evt_id);
00074 
00075 
00076 /**@brief     Macro for initializing the stack event handler.
00077  *
00078  * @details   It will handle dimensioning and allocation of the memory buffer required for reading
00079  *            events from the stack, making sure the buffer is correctly aligned. It will also
00080  *            connect the stack event handler to the scheduler/RTOS (if specified).
00081  *
00082  * @param[in] CLOCK_SOURCE     Low frequency clock source and accuracy (type nrf_clock_lf_cfg_t_t,
00083  *                             see sd_softdevice_enable() for details).
00084  * @param[in] EVT_HANDLER      scheduler/RTOS event handler function.
00085  *
00086  * @note      Since this macro allocates a buffer, it must only be called once (it is OK to call it
00087  *            several times as long as it is from the same location, that is to do a
00088  *            reinitialization).
00089  */
00090 /*lint -emacro(506, SOFTDEVICE_HANDLER_INIT) */ /* Suppress "Constant value Boolean */
00091 #define SOFTDEVICE_HANDLER_INIT(CLOCK_SOURCE,                                                      \
00092                                 EVT_HANDLER)                                                       \
00093     do                                                                                             \
00094     {                                                                                              \
00095         static uint32_t BLE_EVT_BUFFER[CEIL_DIV(BLE_STACK_EVT_MSG_BUF_SIZE, sizeof(uint32_t))];    \
00096         uint32_t ERR_CODE;                                                                         \
00097         ERR_CODE = softdevice_handler_init((CLOCK_SOURCE),                                         \
00098                                            BLE_EVT_BUFFER,                                         \
00099                                            sizeof(BLE_EVT_BUFFER),                                 \
00100                                            EVT_HANDLER);                                           \
00101         APP_ERROR_CHECK(ERR_CODE);                                                                 \
00102     } while (0)
00103 
00104 /**
00105  * @brief Function for retrieving the information about SD state
00106  *
00107  * The information about current state of softdevice.
00108  * @retval false SD is not initialized and SD commands should not be called.
00109  * @retval true  SD is already initialized
00110  */
00111 bool softdevice_handler_isEnabled(void);
00112 
00113 /**@brief      Function for initializing the stack handler module.
00114  *
00115  * @details    Enables the SoftDevice and the stack event interrupt handler.
00116  *
00117  * @note       This function must be called before calling any function in the SoftDevice API.
00118  *
00119  * @note       Normally initialization should be done using the SOFTDEVICE_HANDLER_INIT() macro,
00120  *             as that will both allocate the event buffer, and also align the buffer correctly.
00121  *
00122  * @param[in]  p_clock_lf_cfg      Low frequency clock source to be used by the SoftDevice.
00123  * @param[in]  p_ble_evt_buffer    Buffer for holding one BLE stack event. Since heap is not being
00124  *                                 used, this buffer must be provided by the application. The
00125  *                                 buffer must be large enough to hold the biggest stack event the
00126  *                                 application is supposed to handle. The buffer must be aligned to
00127  *                                 a 4 byte boundary. This parameter is unused if BLE stack support 
00128  *                                 is not required.
00129  * @param[in]  ble_evt_buffer_size Size of SoftDevice BLE event buffer. This parameter is unused if
00130  *                                 BLE stack support is not required.
00131  * @param[in]  evt_schedule_func   Function for passing events to the scheduler. Point to
00132  *                                 ble_ant_stack_evt_schedule() to connect to the scheduler.
00133  *                                 Set to NULL to make the stack handler module call the event
00134  *                                 handler directly from the stack event interrupt handler.
00135  *
00136  * @retval     NRF_SUCCESS               Successful initialization.
00137  * @retval     NRF_ERROR_INVALID_PARAM   Invalid parameter (buffer not aligned to a 4 byte
00138  *                                       boundary) or NULL.
00139  */
00140 uint32_t softdevice_handler_init(nrf_clock_lf_cfg_t *              p_clock_lf_cfg,
00141                                  void *                            p_ble_evt_buffer,
00142                                  uint16_t                          ble_evt_buffer_size,
00143                                  softdevice_evt_schedule_func_t    evt_schedule_func);
00144 
00145 
00146 /**@brief     Function for disabling the SoftDevice.
00147  *
00148  * @details   This function will disable the SoftDevice. It will also update the internal state
00149  *            of this module.
00150  */
00151 uint32_t softdevice_handler_sd_disable(void);
00152 
00153 
00154 /**@brief     Function for registering for System (SOC) events.
00155  *
00156  * @details   The application should use this function to register for receiving System (SOC)
00157  *            events from the SoftDevice. If the application does not call this function, then any
00158  *            System (SOC) events that may be generated by the SoftDevice will NOT be fetched. Once
00159  *            the application has registered for the events, it is not possible to  possible to
00160  *            cancel the registration. However, it is possible to register a different function for
00161  *            handling the events at any point of time.
00162  *
00163  * @param[in] sys_evt_handler Function to be called for each received System (SOC) event.
00164  *
00165  * @retval    NRF_SUCCESS     Successful registration.
00166  * @retval    NRF_ERROR_NULL  Null pointer provided as input.
00167  */
00168 uint32_t softdevice_sys_evt_handler_set(sys_evt_handler_t sys_evt_handler);
00169 
00170 #if defined(BLE_STACK_SUPPORT_REQD)
00171 /**@brief     Function for fetching the default enable parameters for the SoftDevice.
00172  *
00173  * @details   The default enable parameters will work for most projects in the SDK.
00174  *            They are not optimized with regards to RAM use. This function is meant as a way to abstract the
00175  *            details of p_ble_enable_params needed by @ref softdevice_enable. You might want to tweak
00176  *            the struct returned by this function or fill in the entire ble_enable_params_t
00177  *            instead of fetching it from this function.
00178  *
00179  * @param[in]  central_links_count Number of central links used by the application.
00180  * @param[in]  periph_links_count Number of peripheral links used by the application.
00181  * @param[out] p_ble_enable_params Default ble_enable_params_t to be used by @ref softdevice_enable.
00182  *
00183  * @retval    NRF_SUCCESS     If the operation was successful.
00184  */
00185 uint32_t softdevice_enable_get_default_config(uint8_t central_links_count, 
00186                                               uint8_t periph_links_count,
00187                                               ble_enable_params_t * p_ble_enable_params);
00188 
00189 /**@brief     Function for enabling the SoftDevice.
00190  *
00191  * @details   This function calls the @ref sd_ble_enable SVC call. It has been abstracted to give
00192  *            feedback on the app_ram_base. If the app_ram_base is too low, this function will
00193  *            return an error. Using a app_ram_base that is too high will not fail, but will
00194  *            result in RAM that is never used. If the DEBUG macro is enabled, this
00195  *            function will provide the correct app_ram_base as mandated by the SoftDevice.
00196  *            This is useful to tweak the RAM use of your application.
00197  *
00198  * @param[in]  p_ble_enable_params Parameters for configuring links and bandwidths.
00199  *
00200  * @retval    NRF_SUCCESS     If the operation was successful.
00201  */
00202 uint32_t softdevice_enable(ble_enable_params_t * p_ble_enable_params);
00203 #endif //BLE_STACK_SUPPORT_REQD
00204 // Functions for connecting the Stack Event Handler to the scheduler:
00205 /**@cond NO_DOXYGEN */
00206 void intern_softdevice_events_execute(void);
00207 
00208 
00209 /**@endcond */
00210 
00211 #endif // SOFTDEVICE_HANDLER_H__
00212 
00213 /** @} */
00214