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

Committer:
garivetm
Date:
Sat May 05 13:46:16 2018 +0000
Revision:
4:0ed21bbd917b
Parent:
2:c81dff9c8a93
Child:
5:b23a3876bb1d
Make CAN instance static and update Pins to match with STM32L476RG

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 4:0ed21bbd917b 28 CAN ControllerCAN::can(PA_8, PA_9);
garivetm 4:0ed21bbd917b 29
garivetm 4:0ed21bbd917b 30 ControllerCAN::ControllerCAN(){
garivetm 0:ebe6f5e97160 31 can.frequency(250000); // Baud rate : kbits/s
garivetm 4:0ed21bbd917b 32 can.attach(callback(this, &ControllerCAN::can_ISR_Reader)); // CAN ISR
garivetm 0:ebe6f5e97160 33 FIFO_ecriture = 0;
garivetm 0:ebe6f5e97160 34 FIFO_lecture = 0;
garivetm 0:ebe6f5e97160 35 FIFO_occupation = 0;
garivetm 0:ebe6f5e97160 36 FIFO_max_occupation = 0;
garivetm 0:ebe6f5e97160 37 }
garivetm 0:ebe6f5e97160 38
garivetm 0:ebe6f5e97160 39 ControllerCAN::~ControllerCAN(){
garivetm 0:ebe6f5e97160 40 }
garivetm 0:ebe6f5e97160 41
garivetm 1:b69d05604535 42 void ControllerCAN::attach(PeripheralCAN* peripheral){
garivetm 1:b69d05604535 43 peripherals.push_back(peripheral);
garivetm 0:ebe6f5e97160 44 }
garivetm 0:ebe6f5e97160 45
garivetm 0:ebe6f5e97160 46 long ControllerCAN::writeData(long Id, const char *data, char len){
garivetm 0:ebe6f5e97160 47 CANMessage msg(Id, data, len);
garivetm 0:ebe6f5e97160 48 return (can.write(msg));
garivetm 0:ebe6f5e97160 49 }
garivetm 0:ebe6f5e97160 50
garivetm 0:ebe6f5e97160 51 long ControllerCAN::writeRemote(long Id){
garivetm 0:ebe6f5e97160 52 CANMessage msg(Id);
garivetm 0:ebe6f5e97160 53 return (can.write(msg));
garivetm 0:ebe6f5e97160 54 }
garivetm 0:ebe6f5e97160 55
garivetm 0:ebe6f5e97160 56 char ControllerCAN::FIFOread(void){
garivetm 0:ebe6f5e97160 57 FIFO_occupation=FIFO_ecriture-FIFO_lecture;
garivetm 0:ebe6f5e97160 58 if(FIFO_occupation<0)
garivetm 0:ebe6f5e97160 59 FIFO_occupation=FIFO_occupation+SIZE_FIFO;
garivetm 0:ebe6f5e97160 60 if(FIFO_max_occupation<FIFO_occupation)
garivetm 0:ebe6f5e97160 61 FIFO_max_occupation=FIFO_occupation;
garivetm 0:ebe6f5e97160 62 if(FIFO_occupation>SIZE_FIFO)
garivetm 1:b69d05604535 63 {}
garivetm 0:ebe6f5e97160 64 if(FIFO_occupation!=0)
garivetm 0:ebe6f5e97160 65 {
garivetm 0:ebe6f5e97160 66 char res = 0;
garivetm 1:b69d05604535 67 for (int i = 0; i < peripherals.size(); i++){
garivetm 1:b69d05604535 68 vector<unsigned short*> IdsRead = peripherals[i]->getIdsRead();
garivetm 0:ebe6f5e97160 69 for (int j = 0; j < IdsRead.size() ; j++){
garivetm 0:ebe6f5e97160 70 if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){
garivetm 1:b69d05604535 71 peripherals[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]);
garivetm 0:ebe6f5e97160 72 res = 1;
garivetm 0:ebe6f5e97160 73 break;
garivetm 0:ebe6f5e97160 74 }
garivetm 0:ebe6f5e97160 75 }
garivetm 0:ebe6f5e97160 76 if(res) break;
garivetm 0:ebe6f5e97160 77 }
garivetm 0:ebe6f5e97160 78 FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 79 return 1;
garivetm 0:ebe6f5e97160 80 }
garivetm 0:ebe6f5e97160 81 return -1;
garivetm 0:ebe6f5e97160 82 }
garivetm 0:ebe6f5e97160 83
garivetm 0:ebe6f5e97160 84 void ControllerCAN::can_ISR_Reader(void){
garivetm 0:ebe6f5e97160 85 if (can.read(can_MsgRx[FIFO_ecriture]))
garivetm 0:ebe6f5e97160 86 {
garivetm 0:ebe6f5e97160 87 // FIFO gestion
garivetm 0:ebe6f5e97160 88 FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO;
garivetm 0:ebe6f5e97160 89 }
garivetm 4:0ed21bbd917b 90 }