Esempio n. 1
0
//Function that writes configuration of LTC6803-1/-3
void LTC6803_wrcfg(uint8_t total_ic,uint8_t config[][6])
{
  uint8_t BYTES_IN_REG = 6;
  uint8_t CMD_LEN = 2+(7*total_ic);
  uint8_t *cmd;
  uint16_t cfg_pec;
  uint8_t cmd_index; //command counter

  cmd = (uint8_t *)malloc(CMD_LEN*sizeof(uint8_t));

  cmd[0] = 0x01;
  cmd[1] = 0xc7;

  cmd_index = 2;
  for (uint8_t current_ic = total_ic; current_ic > 0; current_ic--)
  {

    for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++)
    {
      cmd[cmd_index] = config[current_ic-1][current_byte];
      cmd_index = cmd_index + 1;
    }

    cfg_pec = pec8_calc(BYTES_IN_REG, &config[current_ic-1][0]);    // calculating the PEC for each ICs configuration register data
    cmd[cmd_index ] = (uint8_t)cfg_pec;
    cmd_index = cmd_index + 1;
  }

  output_low(LTC6803_CS);
  spi_write_array(CMD_LEN, cmd);
  output_high(LTC6803_CS);
  free(cmd);
}
// Start an open wire Conversion
void ltc6811_adow(
  uint8_t MD, //ADC Mode
  uint8_t PUP //Discharge Permit
)
{

  uint8_t cmd[4];
  uint16_t cmd_pec;
  uint8_t md_bits;

  md_bits = (MD & 0x02) >> 1;
  cmd[0] = md_bits + 0x02;
  md_bits = (MD & 0x01) << 7;
  cmd[1] =  md_bits + 0x28 + (PUP<<6) ;//+ CH;

  cmd_pec = pec15_calc(2, cmd);
  cmd[2] = (uint8_t)(cmd_pec >> 8);
  cmd[3] = (uint8_t)(cmd_pec);

 // wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake. This command can be removed.
  output_low(LTC6811_CS);
  spi_write_array(4,cmd);
  output_high(LTC6811_CS);

}
/*
Starts cell voltage conversion
*/
void ltc6811_adcv(
  uint8_t MD, //ADC Mode
  uint8_t DCP, //Discharge Permit
  uint8_t CH //Cell Channels to be measured
)
{
  uint8_t cmd[4];
  uint16_t cmd_pec;
  uint8_t md_bits;

  md_bits = (MD & 0x02) >> 1;
  cmd[0] = md_bits + 0x02;
  md_bits = (MD & 0x01) << 7;
  cmd[1] =  md_bits + 0x60 + (DCP<<4) + CH;
  cmd_pec = pec15_calc(2, cmd);
  cmd[2] = (uint8_t)(cmd_pec >> 8);
  cmd[3] = (uint8_t)(cmd_pec);


  //wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake. This command can be removed.
  output_low(LTC6811_CS);
  spi_write_array(4,cmd);
  output_high(LTC6811_CS);

}
// Start a Status register redundancy test Conversion
void ltc6811_adstatd(
  uint8_t MD, //ADC Mode
  uint8_t CHST //GPIO Channels to be measured
)
{
  uint8_t cmd[4];
  uint16_t cmd_pec;
  uint8_t md_bits;

  md_bits = (MD & 0x02) >> 1;
  cmd[0] = md_bits + 0x04;
  md_bits = (MD & 0x01) << 7;
  cmd[1] = md_bits + 0x08 + CHST ;


  cmd_pec = pec15_calc(2, cmd);
  cmd[2] = (uint8_t)(cmd_pec >> 8);
  cmd[3] = (uint8_t)(cmd_pec);

//  wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake. This command can be removed.
  output_low(LTC6811_CS);
  spi_write_array(4,cmd);
  output_high(LTC6811_CS);

}
//Starts cell voltage and SOC conversion
void ltc6811_adcvsc(
  uint8_t MD, //ADC Mode
  uint8_t DCP //Discharge Permit
)
{
  uint8_t cmd[4];
  uint16_t cmd_pec;
  uint8_t md_bits;

  //1
  md_bits = (MD & 0x02) >> 1;
  cmd[0] = md_bits + 0x04;
  md_bits = (MD & 0x01) << 7;
  cmd[1] =  md_bits + 0x60 + (DCP<<4) +0x07;

  //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 ltc6811 isoSPI port is awake. This command can be removed.

  //4
  output_low(LTC6811_CS);
  spi_write_array(4,cmd);
  output_high(LTC6811_CS);

}
//Sends the poll adc command
uint8_t ltc6811_pladc()
{
  uint8_t cmd[4];
  uint16_t cmd_pec;
  uint8_t adc_state = 0xFF;

  cmd[0] = 0x07;
  cmd[1] = 0x14;
  cmd_pec = pec15_calc(2, cmd);
  cmd[2] = (uint8_t)(cmd_pec >> 8);
  cmd[3] = (uint8_t)(cmd_pec);

  //wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake. This command can be removed.
  output_low(LTC6811_CS);
  spi_write_array(4,cmd);
  adc_state = spi_read(0xFF);

  output_high(LTC6811_CS);
  return(adc_state);
}
//This function will block operation until the ADC has finished it's conversion
uint32_t ltc6811_pollAdc()
{
  uint32_t counter = 0;
  uint8_t finished = 0;
  uint8_t current_time = 0;
  uint8_t cmd[4];
  uint16_t cmd_pec;


  cmd[0] = 0x07;
  cmd[1] = 0x14;
  cmd_pec = pec15_calc(2, cmd);
  cmd[2] = (uint8_t)(cmd_pec >> 8);
  cmd[3] = (uint8_t)(cmd_pec);

  //wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake. This command can be removed.

  output_low(LTC6811_CS);
  spi_write_array(4,cmd);

  while ((counter<200000)&&(finished == 0))
  {
    current_time = spi_read(0xFF);
    if (current_time>0)
    {
      finished = 1;
    }
    else
    {
      counter = counter + 10;
    }
  }

  output_high(LTC6811_CS);


  return(counter);
}
//Start a Status Register Self Test Conversion
void ltc6811_statst(
  uint8_t MD, //ADC Mode
  uint8_t ST //Self Test
)
{
  uint8_t cmd[4];
  uint16_t cmd_pec;
  uint8_t md_bits;

  md_bits = (MD & 0x02) >> 1;
  cmd[0] = md_bits + 0x04;
  md_bits = (MD & 0x01) << 7;
  cmd[1] =  md_bits + ((ST&0x03)<<5) +0x0F;
  cmd_pec = pec15_calc(2, cmd);
  cmd[2] = (uint8_t)(cmd_pec >> 8);
  cmd[3] = (uint8_t)(cmd_pec);

  //wakeup_idle (); //This will guarantee that the ltc6811 isoSPI port is awake. This command can be removed.

  output_low(LTC6811_CS);
  spi_write_array(4,cmd);
  output_high(LTC6811_CS);

}