Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
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/platform.h" 00020 00021 #if defined (DEVICE_CAN) || defined(DOXYGEN_ONLY) 00022 00023 #include "hal/can_api.h" 00024 #include "platform/Callback.h" 00025 #include "platform/PlatformMutex.h" 00026 #include "platform/NonCopyable.h" 00027 00028 namespace mbed { 00029 /** \addtogroup drivers */ 00030 00031 /** CANMessage class 00032 * 00033 * @note Synchronization level: Thread safe 00034 * @ingroup drivers 00035 */ 00036 class CANMessage : public CAN_Message { 00037 00038 public: 00039 /** Creates empty CAN message. 00040 */ 00041 CANMessage() : CAN_Message() 00042 { 00043 len = 8; 00044 type = CANData; 00045 format = CANStandard; 00046 id = 0; 00047 memset(data, 0, 8); 00048 } 00049 00050 /** Creates CAN message with specific content. 00051 * 00052 * @param _id Message ID 00053 * @param _data Mesaage Data 00054 * @param _len Message Data length 00055 * @param _type Type of Data: Use enum CANType for valid parameter values 00056 * @param _format Data Format: Use enum CANFormat for valid parameter values 00057 */ 00058 CANMessage(unsigned _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) 00059 { 00060 len = _len & 0xF; 00061 type = _type; 00062 format = _format; 00063 id = _id; 00064 memcpy(data, _data, _len); 00065 } 00066 00067 /** Creates CAN remote message. 00068 * 00069 * @param _id Message ID 00070 * @param _format Data Format: Use enum CANType for valid parameter values 00071 */ 00072 CANMessage(unsigned _id, CANFormat _format = CANStandard) 00073 { 00074 len = 0; 00075 type = CANRemote; 00076 format = _format; 00077 id = _id; 00078 memset(data, 0, 8); 00079 } 00080 }; 00081 00082 /** A can bus client, used for communicating with can devices 00083 * @ingroup drivers 00084 */ 00085 class CAN : private NonCopyable<CAN> { 00086 00087 public: 00088 /** Creates an CAN interface connected to specific pins. 00089 * 00090 * @param rd read from transmitter 00091 * @param td transmit to transmitter 00092 * 00093 * Example: 00094 * @code 00095 * #include "mbed.h" 00096 * 00097 * Ticker ticker; 00098 * DigitalOut led1(LED1); 00099 * DigitalOut led2(LED2); 00100 * CAN can1(p9, p10); 00101 * CAN can2(p30, p29); 00102 * 00103 * char counter = 0; 00104 * 00105 * void send() { 00106 * if(can1.write(CANMessage(1337, &counter, 1))) { 00107 * printf("Message sent: %d\n", counter); 00108 * counter++; 00109 * } 00110 * led1 = !led1; 00111 * } 00112 * 00113 * int main() { 00114 * ticker.attach(&send, 1); 00115 * CANMessage msg; 00116 * while(1) { 00117 * if(can2.read(msg)) { 00118 * printf("Message received: %d\n\n", msg.data[0]); 00119 * led2 = !led2; 00120 * } 00121 * wait(0.2); 00122 * } 00123 * } 00124 * @endcode 00125 */ 00126 CAN(PinName rd, PinName td); 00127 00128 /** Initialize CAN interface and set the frequency 00129 * 00130 * @param rd the rd pin 00131 * @param td the td pin 00132 * @param hz the bus frequency in hertz 00133 */ 00134 CAN(PinName rd, PinName td, int hz); 00135 00136 virtual ~CAN(); 00137 00138 /** Set the frequency of the CAN interface 00139 * 00140 * @param hz The bus frequency in hertz 00141 * 00142 * @returns 00143 * 1 if successful, 00144 * 0 otherwise 00145 */ 00146 int frequency(int hz); 00147 00148 /** Write a CANMessage to the bus. 00149 * 00150 * @param msg The CANMessage to write. 00151 * 00152 * @returns 00153 * 0 if write failed, 00154 * 1 if write was successful 00155 */ 00156 int write(CANMessage msg); 00157 00158 /** Read a CANMessage from the bus. 00159 * 00160 * @param msg A CANMessage to read to. 00161 * @param handle message filter handle (0 for any message) 00162 * 00163 * @returns 00164 * 0 if no message arrived, 00165 * 1 if message arrived 00166 */ 00167 int read(CANMessage &msg, int handle = 0); 00168 00169 /** Reset CAN interface. 00170 * 00171 * To use after error overflow. 00172 */ 00173 void reset(); 00174 00175 /** Puts or removes the CAN interface into silent monitoring mode 00176 * 00177 * @param silent boolean indicating whether to go into silent mode or not 00178 */ 00179 void monitor(bool silent); 00180 00181 enum Mode { 00182 Reset = 0, 00183 Normal, 00184 Silent, 00185 LocalTest, 00186 GlobalTest, 00187 SilentTest 00188 }; 00189 00190 /** Change CAN operation to the specified mode 00191 * 00192 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) 00193 * 00194 * @returns 00195 * 0 if mode change failed or unsupported, 00196 * 1 if mode change was successful 00197 */ 00198 int mode(Mode mode); 00199 00200 /** Filter out incomming messages 00201 * 00202 * @param id the id to filter on 00203 * @param mask the mask applied to the id 00204 * @param format format to filter on (Default CANAny) 00205 * @param handle message filter handle (Optional) 00206 * 00207 * @returns 00208 * 0 if filter change failed or unsupported, 00209 * new filter handle if successful 00210 */ 00211 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0); 00212 00213 /** Detects read errors - Used to detect read overflow errors. 00214 * 00215 * @returns number of read errors 00216 */ 00217 unsigned char rderror(); 00218 00219 /** Detects write errors - Used to detect write overflow errors. 00220 * 00221 * @returns number of write errors 00222 */ 00223 unsigned char tderror(); 00224 00225 enum IrqType { 00226 RxIrq = 0, 00227 TxIrq, 00228 EwIrq, 00229 DoIrq, 00230 WuIrq, 00231 EpIrq, 00232 AlIrq, 00233 BeIrq, 00234 IdIrq, 00235 00236 IrqCnt 00237 }; 00238 00239 /** Attach a function to call whenever a CAN frame received interrupt is 00240 * generated. 00241 * 00242 * This function locks the deep sleep while a callback is attached 00243 * 00244 * @param func A pointer to a void function, or 0 to set as none 00245 * @param type 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) 00246 */ 00247 void attach(Callback<void()> func, IrqType type = RxIrq); 00248 00249 /** Attach a member function to call whenever a CAN frame received interrupt 00250 * is generated. 00251 * 00252 * @param obj pointer to the object to call the member function on 00253 * @param method pointer to the member function to be called 00254 * @param type 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) 00255 * @deprecated 00256 * The attach function does not support cv-qualifiers. Replaced by 00257 * attach(callback(obj, method), type). 00258 */ 00259 template<typename T> 00260 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00261 "The attach function does not support cv-qualifiers. Replaced by " 00262 "attach(callback(obj, method), type).") 00263 void attach(T *obj, void (T::*method)(), IrqType type = RxIrq) 00264 { 00265 // Underlying call thread safe 00266 attach(callback(obj, method), type); 00267 } 00268 00269 /** Attach a member function to call whenever a CAN frame received interrupt 00270 * is generated. 00271 * 00272 * @param obj pointer to the object to call the member function on 00273 * @param method pointer to the member function to be called 00274 * @param type 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) 00275 * @deprecated 00276 * The attach function does not support cv-qualifiers. Replaced by 00277 * attach(callback(obj, method), type). 00278 */ 00279 template<typename T> 00280 MBED_DEPRECATED_SINCE("mbed-os-5.1", 00281 "The attach function does not support cv-qualifiers. Replaced by " 00282 "attach(callback(obj, method), type).") 00283 void attach(T *obj, void (*method)(T *), IrqType type = RxIrq) 00284 { 00285 // Underlying call thread safe 00286 attach(callback(obj, method), type); 00287 } 00288 00289 static void _irq_handler(uint32_t id, CanIrqType type); 00290 00291 protected: 00292 virtual void lock(); 00293 virtual void unlock(); 00294 can_t _can; 00295 Callback<void()> _irq[IrqCnt]; 00296 PlatformMutex _mutex; 00297 }; 00298 00299 } // namespace mbed 00300 00301 #endif 00302 00303 #endif // MBED_CAN_H 00304
Generated on Tue Aug 9 2022 00:37:03 by
