mirror of
https://github.com/markqvist/OpenModem.git
synced 2025-05-14 20:32:23 -04:00
Added bluetooth and user I/O drivers
This commit is contained in:
parent
4df7a7d781
commit
f1a951e508
14 changed files with 541 additions and 58 deletions
168
hardware/SD.c
168
hardware/SD.c
|
@ -1,6 +1,16 @@
|
|||
#include "SD.h"
|
||||
|
||||
FATFS sdfs; // FatFs work area
|
||||
FATFS sdfs;
|
||||
|
||||
uint8_t sd_scheduler_timer = 0;
|
||||
uint8_t sd_status = SD_STATUS_UNKNOWN;
|
||||
bool sd_mountstate = false;
|
||||
|
||||
// Job flags
|
||||
bool sd_flag_needs_automount = false;
|
||||
bool sd_flag_needs_autounmount = false;
|
||||
bool sd_flag_was_automounted = false;
|
||||
bool sd_flag_was_autounmounted = false;
|
||||
|
||||
void sd_init(void) {
|
||||
SPI_DDR |= _BV(SPI_MOSI) | _BV(SPI_CLK);
|
||||
|
@ -12,48 +22,144 @@ void sd_init(void) {
|
|||
SD_DETECT_PORT |= _BV(SD_DETECT_PIN); // Enable pull-up
|
||||
}
|
||||
|
||||
// TODO: Remove this
|
||||
void sd_test(void) {
|
||||
printf("Testing SD card functions, waiting for card...\r\n");
|
||||
void sd_automounted_hook(void) {
|
||||
sd_statuschange_indication(1);
|
||||
}
|
||||
|
||||
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);
|
||||
void sd_autounmounted_hook(void) {
|
||||
sd_statuschange_indication(0);
|
||||
}
|
||||
|
||||
void sd_jobs(void) {
|
||||
if (sd_flag_needs_automount) {
|
||||
sd_flag_needs_automount = false;
|
||||
sd_automount();
|
||||
}
|
||||
|
||||
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);
|
||||
printf("File open result: %d\r\nContents:\r\n", fr);
|
||||
|
||||
|
||||
while (f_gets(line, sizeof line, &fil)) {
|
||||
printf(line);
|
||||
if (sd_flag_needs_autounmount) {
|
||||
sd_flag_needs_autounmount = false;
|
||||
sd_autounmount();
|
||||
}
|
||||
printf("\r\n");
|
||||
|
||||
f_close(&fil);
|
||||
if (sd_flag_was_automounted) {
|
||||
sd_flag_was_automounted = false;
|
||||
sd_automounted_hook();
|
||||
}
|
||||
|
||||
printf("Returning from SD test\r\n");
|
||||
if (sd_flag_was_autounmounted) {
|
||||
sd_flag_was_autounmounted = false;
|
||||
sd_autounmounted_hook();
|
||||
}
|
||||
}
|
||||
|
||||
void sd_statuschange_indication(uint8_t pattern) {
|
||||
if (pattern == 1) {
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
LED_STATUS_OFF();
|
||||
delay_ms(65);
|
||||
LED_STATUS_ON();
|
||||
delay_ms(30);
|
||||
}
|
||||
}
|
||||
|
||||
if (pattern == 0) {
|
||||
for (uint8_t i = 0; i < 2; i++) {
|
||||
LED_STATUS_OFF();
|
||||
delay_ms(250);
|
||||
LED_STATUS_ON();
|
||||
delay_ms(20);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool sd_inserted(void) {
|
||||
if (sd_status & SD_STATUS_NODISK) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool sd_card_ready(void) {
|
||||
if (sd_status == SD_STATUS_READY) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool sd_mounted(void) {
|
||||
return sd_mountstate;
|
||||
}
|
||||
|
||||
bool sd_mount(void) {
|
||||
if (sd_inserted()) {
|
||||
FRESULT mount_result = 0xFF;
|
||||
mount_result = f_mount(&sdfs, "", 1);
|
||||
|
||||
if (mount_result == 0) {
|
||||
sd_mountstate = true;
|
||||
|
||||
} else {
|
||||
sd_mountstate = false;
|
||||
}
|
||||
|
||||
return sd_mountstate;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool sd_unmount(void) {
|
||||
FRESULT unmount_result = 0xFF;
|
||||
unmount_result = f_mount(0, "", 0);
|
||||
|
||||
if (unmount_result == 0) {
|
||||
sd_mountstate = false;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void sd_automount(void) {
|
||||
if (sd_mount()) {
|
||||
sd_automounted_hook();
|
||||
}
|
||||
}
|
||||
|
||||
void sd_autounmount(void) {
|
||||
if (sd_unmount()) {
|
||||
sd_autounmounted_hook();
|
||||
}
|
||||
}
|
||||
|
||||
void sd_scheduler(void) {
|
||||
sd_scheduler_timer++;
|
||||
|
||||
disk_timerproc();
|
||||
sd_status = disk_status(0);
|
||||
|
||||
if (sd_scheduler_timer % 50 == 0) {
|
||||
sd_scheduler_timer = 0;
|
||||
|
||||
if (!sd_mounted() && sd_inserted()) {
|
||||
sd_flag_needs_automount = true;
|
||||
}
|
||||
|
||||
if (!sd_inserted() && sd_mounted()) {
|
||||
sd_flag_needs_autounmount = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Get time from RTC or host
|
||||
DWORD get_fattime (void)
|
||||
{
|
||||
// Returns current time packed into a DWORD variable
|
||||
return ((DWORD)(2013 - 1980) << 25) // Year 2013
|
||||
return ((DWORD)(2018 - 1980) << 25) // Year 2013
|
||||
| ((DWORD)8 << 21) // Month 7
|
||||
| ((DWORD)2 << 16) // Mday 28
|
||||
| ((DWORD)20 << 11) // Hour 0..24
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue