BMS

Dependencies:   bms_master mbed

Revision:
2:e0ec3ed506ea
Parent:
0:3547f7580dbd
Child:
4:47d6a0b63e38
--- a/main.cpp	Sun Nov 12 01:23:38 2017 +0000
+++ b/main.cpp	Fri Nov 17 09:02:34 2017 +0000
@@ -1,31 +1,4 @@
-/*!
-Linear Technology DC2259 Demonstration Board
-LTC6811-1: Battery stack monitor
-
-REVISION HISTORY
-$Revision: 7139 $
-$Date: 2017-06-01 13:55:14 -0700 (Thu, 01 Jun 2017) $
-
-@verbatim
-
-NOTES
- Setup:
-   Set the terminal baud rate to 115200 and select the newline terminator.
-   Ensure all jumpers on the demo board are installed in their default positions from the factory.
-   Refer to Demo Manual DC2259.
-
-USER INPUT DATA FORMAT:
- decimal : 1024
- hex     : 0x400
- octal   : 02000  (leading 0)
- binary  : B10000000000
- float   : 1024.0
-@endverbatim
-
-http://www.linear.com/product/LTC6811-1
-
-http://www.linear.com/product/LTC6811-1#demoboards
-
+/*
 Copyright (c) 2017, Linear Technology Corp.(LTC)
 All rights reserved.
 
@@ -63,16 +36,9 @@
  */
 
 
-/*! @file
-    @ingroup LTC6811-1
-*/
-
 #include "mbed.h"
 #include <stdint.h>
-#include "LT_SPI.h"
-#include "UserInterface.h"
-#include "LTC681x.h"
-#include "LTC6811.h"
+#include "bms_master.h"
 
 #define ENABLED 1
 #define DISABLED 0
@@ -136,36 +102,32 @@
 void print_pec(void);
 void serial_print_hex(uint8_t data);
 
-/*!**********************************************************************
- \brief  Inititializes hardware and variables
- ***********************************************************************/
-void setup()
-{
-    pc.baud(115200);
-//  quikeval_SPI_connect();
-    CS_PIN = 1;
-    spi_enable(); // This will set the Linduino to have a 1MHz Clock
-    LTC681x_init_cfg(TOTAL_IC, bms_ic);
-    LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
-    LTC6811_init_reg_limits(TOTAL_IC,bms_ic);
-    print_menu();
-}
 
 /*!*********************************************************************
   \brief main loop
 ***********************************************************************/
 int main(void)
 {
-  while(1)
-  {
-      if (pc.readable())           // Check for user input
-      {
-        uint32_t user_command;
-        user_command = read_int();      // Read the user command
-        pc.printf("%d",user_command);
-        run_command(user_command);
-      }
-  }
+    pc.baud(115200);
+    CS_PIN = 1;
+    spi.format(16,3);
+    spi.frequency(1000000);  // 1MHz clock rate
+    
+    LTC681x_init_cfg(TOTAL_IC, bms_ic);
+    LTC681x_reset_crc_count(TOTAL_IC,bms_ic);
+    LTC6811_init_reg_limits(TOTAL_IC,bms_ic);
+    print_menu();
+    
+    while(1)
+    {      
+        if (pc.readable())           // Check for user input
+        {
+            uint32_t user_command;
+            user_command = read_int();      // Read the user command
+            pc.printf("%d",user_command);
+            run_command(user_command);
+        }
+    }
 }
 
 
@@ -185,21 +147,21 @@
 
     case 1: // Write Configuration Register
       wakeup_sleep(TOTAL_IC);
-      LTC6811_wrcfg(TOTAL_IC,bms_ic);
+      LTC681x_wrcfg(TOTAL_IC,bms_ic);
       print_config();
       break;
 
     case 2: // Read Configuration Register
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
+      error = LTC681x_rdcfg(TOTAL_IC,bms_ic);
       check_error(error);
       print_rxconfig();
       break;
 
     case 3: // Start Cell ADC Measurement
       wakeup_sleep(TOTAL_IC);
-      LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
-      conv_time = LTC6811_pollAdc();
+      LTC681x_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
+      conv_time = LTC681x_pollAdc();
       pc.printf("cell conversion completed in:");
       pc.printf("%.1f",((float)conv_time/1000));
       pc.printf("mS");
@@ -208,37 +170,37 @@
 
     case 4: // Read Cell Voltage Registers
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers
+      error = LTC681x_rdcv(0, TOTAL_IC,bms_ic); // Set to read back all cell voltage registers
       check_error(error);
       print_cells(DATALOG_DISABLED);
       break;
 
     case 5: // Start GPIO ADC Measurement
       wakeup_sleep(TOTAL_IC);
-      LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_TO_CONVERT);
-      LTC6811_pollAdc();
+      LTC681x_adax(ADC_CONVERSION_MODE , AUX_CH_TO_CONVERT);
+      LTC681x_pollAdc();
       pc.printf("aux conversion completed");
       pc.printf("\r");
       break;
 
     case 6: // Read AUX Voltage Registers
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
+      error = LTC681x_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
       check_error(error);
       print_aux(DATALOG_DISABLED);
       break;
 
     case 7: // Start Status ADC Measurement
       wakeup_sleep(TOTAL_IC);
-      LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_TO_CONVERT);
-      LTC6811_pollAdc();
+      LTC681x_adstat(ADC_CONVERSION_MODE, STAT_CH_TO_CONVERT);
+      LTC681x_pollAdc();
       pc.printf("stat conversion completed");
       pc.printf("\r");
       break;
 
     case 8: // Read Status registers
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
+      error = LTC681x_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
       check_error(error);
       print_stat();
       break;
@@ -246,7 +208,7 @@
     case 9: // Loop Measurements
       pc.printf("transmit 'm' to quit");
       wakeup_sleep(TOTAL_IC);
-      LTC6811_wrcfg(TOTAL_IC,bms_ic);
+      LTC681x_wrcfg(TOTAL_IC,bms_ic);
       while (input != 'm')
       {
         if (pc.readable() > 0)
@@ -267,22 +229,22 @@
       break;
 
     case 11: // Read in raw configuration data
-      LTC6811_reset_crc_count(TOTAL_IC,bms_ic);
+      LTC681x_reset_crc_count(TOTAL_IC,bms_ic);
       break;
 
     case 12:  // Run the ADC/Memory Self Test
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_run_cell_adc_st(CELL,ADC_CONVERSION_MODE,bms_ic);
+      error = LTC681x_run_cell_adc_st(CELL,ADC_CONVERSION_MODE,bms_ic);
       pc.printf("%d",error);
       pc.printf(" : errors detected in Digital Filter and CELL Memory \n");
 
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_run_cell_adc_st(AUX,ADC_CONVERSION_MODE, bms_ic);
+      error = LTC681x_run_cell_adc_st(AUX,ADC_CONVERSION_MODE, bms_ic);
       pc.printf("%d",error);
       pc.printf(" : errors detected in Digital Filter and AUX Memory \n");
 
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_run_cell_adc_st(STAT,ADC_CONVERSION_MODE, bms_ic);
+      error = LTC681x_run_cell_adc_st(STAT,ADC_CONVERSION_MODE, bms_ic);
       pc.printf("%d",error);
       pc.printf(" : errors detected in Digital Filter and STAT Memory \n");
       print_menu();
@@ -293,30 +255,30 @@
       readIC = (int8_t)read_int();
       LTC6811_set_discharge(readIC,TOTAL_IC,bms_ic);
       wakeup_sleep(TOTAL_IC);
-      LTC6811_wrcfg(TOTAL_IC,bms_ic);
+      LTC681x_wrcfg(TOTAL_IC,bms_ic);
       print_config();
       break;
 
     case 14: // Clear all discharge transistors
       clear_discharge(TOTAL_IC,bms_ic);
       wakeup_sleep(TOTAL_IC);
-      LTC6811_wrcfg(TOTAL_IC,bms_ic);
+      LTC681x_wrcfg(TOTAL_IC,bms_ic);
       print_config();
       break;
 
     case 15: // Clear all ADC measurement registers
       wakeup_sleep(TOTAL_IC);
-      LTC6811_clrcell();
-      LTC6811_clraux();
-      LTC6811_clrstat();
+      LTC681x_clrcell();
+      LTC681x_clraux();
+      LTC681x_clrstat();
       pc.printf("All Registers Cleared");
       break;
 
     case 16: // Run the Mux Decoder Self Test
       wakeup_sleep(TOTAL_IC);
-      LTC6811_diagn();
+      LTC681x_diagn();
       wait_ms(5);
-      error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
+      error = LTC681x_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
       check_error(error);
       error = 0;
       for (int ic = 0; ic<TOTAL_IC; ic++)
@@ -330,32 +292,32 @@
 
     case 17: // Run ADC Overlap self test
       wakeup_sleep(TOTAL_IC);
-      error = (int8_t)LTC6811_run_adc_overlap(TOTAL_IC,bms_ic);
+      error = (int8_t)LTC681x_run_adc_overlap(TOTAL_IC,bms_ic);
       if (error==0) pc.printf("Overlap Test: PASS ");
       else pc.printf("Overlap Test: FAIL");
       break;
 
     case 18: // Run ADC Redundancy self test
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,AUX,TOTAL_IC, bms_ic);
+      error = LTC681x_run_adc_redundancy_st(ADC_CONVERSION_MODE,AUX,TOTAL_IC, bms_ic);
       pc.printf("%d",error);
       pc.printf(" : errors detected in AUX Measurement \n");
 
       wakeup_sleep(TOTAL_IC);
-      error = LTC6811_run_adc_redundancy_st(ADC_CONVERSION_MODE,STAT,TOTAL_IC, bms_ic);
+      error = LTC681x_run_adc_redundancy_st(ADC_CONVERSION_MODE,STAT,TOTAL_IC, bms_ic);
       pc.printf("%d",error);
       pc.printf(" : errors detected in STAT Measurement \n");
       break;
 
     case 19:
-      LTC6811_run_openwire(TOTAL_IC, bms_ic);
+      LTC681x_run_openwire(TOTAL_IC, bms_ic);
       print_open();
       break;
 
     case 20: //Datalog print option Loop Measurements
       pc.printf("transmit 'm' to quit");
       wakeup_sleep(TOTAL_IC);
-      LTC6811_wrcfg(TOTAL_IC,bms_ic);
+      LTC681x_wrcfg(TOTAL_IC,bms_ic);
       while (input != 'm')
       {
         if (pc.readable() > 0)
@@ -386,14 +348,14 @@
   if (WRITE_CONFIG == ENABLED)
   {
     wakeup_sleep(TOTAL_IC);
-    LTC6811_wrcfg(TOTAL_IC,bms_ic);
+    LTC681x_wrcfg(TOTAL_IC,bms_ic);
     print_config();
   }
 
   if (READ_CONFIG == ENABLED)
   {
     wakeup_sleep(TOTAL_IC);
-    error = LTC6811_rdcfg(TOTAL_IC,bms_ic);
+    error = LTC681x_rdcfg(TOTAL_IC,bms_ic);
     check_error(error);
     print_rxconfig();
   }
@@ -401,10 +363,10 @@
   if (MEASURE_CELL == ENABLED)
   {
     wakeup_idle(TOTAL_IC);
-    LTC6811_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
-    LTC6811_pollAdc();
+    LTC681x_adcv(ADC_CONVERSION_MODE,ADC_DCP,CELL_CH_TO_CONVERT);
+    LTC681x_pollAdc();
     wakeup_idle(TOTAL_IC);
-    error = LTC6811_rdcv(0, TOTAL_IC,bms_ic);
+    error = LTC681x_rdcv(0, TOTAL_IC,bms_ic);
     check_error(error);
     print_cells(datalog_en);
 
@@ -413,10 +375,10 @@
   if (MEASURE_AUX == ENABLED)
   {
     wakeup_idle(TOTAL_IC);
-    LTC6811_adax(ADC_CONVERSION_MODE , AUX_CH_ALL);
-    LTC6811_pollAdc();
+    LTC681x_adax(ADC_CONVERSION_MODE , AUX_CH_ALL);
+    LTC681x_pollAdc();
     wakeup_idle(TOTAL_IC);
-    error = LTC6811_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
+    error = LTC681x_rdaux(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
     check_error(error);
     print_aux(datalog_en);
   }
@@ -424,10 +386,10 @@
   if (MEASURE_STAT == ENABLED)
   {
     wakeup_idle(TOTAL_IC);
-    LTC6811_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL);
-    LTC6811_pollAdc();
+    LTC681x_adstat(ADC_CONVERSION_MODE, STAT_CH_ALL);
+    LTC681x_pollAdc();
     wakeup_idle(TOTAL_IC);
-    error = LTC6811_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
+    error = LTC681x_rdstat(0,TOTAL_IC,bms_ic); // Set to read back all aux registers
     check_error(error);
     print_stat();
   }