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,
|
2018-03-19 13:07:05 -03:00
|
|
|
Protocol_Type_KDECAN = 2,
|
2019-01-15 00:49:40 -04:00
|
|
|
Protocol_Type_ToshibaCAN = 3
|
2018-07-18 03:24:11 -03:00
|
|
|
};
|
|
|
|
|
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
|
2019-02-14 02:07:53 -04:00
|
|
|
uint8_t get_num_drivers(void) const { return _num_drivers; }
|
2018-07-18 03:24:11 -03:00
|
|
|
|
|
|
|
// return debug level for interface i
|
2019-02-14 02:07:53 -04:00
|
|
|
uint8_t get_debug_level(uint8_t i) const {
|
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
|
2019-02-14 02:07:53 -04:00
|
|
|
uint8_t get_debug_level(void) const {
|
2018-07-18 03:24:11 -03:00
|
|
|
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
|
2019-02-14 02:07:53 -04:00
|
|
|
uint8_t get_debug_level_driver(uint8_t i) const {
|
2018-07-18 03:24:11 -03:00
|
|
|
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
|
2019-02-14 02:07:53 -04:00
|
|
|
AP_HAL::CANProtocol* get_driver(uint8_t i) const {
|
2018-07-18 03:24:11 -03:00
|
|
|
if (i < MAX_NUMBER_OF_CAN_DRIVERS) {
|
|
|
|
return _drivers[i]._driver;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return protocol type index i
|
2019-02-14 02:07:53 -04:00
|
|
|
Protocol_Type get_protocol_type(uint8_t i) const {
|
2018-07-18 03:24:11 -03:00
|
|
|
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[];
|
2019-01-28 21:59:30 -04:00
|
|
|
#if !HAL_MINIMIZE_FEATURES
|
2019-01-21 22:12:08 -04:00
|
|
|
int8_t get_slcan_serial() { return _slcan._ser_port; }
|
|
|
|
uint8_t get_slcan_timeout() { return _slcan._timeout; }
|
|
|
|
void reset_slcan_serial() { _slcan._ser_port.set_and_save_ifchanged(-1); }
|
2019-01-28 21:59:30 -04:00
|
|
|
#endif
|
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;
|
2018-03-19 13:07:05 -03:00
|
|
|
AP_HAL::CANProtocol* _uavcan;
|
|
|
|
AP_HAL::CANProtocol* _kdecan;
|
2019-01-15 00:49:40 -04:00
|
|
|
AP_HAL::CANProtocol* _tcan;
|
2017-05-06 06:12:54 -03:00
|
|
|
};
|
|
|
|
|
2019-01-28 21:59:30 -04:00
|
|
|
#if !HAL_MINIMIZE_FEATURES
|
2019-01-21 22:12:08 -04:00
|
|
|
class SLCAN_Interface {
|
|
|
|
friend class AP_BoardConfig_CAN;
|
|
|
|
|
|
|
|
public:
|
|
|
|
SLCAN_Interface() {
|
|
|
|
AP_Param::setup_object_defaults(this, var_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
|
|
|
private:
|
|
|
|
AP_Int8 _can_port;
|
|
|
|
AP_Int8 _ser_port;
|
|
|
|
AP_Int16 _timeout;
|
|
|
|
};
|
2019-01-28 21:59:30 -04:00
|
|
|
SLCAN_Interface _slcan;
|
|
|
|
#endif
|
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;
|
|
|
|
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
|