Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Ardupilot
GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_CANManager/AP_CANManager.h
9539 views
1
/*
2
* This file is free software: you can redistribute it and/or modify it
3
* under the terms of the GNU General Public License as published by the
4
* Free Software Foundation, either version 3 of the License, or
5
* (at your option) any later version.
6
*
7
* This file is distributed in the hope that it will be useful, but
8
* WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
* See the GNU General Public License for more details.
11
*
12
* You should have received a copy of the GNU General Public License along
13
* with this program. If not, see <http://www.gnu.org/licenses/>.
14
*
15
* Code by Siddharth Bharat Purohit
16
*/
17
18
#pragma once
19
20
#include "AP_CANManager_config.h"
21
22
#if HAL_CANMANAGER_ENABLED
23
24
#include <AP_HAL/AP_HAL.h>
25
26
#include <AP_Param/AP_Param.h>
27
#include "AP_SLCANIface.h"
28
#include "AP_CANDriver.h"
29
30
#include "AP_CAN.h"
31
32
class CANSensor;
33
34
class AP_CANManager
35
{
36
public:
37
AP_CANManager();
38
39
/* Do not allow copies */
40
CLASS_NO_COPY(AP_CANManager);
41
42
static AP_CANManager* get_singleton()
43
{
44
if (_singleton == nullptr) {
45
AP_HAL::panic("CANManager used before allocation.");
46
}
47
return _singleton;
48
}
49
50
enum LogLevel : uint8_t {
51
LOG_NONE,
52
LOG_ERROR,
53
LOG_WARNING,
54
LOG_INFO,
55
LOG_DEBUG,
56
};
57
58
__INITFUNC__ void init(void);
59
60
// register a new driver
61
bool register_driver(AP_CAN::Protocol dtype, AP_CANDriver *driver);
62
63
// register a new auxillary sensor driver for 11 bit address frames
64
bool register_11bit_driver(AP_CAN::Protocol dtype, CANSensor *sensor, uint8_t &driver_index);
65
66
// returns number of active CAN Drivers
67
uint8_t get_num_drivers(void) const
68
{
69
return HAL_MAX_CAN_PROTOCOL_DRIVERS;
70
}
71
72
// return driver for index i
73
AP_CANDriver* get_driver(uint8_t i) const
74
{
75
if (i < ARRAY_SIZE(_drivers)) {
76
return _drivers[i];
77
}
78
return nullptr;
79
}
80
81
// returns current log level
82
LogLevel get_log_level(void) const
83
{
84
return LogLevel(_loglevel.get());
85
}
86
87
// Method to log status and debug information for review while debugging
88
void log_text(AP_CANManager::LogLevel loglevel, const char *tag, const char *fmt, ...) FMT_PRINTF(4,5);
89
90
void log_retrieve(ExpandingString &str) const;
91
92
// return driver type index i
93
AP_CAN::Protocol get_driver_type(uint8_t i) const
94
{
95
if (i < ARRAY_SIZE(_driver_type_cache)) {
96
return _driver_type_cache[i];
97
}
98
return AP_CAN::Protocol::None;
99
}
100
101
static const struct AP_Param::GroupInfo var_info[];
102
103
private:
104
105
// Parameter interface for CANIfaces
106
class CANIface_Params
107
{
108
friend class AP_CANManager;
109
110
public:
111
CANIface_Params()
112
{
113
AP_Param::setup_object_defaults(this, var_info);
114
}
115
116
static const struct AP_Param::GroupInfo var_info[];
117
118
enum class Options : uint32_t {
119
LOG_ALL_FRAMES = (1U<<0),
120
};
121
122
bool option_is_set(Options option) const {
123
return (_options & uint32_t(option)) != 0;
124
}
125
126
private:
127
AP_Int8 _driver_number;
128
AP_Int32 _bitrate;
129
AP_Int32 _fdbitrate;
130
AP_Int32 _options;
131
132
#if AP_CAN_LOGGING_ENABLED && HAL_LOGGING_ENABLED
133
uint8_t logging_id;
134
#endif
135
};
136
137
//Parameter Interface for CANDrivers
138
class CANDriver_Params
139
{
140
friend class AP_CANManager;
141
142
public:
143
CANDriver_Params()
144
{
145
AP_Param::setup_object_defaults(this, var_info);
146
}
147
static const struct AP_Param::GroupInfo var_info[];
148
149
private:
150
AP_Int8 _driver_type;
151
AP_Int8 _driver_type_11bit;
152
AP_CANDriver* _uavcan;
153
AP_CANDriver* _piccolocan;
154
};
155
156
CANIface_Params _interfaces[HAL_NUM_CAN_IFACES];
157
AP_CANDriver* _drivers[HAL_MAX_CAN_PROTOCOL_DRIVERS];
158
CANDriver_Params _drv_param[HAL_MAX_CAN_PROTOCOL_DRIVERS];
159
AP_CAN::Protocol _driver_type_cache[HAL_MAX_CAN_PROTOCOL_DRIVERS];
160
161
AP_Int8 _loglevel;
162
uint8_t _num_drivers;
163
#if AP_CAN_SLCAN_ENABLED
164
SLCAN::CANIface _slcan_interface;
165
#endif
166
167
static AP_CANManager *_singleton;
168
169
char* _log_buf;
170
uint32_t _log_pos;
171
172
HAL_Semaphore _sem;
173
174
#if AP_CAN_LOGGING_ENABLED && HAL_LOGGING_ENABLED
175
/*
176
handler for CAN frames for logging
177
*/
178
void can_logging_callback(uint8_t bus, const AP_HAL::CANFrame &frame, AP_HAL::CANIface::CanIOFlags flags);
179
void check_logging_enable(void);
180
#endif
181
};
182
183
namespace AP
184
{
185
AP_CANManager& can();
186
}
187
188
#endif // HAL_CANMANAGER_ENABLED
189
190