nRF51 SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Main file for Heart Rate Service Sample Application for nRF51822 evaluation board. More...

Macros

#define ADC_REF_VOLTAGE_IN_MILLIVOLTS   1200
 
#define ADC_PRE_SCALING_COMPENSATION   3
 
#define DIODE_FWD_VOLT_DROP_MILLIVOLTS   270
 
#define ADC_RESULT_IN_MILLI_VOLTS(ADC_VALUE)   ((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS) / 255) * ADC_PRE_SCALING_COMPENSATION)
 Macro to convert the result of ADC conversion in millivolts. More...
 
#define HR_INC_BUTTON_PIN_NO   EVAL_BOARD_BUTTON_0
 
#define HR_DEC_BUTTON_PIN_NO   EVAL_BOARD_BUTTON_1
 
#define BONDMNGR_DELETE_BUTTON_PIN_NO   HR_DEC_BUTTON_PIN_NO
 
#define DEVICE_NAME   "Nordic_HRM"
 
#define MANUFACTURER_NAME   "NordicSemiconductor"
 
#define APP_ADV_INTERVAL   40
 
#define APP_ADV_TIMEOUT_IN_SECONDS   180
 
#define APP_TIMER_PRESCALER   0
 
#define APP_TIMER_MAX_TIMERS   4
 
#define APP_TIMER_OP_QUEUE_SIZE   5
 
#define BATTERY_LEVEL_MEAS_INTERVAL   APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER)
 
#define HEART_RATE_MEAS_INTERVAL   APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)
 
#define MIN_HEART_RATE   60
 
#define MAX_HEART_RATE   300
 
#define HEART_RATE_CHANGE   2
 
#define APP_GPIOTE_MAX_USERS   1
 
#define BUTTON_DETECTION_DELAY   APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)
 
#define MIN_CONN_INTERVAL   MSEC_TO_UNITS(500, UNIT_1_25_MS)
 
#define MAX_CONN_INTERVAL   MSEC_TO_UNITS(1000, UNIT_1_25_MS)
 
#define SLAVE_LATENCY   0
 
#define CONN_SUP_TIMEOUT   MSEC_TO_UNITS(4000, UNIT_10_MS)
 
#define FIRST_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)
 
#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)
 
#define MAX_CONN_PARAMS_UPDATE_COUNT   3
 
#define SEC_PARAM_TIMEOUT   30
 
#define SEC_PARAM_BOND   1
 
#define SEC_PARAM_MITM   0
 
#define SEC_PARAM_IO_CAPABILITIES   BLE_GAP_IO_CAPS_NONE
 
#define SEC_PARAM_OOB   0
 
#define SEC_PARAM_MIN_KEY_SIZE   7
 
#define SEC_PARAM_MAX_KEY_SIZE   16
 
#define FLASH_PAGE_SYS_ATTR   (BLE_FLASH_PAGE_END - 3)
 
#define FLASH_PAGE_BOND   (BLE_FLASH_PAGE_END - 1)
 
#define DEAD_BEEF   0xDEADBEEF
 

Functions

void ADC_IRQHandler (void)
 Function for handling the ADC interrupt. More...
 
void battery_start (void)
 
static void ble_evt_dispatch (ble_evt_t *p_ble_evt)
 Function for dispatching a BLE stack event to all modules with a BLE stack event handler. More...
 
void app_error_handler (uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
 Function for error handling, which is called when an error has occurred. More...
 
void assert_nrf_callback (uint16_t line_num, const uint8_t *p_file_name)
 Callback function for asserts in the SoftDevice. More...
 
static void conn_params_error_handler (uint32_t nrf_error)
 Function for handling a Connection Parameters error. More...
 
static void bond_manager_error_handler (uint32_t nrf_error)
 Function for handling a Bond Manager error. More...
 
static void battery_level_meas_timeout_handler (void *p_context)
 Function for handling the Battery measurement timer timeout. More...
 
static void heart_rate_meas_timeout_handler (void *p_context)
 Function for handling the Heart rate measurement timer timeout. More...
 
static void button_event_handler (uint8_t pin_no)
 Function for handling button events. More...
 
static void timers_init (void)
 Function for the Timer initialization. More...
 
static void gap_params_init (void)
 Function for the GAP initialization. More...
 
static void advertising_init (void)
 Function for initializing the Advertising functionality. More...
 
static void services_init (void)
 Function for initializing the services that will be used by the application. More...
 
static void sec_params_init (void)
 Function for initializing the security parameters.
 
static void conn_params_init (void)
 Function for initializing the Connection Parameters module.
 
static void bond_manager_init (void)
 Function for the Bond Manager initialization.
 
static void radio_notification_init (void)
 Function for initializing the Radio Notification event.
 
static void ble_stack_init (void)
 Function for initializing the BLE stack. More...
 
static void gpiote_init (void)
 Function for initializing the GPIOTE module.
 
static void buttons_init (void)
 Function for initializing the button module.
 
static bool is_first_start (void)
 Function for checking if this is the first start, or if it was a restart due to a pushed button.
 
static void application_timers_start (void)
 Function for starting the application timers.
 
static void advertising_start (void)
 Function for starting advertising.
 
static void system_off_mode_enter (void)
 Function for putting the chip in System OFF Mode.
 
static void on_ble_evt (ble_evt_t *p_ble_evt)
 Function for handling the Application's BLE Stack events. More...
 
int main (void)
 Function for the application main entry.
 

Variables

static ble_gap_sec_params_t m_sec_params
 
static ble_gap_adv_params_t m_adv_params
 
ble_bas_t bas
 
static ble_hrs_t m_hrs
 
static volatile uint16_t m_cur_heart_rate
 
static app_timer_id_t m_battery_timer_id
 
static app_timer_id_t m_heart_rate_timer_id
 

Detailed Description

This file contains the source code for a sample application using the Heart Rate service (and also Battery and Device Information services) for the nRF51822 evaluation board (PCA10001). This application uses the Connection Parameters Negotiation module.

Macro Definition Documentation

#define ADC_REF_VOLTAGE_IN_MILLIVOLTS   1200

Reference voltage (in milli volts) used by ADC while doing conversion.

#define ADC_PRE_SCALING_COMPENSATION   3

The ADC is configured to use VDD with 1/3 prescaling as input. And hence the result of conversion is to be multiplied by 3 to get the actual value of the battery voltage.

#define DIODE_FWD_VOLT_DROP_MILLIVOLTS   270

Typical forward voltage drop of the diode (Part no: SD103ATW-7-F) that is connected in series with the voltage supply. This is the voltage drop when the forward current is 1mA. Source: Data sheet of 'SURFACE MOUNT SCHOTTKY BARRIER DIODE ARRAY' available at www.diodes.com.

#define ADC_RESULT_IN_MILLI_VOLTS (   ADC_VALUE)    ((((ADC_VALUE) * ADC_REF_VOLTAGE_IN_MILLIVOLTS) / 255) * ADC_PRE_SCALING_COMPENSATION)
Parameters
[in]ADC_VALUEADC result.
Return values
Resultconverted to millivolts.
#define HR_INC_BUTTON_PIN_NO   EVAL_BOARD_BUTTON_0

Button used to increment heart rate.

#define HR_DEC_BUTTON_PIN_NO   EVAL_BOARD_BUTTON_1

Button used to decrement heart rate.

#define BONDMNGR_DELETE_BUTTON_PIN_NO   HR_DEC_BUTTON_PIN_NO

Button used for deleting all bonded masters during startup.

#define DEVICE_NAME   "Nordic_HRM"

Name of device. Will be included in the advertising data.

#define MANUFACTURER_NAME   "NordicSemiconductor"

Manufacturer. Will be passed to Device Information Service.

#define APP_ADV_INTERVAL   40

The advertising interval (in units of 0.625 ms. This value corresponds to 25 ms).

#define APP_ADV_TIMEOUT_IN_SECONDS   180

The advertising timeout in units of seconds.

#define APP_TIMER_PRESCALER   0

Value of the RTC1 PRESCALER register.

#define APP_TIMER_MAX_TIMERS   4

Maximum number of simultaneously created timers.

#define APP_TIMER_OP_QUEUE_SIZE   5

Size of timer operation queues.

#define BATTERY_LEVEL_MEAS_INTERVAL   APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER)

Battery level measurement interval (ticks).

#define HEART_RATE_MEAS_INTERVAL   APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)

Heart rate measurement interval (ticks).

#define MIN_HEART_RATE   60

Minimum heart rate as returned by the simulated measurement function.

#define MAX_HEART_RATE   300

Maximum heart rate as returned by the simulated measurement function.

#define HEART_RATE_CHANGE   2

Value by which the heart rate is incremented/decremented during button press.

#define APP_GPIOTE_MAX_USERS   1

Maximum number of users of the GPIOTE handler.

#define BUTTON_DETECTION_DELAY   APP_TIMER_TICKS(50, APP_TIMER_PRESCALER)

Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks).

#define MIN_CONN_INTERVAL   MSEC_TO_UNITS(500, UNIT_1_25_MS)

Minimum acceptable connection interval (0.5 seconds).

#define MAX_CONN_INTERVAL   MSEC_TO_UNITS(1000, UNIT_1_25_MS)

Maximum acceptable connection interval (1 second).

#define SLAVE_LATENCY   0

Slave latency.

#define CONN_SUP_TIMEOUT   MSEC_TO_UNITS(4000, UNIT_10_MS)

Connection supervisory timeout (4 seconds).

#define FIRST_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)

Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds).

#define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER)

Time between each call to sd_ble_gap_conn_param_update after the first (30 seconds).

#define MAX_CONN_PARAMS_UPDATE_COUNT   3

Number of attempts before giving up the connection parameter negotiation.

#define SEC_PARAM_TIMEOUT   30

Timeout for Pairing Request or Security Request (in seconds).

#define SEC_PARAM_BOND   1

Perform bonding.

#define SEC_PARAM_MITM   0

Man In The Middle protection not required.

#define SEC_PARAM_IO_CAPABILITIES   BLE_GAP_IO_CAPS_NONE

No I/O capabilities.

#define SEC_PARAM_OOB   0

Out Of Band data not available.

#define SEC_PARAM_MIN_KEY_SIZE   7

Minimum encryption key size.

#define SEC_PARAM_MAX_KEY_SIZE   16

Maximum encryption key size.

#define FLASH_PAGE_SYS_ATTR   (BLE_FLASH_PAGE_END - 3)

Flash page used for bond manager system attribute information.

#define FLASH_PAGE_BOND   (BLE_FLASH_PAGE_END - 1)

Flash page used for bond manager bonding information.

#define DEAD_BEEF   0xDEADBEEF

Value used as error code on stack dump, can be used to identify stack location on stack unwind.

Function Documentation

void ADC_IRQHandler ( void  )

This function will fetch the conversion result from the ADC, convert the value into percentage and send it to peer.

static void ble_evt_dispatch ( ble_evt_t p_ble_evt)
static

This function is called from the BLE Stack event interrupt handler after a BLE stack event has been received.

Parameters
[in]p_ble_evtBluetooth stack event.
void app_error_handler ( uint32_t  error_code,
uint32_t  line_num,
const uint8_t *  p_file_name 
)
Warning
This handler is an example only and does not fit a final product. You need to analyze how your product is supposed to react in case of error.
Parameters
[in]error_codeError code supplied to the handler.
[in]line_numLine number where the handler is called.
[in]p_file_namePointer to the file name.
void assert_nrf_callback ( uint16_t  line_num,
const uint8_t *  p_file_name 
)

This function will be called in case of an assert in the SoftDevice.

Warning
This handler is an example only and does not fit a final product. You need to analyze how your product is supposed to react in case of Assert.
On assert from the SoftDevice, the system can only recover on reset.
Parameters
[in]line_numLine number of the failing ASSERT call.
[in]file_nameFile name of the failing ASSERT call.
static void conn_params_error_handler ( uint32_t  nrf_error)
static
Parameters
[in]nrf_errorError code containing information about what went wrong.
static void bond_manager_error_handler ( uint32_t  nrf_error)
static
Parameters
[in]nrf_errorError code containing information about what went wrong.
static void battery_level_meas_timeout_handler ( void *  p_context)
static

This function will be called each time the battery level measurement timer expires. This function will start the ADC.

Parameters
[in]p_contextPointer used for passing some arbitrary information (context) from the app_start_timer() call to the timeout handler.
static void heart_rate_meas_timeout_handler ( void *  p_context)
static

This function will be called each time the heart rate measurement timer expires. It will exclude RR Interval data from every third measurement.

Parameters
[in]p_contextPointer used for passing some arbitrary information (context) from the app_start_timer() call to the timeout handler.
static void button_event_handler ( uint8_t  pin_no)
static
Parameters
[in]pin_noThe pin number of the button pressed.
static void timers_init ( void  )
static

Initializes the timer module. This creates and starts application timers.

static void gap_params_init ( void  )
static

This function shall be used to setup all the necessary GAP (Generic Access Profile) parameters of the device. It also sets the permissions and appearance.

static void advertising_init ( void  )
static

Encodes the required advertising data and passes it to the stack. Also builds a structure to be passed to the stack when starting advertising.

static void services_init ( void  )
static

Initialize the Heart Rate, Battery and Device Information services.

static void ble_stack_init ( void  )
static

Initializes the SoftDevice and the BLE event interrupt.

static void on_ble_evt ( ble_evt_t p_ble_evt)
static
Parameters
[in]p_ble_evtBluetooth stack event.

Variable Documentation

ble_gap_sec_params_t m_sec_params
static

Security requirements for this application.

ble_gap_adv_params_t m_adv_params
static

Parameters to be passed to the stack when starting advertising.

ble_bas_t bas

Structure used to identify the battery service.

ble_hrs_t m_hrs
static

Structure used to identify the heart rate service.

volatile uint16_t m_cur_heart_rate
static

Current heart rate value.

app_timer_id_t m_battery_timer_id
static

Battery timer.

app_timer_id_t m_heart_rate_timer_id
static

Heart rate measurement timer.