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.
Diff: bq76pl536a.cpp
- Revision:
- 1:4ae1a58697d7
- Parent:
- 0:47a258755906
- Child:
- 2:7083c7257556
--- a/bq76pl536a.cpp Sun May 14 20:40:36 2017 +0000 +++ b/bq76pl536a.cpp Wed Jul 29 03:11:51 2020 +0000 @@ -3,16 +3,25 @@ // 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) + : spi(_spi), + cs(_cs), + numDev(_numDev), + cov(_cov), + cuv(_cuv), + balanceTimeout(_balanceTimeout), + balanceEnabled(_balanceEnabled) { + DigitalOut cs(_cs); cs = 1; - softReset(); + spi.format(8,1); + spi.frequency(500000); + + cs=0; + wait_ms(1); + cs=1; + wait_ms(1); + + //softReset(); setAddresses(); }; @@ -28,13 +37,23 @@ } //=========================================================================== +// Reset all devices in stack +void BQ76PL536A::softReset() +//=========================================================================== +{ + write(BROADCAST_ADDR, RESET_REG, BQ76PL536A_RESET); +} + +//=========================================================================== // 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 + for(u8t i=0; i<numDev; i++) { + write(i, ADDRESS_CONTROL_REG, i+1); //Write address to device 1 (for testing only) + + //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 @@ -42,12 +61,17 @@ } //=========================================================================== -// Reset all devices in stack -void BQ76PL536A::softReset() +// Device status +u8t BQ76PL536A::devStatus() //=========================================================================== { - write(BROADCAST_ADDR, RESET_REG, BQ76PL536A_RESET); + //update to use numDev + u8t currentStatus[1]; + read(1,DEVICE_STATUS_REG,1,currentStatus); + return currentStatus[0]; } +//=========================================================================== + //=========================================================================== // Read one or more bytes from the BQ76 device stack. Count does not include CRC byte. @@ -58,50 +82,56 @@ 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}; + u8t crcInput[3+count]; + crcInput[0] = logicalAddress; + crcInput[1] = regAddress; + crcInput[2] = count; cs = 0; - spi.write(logicalAddress); + spi.write(deviceAddress); 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) { + crcInput[3+i] = pRegisterValue[i]; + } if (i == count) { - if (pec(crcInput) != pRegisterValue[i]) + if (pec(crcInput,3+count) != pRegisterValue[i]) error = 1; } } cs = 1; //End of transmission, slave select high - return false; } //=========================================================================== -// Write one byte to the BQ76 device stack +// Write one byte to the BQ76 device stack. Should add code to read back written register and compare... void BQ76PL536A::write(u8t deviceAddress, u8t regAddress, u8t regData) //=========================================================================== { - u8t logicalAddress = ((deviceAddress << 1) | 1); // Shift address left one bit and set LSB + u8t logicalAddress = (deviceAddress << 1 | 1); // Shift address left one bit and set LSB u8t crcInput[3] = {logicalAddress, regAddress, regData}; - cs = 1; + cs = 0; spi.write(logicalAddress); spi.write(regAddress); spi.write(regData); - spi.write(pec(crcInput)); + spi.write(pec(crcInput, sizeof(crcInput)/sizeof(crcInput[0]))); cs = 1; } //=========================================================================== // Calculate CRC byte to verify data integrity -u8t BQ76PL536A::pec(u8t crcBuffer[]) +u8t BQ76PL536A::pec(u8t crcBuffer[], u8t crcLength) //=========================================================================== { + //u8t crcTestInput[3] = {0x01,0x3b,0x01}; // should return 0x02 u8t crc = 0; - int temp = 0; - for (int i = 0; i < sizeof(crcBuffer)/sizeof(crcBuffer[0]) + 1; i++) { + u8t temp = 0; + for (u8t i = 0; i < crcLength; i++) { temp = crc ^ crcBuffer[i]; crc = crcTable[temp]; }