mouvement AX12 petit robot version 2
Fork of command_AX12_petit_robot_v2 by
mbed/CAN.h@7:ad4a19e26b84, 2017-05-12 (annotated)
- Committer:
- ClementBreteau
- Date:
- Fri May 12 14:35:09 2017 +0000
- Revision:
- 7:ad4a19e26b84
- Parent:
- 2:99b1cb0d9f5e
position ax12
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SquirrelGod | 2:99b1cb0d9f5e | 1 | /* mbed Microcontroller Library - can |
SquirrelGod | 2:99b1cb0d9f5e | 2 | * Copyright (c) 2009-2011 ARM Limited. All rights reserved. |
SquirrelGod | 2:99b1cb0d9f5e | 3 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 4 | |
SquirrelGod | 2:99b1cb0d9f5e | 5 | #ifndef MBED_CAN_H |
SquirrelGod | 2:99b1cb0d9f5e | 6 | #define MBED_CAN_H |
SquirrelGod | 2:99b1cb0d9f5e | 7 | |
SquirrelGod | 2:99b1cb0d9f5e | 8 | #include "device.h" |
SquirrelGod | 2:99b1cb0d9f5e | 9 | |
SquirrelGod | 2:99b1cb0d9f5e | 10 | #if DEVICE_CAN |
SquirrelGod | 2:99b1cb0d9f5e | 11 | |
SquirrelGod | 2:99b1cb0d9f5e | 12 | #include "Base.h" |
SquirrelGod | 2:99b1cb0d9f5e | 13 | #include "platform.h" |
SquirrelGod | 2:99b1cb0d9f5e | 14 | #include "PinNames.h" |
SquirrelGod | 2:99b1cb0d9f5e | 15 | #include "PeripheralNames.h" |
SquirrelGod | 2:99b1cb0d9f5e | 16 | |
SquirrelGod | 2:99b1cb0d9f5e | 17 | #include "can_helper.h" |
SquirrelGod | 2:99b1cb0d9f5e | 18 | #include "FunctionPointer.h" |
SquirrelGod | 2:99b1cb0d9f5e | 19 | |
SquirrelGod | 2:99b1cb0d9f5e | 20 | #include <string.h> |
SquirrelGod | 2:99b1cb0d9f5e | 21 | |
SquirrelGod | 2:99b1cb0d9f5e | 22 | namespace mbed { |
SquirrelGod | 2:99b1cb0d9f5e | 23 | |
SquirrelGod | 2:99b1cb0d9f5e | 24 | /* Class: CANMessage |
SquirrelGod | 2:99b1cb0d9f5e | 25 | * |
SquirrelGod | 2:99b1cb0d9f5e | 26 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 27 | class CANMessage : public CAN_Message { |
SquirrelGod | 2:99b1cb0d9f5e | 28 | |
SquirrelGod | 2:99b1cb0d9f5e | 29 | public: |
SquirrelGod | 2:99b1cb0d9f5e | 30 | |
SquirrelGod | 2:99b1cb0d9f5e | 31 | /* Constructor: CANMessage |
SquirrelGod | 2:99b1cb0d9f5e | 32 | * Creates empty CAN message. |
SquirrelGod | 2:99b1cb0d9f5e | 33 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 34 | CANMessage() { |
SquirrelGod | 2:99b1cb0d9f5e | 35 | len = 8; |
SquirrelGod | 2:99b1cb0d9f5e | 36 | type = CANData; |
SquirrelGod | 2:99b1cb0d9f5e | 37 | format = CANStandard; |
SquirrelGod | 2:99b1cb0d9f5e | 38 | id = 0; |
SquirrelGod | 2:99b1cb0d9f5e | 39 | memset(data, 0, 8); |
SquirrelGod | 2:99b1cb0d9f5e | 40 | } |
SquirrelGod | 2:99b1cb0d9f5e | 41 | |
SquirrelGod | 2:99b1cb0d9f5e | 42 | /* Constructor: CANMessage |
SquirrelGod | 2:99b1cb0d9f5e | 43 | * Creates CAN message with specific content. |
SquirrelGod | 2:99b1cb0d9f5e | 44 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 45 | CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { |
SquirrelGod | 2:99b1cb0d9f5e | 46 | len = _len & 0xF; |
SquirrelGod | 2:99b1cb0d9f5e | 47 | type = _type; |
SquirrelGod | 2:99b1cb0d9f5e | 48 | format = _format; |
SquirrelGod | 2:99b1cb0d9f5e | 49 | id = _id; |
SquirrelGod | 2:99b1cb0d9f5e | 50 | memcpy(data, _data, _len); |
SquirrelGod | 2:99b1cb0d9f5e | 51 | } |
SquirrelGod | 2:99b1cb0d9f5e | 52 | |
SquirrelGod | 2:99b1cb0d9f5e | 53 | /* Constructor: CANMessage |
SquirrelGod | 2:99b1cb0d9f5e | 54 | * Creates CAN remote message. |
SquirrelGod | 2:99b1cb0d9f5e | 55 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 56 | CANMessage(int _id, CANFormat _format = CANStandard) { |
SquirrelGod | 2:99b1cb0d9f5e | 57 | len = 0; |
SquirrelGod | 2:99b1cb0d9f5e | 58 | type = CANRemote; |
SquirrelGod | 2:99b1cb0d9f5e | 59 | format = _format; |
SquirrelGod | 2:99b1cb0d9f5e | 60 | id = _id; |
SquirrelGod | 2:99b1cb0d9f5e | 61 | memset(data, 0, 8); |
SquirrelGod | 2:99b1cb0d9f5e | 62 | } |
SquirrelGod | 2:99b1cb0d9f5e | 63 | #if 0 // Inhereted from CAN_Message, for documentation only |
SquirrelGod | 2:99b1cb0d9f5e | 64 | |
SquirrelGod | 2:99b1cb0d9f5e | 65 | /* Variable: id |
SquirrelGod | 2:99b1cb0d9f5e | 66 | * The message id. |
SquirrelGod | 2:99b1cb0d9f5e | 67 | * |
SquirrelGod | 2:99b1cb0d9f5e | 68 | * If format is CANStandard it must be an 11 bit long id |
SquirrelGod | 2:99b1cb0d9f5e | 69 | * If format is CANExtended it must be an 29 bit long id |
SquirrelGod | 2:99b1cb0d9f5e | 70 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 71 | unsigned int id; |
SquirrelGod | 2:99b1cb0d9f5e | 72 | |
SquirrelGod | 2:99b1cb0d9f5e | 73 | /* Variable: data |
SquirrelGod | 2:99b1cb0d9f5e | 74 | * Space for 8 byte payload. |
SquirrelGod | 2:99b1cb0d9f5e | 75 | * |
SquirrelGod | 2:99b1cb0d9f5e | 76 | * If type is CANData data can store up to 8 byte data. |
SquirrelGod | 2:99b1cb0d9f5e | 77 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 78 | unsigned char data[8]; |
SquirrelGod | 2:99b1cb0d9f5e | 79 | |
SquirrelGod | 2:99b1cb0d9f5e | 80 | /* Variable: len |
SquirrelGod | 2:99b1cb0d9f5e | 81 | * Length of data in bytes. |
SquirrelGod | 2:99b1cb0d9f5e | 82 | * |
SquirrelGod | 2:99b1cb0d9f5e | 83 | * If type is CANData data can store up to 8 byte data. |
SquirrelGod | 2:99b1cb0d9f5e | 84 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 85 | unsigned char len; |
SquirrelGod | 2:99b1cb0d9f5e | 86 | |
SquirrelGod | 2:99b1cb0d9f5e | 87 | /* Variable: format |
SquirrelGod | 2:99b1cb0d9f5e | 88 | * Defines if the message has standard or extended format. |
SquirrelGod | 2:99b1cb0d9f5e | 89 | * |
SquirrelGod | 2:99b1cb0d9f5e | 90 | * Defines the type of message id: |
SquirrelGod | 2:99b1cb0d9f5e | 91 | * Default is CANStandard which implies 11 bit id. |
SquirrelGod | 2:99b1cb0d9f5e | 92 | * CANExtended means 29 bit message id. |
SquirrelGod | 2:99b1cb0d9f5e | 93 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 94 | CANFormat format; |
SquirrelGod | 2:99b1cb0d9f5e | 95 | |
SquirrelGod | 2:99b1cb0d9f5e | 96 | /* Variable: type |
SquirrelGod | 2:99b1cb0d9f5e | 97 | * Defines the type of a message. |
SquirrelGod | 2:99b1cb0d9f5e | 98 | * |
SquirrelGod | 2:99b1cb0d9f5e | 99 | * The message type can rather be CANData for a message with data (default). |
SquirrelGod | 2:99b1cb0d9f5e | 100 | * Or CANRemote for a request of a specific CAN message. |
SquirrelGod | 2:99b1cb0d9f5e | 101 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 102 | CANType type; // 0 - DATA FRAME, 1 - REMOTE FRAME |
SquirrelGod | 2:99b1cb0d9f5e | 103 | #endif |
SquirrelGod | 2:99b1cb0d9f5e | 104 | }; |
SquirrelGod | 2:99b1cb0d9f5e | 105 | |
SquirrelGod | 2:99b1cb0d9f5e | 106 | /* Class: CAN |
SquirrelGod | 2:99b1cb0d9f5e | 107 | * A can bus client, used for communicating with can devices |
SquirrelGod | 2:99b1cb0d9f5e | 108 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 109 | class CAN : public Base { |
SquirrelGod | 2:99b1cb0d9f5e | 110 | |
SquirrelGod | 2:99b1cb0d9f5e | 111 | public: |
SquirrelGod | 2:99b1cb0d9f5e | 112 | |
SquirrelGod | 2:99b1cb0d9f5e | 113 | /* Constructor: CAN |
SquirrelGod | 2:99b1cb0d9f5e | 114 | * Creates an CAN interface connected to specific pins. |
SquirrelGod | 2:99b1cb0d9f5e | 115 | * |
SquirrelGod | 2:99b1cb0d9f5e | 116 | * Example: |
SquirrelGod | 2:99b1cb0d9f5e | 117 | * > #include "mbed.h" |
SquirrelGod | 2:99b1cb0d9f5e | 118 | * > |
SquirrelGod | 2:99b1cb0d9f5e | 119 | * > Ticker ticker; |
SquirrelGod | 2:99b1cb0d9f5e | 120 | * > DigitalOut led1(LED1); |
SquirrelGod | 2:99b1cb0d9f5e | 121 | * > DigitalOut led2(LED2); |
SquirrelGod | 2:99b1cb0d9f5e | 122 | * > CAN can1(p9, p10); |
SquirrelGod | 2:99b1cb0d9f5e | 123 | * > CAN can2(p30, p29); |
SquirrelGod | 2:99b1cb0d9f5e | 124 | * > |
SquirrelGod | 2:99b1cb0d9f5e | 125 | * > char counter = 0; |
SquirrelGod | 2:99b1cb0d9f5e | 126 | * > |
SquirrelGod | 2:99b1cb0d9f5e | 127 | * > void send() { |
SquirrelGod | 2:99b1cb0d9f5e | 128 | * > if(can1.write(CANMessage(1337, &counter, 1))) { |
SquirrelGod | 2:99b1cb0d9f5e | 129 | * > printf("Message sent: %d\n", counter); |
SquirrelGod | 2:99b1cb0d9f5e | 130 | * > counter++; |
SquirrelGod | 2:99b1cb0d9f5e | 131 | * > } |
SquirrelGod | 2:99b1cb0d9f5e | 132 | * > led1 = !led1; |
SquirrelGod | 2:99b1cb0d9f5e | 133 | * > } |
SquirrelGod | 2:99b1cb0d9f5e | 134 | * > |
SquirrelGod | 2:99b1cb0d9f5e | 135 | * > int main() { |
SquirrelGod | 2:99b1cb0d9f5e | 136 | * > ticker.attach(&send, 1); |
SquirrelGod | 2:99b1cb0d9f5e | 137 | * > CANMessage msg; |
SquirrelGod | 2:99b1cb0d9f5e | 138 | * > while(1) { |
SquirrelGod | 2:99b1cb0d9f5e | 139 | * > if(can2.read(msg)) { |
SquirrelGod | 2:99b1cb0d9f5e | 140 | * > printf("Message received: %d\n\n", msg.data[0]); |
SquirrelGod | 2:99b1cb0d9f5e | 141 | * > led2 = !led2; |
SquirrelGod | 2:99b1cb0d9f5e | 142 | * > } |
SquirrelGod | 2:99b1cb0d9f5e | 143 | * > wait(0.2); |
SquirrelGod | 2:99b1cb0d9f5e | 144 | * > } |
SquirrelGod | 2:99b1cb0d9f5e | 145 | * > } |
SquirrelGod | 2:99b1cb0d9f5e | 146 | * |
SquirrelGod | 2:99b1cb0d9f5e | 147 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 148 | * rd - read from transmitter |
SquirrelGod | 2:99b1cb0d9f5e | 149 | * td - transmit to transmitter |
SquirrelGod | 2:99b1cb0d9f5e | 150 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 151 | CAN(PinName rd, PinName td); |
SquirrelGod | 2:99b1cb0d9f5e | 152 | virtual ~CAN(); |
SquirrelGod | 2:99b1cb0d9f5e | 153 | |
SquirrelGod | 2:99b1cb0d9f5e | 154 | /* Function: frequency |
SquirrelGod | 2:99b1cb0d9f5e | 155 | * Set the frequency of the CAN interface |
SquirrelGod | 2:99b1cb0d9f5e | 156 | * |
SquirrelGod | 2:99b1cb0d9f5e | 157 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 158 | * hz - The bus frequency in hertz |
SquirrelGod | 2:99b1cb0d9f5e | 159 | * returns - 1 if successful, 0 otherwise |
SquirrelGod | 2:99b1cb0d9f5e | 160 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 161 | int frequency(int hz); |
SquirrelGod | 2:99b1cb0d9f5e | 162 | |
SquirrelGod | 2:99b1cb0d9f5e | 163 | /* Function: write |
SquirrelGod | 2:99b1cb0d9f5e | 164 | * Write a CANMessage to the bus. |
SquirrelGod | 2:99b1cb0d9f5e | 165 | * |
SquirrelGod | 2:99b1cb0d9f5e | 166 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 167 | * msg - The CANMessage to write. |
SquirrelGod | 2:99b1cb0d9f5e | 168 | * |
SquirrelGod | 2:99b1cb0d9f5e | 169 | * Returns: |
SquirrelGod | 2:99b1cb0d9f5e | 170 | * 0 - If write failed. |
SquirrelGod | 2:99b1cb0d9f5e | 171 | * 1 - If write was successful. |
SquirrelGod | 2:99b1cb0d9f5e | 172 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 173 | int write(CANMessage msg); |
SquirrelGod | 2:99b1cb0d9f5e | 174 | |
SquirrelGod | 2:99b1cb0d9f5e | 175 | /* Function: read |
SquirrelGod | 2:99b1cb0d9f5e | 176 | * Read a CANMessage from the bus. |
SquirrelGod | 2:99b1cb0d9f5e | 177 | * |
SquirrelGod | 2:99b1cb0d9f5e | 178 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 179 | * msg - A CANMessage to read to. |
SquirrelGod | 2:99b1cb0d9f5e | 180 | * |
SquirrelGod | 2:99b1cb0d9f5e | 181 | * Returns: |
SquirrelGod | 2:99b1cb0d9f5e | 182 | * 0 - If no message arrived. |
SquirrelGod | 2:99b1cb0d9f5e | 183 | * 1 - If message arrived. |
SquirrelGod | 2:99b1cb0d9f5e | 184 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 185 | int read(CANMessage &msg); |
SquirrelGod | 2:99b1cb0d9f5e | 186 | |
SquirrelGod | 2:99b1cb0d9f5e | 187 | /* Function: reset |
SquirrelGod | 2:99b1cb0d9f5e | 188 | * Reset CAN interface. |
SquirrelGod | 2:99b1cb0d9f5e | 189 | * |
SquirrelGod | 2:99b1cb0d9f5e | 190 | * To use after error overflow. |
SquirrelGod | 2:99b1cb0d9f5e | 191 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 192 | void reset(); |
SquirrelGod | 2:99b1cb0d9f5e | 193 | |
SquirrelGod | 2:99b1cb0d9f5e | 194 | /* Function: monitor |
SquirrelGod | 2:99b1cb0d9f5e | 195 | * Puts or removes the CAN interface into silent monitoring mode |
SquirrelGod | 2:99b1cb0d9f5e | 196 | * |
SquirrelGod | 2:99b1cb0d9f5e | 197 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 198 | * silent - boolean indicating whether to go into silent mode or not |
SquirrelGod | 2:99b1cb0d9f5e | 199 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 200 | void monitor(bool silent); |
SquirrelGod | 2:99b1cb0d9f5e | 201 | |
SquirrelGod | 2:99b1cb0d9f5e | 202 | /* Function: rderror |
SquirrelGod | 2:99b1cb0d9f5e | 203 | * Returns number of read errors to detect read overflow errors. |
SquirrelGod | 2:99b1cb0d9f5e | 204 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 205 | unsigned char rderror(); |
SquirrelGod | 2:99b1cb0d9f5e | 206 | |
SquirrelGod | 2:99b1cb0d9f5e | 207 | /* Function: tderror |
SquirrelGod | 2:99b1cb0d9f5e | 208 | * Returns number of write errors to detect write overflow errors. |
SquirrelGod | 2:99b1cb0d9f5e | 209 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 210 | unsigned char tderror(); |
SquirrelGod | 2:99b1cb0d9f5e | 211 | |
SquirrelGod | 2:99b1cb0d9f5e | 212 | /* Function: attach |
SquirrelGod | 2:99b1cb0d9f5e | 213 | * Attach a function to call whenever a CAN frame received interrupt is |
SquirrelGod | 2:99b1cb0d9f5e | 214 | * generated. |
SquirrelGod | 2:99b1cb0d9f5e | 215 | * |
SquirrelGod | 2:99b1cb0d9f5e | 216 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 217 | * fptr - A pointer to a void function, or 0 to set as none |
SquirrelGod | 2:99b1cb0d9f5e | 218 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 219 | void attach(void (*fptr)(void)); |
SquirrelGod | 2:99b1cb0d9f5e | 220 | |
SquirrelGod | 2:99b1cb0d9f5e | 221 | /* Function attach |
SquirrelGod | 2:99b1cb0d9f5e | 222 | * Attach a member function to call whenever a CAN frame received interrupt |
SquirrelGod | 2:99b1cb0d9f5e | 223 | * is generated. |
SquirrelGod | 2:99b1cb0d9f5e | 224 | * |
SquirrelGod | 2:99b1cb0d9f5e | 225 | * Variables: |
SquirrelGod | 2:99b1cb0d9f5e | 226 | * tptr - pointer to the object to call the member function on |
SquirrelGod | 2:99b1cb0d9f5e | 227 | * mptr - pointer to the member function to be called |
SquirrelGod | 2:99b1cb0d9f5e | 228 | */ |
SquirrelGod | 2:99b1cb0d9f5e | 229 | template<typename T> |
SquirrelGod | 2:99b1cb0d9f5e | 230 | void attach(T* tptr, void (T::*mptr)(void)) { |
SquirrelGod | 2:99b1cb0d9f5e | 231 | if((mptr != NULL) && (tptr != NULL)) { |
SquirrelGod | 2:99b1cb0d9f5e | 232 | _rxirq.attach(tptr, mptr); |
SquirrelGod | 2:99b1cb0d9f5e | 233 | setup_interrupt(); |
SquirrelGod | 2:99b1cb0d9f5e | 234 | } else { |
SquirrelGod | 2:99b1cb0d9f5e | 235 | remove_interrupt(); |
SquirrelGod | 2:99b1cb0d9f5e | 236 | } |
SquirrelGod | 2:99b1cb0d9f5e | 237 | } |
SquirrelGod | 2:99b1cb0d9f5e | 238 | |
SquirrelGod | 2:99b1cb0d9f5e | 239 | private: |
SquirrelGod | 2:99b1cb0d9f5e | 240 | |
SquirrelGod | 2:99b1cb0d9f5e | 241 | CANName _id; |
SquirrelGod | 2:99b1cb0d9f5e | 242 | FunctionPointer _rxirq; |
SquirrelGod | 2:99b1cb0d9f5e | 243 | |
SquirrelGod | 2:99b1cb0d9f5e | 244 | void setup_interrupt(void); |
SquirrelGod | 2:99b1cb0d9f5e | 245 | void remove_interrupt(void); |
SquirrelGod | 2:99b1cb0d9f5e | 246 | }; |
SquirrelGod | 2:99b1cb0d9f5e | 247 | |
SquirrelGod | 2:99b1cb0d9f5e | 248 | } // namespace mbed |
SquirrelGod | 2:99b1cb0d9f5e | 249 | |
SquirrelGod | 2:99b1cb0d9f5e | 250 | #endif // MBED_CAN_H |
SquirrelGod | 2:99b1cb0d9f5e | 251 | |
SquirrelGod | 2:99b1cb0d9f5e | 252 | #endif |