-
-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathArduino_CAN.cpp
104 lines (80 loc) · 2.8 KB
/
Arduino_CAN.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2022 by Alexander Entinger <a.entinger@arduino.cc>
* Arduino_CAN library for Arduino.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either the GNU General Public License version 2
* or the GNU Lesser General Public License version 2.1, both as
* published by the Free Software Foundation.
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "Arduino_CAN.h"
/**************************************************************************************
* NAMESPACE
**************************************************************************************/
namespace arduino
{
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
Arduino_CAN::Arduino_CAN(PinName const can_tx_pin, PinName const can_rx_pin)
: _can(can_rx_pin, can_tx_pin)
{
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
bool Arduino_CAN::begin(CanBitRate const can_bitrate)
{
int const rc = _can.frequency(static_cast<int>(can_bitrate));
return (rc == 1);
}
void Arduino_CAN::end()
{
/* Nothing to do. */
}
int Arduino_CAN::write(CanMsg const & msg)
{
bool const is_standard_id = msg.isStandardId();
mbed::CANMessage const can_msg(
is_standard_id ? msg.getStandardId() : msg.getExtendedId(),
msg.data,
msg.data_length,
CANData,
is_standard_id ? CANStandard : CANExtended);
return _can.write(can_msg);
}
size_t Arduino_CAN::available()
{
mbed::CANMessage can_msg;
bool const msg_read = _can.read(can_msg) > 0;
if (msg_read)
{
bool const is_standard_id = (can_msg.format == CANStandard);
CanMsg const msg(
is_standard_id ? CanStandardId(can_msg.id) : CanExtendedId(can_msg.id),
can_msg.len,
can_msg.data);
_rx_msg_buf.enqueue(msg);
}
return _rx_msg_buf.available();
}
CanMsg Arduino_CAN::read()
{
return _rx_msg_buf.dequeue();
}
/**************************************************************************************
* NAMESPACE
**************************************************************************************/
} /* arduino */
/**************************************************************************************
* OBJECT INSTANTIATION
**************************************************************************************/
#if CAN_HOWMANY > 0
arduino::Arduino_CAN CAN(PIN_CAN0_TX, PIN_CAN0_RX);
#endif
#if CAN_HOWMANY > 1
arduino::Arduino_CAN CAN1(PIN_CAN1_TX, PIN_CAN1_RX);
#endif