Life is random and unfair!
Fork of mbed-dev by
Embed:
(wiki syntax)
Show/hide line numbers
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 DEVICE_CAN 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 00031 /** CANMessage class 00032 * 00033 * @Note Synchronization level: Thread safe 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 */ 00071 class CAN { 00072 00073 public: 00074 /** Creates an CAN interface connected to specific pins. 00075 * 00076 * @param rd read from transmitter 00077 * @param td transmit to transmitter 00078 * 00079 * Example: 00080 * @code 00081 * #include "mbed.h" 00082 * 00083 * Ticker ticker; 00084 * DigitalOut led1(LED1); 00085 * DigitalOut led2(LED2); 00086 * CAN can1(p9, p10); 00087 * CAN can2(p30, p29); 00088 * 00089 * char counter = 0; 00090 * 00091 * void send() { 00092 * if(can1.write(CANMessage(1337, &counter, 1))) { 00093 * printf("Message sent: %d\n", counter); 00094 * counter++; 00095 * } 00096 * led1 = !led1; 00097 * } 00098 * 00099 * int main() { 00100 * ticker.attach(&send, 1); 00101 * CANMessage msg; 00102 * while(1) { 00103 * if(can2.read(msg)) { 00104 * printf("Message received: %d\n\n", msg.data[0]); 00105 * led2 = !led2; 00106 * } 00107 * wait(0.2); 00108 * } 00109 * } 00110 * @endcode 00111 */ 00112 CAN(PinName rd, PinName td); 00113 virtual ~CAN(); 00114 00115 /** Set the frequency of the CAN interface 00116 * 00117 * @param hz The bus frequency in hertz 00118 * 00119 * @returns 00120 * 1 if successful, 00121 * 0 otherwise 00122 */ 00123 int frequency(int hz); 00124 00125 /** Write a CANMessage to the bus. 00126 * 00127 * @param msg The CANMessage to write. 00128 * 00129 * @returns 00130 * 0 if write failed, 00131 * 1 if write was successful 00132 */ 00133 int write(CANMessage msg); 00134 00135 /** Read a CANMessage from the bus. 00136 * 00137 * @param msg A CANMessage to read to. 00138 * @param handle message filter handle (0 for any message) 00139 * 00140 * @returns 00141 * 0 if no message arrived, 00142 * 1 if message arrived 00143 */ 00144 int read(CANMessage &msg, int handle = 0); 00145 00146 /** Reset CAN interface. 00147 * 00148 * To use after error overflow. 00149 */ 00150 void reset(); 00151 00152 /** Puts or removes the CAN interface into silent monitoring mode 00153 * 00154 * @param silent boolean indicating whether to go into silent mode or not 00155 */ 00156 void monitor(bool silent); 00157 00158 enum Mode { 00159 Reset = 0, 00160 Normal, 00161 Silent, 00162 LocalTest, 00163 GlobalTest, 00164 SilentTest 00165 }; 00166 00167 /** Change CAN operation to the specified mode 00168 * 00169 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) 00170 * 00171 * @returns 00172 * 0 if mode change failed or unsupported, 00173 * 1 if mode change was successful 00174 */ 00175 int mode(Mode mode); 00176 00177 /** Filter out incomming messages 00178 * 00179 * @param id the id to filter on 00180 * @param mask the mask applied to the id 00181 * @param format format to filter on (Default CANAny) 00182 * @param handle message filter handle (Optional) 00183 * 00184 * @returns 00185 * 0 if filter change failed or unsupported, 00186 * new filter handle if successful 00187 */ 00188 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0); 00189 00190 /** Returns number of read errors to detect read overflow errors. 00191 */ 00192 unsigned char rderror(); 00193 00194 /** Returns number of write errors to detect write overflow errors. 00195 */ 00196 unsigned char tderror(); 00197 00198 enum IrqType { 00199 RxIrq = 0, 00200 TxIrq, 00201 EwIrq, 00202 DoIrq, 00203 WuIrq, 00204 EpIrq, 00205 AlIrq, 00206 BeIrq, 00207 IdIrq, 00208 00209 IrqCnt 00210 }; 00211 00212 /** Attach a function to call whenever a CAN frame received interrupt is 00213 * generated. 00214 * 00215 * @param func A pointer to a void function, or 0 to set as none 00216 * @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) 00217 */ 00218 void attach(Callback<void()> func, IrqType type=RxIrq); 00219 00220 /** Attach a member function to call whenever a CAN frame received interrupt 00221 * is generated. 00222 * 00223 * @param obj pointer to the object to call the member function on 00224 * @param method pointer to the member function to be called 00225 * @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) 00226 */ 00227 template<typename T> 00228 void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { 00229 // Underlying call thread safe 00230 attach(Callback<void()>(obj, method), type); 00231 } 00232 00233 /** Attach a member function to call whenever a CAN frame received interrupt 00234 * is generated. 00235 * 00236 * @param obj pointer to the object to call the member function on 00237 * @param method pointer to the member function to be called 00238 * @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) 00239 */ 00240 template<typename T> 00241 void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { 00242 // Underlying call thread safe 00243 attach(Callback<void()>(obj, method), type); 00244 } 00245 00246 static void _irq_handler(uint32_t id, CanIrqType type); 00247 00248 protected: 00249 virtual void lock(); 00250 virtual void unlock(); 00251 can_t _can; 00252 Callback<void()> _irq[IrqCnt]; 00253 PlatformMutex _mutex; 00254 }; 00255 00256 } // namespace mbed 00257 00258 #endif 00259 00260 #endif // MBED_CAN_H 00261 00262 /** @}*/
Generated on Tue Jul 12 2022 19:28:22 by 1.7.2