Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

172 lines
4.2KB

  1. /* Optimized SD Library for Teensy 3.X
  2. * Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this SD library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing genuine Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #if defined(__arm__)
  27. #include "SD_t3.h"
  28. #ifdef USE_TEENSY3_OPTIMIZED_CODE
  29. volatile uint8_t * SDClass::csreg;
  30. uint8_t SDClass::csmask;
  31. uint8_t SDClass::card_type;
  32. #define CMD0_GO_IDLE_STATE 0x4095 // arg=0
  33. #define CMD1_SEND_OP_COND 0x41FF
  34. #define CMD8_SEND_IF_COND 0x4887 // arg=0x1AA
  35. #define CMD55_APP_CMD 0x77FF
  36. #define ACMD41_SD_SEND_OP_COND 0x69FF
  37. #define CMD58_READ_OCR 0x7AFF
  38. #define CMD17_READ_SINGLE_BLOCK 0x51FF
  39. #define CMD24_WRITE_BLOCK 0x58FF
  40. uint8_t SDClass::sd_cmd0()
  41. {
  42. send_cmd(CMD0_GO_IDLE_STATE, 0);
  43. uint8_t r1 = recv_r1();
  44. end_cmd();
  45. return r1;
  46. }
  47. uint32_t SDClass::sd_cmd8()
  48. {
  49. send_cmd(CMD8_SEND_IF_COND, 0x1AA);
  50. uint8_t r1 = recv_r1();
  51. uint32_t cond = 0x80000000;
  52. if (r1 == 1) {
  53. cond = recv_r3_or_r7();
  54. //Serial.print(cond, HEX);
  55. }
  56. end_cmd();
  57. return cond;
  58. }
  59. uint8_t SDClass::sd_acmd41(uint32_t hcs)
  60. {
  61. //Serial.print("acmd41:");
  62. send_cmd(CMD55_APP_CMD, 0);
  63. uint8_t r1 = recv_r1();
  64. end_cmd();
  65. send_cmd(ACMD41_SD_SEND_OP_COND, hcs);
  66. r1 = recv_r1();
  67. end_cmd();
  68. return r1;
  69. }
  70. uint32_t SDClass::sd_cmd58(void)
  71. {
  72. send_cmd(CMD58_READ_OCR, 0);
  73. uint8_t r1 = recv_r1();
  74. uint32_t ocr = 0;
  75. if (r1 == 0) ocr = recv_r3_or_r7();
  76. end_cmd();
  77. return ocr;
  78. }
  79. bool SDClass::sd_read(uint32_t addr, void * data)
  80. {
  81. //Serial.printf("sd_read %ld\n", addr);
  82. if (card_type < 2) addr = addr << 9;
  83. send_cmd(CMD17_READ_SINGLE_BLOCK, addr);
  84. uint8_t r1 = recv_r1();
  85. if (r1 != 0) {
  86. end_cmd();
  87. return false;
  88. }
  89. while (1) {
  90. uint8_t token = SPI.transfer(0xFF);
  91. //Serial.printf("t=%02X.", token);
  92. if (token == 0xFE) break;
  93. if (token != 0xFF) {
  94. end_cmd();
  95. return false;
  96. }
  97. // TODO: timeout
  98. }
  99. uint8_t *p = (uint8_t *)data;
  100. uint8_t *end = p + 510;
  101. SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
  102. SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
  103. while (p < end) {
  104. while (!(SPI0_SR & 0xF0)) ;
  105. SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
  106. uint32_t in = SPI0_POPR;
  107. *p++ = in >> 8;
  108. *p++ = in;
  109. }
  110. while (!(SPI0_SR & 0xF0)) ;
  111. uint32_t in = SPI0_POPR;
  112. *p++ = in >> 8;
  113. *p++ = in;
  114. while (!(SPI0_SR & 0xF0)) ;
  115. SPI0_POPR; // ignore crc
  116. DIRECT_WRITE_HIGH(csreg, csmask);
  117. SPI.transfer(0xFF);
  118. return true;
  119. // token = 0xFE
  120. // data, 512 bytes
  121. // crc, 2 bytes
  122. }
  123. void SDClass::send_cmd(uint16_t cmd, uint32_t arg)
  124. {
  125. DIRECT_WRITE_LOW(csreg, csmask);
  126. SPI.transfer(cmd >> 8);
  127. SPI.transfer16(arg >> 16);
  128. SPI.transfer16(arg);
  129. SPI.transfer(cmd);
  130. }
  131. uint8_t SDClass::recv_r1(void)
  132. {
  133. uint8_t ret, count=0;
  134. do {
  135. ret = SPI.transfer(0xFF);
  136. //Serial.print(ret);
  137. //Serial.print(".");
  138. if ((ret & 0x80) == 0) break;
  139. } while (++count < 9);
  140. return ret;
  141. }
  142. uint32_t SDClass::recv_r3_or_r7(void)
  143. {
  144. uint32_t r;
  145. r = SPI.transfer16(0xFFFF) << 16;
  146. r |= SPI.transfer16(0xFFFF);
  147. return r;
  148. }
  149. void SDClass::end_cmd(void)
  150. {
  151. DIRECT_WRITE_HIGH(csreg, csmask);
  152. SPI.transfer(0xFF);
  153. }
  154. #endif
  155. #endif