a

Fork of mbed by -deleted-

Committer:
TUATBM
Date:
Sun Sep 09 14:57:10 2018 +0000
Revision:
44:8d22cce05b9a
Parent:
43:aff670d0d510
a

Who changed what in which revision?

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