Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CODE

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

#include <stdint.

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 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<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);
}

void LTC6804_rdaux_reg(uint8_t reg,


uint8_t total_ic,
uint8_t *data
)
{
uint8_t cmd[4];
uint16_t cmd_pec;

//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);
}
}

/*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<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);

// Initialise le bus SPI


//pinMode(CS_PIN, OUTPUT);
SPI.begin(SCLK_PIN, MISO_PIN, MOSI_PIN, CS_PIN);
SPI.beginTransaction(SPISettings(1000000, MSBFIRST, SPI_MODE0));

LTC6804_initialize(); //Initialize LTC6804 hardware


init_cfg();
}

void loop(){
}

You might also like