SdSpiParticle.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright (c) 2011-2021 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(PLATFORM_ID)
  27. static volatile bool SPI_DMA_TransferCompleted = false;
  28. //-----------------------------------------------------------------------------
  29. static void SD_SPI_DMA_TransferComplete_Callback() {
  30. SPI_DMA_TransferCompleted = true;
  31. }
  32. //------------------------------------------------------------------------------
  33. void SdSpiArduinoDriver::activate() {
  34. m_spi->beginTransaction(m_spiSettings);
  35. }
  36. //------------------------------------------------------------------------------
  37. void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
  38. if (spiConfig.spiPort) {
  39. m_spi = spiConfig.spiPort;
  40. } else {
  41. m_spi = &SPI;
  42. }
  43. m_spi->begin();
  44. }
  45. //------------------------------------------------------------------------------
  46. void SdSpiArduinoDriver::deactivate() {
  47. m_spi->endTransaction();
  48. }
  49. //------------------------------------------------------------------------------
  50. void SdSpiArduinoDriver::end() {
  51. m_spi->end();
  52. }
  53. //------------------------------------------------------------------------------
  54. uint8_t SdSpiArduinoDriver::receive() {
  55. return m_spi->transfer(0XFF);
  56. }
  57. //------------------------------------------------------------------------------
  58. uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
  59. SPI_DMA_TransferCompleted = false;
  60. m_spi->transfer(nullptr, buf, count, SD_SPI_DMA_TransferComplete_Callback);
  61. while (!SPI_DMA_TransferCompleted) {}
  62. return 0;
  63. }
  64. //------------------------------------------------------------------------------
  65. void SdSpiArduinoDriver::send(uint8_t data) {
  66. m_spi->transfer(data);
  67. }
  68. //------------------------------------------------------------------------------
  69. void SdSpiArduinoDriver::send(const uint8_t* buf , size_t count) {
  70. SPI_DMA_TransferCompleted = false;
  71. m_spi->transfer(const_cast<uint8_t*>(buf), nullptr, count,
  72. SD_SPI_DMA_TransferComplete_Callback);
  73. while (!SPI_DMA_TransferCompleted) {}
  74. }
  75. #endif // defined(SD_USE_CUSTOM_SPI) && defined(PLATFORM_ID)