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

Committer:
DGonz
Date:
Mon Dec 16 19:18:00 2019 +0000
Revision:
5:b23a3876bb1d
Parent:
4:0ed21bbd917b
Changed pins to match F446RE

Who changed what in which revision?

UserRevisionLine numberNew 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 0:ebe6f5e97160 72 */
garivetm 0:ebe6f5e97160 73 ControllerCAN();
garivetm 0:ebe6f5e97160 74
garivetm 0:ebe6f5e97160 75 /** Destroy ControllerCAN instance
garivetm 0:ebe6f5e97160 76 */
garivetm 0:ebe6f5e97160 77 ~ControllerCAN();
garivetm 0:ebe6f5e97160 78
garivetm 0:ebe6f5e97160 79 /** Write a CANMessage on CAN Bus
garivetm 0:ebe6f5e97160 80 *
garivetm 0:ebe6f5e97160 81 * @param Id Id message
garivetm 0:ebe6f5e97160 82 * @param data char array containing data to be send
garivetm 0:ebe6f5e97160 83 * @param len size of the data array
garivetm 0:ebe6f5e97160 84 * @returns
garivetm 0:ebe6f5e97160 85 * 1 if write was successful,
garivetm 0:ebe6f5e97160 86 * 0 if write failed
garivetm 0:ebe6f5e97160 87 */
garivetm 0:ebe6f5e97160 88 long writeData(long Id, const char *data, char len);
garivetm 0:ebe6f5e97160 89
garivetm 0:ebe6f5e97160 90 /** Write a remote CANMessage on CAN Bus
garivetm 0:ebe6f5e97160 91 *
garivetm 0:ebe6f5e97160 92 * @param Id Id message
garivetm 0:ebe6f5e97160 93 * @returns
garivetm 0:ebe6f5e97160 94 * 1 if write was successful,
garivetm 0:ebe6f5e97160 95 * 0 if write failed
garivetm 0:ebe6f5e97160 96 */
garivetm 0:ebe6f5e97160 97 long writeRemote(long Id);
garivetm 0:ebe6f5e97160 98
garivetm 1:b69d05604535 99 /** Attach a PeripheralCAN instance to a ControllerCAN
garivetm 0:ebe6f5e97160 100 *
garivetm 1:b69d05604535 101 * @param peripheral Pointer on a PeripheralCAN instance
garivetm 0:ebe6f5e97160 102 */
garivetm 1:b69d05604535 103 void attach(PeripheralCAN* peripheral);
garivetm 0:ebe6f5e97160 104
garivetm 0:ebe6f5e97160 105 /** Read one message on the CAN FIFO
garivetm 0:ebe6f5e97160 106 *
garivetm 0:ebe6f5e97160 107 * @returns
garivetm 0:ebe6f5e97160 108 * -1 if no match is found between the message Id and all Ids of
garivetm 1:b69d05604535 109 * the PeripheralCAN instances attached to the ControllerCAN,
garivetm 0:ebe6f5e97160 110 * 1 if a match is found
garivetm 0:ebe6f5e97160 111 */
garivetm 0:ebe6f5e97160 112 char FIFOread(void);
garivetm 0:ebe6f5e97160 113 //void FIFO_remove_msg(void);
garivetm 0:ebe6f5e97160 114
garivetm 0:ebe6f5e97160 115 private :
garivetm 0:ebe6f5e97160 116
garivetm 4:0ed21bbd917b 117 static CAN can;
garivetm 1:b69d05604535 118 vector<PeripheralCAN*> peripherals;
garivetm 0:ebe6f5e97160 119 unsigned char FIFO_ecriture;
garivetm 0:ebe6f5e97160 120 signed char FIFO_lecture;
garivetm 0:ebe6f5e97160 121 signed char FIFO_occupation;
garivetm 0:ebe6f5e97160 122 signed char FIFO_max_occupation;
garivetm 0:ebe6f5e97160 123 CANMessage can_MsgRx[SIZE_FIFO];
garivetm 0:ebe6f5e97160 124
garivetm 0:ebe6f5e97160 125 /** Interrupt Service Routine called whenever a CAN frame received interrupt
garivetm 0:ebe6f5e97160 126 * is generated.
garivetm 0:ebe6f5e97160 127 */
garivetm 0:ebe6f5e97160 128 void can_ISR_Reader(void);
garivetm 0:ebe6f5e97160 129
garivetm 0:ebe6f5e97160 130 //static void CAN_automate_reception(void);
garivetm 0:ebe6f5e97160 131 };
garivetm 0:ebe6f5e97160 132
garivetm 4:0ed21bbd917b 133 #endif