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

Committer:
garivetm
Date:
Sat Feb 06 14:18:08 2016 +0000
Revision:
1:b69d05604535
Parent:
0:ebe6f5e97160
Child:
2:c81dff9c8a93
Naming corrections

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garivetm 0:ebe6f5e97160 1 #include "mbed.h"
garivetm 0:ebe6f5e97160 2 #include "ControllerCAN.h"
garivetm 0:ebe6f5e97160 3
garivetm 0:ebe6f5e97160 4 ControllerCAN::ControllerCAN() : can(p30, p29) {
garivetm 0:ebe6f5e97160 5 can.frequency(250000); // Baud rate : kbits/s
garivetm 1:b69d05604535 6 can.attach(this, &ControllerCAN::can_ISR_Reader); // CAN ISR
garivetm 0:ebe6f5e97160 7 FIFO_ecriture = 0;
garivetm 0:ebe6f5e97160 8 FIFO_lecture = 0;
garivetm 0:ebe6f5e97160 9 FIFO_occupation = 0;
garivetm 0:ebe6f5e97160 10 FIFO_max_occupation = 0;
garivetm 0:ebe6f5e97160 11 }
garivetm 0:ebe6f5e97160 12
garivetm 0:ebe6f5e97160 13 ControllerCAN::~ControllerCAN(){
garivetm 0:ebe6f5e97160 14 }
garivetm 0:ebe6f5e97160 15
garivetm 1:b69d05604535 16 void ControllerCAN::attach(PeripheralCAN* peripheral){
garivetm 1:b69d05604535 17 peripherals.push_back(peripheral);
garivetm 0:ebe6f5e97160 18 }
garivetm 0:ebe6f5e97160 19
garivetm 0:ebe6f5e97160 20 long ControllerCAN::writeData(long Id, const char *data, char len){
garivetm 0:ebe6f5e97160 21 CANMessage msg(Id, data, len);
garivetm 0:ebe6f5e97160 22 return (can.write(msg));
garivetm 0:ebe6f5e97160 23 }
garivetm 0:ebe6f5e97160 24
garivetm 0:ebe6f5e97160 25 long ControllerCAN::writeRemote(long Id){
garivetm 0:ebe6f5e97160 26 CANMessage msg(Id);
garivetm 0:ebe6f5e97160 27 return (can.write(msg));
garivetm 0:ebe6f5e97160 28 }
garivetm 0:ebe6f5e97160 29
garivetm 0:ebe6f5e97160 30 char ControllerCAN::FIFOread(void){
garivetm 0:ebe6f5e97160 31 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
garivetm 0:ebe6f5e97160 32 if(FIFO_occupation<0)
garivetm 0:ebe6f5e97160 33 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
garivetm 0:ebe6f5e97160 34 if(FIFO_max_occupation<FIFO_occupation)
garivetm 0:ebe6f5e97160 35 FIFO_max_occupation=FIFO_occupation;
garivetm 0:ebe6f5e97160 36 if(FIFO_occupation>SIZE_FIFO)
garivetm 1:b69d05604535 37 {}
garivetm 0:ebe6f5e97160 38 if(FIFO_occupation!=0)
garivetm 0:ebe6f5e97160 39 {
garivetm 0:ebe6f5e97160 40 char res = 0;
garivetm 1:b69d05604535 41 for (int i = 0; i < peripherals.size(); i++){
garivetm 1:b69d05604535 42 vector<unsigned short*> IdsRead = peripherals[i]->getIdsRead();
garivetm 0:ebe6f5e97160 43 for (int j = 0; j < IdsRead.size() ; j++){
garivetm 0:ebe6f5e97160 44 if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
garivetm 1:b69d05604535 45 peripherals[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]);
garivetm 0:ebe6f5e97160 46 res = 1;
garivetm 0:ebe6f5e97160 47 break;
garivetm 0:ebe6f5e97160 48 }
garivetm 0:ebe6f5e97160 49 }
garivetm 0:ebe6f5e97160 50 if(res) break;
garivetm 0:ebe6f5e97160 51 }
garivetm 0:ebe6f5e97160 52 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 53 return 1;
garivetm 0:ebe6f5e97160 54 }
garivetm 0:ebe6f5e97160 55 return -1;
garivetm 0:ebe6f5e97160 56 }
garivetm 0:ebe6f5e97160 57
garivetm 0:ebe6f5e97160 58 void ControllerCAN::can_ISR_Reader(void){
garivetm 0:ebe6f5e97160 59 if (can.read(can_MsgRx[FIFO_ecriture]))
garivetm 0:ebe6f5e97160 60 {
garivetm 0:ebe6f5e97160 61 // FIFO gestion
garivetm 0:ebe6f5e97160 62 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 63 }
garivetm 1:b69d05604535 64 }