Kenji Arai / TYBLE16_mbedlized_os5_several_examples_1st

Dependencies:   nRF51_Vdd TextLCD BME280

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