CAN library containing a CAN controller object handling a FIFO, and CAN peripherals attached to it.
ControllerCAN.h@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 | 3:e6f72461e31f | 1 | /*************************************************************************** |
garivetm | 3:e6f72461e31f | 2 | Copyright 2016 LARNAUDIE GARIVET |
garivetm | 3:e6f72461e31f | 3 | |
garivetm | 3:e6f72461e31f | 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
garivetm | 3:e6f72461e31f | 5 | you may not use this file except in compliance with the License. |
garivetm | 3:e6f72461e31f | 6 | You may obtain a copy of the License at |
garivetm | 3:e6f72461e31f | 7 | |
garivetm | 3:e6f72461e31f | 8 | http://www.apache.org/licenses/LICENSE-2.0 |
garivetm | 3:e6f72461e31f | 9 | |
garivetm | 3:e6f72461e31f | 10 | Unless required by applicable law or agreed to in writing, software |
garivetm | 3:e6f72461e31f | 11 | distributed under the License is distributed on an "AS IS" BASIS, |
garivetm | 3:e6f72461e31f | 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
garivetm | 3:e6f72461e31f | 13 | See the License for the specific language governing permissions and |
garivetm | 3:e6f72461e31f | 14 | limitations under the License. |
garivetm | 3:e6f72461e31f | 15 | ***************************************************************************/ |
garivetm | 0:ebe6f5e97160 | 16 | #ifndef CONTROLLERCAN_H |
garivetm | 0:ebe6f5e97160 | 17 | #define CONTROLLERCAN_H |
garivetm | 0:ebe6f5e97160 | 18 | |
garivetm | 0:ebe6f5e97160 | 19 | #include "mbed.h" |
garivetm | 1:b69d05604535 | 20 | #include "PeripheralCAN.h" |
garivetm | 0:ebe6f5e97160 | 21 | #include <vector> |
garivetm | 0:ebe6f5e97160 | 22 | |
garivetm | 0:ebe6f5e97160 | 23 | #define SIZE_FIFO 32 |
garivetm | 0:ebe6f5e97160 | 24 | |
garivetm | 1:b69d05604535 | 25 | class PeripheralCAN; |
garivetm | 0:ebe6f5e97160 | 26 | |
garivetm | 0:ebe6f5e97160 | 27 | /** My CAN Controller class |
garivetm | 0:ebe6f5e97160 | 28 | * Used to manage reading and writing procedures on a CAN Bus |
garivetm | 0:ebe6f5e97160 | 29 | * |
garivetm | 0:ebe6f5e97160 | 30 | * Examples : |
garivetm | 0:ebe6f5e97160 | 31 | * @code |
garivetm | 2:c81dff9c8a93 | 32 | * class Motor : public PeripheralCAN{ // Your own Peripheral class (here a motor) |
garivetm | 2:c81dff9c8a93 | 33 | * public : |
garivetm | 2:c81dff9c8a93 | 34 | * Motor(ControllerCAN* controller, unsigned short Id1, unsigned short Id2) : PeripheralCAN(controller), Id1_m(Id1), Id2_m(Id2){ |
garivetm | 2:c81dff9c8a93 | 35 | * // Your stuffs |
garivetm | 2:c81dff9c8a93 | 36 | * }; |
garivetm | 2:c81dff9c8a93 | 37 | * |
garivetm | 2:c81dff9c8a93 | 38 | * void init(void){ |
garivetm | 2:c81dff9c8a93 | 39 | * addIdRead(&Id1); // To be done for each incomming Id |
garivetm | 2:c81dff9c8a93 | 40 | * addIdRead(&Id2); // To be done for each incomming Id |
garivetm | 2:c81dff9c8a93 | 41 | * // Your stuffs |
garivetm | 2:c81dff9c8a93 | 42 | * }; |
garivetm | 2:c81dff9c8a93 | 43 | * |
garivetm | 2:c81dff9c8a93 | 44 | * void update(const unsigned short& Id, const CANMessage& msg){ |
garivetm | 2:c81dff9c8a93 | 45 | * if(Id == Id1){ |
garivetm | 2:c81dff9c8a93 | 46 | * // update your own attributes with msg content |
garivetm | 2:c81dff9c8a93 | 47 | * } |
garivetm | 2:c81dff9c8a93 | 48 | * else if(Id == Id2){ |
garivetm | 2:c81dff9c8a93 | 49 | * // update your own attributes with msg content |
garivetm | 2:c81dff9c8a93 | 50 | * } |
garivetm | 2:c81dff9c8a93 | 51 | * }; |
garivetm | 2:c81dff9c8a93 | 52 | * |
garivetm | 2:c81dff9c8a93 | 53 | * private : |
garivetm | 2:c81dff9c8a93 | 54 | * unsigned short Id1_m; |
garivetm | 2:c81dff9c8a93 | 55 | * unsigned short Id2_m; |
garivetm | 2:c81dff9c8a93 | 56 | * }; |
garivetm | 2:c81dff9c8a93 | 57 | * |
garivetm | 2:c81dff9c8a93 | 58 | * int main(){ |
garivetm | 2:c81dff9c8a93 | 59 | * ControllerCAN controller; |
garivetm | 2:c81dff9c8a93 | 60 | * Motor peripherial(&controller, 0x100, 0x101); |
garivetm | 2:c81dff9c8a93 | 61 | * |
garivetm | 2:c81dff9c8a93 | 62 | * while(1){ |
garivetm | 2:c81dff9c8a93 | 63 | * controller.FIFOread(); // Treatement of the FIFO CAN messages |
garivetm | 2:c81dff9c8a93 | 64 | * // Your stuffs |
garivetm | 2:c81dff9c8a93 | 65 | * } |
garivetm | 2:c81dff9c8a93 | 66 | * } |
garivetm | 0:ebe6f5e97160 | 67 | * @endcode |
garivetm | 0:ebe6f5e97160 | 68 | */ |
garivetm | 0:ebe6f5e97160 | 69 | class ControllerCAN { |
garivetm | 0:ebe6f5e97160 | 70 | public : |
garivetm | 0:ebe6f5e97160 | 71 | /** Create ControllerCAN instance |
garivetm | 5:d1920eb1d63e | 72 | * |
garivetm | 5:d1920eb1d63e | 73 | * @param rx Rd pin |
garivetm | 5:d1920eb1d63e | 74 | * @param tx td pin |
garivetm | 0:ebe6f5e97160 | 75 | */ |
garivetm | 5:d1920eb1d63e | 76 | ControllerCAN(PinName rd, PinName td); |
garivetm | 0:ebe6f5e97160 | 77 | |
garivetm | 0:ebe6f5e97160 | 78 | /** Destroy ControllerCAN instance |
garivetm | 0:ebe6f5e97160 | 79 | */ |
garivetm | 0:ebe6f5e97160 | 80 | ~ControllerCAN(); |
garivetm | 0:ebe6f5e97160 | 81 | |
garivetm | 0:ebe6f5e97160 | 82 | /** Write a CANMessage on CAN Bus |
garivetm | 0:ebe6f5e97160 | 83 | * |
garivetm | 0:ebe6f5e97160 | 84 | * @param Id Id message |
garivetm | 0:ebe6f5e97160 | 85 | * @param data char array containing data to be send |
garivetm | 0:ebe6f5e97160 | 86 | * @param len size of the data array |
garivetm | 0:ebe6f5e97160 | 87 | * @returns |
garivetm | 0:ebe6f5e97160 | 88 | * 1 if write was successful, |
garivetm | 0:ebe6f5e97160 | 89 | * 0 if write failed |
garivetm | 0:ebe6f5e97160 | 90 | */ |
garivetm | 0:ebe6f5e97160 | 91 | long writeData(long Id, const char *data, char len); |
garivetm | 0:ebe6f5e97160 | 92 | |
garivetm | 0:ebe6f5e97160 | 93 | /** Write a remote CANMessage on CAN Bus |
garivetm | 0:ebe6f5e97160 | 94 | * |
garivetm | 0:ebe6f5e97160 | 95 | * @param Id Id message |
garivetm | 0:ebe6f5e97160 | 96 | * @returns |
garivetm | 0:ebe6f5e97160 | 97 | * 1 if write was successful, |
garivetm | 0:ebe6f5e97160 | 98 | * 0 if write failed |
garivetm | 0:ebe6f5e97160 | 99 | */ |
garivetm | 0:ebe6f5e97160 | 100 | long writeRemote(long Id); |
garivetm | 0:ebe6f5e97160 | 101 | |
garivetm | 1:b69d05604535 | 102 | /** Attach a PeripheralCAN instance to a ControllerCAN |
garivetm | 0:ebe6f5e97160 | 103 | * |
garivetm | 1:b69d05604535 | 104 | * @param peripheral Pointer on a PeripheralCAN instance |
garivetm | 0:ebe6f5e97160 | 105 | */ |
garivetm | 1:b69d05604535 | 106 | void attach(PeripheralCAN* peripheral); |
garivetm | 0:ebe6f5e97160 | 107 | |
garivetm | 0:ebe6f5e97160 | 108 | /** Read one message on the CAN FIFO |
garivetm | 0:ebe6f5e97160 | 109 | * |
garivetm | 0:ebe6f5e97160 | 110 | * @returns |
garivetm | 0:ebe6f5e97160 | 111 | * -1 if no match is found between the message Id and all Ids of |
garivetm | 1:b69d05604535 | 112 | * the PeripheralCAN instances attached to the ControllerCAN, |
garivetm | 0:ebe6f5e97160 | 113 | * 1 if a match is found |
garivetm | 0:ebe6f5e97160 | 114 | */ |
garivetm | 0:ebe6f5e97160 | 115 | char FIFOread(void); |
garivetm | 0:ebe6f5e97160 | 116 | //void FIFO_remove_msg(void); |
garivetm | 0:ebe6f5e97160 | 117 | |
garivetm | 0:ebe6f5e97160 | 118 | private : |
garivetm | 0:ebe6f5e97160 | 119 | |
garivetm | 5:d1920eb1d63e | 120 | CAN can; |
garivetm | 1:b69d05604535 | 121 | vector<PeripheralCAN*> peripherals; |
garivetm | 0:ebe6f5e97160 | 122 | unsigned char FIFO_ecriture; |
garivetm | 0:ebe6f5e97160 | 123 | signed char FIFO_lecture; |
garivetm | 0:ebe6f5e97160 | 124 | signed char FIFO_occupation; |
garivetm | 0:ebe6f5e97160 | 125 | signed char FIFO_max_occupation; |
garivetm | 0:ebe6f5e97160 | 126 | CANMessage can_MsgRx[SIZE_FIFO]; |
garivetm | 0:ebe6f5e97160 | 127 | |
garivetm | 0:ebe6f5e97160 | 128 | /** Interrupt Service Routine called whenever a CAN frame received interrupt |
garivetm | 0:ebe6f5e97160 | 129 | * is generated. |
garivetm | 0:ebe6f5e97160 | 130 | */ |
garivetm | 0:ebe6f5e97160 | 131 | void can_ISR_Reader(void); |
garivetm | 0:ebe6f5e97160 | 132 | |
garivetm | 0:ebe6f5e97160 | 133 | //static void CAN_automate_reception(void); |
garivetm | 0:ebe6f5e97160 | 134 | }; |
garivetm | 0:ebe6f5e97160 | 135 | |
garivetm | 4:0ed21bbd917b | 136 | #endif |