1 #include <AT91SAM7S64.h>
2 #include <interfaces/sd.h>
3 #include <efs-sdcard.h>
7 #define SPI_SPEED 10000000
9 #define SPI_TRANSFER (AT91C_PA12_MISO | AT91C_PA13_MOSI | AT91C_PA14_SPCK)
11 #define SPI_CS (AT91C_PA11_NPCS0)
13 static struct process *event_process =
NULL;
18 *AT91C_SPI_CR = AT91C_SPI_SPIDIS | AT91C_SPI_SWRST;
19 *AT91C_PMC_PCER = (1 << AT91C_ID_SPI);
20 *AT91C_PIOA_ASR = SPI_TRANSFER | SPI_CS;
21 *AT91C_PIOA_PDR = SPI_TRANSFER | SPI_CS;
22 *AT91C_PIOA_PPUER = AT91C_PA12_MISO | SPI_CS;
23 *AT91C_SPI_MR = (AT91C_SPI_MSTR | AT91C_SPI_PS_FIXED
30 ((((MCK+SPI_SPEED/2)/SPI_SPEED)<<8) | AT91C_SPI_CPOL
31 | AT91C_SPI_BITS_8 | AT91C_SPI_CSAAT);
32 *AT91C_SPI_CR = AT91C_SPI_SPIEN;
37 if_spiInit(hwInterface *iface)
40 *AT91C_PIOA_SODR = AT91C_PA11_NPCS0;
41 *AT91C_PIOA_PER = AT91C_PA11_NPCS0;
43 if_spiSend(iface, 0xff);
45 *AT91C_PIOA_PDR = AT91C_PA11_NPCS0;
51 if_initInterface(hwInterface* file, eint8* opts)
57 DBG((TXT(
"Card failed to init, breaking up...\n")));
62 DBG((TXT(
"Card didn't return the ready state, breaking up...\n")
69 sd_getDriveSize(file, &sc);
70 file->sectorCount = sc/512;
74 DBG((TXT(
"Card Capacity is %lu Bytes (%lu Sectors)\n"), sc, file->sectorCount));
83 if_readBuf(hwInterface* file,euint32 address,euint8* buf)
85 return(sd_readSector(file,address,buf,512));
89 if_writeBuf(hwInterface* file,euint32 address,euint8* buf)
91 return(sd_writeSector(file,address, buf));
95 if_setPos(hwInterface* file,euint32 address)
102 if_spiSend(hwInterface *iface, euint8 outgoing)
105 *AT91C_SPI_TDR = outgoing;
106 while(!(*AT91C_SPI_SR & AT91C_SPI_RDRF));
107 ingoing = *AT91C_SPI_RDR;
112 static EmbeddedFileSystem sdcard_efs;
114 PROCESS(sdcard_process,
"SD card process");
120 *AT91C_PIOA_PER = AT91C_PIO_PA20 | AT91C_PIO_PA1;
121 *AT91C_PIOA_ODR = AT91C_PIO_PA20 | AT91C_PIO_PA1;
125 sdcard_efs.myCard.sectorCount = 0;
129 if (!(*AT91C_PIOA_PDSR & AT91C_PA20_IRQ0)) {
130 if (sdcard_efs.myCard.sectorCount == 0) {
131 if (efs_init(&sdcard_efs,0) == 0) {
135 printf(
"SD card inserted\n");
137 printf(
"SD card insertion failed\n");
141 if (sdcard_efs.myCard.sectorCount != 0) {
143 fs_umount(&sdcard_efs.myFs);
144 sdcard_efs.myCard.sectorCount = 0;
148 printf(
"SD card removed\n");
154 ev == PROCESS_EVENT_TIMER);
155 if (ev == PROCESS_EVENT_EXIT)
break;
156 if (!(*AT91C_PIOA_PDSR & AT91C_PA20_IRQ0)) {
170 return &sdcard_efs.myFs;
176 static int initialized = 0;
188 return sdcard_efs.myCard.sectorCount > 0;
192 sdcard_event_process(
struct process *p)