📄 fat.lst
字号:
756 * fat_dir_list_index : # of the fetched entry
757 *
758 * return:
759 * OK: file available
760 * KO: low level error
761 *----------------------------------------------------------------------------
762 * PURPOSE:
763 * Fetch the selected entry
764 *----------------------------------------------------------------------------
765 * EXAMPLE:
766 *----------------------------------------------------------------------------
767 * NOTE:
768 *----------------------------------------------------------------------------
769 * REQUIREMENTS:
770 *****************************************************************************/
771 bit fat_seek_entry_record (void)
772 {
773 1 Uint16 gl_offset = 0;
774 1 Uint16 i;
775 1 if (dir_is_root)
776 1 {
777 2 fat_dir_current_sect = fat_ptr_rdir;
778 2 }
779 1 else
780 1 {
781 2 fat_dir_current_sect = (((Uint32)(dclusters[0].cluster)) * fat_cluster_size)
782 2 + fat_ptr_data;
783 2 }
784 1 fat_dir_current_offs = 0; /* reset the global offset */
785 1
786 1 for (i = 0; i <= fat_dir_list_index; i++)
787 1 gl_offset += fat_dir_entry_list[i];
788 1
789 1 return fat_dseek(gl_offset * DIR_SIZE);
790 1 }
791 /*F**************************************************************************
792 * NAME: fat_seek_first
793 *----------------------------------------------------------------------------
794 * PARAMS:
795 *
796 * return:
797 * - OK: first file found
798 * - KO: low level error
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 14
799 *----------------------------------------------------------------------------
800 * PURPOSE:
801 * Fetch the first directory info in cache
802 *----------------------------------------------------------------------------
803 * EXAMPLE:
804 *----------------------------------------------------------------------------
805 * NOTE:
806 *----------------------------------------------------------------------------
807 * REQUIREMENTS:
808 *****************************************************************************/
809 bit fat_seek_first (void)
810 {
811 1 fat_dir_current_offs = 0; /* reset the global offset */
812 1
813 1 if (dir_is_root)
814 1 { /* root diretory */
815 2 fat_dir_list_index = 0; /* point first root entry */
816 2 if (fat_dseek((Int16)(fat_dir_entry_list[0] * DIR_SIZE)) == OK)
817 2 {
818 3 fat_get_dir_entry(&fat_cache.current);/* update first file info */
819 3 return OK;
820 3 }
821 2 else
822 2 {
823 3 return KO; /* low level error */
824 3 }
825 2 }
826 1 else
827 1 { /* not root dir */
828 2 fat_dir_list_index = 1; /* point ".." entry */
829 2 if (fat_dseek((Int16)(fat_dir_entry_list[1] * DIR_SIZE)) == OK)
830 2 {
831 3 fat_get_dir_entry(&fat_cache.parent); /* update parent dir info */
832 3 return fat_goto_next(); /* update first file info */
833 3 }
834 2 else
835 2 return KO; /* low level error */
836 2 }
837 1 }
838
839
840 /*F**************************************************************************
841 * NAME: fat_goto_subdir
842 *----------------------------------------------------------------------------
843 * PARAMS:
844 * id: file extension to select
845 *
846 * return:
847 * - OK: subdir selected
848 * - KO: current entry not a directory
849 * - KO: no file in subdir
850 * - KO: low level error
851 *----------------------------------------------------------------------------
852 * PURPOSE:
853 * Go to the subdir if current is a directory
854 *----------------------------------------------------------------------------
855 * EXAMPLE:
856 *----------------------------------------------------------------------------
857 * NOTE:
858 * Also called by goto_parentdir() with current info from parent info
859 *----------------------------------------------------------------------------
860 * REQUIREMENTS:
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 15
861 *****************************************************************************/
862 bit fat_goto_subdir (Byte id)
863 {
864 1 /* check if current file is a directory */
865 1 if ((fat_cache.current.attributes & ATTR_DIRECTORY) == ATTR_DIRECTORY)
866 1 {
867 2 /* computes the sector address (RELATIVE) */
868 2 if (fat_cache.current.start_cluster != 0)
869 2 { /* go to not root dir */
870 3 dir_is_root = FALSE; /* not the root dir */
871 3 /* get directory allocation table */
872 3 fat_get_clusters(&dclusters, MAX_DIR_FRAGMENT_NUMBER);
873 3
874 3 /* Save last index position for chain cluster */
875 3 fat_last_dclust_index = fat_last_clust_index;
876 3 /* initialize fat pointers */
877 3 fat_dchain_nb_clust = 0;
878 3 fat_dchain_index = 0;
879 3 fat_dclust_byte_count = 0;
880 3
881 3 /* computes sector address from allocation table */
882 3 fat_dir_current_sect = (((Uint32)(dclusters[0].cluster)) * fat_cluster_size)
883 3 + fat_ptr_data;
884 3 }
885 2 else
886 2 { /* go to root dir */
887 3 return fat_get_root_directory(id);
888 3 }
889 2
890 2 fat_get_dir_file_list(id); /* create list of entries */
891 2
892 2 fat_dir_list_index = 1; /* point ".." entry */
893 2 if (fat_dseek((Int16)(fat_dir_entry_list[1] * DIR_SIZE)) == OK)
894 2 {
895 3 fat_get_dir_entry(&fat_cache.parent); /* update parent dir info */
896 3 return fat_goto_next(); /* update first file info */
897 3 }
898 2 else
899 2 return KO; /* low level error */
900 2 }
901 1 else
902 1 return KO; /* current entry is not a dir */
903 1 }
904
905
906 /*F**************************************************************************
907 * NAME: fat_goto_parentdir
908 *----------------------------------------------------------------------------
909 * PARAMS:
910 * id: file extension to select
911 *
912 * return:
913 * status: OK: parent_dir selected
914 * KO: no parent dir (root)
915 *----------------------------------------------------------------------------
916 * PURPOSE:
917 * Go to the parent directory
918 *----------------------------------------------------------------------------
919 * EXAMPLE:
920 *----------------------------------------------------------------------------
921 * NOTE:
922 * File pointed is sub-dir if parent dir is not root or first file if root
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 16
923 *----------------------------------------------------------------------------
924 * REQUIREMENTS:
925 *****************************************************************************/
926 bit fat_goto_parentdir (Byte id)
927 {
928 1 Uint16 temp_cluster;
929 1
930 1 if (dir_is_root)
931 1 { /* already in root dir */
932 2 fat_seek_first(); /* point on first file */
933 2 return KO;
934 2 }
935 1 else
936 1 { /* not in root dir */
937 2 temp_cluster = dclusters[0].cluster + 2;/* save cluster info */
938 2 fat_cache.current = fat_cache.parent; /* goto the parent directory */
939 2
940 2 /* issue the equivalent to a cd .. DOS command */
941 2 if (fat_goto_subdir(id))
942 2 { /* reselect the dir entry in list */
943 3 while (temp_cluster != fat_cache.current.start_cluster)
944 3 {
945 4 if (fat_goto_next() == KO)
946 4 break;
947 4 }
948 3 if (temp_cluster == fat_cache.current.start_cluster)
949 3 return OK;
950 3 else
951 3 return KO;
952 3 }
953 2 else
954 2 {
955 3 return KO;
956 3 }
957 2 }
958 1 }
959
960
961
962
963 /*F**************************************************************************
964 * NAME: fat_fopen
965 *----------------------------------------------------------------------------
966 * PARAMS:
967 * mode: READ: open file for read
968 * WRITE: open file for write
969 *
970 * return:
971 * - OK: file opened
972 * - KO: file not opened: - file is empty
973 * - low level read error
974 *----------------------------------------------------------------------------
975 * PURPOSE:
976 * Open the file in read or write mode
977 *----------------------------------------------------------------------------
978 * EXAMPLE:
979 * if (fat_get_root_directory(FILE_WAV) == OK) // Select first WAV file in root
980 * {
981 * fat_fopen(WRITE); // Open this file in WRITE mode
982 * for (j = 0; j < 10; j++)
983 * fat_fputc(buff[j]);
984 * fat_fclose();
C51 COMPILER V7.50 FAT 02/16/2009 09:59:55 PAGE 17
985 * }
986 *----------------------------------------------------------------------------
987 * NOTE:
988 *----------------------------------------------------------------------------
989 * REQUIREMENTS:
990 * For write mode, there must be an entry in the root and entry data must be
991 * updated.
992 *****************************************************************************/
993 bit fat_fopen (bit mode)
994 {
995 1 if (mode == READ)
996 1 {
997 2 if (fat_cache.current.size.l == 0)
998 2 {
999 3 return KO; /* file empty */
1000 3 }
1001 2 else
1002 2 {
1003 3 fat_fclust_byte_count = 0; /* byte 0 of cluster */
1004 3
1005 3 /* reset the allocation list variable */
1006 3 fat_fchain_index = 0;
1007 3 fat_fchain_nb_clust = 0; /* start on first contiguous cl */
1008 3 /* get file allocation list */
1009 3 fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER);
1010 3
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -