mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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