i2c slave integrated

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDM_INTEGRATION by green rosh

Files at this revision

API Documentation at this revision

Comitter:
sakthipriya
Date:
Mon Dec 15 11:21:57 2014 +0000
Parent:
8:667fbc82d634
Commit message:
i2c_slave integrated

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
slave.cpp Show annotated file Show diff for this revision Revisions of this file
slave.h Show annotated file Show diff for this revision Revisions of this file
diff -r 667fbc82d634 -r a9de938283f9 main.cpp
--- a/main.cpp	Mon Dec 15 05:58:23 2014 +0000
+++ b/main.cpp	Mon Dec 15 11:21:57 2014 +0000
@@ -5,11 +5,16 @@
 #include "beacon.h"
 #include "ACS.h"
 #include "fault.h"
+#include "pin_config.h"
 
 #define DEFAULT_POW_MODE 0                                  //power mode initialization
 Serial pc(USBTX, USBRX);
 
-
+InterruptIn interrupt (PIN95);
+InterruptIn master_reset(PIN99);
+int reset =0;
+int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master
+char data_send[40],data_receive;
 
 Timer t;                                                        //To know the time of execution each thread
 Timer t1;
@@ -22,15 +27,27 @@
 //Thread *ptr_t_bea_telecommand;
 //Thread *ptr_t_fault;                                  //will be taken care by HK thread and interrupts
 Thread *ptr_t_i2c;                                      //for interprocessor communication
-Thread *ptr_t_exec_tc;                                  //Taking data from i2c mail box and execute the stuff
+Thread *ptr_t_exec_tc;                                  //Taking data from i2c mail box and execute the stuffread
+Thread *ptr_t_wdt;                                      //Thread ofr watchdog implementation
+
+
+
 
 
 //--------------------------------------------------------------------------------------------------------------------------------------------------
 //TASK 2 : HK
 //--------------------------------------------------------------------------------------------------------------------------------------------------
 
+//char hk_data[100];
+extern SensorData Sensor;
+typedef struct{
+char data[40];
+}i2c_hk_data;
+DigitalOut data_ready(D10);      //PIN NAME TO BE CHANGED
 
-extern SensorData Sensor;
+Mail<i2c_hk_data,16> i2c_hk_send;
+
+    
 void t_hk(void const *args)
 {
     Sensor.power_mode = DEFAULT_POW_MODE;
@@ -42,7 +59,17 @@
         //t.start();
         FAULTS();
         POWER(Sensor.power_mode);
-        FUNC_HK_MAIN();                                                             //Collecting HK data
+        FUNC_HK_MAIN();
+        reset =0;
+        i2c_status = 1;
+        data_ready = 0;
+        char hk_data[40]={Sensor.Voltage[10],Sensor.Current[10],Sensor.Temperature[10],Sensor.PanelTemperature[2],Sensor.Vcell,Sensor.soc,Sensor.alerts,Sensor.crate,Sensor.faultpoll,Sensor.faultir,Sensor.power_mode};
+        i2c_hk_data * i2c_hk_data_s = i2c_hk_send.alloc();
+        strcpy(i2c_hk_data_s->data,hk_data);
+        i2c_hk_send.put(i2c_hk_data_s);
+        data_ready = 1;
+        
+                                                                   //Collecting HK data
         //thread_2.signal_set(0x4);
        // FUNC_I2C_SLAVE_MAIN(24);                                                    //Put HK data to I2C thread instead
         
@@ -219,7 +246,79 @@
         //Sensor.power_mode++;            //testing ... should be removed
     }    
 }
-*/
+/*-------------------------------------------------------------------------------------------------------------------------------------------
+-------------------------------------------------------WATCHDOG----------------------------------------------------------------------------*/
+DigitalOut trigger(PIN63);                                             // has to be changed
+void T_WDT(void const * args)
+{
+    trigger = 1;
+    while(true)
+    {
+        Thread::signal_wait(0x5);                               //signal set from scheduler or sthing. r RTOS timer nce the timing is finalized
+        printf("\nEntered WD\n");
+        trigger = !trigger;
+    }
+}
+//------------------------------------------------------------------------------------------------------------------------------------------------
+//SLAVE
+//------------------------------------------------------------------------------------------------------------------------------------------------
+typedef struct
+{
+    char data;          // To avoid dynamic memory allocation
+}i2c_data;
+Mail<i2c_data,16> i2c_data_receive;                  //to be uncommented when receiving data
+
+
+void FUNC_INT()
+{
+   reset = 0;
+   ptr_t_i2c->signal_set(0x4);  
+}
+
+void FUNC_RESET()
+{
+   reset = 1;
+}
+
+
+void T_I2C_BAE(void const * args)
+{
+    //char data_send,data_receive;
+    while(1)
+    {
+        Thread::signal_wait(0x4);
+        printf("\n entered thread\n\r");
+        if(i2c_status == 0 && reset !=1)
+        {
+            
+            FUNC_I2C_WRITE2CDMS(&data_receive,1);
+            i2c_data * i2c_data_r = i2c_data_receive.alloc();
+            i2c_data_r->data = data_receive;
+            
+            i2c_data_receive.put(i2c_data_r);
+            printf("\n Data received from CDMS is %c\n\r",data_receive);
+            i2c_data_receive.free(i2c_data_r);                              // This has to be done from a differen thread
+            
+        }
+        else if(i2c_status ==1 && reset !=1)
+        {
+            osEvent evt = i2c_hk_send.get();
+            if (evt.status == osEventMail) 
+            {
+                i2c_hk_data *i2c_hk_data_s = (i2c_hk_data*)evt.value.p;
+                strcpy(data_send,i2c_hk_data_s -> data);
+                FUNC_I2C_WRITE2CDMS(data_send,40);
+                printf("\nData sent to CDMS is %s\n\r",data_send);
+                i2c_hk_send.free(i2c_hk_data_s);                           //This has to be done in a different thread
+                i2c_status = 0;
+                //temp = i2c_status;
+            }
+        } 
+         
+    }
+}
+
+
 //------------------------------------------------------------------------------------------------------------------------------------------------
 //SCHEDULER
 //------------------------------------------------------------------------------------------------------------------------------------------------
@@ -263,19 +362,20 @@
     
     ptr_t_hk = new Thread(t_hk);
     ptr_t_acs = new Thread(t_acs);
-    //ptr_t_acs_write2flash = new Thread(t_acs_write2flash);
+    
     ptr_t_bea = new Thread(t_bea);
-    //ptr_t_bea_telecommand = new Thread(t_bea_telecommand);
-    //ptr_t_fault = new Thread(T_FAULT);
-    //ptr_t_sc = new Thread(t_sc);
+    ptr_t_wdt = new Thread(T_WDT);
+    ptr_t_i2c = new Thread(T_I2C_BAE);
 
    // ptr_t_fault -> set_priority(osPriorityRealtime);
     ptr_t_acs->set_priority(osPriorityHigh);
     ptr_t_hk->set_priority(osPriorityNormal);
     //ptr_t_acs_write2flash->set_priority(osPriorityBelowNormal);
     ptr_t_bea->set_priority(osPriorityAboveNormal);
+    ptr_t_i2c->set_priority(osPriorityAboveNormal);
     //ptr_t_bea_telecommand->set_priority(osPriorityIdle);
     //ptr_t_sc->set_priority(osPriorityAboveNormal);
+    ptr_t_wdt -> set_priority(osPriorityIdle);
     
   
    // ----------------------------------------------------------------------------------------------
@@ -287,6 +387,9 @@
     RtosTimer t_sc_timer(t_sc,osTimerPeriodic);
     t_sc_timer.start(10000);
     printf("\n%f\n",t1.read()); 
+    
+    interrupt.rise(&FUNC_INT);
+    master_reset.fall(&FUNC_RESET);
         
     while(1)
     {   
diff -r 667fbc82d634 -r a9de938283f9 slave.cpp
--- a/slave.cpp	Mon Dec 15 05:58:23 2014 +0000
+++ b/slave.cpp	Mon Dec 15 11:21:57 2014 +0000
@@ -105,149 +105,54 @@
 #include "HK.h"
 #include "slave.h"
 
+#include "mbed.h"
+#include "rtos.h"
+#include "slave.h"
 
+void write_to_master(char);                    //function to write data to master
 
 I2CSlave slave(D14,D15);                       //configuring pins p27, p28 as I2Cslave
-Serial pc_slave (USBTX,USBRX);
-//InterruptIn interrupt(D9);                    //This should be in the main
-//DigitalOut data_ready(D10);                     //Again in the main function
-//int i2c_status=0;                                 //read/write mode for i2c 0 : write2slave, 1 : write2master (Again in tha main)
+
+Serial pcslave(USBTX, USBRX);
+
+
 
-typedef struct
+//int temp;
+ 
+
+void FUNC_I2C_WRITE2CDMS(char *data,int length)
 {
-    char data;          // To avoid dynamic memory allocation
-    int length;
-}i2c_data;
-
-//Mail<i2c_data,16> i2c_data_receive;           To be done in the main
-//Mail<i2c_data,16> i2c_data_send;              To be done in the main
-
-void FUNC_I2C_WRITE2CDMS(char *data, int length=1)
-{
+       int slave_status = 1;
        
-    int slave_status = 1;
+   
     while(slave_status)
     {                 
             slave.address(0x20);
             if(slave.receive()==1)
             {
                 slave_status=slave.write(data,length);
-               // printf("\n In the while loop\n");
-                
-                /*if(!slave_status)
-                {
-                        printf("slave wrote %c to master\n\r",*data);
-              
-                }*/
+               
                 
             }
-            else
-                if(slave.receive()==3 || slave.receive()==2)
-                {
-                     //i2c_data *i2c_data_r = i2c_data_receive.alloc();
-                     slave_status=slave.read(data,length);
-                     /*if(!slave_status)
-                     {
-                         printf("1 read %c from master\n\r",*data);
-                     }*/
-                }
-            //slave.stop();
+            else if(slave.receive()==3 || slave.receive()==2)
+            {
+                slave_status=slave.read(data,length);
+            }
+            
     }
-            printf("2 done");
+            printf("\ndone\n\r");
 
 }
 
-/*void T_I2C_BAE(void const * args)
-{
-    char data_send,data_receive;
-    //data_send = 'a';
-    while(1)
-    {
-        Thread::signal_wait(0x1);
-        printf("\ndetected interrupt\n");
-        if(i2c_status == 0)
-        {
-            
-            FUNC_I2C_WRITE2CDMS(&data_receive);
-            //printf("\n Data received from CDMS is %c\n",data_receive);
-            i2c_data * i2c_data_r = i2c_data_receive.alloc();
-            i2c_data_r->data = data_receive;
-            i2c_data_r->length = 1;
-            i2c_data_receive.put(i2c_data_r);
-        }
-        else if(i2c_status ==1 )
-        {
-            osEvent evt = i2c_data_send.get();
-            if (evt.status == osEventMail) 
-            {
-                i2c_data *i2c_data_s = (i2c_data*)evt.value.p;
-                //printf("\nData read from CDMS is %c\n",i2c_data_r->data);
-                data_send = i2c_data_s -> data;
-                FUNC_I2C_WRITE2CDMS(&data_send);
-                printf("\nData sent to CDMS is %c\n",data_send);
-                i2c_data_send.free(i2c_data_s);
-                i2c_status = 0;
-                //delete i2c_data_r;
-            }
-        }   
-    }
-}
-        
-void FUNC_INT()
-{
-  // char * data = new char;
-   //*data = 'a';
-   //printf("\nInterrupt from master detected\n");
-   ptr_t_i2c->signal_set(0x1);
-  // 
-   //delete data;
-}
+
+
+
+
+      
+   
    
 
 
-void main()
-{
-    printf("\nStarted slave... waiting for master\n");
-    ptr_t_i2c = new Thread(T_I2C_BAE);
-    char data='a';
-    interrupt.rise(&FUNC_INT);
-    
-   /* i2c_data * i2c_data_s = i2c_data_send.alloc();
-    i2c_data_s->data = data;
-    i2c_data_s->length = 1;
-    i2c_data_send.put(i2c_data_s);
-    //data_ready=1;
-    i2c_status=1;*/
-   // while(1)
-    //{
-        //*(data_send)=pc.getc();
-        
-        //FUNC_I2C_WRITE2CDMS(&data,1);
-        //printf("\n The data send or received by slave is %c\n",data);
-        //(data)++;
-        //wait(1);
-        //Remove the comments for receiving data
-        /* osEvent evt = i2c_data_receive.get();
-         if (evt.status == osEventMail) 
-         {
-            i2c_data *i2c_data_r = (i2c_data*)evt.value.p;
-            printf("\nData read from CDMS is %c\n",i2c_data_r->data);
-            
-            i2c_data_receive.free(i2c_data_r);
-            //delete i2c_data_r;
-        }*/
-/*        //put in comments for receiving data
-        data_ready=0;
-        data = pc.getc();
-        i2c_data * i2c_data_s = i2c_data_send.alloc();
-        i2c_data_s->data = data;
-        i2c_data_s->length = 1;
-        i2c_data_send.put(i2c_data_s);
-        data_ready=1;
-        i2c_status=1;
-                
-    }
-}
-*/
+
 
-//To be done in the main
\ No newline at end of file
+
diff -r 667fbc82d634 -r a9de938283f9 slave.h
--- a/slave.h	Mon Dec 15 05:58:23 2014 +0000
+++ b/slave.h	Mon Dec 15 11:21:57 2014 +0000
@@ -2,5 +2,8 @@
 #define WriteGeneral 3
 #define ReadAddressed 1
 #define slave_address 0x20
-void write_to_master(char);                    //function to write data to master
-void FUNC_I2C_SLAVE_MAIN(int iterations);
\ No newline at end of file
+
+void FUNC_I2C_WRITE2CDMS(char*,int);
+
+//void ir2master();
+