Cell voltages fork (SoC)

Dependencies:   CUER_CAN CUER_DS1820 LTC2943 LTC6804 mbed PowerControl

Revision:
32:5b82679b2e6f
Parent:
31:888b2602aab2
Child:
33:44b241c7b2c1
--- a/main.cpp	Thu Jul 13 20:05:16 2017 +0000
+++ b/main.cpp	Thu Jul 13 21:30:18 2017 +0000
@@ -22,6 +22,7 @@
 void CANDataSentCallback();
 void write_SOC_EEPROM(BMU_data &measurements,uint16_t start_address);
 uint16_t read_EEPROM_startup(BMU_data &measurements);
+void reset_EEPROM(float init_SOC, float init_SOC_Percent);
 uint32_t check_measurements(BMU_data &measurements);
 uint32_t take_measurements(BMU_data &measurements);
 void test_read_CAN_buffer();
@@ -69,7 +70,9 @@
     
     init();
     
+    reset_EEPROM(1,100); //Used to completely initialize EEPROM as if it has never been touched
     //current_EEPROM_address = read_EEPROM_startup(measurements); // Read from the eeprom at startup to fill in the values of SoC
+    //printf("Current EEPROM Address %d \r\n", current_EEPROM_address);
     //ltc2943.accumulatedCharge(measurements.percentage_SOC); // Initialise the LTC2943 with the current state of charge
     
     while (true) {
@@ -180,6 +183,7 @@
 
     // Get a pointer to the start address for the data stored in the eeprom
     i2c_page_read(0x0000,2,start_address_array1);
+    printf("\r\n\ Start address (1,2) (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
     i2c_page_read(0x0002,2,start_address_array2);
     
     is_first_read_true = check_EEPROM_PEC(start_address, start_address_array1, SOC_out);
@@ -203,16 +207,16 @@
     
     if(is_second_read_true || is_first_read_true){
         // Select the next address to write to
-        start_address1 += 0x0040;
-        start_address2 += 0x0040;
+        start_address1 += 0x0040; //This value was never initialized
+        start_address2 += 0x0040; //Also each SOC is taking 0xA space, so 0x15 should be sufficient offset
         if(start_address > MAX_WRITE_ADDRESS) {
             start_address1 = START_WRITE_ADDRESS;        // Loop to the start of the eeprom
             start_address2 = START_WRITE_ADDRESS + SECOND_ADDRESS_OFFSET;  // Write this data SECOND_ADDRESS_OFFSET memory locations later than the first set                                                   //  (this was chosen since only 10 bytes are written to memory
         }
-        start_address_array1[0] = start_address1 | 0x00FF;
-        start_address_array1[1] = start_address1 >> 8;
+        start_address_array1[0] = start_address1 | 0x00FF; //This should be an & I think
+        start_address_array1[1] = start_address1 >> 8; //Also does setting a 16 bit val to a char work?
         start_address_array2[0] = start_address2 | 0x00FF;
-        start_address_array2[1] = start_address2 >> 8;
+        start_address_array2[1] = start_address2 >> 8; 
         
         // Write the new location of the address to memory
         i2c_page_write(0x0000, 2, start_address_array1);
@@ -222,7 +226,51 @@
     }
     else{
         printf("PEC error"); //@TODO an error flag should be raised since both values have failed
+        
     }
+    return -1; //Will end up as maximum integer, just indicating an error.
+}
+
+void reset_EEPROM(float init_SOC, float init_SOC_Percent)
+{
+    char start_address_array1[2]; //Purely for testing
+    //Very first addresses to use
+    char first_address[2] = {0x40,0}; //Address 0x0040
+    char second_address[2] = {first_address[0] + SECOND_ADDRESS_OFFSET,0};
+    uint16_t address1 = (first_address[1] << 8) | first_address[0];
+    uint16_t address2 = (second_address[1] << 8) | second_address[0];
+    
+    i2c_page_write(0x0000, 2, first_address);
+    i2c_page_write(0x0002, 2, second_address);   //This initializes addresses
+    //Next segment is for putting initial SOC in:
+    
+    i2c_page_read(0x0000,2,start_address_array1);
+    printf("Start address (1,2) (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
+    
+    char data_out[10];
+    float *fp1,*fp2;
+    uint16_t data_pec; 
+
+    fp1 = (float*)(&init_SOC); //This may be source of error, uncomfortable with this
+    fp2 = (float*)(&init_SOC_Percent);
+
+    for(int i = 0; i < 4; i++ ) {
+        data_out[i] = *fp1;
+        fp1++;
+    }
+    for(int j = 4; j < 7; j++ ) {
+        data_out[j] = *fp2;
+        fp2++;
+    } 
+    data_pec = pec15_calc(8, ((uint8_t*)data_out)); // Calculate the pec and then write it to memory
+    data_out[8] = (char)(data_pec >> 8);
+    data_out[9] = (char)(data_pec);
+    i2c_page_write(address1, 10,data_out);
+    i2c_page_write(address2, 10,data_out);
+    
+    //Stuff for testing purposes only
+    i2c_page_read(0x0000,2,start_address_array1);
+    printf("Start address (1,2) (%d,%d) \r\n \r\n", start_address_array1[0], start_address_array1[1]);
 }
 
 bool check_EEPROM_PEC( uint16_t start_address, char start_address_array[], char SOC_out[]){