Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

UserSPIDriver.ino 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // An example of an external SPI driver.
  2. //
  3. #include "SdFat.h"
  4. #include "SPI.h" // Only required if you use features in the SPI library.
  5. #if SPI_DRIVER_SELECT == 3 // Must be set in SdFat/SdFatConfig.h
  6. // SD chip select pin.
  7. #define SD_CS_PIN SS
  8. // This is a simple driver based on the the standard SPI.h library.
  9. // You can write a driver entirely independent of SPI.h.
  10. // It can be optimized for your board or a different SPI port can be used.
  11. // The driver must be derived from SdSpiBaseClass.
  12. // See: SdFat/src/SpiDriver/SdSpiBaseClass.h
  13. class MySpiClass : public SdSpiBaseClass {
  14. public:
  15. // Activate SPI hardware with correct speed and mode.
  16. void activate() {
  17. SPI.beginTransaction(m_spiSettings);
  18. }
  19. // Initialize the SPI bus.
  20. void begin(SdSpiConfig config) {
  21. (void)config;
  22. SPI.begin();
  23. }
  24. // Deactivate SPI hardware.
  25. void deactivate() {
  26. SPI.endTransaction();
  27. }
  28. // Receive a byte.
  29. uint8_t receive() {
  30. return SPI.transfer(0XFF);
  31. }
  32. // Receive multiple bytes.
  33. // Replace this function if your board has multiple byte receive.
  34. uint8_t receive(uint8_t* buf, size_t count) {
  35. for (size_t i = 0; i < count; i++) {
  36. buf[i] = SPI.transfer(0XFF);
  37. }
  38. return 0;
  39. }
  40. // Send a byte.
  41. void send(uint8_t data) {
  42. SPI.transfer(data);
  43. }
  44. // Send multiple bytes.
  45. // Replace this function if your board has multiple byte send.
  46. void send(const uint8_t* buf, size_t count) {
  47. for (size_t i = 0; i < count; i++) {
  48. SPI.transfer(buf[i]);
  49. }
  50. }
  51. // Save SPISettings for new max SCK frequency
  52. void setSckSpeed(uint32_t maxSck) {
  53. m_spiSettings = SPISettings(maxSck, MSBFIRST, SPI_MODE0);
  54. }
  55. private:
  56. SPISettings m_spiSettings;
  57. } mySpi;
  58. #define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SD_SCK_MHZ(50), &mySpi)
  59. SdFat sd;
  60. //------------------------------------------------------------------------------
  61. void setup() {
  62. Serial.begin(9600);
  63. if (!sd.begin(SD_CONFIG)) {
  64. sd.initErrorHalt(&Serial);
  65. }
  66. sd.ls(&Serial, LS_SIZE);
  67. }
  68. //------------------------------------------------------------------------------
  69. void loop() {}
  70. #else // SPI_DRIVER_SELECT
  71. #error SPI_DRIVER_SELECT must be three in SdFat/SdFatConfig.h
  72. #endif // SPI_DRIVER_SELECT