SdSpiCardEX.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 20011-2017 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 "SdSpiCard.h"
  26. bool SdSpiCardEX::readBlock(uint32_t block, uint8_t* dst) {
  27. if (m_curState != READ_STATE || block != m_curBlock) {
  28. if (!syncBlocks()) {
  29. return false;
  30. }
  31. if (!SdSpiCard::readStart(block)) {
  32. return false;
  33. }
  34. m_curBlock = block;
  35. m_curState = READ_STATE;
  36. }
  37. if (!SdSpiCard::readData(dst)) {
  38. return false;
  39. }
  40. m_curBlock++;
  41. return true;
  42. }
  43. //-----------------------------------------------------------------------------
  44. bool SdSpiCardEX::readBlocks(uint32_t block, uint8_t* dst, size_t nb) {
  45. for (size_t i = 0; i < nb; i++) {
  46. if (!readBlock(block + i, dst + i*512UL)) {
  47. return false;
  48. }
  49. }
  50. return true;
  51. }
  52. //-----------------------------------------------------------------------------
  53. bool SdSpiCardEX::syncBlocks() {
  54. if (m_curState == READ_STATE) {
  55. m_curState = IDLE_STATE;
  56. if (!SdSpiCard::readStop()) {
  57. return false;
  58. }
  59. } else if (m_curState == WRITE_STATE) {
  60. m_curState = IDLE_STATE;
  61. if (!SdSpiCard::writeStop()) {
  62. return false;
  63. }
  64. }
  65. return true;
  66. }
  67. //-----------------------------------------------------------------------------
  68. bool SdSpiCardEX::writeBlock(uint32_t block, const uint8_t* src) {
  69. if (m_curState != WRITE_STATE || m_curBlock != block) {
  70. if (!syncBlocks()) {
  71. return false;
  72. }
  73. if (!SdSpiCard::writeStart(block)) {
  74. return false;
  75. }
  76. m_curBlock = block;
  77. m_curState = WRITE_STATE;
  78. }
  79. if (!SdSpiCard::writeData(src)) {
  80. return false;
  81. }
  82. m_curBlock++;
  83. return true;
  84. }
  85. //-----------------------------------------------------------------------------
  86. bool SdSpiCardEX::writeBlocks(uint32_t block,
  87. const uint8_t* src, size_t nb) {
  88. for (size_t i = 0; i < nb; i++) {
  89. if (!writeBlock(block + i, src + i*512UL)) {
  90. return false;
  91. }
  92. }
  93. return true;
  94. }