Battery Management System LV - 2020/2021

Dependencies:   mbed

main.cpp

Committer:
minamax
Date:
2021-03-20
Revision:
3:4dc457ed63d5
Parent:
2:03a6da61d834

File content as of revision 3:4dc457ed63d5:

#include "mbed.h"
#include "bq79606.h"

// - - - PIN CONFIGURATION - - -

DigitalIn bmsFault(PB_4);
DigitalOut bmsWakeUp(PB_5);

// - - - UART CONFIGURATION - - -

Serial bms(PA_0, PA_1, 250000); //UART ka BMS Slaveu
Serial pc1(USBTX, USBRX, 9600);//PC_10, PC_11,9600); //UART ka PCu Serijskom monitoru

CAN can1(PB_8, PB_9, 500000);
CANMessage message1;
        

BYTE recBuff[1024];
volatile int recLen=0;
volatile int expected=0;
volatile bool full = false;
volatile int rdLen=0;
int counter = 0;

uint8_t pFrame1[(MAXBYTES+6)*TOTALBOARDS];

void callback() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    //pc1.printf("* * *    Uspesan PRIJEM!     * * *\n");
    
        //pc1.printf("rec = %d", c);
        recBuff[recLen++] = bms.getc();
        if(expected==0) expected = recBuff[0]+7; //prvi bajt je (broj data - 1), +1 device id, +2 reg address, +2 CRC
        //pc1.printf("Prva rec = %d", recBuff[0]); 
        if(expected == recLen){
            //pc1.printf("\n\n- - - USAO U EXPECTED - - -\n"); 
            full = true;
            rdLen = expected;
            expected = 0;
            recLen = 0;
        }
        
    
    //full = true;
    //recLen = 0;
    //rdLen = 17; //samo test - nebitno koji broj
}

void waitFrame(){
    while(!full);
    //wait(2);
    full=false;
    pc1.printf("\n%d\n", rdLen);
    for(int i = 0;i<rdLen;i++){
        pc1.printf("%X ",recBuff[i]);
    }
    
    pc1.printf("\n\n- - - VOLTAGE - - -\n"); 
    message1.id = 0x71;
    int j = 0;
    for(int i = 4; i < recBuff[0] + 4; i += 2){
        int voltage = recBuff[i+1];   //LSB
        voltage |= (recBuff[i]) << 8; //MSB
        double vol = voltage*0.0001907349;
        //double vol = ((double)voltage)/65536.0 * 5.0;
        pc1.printf("CELL[%d] = %6.2f V\n", i/2-1, vol);
        
        message1.data[j++] = recBuff[i];
        
    }
    can1.write(message1);
      
    pc1.printf("\n");
}


void waitFrameTemp(){
    while(!full);
    //wait(2);
    full=false;
    pc1.printf("\n%d\n", rdLen);
    for(int i = 0;i<rdLen;i++){
        pc1.printf("%X ",recBuff[i]);
    }
    
    
        int voltage = recBuff[5];   //LSB
        voltage |= (recBuff[4]) << 8; //MSB
        double vol = voltage*0.0001907349;
        //double vol = ((double)voltage)/65536.0 * 5.0;
        pc1.printf("temp1  = %6.2f V\n", vol);
   
}
void waitFrameResponse(){
    while(!full);
    full = false;
    for(int i = 0;i < rdLen; i++){
        pc1.printf("%X ", recBuff[i]);
    }
    pc1.printf("\n");
}


int main(){
    bms.attach(&callback);
    Wake79606();
    //DigitalOut(PA_0, 0);
    //DigitalOut(PA_1, 0);
    //wait(1);
    //DigitalOut(PA_0, 1);
    //DigitalOut(PA_1, 1);
    //bms(PA_0, PA_1, 250000);
    bms.baud(10);
    bms.send_break();
    bms.baud(250000);
    //wait_ms(500);
    //bms.clear_break();
    
    wait(2); //marta rekla da mozda treba da se doda wait
    AutoAddress();
    
    
    //WriteReg(0, COMM_CTRL, 0x343C, 2, FRMWRT_ALL_NR); //mask GPIO faults
    
    wait(2);
    init();
    /*WriteReg(0, COMM_TO, 0x00, 1, FRMWRT_ALL_NR); //disable COMM timeout because printf takes a long time between reads
    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)

    //SET UP MAIN ADC
    WriteReg(0, CELL_ADC_CTRL, 0x3F, 1, FRMWRT_ALL_NR);     //enable conversions for all cells
    WriteReg(0, CELL_ADC_CONF2, 0x08, 1, FRMWRT_ALL_NR);    //set continuous ADC conversions, and set minimum conversion interval
    WriteReg(0, CONTROL2, 0x01, 1, FRMWRT_ALL_NR);          //CELL_ADC_GO = 1
    wait_ms(5);*/
    
    bmsWakeUp = 0;
    
    while (1) {
        pc1.printf("Main Code \n");
                
        pc1.printf("Board 0 \n");
        
        
        
        
        wait(2);
        //while(bms.readable()) bms.getc();
        int rdLen = ReadReg(0, VCELL1H , pFrame1, 6 , 0, FRMWRT_ALL_R); //6 bajtova jer cita od adrese VCELL1H po dva bajta za svaki kanal (ima 3 kanala)
        
        
        
        waitFrame();
        
        //ReadReg(0, AUX_GPIO1H, pFrame1, 2 , 0, FRMWRT_ALL_R); 
        
        //waitFrameTemp();
        
        
        //slanje zahteva za GRESKAMA
        //ReadReg(0, 0x52, &wTemp, 2, 0); // 0ms timeout
        //waitFrameResponse();
        

    }
    
    
    
}