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

Committer:
garivetm
Date:
Thu Feb 18 15:40:24 2016 +0000
Revision:
2:c81dff9c8a93
Parent:
1:b69d05604535
Child:
4:0ed21bbd917b
Adding a use case example in the documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
garivetm 2:c81dff9c8a93 1 /***************************************************************************
garivetm 2:c81dff9c8a93 2 AUTHORS : BRUNO LARNAUDIE (Main functions) bruno.larnaudie@u-psud.fr
garivetm 2:c81dff9c8a93 3 MATHIEU GARIVET (Object Oriented aspect)
garivetm 2:c81dff9c8a93 4 *
garivetm 2:c81dff9c8a93 5 INSTITUTION : IUT de CACHAN - 9 av. de la div. Leclerc - 94230 CACHAN
garivetm 2:c81dff9c8a93 6
garivetm 2:c81dff9c8a93 7 VERSIONS : v1 (03/07/2012) : FIFO organisation
garivetm 2:c81dff9c8a93 8 v2 (18/02/2016) : Controller aand Peripheral organisation
garivetm 2:c81dff9c8a93 9
garivetm 2:c81dff9c8a93 10 ****************************************************************************
garivetm 2:c81dff9c8a93 11 Copyright 2016 LARNAUDIE GARIVET
garivetm 2:c81dff9c8a93 12
garivetm 2:c81dff9c8a93 13 Licensed under the Apache License, Version 2.0 (the "License");
garivetm 2:c81dff9c8a93 14 you may not use this file except in compliance with the License.
garivetm 2:c81dff9c8a93 15 You may obtain a copy of the License at
garivetm 2:c81dff9c8a93 16
garivetm 2:c81dff9c8a93 17 http://www.apache.org/licenses/LICENSE-2.0
garivetm 2:c81dff9c8a93 18
garivetm 2:c81dff9c8a93 19 Unless required by applicable law or agreed to in writing, software
garivetm 2:c81dff9c8a93 20 distributed under the License is distributed on an "AS IS" BASIS,
garivetm 2:c81dff9c8a93 21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
garivetm 2:c81dff9c8a93 22 See the License for the specific language governing permissions and
garivetm 2:c81dff9c8a93 23 limitations under the License.
garivetm 2:c81dff9c8a93 24 ***************************************************************************/
garivetm 0:ebe6f5e97160 25 #include "mbed.h"
garivetm 0:ebe6f5e97160 26 #include "ControllerCAN.h"
garivetm 0:ebe6f5e97160 27
garivetm 0:ebe6f5e97160 28 ControllerCAN::ControllerCAN() : can(p30, p29) {
garivetm 0:ebe6f5e97160 29 can.frequency(250000); // Baud rate : kbits/s
garivetm 1:b69d05604535 30 can.attach(this, &ControllerCAN::can_ISR_Reader); // CAN ISR
garivetm 0:ebe6f5e97160 31 FIFO_ecriture = 0;
garivetm 0:ebe6f5e97160 32 FIFO_lecture = 0;
garivetm 0:ebe6f5e97160 33 FIFO_occupation = 0;
garivetm 0:ebe6f5e97160 34 FIFO_max_occupation = 0;
garivetm 0:ebe6f5e97160 35 }
garivetm 0:ebe6f5e97160 36
garivetm 0:ebe6f5e97160 37 ControllerCAN::~ControllerCAN(){
garivetm 0:ebe6f5e97160 38 }
garivetm 0:ebe6f5e97160 39
garivetm 1:b69d05604535 40 void ControllerCAN::attach(PeripheralCAN* peripheral){
garivetm 1:b69d05604535 41 peripherals.push_back(peripheral);
garivetm 0:ebe6f5e97160 42 }
garivetm 0:ebe6f5e97160 43
garivetm 0:ebe6f5e97160 44 long ControllerCAN::writeData(long Id, const char *data, char len){
garivetm 0:ebe6f5e97160 45 CANMessage msg(Id, data, len);
garivetm 0:ebe6f5e97160 46 return (can.write(msg));
garivetm 0:ebe6f5e97160 47 }
garivetm 0:ebe6f5e97160 48
garivetm 0:ebe6f5e97160 49 long ControllerCAN::writeRemote(long Id){
garivetm 0:ebe6f5e97160 50 CANMessage msg(Id);
garivetm 0:ebe6f5e97160 51 return (can.write(msg));
garivetm 0:ebe6f5e97160 52 }
garivetm 0:ebe6f5e97160 53
garivetm 0:ebe6f5e97160 54 char ControllerCAN::FIFOread(void){
garivetm 0:ebe6f5e97160 55 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
garivetm 0:ebe6f5e97160 56 if(FIFO_occupation<0)
garivetm 0:ebe6f5e97160 57 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
garivetm 0:ebe6f5e97160 58 if(FIFO_max_occupation<FIFO_occupation)
garivetm 0:ebe6f5e97160 59 FIFO_max_occupation=FIFO_occupation;
garivetm 0:ebe6f5e97160 60 if(FIFO_occupation>SIZE_FIFO)
garivetm 1:b69d05604535 61 {}
garivetm 0:ebe6f5e97160 62 if(FIFO_occupation!=0)
garivetm 0:ebe6f5e97160 63 {
garivetm 0:ebe6f5e97160 64 char res = 0;
garivetm 1:b69d05604535 65 for (int i = 0; i < peripherals.size(); i++){
garivetm 1:b69d05604535 66 vector<unsigned short*> IdsRead = peripherals[i]->getIdsRead();
garivetm 0:ebe6f5e97160 67 for (int j = 0; j < IdsRead.size() ; j++){
garivetm 0:ebe6f5e97160 68 if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
garivetm 1:b69d05604535 69 peripherals[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]);
garivetm 0:ebe6f5e97160 70 res = 1;
garivetm 0:ebe6f5e97160 71 break;
garivetm 0:ebe6f5e97160 72 }
garivetm 0:ebe6f5e97160 73 }
garivetm 0:ebe6f5e97160 74 if(res) break;
garivetm 0:ebe6f5e97160 75 }
garivetm 0:ebe6f5e97160 76 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 77 return 1;
garivetm 0:ebe6f5e97160 78 }
garivetm 0:ebe6f5e97160 79 return -1;
garivetm 0:ebe6f5e97160 80 }
garivetm 0:ebe6f5e97160 81
garivetm 0:ebe6f5e97160 82 void ControllerCAN::can_ISR_Reader(void){
garivetm 0:ebe6f5e97160 83 if (can.read(can_MsgRx[FIFO_ecriture]))
garivetm 0:ebe6f5e97160 84 {
garivetm 0:ebe6f5e97160 85 // FIFO gestion
garivetm 0:ebe6f5e97160 86 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 87 }
garivetm 1:b69d05604535 88 }