LTC6811 Battery Management System with ADuCM3029.

Files at this revision

API Documentation at this revision

Comitter:
APS_Lab
Date:
Fri Feb 09 04:43:04 2018 +0000
Parent:
0:f06ed53310a3
Commit message:
Improved for SPI.

Changed in this revision

LTC681x.cpp Show annotated file Show diff for this revision Revisions of this file
bms.cpp Show annotated file Show diff for this revision Revisions of this file
bms.h Show annotated file Show diff for this revision Revisions of this file
diff -r f06ed53310a3 -r 4dd3e328a30b LTC681x.cpp
--- a/LTC681x.cpp	Wed Feb 07 08:26:04 2018 +0000
+++ b/LTC681x.cpp	Fri Feb 09 04:43:04 2018 +0000
@@ -13,10 +13,10 @@
 void wakeup_idle(uint8_t total_ic)
 {
     for (int i =0; i<total_ic; i++) {
-        //spi2_CS = 0;
-//    wait_ms(2); //Guarantees the isoSPI will be in ready mode
+        cs_low();
+        wait_ms(2); //Guarantees the isoSPI will be in ready mode
         spi_read_byte(0xff);
-        //spi2_CS = 1;
+        cs_high();
     }
 }
 
@@ -24,9 +24,9 @@
 void wakeup_sleep(uint8_t total_ic)
 {
     for (int i =0; i<total_ic; i++) {
-        //spi2_CS = 0;
+        cs_low();
         delay_u(300); // Guarantees the LTC6813 will be in standby
-        //spi2_CS = 1;
+        cs_high();
         delay_u(10);
     }
 }
@@ -43,9 +43,9 @@
     cmd_pec = pec15_calc(2, cmd);
     cmd[2] = (uint8_t)(cmd_pec >> 8);
     cmd[3] = (uint8_t)(cmd_pec);
-    //spi2_CS = 0;
+    cs_low();
     spi_write_array(4,cmd);
-    //spi2_CS = 1;
+    cs_high();
 }
 
 //Generic function to write 68xx commands and write payload data. Function calculated PEC for tx_cmd data
@@ -81,9 +81,9 @@
     }
 
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_array(CMD_LEN, cmd);
-    //spi2_CS = 1;
+    cs_high();
     free(cmd);
 }
 
@@ -107,9 +107,9 @@
     cmd[3] = (uint8_t)(cmd_pec);
 
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_read(cmd, 4, data, (BYTES_IN_REG*total_ic));         //Read the configuration data of all ICs on the daisy chain into
-    //spi2_CS = 1;                         //rx_data[] array
+    cs_high();                         //rx_data[] array
 
     for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) {     //executes for each LTC681x in the daisy chain and packs the data
         //into the r_comm array as well as check the received Config data
@@ -279,10 +279,10 @@
     cmd[3] = (uint8_t)(cmd_pec);
 
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_array(4,cmd);
 // adc_state = spi_read_byte(0xFF);
-    //spi2_CS = 1;
+    cs_high();
     return(adc_state);
 }
 
@@ -302,7 +302,7 @@
     cmd[2] = (uint8_t)(cmd_pec >> 8);
     cmd[3] = (uint8_t)(cmd_pec);
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_array(4,cmd);
 
     while ((counter<200000)&&(finished == 0)) {
@@ -313,8 +313,8 @@
             counter = counter + 10;
         }
     }
-    //spi2_CS = 1;
-
+    cs_high();
+    
 
     return(counter);
 }
@@ -436,9 +436,9 @@
     cmd[2] = (uint8_t)(cmd_pec >> 8);
     cmd[3] = (uint8_t)(cmd_pec);
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_read(cmd,4,data,(REG_LEN*total_ic));
-    //spi2_CS = 1;
+    cs_high();
 
 }
 
@@ -515,9 +515,9 @@
     cmd[2] = (uint8_t)(cmd_pec >> 8);
     cmd[3] = (uint8_t)(cmd_pec);
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_read(cmd,4,data,(REG_LEN*total_ic));
-    //spi2_CS = 1;
+    cs_high();
 
 }
 
@@ -553,9 +553,9 @@
     cmd[2] = (uint8_t)(cmd_pec >> 8);
     cmd[3] = (uint8_t)(cmd_pec);
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_read(cmd,4,data,(REG_LEN*total_ic));
-    //spi2_CS = 1;
+    cs_high();
 
 }
 
@@ -1381,12 +1381,12 @@
     cmd[2] = (uint8_t)(cmd_pec >> 8);
     cmd[3] = (uint8_t)(cmd_pec);
 
-    //spi2_CS = 0;
+    cs_low();
     spi_write_array(4,cmd);
     for (int i = 0; i<9; i++) {
         spi_read_byte(0xFF);
     }
-    //spi2_CS = 1;
+    cs_high();
 
 }
 
diff -r f06ed53310a3 -r 4dd3e328a30b bms.cpp
--- a/bms.cpp	Wed Feb 07 08:26:04 2018 +0000
+++ b/bms.cpp	Fri Feb 09 04:43:04 2018 +0000
@@ -4,6 +4,16 @@
 SPI spi(D11, D12, D13);
 DigitalOut cs(D10);
 
+void cs_low(void)
+{
+    cs=0;
+}
+
+void cs_high(void)
+{
+    cs=1;
+}
+
 void delay_u(uint16_t micro)
 {
     wait_us(micro);
@@ -29,9 +39,11 @@
                      uint8_t data[] //Array of bytes to be written on the SPI port
                     )
 {
+    //cs=0;
     for (uint8_t i = 0; i < len; i++) {
         spi.write((int8_t)data[i]);
     }
+    //cs=1;
 }
 
 /*
@@ -45,14 +57,18 @@
                     uint8_t rx_len //Option: number of bytes to be read from the SPI port
                    )
 {
+    //cs=0;
     for (uint8_t i = 0; i < tx_len; i++) {
         spi.write(tx_Data[i]);
     }
+    //cs=1;
 
+    //cs=0;
     for (uint8_t i = 0; i < rx_len; i++) {
 
         rx_data[i] = (uint8_t)spi.write(0xFF);
     }
+    //cs=1;
 
 }
 
@@ -60,6 +76,8 @@
 uint8_t spi_read_byte(uint8_t tx_dat)
 {
     uint8_t data;
+    //cs=0;
     data = (uint8_t)spi.write(0xFF);
+    //cs=1;
     return(data);
 }
diff -r f06ed53310a3 -r 4dd3e328a30b bms.h
--- a/bms.h	Wed Feb 07 08:26:04 2018 +0000
+++ b/bms.h	Fri Feb 09 04:43:04 2018 +0000
@@ -1,3 +1,6 @@
+void cs_low(void);
+void cs_high(void);
+
 void delay_u(uint16_t micro);
 
 void delay_m(uint16_t milli);