1 /* mbed Microcontroller Library - can
2 * Copyright (c) 2009 ARM Limited. All rights reserved.
12 #include "PeripheralNames.h"
14 #include "can_helper.h"
15 #include "FunctionPointer.h"
24 class CANMessage : public CAN_Message {
28 /* Constructor: CANMessage
29 * Creates empty CAN message.
39 /* Constructor: CANMessage
40 * Creates CAN message with specific content.
42 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
47 memcpy(data, _data, _len);
50 /* Constructor: CANMessage
51 * Creates CAN remote message.
53 CANMessage(int _id, CANFormat _format = CANStandard) {
60 #if 0 // Inhereted from CAN_Message, for documentation only
65 * If format is CANStandard it must be an 11 bit long id
66 * If format is CANExtended it must be an 29 bit long id
71 * Space for 8 byte payload.
73 * If type is CANData data can store up to 8 byte data.
75 unsigned char data[8];
78 * Length of data in bytes.
80 * If type is CANData data can store up to 8 byte data.
85 * Defines if the message has standard or extended format.
87 * Defines the type of message id:
88 * Default is CANStandard which implies 11 bit id.
89 * CANExtended means 29 bit message id.
94 * Defines the type of a message.
96 * The message type can rather be CANData for a message with data (default).
97 * Or CANRemote for a request of a specific CAN message.
99 CANType type; // 0 - DATA FRAME, 1 - REMOTE FRAME
104 * A can bus client, used for communicating with can devices
106 class CAN : public Base {
111 * Creates an CAN interface connected to specific pins.
114 * > #include "mbed.h"
117 * > DigitalOut led1(LED1);
118 * > DigitalOut led2(LED2);
119 * > CAN can1(p9, p10);
120 * > CAN can2(p30, p29);
122 * > char counter = 0;
125 * > if(can1.write(CANMessage(1337, &counter, 1))) {
126 * > printf("Message sent: %d\n", counter);
133 * > ticker.attach(&send, 1);
136 * > if(can2.read(msg)) {
137 * > printf("Message received: %d\n\n", msg.data[0]);
145 * rd - read from transmitter
146 * td - transmit to transmitter
148 CAN(PinName rd, PinName td);
151 /* Function: frequency
152 * Set the frequency of the CAN interface
155 * hz - The bus frequency in hertz
156 * returns - 1 if successful, 0 otherwise
158 int frequency(int hz);
161 * Write a CANMessage to the bus.
164 * msg - The CANMessage to write.
167 * 0 - If write failed.
168 * 1 - If write was successful.
170 int write(CANMessage msg);
173 * Read a CANMessage from the bus.
176 * msg - A CANMessage to read to.
179 * 0 - If no message arrived.
180 * 1 - If message arrived.
182 int read(CANMessage &msg);
185 * Reset CAN interface.
187 * To use after error overflow.
192 * Puts or removes the CAN interface into silent monitoring mode
195 * silent - boolean indicating whether to go into silent mode or not
197 void monitor(bool silent);
200 * Returns number of read errors to detect read overflow errors.
202 unsigned char rderror();
205 * Returns number of write errors to detect write overflow errors.
207 unsigned char tderror();
210 * Attach a function to call whenever a CAN frame received interrupt is
214 * fptr - A pointer to a void function, or 0 to set as none
216 void attach(void (*fptr)(void));
219 * Attach a member function to call whenever a CAN frame received interrupt
223 * tptr - pointer to the object to call the member function on
224 * mptr - pointer to the member function to be called
227 void attach(T* tptr, void (T::*mptr)(void));
232 FunctionPointer _rxirq;
234 void setup_interrupt(void);
235 void remove_interrupt(void);