選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

card_t3.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. //Serial.println(" sd_read fail r1");
  88. return false;
  89. }
  90. while (1) {
  91. uint8_t token = SPI.transfer(0xFF);
  92. //Serial.printf("t=%02X.", token);
  93. if (token == 0xFE) break;
  94. if (token != 0xFF) {
  95. end_cmd();
  96. //Serial.println(" sd_read fail token");
  97. return false;
  98. }
  99. // TODO: timeout
  100. }
  101. uint8_t *p = (uint8_t *)data;
  102. uint8_t *end = p + 510;
  103. SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
  104. SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
  105. while (p < end) {
  106. while (!(SPI0_SR & 0xF0)) ;
  107. SPI0_PUSHR = 0xFFFF | SPI_PUSHR_CTAS(1);
  108. uint32_t in = SPI0_POPR;
  109. *p++ = in >> 8;
  110. *p++ = in;
  111. }
  112. while (!(SPI0_SR & 0xF0)) ;
  113. uint32_t in = SPI0_POPR;
  114. *p++ = in >> 8;
  115. *p++ = in;
  116. while (!(SPI0_SR & 0xF0)) ;
  117. SPI0_POPR; // ignore crc
  118. DIRECT_WRITE_HIGH(csreg, csmask);
  119. SPI.transfer(0xFF);
  120. return true;
  121. // token = 0xFE
  122. // data, 512 bytes
  123. // crc, 2 bytes
  124. }
  125. void SDClass::send_cmd(uint16_t cmd, uint32_t arg)
  126. {
  127. DIRECT_WRITE_LOW(csreg, csmask);
  128. SPI.transfer(cmd >> 8);
  129. SPI.transfer16(arg >> 16);
  130. SPI.transfer16(arg);
  131. SPI.transfer(cmd);
  132. }
  133. uint8_t SDClass::recv_r1(void)
  134. {
  135. uint8_t ret, count=0;
  136. do {
  137. ret = SPI.transfer(0xFF);
  138. //Serial.print(ret);
  139. //Serial.print(".");
  140. if ((ret & 0x80) == 0) break;
  141. } while (++count < 9);
  142. return ret;
  143. }
  144. uint32_t SDClass::recv_r3_or_r7(void)
  145. {
  146. uint32_t r;
  147. r = SPI.transfer16(0xFFFF) << 16;
  148. r |= SPI.transfer16(0xFFFF);
  149. return r;
  150. }
  151. void SDClass::end_cmd(void)
  152. {
  153. DIRECT_WRITE_HIGH(csreg, csmask);
  154. SPI.transfer(0xFF);
  155. }
  156. #endif
  157. #endif