APM:Libraries
AP_BoardConfig_CAN.cpp
Go to the documentation of this file.
1  /*
2  This program is free software: you can redistribute it and/or modify
3  it under the terms of the GNU General Public License as published by
4  the Free Software Foundation, either version 3 of the License, or
5  (at your option) any later version.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program. If not, see <http://www.gnu.org/licenses/>.
14  */
15 /*
16  * AP_BoardConfig_CAN - board specific configuration for CAN interface
17  */
18 
19 #include <AP_HAL/AP_HAL.h>
20 #include <AP_Common/AP_Common.h>
21 #include "AP_BoardConfig_CAN.h"
22 
23 #if HAL_WITH_UAVCAN
24 
25 #if CONFIG_HAL_BOARD == HAL_BOARD_PX4 || CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 
31 #include <AP_HAL_PX4/CAN.h>
32 #elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
33 #include <AP_HAL_Linux/CAN.h>
34 #elif CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
35 #include <AP_HAL_ChibiOS/CAN.h>
36 #endif
37 
38 #include <AP_UAVCAN/AP_UAVCAN.h>
39 
40 extern const AP_HAL::HAL& hal;
41 
42 // table of user settable parameters
43 const AP_Param::GroupInfo AP_BoardConfig_CAN::var_info[] = {
44 
45 #if MAX_NUMBER_OF_CAN_INTERFACES > 0
46  // @Group: P1_
47  // @Path: ../AP_BoardConfig/canbus.cpp
48  AP_SUBGROUPINFO(_var_info_can[0], "P1_", 1, AP_BoardConfig_CAN, AP_BoardConfig_CAN::CAN_if_var_info),
49 #endif
50 
51 #if MAX_NUMBER_OF_CAN_INTERFACES > 1
52  // @Group: P2_
53  // @Path: ../AP_BoardConfig/canbus.cpp
54  AP_SUBGROUPINFO(_var_info_can[1], "P2_", 2, AP_BoardConfig_CAN, AP_BoardConfig_CAN::CAN_if_var_info),
55 #endif
56 
57 #if MAX_NUMBER_OF_CAN_INTERFACES > 2
58  // @Group: P3_
59  // @Path: ../AP_BoardConfig/canbus.cpp
60  AP_SUBGROUPINFO(_var_info_can[2], "P3_", 3, AP_BoardConfig_CAN, AP_BoardConfig_CAN::CAN_if_var_info),
61 #endif
62 
63 #if MAX_NUMBER_OF_CAN_DRIVERS > 0
64  // @Group: D1_
65  // @Path: ../AP_BoardConfig/canbus_driver.cpp
66  AP_SUBGROUPINFO(_var_info_can_protocol[0], "D1_", 4, AP_BoardConfig_CAN, AP_BoardConfig_CAN::CAN_driver_var_info),
67 #endif
68 
69 #if MAX_NUMBER_OF_CAN_DRIVERS > 1
70  // @Group: D2_
71  // @Path: ../AP_BoardConfig/canbus_driver.cpp
72  AP_SUBGROUPINFO(_var_info_can_protocol[1], "D2_", 5, AP_BoardConfig_CAN, AP_BoardConfig_CAN::CAN_driver_var_info),
73 #endif
74 
75 #if MAX_NUMBER_OF_CAN_DRIVERS > 2
76  // @Group: D3_
77  // @Path: ../AP_BoardConfig/canbus_driver.cpp
78  AP_SUBGROUPINFO(_var_info_can_protocol[2], "D3_", 6, AP_BoardConfig_CAN, AP_BoardConfig_CAN::CAN_driver_var_info),
79 #endif
80 
82 };
83 
84 int8_t AP_BoardConfig_CAN::_st_driver_number[MAX_NUMBER_OF_CAN_INTERFACES];
85 int8_t AP_BoardConfig_CAN::_st_can_debug[MAX_NUMBER_OF_CAN_INTERFACES];
86 
88 {
89  for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_INTERFACES; i++)
90  {
91  _st_driver_number[i] = (int8_t) _var_info_can[i]._driver_number;
92  _st_can_debug[i] = (int8_t) _var_info_can[i]._can_debug;
93  }
94 
95  setup_canbus();
96 }
97 
98 void AP_BoardConfig_CAN::setup_canbus(void)
99 {
100  // Create all drivers that we need
101  bool initret = true;
102  for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_INTERFACES; i++) {
103  uint8_t drv_num = _var_info_can[i]._driver_number;
104 
105  if (drv_num != 0 && drv_num <= MAX_NUMBER_OF_CAN_DRIVERS) {
106  if (hal.can_mgr[drv_num - 1] == nullptr) {
107 
108  #if CONFIG_HAL_BOARD == HAL_BOARD_PX4 || CONFIG_HAL_BOARD == HAL_BOARD_VRBRAIN
109  const_cast <AP_HAL::HAL&> (hal).can_mgr[drv_num - 1] = new PX4::PX4CANManager;
110  #elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
111  const_cast <AP_HAL::HAL&> (hal).can_mgr[drv_num - 1] = new Linux::CANManager;
112  #elif CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
113  const_cast <AP_HAL::HAL&> (hal).can_mgr[drv_num - 1] = new ChibiOS::CANManager;
114  #endif
115 
116  }
117 
118  if (hal.can_mgr[drv_num - 1] != nullptr) {
119  initret &= hal.can_mgr[drv_num - 1]->begin(_var_info_can[i]._can_bitrate, i);
120  } else {
121  printf("Failed to initialize can interface %d\n\r", i + 1);
122  }
123  }
124  }
125 
126  bool any_uavcan_present = false;
127 
128  if (initret) {
129  for (uint8_t i = 0; i < MAX_NUMBER_OF_CAN_DRIVERS; i++) {
130  if (hal.can_mgr[i] != nullptr) {
131  hal.can_mgr[i]->initialized(true);
132  printf("can_mgr %d initialized well\n\r", i + 1);
133 
134  if (_var_info_can_protocol[i]._protocol == UAVCAN_PROTOCOL_ENABLE) {
135  _var_info_can_protocol[i]._uavcan = new AP_UAVCAN;
136 
137  if (_var_info_can_protocol[i]._uavcan != nullptr)
138  {
139  AP_Param::load_object_from_eeprom(_var_info_can_protocol[i]._uavcan, AP_UAVCAN::var_info);
140 
141  hal.can_mgr[i]->set_UAVCAN(_var_info_can_protocol[i]._uavcan);
142  _var_info_can_protocol[i]._uavcan->set_parent_can_mgr(hal.can_mgr[i]);
143 
144  if (_var_info_can_protocol[i]._uavcan->try_init() == true) {
145  any_uavcan_present = true;
146  } else {
147  printf("Failed to initialize uavcan interface %d\n\r", i + 1);
148  }
149 
150  } else {
151  AP_HAL::panic("Failed to allocate uavcan %d\n\r", i + 1);
152  }
153  }
154  }
155  }
156 
157  if (any_uavcan_present) {
158  // start UAVCAN working thread
160 
161  // Delay for magnetometer and barometer discovery
162  hal.scheduler->delay(5000);
163  }
164  }
165 }
166 #endif
167 
int printf(const char *fmt,...)
Definition: stdio.c:113
static void load_object_from_eeprom(const void *object_pointer, const struct GroupInfo *group_info)
Definition: AP_Param.cpp:1335
const AP_HAL::HAL & hal
Definition: UARTDriver.cpp:37
AP_HAL::CANManager ** can_mgr
Definition: HAL.h:120
virtual void delay(uint16_t ms)=0
virtual void create_uavcan_thread()
Definition: Scheduler.h:70
static const struct AP_Param::GroupInfo var_info[]
Definition: AP_UAVCAN.h:55
Common definitions and utility routines for the ArduPilot libraries.
void init()
Generic board initialization function.
Definition: system.cpp:136
void panic(const char *errormsg,...) FMT_PRINTF(1
Definition: system.cpp:140
#define AP_SUBGROUPINFO(element, name, idx, thisclazz, elclazz)
Definition: AP_Param.h:109
#define AP_GROUPEND
Definition: AP_Param.h:121
AP_HAL::Scheduler * scheduler
Definition: HAL.h:114