SdSpiSTM32.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Driver for: https://github.com/rogerclarkmelbourne/Arduino_STM32
  26. #include "SdSpiDriver.h"
  27. #if defined(SD_USE_CUSTOM_SPI)\
  28. && (defined(__STM32F1__) || defined(__STM32F4__))
  29. #if defined(__STM32F1__)
  30. #define USE_STM32_DMA 1
  31. #elif defined(__STM32F4__)
  32. #define USE_STM32_DMA 1
  33. #else // defined(__STM32F1__)
  34. #error Unknown STM32 type
  35. #endif // defined(__STM32F1__)
  36. //------------------------------------------------------------------------------
  37. void SdSpiArduinoDriver::activate() {
  38. m_spi->beginTransaction(m_spiSettings);
  39. }
  40. //------------------------------------------------------------------------------
  41. void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
  42. if (spiConfig.spiPort) {
  43. m_spi = spiConfig.spiPort;
  44. } else {
  45. m_spi = &SPI;
  46. }
  47. m_spi->begin();
  48. }
  49. //------------------------------------------------------------------------------
  50. void SdSpiArduinoDriver::deactivate() {
  51. m_spi->endTransaction();
  52. }
  53. //------------------------------------------------------------------------------
  54. void SdSpiArduinoDriver::end() {
  55. m_spi->end();
  56. }
  57. //------------------------------------------------------------------------------
  58. uint8_t SdSpiArduinoDriver::receive() {
  59. return m_spi->transfer(0XFF);
  60. }
  61. //------------------------------------------------------------------------------
  62. uint8_t SdSpiArduinoDriver::receive(uint8_t* buf, size_t count) {
  63. #if USE_STM32_DMA
  64. return m_spi->dmaTransfer(nullptr, buf, count);
  65. #else // USE_STM32_DMA
  66. m_spi->read(buf, count);
  67. return 0;
  68. #endif // USE_STM32_DMA
  69. }
  70. //------------------------------------------------------------------------------
  71. void SdSpiArduinoDriver::send(uint8_t data) {
  72. m_spi->transfer(data);
  73. }
  74. //------------------------------------------------------------------------------
  75. void SdSpiArduinoDriver::send(const uint8_t* buf , size_t count) {
  76. #if USE_STM32_DMA
  77. m_spi->dmaTransfer(const_cast<uint8*>(buf), nullptr, count);
  78. #else // USE_STM32_DMA
  79. m_spi->write(const_cast<uint8*>(buf), count);
  80. #endif // USE_STM32_DMA
  81. }
  82. #endif // defined(SD_USE_CUSTOM_SPI) && defined(__STM32F1__)