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