sdcard.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef SDCARD_H
  2. #define SDCARD_H
  3. #include "fw.h"
  4. #include "io.h"
  5. #include "irq.h"
  6. #include "ff.h"
  7. #include "diskio.h"
  8. #define SECTOR_SHIFT 9
  9. #define SECTOR_SIZE (1UL << SECTOR_SHIFT)
  10. struct sdcard_csd {
  11. uint32_t raw[4];
  12. };
  13. struct sdcard_cid {
  14. uint32_t raw[4];
  15. };
  16. struct sdcard_info {
  17. DSTATUS status; /* Physical status */
  18. DSTATUS fsstatus; /* Filesystem visible status */
  19. int8_t card_type;
  20. unsigned long lbasize;
  21. uint32_t if_cond;
  22. uint32_t ocr;
  23. struct sdcard_csd csd;
  24. struct sdcard_cid cid;
  25. };
  26. extern struct sdcard_info sdc;
  27. typedef unsigned int sector_t;
  28. extern int sdcard_read_sectors(void *, sector_t, int);
  29. extern int sdcard_write_sectors(const void *, sector_t, int);
  30. extern DSTATUS sdcard_init(void);
  31. extern DSTATUS sdcard_present_poll(void);
  32. static inline void sd_set_mode(uint8_t divisor, bool cs)
  33. {
  34. waitfor(SDCARD_IRQ);
  35. SDCARD_CTL_SPEED = (divisor - 1) | (cs << 7);
  36. }
  37. /* Read/write SD card and start transaction */
  38. enum sd_data_flags {
  39. SD_B0 = 0x00, /* Byte offset 0 */
  40. SD_B1 = 0x01, /* Byte offset 1 */
  41. SD_B2 = 0x02, /* Byte offset 2 */
  42. SD_B3 = 0x03, /* Byte offset 3 */
  43. SD_GO8 = 0x04, /* Start 8-bit transaction */
  44. SD_GO16 = 0x08, /* Start 16-bit transaction */
  45. SD_GO32 = 0x0c, /* Start 32-bit transaction */
  46. SD_BE = 0x10, /* Bigendian data */
  47. SD_DATA = 0x20, /* Data register access (assumed) */
  48. SD_CLEARCRC = 0x40 /* Clear CRC registers */
  49. };
  50. static inline void sd_writeb(uint8_t d, enum sd_data_flags flags)
  51. {
  52. waitfor(SDCARD_IRQ);
  53. flags ^= (flags & SD_BE) ? 3 : 0;
  54. *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  55. }
  56. static inline void sd_writeh(uint16_t d, enum sd_data_flags flags)
  57. {
  58. waitfor(SDCARD_IRQ);
  59. flags ^= (flags & SD_BE) ? 2 : 0;
  60. *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  61. }
  62. static inline void sd_writel(uint32_t d, enum sd_data_flags flags)
  63. {
  64. waitfor(SDCARD_IRQ);
  65. *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  66. }
  67. static inline uint8_t sd_readb(enum sd_data_flags flags)
  68. {
  69. waitfor(SDCARD_IRQ);
  70. flags ^= (flags & SD_BE) ? 0 : 3;
  71. return *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  72. }
  73. static inline uint16_t sd_readh(enum sd_data_flags flags)
  74. {
  75. waitfor(SDCARD_IRQ);
  76. flags ^= (flags & SD_BE) ? 0 : 2;
  77. return *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  78. }
  79. static inline uint32_t sd_readl(enum sd_data_flags flags)
  80. {
  81. waitfor(SDCARD_IRQ);
  82. return *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  83. }
  84. static inline uint8_t sd_crc7_rd(void)
  85. {
  86. waitfor(SDCARD_IRQ);
  87. return SDCARD_CRC7_RD;
  88. }
  89. static inline uint8_t sd_crc7_wr(void)
  90. {
  91. waitfor(SDCARD_IRQ);
  92. return SDCARD_CRC7_WR;
  93. }
  94. static inline uint16_t sd_crc16_rd(void)
  95. {
  96. waitfor(SDCARD_IRQ);
  97. return SDCARD_CRC16_RD;
  98. }
  99. static inline uint16_t sd_crc16_wr(void)
  100. {
  101. waitfor(SDCARD_IRQ);
  102. return SDCARD_CRC16_WR;
  103. }
  104. #endif /* SDCARD_H */