BlinkenArea - GitList
Repositories
Blog
Wiki
flaneth
Code
Commits
Branches
Tags
Search
Tree:
80d3768
Branches
Tags
master
flaneth
firmware
main.c
added dosfs code to read FAT filesystems
Stefan Schuermans
commited
80d3768
at 2012-05-03 18:43:13
main.c
Blame
History
Raw
/* flaneth - flash and ethernet Copyright (C) 2007-2012 Stefan Schuermans <stefan@schuermans.info> Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html a BlinkenArea project - http://www.blinkenarea.org/ */ #include <avr/interrupt.h> #include <avr/wdt.h> #include <string.h> #include "arp.h" #include "bus.h" #include "cf.h" #include "debug.h" #include "dosfs.h" #include "eeprom.h" #include "http.h" #include "random.h" #include "rtl8019.h" #include "status.h" #include "tasks.h" #include "tcp.h" #include "timing.h" #include "uart.h" // try to work with CF card static void mainWorkCf(void) { unsigned long sectors; uint8_t sectorBuf[SECTOR_SIZE]; uint32_t partStart, partSize; uint8_t partAct, partType; VOLINFO vi; const char *sec_clus, *fstype; DIRINFO di; DIRENT de; uint8_t filename[12]; FILEINFO fi; uint8_t buf[16]; uint32_t len, i; // reset card if (CfReset() != 0) return; // identify card if (CfIdentify(§ors) != 0) return; // get start of first partition partStart = DFS_GetPtnStart(0, sectorBuf, 0, &partAct, &partType, &partSize); if (partStart == 0xFFFFFFFF) { debug_printf("cannot find first partition"); return; } debug_printf("partition: start %lu size %lu", partStart, partSize); // get volume info if (DFS_GetVolInfo(0, sectorBuf, partStart, &vi) != 0) { debug_printf("cannot get volume info"); return; } switch (vi.filesystem) { case FAT12: sec_clus = "sector: "; fstype = "FAT12"; break; case FAT16: sec_clus = "sector: "; fstype = "FAT16"; break; case FAT32: sec_clus = "cluster:"; fstype = "FAT32"; break; default: sec_clus = "sector: "; fstype = "unknown"; break; } debug_printf("filesystem: %s", fstype); debug_printf("volume label: %-11.11s", vi.label); debug_printf("sector/s per cluster: %hu", vi.secperclus); debug_printf("reserved sector/s: %hu", vi.reservedsecs); debug_printf("volume total sectors: %lu", vi.numsecs); debug_printf("sectors per FAT: %lu", vi.secperfat); debug_printf("first FAT at sector: %lu", vi.fat1); debug_printf("root dir at %s %lu", sec_clus, vi.rootdir); debug_printf("root dir entries: %hu", vi.rootentries); debug_printf("data area commences at sector: %lu", vi.dataarea); debug_printf("clusters in data area: %lu", vi.numclusters); debug_printf("sectors per cluster: %hu", vi.secperclus); // list files in root directory di.scratch = sectorBuf; if (DFS_OpenDir(&vi, (uint8_t *)"", &di) != 0) { debug_printf("cannot open root directory"); return; } debug_printf("files in root directory:"); while (DFS_GetNext(&vi, &di, &de) == 0) { if (de.attr == ATTR_LONG_NAME) { // ignore long names } else if (de.attr & ATTR_VOLUME_ID) { debug_printf("volume ID: %-11.11s", de.name); } else if (de.attr & ATTR_DIRECTORY) { debug_printf("directory: %-11.11s", de.name); } else { debug_printf("file: %-11.11s", de.name); memcpy(filename, de.name, 11); // remember last filename filename[11] = 0; } } // dump last file if (DFS_OpenFile(&vi, filename, DFS_READ, sectorBuf, &fi) != 0) { debug_printf("cannot open file %s", filename); return; } debug_printf("dumping file %s:", filename); while (fi.pointer < fi.filelen) { len = 0; DFS_ReadFile(&fi, sectorBuf, buf, &len, sizeof(buf)); for (i = 0; i < len; ++i) debug_printf_nolf(" %02X", buf[i]); debug_printf(""); } debug_printf("done", filename); } // wait for CF card and work with it static void mainWaitCf(void) { // wait for new card while (!CfIsPresent()) Tasks(); // work with card mainWorkCf(); // wait for failing card to be removed while (CfIsPresent()) Tasks(); } // main code entry point int main(void) { wdt_reset(); #ifdef DEBUG wdt_disable(); #else wdt_enable(WDTO_60MS); #endif wdt_reset(); // initialize uart to be able to use stdio UartInit(); debug_printf(""); debug_printf("flaneth"); debug_init_printf("init"); // initialize low level modules BusInit(); // initialize middle level modules CfInit(); RtlInit(); StatusInit(); TimingInit(); // initialize high level modules ArpInit(); HttpInit(); TcpInit(); // use entropy collected during initialization RandomTask(); debug_init_printf("config"); // get configuration from EEPROM EepromGetConfig(); // enable interrupts sei(); debug_init_printf("start"); // main loop while (1) mainWaitCf(); return 0; }