1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| #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(){
} |
Partager