BusTest.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // scan I2C and SPI buses for expected devices
  3. //
  4. #include <AP_Common/AP_Common.h>
  5. #include <AP_HAL/AP_HAL.h>
  6. const AP_HAL::HAL& hal = AP_HAL::get_HAL();
  7. static struct {
  8. const char *name;
  9. uint8_t whoami_reg;
  10. } whoami_list[] = {
  11. { "ms5611", 0x00 | 0x80 },
  12. { "mpu9250", 0x75 | 0x80 },
  13. { "mpu6000", 0x75 | 0x80 },
  14. { "lsm9ds0_am", 0x0F | 0x80 },
  15. { "lsm9ds0_g", 0x0F | 0x80 },
  16. };
  17. AP_HAL::OwnPtr<AP_HAL::Device>
  18. get_device(const char *name)
  19. {
  20. AP_HAL::OwnPtr<AP_HAL::Device> dev;
  21. const char *spi_name = hal.spi->get_device_name(0);
  22. /* Dummy is the default device at index 0
  23. if there isn't registered device for the target board.
  24. */
  25. const char *dummy = "**dummy**";
  26. if (!strcmp(dummy, spi_name)) {
  27. hal.console->printf("No devices, nothing to do.\n");
  28. while (1) {
  29. hal.scheduler->delay(500);
  30. }
  31. }
  32. /* We get possible registered device count on the target board */
  33. uint8_t spicount = hal.spi->get_count();
  34. for (uint8_t ref = 0; ref < spicount; ref++) {
  35. /*
  36. * We get the name from the index and we compare
  37. * with our possible devices list.
  38. */
  39. spi_name = hal.spi->get_device_name(ref);
  40. if (!strcmp(name, spi_name)) {
  41. dev = hal.spi->get_device(spi_name);
  42. break;
  43. }
  44. }
  45. return dev;
  46. }
  47. void setup(void)
  48. {
  49. hal.console->println("BusTest startup...\n");
  50. }
  51. void loop(void)
  52. {
  53. AP_HAL::OwnPtr<AP_HAL::Device> dev;
  54. AP_HAL::Semaphore *spi_sem;
  55. const char *bus_type = new char;
  56. hal.console->printf("Scanning SPI and I2C bus devices\n");
  57. for (uint8_t i = 0; i < ARRAY_SIZE(whoami_list); i++) {
  58. dev = get_device(whoami_list[i].name);
  59. if (!dev) {
  60. continue;
  61. }
  62. if (dev->bus_type() == AP_HAL::Device::BusType::BUS_TYPE_SPI) {
  63. bus_type = "SPI";
  64. } else {
  65. bus_type = "I2C";
  66. }
  67. hal.console->printf("Device %s registered on %s bus\n", whoami_list[i].name, bus_type);
  68. hal.console->printf("Trying to send data...\n");
  69. spi_sem = dev->get_semaphore();
  70. if (!spi_sem->take(1000)) {
  71. hal.console->printf("Failed to get SPI semaphore for %s\n", whoami_list[i].name);
  72. break;
  73. }
  74. uint8_t tx[2] = { whoami_list[i].whoami_reg, 0 };
  75. uint8_t rx[2] = { 0, 0 };
  76. dev->transfer(tx, sizeof(tx), rx, sizeof(rx));
  77. if (rx[1] != 0) {
  78. hal.console->printf("WHO_AM_I for %s: 0x%02x\n\n", whoami_list[i].name, (unsigned)rx[1]);
  79. } else {
  80. hal.console->printf("No response %s!\n\n", whoami_list[i].name);
  81. break;
  82. }
  83. spi_sem->give();
  84. }
  85. hal.scheduler->delay(1000);
  86. }
  87. AP_HAL_MAIN();