PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

114 líneas
3.1KB

  1. // RHNRFSPIDriver.cpp
  2. //
  3. // Copyright (C) 2014 Mike McCauley
  4. // $Id: RHNRFSPIDriver.cpp,v 1.3 2015/12/16 04:55:33 mikem Exp $
  5. #include <RHNRFSPIDriver.h>
  6. RHNRFSPIDriver::RHNRFSPIDriver(uint8_t slaveSelectPin, RHGenericSPI& spi)
  7. :
  8. _spi(spi),
  9. _slaveSelectPin(slaveSelectPin)
  10. {
  11. }
  12. bool RHNRFSPIDriver::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. // Low level commands for interfacing with the device
  25. uint8_t RHNRFSPIDriver::spiCommand(uint8_t command)
  26. {
  27. uint8_t status;
  28. ATOMIC_BLOCK_START;
  29. _spi.beginTransaction();
  30. digitalWrite(_slaveSelectPin, LOW);
  31. status = _spi.transfer(command);
  32. digitalWrite(_slaveSelectPin, HIGH);
  33. _spi.endTransaction();
  34. ATOMIC_BLOCK_END;
  35. return status;
  36. }
  37. uint8_t RHNRFSPIDriver::spiRead(uint8_t reg)
  38. {
  39. uint8_t val;
  40. ATOMIC_BLOCK_START;
  41. _spi.beginTransaction();
  42. digitalWrite(_slaveSelectPin, LOW);
  43. _spi.transfer(reg); // Send the address, discard the status
  44. val = _spi.transfer(0); // The written value is ignored, reg value is read
  45. digitalWrite(_slaveSelectPin, HIGH);
  46. _spi.endTransaction();
  47. ATOMIC_BLOCK_END;
  48. return val;
  49. }
  50. uint8_t RHNRFSPIDriver::spiWrite(uint8_t reg, uint8_t val)
  51. {
  52. uint8_t status = 0;
  53. ATOMIC_BLOCK_START;
  54. _spi.beginTransaction();
  55. digitalWrite(_slaveSelectPin, LOW);
  56. status = _spi.transfer(reg); // Send the address
  57. _spi.transfer(val); // New value follows
  58. #if (RH_PLATFORM == RH_PLATFORM_ARDUINO) && defined(__arm__) && defined(CORE_TEENSY)
  59. // Sigh: some devices, such as MRF89XA dont work properly on Teensy 3.1:
  60. // At 1MHz, the clock returns low _after_ slave select goes high, which prevents SPI
  61. // write working. This delay gixes time for the clock to return low.
  62. delayMicroseconds(5);
  63. #endif
  64. digitalWrite(_slaveSelectPin, HIGH);
  65. _spi.endTransaction();
  66. ATOMIC_BLOCK_END;
  67. return status;
  68. }
  69. uint8_t RHNRFSPIDriver::spiBurstRead(uint8_t reg, uint8_t* dest, uint8_t len)
  70. {
  71. uint8_t status = 0;
  72. ATOMIC_BLOCK_START;
  73. _spi.beginTransaction();
  74. digitalWrite(_slaveSelectPin, LOW);
  75. status = _spi.transfer(reg); // Send the start address
  76. while (len--)
  77. *dest++ = _spi.transfer(0);
  78. digitalWrite(_slaveSelectPin, HIGH);
  79. _spi.endTransaction();
  80. ATOMIC_BLOCK_END;
  81. return status;
  82. }
  83. uint8_t RHNRFSPIDriver::spiBurstWrite(uint8_t reg, const uint8_t* src, uint8_t len)
  84. {
  85. uint8_t status = 0;
  86. ATOMIC_BLOCK_START;
  87. _spi.beginTransaction();
  88. digitalWrite(_slaveSelectPin, LOW);
  89. status = _spi.transfer(reg); // Send the start address
  90. while (len--)
  91. _spi.transfer(*src++);
  92. digitalWrite(_slaveSelectPin, HIGH);
  93. _spi.endTransaction();
  94. ATOMIC_BLOCK_END;
  95. return status;
  96. }
  97. void RHNRFSPIDriver::setSlaveSelectPin(uint8_t slaveSelectPin)
  98. {
  99. _slaveSelectPin = slaveSelectPin;
  100. }