Austins SD card logger

Dependencies:   mbed SDFileSystem

Committer:
austinbrown124
Date:
Tue Apr 16 04:30:26 2019 +0000
Revision:
2:7bb016846566
Parent:
0:bdbd3d6fc5d5
Child:
3:8d4c12a3b5d4
No idea what this does

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:bdbd3d6fc5d5 1 #include "mbed.h"
mbed_official 0:bdbd3d6fc5d5 2 #include "SDFileSystem.h"
austinbrown124 2:7bb016846566 3 #include "string.h"
mbed_official 0:bdbd3d6fc5d5 4
austinbrown124 2:7bb016846566 5 /*SD card*/
austinbrown124 2:7bb016846566 6 //#define DI D11
austinbrown124 2:7bb016846566 7 //#define DO D12
austinbrown124 2:7bb016846566 8 //#define SCK D13
austinbrown124 2:7bb016846566 9 //#define CS D10
austinbrown124 2:7bb016846566 10 #define DI PB_5
austinbrown124 2:7bb016846566 11 #define DO PB_4
austinbrown124 2:7bb016846566 12 #define SCK PB_3
austinbrown124 2:7bb016846566 13 #define CS PB_6
austinbrown124 2:7bb016846566 14
austinbrown124 2:7bb016846566 15
austinbrown124 2:7bb016846566 16 char fname[255];
austinbrown124 2:7bb016846566 17 char current_time[64];
austinbrown124 2:7bb016846566 18
austinbrown124 2:7bb016846566 19
austinbrown124 2:7bb016846566 20 /*Serial buffer*/
austinbrown124 2:7bb016846566 21 #define PAGE_SIZE 4096
austinbrown124 2:7bb016846566 22 #define HEADER_SIZE 10
austinbrown124 2:7bb016846566 23 #define CHUNK_SIZE (PAGE_SIZE-HEADER_SIZE)
austinbrown124 2:7bb016846566 24 #define BUF_SIZE (16*PAGE_SIZE)
austinbrown124 2:7bb016846566 25
austinbrown124 2:7bb016846566 26 //RTC
austinbrown124 2:7bb016846566 27 #define SET_TIME false
austinbrown124 2:7bb016846566 28
austinbrown124 2:7bb016846566 29 int start = 0, end = 0;
austinbrown124 2:7bb016846566 30 unsigned char buf[BUF_SIZE];
austinbrown124 2:7bb016846566 31 unsigned char hdr[HEADER_SIZE] = {0xff, 0xff,'M','A','N','B','L','O','R','T'};
austinbrown124 2:7bb016846566 32
austinbrown124 2:7bb016846566 33 /*UART*/
austinbrown124 2:7bb016846566 34 #define BAUD_RATE 256000
austinbrown124 2:7bb016846566 35 //#define BAUD_RATE 9600
austinbrown124 2:7bb016846566 36 //Serial serial4(PA_11, D9);
austinbrown124 2:7bb016846566 37 Serial serial4(PA_2, PA_3);
austinbrown124 2:7bb016846566 38 Serial pc(PA_9, PA_10);
austinbrown124 2:7bb016846566 39
austinbrown124 2:7bb016846566 40 /*Test pins*/
austinbrown124 2:7bb016846566 41 DigitalOut test(PC_0);
austinbrown124 2:7bb016846566 42 DigitalOut test2(PC_2);
austinbrown124 2:7bb016846566 43 int flag=0, flag2=0;
austinbrown124 2:7bb016846566 44
austinbrown124 2:7bb016846566 45
austinbrown124 2:7bb016846566 46
austinbrown124 2:7bb016846566 47
austinbrown124 2:7bb016846566 48
austinbrown124 2:7bb016846566 49 void rx_callback() {
austinbrown124 2:7bb016846566 50 while(serial4.readable()) {
austinbrown124 2:7bb016846566 51 buf[end] = serial4.getc();
austinbrown124 2:7bb016846566 52 //pc.printf("%c",buf[end]);
austinbrown124 2:7bb016846566 53 end++;
austinbrown124 2:7bb016846566 54 if (end == BUF_SIZE) end = 0;
austinbrown124 2:7bb016846566 55 }
austinbrown124 2:7bb016846566 56 }
mbed_official 0:bdbd3d6fc5d5 57
mbed_official 0:bdbd3d6fc5d5 58 int main() {
austinbrown124 2:7bb016846566 59 pc.baud(256000);
austinbrown124 2:7bb016846566 60
austinbrown124 2:7bb016846566 61 /*init SD card, wait for card insertion*/
austinbrown124 2:7bb016846566 62 pc.printf("\n\rconfiging SD card \n\r");
austinbrown124 2:7bb016846566 63 SDFileSystem sd(DI, DO, SCK, CS, "sd");
austinbrown124 2:7bb016846566 64
austinbrown124 2:7bb016846566 65 while (sd.disk_status()) {
austinbrown124 2:7bb016846566 66 sd.disk_initialize();
austinbrown124 2:7bb016846566 67 wait(0.5);
austinbrown124 2:7bb016846566 68 }
austinbrown124 2:7bb016846566 69 pc.printf("disk initialized \n\r");
austinbrown124 2:7bb016846566 70
austinbrown124 2:7bb016846566 71 if (SET_TIME) {set_time(1526496200);}
austinbrown124 2:7bb016846566 72
austinbrown124 2:7bb016846566 73 time_t seconds = time(NULL);
austinbrown124 2:7bb016846566 74 pc.printf("Time as seconds since January 1, 1970 = %d\n\r", seconds);
austinbrown124 2:7bb016846566 75
austinbrown124 2:7bb016846566 76
austinbrown124 2:7bb016846566 77
austinbrown124 2:7bb016846566 78 strcpy(fname, "/sd/log_");
mbed_official 0:bdbd3d6fc5d5 79
austinbrown124 2:7bb016846566 80 sprintf(current_time, "%u", seconds );
austinbrown124 2:7bb016846566 81 strcat(fname, current_time);
austinbrown124 2:7bb016846566 82 strcat(fname, ".dat");
austinbrown124 2:7bb016846566 83 pc.printf("Logging to %s\n", fname);
mbed_official 0:bdbd3d6fc5d5 84
austinbrown124 2:7bb016846566 85 FILE *fp = fopen(fname, "wb");
austinbrown124 2:7bb016846566 86
austinbrown124 2:7bb016846566 87
austinbrown124 2:7bb016846566 88 /*init serial*/
austinbrown124 2:7bb016846566 89 serial4.baud(BAUD_RATE);
austinbrown124 2:7bb016846566 90 serial4.attach(rx_callback);
austinbrown124 2:7bb016846566 91
austinbrown124 2:7bb016846566 92 //while (1==1) {pc.prinf("%i",end);}
austinbrown124 2:7bb016846566 93
austinbrown124 2:7bb016846566 94 for (;;) {
austinbrown124 2:7bb016846566 95 int length = end - start;
austinbrown124 2:7bb016846566 96 if (length < 0) length += BUF_SIZE;
austinbrown124 2:7bb016846566 97 if (length >= CHUNK_SIZE) {
austinbrown124 2:7bb016846566 98 //fwrite(hdr, 1, HEADER_SIZE, fp);
austinbrown124 2:7bb016846566 99 if (start+CHUNK_SIZE-1 < BUF_SIZE) {
austinbrown124 2:7bb016846566 100 fwrite(buf+start, 1, CHUNK_SIZE, fp);
austinbrown124 2:7bb016846566 101 } else {
austinbrown124 2:7bb016846566 102 fwrite(buf+start, 1, BUF_SIZE-start+1, fp);
austinbrown124 2:7bb016846566 103 fwrite(buf, 1, CHUNK_SIZE-(BUF_SIZE-start+1), fp);
austinbrown124 2:7bb016846566 104 }
austinbrown124 2:7bb016846566 105 fflush(fp);
austinbrown124 2:7bb016846566 106 start += CHUNK_SIZE;
austinbrown124 2:7bb016846566 107 if (start >= BUF_SIZE) start -= BUF_SIZE;
austinbrown124 2:7bb016846566 108 }
mbed_official 0:bdbd3d6fc5d5 109 }
austinbrown124 2:7bb016846566 110 }
mbed_official 0:bdbd3d6fc5d5 111
austinbrown124 2:7bb016846566 112