This invention discloses a power-loss protection method for the FATFS
file system, which is applied to an embedded
file storage system using non-
volatile memory as the storage medium. The power-loss protection is achieved solely through
computer programming technology, and includes the following steps: S1, allocating a region in the
file storage medium as a
backup area for file write operations; S2, when the
file system performs a write operation on any sector in the storage medium, before executing the erase operation on that sector, reading the data of that sector and saving it to the
backup area; S3, calculating the CRC
checksum, saving the CRC
checksum and the sector number of the sector together to the
backup area, making the data in the backup area valid; S4, executing the erase operation on the sector to complete the data writing; S5, after completing the writing of the data in that sector, invalidating the data in the backup area; S6, when the embedded
file system is powered on again, checking whether the data in the backup area is valid, and if valid, restoring the sector data saved in the backup area and writing it back to the corresponding sector.