Long file names, and date-time set on creation from RTC.

This commit is contained in:
Peter Weingartner 2024-07-10 15:52:31 -04:00
parent a5531fb1be
commit fd80b729bd
3 changed files with 120 additions and 38 deletions

View file

@ -113,7 +113,7 @@
*/
#define FF_USE_LFN 0
#define FF_USE_LFN 1
#define FF_MAX_LFN 127
/* The FF_USE_LFN switches the support for LFN (long file name).
/
@ -170,7 +170,7 @@
/* Number of volumes (logical drives) to be used. (1-10) */
#define FF_STR_VOLUME_ID 1
#define FF_STR_VOLUME_ID 2
// #define FF_VOLUME_STRS "SD","SD2"
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
@ -237,7 +237,7 @@
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
#define FF_FS_NORTC 1
#define FF_FS_NORTC 0
#define FF_NORTC_MON 2
#define FF_NORTC_MDAY 1
#define FF_NORTC_YEAR 2024

View file

@ -93,3 +93,20 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
INFO1("disk_ioctl: %d", result);
return bdev_to_fatfs(result);
}
DWORD get_fattime(void) {
t_time time;
rtc_get_time(&time);
DWORD timestamp = (DWORD)(time.year - 1980) << 25 |
(DWORD)(time.month) << 21 |
(DWORD)time.day << 16 |
(DWORD)time.hour << 11 |
(DWORD)time.minute << 5 |
(DWORD)time.second >> 1;
INFO1("get_fattime %08lX", timestamp);
return timestamp;
}

View file

@ -305,65 +305,130 @@ void dump(uint8_t * buffer, int count) {
printf(" %s\n", char_buffer);
}
int main(int argc, char * argv[]) {
short result;
short i;
char message[256];
union fatfs_date_u {
struct {
unsigned int day : 5;
unsigned int month : 4;
unsigned int year : 7;
} s;
short date;
};
initialize();
union fatfs_time_u {
struct {
unsigned int second : 5;
unsigned int minute : 6;
unsigned int hour : 5;
} s;
short time;
};
short fd = fsys_opendir("0:/");
void print_fatfs_datetime(short date, short time) {
union fatfs_date_u fat_date;
union fatfs_time_u fat_time;
fat_date.date = date;
fat_time.time = time;
printf("%04d-%02d-%02d %02d:%02d ", fat_date.s.year + 1980, fat_date.s.month, fat_date.s.day, fat_time.s.hour, fat_time.s.minute);
}
void print_directory() {
printf("\nDirectory for /sd0/\n");
short fd = fsys_opendir("/sd0/");
if (fd > -1) {
INFO("fsys_opendir");
short result = fsys_readdir(fd, &dir);
while (result == 0) {
if (dir.name[0] != 0) {
printf("%s\n", dir.name);
} else {
while ((result == 0) && (dir.name[0] != 0)) {
if (dir.name[0] == 0) {
break;
} else {
if ((dir.attributes & FSYS_AM_SYS) == 0) {
print_fatfs_datetime(dir.date, dir.time);
printf(" %4ld ", dir.size);
if (dir.attributes & FSYS_AM_DIR) {
printf(" %s/\n", dir.name);
} else {
printf(" %s\n", dir.name);
}
}
result = fsys_readdir(fd, &dir);
}
}
fsys_closedir(fd);
INFO("fsys_closedir");
} else {
ERROR1("Could not open directory %d", fd);
}
}
// kbd_init();
// printf("\n> ");
// chan_ioctrl(0, CON_IOCTRL_ECHO_OFF, 0, 0);
// while (!kbd_break()) {
// char c = chan_read_b(0);
// if (c != 0) {
// chan_write_b(0, c);
// }
// }
void create_sample_file(const char * path) {
printf("\nTrying to create: %s\n", path);
short fd = fsys_open(path, FSYS_CREATE_ALWAYS | FSYS_WRITE);
if (fd > 0) {
char message[80];
printf("Got channel #%d\n", fd);
sprintf(message, "Hello, world!\n");
short result = chan_write(fd, (uint8_t *)message, strlen(message));
printf("Wrote %d characters.\n", result);
fsys_close(fd);
// INFO("bdev_init");
// short status = bdev_init(BDEV_SD0);
// if (status) {
// ERROR1("bdev_init returned %d", status);
// }
// INFO("bdev_read");
// status = bdev_read(BDEV_SD0, 97, buffer, 512);
// if (status < 512) {
// ERROR1("bdev_read returned %d", status);
// }
// INFO("Read.");
// dump(buffer, 512);
} else {
printf("Could not create file: %d\n", fd);
}
}
void read_sample_file(const char * path) {
printf("\nContents of %s:\n", path);
short fd = fsys_open(path, FSYS_READ);
if (fd >= 0) {
short c = 0;
short status;
do {
c = chan_read_b(fd);
chan_write_b(0, (uint8_t)c);
status = chan_status(fd);
} while ((status & CDEV_STAT_EOF) == 0);
chan_close(fd);
// printf("\nDone.\n");
} else {
printf("Could not open file: %d\n", fd);
}
}
int main(int argc, char * argv[]) {
short result;
short i;
char message[256];
initialize();
print_directory();
printf("\nfsys_rename(\"/sd0/hello.txt\", \"/sd0/renamed.txt\")");
fsys_rename("/sd0/hello.txt", "/sd0/renamed.txt");
print_directory();
printf("\nfsys_delete(\"/sd0/renamed.txt\")");
fsys_delete("/sd0/renamed.txt");
print_directory();
printf("\nCreating /sd0/hello.txt\n");
create_sample_file("/sd0/hello.txt");
print_directory();
read_sample_file("/sd0/test.txt");
read_sample_file("/sd0/hello.txt");
// Attempt to start up the user code
// log(LOG_INFO, "Looking for user startup code:");
// boot_launch();
INFO("Done.");
printf("Done.\n");
#ifdef _CALYPSI_MCP_DEBUGGER
extern int CalypsiDebugger(void);