sensors.c
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
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
#include "sensors.h"
sensor light_sensor = {LIGHT_ADDR, "Light sensor", "illumination", "lux"};
sensor ultrasound_sensor = {DISTANCE_ADDR, "Ultrasound sensor", "distance", "cm"};
sensor pressure_sensor = {PRESSURE_ADDR, "Pressure sensor", "pressure", "hPa"};
sensor humidity_temp_sensor = {HUMIDITY_TEMP_ADDR, "Temperature and humidity sensor", "temperature,humidity", "ºC,RH"};
sensor sound_sensor = {SOUND_ADDR, "Sound sensor", "sound", "mV"};
sensor battery = {BATTERY_ADDR, "Battery Level", "power", "mV"};
sensor* sensors[TOTAL_SENSORS+1] = {&light_sensor,&ultrasound_sensor,&pressure_sensor,&humidity_temp_sensor,&sound_sensor,&battery};
void wakeup_sensors(uint8_t logic_address)
{
// X X X PA7 PA6 PA5 PA4 PA3
// b7 b6 b5 b4 b3 b2 b1 b0
DBG_SENSORS("Waking up sensors...\r\n");
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
if(logic_address & 0x1)
{
DBG_SENSORS("Woken Pin 3\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_3);
}
else
{
DBG_SENSORS("Pin 3 gone low\r\n");
GPIO_ResetBits(GPIOA,GPIO_Pin_3);
}
if(logic_address & 0x2)
{
DBG_SENSORS("Woken Pin 4\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_4);
}
else
{
DBG_SENSORS("Pin 4 gone low\r\n");
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
}
if(logic_address & 0x4)
{
DBG_SENSORS("Woken Pin 5\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_5);
}
else
{
DBG_SENSORS("Pin 5 gone low\r\n");
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
}
if(logic_address & 0x8)
{
DBG_SENSORS("Woken Pin 6\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_6);
}
else
{
DBG_SENSORS("Pin 6 gone low\r\n");
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
}
if(logic_address & 0x10)
{
DBG_SENSORS("Woken Pin 7\r\n");
GPIO_SetBits(GPIOA,GPIO_Pin_7);
}
else
{
DBG_SENSORS("Pin 7 gone low\r\n");
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
}
}
void init_light(void)
{
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x80);
I2C_write(I2C1, 0x03);
I2C_stop(I2C1);
DBG_SENSORS("[LIGHT] Sent power on command\r\n");
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x81);
I2C_write(I2C1, 0x0A);
I2C_stop(I2C1);
DBG_SENSORS("[LIGHT] Sent start conversion command\r\n");
chThdSleepMilliseconds(500);
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x81);
I2C_write(I2C1, 0x02);
I2C_stop(I2C1);
DBG_SENSORS("[LIGHT] Sent stop conversion command\r\n");
}
uint32_t get_light_data(void)
{
init_light();
uint32_t data = 0;
data = get_light_ch1();
DBG_SENSORS("[LIGHT] Got ch1\r\n");
data = data << 16;
data = data | get_light_ch0();
DBG_SENSORS("[LIGHT] Got ch0\r\n");
DBG_SENSORS("[LIGHT] CH1 = %02x\r\n",(data & 0xFFFF0000) >> 16);
DBG_SENSORS("[LIGHT] CH0 = %02x\r\n",data & 0x0000FFFF);
return data;
}
uint16_t get_light_ch0(void)
{
uint16_t data = 0;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAC);
DBG_SENSORS("[LIGHT] Sent fetch command for CH0\r\n");
I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
DBG_SENSORS("[LIGHT] Got low byte for CH0\r\n");
data = data | (I2C_read_nack(I2C1) << 8);
DBG_SENSORS("[LIGHT] Got high byte for CH0\r\n");
I2C_stop(I2C1);
return data;
}
uint16_t get_light_ch1(void)
{
uint16_t data = 0;
I2C_start(I2C1,LIGHT_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x8E);
DBG_SENSORS("[LIGHT] Sent fetch command for CH1\r\n");
I2C_restart(I2C1, LIGHT_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
DBG_SENSORS("[LIGHT] Got low byte for CH1\r\n");
data = data | (I2C_read_nack(I2C1) << 8);
DBG_SENSORS("[LIGHT] Got high byte for CH1\r\n");
I2C_stop(I2C1);
return data;
}
char* light_value(uint32_t light)
{
char *value = chHeapAlloc(NULL,10 + 1);
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,11,value,0x1B);
sprintf(value,"%u",(unsigned int)light);
return value;
}
void init_ultrasound(void)
{
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x00);
I2C_write(I2C1, 0x50);
I2C_stop(I2C1);
chThdSleepMilliseconds(70);
}
uint16_t get_distance_data(void)
{
init_ultrasound();
DBG_SENSORS("[DISTANCE] Initialized\r\n");
uint16_t data;
I2C_start(I2C1,DISTANCE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0x02);
I2C_restart(I2C1, DISTANCE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
DBG_SENSORS("[DISTANCE] Got high byte\r\n");
data = data << 8;
data = data | I2C_read_nack(I2C1);
DBG_SENSORS("[DISTANCE] Got low byte\r\n");
I2C_stop(I2C1);
return data;
}
char* distance_value(uint16_t distance)
{
char *value = chHeapAlloc(NULL,5 + 1);
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,6,value,0x1B);
sprintf(value,"%u",distance);
return value;
}
void init_pressure(void)
{
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF4);
I2C_write(I2C1, 0x34);
I2C_stop(I2C1);
chThdSleepMilliseconds(5);
}
void init_pressure_temperature(void)
{
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF4);
I2C_write(I2C1, 0x2E);
I2C_stop(I2C1);
chThdSleepMilliseconds(5);
}
void get_pressure_callibration_data(bmp085_callibration_t *calib_data)
{
DBG_SENSORS("[PRESSURE] Fetching callibration data\r\n");
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAA);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->AC1 = (I2C_read_ack(I2C1) << 8);
calib_data->AC1 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAC);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->AC2 = (I2C_read_ack(I2C1) << 8);
calib_data->AC2 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xAE);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->AC3 = (I2C_read_ack(I2C1) << 8);
calib_data->AC3 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB0);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->AC4 = (I2C_read_ack(I2C1) << 8);
calib_data->AC4 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB2);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->AC5 = (I2C_read_ack(I2C1) << 8);
calib_data->AC5 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB4);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->AC6 = (I2C_read_ack(I2C1) << 8);
calib_data->AC6 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB6);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->B1 = (I2C_read_ack(I2C1) << 8);
calib_data->B1 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xB8);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->B2 = (I2C_read_ack(I2C1) << 8);
calib_data->B2 |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBA);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->MB = (I2C_read_ack(I2C1) << 8);
calib_data->MB |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBC);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->MC = (I2C_read_ack(I2C1) << 8);
calib_data->MC |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xBE);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
calib_data->MD = (I2C_read_ack(I2C1) << 8);
calib_data->MD |= I2C_read_nack(I2C1);
I2C_stop(I2C1);
DBG_SENSORS("[PRESSURE] Got callibration data\r\n");
}
char* callibration_pressure_data_csv(bmp085_callibration_t *parameters)
{
char *str = chHeapAlloc(NULL,(11*11-1));
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,11*11-1,str,0x1B);
memset(str,0x00,(11*11-1));
sprintf(str + strlen(str),"%u,",parameters->AC1);
sprintf(str + strlen(str),"%u,",parameters->AC2);
sprintf(str + strlen(str),"%u,",parameters->AC3);
sprintf(str + strlen(str),"%u,",parameters->AC4);
sprintf(str + strlen(str),"%u,",parameters->AC5);
sprintf(str + strlen(str),"%u,",parameters->AC6);
sprintf(str + strlen(str),"%u,",parameters->B1);
sprintf(str + strlen(str),"%u,",parameters->B2);
sprintf(str + strlen(str),"%u,",parameters->MB);
sprintf(str + strlen(str),"%u,",parameters->MC);
sprintf(str + strlen(str),"%u",parameters->MD);
return str;
}
uint16_t get_pressure_data(void)
{
uint16_t data;
init_pressure();
DBG_SENSORS("[PRESSURE] Initialized\r\n");
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF6);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
DBG_SENSORS("[PRESSURE] Got high byte\r\n");
data = data << 8;
data = data | I2C_read_nack(I2C1);
DBG_SENSORS("[PRESSURE] Got low byte\r\n");
I2C_stop(I2C1);
return data;
}
uint16_t get_pressure_temperature_data(void)
{
uint16_t data;
init_pressure_temperature();
DBG_SENSORS("[PRESSURE] Initialized temperature\r\n");
I2C_start(I2C1,PRESSURE_ADDR << 1, I2C_Direction_Transmitter);
I2C_write(I2C1, 0xF6);
I2C_restart(I2C1, PRESSURE_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1);
DBG_SENSORS("[PRESSURE] Got temperature high byte\r\n");
data = data << 8;
data = data | I2C_read_nack(I2C1);
DBG_SENSORS("[PRESSURE] Got temperature low byte\r\n");
I2C_stop(I2C1);
return data;
}
char* pressure_value(uint16_t pressure, uint16_t temperature)
{
char *value = chHeapAlloc(NULL,11 + 1);
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,12,value,0x1B);
sprintf(value,"%u,%u",pressure,temperature);
return value;
}
void init_humidity_temp(void)
{
DBG_SENSORS("[HUMIDITY_TEMP] Initializing sensor\r\n");
I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Transmitter);
I2C_stop(I2C1);
}
uint16_t get_humidity_data(void)
{
init_humidity_temp();
DBG_SENSORS("[HUMIDITY_TEMP] Initialized humidity\r\n");
uint16_t data = 0;
I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Receiver);
data = I2C_read_ack(I2C1) & 0x3F;
DBG_SENSORS("[HUMIDITY_TEMP] Got humidity high byte\r\n");
data = data << 8;
data |= I2C_read_nack(I2C1);
DBG_SENSORS("[HUMIDITY_TEMP] Got humidity low byte\r\n");
I2C_stop(I2C1);
return data;
}
uint16_t get_temperature_data(void)
{
init_humidity_temp();
DBG_SENSORS("[HUMIDITY_TEMP] Initialized temperature\r\n");
uint16_t data = 0;
I2C_start(I2C1,HUMIDITY_TEMP_ADDR << 1, I2C_Direction_Receiver);
I2C_read_ack(I2C1);
DBG_SENSORS("[HUMIDITY_TEMP] Discarded first humidity byte\r\n");
I2C_read_ack(I2C1);
DBG_SENSORS("[HUMIDITY_TEMP] Discarded second humidity byte\r\n");
data = I2C_read_ack(I2C1);
DBG_SENSORS("[HUMIDITY_TEMP] Got temperature high byte\r\n");
data = data << 8;
data |= I2C_read_nack(I2C1) & 0xFC ;
DBG_SENSORS("[HUMIDITY_TEMP] Got temperature low byte\r\n");
data = data >> 2;
I2C_stop(I2C1);
return data;
}
char* temp_humidity_value(uint16_t temp, uint16_t humidity)
{
char *value = chHeapAlloc(NULL,11 + 1);
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,12,value,0x1B);
sprintf(value,"%u,%u",temp,humidity);
return value;
}
void init_sound(void)
{
adc_sound_init();
}
uint32_t get_sound_data(void)
{
init_sound();
return adc_sound_process();
}
char* sound_value(uint32_t sound)
{
char *value = chHeapAlloc(NULL,11 + 1);
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,12,value,0x1B);
sprintf(value,"%4u",(unsigned int)sound);
return value;
}
void init_battery(void)
{
adc_batt_init();
}
uint32_t get_battery_data(void)
{
init_battery();
return adc_batt_process();
}
char* battery_value(uint32_t battery)
{
char *value = chHeapAlloc(NULL,11 + 1);
printf("%c[1;31m[ALLOC] Allocated %d bytes to %08x%c[1;00m\r\n",0x1B,12,value,0x1B);
sprintf(value,"%4u",(unsigned int)battery);
return value;
}
void collectData(char* valueSensors[], uint8_t* sensors)
{
DBG_SENSORS("Collecting data...\r\n");
uint8_t i;
for(i=0;i<strlen((char*)sensors);i++)
{
if(valueSensors[i]!=NULL)
{
DBG_SENSORS("Freeing memory from previous entry on index %d\r\n",i);
chHeapFree(valueSensors[i]);
printf("%c[1;32m[FREE] Freed bytes from %08x%c[1;00m\r\n",0x1B,valueSensors[i],0x1B);
}
else
{
DBG_SENSORS("Not freeing memory on index %d\r\n",i);
}
if(sensors[i]==LIGHT_ADDR)
{
DBG_SENSORS("Fetching data from light sensor\r\n");
valueSensors[i]=light_value(get_light_data());
}
else if(sensors[i]==DISTANCE_ADDR)
{
DBG_SENSORS("Fetching data from distance sensor\r\n");
valueSensors[i]=distance_value(get_distance_data());
}
else if(sensors[i]==SOUND_ADDR)
{
DBG_SENSORS("Fetching data from sound sensor\r\n");
valueSensors[i]=sound_value(get_sound_data());
}
else if(sensors[i]==PRESSURE_ADDR)
{
DBG_SENSORS("Fetching data from pressure sensor\r\n");
valueSensors[i]=pressure_value(get_pressure_data(), get_pressure_temperature_data());
}
else if(sensors[i]==HUMIDITY_TEMP_ADDR)
{
DBG_SENSORS("Fetching data from humidity and temperature sensor\r\n");
valueSensors[i]=temp_humidity_value(get_temperature_data(),get_humidity_data());
}
}
DBG_SENSORS("Data collected\r\n");
}