SdSpiTeensy3.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /**
  2. * Copyright (c) 2011-2022 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "SdSpiDriver.h"
  26. #if defined(SD_USE_CUSTOM_SPI) && defined(__arm__) && defined(CORE_TEENSY)
  27. #define USE_BLOCK_TRANSFER 1
  28. //------------------------------------------------------------------------------
  29. void SdSpiArduinoDriver::activate() {
  30. m_spi->beginTransaction(m_spiSettings);
  31. }
  32. //------------------------------------------------------------------------------
  33. void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
  34. if (spiConfig.spiPort) {
  35. m_spi = spiConfig.spiPort;
  36. #if defined(SDCARD_SPI) && defined(SDCARD_SS_PIN)
  37. } else if (spiConfig.csPin == SDCARD_SS_PIN) {
  38. m_spi = &SDCARD_SPI;
  39. m_spi->setMISO(SDCARD_MISO_PIN);
  40. m_spi->setMOSI(SDCARD_MOSI_PIN);
  41. m_spi->setSCK(SDCARD_SCK_PIN);
  42. #endif // defined(SDCARD_SPI) && defined(SDCARD_SS_PIN)
  43. } else {
  44. m_spi = &SPI;
  45. }
  46. m_spi->begin();
  47. }
  48. //------------------------------------------------------------------------------
  49. void SdSpiArduinoDriver::deactivate() {
  50. m_spi->endTransaction();
  51. }
  52. //------------------------------------------------------------------------------
  53. void SdSpiArduinoDriver::end() {
  54. m_spi->end();
  55. }
  56. //------------------------------------------------------------------------------
  57. uint8_t SdSpiArduinoDriver::receive() {
  58. return m_spi->transfer(0XFF);
  59. }
  60. //------------------------------------------------------------------------------
  61. uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
  62. #if USE_BLOCK_TRANSFER
  63. memset(buf, 0XFF, count);
  64. m_spi->transfer(buf, count);
  65. #else // USE_BLOCK_TRANSFER
  66. for (size_t i = 0; i < count; i++) {
  67. buf[i] = m_spi->transfer(0XFF);
  68. }
  69. #endif // USE_BLOCK_TRANSFER
  70. return 0;
  71. }
  72. //------------------------------------------------------------------------------
  73. void SdSpiArduinoDriver::send(uint8_t data) {
  74. m_spi->transfer(data);
  75. }
  76. //------------------------------------------------------------------------------
  77. void SdSpiArduinoDriver::send(const uint8_t* buf , size_t count) {
  78. #if USE_BLOCK_TRANSFER
  79. uint32_t tmp[128];
  80. if (0 < count && count <= 512) {
  81. memcpy(tmp, buf, count);
  82. m_spi->transfer(tmp, count);
  83. return;
  84. }
  85. #endif // USE_BLOCK_TRANSFER
  86. for (size_t i = 0; i < count; i++) {
  87. m_spi->transfer(buf[i]);
  88. }
  89. }
  90. #endif // defined(SD_USE_CUSTOM_SPI) && defined(__arm__) &&defined(CORE_TEENSY)