PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

95 lines
4.5KB

  1. // RHSPIDriver.h
  2. // Author: Mike McCauley (mikem@airspayce.com)
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: RHSPIDriver.h,v 1.10 2015/12/16 04:55:33 mikem Exp $
  5. #ifndef RHSPIDriver_h
  6. #define RHSPIDriver_h
  7. #include <RHGenericDriver.h>
  8. #include <RHHardwareSPI.h>
  9. // This is the bit in the SPI address that marks it as a write
  10. #define RH_SPI_WRITE_MASK 0x80
  11. class RHGenericSPI;
  12. /////////////////////////////////////////////////////////////////////
  13. /// \class RHSPIDriver RHSPIDriver.h <RHSPIDriver.h>
  14. /// \brief Base class for a RadioHead drivers that use the SPI bus
  15. /// to communicate with its transport hardware.
  16. ///
  17. /// This class can be subclassed by Drivers that require to use the SPI bus.
  18. /// It can be configured to use either the RHHardwareSPI class (if there is one available on the platform)
  19. /// of the bitbanged RHSoftwareSPI class. The default behaviour is to use a pre-instantiated built-in RHHardwareSPI
  20. /// interface.
  21. ///
  22. /// SPI bus access is protected by ATOMIC_BLOCK_START and ATOMIC_BLOCK_END, which will ensure interrupts
  23. /// are disabled during access.
  24. ///
  25. /// The read and write routines implement commonly used SPI conventions: specifically that the MSB
  26. /// of the first byte transmitted indicates that it is a write and the remaining bits indicate the rehgister to access)
  27. /// This can be overriden
  28. /// in subclasses if necessaryor an alternative class, RHNRFSPIDriver can be used to access devices like
  29. /// Nordic NRF series radios, which have different requirements.
  30. ///
  31. /// Application developers are not expected to instantiate this class directly:
  32. /// it is for the use of Driver developers.
  33. class RHSPIDriver : public RHGenericDriver
  34. {
  35. public:
  36. /// Constructor
  37. /// \param[in] slaveSelectPin The controler pin to use to select the desired SPI device. This pin will be driven LOW
  38. /// during SPI communications with the SPI device that uis iused by this Driver.
  39. /// \param[in] spi Reference to the SPI interface to use. The default is to use a default built-in Hardware interface.
  40. RHSPIDriver(uint8_t slaveSelectPin = SS, RHGenericSPI& spi = hardware_spi);
  41. /// Initialise the Driver transport hardware and software.
  42. /// Make sure the Driver is properly configured before calling init().
  43. /// \return true if initialisation succeeded.
  44. bool init();
  45. /// Reads a single register from the SPI device
  46. /// \param[in] reg Register number
  47. /// \return The value of the register
  48. uint8_t spiRead(uint8_t reg);
  49. /// Writes a single byte to the SPI device
  50. /// \param[in] reg Register number
  51. /// \param[in] val The value to write
  52. /// \return Some devices return a status byte during the first data transfer. This byte is returned.
  53. /// it may or may not be meaningfule depending on the the type of device being accessed.
  54. uint8_t spiWrite(uint8_t reg, uint8_t val);
  55. /// Reads a number of consecutive registers from the SPI device using burst read mode
  56. /// \param[in] reg Register number of the first register
  57. /// \param[in] dest Array to write the register values to. Must be at least len bytes
  58. /// \param[in] len Number of bytes to read
  59. /// \return Some devices return a status byte during the first data transfer. This byte is returned.
  60. /// it may or may not be meaningfule depending on the the type of device being accessed.
  61. uint8_t spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len);
  62. /// Write a number of consecutive registers using burst write mode
  63. /// \param[in] reg Register number of the first register
  64. /// \param[in] src Array of new register values to write. Must be at least len bytes
  65. /// \param[in] len Number of bytes to write
  66. /// \return Some devices return a status byte during the first data transfer. This byte is returned.
  67. /// it may or may not be meaningfule depending on the the type of device being accessed.
  68. uint8_t spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len);
  69. /// Set or change the pin to be used for SPI slave select.
  70. /// This can be called at any time to change the
  71. /// pin that will be used for slave select in subsquent SPI operations.
  72. /// \param[in] slaveSelectPin The pin to use
  73. void setSlaveSelectPin(uint8_t slaveSelectPin);
  74. protected:
  75. /// Reference to the RHGenericSPI instance to use to transfer data with teh SPI device
  76. RHGenericSPI& _spi;
  77. /// The pin number of the Slave Select pin that is used to select the desired device.
  78. uint8_t _slaveSelectPin;
  79. };
  80. #endif