Team Fox / Mbed 2 deprecated BAE_QM_MAR9

Dependencies:   FreescaleIAP mbed-rtos mbed

Fork of workinQM_5thJan_azad by Team Fox

Revision:
35:7193e581932f
Parent:
34:1b41c34b12ea
--- a/ACS.cpp	Mon Jul 04 07:01:26 2016 +0000
+++ b/ACS.cpp	Mon Jul 04 13:32:29 2016 +0000
@@ -5,6 +5,7 @@
 
 #include "pni.h" //pni header file
 #include "pin_config.h"
+#include "configuration.h"
 #include "ACS.h"
 #include "EPS.h"
 /*variables will get get updated value from FLash 
@@ -437,30 +438,122 @@
 
 int ack;
 int CONFIG_UPLOAD()
-{  
+{
+    uint8_t value;
+    
     cmd[0]=RESETREQ;
     cmd[1]=BIT_RESREQ;
     i2c.write(SLAVE_ADDR,cmd,2); //When 0x01 is written in reset request register Emulates a hard power down/power up
-    wait_ms(575);
+    wait_ms(600);
     
     //Verify magic number
     
+    cmd[0]=SENTRALSTATUS;
+    i2c.write(SLAVE_ADDR,cmd,1);
+    i2c.read(SLAVE_ADDR_READ,&store,1);
+    value = (uint8_t)store;
+    
+     if(value & 0x02)
+     {
+        printf("Sentral already has eeprom firmware loaded.\n");
+     }
+     /* Write value 0x01 to the ResetReq register, address 0x9B. This will result
+    in a hard reset of the Sentral. This is unnecessary if the prior event was
+    a Reset. */
+     if(!(value & 0x08))
+     {
+        printf("CPU is not in standby, issuing a shutdown request.\n");
+        //i2c_write(I2C_SLAVE_ADDR, 0x34, data, 1);
+        cmd[0]=HOST_CTRL; //0x00 is written in HOST CONTROL register to shut down
+        cmd[1]=0x00;
+        i2c.write(SLAVE_ADDR,cmd,2);
+
+        int cnt=0;
+        do {
+            cmd[0]=SENTRALSTATUS;
+            i2c.write(SLAVE_ADDR,cmd,1);
+            i2c.read(SLAVE_ADDR_READ,&store,1);
+            value = (uint8_t)store;
+            wait_ms(100);
+            cnt++;
+        } while((!(value & 0x08))&&(cnt<4));
+        
+        if(cnt==4)
+        {
+            return 0;   
+        }
+    }
+    
     cmd[0]=HOST_CTRL; //0x02 is written in HOST CONTROL register to enable upload
     cmd[1]=BIT_HOST_UPLD_ENB;
     i2c.write(SLAVE_ADDR,cmd,2);
-    wait_ms(20);
     
-    cmd[0]=UPLOAD_ADDR; //0x02 is written in HOST CONTROL register to enable upload
+    cmd[0]=UPLOAD_ADDR; //0x0000 is written in RAM register to enable upload
     cmd[1]=0x0000;
     i2c.write(SLAVE_ADDR,cmd,3);
-    wait_ms(20);
+    wait_ms(100);
+    
+     printf("Uploading data...\n");
 
+     #define TRASACTION_SIZE 3
+     
+     
+     for(int i = 0; i < EEPROMTextLength; i += TRASACTION_SIZE * 4)
+     {
+            
+            char* data = new char[TRASACTION_SIZE * 4];
+            data[0]=0x96;
+            for(int j = 0; j < TRASACTION_SIZE; j++)
+            {
+                 data[j * 4 + 1] = configdata[i + j * 4 + 3];
+                 data[j * 4 + 2] = configdata[i + j * 4 + 2];
+                 data[j * 4 + 3] = configdata[i + j * 4 + 1];
+                 data[j * 4 + 4] = configdata[i + j * 4 + 0];
+            }
+            
+            if(EEPROMTextLength < (i + (TRASACTION_SIZE * 4)))
+            {
+             uint32_t bytes = EEPROMTextLength - i;
+             i2c.write(SLAVE_ADDR,data,bytes+1);
+            }
+            
+            else
+            {
+            /* Write the Configuration File to Sentral’s program RAM. The file is sent
+            one byte at a time, using the UploadData register, register address 0x96. */
+                i2c.write(SLAVE_ADDR,data,13);
+            }
+            delete data;
+    }
+    
+    char crc[4];
+    cmd[0]=0x97;
+    i2c.write(SLAVE_ADDR,cmd,1);
+    i2c.read(SLAVE_ADDR_READ,crc,4);
+    value = (uint8_t)store;
+     
+      uint32_t actualCRC = ((uint32_t)crc[0] << 0) | ((uint32_t)crc[1] << 8) | ((uint32_t)crc[2] << 16) | ((uint32_t)crc[3] << 24);
+     
+      if(actualCRC != EEPROMTextCRC)
+         {
+            pc_acs.printf("Program crc (0x%.8X) does not match CRC reported by Sentral (0x%0.8X)\n", EEPROMTextCRC, actualCRC);
+            return 0;
+         }
+        else
+         {
+            pc_acs.printf("Firmware Upload Complete.\n");
+            return 1;
+         }
+    
+    
+    
+    
     cmd[0]=HOST_CTRL; //0x00 is written in HOST CONTROL register to free upload
     cmd[1]=0x00;
     i2c.write(SLAVE_ADDR,cmd,2);
-    wait_ms(20);
     
     return 0;
+    
 }
 
 int SENSOR_INIT()