cdms n bae

Dependencies:   SDFileSystem mbed-rtos mbed

Fork of CDMS_RTOS by Pradeep Kotipalli

Committer:
pradeepvk2208
Date:
Thu Feb 05 22:32:29 2015 +0000
Revision:
6:ee97f01e6b7d
Child:
7:60e940b7a36b
CDMS with hk

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pradeepvk2208 6:ee97f01e6b7d 1 #include "hk.h"
pradeepvk2208 6:ee97f01e6b7d 2 SPI spi(PTD6, PTD7, PTD5);
pradeepvk2208 6:ee97f01e6b7d 3 DigitalOut cs(D6);
pradeepvk2208 6:ee97f01e6b7d 4
pradeepvk2208 6:ee97f01e6b7d 5 SDFileSystem sd(PTD6, PTD7, PTD5, D7,"sd");
pradeepvk2208 6:ee97f01e6b7d 6
pradeepvk2208 6:ee97f01e6b7d 7 Timer t1;
pradeepvk2208 6:ee97f01e6b7d 8 DigitalOut interrupt(D9);
pradeepvk2208 6:ee97f01e6b7d 9 const int addr = 0x20;
pradeepvk2208 6:ee97f01e6b7d 10 bool ack0;
pradeepvk2208 6:ee97f01e6b7d 11 I2C master (D14,D15);
pradeepvk2208 6:ee97f01e6b7d 12
pradeepvk2208 6:ee97f01e6b7d 13 void spiwrite(int a)
pradeepvk2208 6:ee97f01e6b7d 14 {
pradeepvk2208 6:ee97f01e6b7d 15 cs=1;
pradeepvk2208 6:ee97f01e6b7d 16 cs=0;
pradeepvk2208 6:ee97f01e6b7d 17 spi.write(a);
pradeepvk2208 6:ee97f01e6b7d 18 spi.write(0x01);
pradeepvk2208 6:ee97f01e6b7d 19 }
pradeepvk2208 6:ee97f01e6b7d 20
pradeepvk2208 6:ee97f01e6b7d 21 int spiread(int a)
pradeepvk2208 6:ee97f01e6b7d 22 {
pradeepvk2208 6:ee97f01e6b7d 23 cs=1;
pradeepvk2208 6:ee97f01e6b7d 24 cs=0;
pradeepvk2208 6:ee97f01e6b7d 25 spi.write(a);
pradeepvk2208 6:ee97f01e6b7d 26 return(spi.write(0x00));
pradeepvk2208 6:ee97f01e6b7d 27 }
pradeepvk2208 6:ee97f01e6b7d 28 char* getname(int year1,int month1,int date1,int day1,int hours1,int minutes1,int seconds1)
pradeepvk2208 6:ee97f01e6b7d 29 {
pradeepvk2208 6:ee97f01e6b7d 30 year1= hexint(year1);
pradeepvk2208 6:ee97f01e6b7d 31 month1=hexint(month1);
pradeepvk2208 6:ee97f01e6b7d 32 date1=hexint(date1);
pradeepvk2208 6:ee97f01e6b7d 33 day1=hexint(day1);
pradeepvk2208 6:ee97f01e6b7d 34 hours1=hexint(hours1);
pradeepvk2208 6:ee97f01e6b7d 35 minutes1=hexint(minutes1);
pradeepvk2208 6:ee97f01e6b7d 36 seconds1=hexint(seconds1);
pradeepvk2208 6:ee97f01e6b7d 37 char time[15];
pradeepvk2208 6:ee97f01e6b7d 38 sprintf(time,"%02d%02d%02d%02d%02d%02d%02d",year1,month1,date1,day1,hours1,minutes1,seconds1);
pradeepvk2208 6:ee97f01e6b7d 39
pradeepvk2208 6:ee97f01e6b7d 40 return(time);
pradeepvk2208 6:ee97f01e6b7d 41
pradeepvk2208 6:ee97f01e6b7d 42 }
pradeepvk2208 6:ee97f01e6b7d 43
pradeepvk2208 6:ee97f01e6b7d 44 int hexint(int a)
pradeepvk2208 6:ee97f01e6b7d 45 {
pradeepvk2208 6:ee97f01e6b7d 46 a=(a/16)*10+(a%16);
pradeepvk2208 6:ee97f01e6b7d 47 return a;
pradeepvk2208 6:ee97f01e6b7d 48 }
pradeepvk2208 6:ee97f01e6b7d 49 //storedata stores the dummy structure in the file with timestamp as the filename in HK directory
pradeepvk2208 6:ee97f01e6b7d 50 void init_rtc(void)
pradeepvk2208 6:ee97f01e6b7d 51 {
pradeepvk2208 6:ee97f01e6b7d 52 spi.format(8,3);
pradeepvk2208 6:ee97f01e6b7d 53 spi.frequency(1000000);
pradeepvk2208 6:ee97f01e6b7d 54 spiwrite(0x80); //write seconds to 01
pradeepvk2208 6:ee97f01e6b7d 55 spiwrite(0x81); //write minutes t0 01
pradeepvk2208 6:ee97f01e6b7d 56 spiwrite(0x82); //write hours to 01
pradeepvk2208 6:ee97f01e6b7d 57 spiwrite(0x83); //write day of week to 01
pradeepvk2208 6:ee97f01e6b7d 58 spiwrite(0x84); //write day of month to 01
pradeepvk2208 6:ee97f01e6b7d 59 spiwrite(0x85); //write month to 01
pradeepvk2208 6:ee97f01e6b7d 60 spiwrite(0x86); //write year to 01
pradeepvk2208 6:ee97f01e6b7d 61 }
pradeepvk2208 6:ee97f01e6b7d 62 void storedata(char * data,int length)
pradeepvk2208 6:ee97f01e6b7d 63 {
pradeepvk2208 6:ee97f01e6b7d 64
pradeepvk2208 6:ee97f01e6b7d 65 ack0 =true;
pradeepvk2208 6:ee97f01e6b7d 66
pradeepvk2208 6:ee97f01e6b7d 67 interrupt = 1;
pradeepvk2208 6:ee97f01e6b7d 68 t1.start();
pradeepvk2208 6:ee97f01e6b7d 69 //wait_ms(20);
pradeepvk2208 6:ee97f01e6b7d 70 ack0 = master.read(addr|1,data,length);
pradeepvk2208 6:ee97f01e6b7d 71 t1.stop();
pradeepvk2208 6:ee97f01e6b7d 72
pradeepvk2208 6:ee97f01e6b7d 73
pradeepvk2208 6:ee97f01e6b7d 74 if(!ack0)
pradeepvk2208 6:ee97f01e6b7d 75 {
pradeepvk2208 6:ee97f01e6b7d 76 printf("\n master has read %s from slave\n\r",data);
pradeepvk2208 6:ee97f01e6b7d 77
pradeepvk2208 6:ee97f01e6b7d 78 }
pradeepvk2208 6:ee97f01e6b7d 79 //master.stop();
pradeepvk2208 6:ee97f01e6b7d 80 printf("\n%d\n\r",t1.read_us());
pradeepvk2208 6:ee97f01e6b7d 81 t1.reset();
pradeepvk2208 6:ee97f01e6b7d 82
pradeepvk2208 6:ee97f01e6b7d 83 interrupt=0;
pradeepvk2208 6:ee97f01e6b7d 84 spi.format(8,3);
pradeepvk2208 6:ee97f01e6b7d 85 spi.frequency(1000000);
pradeepvk2208 6:ee97f01e6b7d 86
pradeepvk2208 6:ee97f01e6b7d 87 int seconds=spiread(0x00); //read seconds
pradeepvk2208 6:ee97f01e6b7d 88 int minutes =spiread(0x01); //read minutes
pradeepvk2208 6:ee97f01e6b7d 89 int hours =spiread(0x02); //read hours
pradeepvk2208 6:ee97f01e6b7d 90 int day =spi.write(0x03); //read day of the week
pradeepvk2208 6:ee97f01e6b7d 91 int date =spiread(0x04); //read day of the month
pradeepvk2208 6:ee97f01e6b7d 92 int month =spiread(0x05); //read month
pradeepvk2208 6:ee97f01e6b7d 93 int year =spiread(0x06); //read year
pradeepvk2208 6:ee97f01e6b7d 94 cs = 1;
pradeepvk2208 6:ee97f01e6b7d 95
pradeepvk2208 6:ee97f01e6b7d 96 //Assigning dummy values to the structure
pradeepvk2208 6:ee97f01e6b7d 97
pradeepvk2208 6:ee97f01e6b7d 98 /* SensorData Sensor;
pradeepvk2208 6:ee97f01e6b7d 99
pradeepvk2208 6:ee97f01e6b7d 100 printf("Writing dummy values\n");
pradeepvk2208 6:ee97f01e6b7d 101 strcpy( Sensor.Voltage, "49");
pradeepvk2208 6:ee97f01e6b7d 102 strcpy( Sensor.Current, "83");
pradeepvk2208 6:ee97f01e6b7d 103 Sensor.Temperature ='5';
pradeepvk2208 6:ee97f01e6b7d 104 strcpy( Sensor.PanelTemperature, "4");
pradeepvk2208 6:ee97f01e6b7d 105 Sensor.Vcell_soc='9';
pradeepvk2208 6:ee97f01e6b7d 106 Sensor.alerts= '4';
pradeepvk2208 6:ee97f01e6b7d 107 Sensor.crate='7';
pradeepvk2208 6:ee97f01e6b7d 108 Sensor.BatteryTemperature='6';
pradeepvk2208 6:ee97f01e6b7d 109 Sensor.faultpoll='4';
pradeepvk2208 6:ee97f01e6b7d 110 Sensor.faultir='g';
pradeepvk2208 6:ee97f01e6b7d 111 Sensor.power_mode='k';
pradeepvk2208 6:ee97f01e6b7d 112 strcpy( Sensor.AngularSpeed, "9");
pradeepvk2208 6:ee97f01e6b7d 113 strcpy(Sensor.Bnewvalue,"6");
pradeepvk2208 6:ee97f01e6b7d 114 printf("Done writing dummy values\n"); */
pradeepvk2208 6:ee97f01e6b7d 115 mkdir("/sd/hk", 0777);
pradeepvk2208 6:ee97f01e6b7d 116 char date2[100]="/sd/hk/";
pradeepvk2208 6:ee97f01e6b7d 117 strcat(date2,getname(year,month,date,day,hours,minutes,seconds));
pradeepvk2208 6:ee97f01e6b7d 118 strcat(date2,".txt");
pradeepvk2208 6:ee97f01e6b7d 119 FILE *fp ;
pradeepvk2208 6:ee97f01e6b7d 120 fp= fopen(date2, "w");
pradeepvk2208 6:ee97f01e6b7d 121 if(fp == NULL)
pradeepvk2208 6:ee97f01e6b7d 122 {
pradeepvk2208 6:ee97f01e6b7d 123 error("Could not open file for write\n");
pradeepvk2208 6:ee97f01e6b7d 124 }
pradeepvk2208 6:ee97f01e6b7d 125 else
pradeepvk2208 6:ee97f01e6b7d 126 {
pradeepvk2208 6:ee97f01e6b7d 127 fprintf(fp, "%s ", data);
pradeepvk2208 6:ee97f01e6b7d 128 fclose(fp);
pradeepvk2208 6:ee97f01e6b7d 129 printf("%s",getname(year,month,date,day,hours,minutes,seconds));
pradeepvk2208 6:ee97f01e6b7d 130 }
pradeepvk2208 6:ee97f01e6b7d 131 }