-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
executable file
·506 lines (419 loc) · 17.5 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/**
* Copyright (c) 2014 - 2019, Nordic Semiconductor ASA
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into a Nordic
* Semiconductor ASA integrated circuit in a product or a software update for
* such product, must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. Neither the name of Nordic Semiconductor ASA nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* Nordic Semiconductor ASA integrated circuit.
*
* 5. Any software provided in binary form under this license must not be reverse
* engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/** @file
*
* @defgroup ble_sdk_app_beacon_main main.c
* @{
* @ingroup ble_sdk_app_beacon
* @brief Beacon Transmitter Sample Application main file.
*
* This file contains the source code for an Beacon transmitter sample application.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "sdk_config.h"
#include "nordic_common.h"
#include "nrf.h"
#include "ble.h"
#include "ble_advdata.h"
#include "ble_advertising.h"
#include "ble_conn_params.h"
#include "ble_srv_common.h"
#include "ble_hci.h"
#include "bsp.h"
#include "nrf_soc.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "ble_advdata.h"
#include "app_timer.h"
#include "nrf_pwr_mgmt.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_delay.h"
#include "nrf_ble_scan.h"
#include "nrf_fstorage.h"
#include "nrf_sdh_soc.h"
// GAP INIT #DEFINES
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(100, UNIT_1_25_MS) /**< Minimum acceptable connection interval (0.1 seconds). */
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(200, UNIT_1_25_MS) /**< Maximum acceptable connection interval (0.2 second). */
#define SLAVE_LATENCY 0 /**< Slave latency. */
#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */
#define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
#define APP_BLE_OBSERVER_PRIO 3 /**< BLE observer priority of the application. There is no need to modify this value. */
#define APP_SOC_OBSERVER_PRIO 1 /**< SoC observer priority of the application. There is no need to modify this value. */
#define NON_CONNECTABLE_ADV_INTERVAL MSEC_TO_UNITS(20, UNIT_0_625_MS) /**< The advertising interval for non-connectable advertisement (100 ms). This value can vary between 100ms to 10.24s). */
#define TX_POWER 4 /** (accepted values are -40, -20, -16, -12, -8, -4, 0, and 4 dBm) */
#define SCAN_INTERVAL 0x0640 /**< Determines scan interval in units of 0.625 millisecond. */
#define SCAN_WINDOW 0x0020 /**< Determines scan window in units of 0.625 millisecond. */
#define SCAN_DURATION 0x0000
static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
static uint8_t m_adv_handle = BLE_GAP_ADV_SET_HANDLE_NOT_SET; /**< Advertising handle used to identify an advertising set. */
static uint8_t m_enc_advdata[BLE_GAP_ADV_SET_DATA_SIZE_MAX]; /**< Buffer for storing an encoded advertising set. */
static bool m_memory_access_in_progress; /**< Flag to keep track of ongoing operations on persistent memory. */
static uint8_t advertisingChannel = 37;
APP_TIMER_DEF(advertisingUpdateTimer);
NRF_BLE_SCAN_DEF(m_scan); /**< Scanning Module instance. */
static ble_gap_adv_data_t m_adv_data =
{
.adv_data = {
.p_data = m_enc_advdata,
.len = BLE_GAP_ADV_SET_DATA_SIZE_MAX},
.scan_rsp_data = {
.p_data = NULL,
.len = 0 }
};
static ble_gap_scan_params_t m_scan_param = /**< Scan parameters requested for scanning and connection. */
{
.active = 0x0,
.interval = SCAN_INTERVAL,
.window = SCAN_WINDOW,
.filter_policy = BLE_GAP_SCAN_FP_ACCEPT_ALL,
.timeout = SCAN_DURATION,
.scan_phys = BLE_GAP_PHY_1MBPS,
.extended = 0,
.channel_mask = {0x0, 0x0, 0x0, 0x0, 0xC0},
};
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
{
app_error_handler(0xDEADBEEF, line_num, p_file_name);
}
void gap_params_init(void)
{
ret_code_t err_code;
ble_gap_conn_params_t gap_conn_params;
ble_gap_conn_sec_mode_t sec_mode;
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
memset(&gap_conn_params, 0, sizeof(gap_conn_params));
gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
gap_conn_params.slave_latency = SLAVE_LATENCY;
gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
APP_ERROR_CHECK(err_code);
}
void advertising_init(void)
{
// Initialize advertising parameters (used when starting advertising).
memset(&m_adv_params, 0, sizeof(m_adv_params));
m_adv_params.properties.type = BLE_GAP_ADV_TYPE_NONCONNECTABLE_NONSCANNABLE_UNDIRECTED;
m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
m_adv_params.filter_policy = BLE_GAP_ADV_FP_ANY;
m_adv_params.interval = NON_CONNECTABLE_ADV_INTERVAL;
m_adv_params.duration = BLE_GAP_ADV_TIMEOUT_GENERAL_UNLIMITED;
m_adv_params.primary_phy = BLE_GAP_PHY_AUTO;
m_adv_params.secondary_phy = BLE_GAP_PHY_AUTO;
m_adv_params.channel_mask[4] = 0xC0; // advertise on 37 only
// m_adv_params.channel_mask[4] = 0xA0; // advertise on 38 only
// m_adv_params.channel_mask[4] = 0x60; // advertise on 39 only
// Data
m_adv_data.adv_data.p_data[0] = 0x1E; // length
for (int i = 1; i < m_adv_data.adv_data.len; i++) { m_adv_data.adv_data.p_data[i] = 0x0; }
// start advertising with m_adv_data and m_adv_params
sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
}
void advertising_start(void)
{
ret_code_t err_code;
// set power level
err_code = sd_ble_gap_tx_power_set(BLE_GAP_TX_POWER_ROLE_ADV, m_adv_handle, TX_POWER);
err_code = sd_ble_gap_adv_start(m_adv_handle, APP_BLE_CONN_CFG_TAG);
err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
APP_ERROR_CHECK(err_code);
}
static uint8_t expectedBytes[31] = {
0x1E,0xCE,0xEE,0xB7,0xA9,0x77,0xF8,0xE3,
0xB6,0x16,0x54,0x2F,0x9D,0x53,0x33,0xD8,
0xBA,0x98,0x08,0x24,0xCB,0x3B,0xFC,0x71,
0xA3,0xF4,0x55,0x94,0x30,0x56,0xE6
};
static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_ADV_REPORT: {
ble_gap_evt_t gapEvent = p_ble_evt->evt.gap_evt;
ble_gap_evt_adv_report_t advReport = gapEvent.params.adv_report;
if ((advReport.ch_index == 37) && (advReport.data.len == 31)) {
// NRF_LOG_RAW_INFO("channelIndex %d\r\n", advReport.ch_index);
// for (int i = 0; i < advReport.data.len; i++) {
// NRF_LOG_RAW_INFO("%02X ", advReport.data.p_data[i]);
// if (((i+1) % 8) == 0) { NRF_LOG_RAW_INFO("\r\n"); }
// } NRF_LOG_RAW_INFO("\r\n");
uint8_t mismatchedBytes = 0;
for (uint8_t i = 0; i < advReport.data.len; i++) {
if (advReport.data.p_data[i] != expectedBytes[i]) {
mismatchedBytes++;
}
}
if (!mismatchedBytes) {
nrf_gpio_pin_clear(29); // disconnect cap
nrf_delay_us(1000);
nrf_gpio_pin_set(28); // connect release
nrf_ble_scan_stop();
app_timer_start(advertisingUpdateTimer, APP_TIMER_TICKS(5000), NULL);
}
// NRF_LOG_RAW_INFO ("------------------------\r\n");
}
break;
}
default: {
NRF_LOG_RAW_INFO ("Unsupported header event: %d\r\n", p_ble_evt->header.evt_id);
break;
}
}
}
static void scan_start(void)
{
ret_code_t err_code;
err_code = nrf_ble_scan_start(&m_scan);
APP_ERROR_CHECK(err_code);
}
static void scan_evt_handler(scan_evt_t const * p_scan_evt)
{
switch(p_scan_evt->scan_evt_id) {
case NRF_BLE_SCAN_EVT_SCAN_TIMEOUT: {
NRF_LOG_INFO("Scan timed out.");
scan_start();
} break;
default: {
// NRF_LOG_RAW_INFO ("Unsupported scan event: %d\r\n", p_scan_evt->scan_evt_id);
break;
}
}
}
static void scan_init(void)
{
ret_code_t err_code;
nrf_ble_scan_init_t init_scan;
memset(&init_scan, 0, sizeof(init_scan));
init_scan.connect_if_match = false;
init_scan.conn_cfg_tag = APP_BLE_CONN_CFG_TAG;
init_scan.p_scan_param = &m_scan_param;
err_code = nrf_ble_scan_init(&m_scan, &init_scan, scan_evt_handler);
APP_ERROR_CHECK(err_code);
}
static void soc_evt_handler(uint32_t evt_id, void * p_context)
{
switch (evt_id) {
case NRF_EVT_FLASH_OPERATION_SUCCESS:
case NRF_EVT_FLASH_OPERATION_ERROR:
if (m_memory_access_in_progress) {
m_memory_access_in_progress = false;
scan_start();
}
break;
default:
break;
}
}
void ble_stack_init(void)
{
ret_code_t err_code;
err_code = nrf_sdh_enable_request();
APP_ERROR_CHECK(err_code);
// Configure the BLE stack using the default settings.
// Fetch the start address of the application RAM.
uint32_t ram_start = 0;
err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
APP_ERROR_CHECK(err_code);
// Enable BLE stack.
err_code = nrf_sdh_ble_enable(&ram_start);
APP_ERROR_CHECK(err_code);
// Register event handling
NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
NRF_SDH_SOC_OBSERVER(m_soc_observer, APP_SOC_OBSERVER_PRIO, soc_evt_handler, NULL);
}
void log_init(void)
{
ret_code_t err_code = NRF_LOG_INIT(NULL);
APP_ERROR_CHECK(err_code);
NRF_LOG_DEFAULT_BACKENDS_INIT();
}
static void timers_init(void)
{
ret_code_t err_code = app_timer_init();
APP_ERROR_CHECK(err_code);
}
static void power_management_init(void)
{
ret_code_t err_code;
err_code = nrf_pwr_mgmt_init();
APP_ERROR_CHECK(err_code);
}
static void idle_state_handle(void)
{
if (NRF_LOG_PROCESS() == false)
{
nrf_pwr_mgmt_run();
}
}
uint8_t reverseByte(uint8_t byte)
{
uint8_t reversedBits = 0;
for (uint8_t i = 0; i < 4; i++) {
reversedBits |= (byte >> (7-(2*i))) & (0x01 << i);
reversedBits |= (byte << (7-(2*i))) & (0x80 >> i);
}
return reversedBits;
}
static uint8_t whiten(uint8_t byte, bool reset)
{
static uint8_t shiftRegister = 37 | (1 << 6);
uint8_t whitenedByte = 0;
uint8_t feedbackBit = 0;
if (reset) { shiftRegister = advertisingChannel | (1 << 6); }
for (int i = 0; i < 8; i++) {
whitenedByte |= ((byte & 0x1) ^ (shiftRegister & 0x1)) << i;
byte = byte >> 1;
feedbackBit = shiftRegister & 0x1;
shiftRegister = shiftRegister >> 1; // rotate right
// clear 0th bit and set it to feedback bit
shiftRegister = (shiftRegister & ~(1 << 6)) | (feedbackBit << 6);
// clear 4th bit and xor with feedback bit
shiftRegister = (shiftRegister & ~(1 << 2)) | ((((shiftRegister >> 2) & 0x1) ^ feedbackBit) << 2);
}
return whitenedByte;
}
void testWhitener()
{
// test whitener
whiten(0, true); // reset shift register
for (int i = 0; i < 8; i++) { whiten(0, false); } // rotate shift register until at first data bit
for (int i = 1; i < m_adv_data.adv_data.len; i++) {
// NRF_LOG_INFO("%02X ", whiten(0xFF, false)); // all ones
}
}
static uint8_t stretchedData[31] = {0};
// This function stretches data and loads it into m_adv_data.adv_data.p_data
static void queueStretchedData(uint8_t* data, uint8_t dataLength, uint8_t stretch)
{
// if ((dataLength * stretch) > 30) { NRF_LOG_INFO("Error, can't fit stretchedData into 30 bytes"); }
uint8_t bitsInserted = 8;
uint8_t bit = 0;
for (int i = 0; i < dataLength; i++) {
for (int b = 0; b < 8; b++) {
bit = (data[i] & (0x80 >> b)) >> (7 - b);
for (int j = 0; j < stretch; j++) {
stretchedData[bitsInserted/8] |= bit << (7-(bitsInserted%8));
bitsInserted++;
}
}
}
}
#define FREQUENCY_DIVIDER 30
#define START_WAIT_TIME_MS 19 // 500uA average current
// #define STOP_WAIT_TIME_MS 1
#define STOP_WAIT_TIME_MS 275
static void advertisingUpdateTimerHandler(void * p_context)
{
static bool restart = true;
if (restart) {
// rotate shift register until at first data bit
// length:2 address:6 payloadLength:1
advertisingChannel = 37 + (((advertisingChannel+1) % 37) % 3);
if (advertisingChannel == 37) { m_adv_params.channel_mask[4] = 0xC0; }
else if (advertisingChannel == 38) { m_adv_params.channel_mask[4] = 0xA0; }
else if (advertisingChannel == 39) { m_adv_params.channel_mask[4] = 0x60; }
whiten(0, true); // reset shift register
for (int i = 0; i < 8; i++) { whiten(0, false); }
// payload
m_adv_data.adv_data.p_data[0] = 0x1E; // length
// load desired data at (1Mb / frequencyDivider) into data
uint8_t data[30] = {0};
uint8_t dataLength = 30 / FREQUENCY_DIVIDER;
for (int i = 0; i < dataLength; i++) { data[i] = i+0xA1; }
queueStretchedData(data, dataLength, 30/dataLength);
// copy stretchedData into actual advertising packet
for (int i = 1; i < m_adv_data.adv_data.len; i++) {
m_adv_data.adv_data.p_data[i] = whiten(reverseByte(stretchedData[i]), false); // all ones
}
// start advertising with m_adv_data and m_adv_params
sd_ble_gap_adv_set_configure(&m_adv_handle, &m_adv_data, &m_adv_params);
advertising_start();
} else {
sd_ble_gap_adv_stop(m_adv_handle);
}
app_timer_start(advertisingUpdateTimer, APP_TIMER_TICKS(restart ? START_WAIT_TIME_MS : STOP_WAIT_TIME_MS), NULL);
restart = !restart;
}
int main(void)
{
log_init();
timers_init();
power_management_init();
ble_stack_init();
gap_params_init();
advertising_init();
sd_power_dcdc_mode_set(true);
// Timer
ret_code_t err_code;
err_code = app_timer_create(&advertisingUpdateTimer,
APP_TIMER_MODE_SINGLE_SHOT,
advertisingUpdateTimerHandler);
APP_ERROR_CHECK(err_code);
// Scan initialization
scan_init();
scan_start();
// Set up gpio
// Don't set release gpio
nrf_gpio_cfg_output(28);
nrf_gpio_pin_clear(28);
// Charge cap
nrf_gpio_cfg_output(29);
nrf_gpio_pin_set(29);
for (;;) {
idle_state_handle();
}
}
// Scratchpad
// Start execution.
// NRF_LOG_INFO("MiniBee started");
// NRF_LOG_INFO("Last modified 12.17.2019");
// NRF_LOG_INFO("Expected:");
// uint8_t data[30] = {0};
// uint8_t dataLength = 30 / FREQUENCY_DIVIDER;
// for (int i = 0; i < dataLength; i++) { data[i] = i+0xA1; }
// queueStretchedData(data, dataLength, 30/dataLength);
// for (int i = 1; i < 31; i++) { NRF_LOG_INFO("%02X", reverseByte(stretchedData[i])); }
// Use this one to print out and generate packets in MATLAB
// for (int i = 1; i < 31; i++) {
// NRF_LOG_INFO("%02X", (stretchedData[i]));
// }