for checking i2c with integrated bae

Dependencies:   SDFileSystem mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
viswa_chaitanya
Date:
Mon Sep 15 14:01:49 2014 +0000
Commit message:
cdms master(hk runs every 20s)

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
hk.cpp Show annotated file Show diff for this revision Revisions of this file
hk.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
master.cpp Show annotated file Show diff for this revision Revisions of this file
master.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
science.cpp Show annotated file Show diff for this revision Revisions of this file
science.h Show annotated file Show diff for this revision Revisions of this file
sdcard.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.h Show annotated file Show diff for this revision Revisions of this file
tmtc.cpp Show annotated file Show diff for this revision Revisions of this file
tmtc.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hk.cpp	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,25 @@
+#include "hk.h"
+#include "sdcard.h"
+//#include "master.h"
+
+void func_hk_recieve() {
+    while(1){
+        wait(0.5);
+        FUNC_I2C_MASTER_MAIN(1,0x20,24);
+        wait(0.5);
+    }
+}
+
+/*void func_hk_write() {
+    SensorData Sensor;
+    time_t seconds = time(NULL);    //seconds has unix standard time
+    
+    char beaname[21] = "/sd/beahk/";
+    char acsname[21] = "/sd/acshk/";
+    char epsname[21] = "/sd/epshk/";
+    
+    writedummies(&Sensor);
+    setfilenames(beaname, acsname, epsname, seconds);
+    writevaluesf(beaname, acsname, epsname, seconds, &Sensor);
+    readvaluesff(beaname, acsname, epsname, &Sensor);
+}*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hk.h	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,11 @@
+#include "mbed.h"
+#include "master.h"
+
+//void t_hk_main(void const *args);   //this function initiates the thread
+
+//void func_hk_show();    //debug only. Shows hk data on pc screen
+
+//void func_hk_signal_wait();
+//void func_hk_cmd_bae();
+void func_hk_recieve();
+//void func_hk_write();
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,103 @@
+#include "mbed.h"
+#include "rtos.h"
+#include "master.h"
+
+//#include "tmtc.h"
+//#include "science.h"
+//#include "hk.h"
+ 
+#define intmax 11999
+
+Thread* t_tmtc;
+Thread* t_science_store_sd;
+Thread* t_hk_store_sd;
+
+void t_tmtc_main(void const *args);
+void t_science_main(void const *args);
+void t_hk_main(void const *args);
+void scheduler(void const * args);
+
+Timer timer_main;
+int schedcount = 1;
+Mutex critical;
+ 
+int main() {
+    Thread thread_tmtc(t_tmtc_main, NULL, osPriorityIdle);
+    t_tmtc = &thread_tmtc;
+    Thread thread_science(t_science_main, NULL, osPriorityLow);
+    t_science_store_sd = &thread_science;
+    Thread thread_hk(t_hk_main, NULL, osPriorityBelowNormal);
+    t_hk_store_sd = &thread_hk;
+        
+    RtosTimer schedule(scheduler,osTimerPeriodic);
+    timer_main.start();
+    schedule.start(5000);
+    Thread::wait(osWaitForever);
+}
+
+void scheduler(void const * args)
+{
+    if(schedcount == intmax+1)                              //the value is reset at this value so as to ensure smooth flow, 65532 and 0 are divisible by 3 and 2.
+    {
+        schedcount =0;
+    }
+         
+    if(schedcount%4==0)
+    {
+        printf("\nHK signal at %f\n",timer_main.read());
+        t_hk_store_sd->signal_set(0x01);
+    }
+    
+    if(schedcount%3==0)
+    {
+        printf("\nScience signal at %f\n",timer_main.read());
+        t_science_store_sd->signal_set(0x01);
+    }
+    
+    if(schedcount==5)
+    {
+        printf("\nTMTC signalled");
+        t_tmtc->signal_set(0x1);
+    }
+    
+    schedcount++;
+}
+
+void t_science_main(void const *args) {
+    while (true) {
+        Thread::signal_wait(0x1);       
+        critical.lock();
+        printf("science data aquisition started\n");
+        wait(3);
+        printf("science data acquisition ended\n");
+        t_hk_store_sd->signal_set(0x10);
+        critical.unlock();
+        wait(3);
+        printf("Science data written to SD card\n");
+    }
+}
+
+void t_hk_main(void const *args) {
+    while (true) {
+        Thread::signal_wait(0x1);
+        critical.lock();
+        printf("HK acquisition started\n");
+        //FUNC_I2C_MASTER_MAIN(1,0x20,24 );
+        wait(2);
+        printf("HK acquisition ended\n");
+        critical.unlock();
+        wait(2);
+        printf("HK data written to SD card");
+    }
+}
+
+void t_tmtc_main(void const *args) {
+    Thread::signal_wait(0x1);
+    critical.lock();
+    printf("tmtc started\n");
+    wait(5);
+    printf("tmtc ended\n");
+    critical.unlock();
+    wait(5);
+    printf("tmtc executed\n");
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master.cpp	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,76 @@
+#include "master.h"
+ 
+I2C master (A4,A5);                      //configure pins p27,p28 as I2C master
+Serial pc (USBTX,USBRX);
+SensorData Sensor;
+  
+void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations)
+{
+    //wait(0.5);
+    printf("\nmaster entered\n");
+    int acknowledge1;
+    int acknowledge2;
+    uint8_t loopvariable1=1,loopvariable2=0;
+    int addr = slave_address;
+    while(loopvariable1)
+    {
+        printf("\nmaster entered loop \n");
+//-------------writing the command to slave--------------------------------------------------------------        
+        master.frequency(100000);                       //set clock frequency
+        master.start();                                 //initiating the data transfer
+        acknowledge2=master.write(addr|0x00);           //addressing the slave to write
+        if(acknowledge2==1)                             //proceeding further only if slave is addressed
+        {
+   //       printf("\nmaster entered 1 \n");
+        acknowledge1=master.write(command);         //sending the command to slave 
+      if(acknowledge1==1)                             //proceeding further only if sent data is acknowledged   
+           {
+               pc.printf("acknowledge1=%d\n",acknowledge1);
+               loopvariable1=0;                         //if acknowledged, breaking loop in next iteration
+//--------------reading data from slave---------------------------------------------------------------          
+               
+                    master.frequency(100000);                    //set clock frequency 
+                    master.start();                              //initiate data transfer 
+                    acknowledge1 = master.write(addr | 0x01);    //addressing the slave to read
+                    
+                    if(acknowledge1==1)                          //proceedong only if slae is addressed
+                       {  
+                       while(loopvariable2<iterations)
+                        { 
+                          if(loopvariable2%3==0)
+                            {
+                              Sensor.voltage[loopvariable2/3] =  receive_byte();              //receiving data if acknowledged
+                              pc.printf(" voltage%d = %u\n",loopvariable2/3,Sensor.voltage[loopvariable2/3]);
+                            }
+                          else if(loopvariable2%3==1)
+                            {  
+                              Sensor.current[loopvariable2/3] =  receive_byte();             //receiving data if acknowledged
+                              pc.printf(" current%d = %u\n",loopvariable2/3, Sensor.current[loopvariable2/3]);
+                            }
+                          else if(loopvariable2%3==2)
+                            {
+                              Sensor.temp[loopvariable2/3] =  receive_byte();                //receiving data if acknowledged
+                              pc.printf(" temperature%d = %u\n",loopvariable2/3,Sensor.temp[loopvariable2/3]);
+                            }
+                          loopvariable2++;
+                          
+                         } //while(loopvariable2<30) 
+                       }//if(acknowledge1==1)
+                 
+              master.stop();
+           }//if(acknowledge1==1)   
+        }//if(acknowledge2==1) 
+     }//while(loopvariable1)
+   pc.printf("done");
+printf("\nMaster exited\n");
+}//main
+ 
+ 
+//----------------function to read and return the data received-----------------------------------
+char receive_byte()                                    
+{
+   
+    char value;
+    value = master.read(1);                     
+    return(value);                              //returning the 4 byte floating point number  
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master.h	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,22 @@
+#include "mbed.h"                          //library                                              //
+#define get_hk_data 1                      //sample telecommands
+#define telecommand2 2
+#define telecommand3 3
+#define telecommand4 '4'
+#define telecommand5 '5'
+#define telecommand6 '6'
+#define telecommand7 '7'
+#define telecommand8 '8'
+#define telecommand9 '9' 
+#define telecommand10 '10'
+
+char receive_byte();                      //funtion to receive float data 
+ 
+ 
+struct SensorData                          //HK_data_structure
+{
+    char voltage[10];
+    char current[10];
+    char temp[10];
+} ;
+void FUNC_I2C_MASTER_MAIN(int command, int slave_address, int iterations);
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/mbed_official/code/mbed-rtos/#1520481a219c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6213f644d804
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/science.cpp	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,1 @@
+#include "science.h"
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/science.h	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,11 @@
+//#include "mbed.h"
+//#include "rtos.h"
+
+void t_science_main(void const *args);
+
+void func_science_signalwait0();
+void func_science_cmd_pyld();
+void func_science_recieve();
+void func_science_write_coarse();
+void func_science_process_fine();
+void func_science_write_fine();
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sdcard.cpp	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,109 @@
+#include "sdcard.h"
+
+/*SDFileSystem sd(p5, p6, p7, p8, "sd"); // mosi, miso, clk, cs, name
+
+void writedummies(SensorData* Sensor){
+    int i;
+    printf("Writing dummy values\n");    //Debug statement
+    for(i=0; i<10; i++) (*Sensor).Voltage[i] = 35.14;      //dummy values. Will be removed
+    for(i=0; i<10; i++) (*Sensor).Current[i] = 644.46;
+    for(i=0; i<10; i++) (*Sensor).Temp[i] = 6534.03;
+    printf("Done writing dummy values\n");
+}
+
+void setfilenames(char* beaname, char* acsname, char* epsname, time_t seconds){
+    char date[7];
+    printf("Setting file names\n");
+    strftime(date, 7, "%d%m%y", localtime(&seconds));   //getting ddmmyy from unix time
+    printf("%s\t%s\t%s\n", beaname, acsname, epsname);
+    
+    printf("Appending file extensions\n");
+    for(int i=10; i<16; i++){
+        printf("In %dth iteration",i);
+        beaname[i] = date[i-10];      //creating file name for beacon hk
+    }
+    beaname[16] = '.';  beaname[17] = 't';  beaname[18] = 'x';  beaname[19] = 't';  beaname[20]='\0';
+    
+    for(int i=10; i<16; i++)    acsname[i] = date[i-10];      //creating file name for acs hk
+    acsname[16] = '.';  acsname[17] = 't';  acsname[18] = 'x';  acsname[19] = 't';  acsname[20]='\0';
+    
+    for(int i=10; i<16; i++)    epsname[i] = date[i-10];      //creating file name for eps hk
+    epsname[16] = '.';  epsname[17] = 't';  epsname[18] = 'x';  epsname[19] = 't';  epsname[20]='\0';
+        
+    printf("All filenames created\n");    //Debug statement
+}
+
+void writevaluesf(char* beaname, char* acsname, char* epsname, time_t seconds, SensorData* Sensor){
+    FILE* fp;
+    char time[7];
+    strftime(time, 7, "%H%M%S", localtime(&seconds));   //getting time from unix time
+
+    fp = fopen(beaname,"w");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+    }
+    else{
+        //fprintf(fp,"%s\n",time);  //time stamping: Look for a better way
+        fwrite((*Sensor).Voltage, sizeof(float), BEANUM, fp);  //needs to be changed
+        fclose(fp);
+    }
+    
+    fp = fopen(acsname,"w");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+    }
+    else{
+        //fprintf(fp,"%s\n",time);  //time stamping: Look for a better way
+        fwrite((*Sensor).Current, sizeof(float), ACSNUM, fp);  //needs to be changed
+        fclose(fp);
+    }
+    
+    fp = fopen(epsname,"w");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+    }
+    else{
+        //fprintf(fp,"%s\n",time);  //time stamping: Look for a better way
+        fwrite((*Sensor).Temp, sizeof(float), EPSNUM, fp);  //needs to be changed
+        fclose(fp);
+    }
+       
+    printf("Writing over\n");    //Debug statement
+}
+
+//Function for debugging purposes
+void readvaluesff(char* beaname, char* acsname, char* epsname, SensorData* Sensor){
+    FILE* fpr;  //debug only
+    float* readstr = (float*) malloc( sizeof(float) * 30);    //reading back for debugging
+    
+    fpr = fopen(beaname,"r");
+    if(fpr == NULL) {
+        error("Could not open file for read\n");
+    }
+    else{
+        fread(readstr, sizeof(float), 10, fpr);
+    }
+    fclose(fpr);
+    
+    fpr = fopen(acsname,"r");
+    if(fpr == NULL) {
+        error("Could not open file for read\n");
+    }
+    else{
+        fread((readstr+10), sizeof(float), 10, fpr);
+    }
+    fclose(fpr);
+    
+    fpr = fopen(epsname,"r");
+    if(fpr == NULL) {
+        error("Could not open file for read\n");
+    }
+    else{
+        fread((readstr+20), sizeof(float), 10, fpr);
+    }
+    fclose(fpr);
+    
+    for(int i=0; i<30; i++) 
+        printf("%f\n",readstr[i]);
+}
+*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sdcard.h	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,18 @@
+#include "mbed.h"
+/*#include "SDFileSystem.h"
+
+#define BEANUM 10
+#define ACSNUM 10
+#define EPSNUM 10
+
+/*struct SensorData {             //will be changed
+    float Voltage[10];
+    float Current[10];
+    float Temp[10];
+};
+
+void writedummies(SensorData*);                         //will be removed
+void setfilenames(char*,char*,char*,time_t);
+void writevaluesf(char*,char*,char*,time_t,SensorData*);
+void readvaluesff(char*,char*,char*,SensorData*);       //for debugging only
+*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tmtc.cpp	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,1 @@
+#include "tmtc.h"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tmtc.h	Mon Sep 15 14:01:49 2014 +0000
@@ -0,0 +1,2 @@
+//#include "mbed.h"
+//#include "rtos.h"
\ No newline at end of file