Device.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * This file is free software: you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published
  4. * by the Free Software Foundation, either version 3 of the License,
  5. * or (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * 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. #pragma once
  16. #include <inttypes.h>
  17. #include <AP_HAL/HAL.h>
  18. #include "Semaphores.h"
  19. #include "AP_HAL_ChibiOS.h"
  20. #if HAL_USE_I2C == TRUE || HAL_USE_SPI == TRUE
  21. #include "Scheduler.h"
  22. #include "shared_dma.h"
  23. #include "hwdef/common/bouncebuffer.h"
  24. namespace ChibiOS {
  25. class DeviceBus {
  26. public:
  27. DeviceBus(uint8_t _thread_priority = APM_I2C_PRIORITY);
  28. struct DeviceBus *next;
  29. Semaphore semaphore;
  30. Shared_DMA *dma_handle;
  31. AP_HAL::Device::PeriodicHandle register_periodic_callback(uint32_t period_usec, AP_HAL::Device::PeriodicCb, AP_HAL::Device *hal_device);
  32. bool adjust_timer(AP_HAL::Device::PeriodicHandle h, uint32_t period_usec);
  33. static void bus_thread(void *arg);
  34. void bouncebuffer_setup(const uint8_t *&buf_tx, uint16_t tx_len,
  35. uint8_t *&buf_rx, uint16_t rx_len);
  36. void bouncebuffer_finish(const uint8_t *buf_tx, uint8_t *buf_rx, uint16_t rx_len);
  37. private:
  38. struct callback_info {
  39. struct callback_info *next;
  40. AP_HAL::Device::PeriodicCb cb;
  41. uint32_t period_usec;
  42. uint64_t next_usec;
  43. } *callbacks;
  44. uint8_t thread_priority;
  45. thread_t* thread_ctx;
  46. bool thread_started;
  47. AP_HAL::Device *hal_device;
  48. // support for bounce buffers for DMA-safe transfers
  49. struct bouncebuffer_t *bounce_buffer_tx;
  50. struct bouncebuffer_t *bounce_buffer_rx;
  51. };
  52. }
  53. #endif // I2C or SPI