CAN library containing a CAN controller object handling a FIFO, and CAN peripherals attached to it.

ControllerCAN.cpp

Committer:
garivetm
Date:
2016-02-06
Revision:
0:ebe6f5e97160
Child:
1:b69d05604535

File content as of revision 0:ebe6f5e97160:

#include "mbed.h"
#include "ControllerCAN.h"

ControllerCAN::ControllerCAN() : can(p30, p29) {
    can.frequency(250000);              // Baud rate : kbits/s
    can.attach(this, &ControllerCAN::can_ISR_Reader);        // Fonction d'interruption CAN
    FIFO_ecriture = 0;
    FIFO_lecture = 0;
    FIFO_occupation = 0;
    FIFO_max_occupation = 0;
}

ControllerCAN::~ControllerCAN(){
}

void ControllerCAN::attach(PeripherialCAN* peripherial){
    peripherials.push_back(peripherial);
}

long ControllerCAN::writeData(long Id, const char *data, char len){
    CANMessage msg(Id, data, len);
    return (can.write(msg));
}

long ControllerCAN::writeRemote(long Id){
    CANMessage msg(Id);
    return (can.write(msg));
}

char ControllerCAN::FIFOread(void){
    FIFO_occupation=FIFO_ecriture-FIFO_lecture;
    if(FIFO_occupation<0)
        FIFO_occupation=FIFO_occupation+SIZE_FIFO;  
    if(FIFO_max_occupation<FIFO_occupation)
        FIFO_max_occupation=FIFO_occupation;
    if(FIFO_occupation>SIZE_FIFO)
        {}//while(!can.write(CANMessage(LCD_OVERFLOW,CANStandard)));
    if(FIFO_occupation!=0)
    {
        /*if(can_MsgRx[FIFO_lecture].id == Id)
        {                    
            //data = can_MsgRx[FIFO_lecture].data;
            msg = can_MsgRx[FIFO_lecture];
            FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
            return 1;
        }
        return 0;*/
        char res = 0;
        for (int i = 0; i < peripherials.size(); i++){
            vector<unsigned short*> IdsRead = peripherials[i]->getIdsRead();
            for (int j = 0; j < IdsRead.size() ; j++){
                if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
                    peripherials[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]);
                    res = 1;
                    break;
                }
            }
            if(res) break;
        }
        FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
        return 1;
    }
    return -1;
}

/*void ControllerCAN::FIFO_remove_msg(void){
    FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
}*/

void ControllerCAN::can_ISR_Reader(void){
    if (can.read(can_MsgRx[FIFO_ecriture]))
    {
      // FIFO gestion
       FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
    }
}

/**void ControllerCAN::CAN_automate_reception(void){
  
    static signed char FIFO_lecture=0,FIFO_occupation=0,FIFO_max_occupation=0;
    FIFO_occupation=FIFO_ecriture-FIFO_lecture;
    if(FIFO_occupation<0)
        FIFO_occupation=FIFO_occupation+SIZE_FIFO;  
    if(FIFO_max_occupation<FIFO_occupation)
        FIFO_max_occupation=FIFO_occupation;
    if(FIFO_occupation>SIZE_FIFO)
        //while(!can.write(CANMessage(LCD_OVERFLOW,CANStandard)));
    if(FIFO_occupation!=0)
    {
        switch(can_MsgRx[FIFO_lecture].id)
        {                    
            case MOT_G_DAT_1 :
                MOT_G_vit=(short)(can_MsgRx[FIFO_lecture].data[1]*256)+can_MsgRx[FIFO_lecture].data[0];
                MOT_G_i=(short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
                MOT_G_pos=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
                break;
            case MOT_G_DAT_2 : 
                MOT_G_voltage=(unsigned short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
                MOT_G_temp1=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
                MOT_G_temp2=( short)(can_MsgRx[FIFO_lecture].data[7]*256)+can_MsgRx[FIFO_lecture].data[6];
                break;
            case MOT_D_DAT_1 :
                MOT_D_vit=(short)(can_MsgRx[FIFO_lecture].data[1]*256)+can_MsgRx[FIFO_lecture].data[0];
                MOT_D_i=(short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
                MOT_D_pos=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
                break;
            case MOT_D_DAT_2 : 
                MOT_D_voltage=(unsigned short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
                MOT_D_temp1=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
                MOT_D_temp2=( short)(can_MsgRx[FIFO_lecture].data[7]*256)+can_MsgRx[FIFO_lecture].data[6];
                break;
            default:
                break;              
        }
    FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
    }
}**/