Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
447 lines (357 loc) · 14.1 KB
/
Copy pathmain.c
File metadata and controls
447 lines (357 loc) · 14.1 KB
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
/**
* Copyright (c) 2017 - 2020, 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.
*
*/
#include <stdint.h>
#include "app_scheduler.h"
#include "app_timer.h"
#include "bsp_thread.h"
#include "nrf_log_ctrl.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_drv_saadc.h"
#include "nrf_temp.h"
#include "nrf_delay.h"
#include "nrf_drv_twi.h"
#include "settings.h"
#include "bosch_bme280_driver/bme280.h"
#include "thread_coap_utils.h"
#include "thread_utils.h"
#include <openthread/thread.h>
#define ADC_CHANNELS 2
#define SCHED_QUEUE_SIZE 32
#define SCHED_EVENT_DATA_SIZE APP_TIMER_SCHED_EVENT_DATA_SIZE
static const nrf_drv_twi_t m_twi_master = NRF_DRV_TWI_INSTANCE(0);
uint32_t bme280_measurement_delay = 0;
struct bme280_dev bme280_sensor;
uint8_t bme280_sensor_addr = BME280_I2C_ADDR_PRIM;
APP_TIMER_DEF(m_voltage_timer_id);
APP_TIMER_DEF(m_internal_temperature_timer_id);
APP_TIMER_DEF(m_bme280_timer_id);
APP_TIMER_DEF(m_bme280_measurement_delay_timer_id);
sensor_subscription sensor_subscriptions[] = {
{ .sensor_name = 'P', .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
{ .sensor_name = 'T', .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
{ .sensor_name = 'H', .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
{ .sensor_name = 'v', .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
{ .sensor_name = 'V', .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
{ .sensor_name = 't', .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
{ .sensor_name = SENSOR_SUBSCRIPTION_NAME_LAST, .sent_value = 0, .current_value = 0, .reportable_change = 0, .disable_reporting = true, .read_only = true, .initialized = false, .report_interval = 10000, .last_sent_at = 0, .set_value_handler = NULL, },
};
static nrf_saadc_value_t adc_buf[ADC_CHANNELS * ADC_SAMPLES_PER_CHANNEL];
int32_t internal_temp_prev = 0x7FFFFFFF;
int32_t dc_voltage_12_prev = 0x7FFFFFFF;
int32_t dc_voltage_12 = 0;
int32_t dc_voltage_3v3_prev = 0x7FFFFFFF;
int32_t dc_voltage_3v3 = 0;
void update_voltage_attributes_callback(void *p_event_data, uint16_t event_size)
{
set_sensor_value('v', dc_voltage_3v3, false);
set_sensor_value('V', dc_voltage_12, false);
}
void saadc_event_handler(nrf_drv_saadc_evt_t const *p_event)
{
if (p_event->type == NRF_DRV_SAADC_EVT_DONE)
{
uint32_t err_code;
int32_t sums[ADC_CHANNELS];
for (int i = 0; i < ADC_CHANNELS; i++) {
sums[i] = 0;
for (int j = 0; j < ADC_SAMPLES_PER_CHANNEL; j++) {
sums[i] += p_event->data.done.p_buffer[j * 2 + i];
}
sums[i] = sums[i] / ADC_SAMPLES_PER_CHANNEL;
}
if (dc_voltage_12_prev == 0x7FFFFFFF) {
dc_voltage_12_prev = sums[1];
dc_voltage_12 = sums[1];
} else {
dc_voltage_12 = (dc_voltage_12_prev + dc_voltage_12_prev + dc_voltage_12_prev + sums[1]) >> 2;
dc_voltage_12_prev = dc_voltage_12;
}
if (sums[0] < 0)
sums[0] = 0;
if (dc_voltage_3v3_prev == 0x7FFFFFFF) {
dc_voltage_3v3_prev = sums[0];
dc_voltage_3v3 = sums[0];
} else {
dc_voltage_3v3 = (dc_voltage_3v3_prev + dc_voltage_3v3_prev + dc_voltage_3v3_prev + sums[0]) >> 2;
dc_voltage_3v3_prev = dc_voltage_3v3;
}
err_code = nrf_drv_saadc_buffer_convert(p_event->data.done.p_buffer, ADC_CHANNELS * ADC_SAMPLES_PER_CHANNEL);
APP_ERROR_CHECK(err_code);
app_sched_event_put(NULL, 0, update_voltage_attributes_callback);
}
else
{
NRF_LOG_INFO("saadc unhandled event: %d", p_event->type);
}
}
static void adc_configure(void)
{
ret_code_t err_code = nrf_drv_saadc_init(NULL, saadc_event_handler);
APP_ERROR_CHECK(err_code);
err_code = nrfx_saadc_calibrate_offset();
APP_ERROR_CHECK(err_code);
while (nrfx_saadc_is_busy());
nrf_saadc_channel_config_t config0 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_VDD);
config0.acq_time = NRF_SAADC_ACQTIME_40US;
err_code = nrf_drv_saadc_channel_init(0, &config0);
APP_ERROR_CHECK(err_code);
nrf_saadc_channel_config_t config1 = NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN4);
config1.acq_time = NRF_SAADC_ACQTIME_40US;
err_code = nrf_drv_saadc_channel_init(1, &config1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_saadc_buffer_convert(adc_buf, ADC_CHANNELS * ADC_SAMPLES_PER_CHANNEL);
APP_ERROR_CHECK(err_code);
}
static void voltage_timeout_handler(void *p_context)
{
UNUSED_PARAMETER(p_context);
ret_code_t err_code = nrf_drv_saadc_sample();
APP_ERROR_CHECK(err_code);
}
static void internal_temperature_timeout_handler(void *p_context)
{
UNUSED_PARAMETER(p_context);
NRF_TEMP->TASKS_START = 1;
/* Busy wait while temperature measurement is not finished. */
while (NRF_TEMP->EVENTS_DATARDY == 0) {
}
NRF_TEMP->EVENTS_DATARDY = 0;
int32_t temp = nrf_temp_read();
NRF_TEMP->TASKS_STOP = 1;
if (internal_temp_prev == 0x7FFFFFFF) {
internal_temp_prev = temp;
} else {
temp = (internal_temp_prev + internal_temp_prev + internal_temp_prev + temp) >> 2;
internal_temp_prev = temp;
set_sensor_value('t', temp, false);
}
}
static void bme280_measurement_delay_timeout_handler(void *p_context)
{
UNUSED_PARAMETER(p_context);
struct bme280_data comp_data;
int8_t rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &bme280_sensor);
if (rslt == BME280_OK) {
set_sensor_value('P', comp_data.pressure, false);
set_sensor_value('T', comp_data.temperature, false);
set_sensor_value('H', comp_data.humidity, false);
}
}
static void bme280_timeout_handler(void *p_context)
{
UNUSED_PARAMETER(p_context);
int8_t rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &bme280_sensor);
if (rslt != BME280_OK)
return;
ret_code_t err_code = app_timer_start(m_bme280_measurement_delay_timer_id, APP_TIMER_TICKS(bme280_measurement_delay + 10), NULL);
APP_ERROR_CHECK(err_code);
}
static void bsp_event_handler(bsp_event_t event)
{
switch(event) {
case BSP_EVENT_KEY_0:
break;
default:
return;
}
}
static void thread_state_changed_callback(uint32_t flags, void * p_context)
{
if (flags & OT_CHANGED_THREAD_ROLE) {
otDeviceRole device_role = otThreadGetDeviceRole(p_context);
const char *szRole = "UNKNOWN ROLE";
switch (device_role) {
case OT_DEVICE_ROLE_CHILD:
szRole = "OT_DEVICE_ROLE_CHILD";
bsp_board_led_on(LED_CHILD_ROLE);
bsp_board_led_off(LED_ROUTER_ROLE);
break;
case OT_DEVICE_ROLE_ROUTER:
szRole = "OT_DEVICE_ROLE_ROUTER";
bsp_board_led_on(LED_ROUTER_ROLE);
bsp_board_led_off(LED_CHILD_ROLE);
break;
case OT_DEVICE_ROLE_LEADER:
szRole = "OT_DEVICE_ROLE_LEADER";
bsp_board_led_on(LED_CHILD_ROLE);
bsp_board_led_on(LED_ROUTER_ROLE);
break;
case OT_DEVICE_ROLE_DISABLED:
szRole = "OT_DEVICE_ROLE_DISABLED";
bsp_board_led_off(LED_ROUTER_ROLE);
bsp_board_led_off(LED_CHILD_ROLE);
break;
case OT_DEVICE_ROLE_DETACHED:
szRole = "OT_DEVICE_ROLE_DETACHED";
bsp_board_led_off(LED_ROUTER_ROLE);
bsp_board_led_off(LED_CHILD_ROLE);
break;
default:
break;
}
NRF_LOG_INFO("State changed! Flags: 0x%08x Current role: %s\r\n", flags, szRole);
}
}
static 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 timer_init(void)
{
uint32_t error_code = app_timer_init();
APP_ERROR_CHECK(error_code);
// Voltage timer
error_code = app_timer_create(&m_voltage_timer_id, APP_TIMER_MODE_REPEATED, voltage_timeout_handler);
APP_ERROR_CHECK(error_code);
// Internal temperature timer
error_code = app_timer_create(&m_internal_temperature_timer_id, APP_TIMER_MODE_REPEATED, internal_temperature_timeout_handler);
APP_ERROR_CHECK(error_code);
// bme280 timer
error_code = app_timer_create(&m_bme280_timer_id, APP_TIMER_MODE_REPEATED, bme280_timeout_handler);
APP_ERROR_CHECK(error_code);
// bme280 measurement delay timer
error_code = app_timer_create(&m_bme280_measurement_delay_timer_id, APP_TIMER_MODE_SINGLE_SHOT, bme280_measurement_delay_timeout_handler);
APP_ERROR_CHECK(error_code);
}
static void thread_instance_init(void)
{
thread_configuration_t thread_configuration =
{
.radio_mode = THREAD_RADIO_MODE_RX_ON_WHEN_IDLE,
.autocommissioning = true,
.default_child_timeout = 10,
.wipe_settings = false,
};
thread_init(&thread_configuration);
thread_state_changed_callback_set(thread_state_changed_callback);
}
void user_delay_us(uint32_t period, void *intf_ptr)
{
nrf_delay_us(period);
}
int8_t user_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
ret_code_t err_code = nrf_drv_twi_tx(&m_twi_master, *(uint8_t *)intf_ptr, ®_addr, 1, false);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_twi_rx(&m_twi_master, *(uint8_t *)intf_ptr, reg_data, len);
APP_ERROR_CHECK(err_code);
return err_code;
}
int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
{
uint8_t write_data[256];
if (len > sizeof(write_data) - 1)
return 1;
write_data[0] = reg_addr;
memcpy(&write_data[1], reg_data, len);
ret_code_t err_code = nrf_drv_twi_tx(&m_twi_master, *(uint8_t *)intf_ptr, write_data, len + 1, false);
APP_ERROR_CHECK(err_code);
return 0;
}
static bool init_bme280()
{
bme280_sensor.intf_ptr = &bme280_sensor_addr;
bme280_sensor.intf = BME280_I2C_INTF;
bme280_sensor.read = user_i2c_read;
bme280_sensor.write = user_i2c_write;
bme280_sensor.delay_us = user_delay_us;
int8_t rslt = bme280_init(&bme280_sensor);
if (rslt != BME280_OK)
return false;
bme280_sensor.settings.osr_h = BME280_OVERSAMPLING_16X;
bme280_sensor.settings.osr_p = BME280_OVERSAMPLING_16X;
bme280_sensor.settings.osr_t = BME280_OVERSAMPLING_16X;
bme280_sensor.settings.filter = BME280_FILTER_COEFF_16;
uint8_t settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
rslt = bme280_set_sensor_settings(settings_sel, &bme280_sensor);
if (rslt != BME280_OK)
return false;
bme280_measurement_delay = bme280_cal_meas_delay(&bme280_sensor.settings);
return rslt == BME280_OK;
}
static bool twi_init()
{
ret_code_t ret;
const nrf_drv_twi_config_t config =
{
.scl = TWI_SCL_M,
.sda = TWI_SDA_M,
.frequency = TWI_DEFAULT_CONFIG_FREQUENCY,
.interrupt_priority = TWI_DEFAULT_CONFIG_IRQ_PRIORITY,
.clear_bus_init = TWI_DEFAULT_CONFIG_CLR_BUS_INIT
};
ret = nrf_drv_twi_init(&m_twi_master, &config, NULL, NULL);
if (NRF_SUCCESS == ret) {
nrf_drv_twi_enable(&m_twi_master);
}
return ret;
}
int main(int argc, char * argv[])
{
log_init();
APP_SCHED_INIT(SCHED_EVENT_DATA_SIZE, SCHED_QUEUE_SIZE);
timer_init();
adc_configure();
nrf_temp_init();
twi_init();
init_bme280();
uint32_t error_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
APP_ERROR_CHECK(error_code);
thread_instance_init();
otPlatRadioSetTransmitPower(thread_ot_instance_get(), 8);
thread_coap_utils_init();
ret_code_t err_code = app_timer_start(m_voltage_timer_id, APP_TIMER_TICKS(VOLTAGE_TIMER_INTERVAL / ADC_SAMPLES_PER_CHANNEL), NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_internal_temperature_timer_id, APP_TIMER_TICKS(INTERNAL_TEMPERATURE_TIMER_INTERVAL), NULL);
APP_ERROR_CHECK(err_code);
err_code = app_timer_start(m_bme280_timer_id, APP_TIMER_TICKS(BME280_TIMER_INTERVAL), NULL);
APP_ERROR_CHECK(err_code);
while (true) {
thread_process();
app_sched_execute();
if (NRF_LOG_PROCESS() == false) {
thread_sleep();
}
}
}