CODE
CODE
CODE
h>
#include <SPI.h>
#include <Arduino.h>
#include "LT_SPI.h"
#include "LTC68042.h"
#define USERINTERFACE_H
#define UI_BUFFER_SIZE 64
#define SERIAL_TERMINATOR '\n'
#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;
/*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 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]
)
{
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<GPIO_IN_REG; current_gpio++) // This
loop parses the read back data. Loops
{ // once for each aux voltage in the register
aux_codes[current_ic][current_gpio +((reg-1)*GPIO_IN_REG)] = 0x0000FFFF &
(data[data_counter] + (data[data_counter+1]<<8));
data_counter=data_counter+2;
}
//b.iii
received_pec = (data[data_counter]<<8) + data[data_counter+1];
data_pec = pec15_calc(6, &data[current_ic*8*(reg-1)]);
if(received_pec != data_pec)
{
pec_error = -1;
}
}
}
free(data);
return (pec_error);
}
//1
if (reg == 1)
{
cmd[1] = 0x0C;
cmd[0] = 0x00;
}
else if(reg == 2)
{
cmd[1] = 0x0e;
cmd[0] = 0x00;
}
else
{
cmd[1] = 0x0C;
cmd[0] = 0x00;
}
//2
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 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<total_ic; current_ic++)
{
cmd[0] = 0x80 + (current_ic<<3); //Setting address
cmd_pec = pec15_calc(2, cmd);
cmd[2] = (uint8_t)(cmd_pec >> 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);
}
}
/*--------------Fonctions supplémentaires------------------------*/
void init_cfg(){
for(int i = 0; i<TOTAL_IC;i++){
tx_cfg[i][0] = 0x04;
tx_cfg[i][1] = 0x00;
tx_cfg[i][2] = 0x00;
tx_cfg[i][3] = 0x00;
tx_cfg[i][4] = 0x00;
tx_cfg[i][5] = 0x10;
}
}
void print_cells()
{
for (int current_ic = 0 ; current_ic < TOTAL_IC; current_ic++)
{
Serial.print(" IC ");
Serial.print(current_ic+1,DEC);
for(int i=0; i<CELL_NUM; i++)
{
Serial.print(" C");
Serial.print(i+1,DEC);
Serial.print(":");
Serial.print(cell_codes[current_ic][i]*.0001, 4);
Serial.print(",");
}
Serial.println();
}
}
/*-----------------------------------MAIN-------------------------------------*/
/*----------------------------------------------------------------------------*/
void setup(){
Serial.begin(115200);
void loop(){
}