Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
main.c
Go to the documentation of this file.
1 /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
2  *
3  * The information contained herein is property of Nordic Semiconductor ASA.
4  * Terms and conditions of usage are described in detail in NORDIC
5  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6  *
7  * Licensees are granted free, non-transferable use of the information. NO
8  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9  * the file.
10  *
11  */
12 
16 #include <stdint.h>
17 #include <string.h>
18 
19 #include "boards.h"
20 #include "app_util_platform.h"
21 #include "app_print.h"
22 #include "debug.h"
23 #include "ptu.h"
24 #include "app_timer.h"
25 #include "softdevice_handler.h"
26 #include "advertiser_beacon.h"
27 #include "ptu_config.h"
28 #include "nrf_nvic.h"
29 
30 #ifdef DFU_SUPPORT
31  #include "common_hal_buttons.h"
32 #endif
33 
34 #define CENTRAL_LINK_COUNT 7
35 #define PERIPHERAL_LINK_COUNT 1
37 // Assert setup
38 #define DEAD_BEEF 0xDEADBEEF
39 #ifdef BOARD_PCA10028
40  #define NO_DEV_CONNECTED_LED_PATTERN 0x03
41  #define POWER_TRANSFER_LED_PATTERN 0x02
42  #define FAULT_LED_PATTERN 0x01
43 #else
44  #define NO_DEV_CONNECTED_LED_PATTERN 0x00
45  #define POWER_TRANSFER_LED_PATTERN 0x01
46  #define FAULT_LED_PATTERN 0x02
47 #endif
48 
49 #define BEACON_UUID 0xff, 0xfe, 0x2d, 0x12, 0x1e, 0x4b, 0x0f, 0xa4,\
50  0x99, 0x4e, 0xce, 0xb5, 0x31, 0xf4, 0x05, 0x45
51 #define BEACON_ADV_INTERVAL 400
52 #define BEACON_MAJOR 0x1234
53 #define BEACON_MINOR 0x5678
54 #define BEACON_RSSI 0xC3
56 #define APP_COMPANY_IDENTIFIER 0x0059
58 static ble_beacon_init_t beacon_init;
62 static void beacon_adv_init(void)
63 {
64  static uint8_t beacon_uuid[] = {BEACON_UUID};
65 
66  memcpy(beacon_init.uuid.uuid128, beacon_uuid, sizeof(beacon_uuid));
72 
73  uint32_t err_code = sd_ble_gap_address_get(&beacon_init.beacon_addr);
74  APP_ERROR_CHECK(err_code);
75 
77 }
78 
79 
83 static void m_sys_evt_dispatch(uint32_t sys_evt)
84 {
85 
86 }
87 
91 static void m_ble_evt_dispatch(ble_evt_t * p_evt)
92 {
93  ptu_on_ble_evt(p_evt);
94 }
95 
98 static void m_ble_stack_init(void)
99 {
100  uint32_t err_code;
101  ble_conn_bw_counts_t count = {{0,0,8},{0,0,8}};
102 
103  // Initialize the SoftDevice handler module.
104  nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
105  SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
106 
107  // Enable BLE stack.
108  ble_enable_params_t ble_enable_params = {0};
109  ble_enable_params.gap_enable_params.central_conn_count = CENTRAL_LINK_COUNT;
110  ble_enable_params.gap_enable_params.central_sec_count = CENTRAL_LINK_COUNT;
111  ble_enable_params.gap_enable_params.periph_conn_count = PERIPHERAL_LINK_COUNT;
112 
113  ble_enable_params.gatts_enable_params.attr_tab_size = BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
114  ble_enable_params.common_enable_params.p_conn_bw_counts = &count;
115 
116  ble_opt_t opt = {0};
117  opt.common_opt.conn_bw.conn_bw.conn_bw_rx = BLE_CONN_BW_LOW;
118  opt.common_opt.conn_bw.conn_bw.conn_bw_tx = BLE_CONN_BW_LOW;
119  opt.common_opt.conn_bw.role = BLE_GAP_ROLE_CENTRAL;
120 
121  //Check the ram settings against the used number of links
122  CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT, PERIPHERAL_LINK_COUNT);
123 
124  err_code = softdevice_enable(&ble_enable_params);
125  APP_ERROR_CHECK(err_code);
126 
127  err_code = sd_ble_opt_set(BLE_COMMON_OPT_CONN_BW, &opt);
128  APP_ERROR_CHECK(err_code);
129 
130  err_code = softdevice_ble_evt_handler_set(m_ble_evt_dispatch);
131  APP_ERROR_CHECK(err_code);
132 
133  err_code = softdevice_sys_evt_handler_set(m_sys_evt_dispatch);
134  APP_ERROR_CHECK(err_code);
135 
136  err_code = sd_ble_gap_tx_power_set(PTU_OUTPUT_TX_POWER);
137  APP_ERROR_CHECK(err_code);
138 
139  // It is important that this is set to the same priority as the
140  // timers in the system.
141  err_code = sd_nvic_SetPriority(SD_EVT_IRQn, APP_IRQ_PRIORITY_LOW);
142  APP_ERROR_CHECK(err_code);
143 }
144 
147 int main()
148 {
149  #ifndef INIT_APP_TIMERS_EXTERNALLY
150  APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
151  #endif
152 
153 #ifdef DFU_SUPPORT
154  // Set up button used for entering DFU mode
156 #endif
157 
159 
160  beacon_adv_init();
161 
162  // Initialize PTU state machine
163  ptu_init(NULL);
164 
165  // Start PTU
166  ptu_start();
167 
168  // Start beacons
170 
171  while(1)
172  {
173  #ifdef DEBUG_OUT_ENABLE
174  debug();
175  #endif
176  }
177 }
178 
179 //lint -restore
uint16_t major
Major identifier to use for 'beacon'.
static void m_sys_evt_dispatch(uint32_t sys_evt)
Dispatch system event, triggered by softdevice.
Definition: main.c:83
static void m_ble_evt_dispatch(ble_evt_t *p_evt)
Dispatch BLE event, triggered by softdevice.
Definition: main.c:91
#define BEACON_RSSI
Definition: main.c:54
void app_beacon_init(ble_beacon_init_t *p_init)
Function for initializing the advertiser module.
int main()
Application main function.
Definition: main.c:90
#define PTU_BUTTON_PRESS_LENGTH_DFU_MS
Definition: ptu_config.h:227
#define BEACON_MINOR
Definition: main.c:53
void common_hal_buttons_init(uint32_t button_press_duration_ms, uint8_t app_timer_prescaler)
Initialize button functionality. Used only for starting DFU.
uint16_t manuf_id
Manufacturer ID.
ble_gap_addr_t beacon_addr
BLEe address to be used by the beacon.
#define APP_TIMER_OP_QUEUE_SIZE
Definition: pru.h:34
static ble_beacon_init_t beacon_init
Definition: main.c:58
#define BEACON_ADV_INTERVAL
Definition: main.c:51
static void beacon_adv_init(void)
Function for initializing Beacon advertiser.
Definition: main.c:62
#define BEACON_MAJOR
Definition: main.c:52
void ptu_start(void)
Start ptu profile. Here "start" means that profile can start generating events through ptu_evt_handle...
Definition: ptu.c:364
uint8_t rssi
Measured RSSI at 1 meter distance in dBm.
Advertiser module. This module shows an example of using periodic timeslots on the radio when the Sof...
#define PTU_OUTPUT_TX_POWER
Definition: ptu_config.h:31
uint32_t adv_interval
Advertising interval in milliseconds to be used for 'beacon' advertisements.
void ptu_on_ble_evt(ble_evt_t *p_ble_evt)
Dispatches a BLE stack event to PTU.
Definition: ptu.c:319
#define APP_TIMER_PRESCALER
Definition: pru.h:33
void app_beacon_start(void)
Function for starting the advertisement.
#define CENTRAL_LINK_COUNT
Definition: main.c:34
#define PERIPHERAL_LINK_COUNT
Definition: main.c:35
#define BEACON_UUID
Definition: main.c:49
ble_uuid128_t uuid
128 bit proprietary service UUID to include in advertisement packets
static void m_ble_stack_init(void)
Initialize the BLE sofdevice and IRQ handler.
Definition: main.c:98
uint16_t minor
Minor identifier to use for 'beacon'.
#define APP_COMPANY_IDENTIFIER
Definition: main.c:56
void debug(void)
Process debug commands.
Definition: pru_debug.c:564
void ptu_init(app_sm_evt_handler_t sm_evt_handler)
Initialize PTU. This function must be called before any other PTU function can be called...
Definition: ptu.c:331