1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #ifndef SDCARD_H
- #define SDCARD_H
- #include "io.h"
- #include "irq.h"
- static inline void sd_set_mode(uint8_t divisor, bool cs)
- {
- waitfor(SDCARD_IRQ);
- SDCARD_CTL_SPEED = (divisor - 1) | (cs << 7);
- }
- /* Read/write SD card and start transaction */
- enum sd_data_flags {
- SD_B0 = 0x00, /* Byte offset 0 */
- SD_B1 = 0x01, /* Byte offset 1 */
- SD_B2 = 0x02, /* Byte offset 2 */
- SD_B3 = 0x03, /* Byte offset 3 */
- SD_GO8 = 0x04, /* Start 8-bit transaction */
- SD_GO16 = 0x08, /* Start 16-bit transaction */
- SD_GO32 = 0x0c, /* Start 32-bit transaction */
- SD_BE = 0x10, /* Bigendian data */
- SD_DATA = 0x20, /* Data register access (assumed) */
- SD_CLEARCRC = 0x40 /* Clear CRC registers */
- };
- static inline void sd_writeb(uint8_t d, enum sd_data_flags flags)
- {
- waitfor(SDCARD_IRQ);
- flags ^= (flags & SD_BE) ? 3 : 0;
- *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
- }
- static inline void sd_writeh(uint16_t d, enum sd_data_flags flags)
- {
- waitfor(SDCARD_IRQ);
- flags ^= (flags & SD_BE) ? 2 : 0;
- *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
- }
- static inline void sd_writel(uint32_t d, enum sd_data_flags flags)
- {
- waitfor(SDCARD_IRQ);
- *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA) = d;
- }
- static inline uint8_t sd_readb(enum sd_data_flags flags)
- {
- waitfor(SDCARD_IRQ);
- flags ^= (flags & SD_BE) ? 0 : 3;
- return *(volatile uint8_t *)IODEVA(SDCARD,0,flags | SD_DATA);
- }
- static inline uint16_t sd_readh(enum sd_data_flags flags)
- {
- waitfor(SDCARD_IRQ);
- flags ^= (flags & SD_BE) ? 0 : 2;
- return *(volatile uint16_t *)IODEVA(SDCARD,0,flags | SD_DATA);
- }
- static inline uint32_t sd_readl(enum sd_data_flags flags)
- {
- waitfor(SDCARD_IRQ);
- return *(volatile uint32_t *)IODEVA(SDCARD,0,flags | SD_DATA);
- }
- static inline uint8_t sd_crc7_rd(void)
- {
- waitfor(SDCARD_IRQ);
- return SDCARD_CRC7_RD;
- }
- static inline uint8_t sd_crc7_wr(void)
- {
- waitfor(SDCARD_IRQ);
- return SDCARD_CRC7_WR;
- }
- static inline uint16_t sd_crc16_rd(void)
- {
- waitfor(SDCARD_IRQ);
- return SDCARD_CRC16_RD;
- }
- static inline uint8_t sd_crc16_wr(void)
- {
- waitfor(SDCARD_IRQ);
- return SDCARD_CRC16_WR;
- }
- #endif /* SDCARD_H */
|