i2c slave integrated

Dependencies:   mbed-rtos mbed

Fork of BAE_FRDM_INTEGRATION by green rosh

Revision:
9:a9de938283f9
Parent:
8:667fbc82d634
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)
     {