board_drivers.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. * AP_BoardConfig - px4 driver loading and setup
  15. */
  16. #include <AP_HAL/AP_HAL.h>
  17. #include "AP_BoardConfig.h"
  18. #include <GCS_MAVLink/GCS.h>
  19. #include <AP_Math/crc.h>
  20. #include <stdio.h>
  21. extern const AP_HAL::HAL& hal;
  22. /*
  23. init safety state
  24. */
  25. void AP_BoardConfig::board_init_safety()
  26. {
  27. #if HAL_HAVE_SAFETY_SWITCH
  28. bool force_safety_off = (state.safety_enable.get() == 0);
  29. if (!force_safety_off && hal.util->was_watchdog_safety_off()) {
  30. gcs().send_text(MAV_SEVERITY_INFO, "Forcing safety off for watchdog\n");
  31. force_safety_off = true;
  32. }
  33. if (force_safety_off) {
  34. hal.rcout->force_safety_off();
  35. // wait until safety has been turned off
  36. uint8_t count = 20;
  37. while (hal.util->safety_switch_state() != AP_HAL::Util::SAFETY_ARMED && count--) {
  38. hal.scheduler->delay(20);
  39. }
  40. }
  41. #endif
  42. }
  43. #if AP_FEATURE_BOARD_DETECT
  44. AP_BoardConfig::px4_board_type AP_BoardConfig::px4_configured_board;
  45. #if defined(CONFIG_ARCH_BOARD_PX4FMU_V4)
  46. extern "C" {
  47. int fmu_main(int, char **);
  48. };
  49. #endif
  50. void AP_BoardConfig::board_setup_drivers(void)
  51. {
  52. #if defined(CONFIG_ARCH_BOARD_PX4FMU_V4)
  53. /*
  54. this works around an issue with some FMUv4 hardware (eg. copies
  55. of the Pixracer) which have incorrect components leading to
  56. sensor brownout on boot
  57. */
  58. if (px4_start_driver(fmu_main, "fmu", "sensor_reset 20")) {
  59. printf("FMUv4 sensor reset complete\n");
  60. }
  61. #endif
  62. if (state.board_type == PX4_BOARD_OLDDRIVERS) {
  63. printf("Old drivers no longer supported\n");
  64. state.board_type = PX4_BOARD_AUTO;
  65. }
  66. // run board auto-detection
  67. board_autodetect();
  68. if (state.board_type == PX4_BOARD_PH2SLIM ||
  69. state.board_type == PX4_BOARD_PIXHAWK2) {
  70. _imu_target_temperature.set_default(45);
  71. if (_imu_target_temperature.get() < 0) {
  72. // don't allow a value of -1 on the cube, or it could cook
  73. // the IMU
  74. _imu_target_temperature.set(45);
  75. }
  76. }
  77. px4_configured_board = (enum px4_board_type)state.board_type.get();
  78. switch (px4_configured_board) {
  79. case PX4_BOARD_PX4V1:
  80. case PX4_BOARD_PIXHAWK:
  81. case PX4_BOARD_PIXHAWK2:
  82. case PX4_BOARD_FMUV5:
  83. case PX4_BOARD_FMUV6:
  84. case PX4_BOARD_SP01:
  85. case PX4_BOARD_PIXRACER:
  86. case PX4_BOARD_PHMINI:
  87. case PX4_BOARD_AUAV21:
  88. case PX4_BOARD_PH2SLIM:
  89. case VRX_BOARD_BRAIN51:
  90. case VRX_BOARD_BRAIN52:
  91. case VRX_BOARD_BRAIN52E:
  92. case VRX_BOARD_UBRAIN51:
  93. case VRX_BOARD_UBRAIN52:
  94. case VRX_BOARD_CORE10:
  95. case VRX_BOARD_BRAIN54:
  96. case PX4_BOARD_AEROFC:
  97. case PX4_BOARD_PIXHAWK_PRO:
  98. case PX4_BOARD_PCNC1:
  99. case PX4_BOARD_MINDPXV2:
  100. break;
  101. default:
  102. sensor_config_error("Unknown board type");
  103. break;
  104. }
  105. }
  106. #define SPI_PROBE_DEBUG 0
  107. /*
  108. check a SPI device for a register value
  109. */
  110. bool AP_BoardConfig::spi_check_register(const char *devname, uint8_t regnum, uint8_t value, uint8_t read_flag)
  111. {
  112. auto dev = hal.spi->get_device(devname);
  113. if (!dev) {
  114. #if SPI_PROBE_DEBUG
  115. hal.console->printf("%s: no device\n", devname);
  116. #endif
  117. return false;
  118. }
  119. dev->set_read_flag(read_flag);
  120. if (!dev->get_semaphore()->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
  121. return false;
  122. }
  123. uint8_t v;
  124. if (!dev->read_registers(regnum, &v, 1)) {
  125. #if SPI_PROBE_DEBUG
  126. hal.console->printf("%s: reg %02x read fail\n", devname, (unsigned)regnum);
  127. #endif
  128. dev->get_semaphore()->give();
  129. return false;
  130. }
  131. dev->get_semaphore()->give();
  132. #if SPI_PROBE_DEBUG
  133. hal.console->printf("%s: reg %02x expected:%02x got:%02x\n", devname, (unsigned)regnum, (unsigned)value, (unsigned)v);
  134. #endif
  135. return v == value;
  136. }
  137. #if defined(HAL_CHIBIOS_ARCH_CUBEBLACK)
  138. static bool check_ms5611(const char* devname) {
  139. auto dev = hal.spi->get_device(devname);
  140. if (!dev) {
  141. #if SPI_PROBE_DEBUG
  142. hal.console->printf("%s: no device\n", devname);
  143. #endif
  144. return false;
  145. }
  146. AP_HAL::Semaphore *dev_sem = dev->get_semaphore();
  147. if (!dev_sem || !dev_sem->take(HAL_SEMAPHORE_BLOCK_FOREVER)) {
  148. return false;
  149. }
  150. static const uint8_t CMD_MS56XX_RESET = 0x1E;
  151. static const uint8_t CMD_MS56XX_PROM = 0xA0;
  152. dev->transfer(&CMD_MS56XX_RESET, 1, nullptr, 0);
  153. hal.scheduler->delay(4);
  154. uint16_t prom[8];
  155. bool all_zero = true;
  156. for (uint8_t i = 0; i < 8; i++) {
  157. const uint8_t reg = CMD_MS56XX_PROM + (i << 1);
  158. uint8_t val[2];
  159. if (!dev->transfer(&reg, 1, val, sizeof(val))) {
  160. dev_sem->give();
  161. #if SPI_PROBE_DEBUG
  162. hal.console->printf("%s: transfer fail\n", devname);
  163. #endif
  164. return false;
  165. }
  166. prom[i] = (val[0] << 8) | val[1];
  167. if (prom[i] != 0) {
  168. all_zero = false;
  169. }
  170. }
  171. dev_sem->give();
  172. uint16_t crc_read = prom[7]&0xf;
  173. prom[7] &= 0xff00;
  174. if (crc_read != crc_crc4(prom) || all_zero) {
  175. #if SPI_PROBE_DEBUG
  176. hal.console->printf("%s: crc fail\n", devname);
  177. #endif
  178. return false;
  179. }
  180. #if SPI_PROBE_DEBUG
  181. hal.console->printf("%s: found successfully\n", devname);
  182. #endif
  183. return true;
  184. }
  185. #endif
  186. #define MPUREG_WHOAMI 0x75
  187. #define MPU_WHOAMI_MPU60X0 0x68
  188. #define MPU_WHOAMI_MPU9250 0x71
  189. #define MPU_WHOAMI_ICM20608 0xaf
  190. #define MPU_WHOAMI_ICM20602 0x12
  191. #define LSMREG_WHOAMI 0x0f
  192. #define LSM_WHOAMI_LSM303D 0x49
  193. #define LSM_WHOAMI_L3GD20 0xd4
  194. #define INV2REG_WHOAMI 0x00
  195. #define INV2_WHOAMI_ICM20948 0xEA
  196. /*
  197. validation of the board type
  198. */
  199. void AP_BoardConfig::validate_board_type(void)
  200. {
  201. /* some boards can be damaged by the user setting the wrong board
  202. type. The key one is the cube which has a heater which can
  203. cook the IMUs if the user uses an old paramater file. We
  204. override the board type for that specific case
  205. */
  206. #if defined(CONFIG_ARCH_BOARD_PX4FMU_V2) || defined(HAL_CHIBIOS_ARCH_FMUV3)
  207. if (state.board_type == PX4_BOARD_PIXHAWK &&
  208. (spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
  209. spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) ||
  210. spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
  211. spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||
  212. spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&
  213. (spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) ||
  214. spi_check_register("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948))) {
  215. // Pixhawk2 has LSM303D and MPUxxxx on external bus. If we
  216. // detect those, then force PIXHAWK2, even if the user has
  217. // configured for PIXHAWK1
  218. #if !defined(CONFIG_ARCH_BOARD_PX4FMU_V3) && !defined(HAL_CHIBIOS_ARCH_FMUV3)
  219. // force user to load the right firmware
  220. sensor_config_error("Pixhawk2 requires FMUv3 firmware");
  221. #endif
  222. state.board_type.set(PX4_BOARD_PIXHAWK2);
  223. hal.console->printf("Forced PIXHAWK2\n");
  224. }
  225. #endif
  226. #if defined(CONFIG_ARCH_BOARD_PX4FMU_V4PRO)
  227. // Nothing to do for the moment
  228. #endif
  229. }
  230. void AP_BoardConfig::check_cubeblack(void)
  231. {
  232. #if defined(HAL_CHIBIOS_ARCH_CUBEBLACK)
  233. if (state.board_type != PX4_BOARD_PIXHAWK2) {
  234. state.board_type.set(PX4_BOARD_PIXHAWK2);
  235. }
  236. bool success = true;
  237. if (!spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250)) { success = false; }
  238. if (!spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) &&
  239. !spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) { success = false; }
  240. if (!(spi_check_register("lsm9ds0_ext_g", LSMREG_WHOAMI, LSM_WHOAMI_L3GD20) &&
  241. spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D)) &&
  242. !spi_check_register("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948)) { success = false; }
  243. if (!check_ms5611("ms5611")) { success = false; }
  244. if (!check_ms5611("ms5611_ext")) { success = false; }
  245. if (!success) {
  246. sensor_config_error("Failed to init CubeBlack - sensor mismatch");
  247. }
  248. #endif
  249. }
  250. /*
  251. auto-detect board type
  252. */
  253. void AP_BoardConfig::board_autodetect(void)
  254. {
  255. #if defined(HAL_CHIBIOS_ARCH_CUBEBLACK)
  256. check_cubeblack();
  257. return;
  258. #endif
  259. if (state.board_type != PX4_BOARD_AUTO) {
  260. validate_board_type();
  261. // user has chosen a board type
  262. return;
  263. }
  264. #if defined(CONFIG_ARCH_BOARD_PX4FMU_V1)
  265. // only one choice
  266. state.board_type.set(PX4_BOARD_PX4V1);
  267. hal.console->printf("Detected PX4v1\n");
  268. #elif defined(CONFIG_ARCH_BOARD_PX4FMU_V2) || defined(HAL_CHIBIOS_ARCH_FMUV3)
  269. if ((spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
  270. spi_check_register("mpu6000_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
  271. spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
  272. spi_check_register("mpu9250_ext", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250) ||
  273. spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
  274. spi_check_register("icm20608_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||
  275. spi_check_register("icm20602_ext", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&
  276. (spi_check_register("lsm9ds0_ext_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) ||
  277. spi_check_register("icm20948_ext", INV2REG_WHOAMI, INV2_WHOAMI_ICM20948))) {
  278. // Pixhawk2 has LSM303D and MPUxxxx on external bus
  279. state.board_type.set(PX4_BOARD_PIXHAWK2);
  280. hal.console->printf("Detected PIXHAWK2\n");
  281. } else if ((spi_check_register("icm20608-am", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
  282. spi_check_register("icm20608-am", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602)) &&
  283. spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250)) {
  284. // PHMINI has an ICM20608 and MPU9250 on sensor bus
  285. state.board_type.set(PX4_BOARD_PHMINI);
  286. hal.console->printf("Detected PixhawkMini\n");
  287. } else if (spi_check_register("lsm9ds0_am", LSMREG_WHOAMI, LSM_WHOAMI_LSM303D) &&
  288. (spi_check_register("mpu6000", MPUREG_WHOAMI, MPU_WHOAMI_MPU60X0) ||
  289. spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20608) ||
  290. spi_check_register("icm20608", MPUREG_WHOAMI, MPU_WHOAMI_ICM20602) ||
  291. spi_check_register("mpu9250", MPUREG_WHOAMI, MPU_WHOAMI_MPU9250))) {
  292. // classic or upgraded Pixhawk1
  293. state.board_type.set(PX4_BOARD_PIXHAWK);
  294. hal.console->printf("Detected Pixhawk\n");
  295. } else {
  296. sensor_config_error("Unable to detect board type");
  297. }
  298. #elif defined(CONFIG_ARCH_BOARD_PX4FMU_V4) || defined(HAL_CHIBIOS_ARCH_FMUV4)
  299. // only one choice
  300. state.board_type.set_and_notify(PX4_BOARD_PIXRACER);
  301. hal.console->printf("Detected Pixracer\n");
  302. #elif defined(HAL_CHIBIOS_ARCH_MINDPXV2)
  303. // only one choice
  304. state.board_type.set_and_notify(PX4_BOARD_MINDPXV2);
  305. hal.console->printf("Detected MindPX-V2\n");
  306. #elif defined(CONFIG_ARCH_BOARD_PX4FMU_V4PRO) || defined(HAL_CHIBIOS_ARCH_FMUV4PRO)
  307. // only one choice
  308. state.board_type.set_and_notify(PX4_BOARD_PIXHAWK_PRO);
  309. hal.console->printf("Detected Pixhawk Pro\n");
  310. #elif defined(CONFIG_ARCH_BOARD_AEROFC_V1)
  311. state.board_type.set_and_notify(PX4_BOARD_AEROFC);
  312. hal.console->printf("Detected Aero FC\n");
  313. #elif defined(HAL_CHIBIOS_ARCH_FMUV5)
  314. state.board_type.set_and_notify(PX4_BOARD_FMUV5);
  315. hal.console->printf("Detected FMUv5\n");
  316. #elif defined(HAL_CHIBIOS_ARCH_FMUV6)
  317. state.board_type.set_and_notify(PX4_BOARD_FMUV5);
  318. hal.console->printf("Detected FMUv6\n");
  319. #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V51) || defined(HAL_CHIBIOS_ARCH_BRAINV51)
  320. state.board_type.set_and_notify(VRX_BOARD_BRAIN51);
  321. hal.console->printf("Detected VR Brain 5.1\n");
  322. #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V52) || defined(HAL_CHIBIOS_ARCH_BRAINV52)
  323. state.board_type.set_and_notify(VRX_BOARD_BRAIN52);
  324. hal.console->printf("Detected VR Brain 5.2\n");
  325. #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V52E)
  326. state.board_type.set_and_notify(VRX_BOARD_BRAIN52E);
  327. hal.console->printf("Detected VR Brain 5.2E\n");
  328. #elif defined(CONFIG_ARCH_BOARD_VRUBRAIN_V51) || defined(HAL_CHIBIOS_ARCH_UBRAINV51)
  329. state.board_type.set_and_notify(VRX_BOARD_UBRAIN51);
  330. hal.console->printf("Detected VR Micro Brain 5.1\n");
  331. #elif defined(CONFIG_ARCH_BOARD_VRUBRAIN_V52)
  332. state.board_type.set_and_notify(VRX_BOARD_UBRAIN52);
  333. hal.console->printf("Detected VR Micro Brain 5.2\n");
  334. #elif defined(CONFIG_ARCH_BOARD_VRCORE_V10) || defined(HAL_CHIBIOS_ARCH_COREV10)
  335. state.board_type.set_and_notify(VRX_BOARD_CORE10);
  336. hal.console->printf("Detected VR Core 1.0\n");
  337. #elif defined(CONFIG_ARCH_BOARD_VRBRAIN_V54) || defined(HAL_CHIBIOS_ARCH_BRAINV54)
  338. state.board_type.set_and_notify(VRX_BOARD_BRAIN54);
  339. hal.console->printf("Detected VR Brain 5.4\n");
  340. #endif
  341. }
  342. #endif // AP_FEATURE_BOARD_DETECT
  343. /*
  344. setup flow control on UARTs
  345. */
  346. void AP_BoardConfig::board_setup_uart()
  347. {
  348. #if AP_FEATURE_RTSCTS
  349. hal.uartC->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser1_rtscts.get());
  350. if (hal.uartD != nullptr) {
  351. hal.uartD->set_flow_control((AP_HAL::UARTDriver::flow_control)state.ser2_rtscts.get());
  352. }
  353. #endif
  354. }
  355. /*
  356. setup SBUS
  357. */
  358. void AP_BoardConfig::board_setup_sbus(void)
  359. {
  360. #if AP_FEATURE_SBUS_OUT
  361. if (state.sbus_out_rate.get() >= 1) {
  362. static const struct {
  363. uint8_t value;
  364. uint16_t rate;
  365. } rates[] = {
  366. { 1, 50 },
  367. { 2, 75 },
  368. { 3, 100 },
  369. { 4, 150 },
  370. { 5, 200 },
  371. { 6, 250 },
  372. { 7, 300 }
  373. };
  374. uint16_t rate = 300;
  375. for (uint8_t i=0; i<ARRAY_SIZE(rates); i++) {
  376. if (rates[i].value == state.sbus_out_rate) {
  377. rate = rates[i].rate;
  378. }
  379. }
  380. if (!hal.rcout->enable_px4io_sbus_out(rate)) {
  381. hal.console->printf("Failed to enable SBUS out\n");
  382. }
  383. }
  384. #endif
  385. }
  386. /*
  387. setup peripherals and drivers
  388. */
  389. void AP_BoardConfig::board_setup()
  390. {
  391. #if CONFIG_HAL_BOARD == HAL_BOARD_CHIBIOS
  392. // init needs to be done after boardconfig is read so parameters are set
  393. hal.gpio->init();
  394. hal.rcin->init();
  395. hal.rcout->init();
  396. #endif
  397. #ifdef HAL_GPIO_PWM_VOLT_PIN
  398. if (_pwm_volt_sel == 0) {
  399. hal.gpio->write(HAL_GPIO_PWM_VOLT_PIN, 1); //set pin for 3.3V PWM Output
  400. } else if (_pwm_volt_sel == 1) {
  401. hal.gpio->write(HAL_GPIO_PWM_VOLT_PIN, 0); //set pin for 5V PWM Output
  402. }
  403. #endif
  404. board_setup_uart();
  405. board_setup_sbus();
  406. #if AP_FEATURE_BOARD_DETECT
  407. board_setup_drivers();
  408. #endif
  409. }