Detailed Description
The following detailed description of embodiments of the present invention is provided in connection with the accompanying drawings and examples. The following examples are intended to illustrate the invention but are not intended to limit the scope of the invention.
In order to support operations such as random writing, reading or deleting of FLASH file data, the embodiment of the invention provides a FLASH memory, which comprises a FLASH memory area, wherein the FLASH memory area is divided into a plurality of pages starting with number 0 according to the minimum unit of FLASH erasing, and then the memory area of the FLASH memory is divided according to functions. In this embodiment, the FLASH memory chip may be applied to a near field communication card (NFC card for short), where a "page" in this embodiment is defined as a minimum unit of FLASH erase, a "volume" is defined as one or more continuous "pages" for storing one type of data, the storage capacity of each "page" is 1024 bytes, and a FLASH chip with a storage capacity of 2M may divide a FLASH memory area into 2048 pages. Each page may also be defined as a different capacity size.
Fig. 1 is a schematic structural diagram of a frame structure of a FLASH memory, and as can be seen from fig. 1, a FLASH memory in the FLASH memory is divided into a boot area, a storage page volume, a file description volume, and a file data storage area in this embodiment. The system comprises a boot area, a storage page volume and a file description volume, wherein the boot area, the storage page volume and the file description volume belong to a system area and are used for describing the distribution and use conditions of information, file information and pages of a FLASH memory, and only occupy a few storage pages; the file data storage area is used for storing data of a file, occupies most storage pages, and specifically, the specific functions of the above parts are as follows:
the boot area is used for describing the information of the FLASH memory, such as the storage name, the version number, the memory generation time, the type of the memory, and the like, and the details are shown in table 1:
TABLE 1
Starting byte position
|
Name (R)
|
Occupied byte
|
Current content
|
0
|
Memory storage name
|
4
|
“NFSS”
|
4
|
Version number
|
4
|
“1000”
|
8
|
Page length
|
2
|
1024
|
10
|
The whole stored page number
|
2
|
1994
|
12
|
Boot sector start page number
|
2
|
0
|
14
|
Number of lead area pages
|
2
|
1
|
16
|
Starting page number of storage page volume
|
2
|
1
|
18
|
Number of pages of storage page volume
|
2
|
4
|
20
|
File description volume start number
|
2
|
5
|
22
|
Number of pages occupied by file description volume
|
2
|
5
|
24
|
Initial page number of file data area
|
2
|
10
|
26
|
Number of pages occupied by file data area
|
2
|
1984
|
28
|
Memory generation time
|
8
|
|
36
|
Memory type encoding
|
2
|
|
38
|
Retention
|
256
|
|
294
|
Memory name length
|
2
|
|
296
|
Memory name
|
Indefinite article
|
|
Remainder of
|
Retention
|
|
|
As can be seen from the above table, the storage capacity of the FLASH memory used in the embodiment of the present invention is 2M, which is divided into 2048 pages according to the minimum unit of FLASH erase, and the number of pages used in the entire memory is 1994 pages. The initial page of the boot sector is page 0, and 1 page is occupied; the initial page of the storage page volume is page 1, and 4 pages are occupied; the file description page volume has a starting page of page 5, occupying 5 total pages, and the remaining pages 10 to 1984 are allocated as file data storage areas.
The storage page volume indicates allocation information of pages and indicates a linked list structure of pages where file data is stored. The allocation information includes a plurality of entries, each entry for storing the page number of the next storage page of the file data, each entry within the storage page volume being a 16-bit integer occupying two 8-bit bytes, so that addressing from the beginning of the storage page volume is twice the entry sequence number. As shown in fig. 2, the order of addresses held by each entry corresponds to the entry number (0 entry at 0 addressing, 1 entry at 2 addressing, and 2 entry at 4 addressing), it should be noted that the correspondence may be identical, and the entry number corresponds to the page number of the corresponding storage page one to one (0 entry corresponds to page 0, 1 entry corresponds to page 1, and 2 entry corresponds to page 2), and the contents of the entry normally indicate the page number of the next storage page of the file data. For example, the address of the 100H entry is the 200H byte, and if the contents of the two bytes are 70H, this indicates that the file uses the data area of the 100H page, and the number of the next page used by the file is 70H.
It should be noted that the embodiment of the present invention makes the following limitations on the content of each item: 1)0x0000 indicates that the corresponding page data is invalid; 2)0xFFFF indicates that the corresponding page has no data written; 3)0x7FFF indicates end of file; 4)0x0006 denotes a system area; 5)0x0003 indicates that this page is a bad page.
The file description volume is used for describing information of stored files, each item of the file description volume corresponds to a file stored in the FLASH memory, the content of the file description volume is file information and the initial page number of the file, and the structure of each item is shown in table 2:
TABLE 2
Starting position
|
Name (R)
|
Occupied byte
|
0
|
File description length
|
2
|
2
|
File attributes
|
2
|
4
|
File generation time
|
8
|
12
|
Starting page number of file
|
2
|
14
|
File length
|
4
|
18
|
File check word
|
16
|
34
|
Reserved area
|
16
|
50
|
Filename Length
|
2
|
52
|
Filename
|
Indefinite article |
The file description of a file may include information such as the starting page number and the length of the file, and may further include one or more other information such as the file name, the file attribute, the length of the file, the starting page number of the file, the file generation time, and the like.
And the file data area is used for storing file data and occupies most of the pages of the FLASH storage area.
According to the FLASH memory provided by the embodiment, the address sequence stored in each item in the storage page volume is consistent with the item number (0 item is at the 0 addressing position, 1 item is at the 2 addressing position, and 2 items are at the 4 addressing position), and the item number is in one-to-one correspondence with the page number of the corresponding storage page, so that the mapping relation among the address, the item number and the page is formed.
The following describes a file management method for a FLASH file according to an embodiment of the present invention with a specific example. Txt file structure is shown in fig. 3, the first ten pages of storage area of FLASH memory are divided into FLASH system area, and the rest is FLASH file data storage area.
According to the information of the file description volume, the file name of the file is sample.txt, the starting page number of the file is 0x000c, the length of the file is 0x0600, namely 1536 bytes, and one page can store 1024 bytes, so that the data of the sample.txt in the file needs to occupy two pages;
knowing that the initial page number of the file is 0x000c from the file description volume, searching 0x000c items with the content of 0x0010 in the page allocation volume according to the mapping relation between the item numbers and the page allocation volume addressing, namely, the next page number of the file is 0x0010, and since the content of the 0x0010 item is 0x0001, the next page points to a system area, which indicates that no file storage data exists behind the file; txt is stored in two pages, 0x000c and 0x0010, and the storage area of the file is determined by a linked list of page numbers starting from the file start page number in the header.
In the embodiment of the invention, the address sequence stored by each item of the page allocation volume is consistent with the item number (0 item is at the 0 addressing position, 1 item is at the 2 addressing position, and 2 items are at the 4 addressing position), and each item corresponds to the corresponding storage page one by one, so that the storage page volume only needs to store the page number of the next page through the mapping relation of the address, the item number and the page, the allocation use condition of the page can be indicated, the storage space required by the storage page volume is reduced, and the index efficiency is improved.
After the FLASH memory area is divided, the file writing, reading and deleting operations can be executed according to the file management method of the embodiment of the invention. After the system establishes contact with the FLASH memory, the storage page volume and the file description volume of the NFC card (the FLASH memory is applied to the NFC card) are read first, and the file list is extracted, so that the file operation is performed on the NFC card quickly in the whole operation process.
FIG. 4 is a flowchart of a storage file management method based on a FLASH memory according to an embodiment of the present invention; as can be seen from fig. 4, this embodiment provides a method for managing a storage file based on the above FLASH memory, where the method for managing a storage file is a file writing method, and specifically, a process of writing a file is started from parameter-file description, so as to establish a link table relationship between file description and pages, and write data into a file data storage area, and specifically, the method includes:
s101: reading information of a file to be written;
the information of the file to be written may include a name of the file to be written, a length of the file to be written, content of the file to be written, and the like.
S102: determining the size of the file description of the file to be written, and judging whether the file description volume in the FLASH memory has enough space for writing the file description;
the file description of the file to be written can be determined through the read information of the file to be written, so that the size of the file description can be determined.
S1031: if the file description volume has enough space to write the file description, judging whether a file data storage area in the FLASH memory has enough space to write the file data to be written;
when it is confirmed that there is enough space in the file description volume, it is indicated that normal write operation can be performed for the file description at this time, and since the present embodiment aims to write the data of the file to be written into the FLASH memory, it is necessary to determine whether there is enough space in the file data storage region to write the data of the file to be written into the FLASH memory in order to ensure that the data of the file to be written is written normally.
S1041: if the file data storage area has enough space to write in the data of the file to be written, searching a first empty page number of the file data storage area as a file initial page number, and writing a file description in a file description volume;
when it is determined that the file data storage area has enough space to write the data of the file to be written, normal write operations can be performed on both the file description and the data of the file to be written, and therefore, preparations can be made for the write operations, such as: the file start page number can be determined, and the file start page number is used for determining the writing position of the file to be written, and generally, the first empty page number in the file data storage area is used as the file start page number, and the file description can be written into the file description volume.
S105: and writing the file data to be written in the file data storage area from the initial page number of the file description.
S106: and updating the storage page volume according to the page linked list relation stored in the file data storage area, and ending the writing process.
After writing the file description to the file description volume, the file data to be written may be written starting at the position of the starting page number in the file description, thereby completing the file writing operation.
It should be noted that, after the above-mentioned determining whether the file description volume in the FLASH memory has enough space to write the file description, the method may further include:
s1032: and if the file description volume does not have enough space to write the file description, outputting the abnormal condition that the file description volume has insufficient space.
Further, after the above-mentioned determining whether the file data storage area in the FLASH memory has enough space to write the file data to be written, the method may further include:
s1042: and if the file data storage area does not have enough space for writing the data of the file to be written, the space of the output file data storage area is insufficient and abnormal.
The method for managing storage files provided in this embodiment implements a process of writing files by using the structure of the FLASH memory, and specifically, the FLASH memory keeps an address sequence of each item in a storage page volume consistent with an item number (0 item is at an addressing position 0, 1 item is at an addressing position 2, and 2 items are at an addressing position 4), and the item numbers correspond to corresponding storage page numbers one to one, so as to form a mapping relationship among addresses, item numbers, and pages.
Fig. 5 is a flowchart of detecting whether there is a rename file corresponding to a file to be written in according to an embodiment of the present invention, and referring to fig. 5 on the basis of the above embodiment, in order to further improve the stability and reliability of the method for managing a storage file, after reading information of the file to be written, the method is further configured to further include:
s201: detecting whether a duplicate name file corresponding to a file to be written exists or not;
after the information of the file to be written is obtained, the stored file can be detected according to the information of the file to be written, whether a duplicate name file corresponding to the file to be written exists in the stored file is detected, and specifically, name information in the information of the file to be written and name information of the stored file can be analyzed and compared.
S202: if yes, outputting the duplicate name file abnormal operation;
if the analysis and comparison result shows that the name information in the file information to be written is the same as the name information of the stored information, it can be confirmed that a duplicate name file corresponding to the file to be written exists in the FLASH memory, and at this time, a process of performing secondary writing on the same file may occur.
S203: and if not, determining the size of the file description of the file to be written.
If the analysis and comparison result indicates that the name information in the information of the file to be written is different from the name information of the stored information, it may be determined that the file to be written is not stored in the FLASH memory at this time, so that the writing operation of the file to be written may be performed normally, that is, the size of the file description of the file to be written may be obtained, and then the subsequent writing operation may be performed according to steps 102 to 105 in the above embodiment, which may specifically refer to the above statements and will not be described herein again.
According to the storage file management method provided by the embodiment, whether the duplicate name file corresponding to the file to be written exists in the FLASH memory is detected, so that the operation of performing secondary error writing on the same file is avoided, the stability and the reliability of the writing operation on the file to be written are effectively ensured, and the stability and the reliability of the use of the storage file management method are further improved.
FIG. 6 is a flowchart illustrating a method for determining whether a file description volume space of an idle file is sufficient for writing a file description according to an embodiment of the present invention; fig. 7 is a flowchart of a method for releasing space in a FLASH release file description volume according to an embodiment of the present invention; on the basis of the above embodiment, as can be seen with continued reference to fig. 6-7, before searching for the first empty page number of the file data storage area as the starting page number of the file, the present embodiment may be configured to further include:
s301: judging whether the space of the file description volume of the idle file is enough to write the file description;
when there is enough space in the file description volume to write the file description, there may be two cases, one is that the file description volume space of the idle file has enough space to write the file description, and the other is that the whole file description volume (including the file description volume space of the idle file, the file description volume space of the used file, the file description volume space of the invalid file, etc.) has enough space to write the file description.
S302: if the space is insufficient, releasing the occupied space described by the invalid file, and searching a first empty page number of the file data storage area as a file initial page number; alternatively, the first and second electrodes may be,
if the space of the file description volume of the idle file is not enough to write the file description, at this time, in order to ensure the normal writing of the file description, the occupied space of the invalid file description can be released, so as to increase the size of the space for storing the file description, and fig. 7 is an implementation manner of releasing the space in the file description volume by FLASH, the process of releasing the occupied space of the invalid file description is performed in the file description volume, and the purpose is to squeeze the deleted file description, regenerate a new file description volume, reconstruct the file description volume according to the file list, then erase the original file description volume, and rewrite the new file description volume; after the occupied space described by the invalid file is released, the first empty page number of the file data storage area may be continuously searched as the starting page number of the file, and the subsequent implementation processes of step S1041 to step S105 in the foregoing embodiment may be performed, which may specifically refer to the above statements and is not described herein again.
S303: if the space is sufficient, the first empty page number of the file data storage area is directly searched as the starting page number of the file.
If the file description volume space of the idle file is sufficient for writing the file description, the first empty page number of the file data storage area may be directly searched as the starting page number of the file, and the subsequent implementation processes of step S1041 to step S105 in the above embodiment may be performed, which may specifically refer to the above statements and will not be described herein again.
According to the method for managing the stored file, whether the file description volume space of the idle file is enough for writing the file description is judged, so that the safe reliability of writing the file description can be effectively ensured, and the stability and the reliability of the method for managing the stored file are further improved.
FIG. 8 is a flowchart illustrating a method for determining whether a storage space of an idle file is sufficient for writing data of a file to be written according to an embodiment of the present invention; fig. 9 is a flowchart of a method for releasing the occupied space of the deleted file by the FLASH according to the embodiment of the present invention; on the basis of the foregoing embodiment, as can be seen with continued reference to fig. 8-9, before writing the file data to be written in the file data storage area from the start page number of the file description, the method of this embodiment is configured to further include:
s401: judging whether the storage space of the idle file is enough to write in the data of the file to be written;
when the file data storage area has enough space to write the data of the file to be written, two situations may be included, one being that the storage space of the idle file has enough space to write the data of the file to be written, and the other being that the entire file data storage area (including the storage space of the idle file, the storage space of the used file, the occupied space of the deleted file, etc.) has enough space to write the data of the file to be written, so to further improve the stability and reliability of the file management method, it is necessary to determine whether the storage space of the idle file is enough to write the data of the file to be written, and a specific determination process may analyze and compare the size of the data of the file to be written with the size of the storage space of the idle file.
S402: if the space is insufficient, releasing the occupied space of the deleted file, and writing the file data to be written in the file data storage area from the initial page number described by the file; alternatively, the first and second electrodes may be,
if the storage space of the idle file is not enough to write the data of the file to be written, at this time, in order to ensure the normal write operation of the data to be written, the occupied space of the deleted file may be released, so as to increase the size of the space for storing the data to be written, and fig. 9 is an implementation manner of releasing the occupied space of the deleted file by FLASH. This operation need only be done within the memory page volume, which acts to rewrite all pages marked as 0x0000 (invalidated) to 0 xffff; after the space occupied by the deleted file is released, the data of the file to be written may be written in the file data storage area from the start page number described in the file, and the subsequent implementation processes of step S1041 to step S105 in the foregoing embodiment may be performed, which may specifically refer to the above statements and will not be described herein again.
S403: and if the space is enough, writing the file data to be written in the file data storage area directly from the initial page number of the file description.
If the storage space of the idle file is sufficient for writing the data of the file to be written, the data of the file to be written may be written in the file data storage area directly from the start page number of the file description, and the subsequent implementation processes of step S1041 to step S105 in the above embodiment may be specifically referred to the above statements, which are not described herein again.
According to the method for managing the stored file, whether the storage space of the idle file is enough for writing the data of the file to be written is judged, so that the stability and the reliability of the writing operation of the data of the file to be written can be effectively guaranteed, and the stability and the reliability of the method for managing the stored file are further improved.
FIG. 10 is a flowchart illustrating an embodiment of detecting whether data written into a file to be written has an error; on the basis of the foregoing embodiment, as can be seen with continued reference to fig. 10, in order to further determine the security reliability of the data writing operation, after writing the file data to be written in the file data storage area from the starting page number of the file description, the present embodiment sets the method to further include:
s501: detecting whether the written file data to be written has errors;
after the writing operation of the file data to be written is finished, the content of the file data written into the FLASH memory and the content of the file data to be written can be analyzed and compared to detect whether the written file data to be written has errors.
S502: if the written file data to be written has errors, outputting error information; alternatively, the first and second electrodes may be,
and when the analysis and comparison result shows that the written file data content is inconsistent with the content of the file data to be written, the written file data to be written has an error, so that the error writing operation can be confirmed, and error information is output to remind a user conveniently.
S503: and if the data of the file to be written is correct, updating the storage page volume according to the page linked list relation stored in the file data storage area of the file to be written.
And when the analysis and comparison result shows that the content of the written file data is consistent with the content of the file data to be written, the written file data to be written is a correct writing operation, and the storage page volume is updated according to the page linked list relation stored in the file data storage area of the file to be written, so that a user can conveniently read or delete the written file to be written.
The storage file management method provided by the embodiment can effectively ensure the correct reliability of the writing operation of the file data to be written by detecting whether the written file data to be written has errors, and further improves the stability and reliability of the storage file management method.
In specific application, fig. 11 is a flowchart of a method for writing a file based on a FLASH memory according to an embodiment of the present invention; as can be seen with reference to fig. 11, the data file writing step includes:
step A: b, the system reads the information of the file, then detects whether there is a duplicate name file, if yes, outputs the duplicate name file to be abnormal, if not, executes step B;
and B: determining the size of the file description, judging whether the file description volume of the FLASH has enough space to write the file description, if the space is insufficient, outputting an abnormal condition that the space of the file description volume is insufficient, and if the space of the file description volume is enough, executing the step C;
and C: judging whether the file data storage area has enough space for writing the file data, if the space is insufficient, outputting that the space of the file data storage area is insufficient and abnormal, and if the space is sufficient, executing the step D;
step D: judging whether the space of the idle file description volume is enough for writing the file description, if so, releasing the space occupied by the invalid file description, and executing the step E; if the space is enough, directly executing the step E;
step E: searching the first empty page number of the file data storage area as the initial page number of the file, writing the file description, and executing the step F;
step F: judging whether the idle storage space is enough for writing file data, if the space is insufficient, releasing the occupied space of the deleted file, and executing the step G; if the space is sufficient, executing step G;
step G: writing data in the file data storage area starting from the start page number of the file description; executing the step H;
step H: detecting whether the written data has errors, and if the written data has errors, returning error information; if the result is correct, executing the step I;
step I: and updating the storage page volume according to the page linked list relation stored in the file data storage area, and ending the writing process.
FIG. 12 is a flowchart of a storage file management method based on a FLASH memory according to another embodiment of the present invention; as can be seen from fig. 12, the present embodiment provides another storage file management method, which is a method for reading a file, and the above-mentioned process for reading a file starts with a parameter-file description, and aims to read data of a page of a linked list determined by a starting page number of a file in the file description, and specifically, the method includes:
step A: the system reads the initial page number of the file from the file description volume;
and B: reading file data stored in a storage page corresponding to the initial page number of the file;
specifically, the specific position of the starting page number of the file may be determined, specifically, whether the starting page number of the file is 0x0001 preset or not may be detected, if so, correct reading information is returned, if not, other positions where the starting page number of the file is located may be determined, and after the specific position of the starting page number of the file is confirmed, file data stored in a storage page corresponding to the starting page number of the file is read;
and C: and reading the page number of the next page of the file according to the file storage page volume, and reading the file data stored in the storage page corresponding to the page number until the file data is completely read.
Specifically, after the file data stored in the storage page corresponding to the starting page number of the file is read, in order to read the whole file data, the page number of the next page of the file may be acquired, and then the file data stored in the corresponding storage page is read according to the page number.
Further, in order to improve the correct reliability of file reading, after reading the file data stored in the storage page corresponding to the starting page number of the file, the method is configured to further include:
step D: judging whether the reading is correct or not;
if the reading is wrong, returning error information and ending the reading of the file;
if the reading is correct, inquiring the content of the corresponding item in the storage page volume according to the initial page number of the file, acquiring the page number of the next page, and reading the file data stored in the storage page corresponding to the page number.
Wherein the error information includes: a read operation error; a file length error; and checking the word error at the end of the file.
The method for managing storage files provided in this embodiment implements a process of writing in files by using the structure of the FLASH memory, and specifically, the FLASH memory keeps an address sequence of each item in a storage page volume consistent with an item number (0 item is at an addressing position 0, 1 item is at an addressing position 2, and 2 items are at an addressing position 4), and the item numbers correspond to corresponding storage page numbers one by one, so as to form a mapping relationship among addresses, item numbers, and pages.
Fig. 13 is a flowchart of a storage file management method based on a FLASH memory according to another embodiment of the present invention, and referring to fig. 13, the present embodiment provides another storage file management method, where the storage file management method is a method for deleting a file, and the file deleting process starts from a parameter-file description, and is intended to mark a file description of a deleted file and a data saving page thereof, and specifically, the method for deleting a file includes:
step A: reading the address addr of the starting page number of the file description and the starting page number pageNum of the file data from the file description volume, and setting the address content specified by the addr in the storage page volume to be 0 (indicating that the corresponding page data of the item is invalid);
after the initial page number pageNum of the file data is read, whether the page number pageNum is preset 0x0001 or not can be judged, and if yes, correct information is returned; if not, step B may continue.
And B: and inquiring the content of the corresponding item in the storage page volume according to the initial page number of the file data, acquiring the address addr of the next page number and the pageNum of the next page number, and setting the content of the specified address of the address addr of the next page number in the storage page volume to be 0 (indicating that the corresponding page data of the item is invalid) until the file data is deleted completely.
Further, in order to improve the reliability of deleting the file, after setting the content of the addr designated address in the storage page volume to 0, the method is further configured to further include:
and C: judging whether the deletion operation is wrong or not;
if the deletion operation is wrong, returning error information;
if the deletion operation is correct, the content of the address of the next page number located at the current page number in the storage page volume may be set to 0 to complete the deletion operation on the whole file.
Wherein the error information includes: operation error and error of data page number occupied by files.
The method for managing storage files provided in this embodiment implements a process of writing in files by using the structure of the FLASH memory, and specifically, the FLASH memory keeps an address sequence of each item in a storage page volume consistent with an item number (0 item is at an addressing position 0, 1 item is at an addressing position 2, and 2 items are at an addressing position 4), and the item numbers correspond to corresponding storage page numbers one by one, so as to form a mapping relationship among addresses, item numbers, and pages.
Finally, it should be noted that: the above embodiments are only used to illustrate the technical solution of the present invention, and not to limit the same; while the invention has been described in detail and with reference to the foregoing embodiments, it will be understood by those skilled in the art that: the technical solutions described in the foregoing embodiments may still be modified, or some or all of the technical features may be equivalently replaced; and the modifications or the substitutions do not make the essence of the corresponding technical solutions depart from the scope of the technical solutions of the embodiments of the present invention.