CAN library containing a CAN controller object handling a FIFO, and CAN peripherals attached to it.
ControllerCAN.cpp@5:d1920eb1d63e, 2020-09-12 (annotated)
- Committer:
- garivetm
- Date:
- Sat Sep 12 12:36:45 2020 +0000
- Revision:
- 5:d1920eb1d63e
- Parent:
- 4:0ed21bbd917b
Enable Bus Off auto management ; Pass Rx and TX as constructor parameters ; Add a timeout to the write method.
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:d1920eb1d63e | 8 | v2 (18/02/2016) : Controller and 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 | 5:d1920eb1d63e | 28 | ControllerCAN::ControllerCAN(PinName rd, PinName td) : can(rd, td, 250000) { |
garivetm | 5:d1920eb1d63e | 29 | // Enable bus off auto mangement |
garivetm | 5:d1920eb1d63e | 30 | unsigned long* CAN_MCR = (unsigned long*) 0x40006400; |
garivetm | 5:d1920eb1d63e | 31 | *CAN_MCR |= CAN_MCR_ABOM; |
garivetm | 5:d1920eb1d63e | 32 | |
garivetm | 4:0ed21bbd917b | 33 | can.attach(callback(this, &ControllerCAN::can_ISR_Reader)); // CAN ISR |
garivetm | 5:d1920eb1d63e | 34 | |
garivetm | 0:ebe6f5e97160 | 35 | FIFO_ecriture = 0; |
garivetm | 0:ebe6f5e97160 | 36 | FIFO_lecture = 0; |
garivetm | 0:ebe6f5e97160 | 37 | FIFO_occupation = 0; |
garivetm | 0:ebe6f5e97160 | 38 | FIFO_max_occupation = 0; |
garivetm | 0:ebe6f5e97160 | 39 | } |
garivetm | 0:ebe6f5e97160 | 40 | |
garivetm | 0:ebe6f5e97160 | 41 | ControllerCAN::~ControllerCAN(){ |
garivetm | 0:ebe6f5e97160 | 42 | } |
garivetm | 0:ebe6f5e97160 | 43 | |
garivetm | 1:b69d05604535 | 44 | void ControllerCAN::attach(PeripheralCAN* peripheral){ |
garivetm | 1:b69d05604535 | 45 | peripherals.push_back(peripheral); |
garivetm | 0:ebe6f5e97160 | 46 | } |
garivetm | 0:ebe6f5e97160 | 47 | |
garivetm | 0:ebe6f5e97160 | 48 | long ControllerCAN::writeData(long Id, const char *data, char len){ |
garivetm | 0:ebe6f5e97160 | 49 | CANMessage msg(Id, data, len); |
garivetm | 0:ebe6f5e97160 | 50 | return (can.write(msg)); |
garivetm | 0:ebe6f5e97160 | 51 | } |
garivetm | 0:ebe6f5e97160 | 52 | |
garivetm | 0:ebe6f5e97160 | 53 | long ControllerCAN::writeRemote(long Id){ |
garivetm | 0:ebe6f5e97160 | 54 | CANMessage msg(Id); |
garivetm | 0:ebe6f5e97160 | 55 | return (can.write(msg)); |
garivetm | 0:ebe6f5e97160 | 56 | } |
garivetm | 0:ebe6f5e97160 | 57 | |
garivetm | 0:ebe6f5e97160 | 58 | char ControllerCAN::FIFOread(void){ |
garivetm | 0:ebe6f5e97160 | 59 | FIFO_occupation=FIFO_ecriture-FIFO_lecture; |
garivetm | 0:ebe6f5e97160 | 60 | if(FIFO_occupation<0) |
garivetm | 0:ebe6f5e97160 | 61 | FIFO_occupation=FIFO_occupation+SIZE_FIFO; |
garivetm | 0:ebe6f5e97160 | 62 | if(FIFO_max_occupation<FIFO_occupation) |
garivetm | 0:ebe6f5e97160 | 63 | FIFO_max_occupation=FIFO_occupation; |
garivetm | 0:ebe6f5e97160 | 64 | if(FIFO_occupation>SIZE_FIFO) |
garivetm | 1:b69d05604535 | 65 | {} |
garivetm | 0:ebe6f5e97160 | 66 | if(FIFO_occupation!=0) |
garivetm | 0:ebe6f5e97160 | 67 | { |
garivetm | 0:ebe6f5e97160 | 68 | char res = 0; |
garivetm | 5:d1920eb1d63e | 69 | for (uint32_t i = 0; i < peripherals.size(); i++){ |
garivetm | 1:b69d05604535 | 70 | vector<unsigned short*> IdsRead = peripherals[i]->getIdsRead(); |
garivetm | 5:d1920eb1d63e | 71 | for (uint32_t j = 0; j < IdsRead.size() ; j++){ |
garivetm | 0:ebe6f5e97160 | 72 | if (can_MsgRx[FIFO_lecture].id == *(IdsRead[j])){ |
garivetm | 1:b69d05604535 | 73 | peripherals[i]->update(*(IdsRead[j]), can_MsgRx[FIFO_lecture]); |
garivetm | 0:ebe6f5e97160 | 74 | res = 1; |
garivetm | 0:ebe6f5e97160 | 75 | break; |
garivetm | 0:ebe6f5e97160 | 76 | } |
garivetm | 0:ebe6f5e97160 | 77 | } |
garivetm | 0:ebe6f5e97160 | 78 | if(res) break; |
garivetm | 0:ebe6f5e97160 | 79 | } |
garivetm | 0:ebe6f5e97160 | 80 | FIFO_lecture=(FIFO_lecture+1)%SIZE_FIFO; |
garivetm | 0:ebe6f5e97160 | 81 | return 1; |
garivetm | 0:ebe6f5e97160 | 82 | } |
garivetm | 0:ebe6f5e97160 | 83 | return -1; |
garivetm | 0:ebe6f5e97160 | 84 | } |
garivetm | 0:ebe6f5e97160 | 85 | |
garivetm | 0:ebe6f5e97160 | 86 | void ControllerCAN::can_ISR_Reader(void){ |
garivetm | 0:ebe6f5e97160 | 87 | if (can.read(can_MsgRx[FIFO_ecriture])) |
garivetm | 0:ebe6f5e97160 | 88 | { |
garivetm | 0:ebe6f5e97160 | 89 | // FIFO gestion |
garivetm | 0:ebe6f5e97160 | 90 | FIFO_ecriture=(FIFO_ecriture+1)%SIZE_FIFO; |
garivetm | 0:ebe6f5e97160 | 91 | } |
garivetm | 4:0ed21bbd917b | 92 | } |