Device.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Device is an I2C/SPI compatible base class library.
  3. Copyright (C) Martin Lindupp 2019
  4. V1.0.0 -- Initial release
  5. V1.0.1 -- Added ESP32 HSPI support
  6. V1.0.2 -- Modification to allow external creation of HSPI object on ESP32
  7. V1.0.3 -- Addition of SPI write and read byte masks
  8. V1.0.4 -- Modification to allow user-defined pins for I2C operation on the ESP8266
  9. V1.0.5 -- Modification to allow user-defined pins for I2C operation on the ESP32
  10. V1.0.6 -- Initialise "device" constructor member variables in the same order they are declared
  11. The MIT License (MIT)
  12. Permission is hereby granted, free of charge, to any person obtaining a copy
  13. of this software and associated documentation files (the "Software"), to deal
  14. in the Software without restriction, including without limitation the rights
  15. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. copies of the Software, and to permit persons to whom the Software is
  17. furnished to do so, subject to the following conditions:
  18. The above copyright notice and this permission notice shall be included in all
  19. copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. SOFTWARE.
  27. */
  28. #include <Device.h>
  29. ////////////////////////////////////////////////////////////////////////////////
  30. // Device Class Constructors
  31. ////////////////////////////////////////////////////////////////////////////////
  32. Device::Device() : comms(I2C_COMMS) {} // Initialise constructor for I2C communications
  33. #ifdef ARDUINO_ARCH_ESP8266
  34. Device::Device(uint8_t sda, uint8_t scl) : comms(I2C_COMMS_DEFINED_PINS), sda(sda), scl(scl) {} // Constructor for ESP8266 I2C with user-defined pins
  35. #endif
  36. Device::Device(uint8_t cs) : comms(SPI_COMMS), cs(cs), spiClockSpeed(1000000) {} // Constructor for SPI communications
  37. #ifdef ARDUINO_ARCH_ESP32
  38. Device::Device(uint8_t sda, uint8_t scl) : comms(I2C_COMMS_DEFINED_PINS), sda(sda), scl(scl) {} // Constructor for ESP32 I2C with user-defined pins
  39. Device::Device(uint8_t cs, uint8_t spiPort, SPIClass& spiClass) // Constructor for ESP32 HSPI communications
  40. : comms(SPI_COMMS), cs(cs), spiPort(spiPort), spi(&spiClass), spiClockSpeed(1000000) {}
  41. #endif
  42. ////////////////////////////////////////////////////////////////////////////////
  43. // Device Public Member Function
  44. ////////////////////////////////////////////////////////////////////////////////
  45. void Device::setClock(uint32_t clockSpeed) // Set the I2C or SPI clock speed
  46. {
  47. if (comms == I2C_COMMS)
  48. {
  49. Wire.setClock(clockSpeed);
  50. }
  51. else
  52. {
  53. spiClockSpeed = clockSpeed;
  54. }
  55. }
  56. ////////////////////////////////////////////////////////////////////////////////
  57. // Device I2C & SPI Wrapper (Protected) Member Functions
  58. ////////////////////////////////////////////////////////////////////////////////
  59. void Device::initialise() // Initialise device communications
  60. {
  61. if (comms == I2C_COMMS) // Check with communications bus has been selected I2C or SPI
  62. {
  63. Wire.begin(); // Initialise I2C communication
  64. Wire.setClock(400000); // Set the SCL clock to default of 400kHz
  65. }
  66. #if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32
  67. else if (comms == I2C_COMMS_DEFINED_PINS) // Check if the ESP8266 or ESP32 has specified user-defined I2C pins
  68. {
  69. Wire.begin(sda, scl); // Initialise I2C communication with user-defined pins
  70. Wire.setClock(400000); // Set the SCL clock to default of 400kHz
  71. comms = I2C_COMMS; // Set the communications to standard I2C
  72. }
  73. #endif
  74. else
  75. {
  76. digitalWrite(cs, HIGH); // Pull the chip select (CS) pin high
  77. pinMode(cs, OUTPUT); // Set-up the SPI chip select pin
  78. #ifdef ARDUINO_ARCH_ESP32
  79. if (spiPort == HSPI) // Set-up spi pointer for VSPI or HSPI communications
  80. {
  81. spi->begin(14, 27, 13, 2); // Start HSPI on SCK 14, MOSI 13, MISO 24, SS CS (GPIO2 acts as dummy pin)
  82. }
  83. else
  84. {
  85. spi = &SPI; // Start VSPI on SCK 5, MOSI 18, MISO 19, SS CS
  86. spi->begin();
  87. }
  88. #else
  89. spi = &SPI; // Set-up spi pointer for SPI communications
  90. spi->begin();
  91. #endif
  92. }
  93. }
  94. void Device::setI2CAddress(uint8_t addr) // Set the Device's I2C address
  95. {
  96. address = addr;
  97. }
  98. void Device::writeByte(uint8_t subAddress, uint8_t data)
  99. {
  100. if (comms == I2C_COMMS)
  101. {
  102. Wire.beginTransmission(address); // Write a byte to the sub-address using I2C
  103. Wire.write(subAddress);
  104. Wire.write(data);
  105. Wire.endTransmission();
  106. }
  107. else // if (comms == SPI_COMMS)
  108. {
  109. spi->beginTransaction(SPISettings(spiClockSpeed, MSBFIRST, SPI_MODE0)); // Write a byte to the sub-address using SPI
  110. digitalWrite(cs, LOW);
  111. spi->transfer(subAddress & WRITE_MASK);
  112. spi->transfer(data);
  113. digitalWrite(cs, HIGH);
  114. spi->endTransaction();
  115. }
  116. }
  117. uint8_t Device::readByte(uint8_t subAddress) // Read a byte from the sub-address using I2C
  118. {
  119. uint8_t data = 0x00;
  120. if (comms == I2C_COMMS)
  121. {
  122. Wire.beginTransmission(address);
  123. Wire.write(subAddress);
  124. Wire.endTransmission(false);
  125. Wire.requestFrom(address, (uint8_t)1);
  126. data = Wire.read();
  127. }
  128. else // if (comms == SPI_COMMS)
  129. {
  130. spi->beginTransaction(SPISettings(spiClockSpeed, MSBFIRST, SPI_MODE0)); // Read a byte from the sub-address using SPI
  131. digitalWrite(cs, LOW);
  132. spi->transfer(subAddress | READ_MASK);
  133. data = spi->transfer(data);
  134. digitalWrite(cs, HIGH);
  135. spi->endTransaction();
  136. }
  137. return data; // Return data read from sub-address register
  138. }
  139. void Device::readBytes(uint8_t subAddress, uint8_t* data, uint16_t count)
  140. {
  141. if (comms == I2C_COMMS) // Read "count" bytes into the "data" buffer using I2C
  142. {
  143. Wire.beginTransmission(address);
  144. Wire.write(subAddress);
  145. Wire.endTransmission(false);
  146. uint8_t i = 0;
  147. Wire.requestFrom(address, (uint8_t)count);
  148. while (Wire.available())
  149. {
  150. data[i++] = Wire.read();
  151. }
  152. }
  153. else // if (comms == SPI_COMMS)
  154. {
  155. spi->beginTransaction(SPISettings(spiClockSpeed, MSBFIRST, SPI_MODE0)); // Read "count" bytes into the "data" buffer using SPI
  156. digitalWrite(cs, LOW);
  157. spi->transfer(subAddress | READ_MASK);
  158. spi->transfer(data, count);
  159. digitalWrite(cs, HIGH);
  160. spi->endTransaction();
  161. }
  162. }