OpenModem/hardware/SD.c

62 lines
1.5 KiB
C
Raw Normal View History

2019-01-27 12:25:11 -07:00
#include "SD.h"
FATFS sdfs; // FatFs work area
void sd_init(void) {
SPI_DDR |= _BV(SPI_MOSI) | _BV(SPI_CLK);
SPI_DDR &= ~(_BV(SPI_MISO));
SPCR = (1<<SPE) | (1<<MSTR) | (1<<SPR0);
SD_CS_DDR |= _BV(SD_CS_PIN);
SD_DETECT_DDR &= ~_BV(SD_DETECT_PIN);
SD_DETECT_PORT |= _BV(SD_DETECT_PIN); // Enable pull-up
}
// TODO: Remove this
void sd_test(void) {
2019-01-27 13:00:22 -07:00
printf("Testing SD card functions, waiting for card...\r\n");
2019-01-27 12:25:11 -07:00
FRESULT res = 0xFF;
while (res != 0) {
res = f_mount(&sdfs, "", 1);
printf("SD Detect: %d\r\n", (SD_DETECT_INPUT & _BV(SD_DETECT_PIN)));
printf("Res: %d\r\n", res);
delay_ms(500);
}
char str[12];
DWORD sn;
f_getlabel("", str, &sn);
printf("Label: %s, SN: %lu\r\n", str, sn);
FIL fil;
char line[100];
FRESULT fr;
fr = f_open(&fil, "file1.txt", FA_READ);
2019-01-27 13:00:22 -07:00
printf("File open result: %d\r\nContents:\r\n", fr);
2019-01-27 12:25:11 -07:00
while (f_gets(line, sizeof line, &fil)) {
printf(line);
}
2019-01-27 13:00:22 -07:00
printf("\r\n");
2019-01-27 12:25:11 -07:00
f_close(&fil);
printf("Returning from SD test\r\n");
}
// TODO: Get time from RTC or host
2019-01-27 13:00:22 -07:00
DWORD get_fattime (void)
{
// Returns current time packed into a DWORD variable
return ((DWORD)(2013 - 1980) << 25) // Year 2013
| ((DWORD)8 << 21) // Month 7
| ((DWORD)2 << 16) // Mday 28
| ((DWORD)20 << 11) // Hour 0..24
| ((DWORD)30 << 5) // Min 0
| ((DWORD)0 >> 1); // Sec 0
}