diff options
-rw-r--r-- | firmware/drivers/fat.c | 77 |
1 files changed, 70 insertions, 7 deletions
diff --git a/firmware/drivers/fat.c b/firmware/drivers/fat.c index 9d1f3af5ff..02d472ceeb 100644 --- a/firmware/drivers/fat.c +++ b/firmware/drivers/fat.c | |||
@@ -725,11 +725,11 @@ static int flush_fat(void) | |||
725 | return 0; | 725 | return 0; |
726 | } | 726 | } |
727 | 727 | ||
728 | #ifdef HAVE_RTC | ||
729 | static void fat_time(unsigned short* date, | 728 | static void fat_time(unsigned short* date, |
730 | unsigned short* time, | 729 | unsigned short* time, |
731 | unsigned short* tenth ) | 730 | unsigned short* tenth ) |
732 | { | 731 | { |
732 | #ifdef HAVE_RTC | ||
733 | struct tm* tm = get_time(); | 733 | struct tm* tm = get_time(); |
734 | 734 | ||
735 | if (date) | 735 | if (date) |
@@ -744,8 +744,68 @@ static void fat_time(unsigned short* date, | |||
744 | 744 | ||
745 | if (tenth) | 745 | if (tenth) |
746 | *tenth = (tm->tm_sec & 1) * 100; | 746 | *tenth = (tm->tm_sec & 1) * 100; |
747 | #else | ||
748 | /* non-RTC version returns an increment from the supplied time, or a | ||
749 | * fixed standard time/date if no time given as input */ | ||
750 | bool next_day = false; | ||
751 | |||
752 | if (time) | ||
753 | { | ||
754 | if (0 == *time) | ||
755 | { | ||
756 | /* set to 00:15:00 */ | ||
757 | *time = (15 << 5); | ||
758 | } | ||
759 | else | ||
760 | { | ||
761 | unsigned short mins = (*time >> 5) & 0x003F; | ||
762 | unsigned short hours = (*time >> 11) & 0x001F; | ||
763 | if ((mins += 10) >= 60) | ||
764 | { | ||
765 | mins = 0; | ||
766 | hours++; | ||
767 | } | ||
768 | if ((++hours) >= 24) | ||
769 | { | ||
770 | hours = hours - 24; | ||
771 | next_day = true; | ||
772 | } | ||
773 | *time = (hours << 11) | (mins << 5); | ||
774 | } | ||
775 | } | ||
776 | |||
777 | if (date) | ||
778 | { | ||
779 | if (0 == *date) | ||
780 | { | ||
781 | /* set to 1 August 2003 */ | ||
782 | *date = ((2003 - 1980) << 9) | (8 << 5) | 1; | ||
783 | } | ||
784 | else | ||
785 | { | ||
786 | unsigned short day = *date & 0x001F; | ||
787 | unsigned short month = (*date >> 5) & 0x000F; | ||
788 | unsigned short year = (*date >> 9) & 0x007F; | ||
789 | if (next_day) | ||
790 | { | ||
791 | /* do a very simple day increment - never go above 28 days */ | ||
792 | if (++day > 28) | ||
793 | { | ||
794 | day = 1; | ||
795 | if (++month > 12) | ||
796 | { | ||
797 | month = 1; | ||
798 | year++; | ||
799 | } | ||
800 | } | ||
801 | *date = (year << 9) | (month << 5) | day; | ||
802 | } | ||
803 | } | ||
804 | } | ||
805 | if (tenth) | ||
806 | *tenth = 0; | ||
807 | #endif /* HAVE_RTC */ | ||
747 | } | 808 | } |
748 | #endif | ||
749 | 809 | ||
750 | static int write_long_name(struct fat_file* file, | 810 | static int write_long_name(struct fat_file* file, |
751 | unsigned int firstentry, | 811 | unsigned int firstentry, |
@@ -861,9 +921,7 @@ static int write_long_name(struct fat_file* file, | |||
861 | entry[FATDIR_ATTR] = 0; | 921 | entry[FATDIR_ATTR] = 0; |
862 | entry[FATDIR_NTRES] = 0; | 922 | entry[FATDIR_NTRES] = 0; |
863 | 923 | ||
864 | #ifdef HAVE_RTC | ||
865 | fat_time(&date, &time, &tenth); | 924 | fat_time(&date, &time, &tenth); |
866 | #endif | ||
867 | entry[FATDIR_CRTTIMETENTH] = tenth; | 925 | entry[FATDIR_CRTTIMETENTH] = tenth; |
868 | *(unsigned short*)(entry + FATDIR_CRTTIME) = SWAB16(time); | 926 | *(unsigned short*)(entry + FATDIR_CRTTIME) = SWAB16(time); |
869 | *(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); | 927 | *(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); |
@@ -1167,15 +1225,20 @@ static int update_short_entry( struct fat_file* file, int size, int attr ) | |||
1167 | sizeptr = (int*)(entry + FATDIR_FILESIZE); | 1225 | sizeptr = (int*)(entry + FATDIR_FILESIZE); |
1168 | *sizeptr = SWAB32(size); | 1226 | *sizeptr = SWAB32(size); |
1169 | 1227 | ||
1170 | #ifdef HAVE_RTC | ||
1171 | { | 1228 | { |
1172 | unsigned short date=0, time=0; | 1229 | #ifdef HAVE_RTC |
1230 | unsigned short time = 0; | ||
1231 | unsigned short date = 0; | ||
1232 | #else | ||
1233 | /* get old time to increment from */ | ||
1234 | unsigned short time = SWAB16(*(unsigned short*)(entry + FATDIR_WRTTIME)); | ||
1235 | unsigned short date = SWAB16(*(unsigned short*)(entry + FATDIR_WRTDATE)); | ||
1236 | #endif | ||
1173 | fat_time(&date, &time, NULL); | 1237 | fat_time(&date, &time, NULL); |
1174 | *(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); | 1238 | *(unsigned short*)(entry + FATDIR_WRTTIME) = SWAB16(time); |
1175 | *(unsigned short*)(entry + FATDIR_WRTDATE) = SWAB16(date); | 1239 | *(unsigned short*)(entry + FATDIR_WRTDATE) = SWAB16(date); |
1176 | *(unsigned short*)(entry + FATDIR_LSTACCDATE) = SWAB16(date); | 1240 | *(unsigned short*)(entry + FATDIR_LSTACCDATE) = SWAB16(date); |
1177 | } | 1241 | } |
1178 | #endif | ||
1179 | 1242 | ||
1180 | rc = fat_seek( &dir, sector ); | 1243 | rc = fat_seek( &dir, sector ); |
1181 | if (rc < 0) | 1244 | if (rc < 0) |