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.

Committer:
dmwahl
Date:
Sun May 14 20:40:36 2017 +0000
Revision:
0:47a258755906
Child:
1:4ae1a58697d7
Basic class compiles

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 0:47a258755906 1 #include "bq76pl536a.h"
dmwahl 0:47a258755906 2 //===========================================================================
dmwahl 0:47a258755906 3 // Default constructor. Setup spi interface and run init functions
dmwahl 0:47a258755906 4 BQ76PL536A::BQ76PL536A(SPI _spi, PinName _cs, u8t _numDev, u16t _cov, u16t _cuv, u8t _balanceTimeout, bool _balanceEnabled)
dmwahl 0:47a258755906 5 //===========================================================================
dmwahl 0:47a258755906 6 : spi(_spi),
dmwahl 0:47a258755906 7 cs(_cs),
dmwahl 0:47a258755906 8 numDev(_numDev),
dmwahl 0:47a258755906 9 cov(_cov),
dmwahl 0:47a258755906 10 cuv(_cuv),
dmwahl 0:47a258755906 11 balanceTimeout(_balanceTimeout),
dmwahl 0:47a258755906 12 balanceEnabled(_balanceEnabled)
dmwahl 0:47a258755906 13 {
dmwahl 0:47a258755906 14 cs = 1;
dmwahl 0:47a258755906 15 softReset();
dmwahl 0:47a258755906 16 setAddresses();
dmwahl 0:47a258755906 17 };
dmwahl 0:47a258755906 18
dmwahl 0:47a258755906 19
dmwahl 0:47a258755906 20
dmwahl 0:47a258755906 21
dmwahl 0:47a258755906 22 //===========================================================================
dmwahl 0:47a258755906 23 // Start conversion on all devices
dmwahl 0:47a258755906 24 void BQ76PL536A::adcConvert()
dmwahl 0:47a258755906 25 //===========================================================================
dmwahl 0:47a258755906 26 {
dmwahl 0:47a258755906 27 write(BROADCAST_ADDR, ADC_CONVERT_REG, 1);
dmwahl 0:47a258755906 28 }
dmwahl 0:47a258755906 29
dmwahl 0:47a258755906 30 //===========================================================================
dmwahl 0:47a258755906 31 // Address devices
dmwahl 0:47a258755906 32 void BQ76PL536A::setAddresses()
dmwahl 0:47a258755906 33 //===========================================================================
dmwahl 0:47a258755906 34 {
dmwahl 0:47a258755906 35 // Address devices
dmwahl 0:47a258755906 36 for(u8t i=1; i<numDev; i++) {
dmwahl 0:47a258755906 37 write(DISCOVERY_ADDR, ADDRESS_CONTROL_REG, i); //Write address to first discovered address
dmwahl 0:47a258755906 38 // write 1 then 0 to Alert Status Reg to remove alert
dmwahl 0:47a258755906 39 //write(i, ALERT_STATUS_REG, 0b10000000); //Write 1 to address bit of alert register
dmwahl 0:47a258755906 40 //write(i, ALERT_STATUS_REG, 0b00000000); //Write 0 to address bit of alert register to clear alert
dmwahl 0:47a258755906 41 }
dmwahl 0:47a258755906 42 }
dmwahl 0:47a258755906 43
dmwahl 0:47a258755906 44 //===========================================================================
dmwahl 0:47a258755906 45 // Reset all devices in stack
dmwahl 0:47a258755906 46 void BQ76PL536A::softReset()
dmwahl 0:47a258755906 47 //===========================================================================
dmwahl 0:47a258755906 48 {
dmwahl 0:47a258755906 49 write(BROADCAST_ADDR, RESET_REG, BQ76PL536A_RESET);
dmwahl 0:47a258755906 50 }
dmwahl 0:47a258755906 51
dmwahl 0:47a258755906 52 //===========================================================================
dmwahl 0:47a258755906 53 // Read one or more bytes from the BQ76 device stack. Count does not include CRC byte.
dmwahl 0:47a258755906 54 bool *BQ76PL536A::read(u8t deviceAddress, u8t regAddress, u8t count, u8t *pRegisterValue)
dmwahl 0:47a258755906 55 //===========================================================================
dmwahl 0:47a258755906 56 {
dmwahl 0:47a258755906 57 error = 0;
dmwahl 0:47a258755906 58 u8t logicalAddress = (deviceAddress << 1 | 0); // Shift address left 1 bit
dmwahl 0:47a258755906 59 //static u8t receivedData[length];
dmwahl 0:47a258755906 60 //memset(receivedData, 0, length);
dmwahl 0:47a258755906 61 static u8t crcInput[3] = {logicalAddress,regAddress,count};
dmwahl 0:47a258755906 62
dmwahl 0:47a258755906 63 cs = 0;
dmwahl 0:47a258755906 64 spi.write(logicalAddress);
dmwahl 0:47a258755906 65 spi.write(regAddress);
dmwahl 0:47a258755906 66 spi.write(count);
dmwahl 0:47a258755906 67
dmwahl 0:47a258755906 68 //Read data. Last byte is CRC data.
dmwahl 0:47a258755906 69 for (int i = 0; i <= count; i++) {
dmwahl 0:47a258755906 70 pRegisterValue[i] = spi.write(0x00);
dmwahl 0:47a258755906 71 if (i == count) {
dmwahl 0:47a258755906 72 if (pec(crcInput) != pRegisterValue[i])
dmwahl 0:47a258755906 73 error = 1;
dmwahl 0:47a258755906 74 }
dmwahl 0:47a258755906 75 }
dmwahl 0:47a258755906 76 cs = 1; //End of transmission, slave select high
dmwahl 0:47a258755906 77
dmwahl 0:47a258755906 78 return false;
dmwahl 0:47a258755906 79 }
dmwahl 0:47a258755906 80
dmwahl 0:47a258755906 81 //===========================================================================
dmwahl 0:47a258755906 82 // Write one byte to the BQ76 device stack
dmwahl 0:47a258755906 83 void BQ76PL536A::write(u8t deviceAddress, u8t regAddress, u8t regData)
dmwahl 0:47a258755906 84 //===========================================================================
dmwahl 0:47a258755906 85 {
dmwahl 0:47a258755906 86 u8t logicalAddress = ((deviceAddress << 1) | 1); // Shift address left one bit and set LSB
dmwahl 0:47a258755906 87 u8t crcInput[3] = {logicalAddress, regAddress, regData};
dmwahl 0:47a258755906 88
dmwahl 0:47a258755906 89 cs = 1;
dmwahl 0:47a258755906 90 spi.write(logicalAddress);
dmwahl 0:47a258755906 91 spi.write(regAddress);
dmwahl 0:47a258755906 92 spi.write(regData);
dmwahl 0:47a258755906 93 spi.write(pec(crcInput));
dmwahl 0:47a258755906 94 cs = 1;
dmwahl 0:47a258755906 95 }
dmwahl 0:47a258755906 96
dmwahl 0:47a258755906 97 //===========================================================================
dmwahl 0:47a258755906 98 // Calculate CRC byte to verify data integrity
dmwahl 0:47a258755906 99 u8t BQ76PL536A::pec(u8t crcBuffer[])
dmwahl 0:47a258755906 100 //===========================================================================
dmwahl 0:47a258755906 101 {
dmwahl 0:47a258755906 102 u8t crc = 0;
dmwahl 0:47a258755906 103 int temp = 0;
dmwahl 0:47a258755906 104 for (int i = 0; i < sizeof(crcBuffer)/sizeof(crcBuffer[0]) + 1; i++) {
dmwahl 0:47a258755906 105 temp = crc ^ crcBuffer[i];
dmwahl 0:47a258755906 106 crc = crcTable[temp];
dmwahl 0:47a258755906 107 }
dmwahl 0:47a258755906 108 return crc;
dmwahl 0:47a258755906 109 }