af3c7d986fdf58c0c25b26399ff484bcfa713128
1 /* mbed Microcontroller Library - can
2 * Copyright (c) 2009-2011 ARM Limited. All rights reserved.
15 #include "PeripheralNames.h"
17 #include "can_helper.h"
18 #include "FunctionPointer.h"
27 class CANMessage
: public CAN_Message
{
31 /* Constructor: CANMessage
32 * Creates empty CAN message.
42 /* Constructor: CANMessage
43 * Creates CAN message with specific content.
45 CANMessage(int _id
, const char *_data
, char _len
= 8, CANType _type
= CANData
, CANFormat _format
= CANStandard
) {
50 memcpy(data
, _data
, _len
);
53 /* Constructor: CANMessage
54 * Creates CAN remote message.
56 CANMessage(int _id
, CANFormat _format
= CANStandard
) {
63 #if 0 // Inhereted from CAN_Message, for documentation only
68 * If format is CANStandard it must be an 11 bit long id
69 * If format is CANExtended it must be an 29 bit long id
74 * Space for 8 byte payload.
76 * If type is CANData data can store up to 8 byte data.
78 unsigned char data
[8];
81 * Length of data in bytes.
83 * If type is CANData data can store up to 8 byte data.
88 * Defines if the message has standard or extended format.
90 * Defines the type of message id:
91 * Default is CANStandard which implies 11 bit id.
92 * CANExtended means 29 bit message id.
97 * Defines the type of a message.
99 * The message type can rather be CANData for a message with data (default).
100 * Or CANRemote for a request of a specific CAN message.
102 CANType type
; // 0 - DATA FRAME, 1 - REMOTE FRAME
107 * A can bus client, used for communicating with can devices
109 class CAN
: public Base
{
114 * Creates an CAN interface connected to specific pins.
117 * > #include "mbed.h"
120 * > DigitalOut led1(LED1);
121 * > DigitalOut led2(LED2);
122 * > CAN can1(p9, p10);
123 * > CAN can2(p30, p29);
125 * > char counter = 0;
128 * > if(can1.write(CANMessage(1337, &counter, 1))) {
129 * > printf("Message sent: %d\n", counter);
136 * > ticker.attach(&send, 1);
139 * > if(can2.read(msg)) {
140 * > printf("Message received: %d\n\n", msg.data[0]);
148 * rd - read from transmitter
149 * td - transmit to transmitter
151 CAN(PinName rd
, PinName td
);
154 /* Function: frequency
155 * Set the frequency of the CAN interface
158 * hz - The bus frequency in hertz
159 * returns - 1 if successful, 0 otherwise
161 int frequency(int hz
);
164 * Write a CANMessage to the bus.
167 * msg - The CANMessage to write.
170 * 0 - If write failed.
171 * 1 - If write was successful.
173 int write(CANMessage msg
);
176 * Read a CANMessage from the bus.
179 * msg - A CANMessage to read to.
182 * 0 - If no message arrived.
183 * 1 - If message arrived.
185 int read(CANMessage
&msg
);
188 * Reset CAN interface.
190 * To use after error overflow.
195 * Puts or removes the CAN interface into silent monitoring mode
198 * silent - boolean indicating whether to go into silent mode or not
200 void monitor(bool silent
);
203 * Returns number of read errors to detect read overflow errors.
205 unsigned char rderror();
208 * Returns number of write errors to detect write overflow errors.
210 unsigned char tderror();
213 * Attach a function to call whenever a CAN frame received interrupt is
217 * fptr - A pointer to a void function, or 0 to set as none
219 void attach(void (*fptr
)(void));
222 * Attach a member function to call whenever a CAN frame received interrupt
226 * tptr - pointer to the object to call the member function on
227 * mptr - pointer to the member function to be called
230 void attach(T
* tptr
, void (T::*mptr
)(void)) {
231 if((mptr
!= NULL
) && (tptr
!= NULL
)) {
232 _rxirq
.attach(tptr
, mptr
);
242 FunctionPointer _rxirq
;
244 void setup_interrupt(void);
245 void remove_interrupt(void);