|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 by Alexander Entinger <[email protected]> |
| 3 | + * CAN library for Arduino. |
| 4 | + * |
| 5 | + * This file is free software; you can redistribute it and/or modify |
| 6 | + * it under the terms of either the GNU General Public License version 2 |
| 7 | + * or the GNU Lesser General Public License version 2.1, both as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef ARDUINO_CORE_MBED_CAN_H_ |
| 12 | +#define ARDUINO_CORE_MBED_CAN_H_ |
| 13 | + |
| 14 | +/************************************************************************************** |
| 15 | + * INCLUDE |
| 16 | + **************************************************************************************/ |
| 17 | + |
| 18 | +#include <Arduino.h> |
| 19 | +#include <mbed.h> |
| 20 | + |
| 21 | +#include "api/HardwareCAN.h" |
| 22 | + |
| 23 | +/************************************************************************************** |
| 24 | + * COMPILE TIME CHECKS |
| 25 | + **************************************************************************************/ |
| 26 | + |
| 27 | +#if !(defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_GIGA)) |
| 28 | +# error "CAN only available on Arduino Portenta H7 and Arduino Giga (of all ArduinoCore-mbed enabled boards)." |
| 29 | +#endif |
| 30 | + |
| 31 | +/************************************************************************************** |
| 32 | + * TYPEDEF |
| 33 | + **************************************************************************************/ |
| 34 | + |
| 35 | +typedef arduino::CanMsg CanMsg; |
| 36 | + |
| 37 | +/************************************************************************************** |
| 38 | + * NAMESPACE |
| 39 | + **************************************************************************************/ |
| 40 | + |
| 41 | +namespace arduino |
| 42 | +{ |
| 43 | + |
| 44 | +/************************************************************************************** |
| 45 | + * CLASS DECLARATION |
| 46 | + **************************************************************************************/ |
| 47 | + |
| 48 | +class Arduino_CAN final : public HardwareCAN |
| 49 | +{ |
| 50 | +public: |
| 51 | + Arduino_CAN(PinName const can_tx_pin, PinName const can_rx_pin); |
| 52 | + virtual ~Arduino_CAN() { } |
| 53 | + |
| 54 | + |
| 55 | + bool begin(CanBitRate const can_bitrate) override; |
| 56 | + void end() override; |
| 57 | + |
| 58 | + |
| 59 | + int write(CanMsg const & msg) override; |
| 60 | + size_t available() override; |
| 61 | + CanMsg read() override; |
| 62 | + |
| 63 | +private: |
| 64 | + mbed::CAN _can; |
| 65 | + CanMsgRingbuffer _rx_msg_buf; |
| 66 | +}; |
| 67 | + |
| 68 | +/************************************************************************************** |
| 69 | + * NAMESPACE |
| 70 | + **************************************************************************************/ |
| 71 | + |
| 72 | +} /* arduino */ |
| 73 | + |
| 74 | +/************************************************************************************** |
| 75 | + * EXTERN DECLARATION |
| 76 | + **************************************************************************************/ |
| 77 | + |
| 78 | +#if CAN_HOWMANY > 0 |
| 79 | +extern arduino::Arduino_CAN CAN; |
| 80 | +#endif |
| 81 | + |
| 82 | +#if CAN_HOWMANY > 1 |
| 83 | +extern arduino::Arduino_CAN CAN1; |
| 84 | +#endif |
| 85 | + |
| 86 | +#endif /* ARDUINO_CORE_MBED_CAN_H_ */ |
0 commit comments