2017-05-06 06:12:54 -03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2018-07-18 03:24:11 -03:00
|
|
|
|
2017-05-06 06:12:54 -03:00
|
|
|
#if HAL_WITH_UAVCAN
|
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2018-07-18 01:22:47 -03:00
|
|
|
|
2018-07-22 16:30:01 -03:00
|
|
|
#ifndef AP_CAN_DEBUG
|
|
|
|
#define AP_CAN_DEBUG 0
|
|
|
|
#endif
|
|
|
|
|
2017-05-06 06:12:54 -03:00
|
|
|
class AP_BoardConfig_CAN {
|
|
|
|
public:
|
2018-07-18 03:24:11 -03:00
|
|
|
AP_BoardConfig_CAN();
|
2017-08-28 21:42:33 -03:00
|
|
|
|
|
|
|
/* Do not allow copies */
|
|
|
|
AP_BoardConfig_CAN(const AP_BoardConfig_CAN &other) = delete;
|
|
|
|
AP_BoardConfig_CAN &operator=(const AP_BoardConfig_CAN&) = delete;
|
2017-05-06 06:12:54 -03:00
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
static AP_BoardConfig_CAN* get_singleton() {
|
|
|
|
return _singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Protocol_Type : uint8_t {
|
|
|
|
Protocol_Type_None = 0,
|
|
|
|
Protocol_Type_UAVCAN = 1,
|
|
|
|
};
|
|
|
|
|
2017-05-06 06:12:54 -03:00
|
|
|
void init(void);
|
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
// returns number of active CAN drivers
|
|
|
|
uint8_t get_num_drivers(void) { return _num_drivers; }
|
|
|
|
|
|
|
|
// return debug level for interface i
|
|
|
|
uint8_t get_debug_level(uint8_t i) {
|
2018-07-22 16:30:01 -03:00
|
|
|
#if AP_CAN_DEBUG
|
2018-07-18 03:24:11 -03:00
|
|
|
if (i < MAX_NUMBER_OF_CAN_INTERFACES) {
|
|
|
|
return _interfaces[i]._driver_number_cache ? _interfaces[i]._debug_level : 0;
|
|
|
|
}
|
2018-07-22 16:30:01 -03:00
|
|
|
#endif
|
2018-07-18 03:24:11 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return maximum level of debug of all interfaces
|
|
|
|
uint8_t get_debug_level(void) {
|
|
|
|
uint8_t ret = 0;
|
2018-07-22 16:30:01 -03:00
|
|
|
#if AP_CAN_DEBUG
|
2018-07-18 03:24:11 -03:00
|
|
|
for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_INTERFACES; i++) {
|
|
|
|
uint8_t dbg = get_debug_level(i);
|
|
|
|
ret = (dbg > ret) ? dbg : ret;
|
|
|
|
}
|
2018-07-22 16:30:01 -03:00
|
|
|
#endif
|
2018-07-18 03:24:11 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return maximum level of debug for driver index i
|
|
|
|
uint8_t get_debug_level_driver(uint8_t i) {
|
|
|
|
uint8_t ret = 0;
|
2018-07-22 16:30:01 -03:00
|
|
|
#if AP_CAN_DEBUG
|
2018-07-18 03:24:11 -03:00
|
|
|
for (uint8_t j = 0; j < MAX_NUMBER_OF_CAN_INTERFACES; j++) {
|
|
|
|
if (_interfaces[j]._driver_number_cache == i) {
|
|
|
|
uint8_t dbg = get_debug_level(j);
|
|
|
|
ret = (dbg > ret) ? dbg : ret;
|
|
|
|
}
|
|
|
|
}
|
2018-07-22 16:30:01 -03:00
|
|
|
#endif
|
2018-07-18 03:24:11 -03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return driver for index i
|
|
|
|
AP_HAL::CANProtocol* get_driver(uint8_t i) {
|
|
|
|
if (i < MAX_NUMBER_OF_CAN_DRIVERS) {
|
|
|
|
return _drivers[i]._driver;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return protocol type index i
|
|
|
|
Protocol_Type get_protocol_type(uint8_t i) {
|
|
|
|
if (i < MAX_NUMBER_OF_CAN_DRIVERS) {
|
|
|
|
return _drivers[i]._protocol_type_cache;
|
|
|
|
}
|
|
|
|
return Protocol_Type_None;
|
|
|
|
}
|
|
|
|
|
2017-05-06 06:12:54 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
private:
|
|
|
|
class Interface {
|
2017-05-06 06:12:54 -03:00
|
|
|
friend class AP_BoardConfig_CAN;
|
|
|
|
|
|
|
|
public:
|
2018-07-18 03:24:11 -03:00
|
|
|
Interface() {
|
2017-05-06 06:12:54 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
private:
|
|
|
|
AP_Int8 _driver_number;
|
2018-07-18 03:24:11 -03:00
|
|
|
uint8_t _driver_number_cache;
|
|
|
|
AP_Int32 _bitrate;
|
2018-07-22 16:30:01 -03:00
|
|
|
#if AP_CAN_DEBUG
|
2018-07-18 03:24:11 -03:00
|
|
|
AP_Int8 _debug_level;
|
2018-07-22 16:30:01 -03:00
|
|
|
#endif
|
2017-05-06 06:12:54 -03:00
|
|
|
};
|
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
class Driver {
|
2017-05-06 06:12:54 -03:00
|
|
|
friend class AP_BoardConfig_CAN;
|
|
|
|
|
|
|
|
public:
|
2018-07-18 03:24:11 -03:00
|
|
|
Driver() {
|
2017-05-06 06:12:54 -03:00
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
}
|
2018-07-18 03:24:11 -03:00
|
|
|
|
2017-05-06 06:12:54 -03:00
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
private:
|
2018-07-18 03:24:11 -03:00
|
|
|
AP_Int8 _protocol_type;
|
|
|
|
Protocol_Type _protocol_type_cache;
|
|
|
|
AP_HAL::CANProtocol* _driver;
|
2017-05-06 06:12:54 -03:00
|
|
|
};
|
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
Interface _interfaces[MAX_NUMBER_OF_CAN_INTERFACES];
|
|
|
|
Driver _drivers[MAX_NUMBER_OF_CAN_DRIVERS];
|
|
|
|
uint8_t _num_drivers;
|
2017-06-01 12:19:51 -03:00
|
|
|
|
2018-07-18 03:24:11 -03:00
|
|
|
static AP_BoardConfig_CAN *_singleton;
|
2017-05-06 06:12:54 -03:00
|
|
|
};
|
2018-07-18 03:24:11 -03:00
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
AP_BoardConfig_CAN& can();
|
|
|
|
}
|
2017-05-06 06:12:54 -03:00
|
|
|
#endif
|