Mirror of CollapseOS
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.

185 lines
4.8KB

  1. #include <stdio.h>
  2. #include "sdc.h"
  3. // Add data to crc with polynomial 0x1021
  4. // https://stackoverflow.com/a/23726131
  5. static uint16_t crc16(uint16_t crc, uint8_t data)
  6. {
  7. uint8_t x = crc >> 8 ^ data;
  8. x ^= x>>4;
  9. crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x <<5)) ^ ((uint16_t)x);
  10. return crc;
  11. }
  12. void sdc_init(SDC *sdc)
  13. {
  14. sdc->initstat = 0;
  15. sdc->recvidx = 0;
  16. sdc->sendidx = -1;
  17. sdc->fp = NULL;
  18. sdc->cmd17bytes = -1;
  19. sdc->cmd24bytes = -2;
  20. }
  21. byte sdc_spix(SDC *sdc, byte val)
  22. {
  23. byte resp = 0xff;
  24. if (sdc->initstat < 8) {
  25. // not woken yet.
  26. sdc->initstat++;
  27. return resp;
  28. }
  29. if (sdc->sendidx >= 0) {
  30. resp = sdc->sendbuf[sdc->sendidx++];
  31. if (sdc->sendidx == 5) {
  32. sdc->sendidx = -1;
  33. }
  34. return resp;
  35. }
  36. if (sdc->cmd17bytes >= 0) {
  37. if (sdc->fp) {
  38. resp = getc(sdc->fp);
  39. }
  40. sdc->crc16 = crc16(sdc->crc16, resp);
  41. sdc->cmd17bytes++;
  42. if (sdc->cmd17bytes == 512) {
  43. sdc->sendbuf[3] = sdc->crc16 >> 8;
  44. sdc->sendbuf[4] = sdc->crc16 & 0xff;
  45. sdc->sendidx = 3;
  46. sdc->cmd17bytes = -1;
  47. }
  48. return resp;
  49. }
  50. if (sdc->cmd24bytes == -1) {
  51. if (val == 0xff) {
  52. // it's ok to receive idle bytes before the data token.
  53. return resp;
  54. }
  55. if (val == 0xfe) {
  56. // data token, good
  57. sdc->cmd24bytes = 0;
  58. } else {
  59. // something is wrong, cancel cmd24
  60. sdc->cmd24bytes = -2;
  61. }
  62. return resp;
  63. }
  64. if (sdc->cmd24bytes >= 0) {
  65. if (sdc->cmd24bytes < 512) {
  66. if (sdc->fp) {
  67. putc(val, sdc->fp);
  68. }
  69. sdc->crc16 = crc16(sdc->crc16, val);
  70. } else if (sdc->cmd24bytes == 512) {
  71. // CRC MSB
  72. if (val == (sdc->crc16>>8)) {
  73. fprintf(stderr, "Good CRC16 MSB\n");
  74. } else {
  75. fprintf(stderr, "Bad CRC16 MSB\n");
  76. }
  77. } else {
  78. if (val == (sdc->crc16&0xff)) {
  79. fprintf(stderr, "Good CRC16 LSB\n");
  80. } else {
  81. fprintf(stderr, "Bad CRC16 LSB\n");
  82. }
  83. // valid response for CMD24
  84. sdc->sendbuf[4] = 0x05;
  85. sdc->sendidx = 4;
  86. sdc->cmd24bytes = -3;
  87. }
  88. sdc->cmd24bytes++;
  89. return resp;
  90. }
  91. if ((sdc->recvidx == 0) && ((val > 0x7f) || (val < 0x40))) {
  92. // not a command
  93. return resp;
  94. }
  95. sdc->recvbuf[sdc->recvidx++] = val;
  96. if (sdc->recvidx < 6) {
  97. // incomplete command
  98. return resp;
  99. }
  100. // Command complete
  101. val &= 0x3f;
  102. sdc->recvidx = 0;
  103. uint8_t *b = sdc->recvbuf;
  104. uint8_t cmd = b[0] & 0x3f;
  105. uint16_t arg1 = (b[1] << 8) | b[2];
  106. uint16_t arg2 = (b[3] << 8) | b[4];
  107. // printf("cmd %02x %04x %04x\n", cmd, arg1, arg2);
  108. if (sdc->initstat == 8) {
  109. // At this stage, we're expecting CMD0
  110. if (cmd == 0) {
  111. sdc->initstat++;
  112. sdc->sendbuf[4] = 0x01;
  113. sdc->sendidx = 4;
  114. }
  115. return resp;
  116. }
  117. if (sdc->initstat == 9) {
  118. // At this stage, we're expecting CMD8 with 0x1aa arg2
  119. if ((cmd == 8) && (arg2 == 0x01aa)) {
  120. sdc->initstat++;
  121. sdc->sendbuf[0] = 0x01;
  122. sdc->sendbuf[1] = 0;
  123. sdc->sendbuf[2] = 0;
  124. sdc->sendbuf[3] = 0x01;
  125. sdc->sendbuf[4] = 0xaa;
  126. sdc->sendidx = 0;
  127. } else {
  128. sdc-> initstat = 8;
  129. }
  130. return resp;
  131. }
  132. if (sdc->initstat == 10) {
  133. // At this stage, we're expecting CMD55
  134. if (cmd == 55) {
  135. sdc->initstat++;
  136. sdc->sendbuf[4] = 0x01;
  137. sdc->sendidx = 4;
  138. } else {
  139. sdc->initstat = 8;
  140. }
  141. return resp;
  142. }
  143. if (sdc->initstat == 11) {
  144. // At this stage, we're expecting CMD41
  145. if ((cmd == 41) && (arg1 == 0x4000)) {
  146. sdc->initstat++;
  147. sdc->sendbuf[4] = 0x00;
  148. sdc->sendidx = 4;
  149. } else {
  150. sdc->initstat = 8;
  151. }
  152. return resp;
  153. }
  154. // We have a fully initialized card.
  155. if (cmd == 17) {
  156. if (sdc->fp) {
  157. fseek(sdc->fp, arg2*512, SEEK_SET);
  158. }
  159. sdc->sendbuf[3] = 0x00;
  160. // data token
  161. sdc->sendbuf[4] = 0xfe;
  162. sdc->sendidx = 3;
  163. sdc->cmd17bytes = 0;
  164. sdc->crc16 = 0;
  165. return resp;
  166. }
  167. if (cmd == 24) {
  168. if (sdc->fp) {
  169. fseek(sdc->fp, arg2*512, SEEK_SET);
  170. }
  171. sdc->sendbuf[4] = 0x00;
  172. sdc->sendidx = 4;
  173. sdc->cmd24bytes = -1;
  174. sdc->crc16 = 0;
  175. return resp;
  176. }
  177. // Simulate success for any unknown command.
  178. sdc->sendbuf[4] = 0x00;
  179. sdc->sendidx = 4;
  180. return resp;
  181. }