#include #include #include #include "LT_SPI.h" #include "LTC68042.h" #define USERINTERFACE_H #define UI_BUFFER_SIZE 64 #define SERIAL_TERMINATOR '\n' #define output_low(pin) digitalWrite(pin, LOW) #define output_high(pin) digitalWrite(pin, HIGH) #define LTC68042_H #ifndef LTC6804_CS #define LTC6804_CS QUIKEVAL_CS #endif /*-----------------------------Variables---------------------------------------*/ /*-----------------------------------------------------------------------------*/ const int CS_PIN = 5; const int SCLK_PIN = 18; const int MOSI_PIN = 23; const int MISO_PIN = 19; const int ledPin1 = 4; // Pin 4 pour la première LED const int ledPin2 = 16; // Pin 16 pour la deuxième LED const int ledPin3 = 17; // Pin 17 pour la troisième LED const int CELL_NUM = 12; //Maximum cell number on each IC const int TOTAL_IC = 1; //Number of IC's uint8_t tx_cfg[TOTAL_IC][6]; //For configuring configuration registers uint8_t error; int input; uint16_t cell_codes[TOTAL_IC][12]; /*--------------------Comandes de conversion du 6804---------------------------*/ /*-----------------------------------------------------------------------------*/ uint8_t ADCV[2]; uint8_t ADAX[2]; /*Initialisation du 6804*/ void LTC6804_initialize(){ quikeval_SPI_connect(); spi_enable(SPI_CLOCK_DIV16); set_adc(MD_NORMAL, DCP_DISABLED, CELL_CH_ALL, AUX_CH_ALL); } void set_adc(uint8_t MD, //ADC Mode uint8_t DCP, //Discharge Permit uint8_t CH, //Cell Channels to be measured uint8_t CHG //GPIO Channels to be measured ) { uint8_t md_bits; md_bits = (MD & 0x02) >> 1; ADCV[0] = md_bits + 0x02; md_bits = (MD & 0x01) << 7; ADCV[1] = md_bits + 0x60 + (DCP<<4) + CH; md_bits = (MD & 0x02) >> 1; ADAX[0] = md_bits + 0x04; md_bits = (MD & 0x01) << 7; ADAX[1] = md_bits + 0x60 + CHG ; } void LTC6804_adax() { uint8_t cmd[4]; uint16_t temp_pec; cmd[0] = ADAX[0]; cmd[1] = ADAX[1]; temp_pec = pec15_calc(2, ADAX); cmd[2] = (uint8_t)(temp_pec >> 8); cmd[3] = (uint8_t)(temp_pec); wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed. output_low(LTC6804_CS); spi_write_array(4,cmd); output_high(LTC6804_CS); } /*Lecture GPIO*/ int8_t LTC6804_rdaux(uint8_t reg, uint8_t total_ic, uint16_t aux_codes[][6] ) { const uint8_t NUM_RX_BYT = 8; const uint8_t BYT_IN_REG = 6; const uint8_t GPIO_IN_REG = 3; uint8_t *data; uint8_t data_counter = 0; int8_t pec_error = 0; uint16_t received_pec; uint16_t data_pec; data = (uint8_t *) malloc((NUM_RX_BYT*total_ic)*sizeof(uint8_t)); //1.a if (reg == 0) { //a.i for(uint8_t gpio_reg = 1; gpio_reg<3; gpio_reg++) //executes once for each of the LTC6804 aux voltage registers { data_counter = 0; LTC6804_rdaux_reg(gpio_reg, total_ic,data); for (uint8_t current_ic = 0 ; current_ic < total_ic; current_ic++) // This loop executes once for each LTC6804 { // current_ic is used as an IC counter //a.ii for(uint8_t current_gpio = 0; current_gpio< GPIO_IN_REG; current_gpio++) // This loop parses GPIO voltages stored in the register { aux_codes[current_ic][current_gpio +((gpio_reg-1)*GPIO_IN_REG)] = data[data_counter] + (data[data_counter+1]<<8); data_counter=data_counter+2; } //a.iii received_pec = (data[data_counter]<<8)+ data[data_counter+1]; data_pec = pec15_calc(BYT_IN_REG, &data[current_ic*NUM_RX_BYT*(gpio_reg-1)]); if(received_pec != data_pec) { pec_error = -1; } data_counter=data_counter+2; } } } else { //b.i LTC6804_rdaux_reg(reg, total_ic, data); for (int current_ic = 0 ; current_ic < total_ic; current_ic++) // executes for every LTC6804 in the stack { // current_ic is used as an IC counter //b.ii for(int current_gpio = 0; current_gpio> 8); cmd[3] = (uint8_t)(cmd_pec); //3 wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake, this command can be removed. //4 for(int current_ic = 0; current_ic> 8); cmd[3] = (uint8_t)(cmd_pec); output_low(LTC6804_CS); spi_write_read(cmd,4,&data[current_ic*8],8); output_high(LTC6804_CS); } } /*Reveiller la comm isoSPI*/ void wakeup_idle() { output_low(LTC6804_CS); delayMicroseconds(10); //Guarantees the isoSPI will be in ready mode output_high(LTC6804_CS); } /*Reveiller le LTC6804*/ void wakeup_sleep() { output_low(LTC6804_CS); delay(1); // Guarantees the LTC6804 will be in standby output_high(LTC6804_CS); } /*--------------Fonctions supplémentaires------------------------*/ void init_cfg(){ for(int i = 0; i