Rev 349 | Rev 359 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 349 | Rev 353 | ||
---|---|---|---|
Line 60... | Line 60... | ||
60 | #include "timer1.h" |
60 | #include "timer1.h" |
61 | #include "fat16.h" |
61 | #include "fat16.h" |
62 | #include "sdc.h" |
62 | #include "sdc.h" |
63 | #include "uart1.h" |
63 | #include "uart1.h" |
Line 64... | Line -... | ||
64 | - | ||
65 | void Test(void); |
64 | |
66 | //________________________________________________________________________________________________________________________________________ |
65 | //________________________________________________________________________________________________________________________________________ |
67 | // Module name: fat16.c |
66 | // Module name: fat16.c |
68 | // Compiler used: avr-gcc 3.4.5 |
67 | // Compiler used: avr-gcc 3.4.5 |
69 | // Last Modifikation: 20.03.2010 |
68 | // Last Modifikation: 20.03.2010 |
Line 321... | Line 320... | ||
321 | Partition_t Partition; // Structure holds partition information |
320 | Partition_t Partition; // Structure holds partition information |
Line 322... | Line 321... | ||
322 | 321 | ||
Line -... | Line 322... | ||
- | 322 | File_t FilePointer[FILE_MAX_OPEN]; // Allocate Memmoryspace for each filepointer used. |
|
- | 323 | ||
323 | File_t FilePointer[FILE_MAX_OPEN]; // Allocate Memmoryspace for each filepointer used. |
324 | |
324 | 325 | ||
325 | 326 | ||
326 | /****************************************************************************************************************************************/ |
327 | /****************************************************************************************************************************************/ |
327 | /* Function: FileDate(DateTime_t *); */ |
328 | /* Function: FileDate(DateTime_t *); */ |
Line 448... | Line 449... | ||
448 | } |
449 | } |
Line 449... | Line 450... | ||
449 | 450 | ||
450 | // clear dirname with spaces |
451 | // clear dirname with spaces |
451 | dirname[11] = 0; // terminate dirname |
452 | dirname[11] = 0; // terminate dirname |
- | 453 | for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' '; |
|
- | 454 | ||
- | 455 | // handle the special dirnames "." and ".." seperately |
|
- | 456 | readpointer = 0; |
|
- | 457 | if(filepath[0] == '/') readpointer++; |
|
- | 458 | // if we are trying to enter directories "." or ".." |
|
- | 459 | if(filepath[readpointer] == '.') |
|
- | 460 | { |
|
- | 461 | // directory '.' |
|
- | 462 | if(filepath[readpointer+1] == 0) |
|
- | 463 | { |
|
- | 464 | dirname[0] = '.'; |
|
- | 465 | return((s8*)&filepath[readpointer]); |
|
- | 466 | } |
|
- | 467 | // directory '..' |
|
- | 468 | if((filepath[readpointer+1] == '.') && (filepath[readpointer+2] == 0)) |
|
- | 469 | { |
|
- | 470 | dirname[0] = '.'; |
|
- | 471 | dirname[1] = '.'; |
|
- | 472 | return((s8*)&filepath[readpointer]); |
|
- | 473 | } |
|
- | 474 | } |
|
452 | for(writepointer = 0; writepointer < 11; writepointer++) dirname[writepointer] = ' '; |
475 | |
453 | writepointer = 0; |
476 | writepointer = 0; |
454 | // start seperating the dirname from the filepath. |
477 | // start seperating the dirname from the filepath. |
455 | readpointer = 0; |
478 | readpointer = 0; |
456 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
479 | if(filepath[0] == '/') readpointer = 1; // ignore first '/' |
Line 660... | Line 683... | ||
660 | } |
683 | } |
661 | Partition.IsValid = 1; // mark data in partition structure as valid |
684 | Partition.IsValid = 1; // mark data in partition structure as valid |
662 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
685 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
663 | strcpy(Partition.PathToCwd,"/"); |
686 | strcpy(Partition.PathToCwd,"/"); |
664 | result = 0; |
687 | result = 0; |
665 | - | ||
666 | // Test(); |
- | |
667 | end: |
688 | end: |
668 | if(result != 0) Fat16_Deinit(); |
689 | if(result != 0) Fat16_Deinit(); |
669 | else UART1_PutString("ok"); |
690 | else UART1_PutString("ok"); |
670 | return(result); |
691 | return(result); |
671 | } |
692 | } |
Line 754... | Line 775... | ||
754 | } |
775 | } |
755 | return(cluster); |
776 | return(cluster); |
756 | } |
777 | } |
Line -... | Line 778... | ||
- | 778 | ||
757 | 779 | ||
758 | 780 | ||
759 | /****************************************************************************************************************************************/ |
781 | /****************************************************************************************************************************************/ |
760 | /* Function: FindNextFreeCluster(File_t *); */ |
782 | /* Function: FindNextFreeCluster(File_t *); */ |
761 | /* */ |
783 | /* */ |
Line 2080... | Line 2102... | ||
2080 | /********************************************************************************************************************************************/ |
2102 | /********************************************************************************************************************************************/ |
Line 2081... | Line 2103... | ||
2081 | 2103 | ||
2082 | u8 FindItem(Find_t *findelement) |
2104 | u8 FindItem(Find_t *findelement) |
2083 | { |
2105 | { |
- | 2106 | u16 index = 0; |
|
2084 | u16 index = 0; |
2107 | u16 max_dir_sector = 0; |
2085 | u16 end_of_directory_not_reached = 1; // the directory has been read completely without a result. |
2108 | u16 end_of_directory_not_reached = 1; // the directory has been read completely without a result. |
2086 | u8 i = 0; |
2109 | u8 i = 0; |
2087 | u8 readpointer = 0; |
2110 | u8 readpointer = 0; |
2088 | u8 writepointer = 0; |
2111 | u8 writepointer = 0; |
Line 2092... | Line 2115... | ||
2092 | 2115 | ||
2093 | file.FirstSectorOfCurrCluster = findelement->fp.FirstSectorOfCurrCluster; |
2116 | file.FirstSectorOfCurrCluster = findelement->fp.FirstSectorOfCurrCluster; |
2094 | file.SectorOfCurrCluster = findelement->fp.SectorOfCurrCluster; |
2117 | file.SectorOfCurrCluster = findelement->fp.SectorOfCurrCluster; |
Line -... | Line 2118... | ||
- | 2118 | index = findelement->fp.DirectoryIndex; |
|
- | 2119 | ||
- | 2120 | // within the root directory area we can read sectors sequentially until the end of this area |
|
- | 2121 | if((Partition.FirstRootDirSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster < Partition.FirstDataSector)) |
|
- | 2122 | { |
|
- | 2123 | max_dir_sector = (Partition.MaxRootEntries * DIRENTRY_SIZE)/BYTES_PER_SECTOR; |
|
- | 2124 | } |
|
- | 2125 | // within the data clusters we can read sectors sequentially only within the cluster |
|
- | 2126 | else if((Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) && (file.FirstSectorOfCurrCluster <= Partition.LastDataSector)) |
|
- | 2127 | { |
|
Line 2095... | Line 2128... | ||
2095 | index = findelement->fp.DirectoryIndex; |
2128 | max_dir_sector = Partition.SectorsPerCluster; // limit max secters before next cluster |
2096 | 2129 | } |
|
2097 | 2130 | ||
2098 | do |
2131 | do |
Line 2145... | Line 2178... | ||
2145 | findelement->fp.SectorOfCurrCluster = file.SectorOfCurrCluster; |
2178 | findelement->fp.SectorOfCurrCluster = file.SectorOfCurrCluster; |
2146 | findelement->fp.Size = DirectoryEntry[index].Size; |
2179 | findelement->fp.Size = DirectoryEntry[index].Size; |
2147 | findelement->fp.Attribute = DirectoryEntry[index].Attribute; |
2180 | findelement->fp.Attribute = DirectoryEntry[index].Attribute; |
2148 | retvalue = 1; |
2181 | retvalue = 1; |
2149 | } |
2182 | } |
- | 2183 | /* search the next sector */ |
|
2150 | index++; |
2184 | index++; |
2151 | } |
2185 | } |
- | 2186 | /* this sector has been searched but we havn't found what we are looking for. Therefore we have to find the next sector */ |
|
2152 | if(!retvalue) // file not found in this sector so take next sector. |
2187 | if(!retvalue) // file not found in this sector so take next sector. |
2153 | { |
2188 | { |
- | 2189 | /* in the next sector we start looking for the specified entry beginning at index 0 */ |
|
- | 2190 | index = 0; |
|
- | 2191 | /* there are still sectors to be read within the cluster or within the linear addresspace of the rootdirectory */ |
|
2154 | file.SectorOfCurrCluster++; |
2192 | if(file.SectorOfCurrCluster < max_dir_sector-1) file.SectorOfCurrCluster++; else end_of_directory_not_reached = 0; |
- | 2193 | /* if we are looking for an directoryentry outside the rootdirectory and have reached the end of the cluster we have to get the next one */ |
|
2155 | if(file.SectorOfCurrCluster >= Partition.SectorsPerCluster) |
2194 | if(Partition.FirstDataSector <= file.FirstSectorOfCurrCluster) |
2156 | { |
2195 | { |
2157 | file.SectorOfCurrCluster=0; |
- | |
2158 | end_of_directory_not_reached = GetNextCluster(&file); |
2196 | end_of_directory_not_reached = GetNextCluster(&file); |
2159 | } |
2197 | } |
2160 | } |
2198 | } |
2161 | index = 0; |
- | |
2162 | } |
2199 | } |
2163 | while((end_of_directory_not_reached) && (!retvalue)); |
2200 | while((end_of_directory_not_reached) && (!retvalue)); |
Line 2164... | Line 2201... | ||
2164 | 2201 | ||
2165 | return(retvalue); |
2202 | return(retvalue); |
Line -... | Line 2203... | ||
- | 2203 | } |
|
- | 2204 | ||
2166 | } |
2205 | |
2167 | 2206 | ||
2168 | 2207 | ||
2169 | /********************************************************************************************************************************************/ |
2208 | /********************************************************************************************************************************************/ |
2170 | /* Function: findnext_(Find_t *); */ |
2209 | /* Function: findnext_(Find_t *); */ |
Line 2193... | Line 2232... | ||
2193 | } |
2232 | } |
Line 2194... | Line 2233... | ||
2194 | 2233 | ||
2195 | 2234 | ||
2196 | 2235 | ||
2197 | /********************************************************************************************************************************************/ |
2236 | /********************************************************************************************************************************************/ |
2198 | /* Function: findfirst_(s8* filename, u8 attribfilter, u8 attribmask, Find_t *); */ |
2237 | /* Function: findfirst_(s8* filename, u8 attribmask, Find_t *); */ |
2199 | /* */ |
2238 | /* */ |
2200 | /* Description: This function looks for the first item in the specified directory with a matching filename and fileattributes */ |
2239 | /* Description: This function looks for the first item in the specified directory with a matching filename and fileattributes */ |
Line 2365... | Line 2404... | ||
2365 | void RemoveLastDirFromPath(void) |
2404 | void RemoveLastDirFromPath(void) |
2366 | { |
2405 | { |
2367 | /* a pointer to the beginning of the absolute path to the cwd */ |
2406 | /* a pointer to the beginning of the absolute path to the cwd */ |
2368 | s8 * cptr = Partition.PathToCwd; |
2407 | s8 * cptr = Partition.PathToCwd; |
2369 | /* lets find the end of the path to the cwd */ |
2408 | /* lets find the end of the path to the cwd */ |
2370 | while(*cptr++ != 0); |
2409 | while(*cptr != 0) cptr++; |
- | 2410 | /* if the path is terminated with an '/' */ |
|
- | 2411 | if((*(cptr-1)) == '/') *(cptr-1)=0; |
|
2371 | /* now lets find the beginning of the last directorientry */ |
2412 | /* now lets find the beginning of the last directorientry */ |
- | 2413 | while((*cptr != '/') && cptr > Partition.PathToCwd) cptr--; |
|
- | 2414 | /* is there one subdirectory left within the path? */ |
|
2372 | while(*cptr-- != '/'); |
2415 | if(cptr > Partition.PathToCwd) |
- | 2416 | { |
|
2373 | /* we delete the direntry by terminating the path with 0 */ |
2417 | /* we delete the direntry by terminating the path with 0 */ |
2374 | *++cptr = 0; |
2418 | *cptr = 0; |
- | 2419 | } |
|
- | 2420 | /* there is no subdirectory left within the path. Therefore we create the root instead. */ |
|
- | 2421 | else |
|
- | 2422 | { |
|
- | 2423 | *cptr = '/'; |
|
- | 2424 | *(cptr+1) = 0; |
|
- | 2425 | } |
|
2375 | } |
2426 | } |
Line 2376... | Line 2427... | ||
2376 | 2427 | ||
2377 | /********************************************************************************************************************************************/ |
2428 | /********************************************************************************************************************************************/ |
2378 | /* Function: chdir_(s8* filepath); */ |
2429 | /* Function: chdir_(s8* filepath); */ |
Line 2406... | Line 2457... | ||
2406 | 2457 | ||
2407 | /* lets remember the actual path */ |
2458 | /* lets remember the actual path */ |
2408 | strcpy(tp, Partition.PathToCwd); |
2459 | strcpy(tp, Partition.PathToCwd); |
2409 | cwdt = Partition.CurrentWorkingDirectory; |
2460 | cwdt = Partition.CurrentWorkingDirectory; |
- | 2461 | /* if the path is absolute we begin at the rootdirectory */ |
|
- | 2462 | if(path[0] == '/') |
|
2410 | /* if the path is absolute we begin at the rootdirectory */ |
2463 | { |
- | 2464 | strcpy(Partition.PathToCwd, "/"); |
|
- | 2465 | Partition.CurrentWorkingDirectory = Partition.FirstRootDirSector; |
|
2411 | strcpy(Partition.PathToCwd, "/"); |
2466 | } |
2412 | /* how many subdirectories are there within the path? */ |
2467 | /* how many subdirectories are there within the path? */ |
2413 | dircount = GetDirCount(path); |
2468 | dircount = GetDirCount(path); |
2414 | /* now we parse through all the subdirectories within the path */ |
2469 | /* now we parse through all the subdirectories within the path */ |
2415 | do |
2470 | do |
Line 2451... | Line 2506... | ||
2451 | strcpy(Partition.PathToCwd, tp); |
2506 | strcpy(Partition.PathToCwd, tp); |
2452 | } |
2507 | } |
2453 | return(retvalue); |
2508 | return(retvalue); |
2454 | } |
2509 | } |
Line 2455... | Line -... | ||
2455 | - | ||
2456 | /********************************************************************************************************************************************/ |
- | |
2457 | /* Function: void Test(void); */ |
- | |
2458 | /* */ |
- | |
2459 | /* Description: Diese Funktion demonstriert die Möglichkeiten der Funktionen findfirst_() findnext_() und changedir_() */ |
- | |
2460 | /* */ |
- | |
2461 | /* */ |
- | |
2462 | /* Returnvalue: */ |
- | |
- | 2510 | ||
Line 2463... | Line 2511... | ||
2463 | /********************************************************************************************************************************************/ |
2511 | |
2464 | 2512 | ||
2465 | void Test(void) |
- | |
2466 | { |
2513 | void Test(void) |
2467 | File_t *fp = NULL; |
2514 | { |
Line -... | Line 2515... | ||
- | 2515 | Find_t find; |
|
- | 2516 | text[64]; |
|
Line 2468... | Line -... | ||
2468 | Find_t fe; |
- | |
2469 | u32 cwd = 0; |
- | |
2470 | - | ||
2471 | 2517 | ||
2472 | cwd = Partition.CurrentWorkingDirectory; |
2518 | UART1_PutString("\r\nsearching for elements in CWD:"); |
- | 2519 | UART1_PutString(GetPath()); |
|
2473 | sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text); |
2520 | |
- | 2521 | if(chdir_("log")) |
|
2474 | /* Als Erstes suchen wir uns das erste Unterverzeichnis (ATTR_SUBDIRECTORY) auf der Speicherkarte */ |
2522 | { |
2475 | if(findfirst_("*.*", ATTR_SUBDIRECTORY, &fe)) |
2523 | UART1_PutString(" done"); |
2476 | { |
2524 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
2477 | /* Danach wechseln wir in in dieses Unterverzeichnis auf der speicherkarte */ |
2525 | |
- | 2526 | if(chdir_("log11")) |
|
2478 | if(chdir_(fe.name)) |
2527 | { |
2479 | { |
2528 | UART1_PutString(" done"); |
2480 | sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text); |
- | |
2481 | /* Dann finden wir die erste Datei (ATTR_ARCIVE) in diesem Unterverzeichnis */ |
2529 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
2482 | if(findfirst_("*.*", ATTR_ARCHIVE, &fe)) |
2530 | |
2483 | { |
- | |
- | 2531 | if(chdir_("log12")) |
|
2484 | /* Der Name der gefundenen Datei wird ausgegeben */ |
2532 | { |
2485 | UART1_PutString("\r\n"); |
2533 | UART1_PutString(" done"); |
2486 | UART1_PutString(fe.name); |
2534 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
2487 | /* nun wir die erste gefundene Datei im Unterverzeichnis lesend geöffnet */ |
2535 | |
2488 | fp = fopen_(fe.name, 'r'); |
2536 | if(findfirst_("*.*", 0xff, &find)) |
2489 | if(fp != NULL){ |
2537 | { |
2490 | /* und die erste Zeile aus dem Inhalt ausgegeben */ |
- | |
2491 | fgets_(text,60,fp); |
2538 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
2492 | UART1_PutString("\r\n"); |
- | |
2493 | UART1_PutString(text); |
- | |
2494 | fclose_(fp); |
2539 | while(findnext_(&find)) |
- | 2540 | { |
|
2495 | } |
2541 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
- | 2542 | } |
|
- | 2543 | } |
|
- | 2544 | if(chdir_(".")) |
|
- | 2545 | { |
|
2496 | else { |
2546 | UART1_PutString(" done"); |
2497 | UART1_PutString("\r\nfile not found"); |
2547 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
- | 2548 | if(findfirst_("*.*", 0xff, &find)) |
|
- | 2549 | { |
|
- | 2550 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
|
- | 2551 | while(findnext_(&find)) |
|
- | 2552 | { |
|
- | 2553 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
|
- | 2554 | } |
|
- | 2555 | } |
|
- | 2556 | } |
|
- | 2557 | else UART1_PutString(" failed"); |
|
- | 2558 | if(chdir_("..")) |
|
2498 | } |
2559 | { |
2499 | - | ||
2500 | /* anschließend suchen wir alle weiteren Dateien im aktuellen Unterverzeichnis */ |
- | |
2501 | while(findnext_(&fe)) |
- | |
2502 | { |
2560 | UART1_PutString(" done"); |
2503 | /* und geben ebenfalls deren Namen */ |
2561 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
2504 | UART1_PutString("\r\n"); |
- | |
2505 | UART1_PutString(fe.name); |
2562 | if(findfirst_("*.*", 0xff, &find)) |
2506 | /* sowie nach dem Öffnen ihren Inhalt aus */ |
- | |
2507 | fp = fopen_(fe.name, 'r'); |
- | |
2508 | if(fp != NULL) |
2563 | { |
2509 | { |
- | |
2510 | fgets_(text,60,fp); |
2564 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
2511 | UART1_PutString("\r\n"); |
2565 | while(findnext_(&find)) |
2512 | UART1_PutString(text); |
2566 | { |
- | 2567 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
|
- | 2568 | } |
|
- | 2569 | } |
|
- | 2570 | } |
|
- | 2571 | else UART1_PutString(" failed"); |
|
- | 2572 | if(chdir_("..")) |
|
- | 2573 | { |
|
- | 2574 | UART1_PutString(" done"); |
|
- | 2575 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
|
- | 2576 | if(findfirst_("*.*", 0xff, &find)) |
|
- | 2577 | { |
|
- | 2578 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
|
2513 | fclose_(fp); |
2579 | while(findnext_(&find)) |
2514 | } |
2580 | { |
2515 | } |
2581 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
2516 | } |
- | |
2517 | } |
2582 | } |
2518 | } |
2583 | } |
- | 2584 | } |
|
2519 | Partition.CurrentWorkingDirectory = cwd; |
2585 | else UART1_PutString(" failed"); |
2520 | sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text); |
2586 | if(chdir_("..")) |
2521 | if(chdir_("/ordner1/ordner12/ordner13")) |
2587 | { |
2522 | { |
2588 | UART1_PutString(" done"); |
2523 | sprintf(text,"\r\n Path: %s\r\n",Partition.PathToCwd); UART1_PutString(text); |
2589 | UART1_PutString("\r\n cwd: "); UART1_PutString(GetPath()); |
2524 | fp = fopen_("Datei13.txt", 'r'); |
2590 | if(findfirst_("*.*", 0xff, &find)) |
2525 | if(fp != NULL){ |
2591 | { |
2526 | /* und die erste Zeile aus dem Inhalt ausgegeben */ |
- | |
2527 | fgets_(text,60,fp); |
2592 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
2528 | UART1_PutString("\r\n"); |
- | |
2529 | UART1_PutString(text); |
- | |
2530 | fclose_(fp); |
2593 | while(findnext_(&find)) |
2531 | } |
2594 | { |
- | 2595 | sprintf(text,"\r\n %s %x",find.name, find.fp.Attribute); UART1_PutString(text); |
|
2532 | else { |
2596 | } |
- | 2597 | } |
|
- | 2598 | } |
|
2533 | UART1_PutString("\r\nfile not found"); |
2599 | else UART1_PutString(" failed"); |
2534 | } |
- | |
2535 | } |
- | |
2536 | else |
- | |
2537 | UART1_PutString("\r\ndirectory not found"); |
2600 | } |
2538 | /* jetzt sind wir wieder im Rootverzeichnis */ |
- |