The official mbed C/C SDK provides the software platform and libraries to build your applications.
Fork of mbed 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 "FunctionPointer.h" 00026 00027 namespace mbed { 00028 00029 /** CANMessage class 00030 */ 00031 class CANMessage : public CAN_Message { 00032 00033 public: 00034 /** Creates empty CAN message. 00035 */ 00036 CANMessage() { 00037 len = 8; 00038 type = CANData; 00039 format = CANStandard; 00040 id = 0; 00041 memset(data, 0, 8); 00042 } 00043 00044 /** Creates CAN message with specific content. 00045 */ 00046 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) { 00047 len = _len & 0xF; 00048 type = _type; 00049 format = _format; 00050 id = _id; 00051 memcpy(data, _data, _len); 00052 } 00053 00054 /** Creates CAN remote message. 00055 */ 00056 CANMessage(int _id, CANFormat _format = CANStandard) { 00057 len = 0; 00058 type = CANRemote; 00059 format = _format; 00060 id = _id; 00061 memset(data, 0, 8); 00062 } 00063 }; 00064 00065 /** A can bus client, used for communicating with can devices 00066 */ 00067 class CAN { 00068 00069 public: 00070 /** Creates an CAN interface connected to specific pins. 00071 * 00072 * @param rd read from transmitter 00073 * @param td transmit to transmitter 00074 * 00075 * Example: 00076 * @code 00077 * #include "mbed.h" 00078 * 00079 * Ticker ticker; 00080 * DigitalOut led1(LED1); 00081 * DigitalOut led2(LED2); 00082 * CAN can1(p9, p10); 00083 * CAN can2(p30, p29); 00084 * 00085 * char counter = 0; 00086 * 00087 * void send() { 00088 * if(can1.write(CANMessage(1337, &counter, 1))) { 00089 * printf("Message sent: %d\n", counter); 00090 * counter++; 00091 * } 00092 * led1 = !led1; 00093 * } 00094 * 00095 * int main() { 00096 * ticker.attach(&send, 1); 00097 * CANMessage msg; 00098 * while(1) { 00099 * if(can2.read(msg)) { 00100 * printf("Message received: %d\n\n", msg.data[0]); 00101 * led2 = !led2; 00102 * } 00103 * wait(0.2); 00104 * } 00105 * } 00106 * @endcode 00107 */ 00108 CAN(PinName rd, PinName td); 00109 virtual ~CAN(); 00110 00111 /** Set the frequency of the CAN interface 00112 * 00113 * @param hz The bus frequency in hertz 00114 * 00115 * @returns 00116 * 1 if successful, 00117 * 0 otherwise 00118 */ 00119 int frequency(int hz); 00120 00121 /** Write a CANMessage to the bus. 00122 * 00123 * @param msg The CANMessage to write. 00124 * 00125 * @returns 00126 * 0 if write failed, 00127 * 1 if write was successful 00128 */ 00129 int write(CANMessage msg); 00130 00131 /** Read a CANMessage from the bus. 00132 * 00133 * @param msg A CANMessage to read to. 00134 * 00135 * @returns 00136 * 0 if no message arrived, 00137 * 1 if message arrived 00138 */ 00139 int read(CANMessage &msg); 00140 00141 /** Reset CAN interface. 00142 * 00143 * To use after error overflow. 00144 */ 00145 void reset(); 00146 00147 /** Puts or removes the CAN interface into silent monitoring mode 00148 * 00149 * @param silent boolean indicating whether to go into silent mode or not 00150 */ 00151 void monitor(bool silent); 00152 00153 enum Mode { 00154 Reset = 0, 00155 Normal, 00156 Silent, 00157 LocalTest, 00158 GlobalTest, 00159 SilentTest 00160 }; 00161 00162 /** Change CAN operation to the specified mode 00163 * 00164 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest) 00165 * 00166 * @returns 00167 * 0 if mode change failed or unsupported, 00168 * 1 if mode change was successful 00169 */ 00170 int mode(Mode mode); 00171 00172 /** Returns number of read errors to detect read overflow errors. 00173 */ 00174 unsigned char rderror(); 00175 00176 /** Returns number of write errors to detect write overflow errors. 00177 */ 00178 unsigned char tderror(); 00179 00180 enum IrqType { 00181 RxIrq = 0, 00182 TxIrq, 00183 EwIrq, 00184 DoIrq, 00185 WuIrq, 00186 EpIrq, 00187 AlIrq, 00188 BeIrq, 00189 IdIrq 00190 }; 00191 00192 /** Attach a function to call whenever a CAN frame received interrupt is 00193 * generated. 00194 * 00195 * @param fptr A pointer to a void function, or 0 to set as none 00196 * @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) 00197 */ 00198 void attach(void (*fptr)(void), IrqType type=RxIrq); 00199 00200 /** Attach a member function to call whenever a CAN frame received interrupt 00201 * is generated. 00202 * 00203 * @param tptr pointer to the object to call the member function on 00204 * @param mptr pointer to the member function to be called 00205 * @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) 00206 */ 00207 template<typename T> 00208 void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { 00209 if((mptr != NULL) && (tptr != NULL)) { 00210 _irq[type].attach(tptr, mptr); 00211 can_irq_set(&_can, (CanIrqType)type, 1); 00212 } 00213 else { 00214 can_irq_set(&_can, (CanIrqType)type, 0); 00215 } 00216 } 00217 00218 static void _irq_handler(uint32_t id, CanIrqType type); 00219 00220 protected: 00221 can_t _can; 00222 FunctionPointer _irq[9]; 00223 }; 00224 00225 } // namespace mbed 00226 00227 #endif 00228 00229 #endif // MBED_CAN_H
Generated on Sat Jul 16 2022 06:59:32 by 1.7.2