SdSpiSTM32Core.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. // Driver for: https://github.com/stm32duino/Arduino_Core_STM32
  26. #include "SdSpiDriver.h"
  27. #if defined(SD_USE_CUSTOM_SPI) && defined(STM32_CORE_VERSION)
  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. } else {
  37. m_spi = &SPI;
  38. }
  39. m_spi->begin();
  40. }
  41. //------------------------------------------------------------------------------
  42. void SdSpiArduinoDriver::deactivate() {
  43. m_spi->endTransaction();
  44. }
  45. //------------------------------------------------------------------------------
  46. void SdSpiArduinoDriver::end() {
  47. m_spi->end();
  48. }
  49. //------------------------------------------------------------------------------
  50. uint8_t SdSpiArduinoDriver::receive() {
  51. return m_spi->transfer(0XFF);
  52. }
  53. //------------------------------------------------------------------------------
  54. uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
  55. // Must send 0XFF - SD looks at send data for command.
  56. memset(buf, 0XFF, count);
  57. m_spi->transfer(buf, count);
  58. return 0;
  59. }
  60. //------------------------------------------------------------------------------
  61. void SdSpiArduinoDriver::send(uint8_t data) {
  62. m_spi->transfer(data);
  63. }
  64. //------------------------------------------------------------------------------
  65. void SdSpiArduinoDriver::send(const uint8_t* buf, size_t count) {
  66. // Avoid stack overflow if bad count. This should cause a write error.
  67. if (count > 512) {
  68. return;
  69. }
  70. // Not easy to avoid receive so use tmp RX buffer.
  71. uint8_t rxBuf[512];
  72. // Discard const - STM32 not const correct.
  73. m_spi->transfer(const_cast<uint8_t*>(buf), rxBuf, count);
  74. }
  75. #endif // defined(SD_USE_CUSTOM_SPI) && defined(STM32_CORE_VERSION)