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

Committer:
garivetm
Date:
Sat Feb 06 14:10:54 2016 +0000
Revision:
0:ebe6f5e97160
Child:
1:b69d05604535
First commit

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 0:ebe6f5e97160 6 can.attach(this, &ControllerCAN::can_ISR_Reader); // Fonction d'interruption CAN
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 0:ebe6f5e97160 16 void ControllerCAN::attach(PeripherialCAN* peripherial){
garivetm 0:ebe6f5e97160 17 peripherials.push_back(peripherial);
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 0:ebe6f5e97160 37 {}//while(!can.write(CANMessage(LCD_OVERFLOW,CANStandard)));
garivetm 0:ebe6f5e97160 38 if(FIFO_occupation!=0)
garivetm 0:ebe6f5e97160 39 {
garivetm 0:ebe6f5e97160 40 /*if(can_MsgRx[FIFO_lecture].id == Id)
garivetm 0:ebe6f5e97160 41 {
garivetm 0:ebe6f5e97160 42 //data = can_MsgRx[FIFO_lecture].data;
garivetm 0:ebe6f5e97160 43 msg = can_MsgRx[FIFO_lecture];
garivetm 0:ebe6f5e97160 44 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 45 return 1;
garivetm 0:ebe6f5e97160 46 }
garivetm 0:ebe6f5e97160 47 return 0;*/
garivetm 0:ebe6f5e97160 48 char res = 0;
garivetm 0:ebe6f5e97160 49 for (int i = 0; i < peripherials.size(); i++){
garivetm 0:ebe6f5e97160 50 vector<unsigned short*> IdsRead = peripherials[i]->getIdsRead();
garivetm 0:ebe6f5e97160 51 for (int j = 0; j < IdsRead.size() ; j++){
garivetm 0:ebe6f5e97160 52 if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
garivetm 0:ebe6f5e97160 53 peripherials[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]);
garivetm 0:ebe6f5e97160 54 res = 1;
garivetm 0:ebe6f5e97160 55 break;
garivetm 0:ebe6f5e97160 56 }
garivetm 0:ebe6f5e97160 57 }
garivetm 0:ebe6f5e97160 58 if(res) break;
garivetm 0:ebe6f5e97160 59 }
garivetm 0:ebe6f5e97160 60 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 61 return 1;
garivetm 0:ebe6f5e97160 62 }
garivetm 0:ebe6f5e97160 63 return -1;
garivetm 0:ebe6f5e97160 64 }
garivetm 0:ebe6f5e97160 65
garivetm 0:ebe6f5e97160 66 /*void ControllerCAN::FIFO_remove_msg(void){
garivetm 0:ebe6f5e97160 67 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 68 }*/
garivetm 0:ebe6f5e97160 69
garivetm 0:ebe6f5e97160 70 void ControllerCAN::can_ISR_Reader(void){
garivetm 0:ebe6f5e97160 71 if (can.read(can_MsgRx[FIFO_ecriture]))
garivetm 0:ebe6f5e97160 72 {
garivetm 0:ebe6f5e97160 73 // FIFO gestion
garivetm 0:ebe6f5e97160 74 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 75 }
garivetm 0:ebe6f5e97160 76 }
garivetm 0:ebe6f5e97160 77
garivetm 0:ebe6f5e97160 78 /**void ControllerCAN::CAN_automate_reception(void){
garivetm 0:ebe6f5e97160 79
garivetm 0:ebe6f5e97160 80 static signed char FIFO_lecture=0,FIFO_occupation=0,FIFO_max_occupation=0;
garivetm 0:ebe6f5e97160 81 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
garivetm 0:ebe6f5e97160 82 if(FIFO_occupation<0)
garivetm 0:ebe6f5e97160 83 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
garivetm 0:ebe6f5e97160 84 if(FIFO_max_occupation<FIFO_occupation)
garivetm 0:ebe6f5e97160 85 FIFO_max_occupation=FIFO_occupation;
garivetm 0:ebe6f5e97160 86 if(FIFO_occupation>SIZE_FIFO)
garivetm 0:ebe6f5e97160 87 //while(!can.write(CANMessage(LCD_OVERFLOW,CANStandard)));
garivetm 0:ebe6f5e97160 88 if(FIFO_occupation!=0)
garivetm 0:ebe6f5e97160 89 {
garivetm 0:ebe6f5e97160 90 switch(can_MsgRx[FIFO_lecture].id)
garivetm 0:ebe6f5e97160 91 {
garivetm 0:ebe6f5e97160 92 case MOT_G_DAT_1 :
garivetm 0:ebe6f5e97160 93 MOT_G_vit=(short)(can_MsgRx[FIFO_lecture].data[1]*256)+can_MsgRx[FIFO_lecture].data[0];
garivetm 0:ebe6f5e97160 94 MOT_G_i=(short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
garivetm 0:ebe6f5e97160 95 MOT_G_pos=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
garivetm 0:ebe6f5e97160 96 break;
garivetm 0:ebe6f5e97160 97 case MOT_G_DAT_2 :
garivetm 0:ebe6f5e97160 98 MOT_G_voltage=(unsigned short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
garivetm 0:ebe6f5e97160 99 MOT_G_temp1=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
garivetm 0:ebe6f5e97160 100 MOT_G_temp2=( short)(can_MsgRx[FIFO_lecture].data[7]*256)+can_MsgRx[FIFO_lecture].data[6];
garivetm 0:ebe6f5e97160 101 break;
garivetm 0:ebe6f5e97160 102 case MOT_D_DAT_1 :
garivetm 0:ebe6f5e97160 103 MOT_D_vit=(short)(can_MsgRx[FIFO_lecture].data[1]*256)+can_MsgRx[FIFO_lecture].data[0];
garivetm 0:ebe6f5e97160 104 MOT_D_i=(short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
garivetm 0:ebe6f5e97160 105 MOT_D_pos=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
garivetm 0:ebe6f5e97160 106 break;
garivetm 0:ebe6f5e97160 107 case MOT_D_DAT_2 :
garivetm 0:ebe6f5e97160 108 MOT_D_voltage=(unsigned short)(can_MsgRx[FIFO_lecture].data[3]*256)+can_MsgRx[FIFO_lecture].data[2];
garivetm 0:ebe6f5e97160 109 MOT_D_temp1=( short)(can_MsgRx[FIFO_lecture].data[5]*256)+can_MsgRx[FIFO_lecture].data[4];
garivetm 0:ebe6f5e97160 110 MOT_D_temp2=( short)(can_MsgRx[FIFO_lecture].data[7]*256)+can_MsgRx[FIFO_lecture].data[6];
garivetm 0:ebe6f5e97160 111 break;
garivetm 0:ebe6f5e97160 112 default:
garivetm 0:ebe6f5e97160 113 break;
garivetm 0:ebe6f5e97160 114 }
garivetm 0:ebe6f5e97160 115 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 116 }
garivetm 0:ebe6f5e97160 117 }**/