You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 satır
3.0KB

  1. /**
  2. * Copyright (c) 2011-2019 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(ARDUINO_ARCH_APOLLO3)
  27. //------------------------------------------------------------------------------
  28. void SdSpiArduinoDriver::activate() {
  29. m_spi->beginTransaction(m_spiSettings);
  30. }
  31. //------------------------------------------------------------------------------
  32. void SdSpiArduinoDriver::begin(SdSpiConfig spiConfig) {
  33. if (spiConfig.spiPort) {
  34. m_spi = spiConfig.spiPort;
  35. } else {
  36. m_spi = &SPI;
  37. }
  38. m_spi->begin();
  39. }
  40. //------------------------------------------------------------------------------
  41. void SdSpiArduinoDriver::deactivate() {
  42. m_spi->endTransaction();
  43. }
  44. //------------------------------------------------------------------------------
  45. uint8_t SdSpiArduinoDriver::receive() {
  46. return m_spi->transfer(0XFF);
  47. }
  48. //------------------------------------------------------------------------------
  49. uint8_t SdSpiArduinoDriver::receive(uint8_t *buf, size_t count) {
  50. m_spi->transferIn(buf, count);
  51. return 0;
  52. }
  53. //------------------------------------------------------------------------------
  54. void SdSpiArduinoDriver::send(uint8_t data) {
  55. m_spi->transfer(data);
  56. }
  57. //------------------------------------------------------------------------------
  58. void SdSpiArduinoDriver::send(const uint8_t *buf, size_t count) {
  59. // If not a multiple of four. Command with CRC used six byte send.
  60. while (count%4) {
  61. send(*buf++);
  62. count--;
  63. }
  64. // Convert byte array to 4 byte array.
  65. uint32_t myArray[count/4]; // NOLINT
  66. for (int x = 0; x < count/4; x++) {
  67. myArray[x] = ((uint32_t)buf[(x * 4) + 3] << (8 * 3)) |
  68. ((uint32_t)buf[(x * 4) + 2] << (8 * 2)) |
  69. ((uint32_t)buf[(x * 4) + 1] << (8 * 1)) |
  70. ((uint32_t)buf[(x * 4) + 0] << (8 * 0));
  71. }
  72. m_spi->transfer(reinterpret_cast<void *>(myArray), count);
  73. }
  74. #endif // defined(SD_USE_CUSTOM_SPI) && defined(ARDUINO_ARCH_APOLLO3)