Christian Weiß / Mbed 2 deprecated Diplomarbeit_MW_CW

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CAN.h Source File

CAN.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00020  * SOFTWARE.
00021  */
00022 #ifndef MBED_CAN_H
00023 #define MBED_CAN_H
00024 
00025 #include "platform.h"
00026 
00027 #if DEVICE_CAN
00028 
00029 #include "can_api.h"
00030 #include "can_helper.h"
00031 #include "FunctionPointer.h"
00032 
00033 namespace mbed {
00034 
00035 /** CANMessage class
00036  */
00037 class CANMessage : public CAN_Message {
00038 
00039 public:
00040     /** Creates empty CAN message.
00041      */
00042     CANMessage() {
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     CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
00053       len    = _len & 0xF;
00054       type   = _type;
00055       format = _format;
00056       id     = _id;
00057       memcpy(data, _data, _len);
00058     }
00059 
00060     /** Creates CAN remote message.
00061      */
00062     CANMessage(int _id, CANFormat _format = CANStandard) {
00063       len    = 0;
00064       type   = CANRemote;
00065       format = _format;
00066       id     = _id;
00067       memset(data, 0, 8);
00068     }
00069 };
00070 
00071 /** A can bus client, used for communicating with can devices
00072  */
00073 class CAN {
00074 
00075 public:
00076     /** Creates an CAN interface connected to specific pins.
00077      *
00078      *  @param rd read from transmitter
00079      *  @param td transmit to transmitter
00080      *
00081      * Example:
00082      * @code
00083      * #include "mbed.h"
00084      *
00085      * Ticker ticker;
00086      * DigitalOut led1(LED1);
00087      * DigitalOut led2(LED2);
00088      * CAN can1(p9, p10);
00089      * CAN can2(p30, p29);
00090      *
00091      * char counter = 0;
00092      *
00093      * void send() {
00094      *     if(can1.write(CANMessage(1337, &counter, 1))) {
00095      *         printf("Message sent: %d\n", counter);
00096      *         counter++;
00097      *     }
00098      *     led1 = !led1;
00099      * }
00100      *
00101      * int main() {
00102      *     ticker.attach(&send, 1);
00103      *    CANMessage msg;
00104      *     while(1) {
00105      *         if(can2.read(msg)) {
00106      *             printf("Message received: %d\n\n", msg.data[0]);
00107      *             led2 = !led2;
00108      *         }
00109      *         wait(0.2);
00110      *     }
00111      * }
00112      * @endcode
00113      */
00114     CAN(PinName rd, PinName td);
00115     virtual ~CAN();
00116 
00117     /** Set the frequency of the CAN interface
00118      *
00119      *  @param hz The bus frequency in hertz
00120      *
00121      *  @returns
00122      *    1 if successful,
00123      *    0 otherwise
00124      */
00125     int frequency(int hz);
00126 
00127     /** Write a CANMessage to the bus.
00128      *
00129      *  @param msg The CANMessage to write.
00130      *
00131      *  @returns
00132      *    0 if write failed,
00133      *    1 if write was successful
00134      */
00135     int write(CANMessage msg);
00136 
00137     /** Read a CANMessage from the bus.
00138      *
00139      *  @param msg A CANMessage to read to.
00140      *
00141      *  @returns
00142      *    0 if no message arrived,
00143      *    1 if message arrived
00144      */
00145     int read(CANMessage &msg);
00146 
00147     /** Reset CAN interface.
00148      *
00149      * To use after error overflow.
00150      */
00151     void reset();
00152 
00153     /** Puts or removes the CAN interface into silent monitoring mode
00154      *
00155      *  @param silent boolean indicating whether to go into silent mode or not
00156      */
00157     void monitor(bool silent);
00158 
00159     /** Returns number of read errors to detect read overflow errors.
00160      */
00161     unsigned char rderror();
00162 
00163     /** Returns number of write errors to detect write overflow errors.
00164      */
00165     unsigned char tderror();
00166 
00167     /** Attach a function to call whenever a CAN frame received interrupt is
00168      *  generated.
00169      *
00170      *  @param fptr A pointer to a void function, or 0 to set as none
00171      */
00172     void attach(void (*fptr)(void));
00173 
00174    /** Attach a member function to call whenever a CAN frame received interrupt
00175     *  is generated.
00176     *
00177     *  @param tptr pointer to the object to call the member function on
00178     *  @param mptr pointer to the member function to be called
00179     */
00180    template<typename T>
00181    void attach(T* tptr, void (T::*mptr)(void)) {
00182         if((mptr != NULL) && (tptr != NULL)) {
00183             _rxirq.attach(tptr, mptr);
00184             setup_interrupt();
00185         } else {
00186             remove_interrupt();
00187         }
00188     }
00189 
00190 private:
00191     can_t _can;
00192     FunctionPointer _rxirq;
00193 
00194     void setup_interrupt(void);
00195     void remove_interrupt(void);
00196 };
00197 
00198 } // namespace mbed
00199 
00200 #endif
00201 
00202 #endif    // MBED_CAN_H
00203