Mbed OS Reference
Loading...
Searching...
No Matches
RawCAN.h
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 "rtos/Mutex.h"
30
31namespace mbed {
32#ifndef FEATURE_EXPERIMENTAL_API
33
34/** Creates an unlocked CAN interface connected to specific pins.
35 *
36 * Example:
37 * @code
38 * #include "mbed.h"
39 *
40 *
41 * Ticker ticker;
42 * DigitalOut led1(LED1);
43 * DigitalOut led2(LED2);
44 * //The constructor takes in RX, and TX pin respectively.
45 * //These pins, for this example, are defined in mbed_app.json
46 * RawCAN can1(MBED_CONF_APP_CAN1_RD, MBED_CONF_APP_CAN1_TD);
47 * RawCAN can2(MBED_CONF_APP_CAN2_RD, MBED_CONF_APP_CAN2_TD);
48 *
49 * unsigned char counter = 0;
50 *
51 * void send() {
52 * if(can1.write(CANMessage(1337U, &counter, 1))) {
53 * printf("Message sent: %d\n", counter);
54 * counter++;
55 * }
56 * led1 = !led1;
57 * }
58 *
59 * int main() {
60 * ticker.attach(&send, 1);
61 * CANMessage msg;
62 * while(1) {
63 * if(can2.read(msg)) {
64 * printf("Message received: %d\n\n", msg.data[0]);
65 * led2 = !led2;
66 * }
67 * ThisThread::sleep_for(200);
68 * }
69 * }
70 *
71 * @endcode
72 */
73class RawCAN: public CAN {
74public:
75
76 /* Note: The can apis are unlocked hence using this when multiple
77 * threads are accessing a single instance of CAN will lead to
78 * race conditions, can be used in single threaded CAN.
79 */
80 using CAN::CAN;
81
82
83 // override lock apis to create unlocked CAN
84 void lock() override {};
85 void unlock() override {};
86
87};
88#endif //FEATURE_EXPERIMENTAL_API
89}
90
91#endif
92#endif //RAWCAN_H
A can bus client, used for communicating with can devices.
Definition: CAN.h:45
CAN(PinName rd, PinName td)
Creates a CAN interface connected to specific pins.
Creates an unlocked CAN interface connected to specific pins.
Definition: RawCAN.h:73