mbed library sources. Supersedes mbed-src.
Dependents: LPCXpresso1769_blinky
Fork of mbed-dev by
CAN.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_CAN_H 00017 #define MBED_CAN_H 00018 00019 #include "platform.h" 00020 00021 #if DEVICE_CAN 00022 00023 #include "can_api.h" 00024 #include "can_helper.h" 00025 #include "Callback.h" 00026 #include "PlatformMutex.h" 00027 00028 namespace mbed { 00029 00030 /** CANMessage class 00031 * 00032 * @Note Synchronization level: Thread safe 00033 */ 00034 class CANMessage : public CAN_Message { 00035 00036 public: 00037 /** Creates empty CAN message. 00038 */ 00039 CANMessage() : CAN_Message() { 00040 len = 8; 00041 type = CANData; 00042 format = CANStandard; 00043 id = 0; 00044 memset(data, 0, 8); 00045 } 00046 00047 /** Creates CAN message with specific content. 00048 */ 00049 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { 00050 len = _len & 0xF; 00051 type = _type; 00052 format = _format; 00053 id = _id; 00054 memcpy(data, _data, _len); 00055 } 00056 00057 /** Creates CAN remote message. 00058 */ 00059 CANMessage(int _id, CANFormat _format = CANStandard) { 00060 len = 0; 00061 type = CANRemote; 00062 format = _format; 00063 id = _id; 00064 memset(data, 0, 8); 00065 } 00066 }; 00067 00068 /** A can bus client, used for communicating with can devices 00069 */ 00070 class CAN { 00071 00072 public: 00073 /** Creates an CAN interface connected to specific pins. 00074 * 00075 * @param rd read from transmitter 00076 * @param td transmit to transmitter 00077 * 00078 * Example: 00079 * @code 00080 * #include "mbed.h" 00081 * 00082 * Ticker ticker; 00083 * DigitalOut led1(LED1); 00084 * DigitalOut led2(LED2); 00085 * CAN can1(p9, p10); 00086 * CAN can2(p30, p29); 00087 * 00088 * char counter = 0; 00089 * 00090 * void send() { 00091 * if(can1.write(CANMessage(1337, &counter, 1))) { 00092 * printf("Message sent: %d\n", counter); 00093 * counter++; 00094 * } 00095 * led1 = !led1; 00096 * } 00097 * 00098 * int main() { 00099 * ticker.attach(&send, 1); 00100 * CANMessage msg; 00101 * while(1) { 00102 * if(can2.read(msg)) { 00103 * printf("Message received: %d\n\n", msg.data[0]); 00104 * led2 = !led2; 00105 * } 00106 * wait(0.2); 00107 * } 00108 * } 00109 * @endcode 00110 */ 00111 CAN(PinName rd, PinName td); 00112 virtual ~CAN(); 00113 00114 /** Set the frequency of the CAN interface 00115 * 00116 * @param hz The bus frequency in hertz 00117 * 00118 * @returns 00119 * 1 if successful, 00120 * 0 otherwise 00121 */ 00122 int frequency(int hz); 00123 00124 /** Write a CANMessage to the bus. 00125 * 00126 * @param msg The CANMessage to write. 00127 * 00128 * @returns 00129 * 0 if write failed, 00130 * 1 if write was successful 00131 */ 00132 int write(CANMessage msg); 00133 00134 /** Read a CANMessage from the bus. 00135 * 00136 * @param msg A CANMessage to read to. 00137 * @param handle message filter handle (0 for any message) 00138 * 00139 * @returns 00140 * 0 if no message arrived, 00141 * 1 if message arrived 00142 */ 00143 int read(CANMessage &msg, int handle = 0); 00144 00145 /** Reset CAN interface. 00146 * 00147 * To use after error overflow. 00148 */ 00149 void reset(); 00150 00151 /** Puts or removes the CAN interface into silent monitoring mode 00152 * 00153 * @param silent boolean indicating whether to go into silent mode or not 00154 */ 00155 void monitor(bool silent); 00156 00157 enum Mode { 00158 Reset = 0, 00159 Normal, 00160 Silent, 00161 LocalTest, 00162 GlobalTest, 00163 SilentTest 00164 }; 00165 00166 /** Change CAN operation to the specified mode 00167 * 00168 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) 00169 * 00170 * @returns 00171 * 0 if mode change failed or unsupported, 00172 * 1 if mode change was successful 00173 */ 00174 int mode(Mode mode); 00175 00176 /** Filter out incomming messages 00177 * 00178 * @param id the id to filter on 00179 * @param mask the mask applied to the id 00180 * @param format format to filter on (Default CANAny) 00181 * @param handle message filter handle (Optional) 00182 * 00183 * @returns 00184 * 0 if filter change failed or unsupported, 00185 * new filter handle if successful 00186 */ 00187 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0); 00188 00189 /** Returns number of read errors to detect read overflow errors. 00190 */ 00191 unsigned char rderror(); 00192 00193 /** Returns number of write errors to detect write overflow errors. 00194 */ 00195 unsigned char tderror(); 00196 00197 enum IrqType { 00198 RxIrq = 0, 00199 TxIrq, 00200 EwIrq, 00201 DoIrq, 00202 WuIrq, 00203 EpIrq, 00204 AlIrq, 00205 BeIrq, 00206 IdIrq, 00207 00208 IrqCnt 00209 }; 00210 00211 /** Attach a function to call whenever a CAN frame received interrupt is 00212 * generated. 00213 * 00214 * @param func A pointer to a void function, or 0 to set as none 00215 * @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) 00216 */ 00217 void attach(Callback<void()> func, IrqType type=RxIrq); 00218 00219 /** Attach a member function to call whenever a CAN frame received interrupt 00220 * is generated. 00221 * 00222 * @param obj pointer to the object to call the member function on 00223 * @param method pointer to the member function to be called 00224 * @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) 00225 */ 00226 template<typename T> 00227 void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { 00228 // Underlying call thread safe 00229 attach(Callback<void()>(obj, method), type); 00230 } 00231 00232 /** Attach a member function to call whenever a CAN frame received interrupt 00233 * is generated. 00234 * 00235 * @param obj pointer to the object to call the member function on 00236 * @param method pointer to the member function to be called 00237 * @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) 00238 */ 00239 template<typename T> 00240 void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { 00241 // Underlying call thread safe 00242 attach(Callback<void()>(obj, method), type); 00243 } 00244 00245 static void _irq_handler(uint32_t id, CanIrqType type); 00246 00247 protected: 00248 virtual void lock(); 00249 virtual void unlock(); 00250 can_t _can; 00251 Callback<void()> _irq[IrqCnt]; 00252 PlatformMutex _mutex; 00253 }; 00254 00255 } // namespace mbed 00256 00257 #endif 00258 00259 #endif // MBED_CAN_H
Generated on Tue Jul 12 2022 16:04:25 by 1.7.2