Programme d'utilisation des AX12 avec rajout de l'MX12

Fork of test_carteAToutFaire_PR by CRAC Team

Committer:
ClementBreteau
Date:
Thu May 11 11:49:50 2017 +0000
Revision:
1:f3f702086a30
test de la carte ? tout faire du petit robot

Who changed what in which revision?

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