-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1405 lines (1327 loc) · 40.3 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Written by Icewire Technologies.
*
*
* The format for config.ini is as follows:
* Text after semicolons is considered a comment.
* A line that matches /^ *sr *= *[0-9]+ *$/ is used to set the sample rate.
* Valid bandwidth values: 40, 160, 640.
* A line that matches /^ *ar *= *[0-9]+ *$/ is used to set the range of the
* accelerometer. Valid range values: 2, 6.
* A line that matches /^ *gr *= *[0-9]+ *$/ is used to set the range of the
* gyroscope. Valid range values: 250, 500, 2000.
* A line that matches /^ *gs *= *[0-9]+ *$/ is used to set the sample rate of
* the gyroscope. Valid bandwidth values: 100, 200, 400, 800.
* A line that matches /^ *disable_gyro *$/ is used to disable logging for the
* gyroscope.
* A line that matches /^ *disable_accel *$/ is used to disable logging for the
* accelerometer.
*/
#include <msp430f5310.h>
#include <stdint.h>
#include <stdbool.h>
#include "spi.h"
#include "sdfat.h"
#include "LIS3LV02DL.h"
#include "L3G4200D.h"
#include "config.h"
#include "msp430f5310_extra.h"
#include "circuit.h"
#include "samplebuffer.h"
#include "buttonbuffer.h"
#include "const.h"
#include "macro.h"
#include "conversions.h"
/*
* Define global debugging variables
*/
#ifdef DEBUG
static volatile bool debug_hit = false;
static volatile uint32_t debug_int = 0;
#endif
/* Possible states of the device */
enum DeviceState {
OFF_STATE,
IDLE_STATE,
LOG_STATE,
FORMAT_STATE
};
/* Data logger settings */
struct Logger {
bool is_enabled;
uint8_t range;
uint8_t bandwidth;
};
/* Buffer of data to write to SD card */
struct SdCardFile {
uint8_t buffer[SD_SAMPLE_BUFF_SIZE];
/* Current index in buffer */
uint16_t index;
/* First cluster index */
uint16_t start_cluster;
/* Current cluster index */
uint32_t cluster;
/* Number of blocks */
uint8_t block_num;
/* Total bytes */
uint32_t size;
};
/*
* Function prototypes
*/
void start_watchdog(void);
void stop_watchdog(void);
void feed_watchdog(void);
void power_on_sd(void);
void power_off_sd(void);
void power_on_accelerometer(void);
void power_off_accelerometer(void);
void power_on_gyroscope(void);
void power_off_gyroscope(void);
void enable_button_pressing(bool enable_button_tap_flash, bool enable_triple_tap);
/* Note: perform an empty read before so we can clear P1.5 */
void accelerometer_empty_read(void);
bool voltage_is_low(void);
/* Flash LED multiple times quickly to show "panic" */
void led_1_panic(void);
/* Flash LED dimly multiple times to signal low voltage */
void led_1_low_voltage(void);
/* Flash LED every chosen amount of seconds determined by the RTC */
bool flash_led_at_rate(uint8_t seconds);
/* Flash LED weakly */
void led_1_weak_flash(void);
/* Flash LED strongly */
void led_1_strong_flash(void);
/* Flash LED for a longer amount of time */
void led_1_long_flash(void);
void init(void);
void restart(void);
enum DeviceState idle(void);
enum DeviceState turn_off(void);
enum DeviceState start_logging(void);
enum DeviceState stop_logging(void);
enum DeviceState format_card(void);
enum DeviceState off_step(void);
enum DeviceState idle_step(void);
enum DeviceState log_step(void);
enum DeviceState format_step(void);
void init_sd_fat(void);
void format_sd_card(void);
void new_sd_card_file(struct SdCardFile *const sd_card_file);
void add_firmware_info_to_sd_card_file(struct SdCardFile *const sd_card_file);
uint32_t get_block_offset(const struct SdCardFile *const sd_card_file);
bool add_value_to_buffer(struct SdCardFile *const sd_card_file, uint8_t value);
bool write_full_buffer_to_sd_card(struct SdCardFile *const sd_card_file);
/* Only write remainder of buffer for end of file */
bool write_remaining_buffer_to_sd_card(struct SdCardFile *const sd_card_file);
void get_config_settings(void);
void timer_interrupt_event(void);
bool button_press_event_handled(void);
bool sample_event_handled(void);
bool timer_interrupt_triggered(void);
void clear_timer_interrupt(void);
bool button_interrupt_triggered(void);
enum ButtonPress get_button_press(bool can_triple_tap);
enum ButtonPress wait_for_button_release(void);
/* High byte for continuous timer */
volatile uint8_t time_cont;
/* Time of last sample for getting delta timestamp for acceleration data for new sample */
uint32_t timestamp_accel;
/* Buffer for samples */
struct SampleBuffer sample_buffer;
/* Samples for buffer */
volatile struct Sample samples[RAW_SAMPLE_BUFF_SIZE];
/* Buffer for button presses */
struct ButtonPressBuffer button_press_buffer;
/* Button presses for buffer of button presses */
volatile enum ButtonPress button_presses[BUTTON_BUFF_SIZE];
/* Whether the user can triple tap */
bool triple_tap_enabled;
/* Whether there is an indication of a button tap by flashing the LED */
bool button_tap_flash_enabled;
/* Information for SD FAT library */
struct fatstruct fatinfo;
/* Buffer for accelerometer sample data to write to SD card */
struct SdCardFile sd_file;
/* Temporary variable for initializing the SD card using an SdCardFile's buffer */
uint8_t *data_sd;
/* Accelerometer settings */
struct Logger accelerometer;
/* Gyroscope settings */
struct Logger gyroscope;
/* So that the LED doesn't flash multiple times per second */
uint8_t prev_sec = 0;
void start_watchdog(void) {
#ifndef DEBUG
wdt_config();
#endif
}
void stop_watchdog(void) {
wdt_stop();
}
void feed_watchdog(void) {
#ifndef DEBUG
wdt_config();
#endif
}
void power_on_sd(void) {
power_on(SD_PWR);
/* Needs a delay to complete powering-on */
POWER_ON_DELAY();
/* Initialize SD card */
/* TODO may not necessarily need to init after each power-on */
if (init_sd() != SD_SUCCESS) {
/* Turn the LED on and hang to indicate failure */
led_1_on();
HANG();
}
}
void power_off_sd(void) {
power_off(SD_PWR);
}
void power_on_accelerometer(void) {
power_on(ACCEL_PWR);
/* Needs a delay to complete powering-on */
POWER_ON_DELAY();
/* Initialize accelerometer */
/* TODO may not necessarily need to init after each power-on */
if (!init_accel(accelerometer.range, accelerometer.bandwidth)) {
/* Turn the LED on and hang to indicate failure */
led_1_on();
HANG();
}
}
void power_off_accelerometer(void) {
/* So accelerometer interrupt is low */
accelerometer_empty_read();
power_down_accel();
power_off(ACCEL_PWR);
}
void power_on_gyroscope(void) {
power_on(GYRO_PWR);
/* Needs a delay to complete powering-on */
POWER_ON_DELAY();
/* Initialize gyroscope */
/* TODO may not necessarily need to init after each power-on */
if (!init_gyro(gyroscope.range, gyroscope.bandwidth)) {
/* Turn the LED on and hang to indicate failure */
led_1_on();
HANG();
}
}
void power_off_gyroscope(void) {
power_down_gyro();
power_off(GYRO_PWR);
}
void enable_button_pressing(bool enable_button_tap_flash, bool enable_triple_tap) {
button_tap_flash_enabled = enable_button_tap_flash;
triple_tap_enabled = enable_triple_tap;
/* Clear button press buffer */
clear_button_press_buffer(&button_press_buffer);
/* Set button press interrupt to active to wait on enable_interrupts() */
activate_ctrl_interrupt();
}
void accelerometer_empty_read(void) {
read_addr_accel(ACCEL_OUTX_H);
read_addr_accel(ACCEL_OUTX_L);
read_addr_accel(ACCEL_OUTY_H);
read_addr_accel(ACCEL_OUTY_L);
read_addr_accel(ACCEL_OUTZ_H);
read_addr_accel(ACCEL_OUTZ_L);
}
bool voltage_is_low(void) {
uint16_t voltage = adc_read();
if (voltage < VOLTAGE_THRSHLD) {
/* Show low voltage with LED 1 */
led_1_low_voltage();
return true;
}
return false;
}
void led_1_panic(void) {
led_1_off();
for (uint8_t k = 0; k < 20; k++) {
led_1_toggle();
for (uint8_t j = 0; j < CLOCK_SPEED; j++) {
for (uint16_t i = 0; i < 8000; i++);
}
}
}
void led_1_low_voltage(void) {
for (uint8_t i = 0; i < 20; i++) {
if (i % 2 == 0) {
led_1_on();
LED_FLASH_DELAY(170);
} else {
led_1_off();
LED_FLASH_DELAY(10922);
}
}
led_1_off();
}
bool flash_led_at_rate(uint8_t seconds) {
if (rtc_rdy()) {
if (RTCSEC % seconds == 0 && RTCSEC != 0 && RTCSEC != prev_sec) {
prev_sec = RTCSEC;
return true;
}
}
return false;
}
void led_1_weak_flash(void) {
led_1_on();
LED_FLASH_DELAY(1000);
led_1_off();
}
void led_1_strong_flash(void) {
led_1_on();
LED_FLASH_DELAY(10000);
led_1_off();
}
void led_1_long_flash(void) {
led_1_on();
LED_FLASH_DELAY(60000);
led_1_off();
}
/* Return int for compiler compatibility */
int main(void) {
/* Initialize upon startup */
init();
/* Start in idle since resetting runs the firmware updater which runs this */
enum DeviceState device_state = idle();
/* Application loop */
while (true) {
feed_watchdog();
switch(device_state) {
case OFF_STATE:
device_state = off_step();
break;
case IDLE_STATE:
device_state = idle_step();
break;
case LOG_STATE:
device_state = log_step();
break;
case FORMAT_STATE:
device_state = format_step();
break;
}
}
return 0;
}
void init(void) {
/* Construct data buffers */
construct_sample_buffer(&sample_buffer, samples, RAW_SAMPLE_BUFF_SIZE);
construct_button_press_buffer(&button_press_buffer, button_presses, BUTTON_BUFF_SIZE);
/* Point pointer to buffer */
data_sd = sd_file.buffer;
/* Watchdog timer is on by default */
stop_watchdog();
/* Set up and configure the clock */
clock_config();
/* Configure MCU pins */
mcu_pin_config();
/* Set up ADC */
adc_config();
/* Set up SPI for MCU */
spi_config();
/* Start the watchdog */
start_watchdog();
/* Deactivate all interrupts */
deactivate_interrupts();
/* Start the timer */
timer_config();
}
void restart(void) {
/* Trigger a brownout reset */
brownout_reset();
}
enum DeviceState idle(void) {
feed_watchdog();
/* Make sure the LED is off */
led_1_off();
disable_interrupts();
feed_watchdog();
/*
* We don't want to waste time checking for triple taps when device
* is on as we don't have any features which require triple tapping
*/
enable_button_pressing(true, false);
feed_watchdog();
/* Set up the clock to flash the LED */
rtc_restart();
prev_sec = RTCSEC;
feed_watchdog();
enable_interrupts();
return IDLE_STATE;
}
enum DeviceState turn_off(void) {
feed_watchdog();
/* Make sure the LED is off */
led_1_off();
disable_interrupts();
/* We have features which require triple tapping when device is off */
feed_watchdog();
enable_button_pressing(false, true);
feed_watchdog();
enable_interrupts();
return OFF_STATE;
}
enum DeviceState start_logging(void) {
feed_watchdog();
/* Turn on power to SD card and read the FAT boot sector */
power_on_sd();
feed_watchdog();
/* Check for low voltage */
if (voltage_is_low()) {
restart();
}
feed_watchdog();
init_sd_fat();
feed_watchdog();
/*
* We parse the config file each time we want to start logging so the user doesn't
* have to restart the device manually each time they modify the config settings.
* NOTE if this ever proves too slow or too power hungry, we can do this in device
* turn on state.
*/
get_config_settings();
disable_interrupts();
feed_watchdog();
enable_button_pressing(true, false);
/* Power on logging devices and activate interrupts */
feed_watchdog();
/* Accelerometer is always turned on since we use its interrupt to grab samples */
{
power_on_accelerometer();
activate_accel_interrupt();
}
feed_watchdog();
if (gyroscope.is_enabled) {
power_on_gyroscope();
}
feed_watchdog();
new_sd_card_file(&sd_file);
feed_watchdog();
/* Clear raw samples buffer */
clear_sample_buffer(&sample_buffer);
/* Reset timer */
time_cont = 0;
/* Reset time of last sample */
timestamp_accel = 0;
feed_watchdog();
/* Set up the clock to flash the LED */
rtc_restart();
prev_sec = RTCSEC;
feed_watchdog();
/* Start capturing samples */
enable_interrupts();
/* Read accelerometer axes to get interrupt started */
accelerometer_empty_read();
return LOG_STATE;
}
enum DeviceState stop_logging(void) {
feed_watchdog();
/* Power off logging devices */
{
power_off_accelerometer();
}
feed_watchdog();
if (gyroscope.is_enabled) {
power_off_gyroscope();
}
feed_watchdog();
/* Write final logger data in buffer and update the directory table */
{
write_remaining_buffer_to_sd_card(&sd_file);
/* Name of log file */
uint8_t file_name[] = FILE_NAME;
/* Get the number of the last log file */
uint16_t file_num = get_file_num(sd_file.buffer, &fatinfo, file_name);
if (update_dir_table(sd_file.buffer,
&fatinfo,
sd_file.start_cluster,
sd_file.size,
file_name,
file_num) != FAT_SUCCESS) {
/* Turn the LED on and hang to indicate failure */
led_1_on();
HANG();
}
}
feed_watchdog();
/* Turn off power to SD card since writing is complete */
power_off_sd();
return idle();
}
enum DeviceState format_card(void) {
feed_watchdog();
disable_interrupts();
/* No triple tapping feature in this state */
enable_button_pressing(true, false);
feed_watchdog();
enable_interrupts();
return FORMAT_STATE;
}
enum DeviceState off_step(void) {
/* Turn off the wdt before entering low power mode */
stop_watchdog();
/* Wait for button press in low power mode */
enter_LPM();
/* Button press happened, so continue */
exit_LPM();
/* Turn on the wdt after exiting low power mode */
start_watchdog();
/* Get the button press */
enum ButtonPress button_press;
bool success = remove_button_press(&button_press_buffer, &button_press);
if (!success) {
#ifdef DEBUG
HANG();
#endif
} else {
/* Change state based on button press */
switch(button_press) {
case BUTTON_TAP:
/* Try again for a different type of button press */
return turn_off();
case BUTTON_HOLD:
restart();
break;
case BUTTON_TRIPLE_TAP:
/* Format the SD card */
return format_card();
}
}
return OFF_STATE;
}
enum DeviceState idle_step(void) {
if (flash_led_at_rate(IDLE_FLASH_RATE)) {
led_1_weak_flash();
}
/* Check for any button presses */
if (button_press_buffer.count > 0) {
enum ButtonPress button_press;
bool success = remove_button_press(&button_press_buffer, &button_press);
if (!success) {
#ifdef DEBUG
HANG();
#endif
} else {
switch(button_press) {
case BUTTON_TAP:
return start_logging();
case BUTTON_HOLD:
return turn_off();
}
}
}
return IDLE_STATE;
}
enum DeviceState log_step(void) {
/* Check for low voltage */
if (voltage_is_low()) {
return stop_logging();
}
#ifndef DEBUG
if (flash_led_at_rate(LOG_FLASH_RATE)) {
led_1_strong_flash();
}
#endif
/* Process samples */
// TODO refactor this to its own function but for now...
/* Convert all current samples in raw buffer to ascii */
uint16_t count = sample_buffer.count;
#ifdef DEBUG
if (count == 0) {
led_1_off();
} else if (count == RAW_SAMPLE_BUFF_SIZE) {
led_1_on();
} else {
led_1_toggle();
}
#endif
for (uint16_t i = 0; i < count; ++i) {
/* Grab a raw accelerometer sample */
struct Sample sample;
bool success = remove_sample(&sample_buffer, &sample);
if (!success) {
#ifdef DEBUG
HANG();
#endif
} else {
// TODO dear god, refactor this...
/* Sample is written on a new line */
if (!add_value_to_buffer(&sd_file, NEW_LINE)) {
return stop_logging();
}
/* Convert delta time to ascii and put in SD card buffer */
{
int32_t delta_time = int8arr_to_uint32(sample.delta_time);
/* Max timestamp value is 8 digits */
uint8_t ascii_buffer[8];
uitoa(delta_time, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 8; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
if (accelerometer.is_enabled) {
/* Add delimiter */
if (!add_value_to_buffer(&sd_file, DELIMITER)) {
return stop_logging();
}
/* Convert axes to ascii and put in SD card buffer */
{
/* Max axis value is 5 digits plus sign */
uint8_t ascii_buffer[6];
/* X-axis */
{
int16_t x_axis = int8arr_to_int16(sample.accel.x_axis);
itoa(x_axis, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 6; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
/* Add delimiter */
if (!add_value_to_buffer(&sd_file, DELIMITER)) {
return stop_logging();
}
/* Y-axis */
{
int16_t y_axis = int8arr_to_int16(sample.accel.y_axis);
itoa(y_axis, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 6; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
/* Add delimiter */
if (!add_value_to_buffer(&sd_file, DELIMITER)) {
return stop_logging();
}
/* Z-axis */
{
int16_t z_axis = int8arr_to_int16(sample.accel.z_axis);
itoa(z_axis, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 6; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
}
}
if (gyroscope.is_enabled) {
/* Add delimiter */
if (!add_value_to_buffer(&sd_file, DELIMITER)) {
return stop_logging();
}
/* Convert axes to ascii and put in SD card buffer */
{
/* Max axis value is 5 digits plus sign */
uint8_t ascii_buffer[6];
/* X-axis */
{
int16_t x_axis = int8arr_to_int16(sample.gyro.x_axis);
itoa(x_axis, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 6; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
/* Add delimiter */
if (!add_value_to_buffer(&sd_file, DELIMITER)) {
return stop_logging();
}
/* Y-axis */
{
int16_t y_axis = int8arr_to_int16(sample.gyro.y_axis);
itoa(y_axis, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 6; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
/* Add delimiter */
if (!add_value_to_buffer(&sd_file, DELIMITER)) {
return stop_logging();
}
/* Z-axis */
{
int16_t z_axis = int8arr_to_int16(sample.gyro.z_axis);
itoa(z_axis, ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 6; ++i) {
if (!add_value_to_buffer(&sd_file, ascii_buffer[i])) {
return stop_logging();
}
}
}
}
}
}
}
/* Check for any button presses */
if (button_press_buffer.count > 0) {
enum ButtonPress button_press;
bool success = remove_button_press(&button_press_buffer, &button_press);
if (!success) {
#ifdef DEBUG
HANG();
#endif
} else {
switch(button_press) {
case BUTTON_TAP:
case BUTTON_HOLD:
return stop_logging();
}
}
}
return LOG_STATE;
}
enum DeviceState format_step(void) {
if (flash_led_at_rate(FORMAT_FLASH_RATE)) {
led_1_weak_flash();
LED_FLASH_DELAY(30000);
led_1_weak_flash();
}
/* Check for any button presses */
if (button_press_buffer.count > 0) {
enum ButtonPress button_press;
bool success = remove_button_press(&button_press_buffer, &button_press);
if (!success) {
#ifdef DEBUG
HANG();
#endif
} else {
/* Perform action based on button press */
switch(button_press) {
case BUTTON_TAP:
return turn_off();
case BUTTON_HOLD:
format_sd_card();
break;
}
}
}
return FORMAT_STATE;
}
void init_sd_fat(void) {
/* Find and read the FAT16 boot sector */
if (valid_boot_sector(data_sd, &fatinfo) != FAT_SUCCESS) {
/* Turn the LED on and hang to indicate failure */
led_1_on();
HANG();
}
/* Parse the FAT16 boot sector */
if (parse_boot_sector(data_sd, &fatinfo) != FAT_SUCCESS) {
/* Show failure with LED 1 */
led_1_panic();
/* Restart upon failure */
restart();
}
}
void format_sd_card(void) {
feed_watchdog();
/* Turn on power to SD card */
power_on_sd();
feed_watchdog();
/* Check for low voltage */
if (voltage_is_low()) {
restart();
}
feed_watchdog();
/* Try to read the boot sector so we can salvage the config file */
if (valid_boot_sector(data_sd, &fatinfo) == FAT_SUCCESS &&
parse_boot_sector(data_sd, &fatinfo) == FAT_SUCCESS) {
} else {
fat_defaults(&fatinfo);
}
/* Formatting takes a while so we need to stop the wdt */
stop_watchdog();
/* Format the SD card, using LED 1 to indicate it's being formatted */
format_sd(data_sd, &fatinfo, led_1_on, led_1_toggle, led_1_off);
restart();
}
void new_sd_card_file(struct SdCardFile *const sd_card_file) {
sd_card_file->start_cluster = find_cluster(sd_card_file->buffer, &fatinfo);
/* The SD card is full */
if (!sd_card_file->start_cluster) {
/* Turn the LED on and hang to indicate failure */
led_1_on();
HANG();
}
sd_card_file->index = 0;
sd_card_file->cluster = sd_card_file->start_cluster;
sd_card_file->block_num = 0;
sd_card_file->size = 0;
/* Firmware info */
add_firmware_info_to_sd_card_file(sd_card_file);
sd_card_file->buffer[sd_card_file->index++] = NEW_LINE;
// feed_watchdog();
/* Sample rate */
sd_card_file->buffer[sd_card_file->index++] = 's';
sd_card_file->buffer[sd_card_file->index++] = 'a';
sd_card_file->buffer[sd_card_file->index++] = 'm';
sd_card_file->buffer[sd_card_file->index++] = 'p';
sd_card_file->buffer[sd_card_file->index++] = 'l';
sd_card_file->buffer[sd_card_file->index++] = 'e';
sd_card_file->buffer[sd_card_file->index++] = '-';
sd_card_file->buffer[sd_card_file->index++] = 'r';
sd_card_file->buffer[sd_card_file->index++] = 'a';
sd_card_file->buffer[sd_card_file->index++] = 't';
sd_card_file->buffer[sd_card_file->index++] = 'e';
sd_card_file->buffer[sd_card_file->index++] = ':';
sd_card_file->buffer[sd_card_file->index++] = ' ';
/* Convert the sample rate to ascii */
{
uint8_t ascii_buffer[3];
itoa(bandwidth_bits_to_hz_accel(accelerometer.bandwidth), ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 3; ++i) {
sd_card_file->buffer[sd_card_file->index++] = ascii_buffer[i];
}
}
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'H';
sd_card_file->buffer[sd_card_file->index++] = 'z';
sd_card_file->buffer[sd_card_file->index++] = NEW_LINE;
// feed_watchdog();
/* Range setting */
if (accelerometer.is_enabled) {
sd_card_file->buffer[sd_card_file->index++] = 'a';
sd_card_file->buffer[sd_card_file->index++] = 'c';
sd_card_file->buffer[sd_card_file->index++] = 'c';
sd_card_file->buffer[sd_card_file->index++] = 'e';
sd_card_file->buffer[sd_card_file->index++] = 'l';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'r';
sd_card_file->buffer[sd_card_file->index++] = 'a';
sd_card_file->buffer[sd_card_file->index++] = 'n';
sd_card_file->buffer[sd_card_file->index++] = 'g';
sd_card_file->buffer[sd_card_file->index++] = 'e';
sd_card_file->buffer[sd_card_file->index++] = ':';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = '+';
sd_card_file->buffer[sd_card_file->index++] = '/';
sd_card_file->buffer[sd_card_file->index++] = '-';
sd_card_file->buffer[sd_card_file->index++] = range_bits_to_g_accel(accelerometer.range) + 0x30;
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'g';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = '(';
sd_card_file->buffer[sd_card_file->index++] = '+';
sd_card_file->buffer[sd_card_file->index++] = '/';
sd_card_file->buffer[sd_card_file->index++] = '-';
sd_card_file->buffer[sd_card_file->index++] = '3';
sd_card_file->buffer[sd_card_file->index++] = '2';
sd_card_file->buffer[sd_card_file->index++] = '7';
sd_card_file->buffer[sd_card_file->index++] = '6';
sd_card_file->buffer[sd_card_file->index++] = '8';
sd_card_file->buffer[sd_card_file->index++] = ')';
sd_card_file->buffer[sd_card_file->index++] = NEW_LINE;
}
if (gyroscope.is_enabled) {
sd_card_file->buffer[sd_card_file->index++] = 'g';
sd_card_file->buffer[sd_card_file->index++] = 'y';
sd_card_file->buffer[sd_card_file->index++] = 'r';
sd_card_file->buffer[sd_card_file->index++] = 'o';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'r';
sd_card_file->buffer[sd_card_file->index++] = 'a';
sd_card_file->buffer[sd_card_file->index++] = 'n';
sd_card_file->buffer[sd_card_file->index++] = 'g';
sd_card_file->buffer[sd_card_file->index++] = 'e';
sd_card_file->buffer[sd_card_file->index++] = ':';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = '+';
sd_card_file->buffer[sd_card_file->index++] = '/';
sd_card_file->buffer[sd_card_file->index++] = '-';
/* Convert the range to ascii */
{
uint8_t ascii_buffer[4];
itoa(range_bits_to_dps_gyro(gyroscope.range), ascii_buffer);
for (uint8_t i = 0; ascii_buffer[i] != NULL_TERMINATOR && i < 4; ++i) {
sd_card_file->buffer[sd_card_file->index++] = ascii_buffer[i];
}
}
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'd';
sd_card_file->buffer[sd_card_file->index++] = 'p';
sd_card_file->buffer[sd_card_file->index++] = 's';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = '(';
sd_card_file->buffer[sd_card_file->index++] = '+';
sd_card_file->buffer[sd_card_file->index++] = '/';
sd_card_file->buffer[sd_card_file->index++] = '-';
sd_card_file->buffer[sd_card_file->index++] = '3';
sd_card_file->buffer[sd_card_file->index++] = '2';
sd_card_file->buffer[sd_card_file->index++] = '7';
sd_card_file->buffer[sd_card_file->index++] = '6';
sd_card_file->buffer[sd_card_file->index++] = '8';
sd_card_file->buffer[sd_card_file->index++] = ')';
sd_card_file->buffer[sd_card_file->index++] = NEW_LINE;
}
/* delta-time units */
sd_card_file->buffer[sd_card_file->index++] = 'd';
sd_card_file->buffer[sd_card_file->index++] = 't';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'u';
sd_card_file->buffer[sd_card_file->index++] = 'n';
sd_card_file->buffer[sd_card_file->index++] = 'i';
sd_card_file->buffer[sd_card_file->index++] = 't';
sd_card_file->buffer[sd_card_file->index++] = 's';
sd_card_file->buffer[sd_card_file->index++] = ':';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = '8';
sd_card_file->buffer[sd_card_file->index++] = '3';
sd_card_file->buffer[sd_card_file->index++] = '.';
sd_card_file->buffer[sd_card_file->index++] = '3';
sd_card_file->buffer[sd_card_file->index++] = '3';
sd_card_file->buffer[sd_card_file->index++] = ' ';
sd_card_file->buffer[sd_card_file->index++] = 'n';
sd_card_file->buffer[sd_card_file->index++] = 's';
sd_card_file->buffer[sd_card_file->index++] = NEW_LINE;
// feed_watchdog();
/* Column titles */
sd_card_file->buffer[sd_card_file->index++] = 'd';
sd_card_file->buffer[sd_card_file->index++] = 't';
if (accelerometer.is_enabled) {
sd_card_file->buffer[sd_card_file->index++] = ',';
sd_card_file->buffer[sd_card_file->index++] = 'a';
sd_card_file->buffer[sd_card_file->index++] = 'c';
sd_card_file->buffer[sd_card_file->index++] = 'c';
sd_card_file->buffer[sd_card_file->index++] = 'e';
sd_card_file->buffer[sd_card_file->index++] = 'l';
sd_card_file->buffer[sd_card_file->index++] = '(';
sd_card_file->buffer[sd_card_file->index++] = 'x';
sd_card_file->buffer[sd_card_file->index++] = ',';
sd_card_file->buffer[sd_card_file->index++] = 'y';
sd_card_file->buffer[sd_card_file->index++] = ',';
sd_card_file->buffer[sd_card_file->index++] = 'z';
sd_card_file->buffer[sd_card_file->index++] = ')';
}
if (gyroscope.is_enabled) {
sd_card_file->buffer[sd_card_file->index++] = ',';
sd_card_file->buffer[sd_card_file->index++] = 'g';
sd_card_file->buffer[sd_card_file->index++] = 'y';
sd_card_file->buffer[sd_card_file->index++] = 'r';
sd_card_file->buffer[sd_card_file->index++] = 'o';
sd_card_file->buffer[sd_card_file->index++] = '(';
sd_card_file->buffer[sd_card_file->index++] = 'x';
sd_card_file->buffer[sd_card_file->index++] = ',';
sd_card_file->buffer[sd_card_file->index++] = 'y';
sd_card_file->buffer[sd_card_file->index++] = ',';
sd_card_file->buffer[sd_card_file->index++] = 'z';
sd_card_file->buffer[sd_card_file->index++] = ')';
}
}
void add_firmware_info_to_sd_card_file(struct SdCardFile *const sd_card_file) {
uint8_t name[] = FIRMWARE_NAME;
uint8_t version[] = FIRMWARE_VERSION;
/* Add firmware name */
for (uint8_t i = 0; name[i] != NULL_TERMINATOR; ++i) {
sd_card_file->buffer[sd_card_file->index++] = name[i];
}
sd_card_file->buffer[sd_card_file->index++] = ' ';
/* Add firmware version */
sd_card_file->buffer[sd_card_file->index++] = 'v';
for (uint8_t i = 0; version[i] != NULL_TERMINATOR; ++i) {
sd_card_file->buffer[sd_card_file->index++] = version[i];
}
}
uint32_t get_block_offset(const struct SdCardFile *const sd_card_file) {
uint32_t block_offset = sd_card_file->block_num;
block_offset *= BLKSIZE;
block_offset += get_cluster_offset(sd_card_file->cluster, &fatinfo);
return block_offset;
}