CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_BoardConfig/board_drivers.cpp
Views: 1798
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 - driver loading and setup
17
*/
18
19
20
#include <AP_HAL/AP_HAL.h>
21
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
22
#include <hal.h>
23
#endif
24
#include "AP_BoardConfig.h"
25
#include <GCS_MAVLink/GCS.h>
26
#include <AP_Math/crc.h>
27
#include <stdio.h>
28
29
extern const AP_HAL::HAL& hal;
30
31
/*
32
init safety state
33
*/
34
void AP_BoardConfig::board_init_safety()
35
{
36
bool force_safety_off = (state.safety_enable.get() == 0);
37
if (!force_safety_off && hal.util->was_watchdog_safety_off()) {
38
GCS_SEND_TEXT(MAV_SEVERITY_INFO, "Forcing safety off for watchdog\n");
39
force_safety_off = true;
40
}
41
if (force_safety_off) {
42
hal.rcout->force_safety_off();
43
// wait until safety has been turned off
44
uint8_t count = 20;
45
while (hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_ARMED && count--) {
46
hal.scheduler->delay(20);
47
}
48
}
49
}
50
51
/*
52
init debug pins. We set debug pins as input if BRD_OPTIONS bit for debug enable is not set
53
this prevents possible ESD issues on the debug pins
54
*/
55
void AP_BoardConfig::board_init_debug()
56
{
57
#if !defined(HAL_BUILD_AP_PERIPH) && !defined(HAL_DEBUG_BUILD)
58
if ((_options & BOARD_OPTION_DEBUG_ENABLE) == 0) {
59
#ifdef HAL_GPIO_PIN_JTCK_SWCLK
60
palSetLineMode(HAL_GPIO_PIN_JTCK_SWCLK, PAL_MODE_INPUT);
61
#endif
62
#ifdef HAL_GPIO_PIN_JTMS_SWDIO
63
palSetLineMode(HAL_GPIO_PIN_JTMS_SWDIO, PAL_MODE_INPUT);
64
#endif
65
}
66
#endif // HAL_BUILD_AP_PERIPH && HAL_DEBUG_BUILD
67
}
68
69
70
#if AP_FEATURE_BOARD_DETECT
71
72
AP_BoardConfig::px4_board_type AP_BoardConfig::px4_configured_board;
73
74
void AP_BoardConfig::board_setup_drivers(void)
75
{
76
if (state.board_type == PX4_BOARD_OLDDRIVERS) {
77
printf("Old drivers no longer supported\n");
78
state.board_type.set(PX4_BOARD_AUTO);
79
}
80
81
// run board auto-detection
82
board_autodetect();
83
84
px4_configured_board = (enum px4_board_type)state.board_type.get();
85
86
switch (px4_configured_board) {
87
case PX4_BOARD_PX4V1:
88
case PX4_BOARD_PIXHAWK:
89
case PX4_BOARD_PIXHAWK2:
90
case PX4_BOARD_FMUV5:
91
case PX4_BOARD_FMUV6:
92
case PX4_BOARD_SP01:
93
case PX4_BOARD_PIXRACER:
94
case PX4_BOARD_PHMINI:
95
case PX4_BOARD_AUAV21:
96
case PX4_BOARD_PH2SLIM:
97
case VRX_BOARD_BRAIN51:
98
case VRX_BOARD_BRAIN52:
99
case VRX_BOARD_BRAIN52E:
100
case VRX_BOARD_UBRAIN51:
101
case VRX_BOARD_UBRAIN52:
102
case VRX_BOARD_CORE10:
103
case VRX_BOARD_BRAIN54:
104
case PX4_BOARD_AEROFC:
105
case PX4_BOARD_PIXHAWK_PRO:
106
case PX4_BOARD_PCNC1:
107
case PX4_BOARD_MINDPXV2:
108
case FMUV6_BOARD_HOLYBRO_6X:
109
case FMUV6_BOARD_HOLYBRO_6X_REV6:
110
case FMUV6_BOARD_HOLYBRO_6X_45686:
111
case FMUV6_BOARD_CUAV_6X:
112
break;
113
default:
114
config_error("Unknown board type");
115
break;
116
}
117
}
118
119
#define SPI_PROBE_DEBUG 0
120
121
/*
122
check a SPI device for a register value
123
*/
124
bool AP_BoardConfig::spi_check_register(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag)
125
{
126
auto dev = hal.spi->get_device(devname);
127
if (!dev) {
128
#if SPI_PROBE_DEBUG
129
hal.console->printf("%s: no device\n", devname);
130
#endif
131
return false;
132
}
133
dev->set_read_flag(read_flag);
134
WITH_SEMAPHORE(dev->get_semaphore());
135
dev->set_speed(AP_HAL::Device::SPEED_LOW);
136
uint8_t v;
137
if (!dev->read_registers(regnum, &v, 1)) {
138
#if SPI_PROBE_DEBUG
139
hal.console->printf("%s: reg %02x read fail\n", devname, (unsigned)regnum);
140
#endif
141
return false;
142
}
143
#if SPI_PROBE_DEBUG
144
hal.console->printf("%s: reg %02x expected:%02x got:%02x\n", devname, (unsigned)regnum, (unsigned)value, (unsigned)v);
145
#endif
146
return v == value;
147
}
148
149
150
#define INV2REG_BANK_SEL 0x7F
151
/*
152
check a SPI device for a register value
153
*/
154
bool AP_BoardConfig::spi_check_register_inv2(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag)
155
{
156
auto dev = hal.spi->get_device(devname);
157
if (!dev) {
158
#if SPI_PROBE_DEBUG
159
hal.console->printf("%s: no device\n", devname);
160
#endif
161
return false;
162
}
163
dev->set_read_flag(read_flag);
164
WITH_SEMAPHORE(dev->get_semaphore());
165
dev->set_speed(AP_HAL::Device::SPEED_LOW);
166
uint8_t v;
167
// select bank 0 for who am i
168
dev->write_register(INV2REG_BANK_SEL, 0, false);
169
if (!dev->read_registers(regnum, &v, 1)) {
170
#if SPI_PROBE_DEBUG
171
hal.console->printf("%s: reg %02x read fail\n", devname, (unsigned)regnum);
172
#endif
173
return false;
174
}
175
#if SPI_PROBE_DEBUG
176
hal.console->printf("%s: reg %02x expected:%02x got:%02x\n", devname, (unsigned)regnum, (unsigned)value, (unsigned)v);
177
#endif
178
return v == value;
179
}
180
181
#if defined(HAL_VALIDATE_BOARD)
182
bool AP_BoardConfig::check_ms5611(const char* devname) {
183
auto dev = hal.spi->get_device(devname);
184
if (!dev) {
185
#if SPI_PROBE_DEBUG
186
hal.console->printf("%s: no device\n", devname);
187
#endif
188
return false;
189
}
190
191
AP_HAL::Semaphore *dev_sem = dev->get_semaphore();
192
193
if (!dev_sem) {
194
return false;
195
}
196
WITH_SEMAPHORE(dev_sem);
197
198
static const uint8_t CMD_MS56XX_RESET = 0x1E;
199
static const uint8_t CMD_MS56XX_PROM = 0xA0;
200
201
dev->transfer(&CMD_MS56XX_RESET, 1, nullptr, 0);
202
hal.scheduler->delay(4);
203
204
uint16_t prom[8];
205
bool all_zero = true;
206
for (uint8_t i = 0; i < 8; i++) {
207
const uint8_t reg = CMD_MS56XX_PROM + (i << 1);
208
uint8_t val[2];
209
if (!dev->transfer(&reg, 1, val, sizeof(val))) {
210
#if SPI_PROBE_DEBUG
211
hal.console->printf("%s: transfer fail\n", devname);
212
#endif
213
return false;
214
}
215
prom[i] = (val[0] << 8) | val[1];
216
217
if (prom[i] != 0) {
218
all_zero = false;
219
}
220
}
221
222
uint16_t crc_read = prom[7]&0xf;
223
prom[7] &= 0xff00;
224
225
if (crc_read != crc_crc4(prom) || all_zero) {
226
#if SPI_PROBE_DEBUG
227
hal.console->printf("%s: crc fail\n", devname);
228
#endif
229
return false;
230
}
231
232
#if SPI_PROBE_DEBUG
233
hal.console->printf("%s: found successfully\n", devname);
234
#endif
235
236
return true;
237
}
238
#endif // HAL_VALIDATE_BOARD
239
240
#define MPUREG_WHOAMI 0x75
241
#define MPU_WHOAMI_MPU60X0 0x68
242
#define MPU_WHOAMI_MPU9250 0x71
243
#define MPU_WHOAMI_ICM20608 0xaf
244
#define MPU_WHOAMI_ICM20602 0x12
245
246
#define LSMREG_WHOAMI 0x0f
247
#define LSM_WHOAMI_LSM303D 0x49
248
#define LSM_WHOAMI_L3GD20 0xd4
249
250
#define INV2REG_WHOAMI 0x00
251
252
#define INV2_WHOAMI_ICM20948 0xEA
253
#define INV2_WHOAMI_ICM20649 0xE1
254
255
#define INV3REG_WHOAMI 0x75
256
#define INV3REG_456_WHOAMI 0x72
257
258
#define INV3_WHOAMI_ICM42688 0x47
259
#define INV3_WHOAMI_ICM42670 0x67
260
#define INV3_WHOAMI_ICM45686 0xE9
261
#define INV3_WHOAMI_IIM42652 0x6f
262
263
/*
264
validation of the board type
265
*/
266
void AP_BoardConfig::validate_board_type(void)
267
{
268
/* some boards can be damaged by the user setting the wrong board
269
type. The key one is the cube which has a heater which can
270
cook the IMUs if the user uses an old paramater file. We
271
override the board type for that specific case
272
*/
273
#if defined(HAL_CHIBIOS_ARCH_FMUV3)
274
if (state.board_type == PX4_BOARD_PIXHAWK &&
275
(spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
276
spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) ||
277
spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
278
spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||
279
spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&
280
(spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) ||
281
spi_check_register_inv2("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948))) {
282
// Pixhawk2 has LSM303D and MPUxxxx on external bus. If we
283
// detect those, then force PIXHAWK2, even if the user has
284
// configured for PIXHAWK1
285
#if !defined(HAL_CHIBIOS_ARCH_FMUV3)
286
// force user to load the right firmware
287
config_error("Pixhawk2 requires FMUv3 firmware");
288
#endif
289
state.board_type.set(PX4_BOARD_PIXHAWK2);
290
DEV_PRINTF("Forced PIXHAWK2\n");
291
}
292
#endif
293
}
294
295
/*
296
auto-detect board type
297
*/
298
void AP_BoardConfig::board_autodetect(void)
299
{
300
#if defined(HAL_VALIDATE_BOARD)
301
if((_options & SKIP_BOARD_VALIDATION) == 0) {
302
const char* errored_check = HAL_VALIDATE_BOARD;
303
if (errored_check == nullptr) {
304
return;
305
} else {
306
config_error("Board Validation %s Failed", errored_check);
307
return;
308
}
309
}
310
#endif
311
312
if (state.board_type != PX4_BOARD_AUTO) {
313
validate_board_type();
314
// user has chosen a board type
315
return;
316
}
317
318
#if defined(HAL_CHIBIOS_ARCH_FMUV3)
319
if ((spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
320
spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
321
spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
322
spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) ||
323
spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
324
spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||
325
spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&
326
(spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) ||
327
spi_check_register_inv2("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948))) {
328
// Pixhawk2 has LSM303D and MPUxxxx on external bus
329
state.board_type.set(PX4_BOARD_PIXHAWK2);
330
DEV_PRINTF("Detected PIXHAWK2\n");
331
} else if ((spi_check_register("icm20608-am", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
332
spi_check_register("icm20608-am", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&
333
spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250)) {
334
// PHMINI has an ICM20608 and MPU9250 on sensor bus
335
state.board_type.set(PX4_BOARD_PHMINI);
336
DEV_PRINTF("Detected PixhawkMini\n");
337
} else if (spi_check_register("lsm9ds0_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) &&
338
(spi_check_register("mpu6000", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
339
spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
340
spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||
341
spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250))) {
342
343
// classic or upgraded Pixhawk1
344
state.board_type.set(PX4_BOARD_PIXHAWK);
345
DEV_PRINTF("Detected Pixhawk\n");
346
} else {
347
config_error("Unable to detect board type");
348
}
349
#elif defined(HAL_CHIBIOS_ARCH_FMUV4)
350
// only one choice
351
state.board_type.set_and_notify(PX4_BOARD_PIXRACER);
352
DEV_PRINTF("Detected Pixracer\n");
353
#elif defined(HAL_CHIBIOS_ARCH_MINDPXV2)
354
// only one choice
355
state.board_type.set_and_notify(PX4_BOARD_MINDPXV2);
356
DEV_PRINTF("Detected MindPX-V2\n");
357
#elif defined(HAL_CHIBIOS_ARCH_FMUV4PRO)
358
// only one choice
359
state.board_type.set_and_notify(PX4_BOARD_PIXHAWK_PRO);
360
DEV_PRINTF("Detected Pixhawk Pro\n");
361
#elif defined(HAL_CHIBIOS_ARCH_FMUV5)
362
state.board_type.set_and_notify(PX4_BOARD_FMUV5);
363
DEV_PRINTF("Detected FMUv5\n");
364
#elif defined(HAL_CHIBIOS_ARCH_FMUV6)
365
detect_fmuv6_variant();
366
#elif defined(HAL_CHIBIOS_ARCH_BRAINV51)
367
state.board_type.set_and_notify(VRX_BOARD_BRAIN51);
368
DEV_PRINTF("Detected VR Brain 5.1\n");
369
#elif defined(HAL_CHIBIOS_ARCH_BRAINV52)
370
state.board_type.set_and_notify(VRX_BOARD_BRAIN52);
371
DEV_PRINTF("Detected VR Brain 5.2\n");
372
#elif defined(HAL_CHIBIOS_ARCH_UBRAINV51)
373
state.board_type.set_and_notify(VRX_BOARD_UBRAIN51);
374
DEV_PRINTF("Detected VR Micro Brain 5.1\n");
375
#elif defined(HAL_CHIBIOS_ARCH_COREV10)
376
state.board_type.set_and_notify(VRX_BOARD_CORE10);
377
DEV_PRINTF("Detected VR Core 1.0\n");
378
#elif defined(HAL_CHIBIOS_ARCH_BRAINV54)
379
state.board_type.set_and_notify(VRX_BOARD_BRAIN54);
380
DEV_PRINTF("Detected VR Brain 5.4\n");
381
#endif
382
383
}
384
385
#endif // AP_FEATURE_BOARD_DETECT
386
387
/*
388
setup flow control on UARTs
389
*/
390
void AP_BoardConfig::board_setup_uart()
391
{
392
#if AP_FEATURE_RTSCTS
393
#ifdef HAL_HAVE_RTSCTS_SERIAL1
394
if (hal.serial(1) != nullptr) {
395
hal.serial(1)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[1].get());
396
}
397
#endif
398
#ifdef HAL_HAVE_RTSCTS_SERIAL2
399
if (hal.serial(2) != nullptr) {
400
hal.serial(2)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[2].get());
401
}
402
#endif
403
#ifdef HAL_HAVE_RTSCTS_SERIAL3
404
if (hal.serial(3) != nullptr) {
405
hal.serial(3)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[3].get());
406
}
407
#endif
408
#ifdef HAL_HAVE_RTSCTS_SERIAL4
409
if (hal.serial(4) != nullptr) {
410
hal.serial(4)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[4].get());
411
}
412
#endif
413
#ifdef HAL_HAVE_RTSCTS_SERIAL5
414
if (hal.serial(5) != nullptr) {
415
hal.serial(5)->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser_rtscts[5].get());
416
}
417
#endif
418
#endif
419
}
420
421
/*
422
setup SBUS
423
*/
424
void AP_BoardConfig::board_setup_sbus(void)
425
{
426
#if AP_FEATURE_SBUS_OUT
427
if (state.sbus_out_rate.get() >= 1) {
428
static const struct {
429
uint8_t value;
430
uint16_t rate;
431
} rates[] = {
432
{ 1, 50 },
433
{ 2, 75 },
434
{ 3, 100 },
435
{ 4, 150 },
436
{ 5, 200 },
437
{ 6, 250 },
438
{ 7, 300 }
439
};
440
uint16_t rate = 300;
441
for (uint8_t i=0; i<ARRAY_SIZE(rates); i++) {
442
if (rates[i].value == state.sbus_out_rate) {
443
rate = rates[i].rate;
444
}
445
}
446
if (!hal.rcout->enable_px4io_sbus_out(rate)) {
447
hal.console->printf("Failed to enable SBUS out\n");
448
}
449
}
450
#endif
451
}
452
453
454
/*
455
setup peripherals and drivers
456
*/
457
void AP_BoardConfig::board_setup()
458
{
459
#if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
460
// init needs to be done after boardconfig is read so parameters are set
461
hal.gpio->init();
462
hal.rcin->init();
463
hal.rcout->init();
464
#endif
465
466
#ifdef HAL_GPIO_PWM_VOLT_PIN
467
if (_pwm_volt_sel == 0) {
468
hal.gpio->write(HAL_GPIO_PWM_VOLT_PIN, HAL_GPIO_PWM_VOLT_3v3); //set pin for 3.3V PWM Output
469
} else if (_pwm_volt_sel == 1) {
470
hal.gpio->write(HAL_GPIO_PWM_VOLT_PIN, !HAL_GPIO_PWM_VOLT_3v3); //set pin for 5V PWM Output
471
}
472
#endif
473
board_setup_uart();
474
board_setup_sbus();
475
#if AP_FEATURE_BOARD_DETECT
476
board_setup_drivers();
477
#endif
478
}
479
480
481
#ifdef HAL_CHIBIOS_ARCH_FMUV6
482
483
#define BMI088REG_CHIPID 0x00
484
#define CHIPID_BMI088_G 0x0F
485
486
/*
487
detect which FMUV6 variant we are running on
488
*/
489
void AP_BoardConfig::detect_fmuv6_variant()
490
{
491
if (((spi_check_register_inv2("icm20649", INV2REG_WHOAMI, INV2_WHOAMI_ICM20649) ||
492
spi_check_register("bmi088_g", BMI088REG_CHIPID, CHIPID_BMI088_G)) && // alternative config
493
spi_check_register("icm42688", INV3REG_WHOAMI, INV3_WHOAMI_ICM42688) &&
494
spi_check_register("icm42670", INV3REG_WHOAMI, INV3_WHOAMI_ICM42670))) {
495
state.board_type.set_and_notify(FMUV6_BOARD_HOLYBRO_6X);
496
DEV_PRINTF("Detected Holybro 6X\n");
497
} else if ((spi_check_register_inv2("icm20649_2", INV2REG_WHOAMI, INV2_WHOAMI_ICM20649) &&
498
spi_check_register("icm42688", INV3REG_WHOAMI, INV3_WHOAMI_ICM42688) &&
499
spi_check_register("bmi088_g", BMI088REG_CHIPID, CHIPID_BMI088_G))) {
500
state.board_type.set_and_notify(FMUV6_BOARD_CUAV_6X);
501
DEV_PRINTF("Detected CUAV 6X\n");
502
AP_Param::load_defaults_file("@ROMFS/param/CUAV_V6X_defaults.parm", false);
503
} else if (spi_check_register("icm45686-1", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686) &&
504
spi_check_register("icm45686-2", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686) &&
505
spi_check_register("icm45686-3", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686)) {
506
state.board_type.set_and_notify(FMUV6_BOARD_HOLYBRO_6X_45686);
507
DEV_PRINTF("Detected Holybro 6X_45686\n");
508
} else if (spi_check_register("iim42652", INV3REG_WHOAMI, INV3_WHOAMI_IIM42652) &&
509
spi_check_register("icm45686", INV3REG_456_WHOAMI, INV3_WHOAMI_ICM45686)) {
510
state.board_type.set_and_notify(FMUV6_BOARD_HOLYBRO_6X_REV6);
511
DEV_PRINTF("Detected Holybro 6X_Rev6\n");
512
}
513
}
514
#endif
515
516