CAN library containing a CAN controller object handling a FIFO, and CAN peripherals attached to it.
ControllerCAN.h@2:c81dff9c8a93, 2016-02-18 (annotated)
- Committer:
- garivetm
- Date:
- Thu Feb 18 15:40:24 2016 +0000
- Revision:
- 2:c81dff9c8a93
- Parent:
- 1:b69d05604535
- Child:
- 3:e6f72461e31f
Adding a use case example in the documentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
garivetm | 0:ebe6f5e97160 | 1 | #ifndef CONTROLLERCAN_H |
garivetm | 0:ebe6f5e97160 | 2 | #define CONTROLLERCAN_H |
garivetm | 0:ebe6f5e97160 | 3 | |
garivetm | 0:ebe6f5e97160 | 4 | #include "mbed.h" |
garivetm | 1:b69d05604535 | 5 | #include "PeripheralCAN.h" |
garivetm | 0:ebe6f5e97160 | 6 | #include <vector> |
garivetm | 0:ebe6f5e97160 | 7 | |
garivetm | 0:ebe6f5e97160 | 8 | #define SIZE_FIFO 32 |
garivetm | 0:ebe6f5e97160 | 9 | |
garivetm | 1:b69d05604535 | 10 | class PeripheralCAN; |
garivetm | 0:ebe6f5e97160 | 11 | |
garivetm | 0:ebe6f5e97160 | 12 | /** My CAN Controller class |
garivetm | 0:ebe6f5e97160 | 13 | * Used to manage reading and writing procedures on a CAN Bus |
garivetm | 0:ebe6f5e97160 | 14 | * |
garivetm | 0:ebe6f5e97160 | 15 | * Examples : |
garivetm | 0:ebe6f5e97160 | 16 | * @code |
garivetm | 2:c81dff9c8a93 | 17 | * class Motor : public PeripheralCAN{ // Your own Peripheral class (here a motor) |
garivetm | 2:c81dff9c8a93 | 18 | * public : |
garivetm | 2:c81dff9c8a93 | 19 | * Motor(ControllerCAN* controller, unsigned short Id1, unsigned short Id2) : PeripheralCAN(controller), Id1_m(Id1), Id2_m(Id2){ |
garivetm | 2:c81dff9c8a93 | 20 | * // Your stuffs |
garivetm | 2:c81dff9c8a93 | 21 | * }; |
garivetm | 2:c81dff9c8a93 | 22 | * |
garivetm | 2:c81dff9c8a93 | 23 | * void init(void){ |
garivetm | 2:c81dff9c8a93 | 24 | * addIdRead(&Id1); // To be done for each incomming Id |
garivetm | 2:c81dff9c8a93 | 25 | * addIdRead(&Id2); // To be done for each incomming Id |
garivetm | 2:c81dff9c8a93 | 26 | * // Your stuffs |
garivetm | 2:c81dff9c8a93 | 27 | * }; |
garivetm | 2:c81dff9c8a93 | 28 | * |
garivetm | 2:c81dff9c8a93 | 29 | * void update(const unsigned short& Id, const CANMessage& msg){ |
garivetm | 2:c81dff9c8a93 | 30 | * if(Id == Id1){ |
garivetm | 2:c81dff9c8a93 | 31 | * // update your own attributes with msg content |
garivetm | 2:c81dff9c8a93 | 32 | * } |
garivetm | 2:c81dff9c8a93 | 33 | * else if(Id == Id2){ |
garivetm | 2:c81dff9c8a93 | 34 | * // update your own attributes with msg content |
garivetm | 2:c81dff9c8a93 | 35 | * } |
garivetm | 2:c81dff9c8a93 | 36 | * }; |
garivetm | 2:c81dff9c8a93 | 37 | * |
garivetm | 2:c81dff9c8a93 | 38 | * private : |
garivetm | 2:c81dff9c8a93 | 39 | * unsigned short Id1_m; |
garivetm | 2:c81dff9c8a93 | 40 | * unsigned short Id2_m; |
garivetm | 2:c81dff9c8a93 | 41 | * }; |
garivetm | 2:c81dff9c8a93 | 42 | * |
garivetm | 2:c81dff9c8a93 | 43 | * int main(){ |
garivetm | 2:c81dff9c8a93 | 44 | * ControllerCAN controller; |
garivetm | 2:c81dff9c8a93 | 45 | * Motor peripherial(&controller, 0x100, 0x101); |
garivetm | 2:c81dff9c8a93 | 46 | * |
garivetm | 2:c81dff9c8a93 | 47 | * while(1){ |
garivetm | 2:c81dff9c8a93 | 48 | * controller.FIFOread(); // Treatement of the FIFO CAN messages |
garivetm | 2:c81dff9c8a93 | 49 | * // Your stuffs |
garivetm | 2:c81dff9c8a93 | 50 | * } |
garivetm | 2:c81dff9c8a93 | 51 | * } |
garivetm | 0:ebe6f5e97160 | 52 | * @endcode |
garivetm | 0:ebe6f5e97160 | 53 | */ |
garivetm | 0:ebe6f5e97160 | 54 | class ControllerCAN { |
garivetm | 0:ebe6f5e97160 | 55 | public : |
garivetm | 0:ebe6f5e97160 | 56 | /** Create ControllerCAN instance |
garivetm | 0:ebe6f5e97160 | 57 | */ |
garivetm | 0:ebe6f5e97160 | 58 | ControllerCAN(); |
garivetm | 0:ebe6f5e97160 | 59 | |
garivetm | 0:ebe6f5e97160 | 60 | /** Destroy ControllerCAN instance |
garivetm | 0:ebe6f5e97160 | 61 | */ |
garivetm | 0:ebe6f5e97160 | 62 | ~ControllerCAN(); |
garivetm | 0:ebe6f5e97160 | 63 | |
garivetm | 0:ebe6f5e97160 | 64 | /** Write a CANMessage on CAN Bus |
garivetm | 0:ebe6f5e97160 | 65 | * |
garivetm | 0:ebe6f5e97160 | 66 | * @param Id Id message |
garivetm | 0:ebe6f5e97160 | 67 | * @param data char array containing data to be send |
garivetm | 0:ebe6f5e97160 | 68 | * @param len size of the data array |
garivetm | 0:ebe6f5e97160 | 69 | * @returns |
garivetm | 0:ebe6f5e97160 | 70 | * 1 if write was successful, |
garivetm | 0:ebe6f5e97160 | 71 | * 0 if write failed |
garivetm | 0:ebe6f5e97160 | 72 | */ |
garivetm | 0:ebe6f5e97160 | 73 | long writeData(long Id, const char *data, char len); |
garivetm | 0:ebe6f5e97160 | 74 | |
garivetm | 0:ebe6f5e97160 | 75 | /** Write a remote CANMessage on CAN Bus |
garivetm | 0:ebe6f5e97160 | 76 | * |
garivetm | 0:ebe6f5e97160 | 77 | * @param Id Id message |
garivetm | 0:ebe6f5e97160 | 78 | * @returns |
garivetm | 0:ebe6f5e97160 | 79 | * 1 if write was successful, |
garivetm | 0:ebe6f5e97160 | 80 | * 0 if write failed |
garivetm | 0:ebe6f5e97160 | 81 | */ |
garivetm | 0:ebe6f5e97160 | 82 | long writeRemote(long Id); |
garivetm | 0:ebe6f5e97160 | 83 | |
garivetm | 1:b69d05604535 | 84 | /** Attach a PeripheralCAN instance to a ControllerCAN |
garivetm | 0:ebe6f5e97160 | 85 | * |
garivetm | 1:b69d05604535 | 86 | * @param peripheral Pointer on a PeripheralCAN instance |
garivetm | 0:ebe6f5e97160 | 87 | */ |
garivetm | 1:b69d05604535 | 88 | void attach(PeripheralCAN* peripheral); |
garivetm | 0:ebe6f5e97160 | 89 | |
garivetm | 0:ebe6f5e97160 | 90 | /** Read one message on the CAN FIFO |
garivetm | 0:ebe6f5e97160 | 91 | * |
garivetm | 0:ebe6f5e97160 | 92 | * @returns |
garivetm | 0:ebe6f5e97160 | 93 | * -1 if no match is found between the message Id and all Ids of |
garivetm | 1:b69d05604535 | 94 | * the PeripheralCAN instances attached to the ControllerCAN, |
garivetm | 0:ebe6f5e97160 | 95 | * 1 if a match is found |
garivetm | 0:ebe6f5e97160 | 96 | */ |
garivetm | 0:ebe6f5e97160 | 97 | char FIFOread(void); |
garivetm | 0:ebe6f5e97160 | 98 | //void FIFO_remove_msg(void); |
garivetm | 0:ebe6f5e97160 | 99 | |
garivetm | 0:ebe6f5e97160 | 100 | private : |
garivetm | 0:ebe6f5e97160 | 101 | |
garivetm | 0:ebe6f5e97160 | 102 | CAN can; |
garivetm | 1:b69d05604535 | 103 | vector<PeripheralCAN*> peripherals; |
garivetm | 0:ebe6f5e97160 | 104 | unsigned char FIFO_ecriture; |
garivetm | 0:ebe6f5e97160 | 105 | signed char FIFO_lecture; |
garivetm | 0:ebe6f5e97160 | 106 | signed char FIFO_occupation; |
garivetm | 0:ebe6f5e97160 | 107 | signed char FIFO_max_occupation; |
garivetm | 0:ebe6f5e97160 | 108 | CANMessage can_MsgRx[SIZE_FIFO]; |
garivetm | 0:ebe6f5e97160 | 109 | |
garivetm | 0:ebe6f5e97160 | 110 | /** Interrupt Service Routine called whenever a CAN frame received interrupt |
garivetm | 0:ebe6f5e97160 | 111 | * is generated. |
garivetm | 0:ebe6f5e97160 | 112 | */ |
garivetm | 0:ebe6f5e97160 | 113 | void can_ISR_Reader(void); |
garivetm | 0:ebe6f5e97160 | 114 | |
garivetm | 0:ebe6f5e97160 | 115 | //static void CAN_automate_reception(void); |
garivetm | 0:ebe6f5e97160 | 116 | }; |
garivetm | 0:ebe6f5e97160 | 117 | |
garivetm | 0:ebe6f5e97160 | 118 | #endif |