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