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.
Diff: main.cpp
- Revision:
- 2:6700d7f380bd
- Parent:
- 1:1214d9ff2f14
- Child:
- 3:d6b68897773f
--- a/main.cpp Fri Mar 20 23:40:18 2020 +0000
+++ b/main.cpp Sun Mar 22 15:37:36 2020 +0000
@@ -1,11 +1,11 @@
#include "mbed.h"
#include <string>
using std::string;
-//PITATI GDE SE DEFINISU FRAMEOVI
-#define FRMWRT_SGL_R 0x80 // single device read with response
-#define FRMWRT_SGL_NR 0x90 // single device write without response
-#define FRMWRT_ALL_R 0xC0 // general broadcast read with response
-#define FRMWRT_ALL_NR 0xD0 // general broadcast write without response
+
+#define FRMWRT_SGL_R 0x00 // single device read with response
+#define FRMWRT_SGL_NR 0x10 // single device write without response
+#define FRMWRT_ALL_R 0x40 // general broadcast read with response
+#define FRMWRT_ALL_NR 0x50 // general broadcast write without response
#define CONFIG 0x001
#define COMM_CTRL 0x20
@@ -14,9 +14,12 @@
#define COMM_TO 0x23
#define TX_HOLD_OFF 0x22
#define CONTROL2 0x106
+#define SYSFLT1_FLT_RST 0x13B
+#define SYSFLT1_FLT_MSK 0x16
+#define VCELL1H 0x215
typedef unsigned char BYTE;
-//CRC TABELA REKLA MARTA DA JE ISTA, PROVERITI!
+
const uint16_t crc16_table[256] = {
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
@@ -51,12 +54,12 @@
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
};
-//PROVERITI PINOVE
-DigitalOut bmsLVWakeUp(PB_0);//PC_9);
-DigitalIn bmsFault(PA_4);//PC_8);
+//ISTI KAO MARTINI, DOGOVOR, PROMENITI U ODNOSU NA LV MASTER
+DigitalOut bmsLVWakeUp(PB_0);
+DigitalIn bmsFault(PA_4);
-Serial pc(PA_0, PA_1, 250000);//PA_9, PA_10,250000);
-Serial pc1(USBTX, USBRX, 9600);//PC_10, PC_11,9600);
+Serial pc(PA_0, PA_1, 250000);
+Serial pc1(USBTX, USBRX, 9600);
void wakeUp()
{
@@ -128,8 +131,6 @@
*pBuf++ = (wCRC & 0xFF00) >> 8;
bPktLen += 2;
- //sciSend(scilinREG, bPktLen, pFrame);
-
posaljiUARTu(bPktLen,pFrame);
return bPktLen;
@@ -224,6 +225,7 @@
return WriteFrame(bID, wAddr, &bReturn, 1, FRMWRT_SGL_R);
}
+
// eg: ReadReg(nDev_ID,0x0207,bFrame,12, 0, FRMWRT_SGL_R)
int ReadReg (BYTE DevID, uint16_t wAddr, void * bufferData, BYTE numDATAb, uint32_t dwTimeOut, BYTE FRM)
@@ -285,33 +287,33 @@
pc1.printf("%X ",recBuff[i]);
}
- pc1.printf("\n\n- - - VOLTAGE - - -\n");
+ /*pc1.printf("\n\n- - - VOLTAGE - - -\n");
for(int i = 1; i < recBuff[0] - 1; i += 2){
-
int voltage = recBuff[i+1];
voltage |= (recBuff[i]) << 8;
double vol = ((double)voltage)/65536.0 * 5.0;
pc1.printf("CELL[%d] = %.6f V\n",(recBuff[0] - 1)/2 -(i-1)/2,vol);
- }
+ }*/
pc1.printf("\n");
}
int main()
{
+ bmsLVWakeUp = 1; //po defaultu je HIGHT
+
uint32_t wTemp = 0;
- pc.attach(&callback);//sta radi callback?
+ pc.attach(&callback);
- //sciSetBaudrate(scilinREG, 250000);
pc1.printf("INICIJALIZACIJA START \n");
wakeUp();
- //sciSetBaudrate(scilinREG,250000);
- //CommReset();
- WriteReg(0,COMM_CTRL,0x3C3C,2, FRMWRT_ALL_NR);//setting the baud rate to device
- //sciSetBaudrate(scilinREG,1000000);
-
- WriteReg(0,ECC_TEST,0x00,1, FRMWRT_ALL_NR); //dummy write to the ECC_TESTregisterto syncthe DLL
+ //CommReset(); <<<-----
+
+ //set 250K baud to BMS first board
+ WriteReg(0,COMM_CTRL,0x343C,2, FRMWRT_ALL_NR);//setting the baud rate to device
+ //****PROVERITI***?
+ WriteReg(0,ECC_TEST,0x00,1, FRMWRT_ALL_NR); //dummy write to the ECC_TESTregisterto syncthe DLL
WriteReg(0,CONFIG,0x00,1, FRMWRT_ALL_NR);//making sure auto-address mode is set on all devices(and NOT GPIO address mode)
@@ -321,38 +323,15 @@
//****
//ReadReg(0, uint16_t wAddr, void * bufferData, 1, uint32_t dwTimeOut, FRMWRT_SGL_R)//dummy read of ECC_TEST DORADITI!
//****
-
- WriteReg(0, COMM_TO, 0x56, 1, FRMWRT_ALL_NR);//Setting Communications Timeout (10 minute short communication timeout, sleep mode on long communication timeout, long timeout length of 30 minutes)
-
+ //waitFrame();?????????????????
+ //COMM_TO DISABLED
+ WriteReg(0, COMM_TO, 0x00, 1, FRMWRT_ALL_NR);//noT(Setting Communications Timeout (10 minute short communication timeout, sleep mode on long communication timeout, long timeout length of 30 minutes))
WriteReg(0, TX_HOLD_OFF, 0x00, 1, FRMWRT_ALL_NR);//Communications transmit delay set to zero
//masking all low level faults (zakomentovacemo ono sto nam ne treba)
- WriteReg(0, 0x0002, 0x3F, 1, FRMWRT_ALL_NR);//GPIO
- WriteReg(0, 0x0003, 0x3F, 1, FRMWRT_ALL_NR);//UV
- WriteReg(0, 0x0004, 0x3F, 1, FRMWRT_ALL_NR);//OV
- WriteReg(0, 0x0005, 0x3F, 1, FRMWRT_ALL_NR);//UT
- WriteReg(0, 0x0006, 0x3F, 1, FRMWRT_ALL_NR);//OT
- WriteReg(0, 0x0007, 0x07, 1, FRMWRT_ALL_NR);//all tone faults
- WriteReg(0, 0x0008, 0x07, 1, FRMWRT_ALL_NR);//UART
- WriteReg(0, 0x0009, 0x3F, 1, FRMWRT_ALL_NR);//UART
- WriteReg(0, 0x000A, 0x3F, 1, FRMWRT_ALL_NR);//UART
- WriteReg(0, 0x000B, 0x03, 1, FRMWRT_ALL_NR);//UART
- WriteReg(0, 0x000C, 0x3F, 1, FRMWRT_ALL_NR);//COMH
- WriteReg(0, 0x000D, 0x3F, 1, FRMWRT_ALL_NR);//COMH
- WriteReg(0, 0x000E, 0x3F, 1, FRMWRT_ALL_NR);//COMH
- WriteReg(0, 0x000F, 0x03, 1, FRMWRT_ALL_NR);//COMH
- WriteReg(0, 0x0010, 0x3F, 1, FRMWRT_ALL_NR);//COML
- WriteReg(0, 0x0011, 0x3F, 1, FRMWRT_ALL_NR);//COML
- WriteReg(0, 0x0012, 0x3F, 1, FRMWRT_ALL_NR);//COML
- WriteReg(0, 0x0013, 0x03, 1, FRMWRT_ALL_NR);//COML
- WriteReg(0, 0x0014, 0x07, 1, FRMWRT_ALL_NR);//OTP
- WriteReg(0, 0x0015, 0xFF, 1, FRMWRT_ALL_NR);//POWER RAIL
- WriteReg(0, 0x0016, 0x7F, 1, FRMWRT_ALL_NR);//SYS_FAULT1
- WriteReg(0, 0x0017, 0xFF, 1, FRMWRT_ALL_NR);//SYS_FAULT2
- WriteReg(0, 0x0018, 0x7F, 1, FRMWRT_ALL_NR);//SYS_FAULT3
- WriteReg(0, 0x0019, 0x03, 1, FRMWRT_ALL_NR);//OVUV BIST
- WriteReg(0, 0x001A, 0xFF, 1, FRMWRT_ALL_NR);//OTUT BIST
+ WriteReg(0, SYSFLT1_FLT_RST, 0xFFFFFF, 3, FRMWRT_ALL_NR); //reset system faults
+ WriteReg(0, SYSFLT1_FLT_MSK, 0xFFFFFF, 3, FRMWRT_ALL_NR); //mask system faults (so we can test boards and not worry about triggering these faults accidentally)
//MASKING_END
@@ -363,7 +342,8 @@
ReadReg(0, 10, &wTemp, 1, 0,FRMWRT_SGL_R );
wait_ms(10);
wait(1);
-
+
+ //----------------------
WriteReg(0, CONTROL2, 0x00, 1, FRMWRT_ALL_NR);//Set OVUV_EN=0 before changing settings
WriteReg(0, 0x29, 0x3F, 1, FRMWRT_ALL_NR);//enabling over voltage under voltage for all 6? cell channels
@@ -371,24 +351,23 @@
WriteReg(0, 0x2B, 0x5B, 1, FRMWRT_ALL_NR);// set cell OV to 4.3V
WriteReg(0, CONTROL2, 0x04, 1, FRMWRT_ALL_NR);//OVUV_EN=1
+ //----------------------
WriteReg(0, 0x28, 0x3F, 1, FRMWRT_ALL_NR);//configure GPIO as AUX voltage(absolute voltage INSTEAD OF RATIOMETRIC) if necessary
WriteReg(0, 0x27, 0x00, 1, FRMWRT_SGL_NR);//modifying the ADC delay of each device individually, since there is only one...modify device 0 delay
WriteReg(0, 0x26, 0x08, 1, FRMWRT_ALL_NR);//AUX sample rate 1 MHz,128 decimation ratio
+ //Konsultuj se s DJalom da bi smo odredili sample i low pass filter koji je on stavio na ploci
WriteReg(0, 0x24, 0x23, 1, FRMWRT_ALL_NR);//1 MHz sample rate,64 decimation ratio,19.7Hz LPF
+ /* ????Odnosi se na DIETEMP pitati Djaleta
WriteReg(0, 0x25, 0x02, 1, FRMWRT_ALL_NR);//5 ms conversion interval if continuous conversion enabled
-
- /*
- nSent= WriteReg(0,CONTROL2,0x10,1, FRMWRT_ALL_NR);// enable TSREF-external temperature sensors
- delayms(2);// provides settling time for TSREF
*/
-
+
WriteReg(0, 0x109, 0x3F, 1, FRMWRT_ALL_NR);// enables ADC for all 6 cell channels
wait_ms(5);// ensure proper settling time for best accuracy
-
+ /* ODLUCILE SMO SE ZA CONTINUOUS READ
//WHICHEVER WE NEED
//One-Shot ADC Conversions
WriteReg(0, CONTROL2, 0x01, 1, FRMWRT_ALL_NR);//set the CELL_ADC_GO bit
@@ -397,22 +376,28 @@
//DORADI!
//ReadReg(0, 0x215, void * bufferData, 12, 0, FRMWRT_SGL_R);// will return 6 overhead byte sand 12 data bytes per device, highest device address responds first
wait_ms(1);
-
+ */
//Continuous ADC Conversions
WriteReg(0, 0x25, 0x0A, 1, FRMWRT_ALL_NR);// enable continuous conversion with 5ms conversion interval
- WriteReg(0, CONTROL2, 0x01, 1, FRMWRT_ALL_NR);//set the CELL_ADC_GO bit
+ WriteReg(0, CONTROL2, 0x01, 1, FRMWRT_ALL_R);//set the CELL_ADC_GO bit
+ waitFrame();
+
wait_ms(5);//delay for ADC accuracy
+
//da li je ovo ispod isto sto i Martin waitFrame? Ako jeste staviti ga u posebnu funkciju, ako nije proveriti da li je uopste potrebno
+ //ReadReg(0, VCELL1H, response_frame, 6*2, 0, FRMWRT_ALL_R); NJIHOV KOD
//ReadReg(0, 0x215, void * bufferData, 12, 0, FRMWRT_SGL_R);// will return 6 overhead byte sand 12 data bytes per device, highest device address responds first
wait_ms(1);//ISTA STVAR KAO IZNAD KOD ONE-SHOT
+
//martin deo koda
while (1) {
pc1.printf("Main Code \n");
wait(2);
//*****
- WriteReg(0, 2, 0x20, 1, FRMWRT_SGL_R); // send read sampled values command, OVO TREBA PROMENITI AKO VEC ZADRZAVAS OVAJ KOD!!!!!
- //*****
- waitFrame();
+ //WriteReg(0, 2, 0x20, 1, FRMWRT_SGL_R); // send read sampled values command, OVO TREBA PROMENITI AKO VEC ZADRZAVAS OVAJ KOD!!!!!
+ //ReadReg(0, VCELL1H, response_frame, 6*2, 0, FRMWRT_ALL_R); PREPORUCEN KOD
+ //waitFrame();
+
if(bmsFault)
pc1.printf("- - - GRESKA FAULT! - - -\n");