-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
431 lines (369 loc) · 14.6 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
/******************************************************************************
* File Name: main.c
*
* Description: This is the source code for the RDK3 BLE SensorHub
* Application for ModusToolbox.
*
* Related Document: See README.md
*
*
* Created on: 2022-12-21
* Company: Rutronik Elektronische Bauelemente GmbH
* Address: Jonavos g. 30, Kaunas 44262, Lithuania
* Author: GDR
*
*******************************************************************************
* (c) 2019-2021, Cypress Semiconductor Corporation. All rights reserved.
*******************************************************************************
* This software, including source code, documentation and related materials
* ("Software"), is owned by Cypress Semiconductor Corporation or one of its
* subsidiaries ("Cypress") and is protected by and subject to worldwide patent
* protection (United States and foreign), United States copyright laws and
* international treaty provisions. Therefore, you may use this Software only
* as provided in the license agreement accompanying the software package from
* which you obtained this Software ("EULA").
*
* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
* non-transferable license to copy, modify, and compile the Software source
* code solely for use in connection with Cypress's integrated circuit products.
* Any reproduction, modification, translation, compilation, or representation
* of this Software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
* reserves the right to make changes to the Software without notice. Cypress
* does not assume any liability arising out of the application or use of the
* Software or any product or circuit described in the Software. Cypress does
* not authorize its products for use in any products where a malfunction or
* failure of the Cypress product may reasonably be expected to result in
* significant property damage, injury or death ("High Risk Product"). By
* including Cypress's product in a High Risk Product, the manufacturer of such
* system or application assumes all risk of such use and in doing so agrees to
* indemnify Cypress against all liability.
*
* Rutronik Elektronische Bauelemente GmbH Disclaimer: The evaluation board
* including the software is for testing purposes only and,
* because it has limited functions and limited resilience, is not suitable
* for permanent use under real conditions. If the evaluation board is
* nevertheless used under real conditions, this is done at one’s responsibility;
* any liability of Rutronik is insofar excluded
*******************************************************************************/
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
#include "cy_retarget_io.h"
#include "common.h"
#include "dps310_app.h"
#include "bmp581_app.h"
#include "bmi270_app.h"
#include "bme688_app.h"
#include "sht4x.h"
#include "sgp40.h"
#include "sensirion_voc_algorithm.h"
/*Priorities for sensor interrupts*/
#define MOTION_IRQ_PRIORITY 5
#define SENSOR_IRQ_PRIORITY 6
/*Li-ION Battery Voltage in mV*/
#define BATTERY_MIN_MV 3000U
#define BATTERY_MAX_MV 4200U
void motion_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event);
static void sensor_timer_isr(void *callback_arg, cyhal_timer_event_t event);
static cy_rslt_t sensor_timer_init(void);
void ProcessSensors(void);
/*Global interrupt flags*/
_Bool sensor_int_flag = false;
_Bool motion_int_flag = false;
/*A structure holding all the data gathered from the various sensors*/
sensor_data_t sensor_data_storage = {0};
/*VOC Index Algorithm Parameters*/
VocAlgorithmParams voc_algorithm_params;
/*Sensor Fusion Interrupt Data*/
cyhal_gpio_callback_data_t motion_int_data =
{
.callback = motion_interrupt_handler,
.callback_arg = NULL,
};
/*I2C Device Global Variables*/
cyhal_i2c_t I2C_scb3;
cyhal_i2c_cfg_t i2c_scb3_cfg =
{
.is_slave = false,
.address = 0,
.frequencyhal_hz = 400000UL,
};
/*SCP Timer Object */
cyhal_timer_t sensors_timer;
/* ADC Object */
cyhal_adc_t adc_obj;
/*ADC Battery Measurement Channel*/
cyhal_adc_channel_t adc_bat_channel;
/*ADC Battery Measurement Channel Configuration*/
const cyhal_adc_channel_config_t channel_config =
{
.enable_averaging = false,
.min_acquisition_ns = 220,
.enabled = true
};
extern int HostMain(void);
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init();
/* Board init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize retarget-io to use the debug UART port */
result = cy_retarget_io_init(KITPROG_TX, KITPROG_RX, CY_RETARGET_IO_BAUDRATE);
/* retarget-io init failed. Stop program execution */
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/* Initialize the User LEDs */
result = cyhal_gpio_init(LED1, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
result |= cyhal_gpio_init(LED2, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
result |= cyhal_gpio_init(LED3, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
cyhal_gpio_write((cyhal_gpio_t)LED1, CYBSP_LED_STATE_OFF);
cyhal_gpio_write((cyhal_gpio_t)LED2, CYBSP_LED_STATE_OFF);
cyhal_gpio_write((cyhal_gpio_t)LED3, CYBSP_LED_STATE_OFF);
/*Charger control*/
result = cyhal_gpio_init(CHR_DIS, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, false);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
cyhal_gpio_write((cyhal_gpio_t)CHR_DIS, false); /*Charger ON*/
/*Battery Voltage Divider Control*/
result = cyhal_gpio_init(DIV_EN, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, false);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
cyhal_gpio_write((cyhal_gpio_t)DIV_EN, false); /*Divider OFF*/
/*Battery Level Measurement ADC Initialization*/
result = cyhal_adc_init(&adc_obj, ARDU_ADC6, NULL);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
result = cyhal_adc_channel_init_diff(&adc_bat_channel, &adc_obj, ARDU_ADC6, CYHAL_ADC_VNEG, &channel_config);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/* Configure USER_BTN */
result = cyhal_gpio_init(USER_BTN, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_PULLUP, CYBSP_BTN_OFF);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/*Initialize I2C Master*/
result = cyhal_i2c_init(&I2C_scb3, ARDU_SDA, ARDU_SCL, NULL);
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
result = cyhal_i2c_configure(&I2C_scb3, &i2c_scb3_cfg);
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
/*Initialize Sensor Fusion Board Interrupt*/
result = cyhal_gpio_init(ARDU_IO2, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_NONE, false);
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/*Register callback function */
cyhal_gpio_register_callback(ARDU_IO2, &motion_int_data);
/* Enable rising edge interrupt events */
cyhal_gpio_enable_event(ARDU_IO2, CYHAL_GPIO_IRQ_BOTH, MOTION_IRQ_PRIORITY, true);
/*Initialize the DPS310 Sensor*/
result = dps310_app_init();
if (result != CY_RSLT_SUCCESS)
{
printf("DPS310 Sensor Failure\n\r");
CY_ASSERT(0);
}
/*Initialize the BMI270 Sensor*/
result = bmi270_app_init();
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/*Initialize the BMP581 Sensor*/
result = bmp581_app_init();
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/*Initialize the BME688 Sensor*/
result = bme688_app_init();
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/*Check the SHT4x sensor*/
if(sht4x_probe() != SHT4X_STATUS_OK)
{
printf("SHT4x Sensor Failure\n\r");
CY_ASSERT(0);
}
/*Check the SGP40 sensor*/
if(sgp40_probe() != SGP40_STATUS_OK)
{
printf("SGP40 Sensor Failure\n\r");
CY_ASSERT(0);
}
/*VOC Index Algorithm Stack Initialization*/
VocAlgorithm_init(&voc_algorithm_params);
/*Initialize SensorsTimer*/
result = sensor_timer_init();
if (result != CY_RSLT_SUCCESS)
{CY_ASSERT(0);}
/* Run Host main */
HostMain();
for (;;)
{
}
}
/* Interrupt handler callback function */
void motion_interrupt_handler(void *handler_arg, cyhal_gpio_event_t event)
{
CY_UNUSED_PARAMETER(handler_arg);
CY_UNUSED_PARAMETER(event);
/*Set the interrupt global flag*/
motion_int_flag = true;
}
/*10 Hz interrupt for sensor measurement and reading control*/
static void sensor_timer_isr(void *callback_arg, cyhal_timer_event_t event)
{
(void) callback_arg;
(void) event;
/*Set the interrupt global flag*/
sensor_int_flag = true;
}
/*This function initialized the timer to generate 10Hz interrupt*/
static cy_rslt_t sensor_timer_init(void)
{
cy_rslt_t result;
const cyhal_timer_cfg_t scp_cfg =
{
.compare_value = 0, /* Timer compare value, not used */
.period = 999, /* Defines the timer period - 10 Hz */
.direction = CYHAL_TIMER_DIR_UP, /* Timer counts up */
.is_compare = false, /* Don't use compare mode */
.is_continuous = true, /* Run the timer indefinitely */
.value = 0 /* Initial value of counter */
};
result = cyhal_timer_init(&sensors_timer, NC, NULL);
if (result != CY_RSLT_SUCCESS)
{return result;}
result = cyhal_timer_configure(&sensors_timer, &scp_cfg);
if (result != CY_RSLT_SUCCESS)
{return result;}
result = cyhal_timer_set_frequency(&sensors_timer, 10000);
if (result != CY_RSLT_SUCCESS)
{return result;}
cyhal_timer_register_callback(&sensors_timer, sensor_timer_isr, NULL);
cyhal_timer_enable_event(&sensors_timer, CYHAL_TIMER_IRQ_TERMINAL_COUNT, SENSOR_IRQ_PRIORITY, true);
result = cyhal_timer_start(&sensors_timer);
if (result != CY_RSLT_SUCCESS)
{return result;}
return result;
}
void ProcessSensors(void)
{
cy_rslt_t result;
int8_t rslt = 0;
static _Bool sensors_read = false;
_Bool pready, tready = false;
uint8_t n_fields;
int32_t batt_voltage = 0;
float batt_level = 0.0;
cyhal_gpio_toggle(LED1);
/*Read the battery level*/
cyhal_gpio_write((cyhal_gpio_t)DIV_EN, true); /*Divider ON*/
for(uint8_t i = 0; i < 4; i++)
{
CyDelay(1);
batt_voltage += (int32_t)(cyhal_adc_read_uv(&adc_bat_channel)/1000);
}
cyhal_gpio_write((cyhal_gpio_t)DIV_EN, false); /*Divider OFF*/
batt_voltage = batt_voltage/2;
batt_level = batt_voltage - BATTERY_MIN_MV;
batt_level = batt_level / (BATTERY_MAX_MV - BATTERY_MIN_MV);
batt_level = batt_level * 100.0;
if (batt_level < 100)
{sensor_data_storage.battery_lvl = (uint8_t)batt_level;}
else
{sensor_data_storage.battery_lvl = 100;}
/*** Read the DPS310 data ***/
result = xensiv_dps3xx_check_ready(&dps310_sensor, &pready, &tready);
if((result == CY_RSLT_SUCCESS) && pready && tready)
{
xensiv_dps3xx_read(&dps310_sensor, &sensor_data_storage.dps_pressure, &sensor_data_storage.dps_temperature);
}
/*** Read the BMP581 data ***/
rslt = bmp5_get_interrupt_status(&bmp5_status, &dev_bmp5);
/* Read temperature and pressure data */
if (bmp5_status & BMP5_INT_ASSERTED_DRDY)
{
rslt = bmp5_get_sensor_data(&bmp5_data, &osr_odr_press_cfg, &dev_bmp5);
if(rslt == BMP5_OK)
{
sensor_data_storage.bmp_temperature = bmp5_data.temperature;
sensor_data_storage.bmp_pressure = bmp5_data.pressure;
}
/* NOTE : Read status register again to clear data ready interrupt status */
(void)bmp5_get_interrupt_status(&bmp5_status, &dev_bmp5);
}
/*** Read the BME688 data ***/
rslt = bme68x_get_data(BME68X_PARALLEL_MODE, bme_data, &n_fields, &bme);
for (uint8_t i = 0; i < n_fields; i++)
{
if (bme_data[i].status == BME68X_VALID_DATA)
{
sensor_data_storage.bme_temperature = bme_data[i].temperature;
sensor_data_storage.bme_humidity = bme_data[i].humidity;
sensor_data_storage.bme_pressure = bme_data[i].pressure;
sensor_data_storage.bme_gas_resistance = bme_data[i].gas_resistance;
sensor_data_storage.bme_gas_index = bme_data[i].gas_index;
}
}
/*** Measure & Read the SGP40 and SHT41 data ***/
if(sensor_int_flag )
{
if(!sensors_read)
{
sht4x_measure();
}
else
{
sht4x_read(&sensor_data_storage.sht_temperature, &sensor_data_storage.sht_humidity);
}
if(!sensors_read)
{
sgp40_measure_raw_with_rht(sensor_data_storage.sht_humidity, sensor_data_storage.sht_temperature);
//sgp40_measure_raw();
}
else
{
/*Read the raw data and process the VOC Index*/
sgp40_read_raw(&sensor_data_storage.sgp_sraw_voc);
VocAlgorithm_process(&voc_algorithm_params, sensor_data_storage.sgp_sraw_voc, &sensor_data_storage.sgp_voc_index);
}
sensors_read = !sensors_read;
/*Clear the interrupt global variable*/
sensor_int_flag = false;
}
/*Store IMU data*/
if(motion_int_flag )
{
/* Get accel and gyro data for x, y and z axis. */
rslt = bmi270_get_sensor_data(bmi_sensor_data, 2, &bmi2_dev);
if(rslt == BMI2_OK)
{
sensor_data_storage.bmi_acc_x = bmi_sensor_data[ACCEL].sens_data.acc.x;
sensor_data_storage.bmi_acc_y = bmi_sensor_data[ACCEL].sens_data.acc.y;
sensor_data_storage.bmi_acc_z = bmi_sensor_data[ACCEL].sens_data.acc.z;
sensor_data_storage.bmi_gyr_x = bmi_sensor_data[GYRO].sens_data.gyr.x;
sensor_data_storage.bmi_gyr_y = bmi_sensor_data[GYRO].sens_data.gyr.y;
sensor_data_storage.bmi_gyr_z = bmi_sensor_data[GYRO].sens_data.gyr.z;
}
/*Clear the interrupt global variable*/
motion_int_flag = false;
/*Clear the interrupt status*/
(void)bmi2_get_int_status(&bmi_int_status, &bmi2_dev);
}
}
/* [] END OF FILE */