1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include "can_helper.h"
25 #include "FunctionPointer.h"
31 class CANMessage : public CAN_Message {
34 /** Creates empty CAN message.
36 CANMessage() : CAN_Message() {
44 /** Creates CAN message with specific content.
46 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
51 memcpy(data, _data, _len);
54 /** Creates CAN remote message.
56 CANMessage(int _id, CANFormat _format = CANStandard) {
65 /** A can bus client, used for communicating with can devices
70 /** Creates an CAN interface connected to specific pins.
72 * @param rd read from transmitter
73 * @param td transmit to transmitter
80 * DigitalOut led1(LED1);
81 * DigitalOut led2(LED2);
88 * if(can1.write(CANMessage(1337, &counter, 1))) {
89 * printf("Message sent: %d\n", counter);
96 * ticker.attach(&send, 1);
99 * if(can2.read(msg)) {
100 * printf("Message received: %d\n\n", msg.data[0]);
108 CAN(PinName rd, PinName td);
111 /** Set the frequency of the CAN interface
113 * @param hz The bus frequency in hertz
119 int frequency(int hz);
121 /** Write a CANMessage to the bus.
123 * @param msg The CANMessage to write.
127 * 1 if write was successful
129 int write(CANMessage msg);
131 /** Read a CANMessage from the bus.
133 * @param msg A CANMessage to read to.
134 * @param handle message filter handle (0 for any message)
137 * 0 if no message arrived,
138 * 1 if message arrived
140 int read(CANMessage &msg, int handle = 0);
142 /** Reset CAN interface.
144 * To use after error overflow.
148 /** Puts or removes the CAN interface into silent monitoring mode
150 * @param silent boolean indicating whether to go into silent mode or not
152 void monitor(bool silent);
163 /** Change CAN operation to the specified mode
165 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
168 * 0 if mode change failed or unsupported,
169 * 1 if mode change was successful
173 /** Filter out incomming messages
175 * @param id the id to filter on
176 * @param mask the mask applied to the id
177 * @param format format to filter on (Default CANAny)
178 * @param handle message filter handle (Optional)
181 * 0 if filter change failed or unsupported,
182 * new filter handle if successful
184 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
186 /** Returns number of read errors to detect read overflow errors.
188 unsigned char rderror();
190 /** Returns number of write errors to detect write overflow errors.
192 unsigned char tderror();
206 /** Attach a function to call whenever a CAN frame received interrupt is
209 * @param fptr A pointer to a void function, or 0 to set as none
210 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error)
212 void attach(void (*fptr)(void), IrqType type=RxIrq);
214 /** Attach a member function to call whenever a CAN frame received interrupt
217 * @param tptr pointer to the object to call the member function on
218 * @param mptr pointer to the member function to be called
219 * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error)
222 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
223 if((mptr != NULL) && (tptr != NULL)) {
224 _irq[type].attach(tptr, mptr);
225 can_irq_set(&_can, (CanIrqType)type, 1);
228 can_irq_set(&_can, (CanIrqType)type, 0);
232 static void _irq_handler(uint32_t id, CanIrqType type);
236 FunctionPointer _irq[9];