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:
1:b69d05604535
Parent:
0:ebe6f5e97160
Child:
2:c81dff9c8a93

File content as of revision 1:b69d05604535:

#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);        // CAN ISR
    FIFO_ecriture = 0;
    FIFO_lecture = 0;
    FIFO_occupation = 0;
    FIFO_max_occupation = 0;
}

ControllerCAN::~ControllerCAN(){
}

void ControllerCAN::attach(PeripheralCAN* peripheral){
    peripherals.push_back(peripheral);
}

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)
        {}
    if(FIFO_occupation!=0)
    {
        char res = 0;
        for (int i = 0; i < peripherals.size(); i++){
            vector<unsigned short*> IdsRead = peripherals[i]->getIdsRead();
            for (int j = 0; j < IdsRead.size() ; j++){
                if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
                    peripherals[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::can_ISR_Reader(void){
    if (can.read(can_MsgRx[FIFO_ecriture]))
    {
      // FIFO gestion
       FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
    }
}