-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_main.c
204 lines (166 loc) · 5.32 KB
/
app_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
/**
* @file app_main.c
*
* @brief Communicates with TTTW labkit accelerometer over I2C protocol. Writes
* results to log output.
*
* MMA8653FC datasheet
* https://www.nxp.com/docs/en/data-sheet/MMA8653FC.pdf
*
* MMA8653FC application note
* https://www.nxp.com/docs/en/application-note/AN4083.pdf
*
* EFR32 Application Note on I2C
* https://www.silabs.com/documents/public/application-notes/AN0011.pdf
*
* EFR32MG12 Wireless Gecko Reference Manual (I2C p501)
* https://www.silabs.com/documents/public/reference-manuals/efr32xg12-rm.pdf
*
* EFR32MG12 Wireless Gecko datasheet
* https://www.silabs.com/documents/public/data-sheets/efr32mg12-datasheet.pdf
*
* GPIO API documentation
* https://docs.silabs.com/mcu/latest/efr32mg12/group-GPIO
*
* ARM RTOS API
* https://arm-software.github.io/CMSIS_5/RTOS2/html/group__CMSIS__RTOS.html
*
* @author Johannes Ehala, ProLab.
* @license MIT
*
* Copyright Thinnect Inc. 2019
* Copyright ProLab, TTÜ. 2021
*
*/
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include "retargetserial.h"
#include "cmsis_os2.h"
#include "platform.h"
#include "SignatureArea.h"
#include "DeviceSignature.h"
#include "loggers_ext.h"
#include "logger_fwrite.h"
#include "i2c_handler.h"
#include "mma8653fc_reg.h"
#include "gpio_handler.h"
#include "mma8653fc_driver.h"
#include "app_main.h"
#include "loglevels.h"
#define __MODUUL__ "main"
#define __LOG_LEVEL__ (LOG_LEVEL_main & BASE_LOG_LEVEL)
#include "log.h"
// Include the information header binary
#include "incbin.h"
INCBIN(Header, "header.bin");
static osThreadId_t dataReadyThreadId;
float calc_signal_energy(float buf[], uint32_t num_elements);
// Heartbeat loop - periodically print 'Heartbeat'
static void hb_loop (void *args)
{
for (;;)
{
osDelay(10000);
info1("Heartbeat");
}
}
/**
* @brief Configures I2C, GPIO and sensor, wakes up on MMA8653FC data ready interrupt, fetches
* a batch of sensor data and analyzes data.
*/
static void mma_data_ready_loop (void *args)
{
// TODO Initialize and enable I2C.
// TODO Read Who-am-I registry
// TODO To configure sensor put sensor in standby mode.
// TODO Configure sensor for xyz data acquisition.
// TODO Configure sensor to generate interrupt when new data becomes ready.
// TODO Configure GPIO for external interrupts and enable external interrupts.
// TODO Activate sensor.
for (;;)
{
// TODO Wait for data ready interrupt signal from MMA8653FC sensor
// TODO Get raw data
// TODO Convert to engineering value
// TODO Signal analysis
}
}
int logger_fwrite_boot (const char *ptr, int len)
{
fwrite(ptr, len, 1, stdout);
fflush(stdout);
return len;
}
int main ()
{
PLATFORM_Init();
// LEDs
PLATFORM_LedsInit(); // This also enables GPIO peripheral.
// Configure debug output.
RETARGET_SerialInit();
log_init(BASE_LOG_LEVEL, &logger_fwrite_boot, NULL);
info1("Digi-sensor-demo "VERSION_STR" (%d.%d.%d)", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
// Initialize OS kernel.
osKernelInitialize();
// Create a thread.
const osThreadAttr_t app_thread_attr = { .name = "heartbeat" , .priority = osPriorityNormal2 };
osThreadNew(hb_loop, NULL, &app_thread_attr);
// Create thread to receive data ready event and read data from sensor.
const osThreadAttr_t data_ready_thread_attr = { .name = "data_ready_thread" };
dataReadyThreadId = osThreadNew(mma_data_ready_loop, NULL, &data_ready_thread_attr);
if (osKernelReady == osKernelGetState())
{
// Switch to a thread-safe logger
logger_fwrite_init();
log_init(BASE_LOG_LEVEL, &logger_fwrite, NULL);
// Start the kernel
osKernelStart();
}
else
{
err1("!osKernelReady");
}
for(;;);
}
/**
* @brief
* Calculate energy of measured signal.
*
* @details
* Energy is calculated by subtracting bias from every sample and then adding
* together the square values of all samples. Energy is small if there is no
* signal (just measurement noise) and larger when a signal is present.
*
* Disclaimer: The signal measured by the ADC is an elecrical signal, and its
* unit would be joule, but since I don't know the exact load that the signal
* is driving I can't account for the load. And so the energy I calculate here
* just indicates the presence or absence of a signal (and its relative
* strength), not the actual electrical energy in joules.
* Such a calculation can be done to all sorts of signals. There is probably
* a more correct scientific term than energy for the result of this calculation
* but I don't know what it is.
*
* Read about signal energy
* https://www.gaussianwaves.com/2013/12/power-and-energy-of-a-signal/
*
* @return Energy value.
*/
float calc_signal_energy(float buf[], uint32_t num_elements)
{
static uint32_t i;
static float signal_bias, signal_energy, res;
signal_bias = signal_energy = res = 0;
for (i = 0; i < num_elements; i++)
{
signal_bias += buf[i];
}
signal_bias /= num_elements;
for (i = 0; i < num_elements; i++)
{
res = buf[i] - signal_bias; // Subtract bias
signal_energy += res * res;
}
return signal_energy;
}