SPIUARTDriver.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "SPIUARTDriver.h"
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <cstdio>
  5. #include <AP_HAL/AP_HAL.h>
  6. #include <AP_Math/AP_Math.h>
  7. extern const AP_HAL::HAL &hal;
  8. #ifdef SPIUART_DEBUG
  9. #include <stdio.h>
  10. #define debug(fmt, args ...) do {hal.console->printf("[SPIUARTDriver]: %s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
  11. #define error(fmt, args ...) do {fprintf(stderr,"%s:%d: " fmt "\n", __FUNCTION__, __LINE__, ## args); } while(0)
  12. #else
  13. #define debug(fmt, args ...)
  14. #define error(fmt, args ...)
  15. #endif
  16. using namespace Linux;
  17. SPIUARTDriver::SPIUARTDriver()
  18. : UARTDriver(false)
  19. {
  20. }
  21. void SPIUARTDriver::begin(uint32_t b, uint16_t rxS, uint16_t txS)
  22. {
  23. if (device_path != nullptr) {
  24. UARTDriver::begin(b, rxS, txS);
  25. if (is_initialized()) {
  26. _external = true;
  27. return;
  28. }
  29. }
  30. if (!is_initialized()) {
  31. _dev = hal.spi->get_device("ublox");
  32. if (!_dev) {
  33. return;
  34. }
  35. }
  36. if (rxS < 1024) {
  37. rxS = 2048;
  38. }
  39. if (txS < 1024) {
  40. txS = 2048;
  41. }
  42. _readbuf.set_size(rxS);
  43. _writebuf.set_size(txS);
  44. if (_buffer == nullptr) {
  45. /* Do not allocate new buffer, if we're just changing speed */
  46. _buffer = new uint8_t[rxS];
  47. if (_buffer == nullptr) {
  48. hal.console->printf("Not enough memory\n");
  49. AP_HAL::panic("Not enough memory\n");
  50. }
  51. }
  52. switch (b) {
  53. case 4000000U:
  54. if (is_initialized()) {
  55. /* Do not allow speed changes before device is initialized, because
  56. * it can lead to misconfiguraration. Once the device is initialized,
  57. * it's sage to update speed
  58. */
  59. _dev->set_speed(AP_HAL::Device::SPEED_HIGH);
  60. debug("Set higher SPI-frequency");
  61. } else {
  62. _dev->set_speed(AP_HAL::Device::SPEED_LOW);
  63. debug("Set lower SPI-frequency");
  64. }
  65. break;
  66. default:
  67. _dev->set_speed(AP_HAL::Device::SPEED_LOW);
  68. debug("Set lower SPI-frequency");
  69. debug("%s: wrong baudrate (%u) for SPI-driven device. setting default speed", __func__, b);
  70. break;
  71. }
  72. _initialised = true;
  73. }
  74. int SPIUARTDriver::_write_fd(const uint8_t *buf, uint16_t size)
  75. {
  76. if (_external) {
  77. return UARTDriver::_write_fd(buf,size);
  78. }
  79. if (!_dev->get_semaphore()->take_nonblocking()) {
  80. return 0;
  81. }
  82. _dev->transfer_fullduplex(buf, _buffer, size);
  83. _dev->get_semaphore()->give();
  84. uint16_t ret = size;
  85. /* Since all SPI-transactions are transfers we need update
  86. * the _readbuf. I do believe there is a way to encapsulate
  87. * this operation since it's the same as in the
  88. * UARTDriver::write().
  89. */
  90. _readbuf.write(_buffer, size);
  91. return ret;
  92. }
  93. int SPIUARTDriver::_read_fd(uint8_t *buf, uint16_t n)
  94. {
  95. static uint8_t ff_stub[100] = {0xff};
  96. if (_external) {
  97. return UARTDriver::_read_fd(buf, n);
  98. }
  99. /* Make SPI transactions shorter. It can save SPI bus from keeping too
  100. * long. It's essential for NavIO as MPU9250 is on the same bus and
  101. * doesn't like to be waiting. Making transactions more frequent but shorter
  102. * is a win.
  103. */
  104. n = MIN(n, 100);
  105. if (!_dev->get_semaphore()->take_nonblocking()) {
  106. return 0;
  107. }
  108. _dev->transfer_fullduplex(ff_stub, buf, n);
  109. _dev->get_semaphore()->give();
  110. return n;
  111. }
  112. void SPIUARTDriver::_timer_tick(void)
  113. {
  114. if (_external) {
  115. UARTDriver::_timer_tick();
  116. return;
  117. }
  118. /* lower the update rate */
  119. if (AP_HAL::micros() - _last_update_timestamp < 10000) {
  120. return;
  121. }
  122. UARTDriver::_timer_tick();
  123. _last_update_timestamp = AP_HAL::micros();
  124. }