Initial rough start to the BQ76PL536A (or BQ76PL536A-Q1) code. Free to use as long as you make any changes/improvements publicly available. Please notify me of any improvements.

bq76pl536a.cpp

Committer:
dmwahl
Date:
2017-05-14
Revision:
0:47a258755906
Child:
1:4ae1a58697d7

File content as of revision 0:47a258755906:

#include "bq76pl536a.h"
//===========================================================================
// Default constructor. Setup spi interface and run init functions
BQ76PL536A::BQ76PL536A(SPI _spi, PinName _cs, u8t _numDev,  u16t _cov, u16t _cuv, u8t _balanceTimeout, bool _balanceEnabled)
//===========================================================================
:   spi(_spi),
    cs(_cs),
    numDev(_numDev),
    cov(_cov),
    cuv(_cuv),
    balanceTimeout(_balanceTimeout),
    balanceEnabled(_balanceEnabled)
{
    cs = 1;
    softReset();
    setAddresses();
};




//===========================================================================
// Start conversion on all devices
void BQ76PL536A::adcConvert()
//===========================================================================
{
    write(BROADCAST_ADDR, ADC_CONVERT_REG, 1);
}

//===========================================================================
// Address devices
void BQ76PL536A::setAddresses()
//===========================================================================
{
    // Address devices
    for(u8t i=1; i<numDev; i++)    {
        write(DISCOVERY_ADDR, ADDRESS_CONTROL_REG, i); //Write address to first discovered address
        // write 1 then 0 to Alert Status Reg to remove alert
        //write(i, ALERT_STATUS_REG, 0b10000000); //Write 1 to address bit of alert register
        //write(i, ALERT_STATUS_REG, 0b00000000); //Write 0 to address bit of alert register to clear alert
    }
}

//===========================================================================
// Reset all devices in stack
void BQ76PL536A::softReset()
//===========================================================================
{
    write(BROADCAST_ADDR, RESET_REG, BQ76PL536A_RESET);
}

//===========================================================================
// Read one or more bytes from the BQ76 device stack. Count does not include CRC byte.
bool *BQ76PL536A::read(u8t deviceAddress, u8t regAddress, u8t count, u8t *pRegisterValue)
//===========================================================================
{
    error = 0;
    u8t logicalAddress = (deviceAddress << 1 | 0); // Shift address left 1 bit
    //static u8t receivedData[length];
    //memset(receivedData, 0, length);
    static u8t crcInput[3] = {logicalAddress,regAddress,count};

    cs = 0;
    spi.write(logicalAddress);
    spi.write(regAddress);
    spi.write(count);

    //Read data. Last byte is CRC data.
    for (int i = 0; i <= count; i++) {
        pRegisterValue[i] = spi.write(0x00);
        if (i == count) {
            if (pec(crcInput) != pRegisterValue[i])
                error = 1;
        }
    }
    cs = 1; //End of transmission, slave select high

    return false;
}

//===========================================================================
// Write one byte to the BQ76 device stack
void BQ76PL536A::write(u8t deviceAddress, u8t regAddress, u8t regData)
//===========================================================================
{
    u8t logicalAddress = ((deviceAddress << 1) | 1); // Shift address left one bit and set LSB
    u8t crcInput[3] = {logicalAddress, regAddress, regData};

    cs = 1;
    spi.write(logicalAddress);
    spi.write(regAddress);
    spi.write(regData);
    spi.write(pec(crcInput));
    cs = 1;
}

//===========================================================================
// Calculate CRC byte to verify data integrity
u8t BQ76PL536A::pec(u8t crcBuffer[])
//===========================================================================
{
    u8t crc = 0;
    int temp = 0;
    for (int i = 0; i < sizeof(crcBuffer)/sizeof(crcBuffer[0]) + 1; i++) {
        temp = crc ^ crcBuffer[i];
        crc = crcTable[temp];
    }
    return crc;
}