sdcard.h 2.8 KB

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