|
| 1 | +/* |
| 2 | + * Copyright (C) 2021, STMicroelectronics, All Rights Reserved |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | + * not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#ifndef RAWCAN_H |
| 19 | +#define RAWCAN_H |
| 20 | + |
| 21 | +#include "platform/platform.h" |
| 22 | +#include "drivers/CAN.h" |
| 23 | + |
| 24 | +#if DEVICE_CAN || defined(DOXYGEN_ONLY) |
| 25 | + |
| 26 | +#include "interfaces/InterfaceCAN.h" |
| 27 | +#include "hal/can_api.h" |
| 28 | +#include "platform/Callback.h" |
| 29 | +#include "platform/PlatformMutex.h" |
| 30 | + |
| 31 | +namespace mbed { |
| 32 | +#ifndef FEATURE_EXPERIMENTAL_API |
| 33 | +class RawCAN: public CAN { |
| 34 | +public: |
| 35 | + /** Creates an unlocked CAN interface connected to specific pins. |
| 36 | + * |
| 37 | + * @param rd read from transmitter |
| 38 | + * @param td transmit to transmitter |
| 39 | + * |
| 40 | + * Example: |
| 41 | + * @code |
| 42 | + * #include "mbed.h" |
| 43 | + * |
| 44 | + * |
| 45 | + * Ticker ticker; |
| 46 | + * DigitalOut led1(LED1); |
| 47 | + * DigitalOut led2(LED2); |
| 48 | + * //The constructor takes in RX, and TX pin respectively. |
| 49 | + * //These pins, for this example, are defined in mbed_app.json |
| 50 | + * RawCAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD); |
| 51 | + * RawCAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD); |
| 52 | + * |
| 53 | + * unsigned char counter = 0; |
| 54 | + * |
| 55 | + * void send() { |
| 56 | + * if(can1.write(CANMessage(1337U, &counter, 1))) { |
| 57 | + * printf("Message sent: %d\n", counter); |
| 58 | + * counter++; |
| 59 | + * } |
| 60 | + * led1 = !led1; |
| 61 | + * } |
| 62 | + * |
| 63 | + * int main() { |
| 64 | + * ticker.attach(&send, 1); |
| 65 | + * CANMessage msg; |
| 66 | + * while(1) { |
| 67 | + * if(can2.read(msg)) { |
| 68 | + * printf("Message received: %d\n\n", msg.data[0]); |
| 69 | + * led2 = !led2; |
| 70 | + * } |
| 71 | + * ThisThread::sleep_for(200); |
| 72 | + * } |
| 73 | + * } |
| 74 | + * |
| 75 | + * @endcode |
| 76 | + */ |
| 77 | + |
| 78 | + /* Note: The can apis are unlocked hence using this when multiple |
| 79 | + * threads are accessing a single instance of CAN will lead to |
| 80 | + * race conditions, can be used in single threaded CAN. |
| 81 | + */ |
| 82 | + using CAN::CAN; |
| 83 | + |
| 84 | + |
| 85 | + // override lock apis to create unlocked CAN |
| 86 | + void lock() override {}; |
| 87 | + void unlock() override {}; |
| 88 | + |
| 89 | +}; |
| 90 | +#endif //FEATURE_EXPERIMENTAL_API |
| 91 | +} |
| 92 | + |
| 93 | +#endif |
| 94 | +#endif //RAWCAN_H |
0 commit comments