Initial rough copy

Dependencies:   BQ76PL536A mbed

Committer:
dmwahl
Date:
Thu Jul 30 01:55:19 2020 +0000
Revision:
1:af7a249bd2b2
Parent:
0:6e263c5ce85d
Child:
2:6c664dcbd052
read/write/pec working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dmwahl 0:6e263c5ce85d 1 #include "mbed.h"
dmwahl 0:6e263c5ce85d 2 #include "bq76pl536a.h"
dmwahl 0:6e263c5ce85d 3
dmwahl 0:6e263c5ce85d 4 // a SPI sub-class that provides a constructed default
dmwahl 0:6e263c5ce85d 5 class SPIPreInit : public SPI
dmwahl 0:6e263c5ce85d 6 {
dmwahl 0:6e263c5ce85d 7 public:
dmwahl 1:af7a249bd2b2 8 SPIPreInit(PinName mosi, PinName miso, PinName sclk, int freq, int bits, int mode) : SPI(mosi, miso, sclk)
dmwahl 1:af7a249bd2b2 9 {
dmwahl 0:6e263c5ce85d 10 frequency(freq);
dmwahl 0:6e263c5ce85d 11 format(bits, mode);
dmwahl 0:6e263c5ce85d 12 };
dmwahl 0:6e263c5ce85d 13 };
dmwahl 0:6e263c5ce85d 14
dmwahl 1:af7a249bd2b2 15 //SPIPreInit spi(PA_7, PA_6, A1, 500000, 8, 1);
dmwahl 1:af7a249bd2b2 16 SPI spi(PA_7, PA_6, A1);
dmwahl 1:af7a249bd2b2 17
dmwahl 1:af7a249bd2b2 18 u8t attachedCells[] = {5};
dmwahl 1:af7a249bd2b2 19 u8t numDev = sizeof(attachedCells)/sizeof(attachedCells[0]);
dmwahl 1:af7a249bd2b2 20 u8t numCells = 0;
dmwahl 0:6e263c5ce85d 21
dmwahl 0:6e263c5ce85d 22 // (SPI object, PinName cs, u8t numDev, u8t attachedCells[], u16t cov[mV], u16t cuv[mV], u8t balanceTimeout[sec], bool balanceEnabled)
dmwahl 1:af7a249bd2b2 23 BQ76PL536A bq_pack(spi, PB_0, numDev, 4000, 3000, 5, true);
dmwahl 0:6e263c5ce85d 24
dmwahl 0:6e263c5ce85d 25 Serial pc(SERIAL_TX, SERIAL_RX);
dmwahl 1:af7a249bd2b2 26 DigitalOut cs(D3);
dmwahl 0:6e263c5ce85d 27
dmwahl 0:6e263c5ce85d 28 int main()
dmwahl 0:6e263c5ce85d 29 {
dmwahl 1:af7a249bd2b2 30 //wait(1);
dmwahl 1:af7a249bd2b2 31 //spi.format(8,1);
dmwahl 1:af7a249bd2b2 32 //spi.frequency(50000);
dmwahl 0:6e263c5ce85d 33
dmwahl 1:af7a249bd2b2 34 //spi.lock();
dmwahl 1:af7a249bd2b2 35 //cs = 0;
dmwahl 1:af7a249bd2b2 36 //wait_ms(1);
dmwahl 1:af7a249bd2b2 37
dmwahl 1:af7a249bd2b2 38 //cs = 1;
dmwahl 1:af7a249bd2b2 39
dmwahl 1:af7a249bd2b2 40
dmwahl 1:af7a249bd2b2 41 //wait_ms(1);
dmwahl 1:af7a249bd2b2 42
dmwahl 1:af7a249bd2b2 43 //cs = 0;
dmwahl 1:af7a249bd2b2 44 //spi.write(0x01);
dmwahl 1:af7a249bd2b2 45 //spi.write(0x3b);
dmwahl 1:af7a249bd2b2 46 //spi.write(0x01);
dmwahl 1:af7a249bd2b2 47 //spi.write(0x02);
dmwahl 1:af7a249bd2b2 48 //cs=1;
dmwahl 1:af7a249bd2b2 49 //spi.unlock();
dmwahl 1:af7a249bd2b2 50
dmwahl 1:af7a249bd2b2 51 // spi.write(BROADCAST_ADDR);
dmwahl 1:af7a249bd2b2 52 //spi.write(RESET_REG);
dmwahl 1:af7a249bd2b2 53 //spi.write(BQ76PL536A_RESET);
dmwahl 1:af7a249bd2b2 54
dmwahl 1:af7a249bd2b2 55
dmwahl 1:af7a249bd2b2 56 for (int i = 0; i<numDev; i++) {
dmwahl 1:af7a249bd2b2 57 numCells +=attachedCells[i];
dmwahl 1:af7a249bd2b2 58 }
dmwahl 0:6e263c5ce85d 59 pc.baud(115200);
dmwahl 1:af7a249bd2b2 60 pc.printf("Starting up...\r\n");
dmwahl 1:af7a249bd2b2 61 bq_pack.devStatus();
dmwahl 1:af7a249bd2b2 62 //pc.printf("Status: %u\r\n",bq_pack.devStatus());
dmwahl 1:af7a249bd2b2 63 //pc.printf("COV: %u CUV: %u, numDev: %d numCells: %d\r\n", bq_pack.cov, bq_pack.cuv, bq_pack.numDev, numCells);
dmwahl 1:af7a249bd2b2 64 //pc.printf("%u %u %u %u %u\r\n", bq_pack.attachedCells[0], bq_pack.attachedCells[1], bq_pack.attachedCells[2], bq_pack.attachedCells[3], bq_pack.attachedCells[4]);
dmwahl 1:af7a249bd2b2 65 //pc.printf("%u %u %u %u\r\n", bq_pack.cellVolts[0], bq_pack.cellVolts[1], bq_pack.cellVolts[2], bq_pack.cellVolts[3]);
dmwahl 1:af7a249bd2b2 66
dmwahl 0:6e263c5ce85d 67 while(1) {
dmwahl 0:6e263c5ce85d 68 wait(1);
dmwahl 1:af7a249bd2b2 69 //u8t addr_ctrl = bq_pack.readRegister(1, ADDRESS_CONTROL_REG);
dmwahl 1:af7a249bd2b2 70
dmwahl 1:af7a249bd2b2 71 //bq_pack.readCells();
dmwahl 1:af7a249bd2b2 72
dmwahl 1:af7a249bd2b2 73 //pc.printf("%u %u %u %u %u\r\n", bq_pack.cellVolts[0], bq_pack.cellVolts[1], bq_pack.cellVolts[2], bq_pack.cellVolts[3], bq_pack.cellVolts[4]);
dmwahl 1:af7a249bd2b2 74
dmwahl 1:af7a249bd2b2 75 //pc.printf("%u\r\n",bq_pack.devStatus());
dmwahl 1:af7a249bd2b2 76 /*u8t data[8];
dmwahl 1:af7a249bd2b2 77 bq_pack.adcConvert();
dmwahl 1:af7a249bd2b2 78 wait_ms(10);
dmwahl 1:af7a249bd2b2 79 cs = 0;
dmwahl 1:af7a249bd2b2 80 spi.write(1);
dmwahl 1:af7a249bd2b2 81 spi.write(VCELL1_L_REG);
dmwahl 1:af7a249bd2b2 82 spi.write(8);
dmwahl 1:af7a249bd2b2 83 for (int i = 0; i<8; i++)
dmwahl 1:af7a249bd2b2 84 {
dmwahl 1:af7a249bd2b2 85 data[i] = spi.write(0x00);
dmwahl 1:af7a249bd2b2 86 }
dmwahl 1:af7a249bd2b2 87 cs = 1;
dmwahl 1:af7a249bd2b2 88 pc.printf("%u %u %u %u\r\n", (data[0]<<8)|data[1], (data[2]<<8)|data[3], (data[4]<<8)|data[5], (data[6]<<8)|data[7]);*/
dmwahl 0:6e263c5ce85d 89 }
dmwahl 0:6e263c5ce85d 90 }