PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 line
2.5KB

  1. // RHSPIDriver.cpp
  2. //
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: RHSPIDriver.cpp,v 1.10 2015/12/16 04:55:33 mikem Exp $
  5. #include <RHSPIDriver.h>
  6. RHSPIDriver::RHSPIDriver(uint8_t slaveSelectPin, RHGenericSPI& spi)
  7. :
  8. _spi(spi),
  9. _slaveSelectPin(slaveSelectPin)
  10. {
  11. }
  12. bool RHSPIDriver::init()
  13. {
  14. // start the SPI library with the default speeds etc:
  15. // On Arduino Due this defaults to SPI1 on the central group of 6 SPI pins
  16. _spi.begin();
  17. // Initialise the slave select pin
  18. // On Maple, this must be _after_ spi.begin
  19. pinMode(_slaveSelectPin, OUTPUT);
  20. digitalWrite(_slaveSelectPin, HIGH);
  21. delay(100);
  22. return true;
  23. }
  24. uint8_t RHSPIDriver::spiRead(uint8_t reg)
  25. {
  26. uint8_t val;
  27. ATOMIC_BLOCK_START;
  28. _spi.beginTransaction();
  29. digitalWrite(_slaveSelectPin, LOW);
  30. _spi.transfer(reg & ~RH_SPI_WRITE_MASK); // Send the address with the write mask off
  31. val = _spi.transfer(0); // The written value is ignored, reg value is read
  32. digitalWrite(_slaveSelectPin, HIGH);
  33. _spi.endTransaction();
  34. ATOMIC_BLOCK_END;
  35. return val;
  36. }
  37. uint8_t RHSPIDriver::spiWrite(uint8_t reg, uint8_t val)
  38. {
  39. uint8_t status = 0;
  40. ATOMIC_BLOCK_START;
  41. _spi.beginTransaction();
  42. digitalWrite(_slaveSelectPin, LOW);
  43. status = _spi.transfer(reg | RH_SPI_WRITE_MASK); // Send the address with the write mask on
  44. _spi.transfer(val); // New value follows
  45. digitalWrite(_slaveSelectPin, HIGH);
  46. _spi.endTransaction();
  47. ATOMIC_BLOCK_END;
  48. return status;
  49. }
  50. uint8_t RHSPIDriver::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len)
  51. {
  52. uint8_t status = 0;
  53. ATOMIC_BLOCK_START;
  54. _spi.beginTransaction();
  55. digitalWrite(_slaveSelectPin, LOW);
  56. status = _spi.transfer(reg & ~RH_SPI_WRITE_MASK); // Send the start address with the write mask off
  57. while (len--)
  58. *dest++ = _spi.transfer(0);
  59. digitalWrite(_slaveSelectPin, HIGH);
  60. _spi.endTransaction();
  61. ATOMIC_BLOCK_END;
  62. return status;
  63. }
  64. uint8_t RHSPIDriver::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len)
  65. {
  66. uint8_t status = 0;
  67. ATOMIC_BLOCK_START;
  68. _spi.beginTransaction();
  69. digitalWrite(_slaveSelectPin, LOW);
  70. status = _spi.transfer(reg | RH_SPI_WRITE_MASK); // Send the start address with the write mask on
  71. while (len--)
  72. _spi.transfer(*src++);
  73. digitalWrite(_slaveSelectPin, HIGH);
  74. _spi.endTransaction();
  75. ATOMIC_BLOCK_END;
  76. return status;
  77. }
  78. void RHSPIDriver::setSlaveSelectPin(uint8_t slaveSelectPin)
  79. {
  80. _slaveSelectPin = slaveSelectPin;
  81. }