Austins SD card logger

Dependencies:   mbed SDFileSystem

Committer:
austinbrown124
Date:
Wed Apr 17 06:36:24 2019 +0000
Revision:
3:8d4c12a3b5d4
Parent:
2:7bb016846566
Child:
4:4bd2b85412c3
Next Commit;

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 3:8d4c12a3b5d4 21 //#define PAGE_SIZE 4096
austinbrown124 3:8d4c12a3b5d4 22 #define PAGE_SIZE 2048
austinbrown124 2:7bb016846566 23 #define HEADER_SIZE 10
austinbrown124 2:7bb016846566 24 #define CHUNK_SIZE (PAGE_SIZE-HEADER_SIZE)
austinbrown124 2:7bb016846566 25 #define BUF_SIZE (16*PAGE_SIZE)
austinbrown124 2:7bb016846566 26
austinbrown124 2:7bb016846566 27 //RTC
austinbrown124 2:7bb016846566 28 #define SET_TIME false
austinbrown124 2:7bb016846566 29
austinbrown124 2:7bb016846566 30 int start = 0, end = 0;
austinbrown124 2:7bb016846566 31 unsigned char buf[BUF_SIZE];
austinbrown124 2:7bb016846566 32 unsigned char hdr[HEADER_SIZE] = {0xff, 0xff,'M','A','N','B','L','O','R','T'};
austinbrown124 2:7bb016846566 33
austinbrown124 2:7bb016846566 34 /*UART*/
austinbrown124 2:7bb016846566 35 #define BAUD_RATE 256000
austinbrown124 2:7bb016846566 36 //#define BAUD_RATE 9600
austinbrown124 2:7bb016846566 37 //Serial serial4(PA_11, D9);
austinbrown124 2:7bb016846566 38 Serial serial4(PA_2, PA_3);
austinbrown124 2:7bb016846566 39 Serial pc(PA_9, PA_10);
austinbrown124 2:7bb016846566 40
austinbrown124 2:7bb016846566 41 /*Test pins*/
austinbrown124 2:7bb016846566 42 DigitalOut test(PC_0);
austinbrown124 2:7bb016846566 43 DigitalOut test2(PC_2);
austinbrown124 2:7bb016846566 44 int flag=0, flag2=0;
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 3:8d4c12a3b5d4 60 test = 1;
austinbrown124 3:8d4c12a3b5d4 61
austinbrown124 3:8d4c12a3b5d4 62 //buf[0] = 1;
austinbrown124 2:7bb016846566 63
austinbrown124 2:7bb016846566 64 /*init SD card, wait for card insertion*/
austinbrown124 2:7bb016846566 65 pc.printf("\n\rconfiging SD card \n\r");
austinbrown124 2:7bb016846566 66 SDFileSystem sd(DI, DO, SCK, CS, "sd");
austinbrown124 3:8d4c12a3b5d4 67 pc.printf("\n\r trying status \n\r");
austinbrown124 2:7bb016846566 68 while (sd.disk_status()) {
austinbrown124 2:7bb016846566 69 sd.disk_initialize();
austinbrown124 2:7bb016846566 70 wait(0.5);
austinbrown124 3:8d4c12a3b5d4 71 pc.printf("nope \r");
austinbrown124 2:7bb016846566 72 }
austinbrown124 3:8d4c12a3b5d4 73 pc.printf("disk initialized \r");
austinbrown124 2:7bb016846566 74
austinbrown124 2:7bb016846566 75 if (SET_TIME) {set_time(1526496200);}
austinbrown124 3:8d4c12a3b5d4 76
austinbrown124 3:8d4c12a3b5d4 77 time_t seconds = 0;//time(NULL);
austinbrown124 2:7bb016846566 78 pc.printf("Time as seconds since January 1, 1970 = %d\n\r", seconds);
austinbrown124 2:7bb016846566 79
austinbrown124 2:7bb016846566 80
austinbrown124 2:7bb016846566 81
austinbrown124 2:7bb016846566 82 strcpy(fname, "/sd/log_");
mbed_official 0:bdbd3d6fc5d5 83
austinbrown124 2:7bb016846566 84 sprintf(current_time, "%u", seconds );
austinbrown124 2:7bb016846566 85 strcat(fname, current_time);
austinbrown124 2:7bb016846566 86 strcat(fname, ".dat");
austinbrown124 2:7bb016846566 87 pc.printf("Logging to %s\n", fname);
mbed_official 0:bdbd3d6fc5d5 88
austinbrown124 2:7bb016846566 89 FILE *fp = fopen(fname, "wb");
austinbrown124 2:7bb016846566 90
austinbrown124 2:7bb016846566 91
austinbrown124 2:7bb016846566 92 /*init serial*/
austinbrown124 2:7bb016846566 93 serial4.baud(BAUD_RATE);
austinbrown124 2:7bb016846566 94 serial4.attach(rx_callback);
austinbrown124 2:7bb016846566 95
austinbrown124 2:7bb016846566 96 //while (1==1) {pc.prinf("%i",end);}
austinbrown124 2:7bb016846566 97
austinbrown124 2:7bb016846566 98 for (;;) {
austinbrown124 2:7bb016846566 99 int length = end - start;
austinbrown124 2:7bb016846566 100 if (length < 0) length += BUF_SIZE;
austinbrown124 2:7bb016846566 101 if (length >= CHUNK_SIZE) {
austinbrown124 2:7bb016846566 102 //fwrite(hdr, 1, HEADER_SIZE, fp);
austinbrown124 2:7bb016846566 103 if (start+CHUNK_SIZE-1 < BUF_SIZE) {
austinbrown124 2:7bb016846566 104 fwrite(buf+start, 1, CHUNK_SIZE, fp);
austinbrown124 2:7bb016846566 105 } else {
austinbrown124 2:7bb016846566 106 fwrite(buf+start, 1, BUF_SIZE-start+1, fp);
austinbrown124 2:7bb016846566 107 fwrite(buf, 1, CHUNK_SIZE-(BUF_SIZE-start+1), fp);
austinbrown124 2:7bb016846566 108 }
austinbrown124 2:7bb016846566 109 fflush(fp);
austinbrown124 2:7bb016846566 110 start += CHUNK_SIZE;
austinbrown124 2:7bb016846566 111 if (start >= BUF_SIZE) start -= BUF_SIZE;
austinbrown124 2:7bb016846566 112 }
mbed_official 0:bdbd3d6fc5d5 113 }
austinbrown124 2:7bb016846566 114 }
mbed_official 0:bdbd3d6fc5d5 115
austinbrown124 2:7bb016846566 116