1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:06036f8bee2d 1 /* mbed Microcontroller Library
ganlikun 0:06036f8bee2d 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:06036f8bee2d 3 *
ganlikun 0:06036f8bee2d 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:06036f8bee2d 5 * you may not use this file except in compliance with the License.
ganlikun 0:06036f8bee2d 6 * You may obtain a copy of the License at
ganlikun 0:06036f8bee2d 7 *
ganlikun 0:06036f8bee2d 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:06036f8bee2d 9 *
ganlikun 0:06036f8bee2d 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:06036f8bee2d 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:06036f8bee2d 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:06036f8bee2d 13 * See the License for the specific language governing permissions and
ganlikun 0:06036f8bee2d 14 * limitations under the License.
ganlikun 0:06036f8bee2d 15 */
ganlikun 0:06036f8bee2d 16 #ifndef MBED_CAN_H
ganlikun 0:06036f8bee2d 17 #define MBED_CAN_H
ganlikun 0:06036f8bee2d 18
ganlikun 0:06036f8bee2d 19 #include "platform/platform.h"
ganlikun 0:06036f8bee2d 20
ganlikun 0:06036f8bee2d 21 #if defined (DEVICE_CAN) || defined(DOXYGEN_ONLY)
ganlikun 0:06036f8bee2d 22
ganlikun 0:06036f8bee2d 23 #include "hal/can_api.h"
ganlikun 0:06036f8bee2d 24 #include "platform/Callback.h"
ganlikun 0:06036f8bee2d 25 #include "platform/PlatformMutex.h"
ganlikun 0:06036f8bee2d 26 #include "platform/NonCopyable.h"
ganlikun 0:06036f8bee2d 27
ganlikun 0:06036f8bee2d 28 namespace mbed {
ganlikun 0:06036f8bee2d 29 /** \addtogroup drivers */
ganlikun 0:06036f8bee2d 30
ganlikun 0:06036f8bee2d 31 /** CANMessage class
ganlikun 0:06036f8bee2d 32 *
ganlikun 0:06036f8bee2d 33 * @note Synchronization level: Thread safe
ganlikun 0:06036f8bee2d 34 * @ingroup drivers
ganlikun 0:06036f8bee2d 35 */
ganlikun 0:06036f8bee2d 36 class CANMessage : public CAN_Message {
ganlikun 0:06036f8bee2d 37
ganlikun 0:06036f8bee2d 38 public:
ganlikun 0:06036f8bee2d 39 /** Creates empty CAN message.
ganlikun 0:06036f8bee2d 40 */
ganlikun 0:06036f8bee2d 41 CANMessage() : CAN_Message() {
ganlikun 0:06036f8bee2d 42 len = 8;
ganlikun 0:06036f8bee2d 43 type = CANData;
ganlikun 0:06036f8bee2d 44 format = CANStandard;
ganlikun 0:06036f8bee2d 45 id = 0;
ganlikun 0:06036f8bee2d 46 memset(data, 0, 8);
ganlikun 0:06036f8bee2d 47 }
ganlikun 0:06036f8bee2d 48
ganlikun 0:06036f8bee2d 49 /** Creates CAN message with specific content.
ganlikun 0:06036f8bee2d 50 *
ganlikun 0:06036f8bee2d 51 * @param _id Message ID
ganlikun 0:06036f8bee2d 52 * @param _data Mesaage Data
ganlikun 0:06036f8bee2d 53 * @param _len Message Data length
ganlikun 0:06036f8bee2d 54 * @param _type Type of Data: Use enum CANType for valid parameter values
ganlikun 0:06036f8bee2d 55 * @param _format Data Format: Use enum CANFormat for valid parameter values
ganlikun 0:06036f8bee2d 56 */
ganlikun 0:06036f8bee2d 57 CANMessage(int _id, const char *_data, char _len = 8, CANType _type = CANData, CANFormat _format = CANStandard) {
ganlikun 0:06036f8bee2d 58 len = _len & 0xF;
ganlikun 0:06036f8bee2d 59 type = _type;
ganlikun 0:06036f8bee2d 60 format = _format;
ganlikun 0:06036f8bee2d 61 id = _id;
ganlikun 0:06036f8bee2d 62 memcpy(data, _data, _len);
ganlikun 0:06036f8bee2d 63 }
ganlikun 0:06036f8bee2d 64
ganlikun 0:06036f8bee2d 65 /** Creates CAN remote message.
ganlikun 0:06036f8bee2d 66 *
ganlikun 0:06036f8bee2d 67 * @param _id Message ID
ganlikun 0:06036f8bee2d 68 * @param _format Data Format: Use enum CANType for valid parameter values
ganlikun 0:06036f8bee2d 69 */
ganlikun 0:06036f8bee2d 70 CANMessage(int _id, CANFormat _format = CANStandard) {
ganlikun 0:06036f8bee2d 71 len = 0;
ganlikun 0:06036f8bee2d 72 type = CANRemote;
ganlikun 0:06036f8bee2d 73 format = _format;
ganlikun 0:06036f8bee2d 74 id = _id;
ganlikun 0:06036f8bee2d 75 memset(data, 0, 8);
ganlikun 0:06036f8bee2d 76 }
ganlikun 0:06036f8bee2d 77 };
ganlikun 0:06036f8bee2d 78
ganlikun 0:06036f8bee2d 79 /** A can bus client, used for communicating with can devices
ganlikun 0:06036f8bee2d 80 * @ingroup drivers
ganlikun 0:06036f8bee2d 81 */
ganlikun 0:06036f8bee2d 82 class CAN : private NonCopyable<CAN> {
ganlikun 0:06036f8bee2d 83
ganlikun 0:06036f8bee2d 84 public:
ganlikun 0:06036f8bee2d 85 /** Creates an CAN interface connected to specific pins.
ganlikun 0:06036f8bee2d 86 *
ganlikun 0:06036f8bee2d 87 * @param rd read from transmitter
ganlikun 0:06036f8bee2d 88 * @param td transmit to transmitter
ganlikun 0:06036f8bee2d 89 *
ganlikun 0:06036f8bee2d 90 * Example:
ganlikun 0:06036f8bee2d 91 * @code
ganlikun 0:06036f8bee2d 92 * #include "mbed.h"
ganlikun 0:06036f8bee2d 93 *
ganlikun 0:06036f8bee2d 94 * Ticker ticker;
ganlikun 0:06036f8bee2d 95 * DigitalOut led1(LED1);
ganlikun 0:06036f8bee2d 96 * DigitalOut led2(LED2);
ganlikun 0:06036f8bee2d 97 * CAN can1(p9, p10);
ganlikun 0:06036f8bee2d 98 * CAN can2(p30, p29);
ganlikun 0:06036f8bee2d 99 *
ganlikun 0:06036f8bee2d 100 * char counter = 0;
ganlikun 0:06036f8bee2d 101 *
ganlikun 0:06036f8bee2d 102 * void send() {
ganlikun 0:06036f8bee2d 103 * if(can1.write(CANMessage(1337, &counter, 1))) {
ganlikun 0:06036f8bee2d 104 * printf("Message sent: %d\n", counter);
ganlikun 0:06036f8bee2d 105 * counter++;
ganlikun 0:06036f8bee2d 106 * }
ganlikun 0:06036f8bee2d 107 * led1 = !led1;
ganlikun 0:06036f8bee2d 108 * }
ganlikun 0:06036f8bee2d 109 *
ganlikun 0:06036f8bee2d 110 * int main() {
ganlikun 0:06036f8bee2d 111 * ticker.attach(&send, 1);
ganlikun 0:06036f8bee2d 112 * CANMessage msg;
ganlikun 0:06036f8bee2d 113 * while(1) {
ganlikun 0:06036f8bee2d 114 * if(can2.read(msg)) {
ganlikun 0:06036f8bee2d 115 * printf("Message received: %d\n\n", msg.data[0]);
ganlikun 0:06036f8bee2d 116 * led2 = !led2;
ganlikun 0:06036f8bee2d 117 * }
ganlikun 0:06036f8bee2d 118 * wait(0.2);
ganlikun 0:06036f8bee2d 119 * }
ganlikun 0:06036f8bee2d 120 * }
ganlikun 0:06036f8bee2d 121 * @endcode
ganlikun 0:06036f8bee2d 122 */
ganlikun 0:06036f8bee2d 123 CAN(PinName rd, PinName td);
ganlikun 0:06036f8bee2d 124
ganlikun 0:06036f8bee2d 125 /** Initialize CAN interface and set the frequency
ganlikun 0:06036f8bee2d 126 *
ganlikun 0:06036f8bee2d 127 * @param rd the rd pin
ganlikun 0:06036f8bee2d 128 * @param td the td pin
ganlikun 0:06036f8bee2d 129 * @param hz the bus frequency in hertz
ganlikun 0:06036f8bee2d 130 */
ganlikun 0:06036f8bee2d 131 CAN(PinName rd, PinName td, int hz);
ganlikun 0:06036f8bee2d 132
ganlikun 0:06036f8bee2d 133 virtual ~CAN();
ganlikun 0:06036f8bee2d 134
ganlikun 0:06036f8bee2d 135 /** Set the frequency of the CAN interface
ganlikun 0:06036f8bee2d 136 *
ganlikun 0:06036f8bee2d 137 * @param hz The bus frequency in hertz
ganlikun 0:06036f8bee2d 138 *
ganlikun 0:06036f8bee2d 139 * @returns
ganlikun 0:06036f8bee2d 140 * 1 if successful,
ganlikun 0:06036f8bee2d 141 * 0 otherwise
ganlikun 0:06036f8bee2d 142 */
ganlikun 0:06036f8bee2d 143 int frequency(int hz);
ganlikun 0:06036f8bee2d 144
ganlikun 0:06036f8bee2d 145 /** Write a CANMessage to the bus.
ganlikun 0:06036f8bee2d 146 *
ganlikun 0:06036f8bee2d 147 * @param msg The CANMessage to write.
ganlikun 0:06036f8bee2d 148 *
ganlikun 0:06036f8bee2d 149 * @returns
ganlikun 0:06036f8bee2d 150 * 0 if write failed,
ganlikun 0:06036f8bee2d 151 * 1 if write was successful
ganlikun 0:06036f8bee2d 152 */
ganlikun 0:06036f8bee2d 153 int write(CANMessage msg);
ganlikun 0:06036f8bee2d 154
ganlikun 0:06036f8bee2d 155 /** Read a CANMessage from the bus.
ganlikun 0:06036f8bee2d 156 *
ganlikun 0:06036f8bee2d 157 * @param msg A CANMessage to read to.
ganlikun 0:06036f8bee2d 158 * @param handle message filter handle (0 for any message)
ganlikun 0:06036f8bee2d 159 *
ganlikun 0:06036f8bee2d 160 * @returns
ganlikun 0:06036f8bee2d 161 * 0 if no message arrived,
ganlikun 0:06036f8bee2d 162 * 1 if message arrived
ganlikun 0:06036f8bee2d 163 */
ganlikun 0:06036f8bee2d 164 int read(CANMessage &msg, int handle = 0);
ganlikun 0:06036f8bee2d 165
ganlikun 0:06036f8bee2d 166 /** Reset CAN interface.
ganlikun 0:06036f8bee2d 167 *
ganlikun 0:06036f8bee2d 168 * To use after error overflow.
ganlikun 0:06036f8bee2d 169 */
ganlikun 0:06036f8bee2d 170 void reset();
ganlikun 0:06036f8bee2d 171
ganlikun 0:06036f8bee2d 172 /** Puts or removes the CAN interface into silent monitoring mode
ganlikun 0:06036f8bee2d 173 *
ganlikun 0:06036f8bee2d 174 * @param silent boolean indicating whether to go into silent mode or not
ganlikun 0:06036f8bee2d 175 */
ganlikun 0:06036f8bee2d 176 void monitor(bool silent);
ganlikun 0:06036f8bee2d 177
ganlikun 0:06036f8bee2d 178 enum Mode {
ganlikun 0:06036f8bee2d 179 Reset = 0,
ganlikun 0:06036f8bee2d 180 Normal,
ganlikun 0:06036f8bee2d 181 Silent,
ganlikun 0:06036f8bee2d 182 LocalTest,
ganlikun 0:06036f8bee2d 183 GlobalTest,
ganlikun 0:06036f8bee2d 184 SilentTest
ganlikun 0:06036f8bee2d 185 };
ganlikun 0:06036f8bee2d 186
ganlikun 0:06036f8bee2d 187 /** Change CAN operation to the specified mode
ganlikun 0:06036f8bee2d 188 *
ganlikun 0:06036f8bee2d 189 * @param mode The new operation mode (CAN::Normal, CAN::Silent, CAN::LocalTest, CAN::GlobalTest, CAN::SilentTest)
ganlikun 0:06036f8bee2d 190 *
ganlikun 0:06036f8bee2d 191 * @returns
ganlikun 0:06036f8bee2d 192 * 0 if mode change failed or unsupported,
ganlikun 0:06036f8bee2d 193 * 1 if mode change was successful
ganlikun 0:06036f8bee2d 194 */
ganlikun 0:06036f8bee2d 195 int mode(Mode mode);
ganlikun 0:06036f8bee2d 196
ganlikun 0:06036f8bee2d 197 /** Filter out incomming messages
ganlikun 0:06036f8bee2d 198 *
ganlikun 0:06036f8bee2d 199 * @param id the id to filter on
ganlikun 0:06036f8bee2d 200 * @param mask the mask applied to the id
ganlikun 0:06036f8bee2d 201 * @param format format to filter on (Default CANAny)
ganlikun 0:06036f8bee2d 202 * @param handle message filter handle (Optional)
ganlikun 0:06036f8bee2d 203 *
ganlikun 0:06036f8bee2d 204 * @returns
ganlikun 0:06036f8bee2d 205 * 0 if filter change failed or unsupported,
ganlikun 0:06036f8bee2d 206 * new filter handle if successful
ganlikun 0:06036f8bee2d 207 */
ganlikun 0:06036f8bee2d 208 int filter(unsigned int id, unsigned int mask, CANFormat format = CANAny, int handle = 0);
ganlikun 0:06036f8bee2d 209
ganlikun 0:06036f8bee2d 210 /** Detects read errors - Used to detect read overflow errors.
ganlikun 0:06036f8bee2d 211 *
ganlikun 0:06036f8bee2d 212 * @returns number of read errors
ganlikun 0:06036f8bee2d 213 */
ganlikun 0:06036f8bee2d 214 unsigned char rderror();
ganlikun 0:06036f8bee2d 215
ganlikun 0:06036f8bee2d 216 /** Detects write errors - Used to detect write overflow errors.
ganlikun 0:06036f8bee2d 217 *
ganlikun 0:06036f8bee2d 218 * @returns number of write errors
ganlikun 0:06036f8bee2d 219 */
ganlikun 0:06036f8bee2d 220 unsigned char tderror();
ganlikun 0:06036f8bee2d 221
ganlikun 0:06036f8bee2d 222 enum IrqType {
ganlikun 0:06036f8bee2d 223 RxIrq = 0,
ganlikun 0:06036f8bee2d 224 TxIrq,
ganlikun 0:06036f8bee2d 225 EwIrq,
ganlikun 0:06036f8bee2d 226 DoIrq,
ganlikun 0:06036f8bee2d 227 WuIrq,
ganlikun 0:06036f8bee2d 228 EpIrq,
ganlikun 0:06036f8bee2d 229 AlIrq,
ganlikun 0:06036f8bee2d 230 BeIrq,
ganlikun 0:06036f8bee2d 231 IdIrq,
ganlikun 0:06036f8bee2d 232
ganlikun 0:06036f8bee2d 233 IrqCnt
ganlikun 0:06036f8bee2d 234 };
ganlikun 0:06036f8bee2d 235
ganlikun 0:06036f8bee2d 236 /** Attach a function to call whenever a CAN frame received interrupt is
ganlikun 0:06036f8bee2d 237 * generated.
ganlikun 0:06036f8bee2d 238 *
ganlikun 0:06036f8bee2d 239 * This function locks the deep sleep while a callback is attached
ganlikun 0:06036f8bee2d 240 *
ganlikun 0:06036f8bee2d 241 * @param func A pointer to a void function, or 0 to set as none
ganlikun 0:06036f8bee2d 242 * @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)
ganlikun 0:06036f8bee2d 243 */
ganlikun 0:06036f8bee2d 244 void attach(Callback<void()> func, IrqType type=RxIrq);
ganlikun 0:06036f8bee2d 245
ganlikun 0:06036f8bee2d 246 /** Attach a member function to call whenever a CAN frame received interrupt
ganlikun 0:06036f8bee2d 247 * is generated.
ganlikun 0:06036f8bee2d 248 *
ganlikun 0:06036f8bee2d 249 * @param obj pointer to the object to call the member function on
ganlikun 0:06036f8bee2d 250 * @param method pointer to the member function to be called
ganlikun 0:06036f8bee2d 251 * @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)
ganlikun 0:06036f8bee2d 252 * @deprecated
ganlikun 0:06036f8bee2d 253 * The attach function does not support cv-qualifiers. Replaced by
ganlikun 0:06036f8bee2d 254 * attach(callback(obj, method), type).
ganlikun 0:06036f8bee2d 255 */
ganlikun 0:06036f8bee2d 256 template<typename T>
ganlikun 0:06036f8bee2d 257 MBED_DEPRECATED_SINCE("mbed-os-5.1",
ganlikun 0:06036f8bee2d 258 "The attach function does not support cv-qualifiers. Replaced by "
ganlikun 0:06036f8bee2d 259 "attach(callback(obj, method), type).")
ganlikun 0:06036f8bee2d 260 void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) {
ganlikun 0:06036f8bee2d 261 // Underlying call thread safe
ganlikun 0:06036f8bee2d 262 attach(callback(obj, method), type);
ganlikun 0:06036f8bee2d 263 }
ganlikun 0:06036f8bee2d 264
ganlikun 0:06036f8bee2d 265 /** Attach a member function to call whenever a CAN frame received interrupt
ganlikun 0:06036f8bee2d 266 * is generated.
ganlikun 0:06036f8bee2d 267 *
ganlikun 0:06036f8bee2d 268 * @param obj pointer to the object to call the member function on
ganlikun 0:06036f8bee2d 269 * @param method pointer to the member function to be called
ganlikun 0:06036f8bee2d 270 * @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)
ganlikun 0:06036f8bee2d 271 * @deprecated
ganlikun 0:06036f8bee2d 272 * The attach function does not support cv-qualifiers. Replaced by
ganlikun 0:06036f8bee2d 273 * attach(callback(obj, method), type).
ganlikun 0:06036f8bee2d 274 */
ganlikun 0:06036f8bee2d 275 template<typename T>
ganlikun 0:06036f8bee2d 276 MBED_DEPRECATED_SINCE("mbed-os-5.1",
ganlikun 0:06036f8bee2d 277 "The attach function does not support cv-qualifiers. Replaced by "
ganlikun 0:06036f8bee2d 278 "attach(callback(obj, method), type).")
ganlikun 0:06036f8bee2d 279 void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) {
ganlikun 0:06036f8bee2d 280 // Underlying call thread safe
ganlikun 0:06036f8bee2d 281 attach(callback(obj, method), type);
ganlikun 0:06036f8bee2d 282 }
ganlikun 0:06036f8bee2d 283
ganlikun 0:06036f8bee2d 284 static void _irq_handler(uint32_t id, CanIrqType type);
ganlikun 0:06036f8bee2d 285
ganlikun 0:06036f8bee2d 286 protected:
ganlikun 0:06036f8bee2d 287 virtual void lock();
ganlikun 0:06036f8bee2d 288 virtual void unlock();
ganlikun 0:06036f8bee2d 289 can_t _can;
ganlikun 0:06036f8bee2d 290 Callback<void()> _irq[IrqCnt];
ganlikun 0:06036f8bee2d 291 PlatformMutex _mutex;
ganlikun 0:06036f8bee2d 292 };
ganlikun 0:06036f8bee2d 293
ganlikun 0:06036f8bee2d 294 } // namespace mbed
ganlikun 0:06036f8bee2d 295
ganlikun 0:06036f8bee2d 296 #endif
ganlikun 0:06036f8bee2d 297
ganlikun 0:06036f8bee2d 298 #endif // MBED_CAN_H