Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 3:a0e12d12f780, committed 2021-07-25
- Comitter:
- dmwahl
- Date:
- Sun Jul 25 18:15:36 2021 +0000
- Parent:
- 2:7083c7257556
- Commit message:
- Initial rough start to the BQ76PL536A (or BQ76PL536A-Q1) code
Changed in this revision
--- a/bq76pl536a.cpp Thu Jul 30 01:55:05 2020 +0000
+++ b/bq76pl536a.cpp Sun Jul 25 18:15:36 2021 +0000
@@ -1,3 +1,13 @@
+///-----------------------------------------------------------------
+/// Description: BQ76PL536A Driver
+/// Author: David Wahl
+/// Date: 13-JUL-2021
+/// Notes: Initial release
+///
+/// Revision History:
+/// Name: Date: Description:
+///-----------------------------------------------------------------
+
#include "bq76pl536a.h"
//===========================================================================
// Default constructor. Setup spi interface and run init functions
@@ -14,7 +24,7 @@
DigitalOut cs(_cs);
cs = 1;
spi.format(8,1);
- spi.frequency(500000);
+ spi.frequency(2.5e6);
cs=0;
wait_ms(1);
@@ -26,6 +36,17 @@
};
+//===========================================================================
+// Convert raw values to cell voltage
+float BQ76PL536A::cellVolts(u8t lobyte, u8t hibyte)
+//===========================================================================
+{
+ float cv = (((((u16t)lobyte << 8) + (u16t)hibyte)*6250/16383)/1000.0); // [V]
+ //float cv = (((lobyte * 256) + hibyte)*6250/16383); // [mV]
+
+ return cv;
+};
+//===========================================================================
//===========================================================================
@@ -51,15 +72,10 @@
{
for(u8t i=0; i<numDev; i++) {
write(DISCOVERY_ADDR, ADDRESS_CONTROL_REG, i+1);
-
- //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
}
}
-//===========================================================================
+/*//===========================================================================
// Device status
u8t BQ76PL536A::devStatus()
//===========================================================================
@@ -69,15 +85,25 @@
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.
-bool *BQ76PL536A::read(u8t deviceAddress, u8t regAddress, u8t count, u8t *pRegisterValue)
+bool BQ76PL536A::readAll(u8t deviceAddress)
+//===========================================================================
+{
+ //error = 0;
+ read(deviceAddress,0,64,bqPackData);
+ return false;
+}
+
+//===========================================================================
+// 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;
+ bool crcGood = true;
u8t logicalAddress = (deviceAddress << 1 | 0); // Shift address left 1 bit
u8t crcInput[3+count];
crcInput[0] = logicalAddress;
@@ -96,12 +122,14 @@
crcInput[3+i] = pRegisterValue[i];
}
if (i == count) {
- if (pec(crcInput,3+count) != pRegisterValue[i])
+ if (pec(crcInput,3+count) != pRegisterValue[i]) {
error = 1;
+ crcGood = false;
+ }
}
}
cs = 1; //End of transmission, slave select high
- return false;
+ return crcGood;
}
//===========================================================================
--- a/bq76pl536a.h Thu Jul 30 01:55:05 2020 +0000
+++ b/bq76pl536a.h Sun Jul 25 18:15:36 2021 +0000
@@ -1,3 +1,13 @@
+///-----------------------------------------------------------------
+/// Description: BQ76PL536A Driver
+/// Author: David Wahl
+/// Date: 13-JUL-2021
+/// Notes: Initial release
+///
+/// Revision History:
+/// Name: Date: Description:
+///-----------------------------------------------------------------
+
#ifndef BQ76PL536A_H
#define BQ76PL536A_H
@@ -9,14 +19,12 @@
protected:
SPI spi;
DigitalOut cs;
-
- bool *read(u8t deviceAddress, u8t regAddress, u8t count, u8t *pRegisterValue);
- void write(u8t deviceAddress, u8t regAddress, u8t regData);
-
- void adcConvert();
+
+
+
void setAddresses();
void softReset();
-
+
u8t pec(u8t crcBuffer[], u8t crcLength);
public:
@@ -25,12 +33,21 @@
//BQ76PL536A(SPI _spi, PinName _cs);
BQ76PL536A();
+
+ bool read(u8t deviceAddress, u8t regAddress, u8t count, u8t *pRegisterValue);
+ void write(u8t deviceAddress, u8t regAddress, u8t regData);
+
+ void adcConvert();
+ bool readAll(u8t deviceAddress);
+
u8t error, balanceTimeout;
u16t cov, cuv;
bool balanceEnabled;
u8t numDev, attachedCells[];
-
- u8t devStatus();
+
+ u8t bqPackData[65];
+
+ float cellVolts(u8t lobyte, u8t hibyte);
};
#endif
\ No newline at end of file
--- a/bq76pl536a_typedefs.h Thu Jul 30 01:55:05 2020 +0000
+++ b/bq76pl536a_typedefs.h Sun Jul 25 18:15:36 2021 +0000
@@ -1,3 +1,13 @@
+///-----------------------------------------------------------------
+/// Description: BQ76PL536A Driver
+/// Author: David Wahl
+/// Date: 13-JUL-2021
+/// Notes: Initial release
+///
+/// Revision History:
+/// Name: Date: Description:
+///-----------------------------------------------------------------
+
#ifndef BQ76PL536A_TYPEDEFS_H
#define BQ76PL536A_TYPEDEFS_H
@@ -72,12 +82,12 @@
0x8A,0x8D,0x84,0x83,0xDE,0xD9,0xD0,0xD7,0xC2,0xC5,0xCC,0xCB,0xE6,0xE1,0xE8,0xEF,0xFA,
0xFD,0xF4,0xF3};
-//*BQ Pack special addresses*/
+//*BQ76PL536A special addresses*/
#define BROADCAST_ADDR 0x3F
#define DISCOVERY_ADDR 0x00
-#define BQ76PL536_RESET 0xa5
+#define BQ76PL536A_RESET 0xa5
-//Definition of the BQ76PL536 registers
+//Definition of the BQ76PL536A registers
typedef enum BQ_DEV_REGS
{
DEVICE_STATUS_REG=0x00,
@@ -130,12 +140,6 @@
BQ_SPI_REG_MAX=0x4F
} bq_dev_regs_t;
-
-//*BQ Pack special addresses*/
-#define BROADCAST_ADDR 0x3F
-#define DISCOVERY_ADDR 0x00
-#define BQ76PL536A_RESET 0xa5
-
//delay after bq calls
#define BQDELAY 10
//----------------------------------------------------------------------------------------