2
0

sdcard.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef SDCARD_H
  2. #define SDCARD_H
  3. #include "common.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 void sdcard_reset(void);
  31. extern DSTATUS sdcard_init(void);
  32. extern DSTATUS sdcard_present_poll(void);
  33. static inline void sd_set_mode(uint8_t divisor, bool cs)
  34. {
  35. waitfor(SDCARD_IRQ);
  36. SDCARD_CTL_SPEED = (divisor - 1) | (cs << 7);
  37. }
  38. /* Read/write SD card and start transaction */
  39. enum sd_data_flags {
  40. SD_B0 = 0x00, /* Byte offset 0 */
  41. SD_B1 = 0x01, /* Byte offset 1 */
  42. SD_B2 = 0x02, /* Byte offset 2 */
  43. SD_B3 = 0x03, /* Byte offset 3 */
  44. SD_GO8 = 0x04, /* Start 8-bit transaction */
  45. SD_GO16 = 0x08, /* Start 16-bit transaction */
  46. SD_GO32 = 0x0c, /* Start 32-bit transaction */
  47. SD_BE = 0x10, /* Bigendian data */
  48. SD_DATA = 0x20, /* Data register access (assumed) */
  49. SD_CLEARCRC = 0x40 /* Clear CRC registers */
  50. };
  51. static inline void sd_writeb(uint8_t d, enum sd_data_flags flags)
  52. {
  53. waitfor(SDCARD_IRQ);
  54. flags ^= (flags & SD_BE) ? 3 : 0;
  55. *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  56. }
  57. static inline void sd_writeh(uint16_t d, enum sd_data_flags flags)
  58. {
  59. waitfor(SDCARD_IRQ);
  60. flags ^= (flags & SD_BE) ? 2 : 0;
  61. *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  62. }
  63. static inline void sd_writel(uint32_t d, enum sd_data_flags flags)
  64. {
  65. waitfor(SDCARD_IRQ);
  66. *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
  67. }
  68. static inline uint8_t sd_readb(enum sd_data_flags flags)
  69. {
  70. waitfor(SDCARD_IRQ);
  71. flags ^= (flags & SD_BE) ? 0 : 3;
  72. return *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  73. }
  74. static inline uint16_t sd_readh(enum sd_data_flags flags)
  75. {
  76. waitfor(SDCARD_IRQ);
  77. flags ^= (flags & SD_BE) ? 0 : 2;
  78. return *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  79. }
  80. static inline uint32_t sd_readl(enum sd_data_flags flags)
  81. {
  82. waitfor(SDCARD_IRQ);
  83. return *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA);
  84. }
  85. static inline uint8_t sd_crc7_rd(void)
  86. {
  87. waitfor(SDCARD_IRQ);
  88. return SDCARD_CRC7_RD;
  89. }
  90. static inline uint8_t sd_crc7_wr(void)
  91. {
  92. waitfor(SDCARD_IRQ);
  93. return SDCARD_CRC7_WR;
  94. }
  95. static inline uint16_t sd_crc16_rd(void)
  96. {
  97. waitfor(SDCARD_IRQ);
  98. return SDCARD_CRC16_RD;
  99. }
  100. static inline uint16_t sd_crc16_wr(void)
  101. {
  102. waitfor(SDCARD_IRQ);
  103. return SDCARD_CRC16_WR;
  104. }
  105. #endif /* SDCARD_H */