Abstract:
The present disclosure provides a data storing method for writing update data into a flash memory module to replace original data. The flash memory module includes at least one block including a plurality of sectors. Each of the sectors records a flag. The data storing method includes: getting a first sector, wherein all the bits of the flag of the first sector are the second storage status; writing the update data into the first sector, and programming at least one bit as the first storage status and at least one bit as the second storage status in the flag of the first sector; identifying a second sector storing original data, wherein at least one bit of the flag of the second sector is first storage status and at least one other bit is second storage status; programming all the bits of the second sector as the first storage status.

Description:
CROSS-REFERENCE TO RELATED APPLICATION 
       [0001]    This application claims the priority benefit of Taiwan application serial no. 104102675, filed on Jan. 27, 2015. The entirety of the above-mentioned patent application is hereby incorporated by reference herein and made a part of this specification. 
       BACKGROUND OF THE DISCLOSURE 
       [0002]    1. Field of the Disclosure 
         [0003]    The disclosure is related to a data storing method and an embedded system, and more particularly to a data storing method and an embedded system having high write counts. 
         [0004]    2. Description of Related Art 
         [0005]    Generally speaking, a conventional embedded system uses an electrically erasable programmable read only memory (EEPROM) as a storage medium for storing small data, which has an advantage of writing and erasing in unit of bytes and allows for a lot of ease when writing multiple units of small data. However, as the capacity of flash memory increases with lower price, the EEPROM in an embedded system becomes relatively expensive and is gradually replaced by the flash memory which is less expensive. 
         [0006]    However, unlike EEPROM which performs erase operation in unit of bytes, the flash memory always has to erase a whole block when executing an erase instruction. When a file with smaller data, e.g. a bootloader of a system or configuration of network setting is stored in a block of the flash memory, as the file is updated, the system needs to clear the whole block where the file is stored and then stores the updated file in the original block, which makes the flash memory to have far less re-write counts compared to the EEPROM. 
       SUMMARY OF THE DISCLOSURE 
       [0007]    The disclosure provides a data storing method and an embedded system which may effectively prolong the service life of a flash memory. 
         [0008]    According to an exemplary embodiment of the disclosure, a data storing method for writing an update data into a flash memory module to update the original data is provided. The flash memory module at least includes a block which includes a plurality of sectors. Each of the sectors includes a plurality of bits. Each bit may be identified as a first storage status or a second storage status. Each of the sectors records a flag. The data storing method includes getting a first sector of the sectors, wherein all the bits of the flag of the first sector are the second storage status. The data storing method further includes writing the update data into the first sector and programming at least one bit of the flag of the first sector as the first storage status, and programming at least one bit of the flag of the first sector as the second storage status. The data storing method further includes identifying a second sector storing the original data among the sectors, wherein at least one bit of the flag of the second sector is the first storage status, and at least one other bit of the flag of the second sector is the second storage status. The data storing method further includes programming all the bits of the second sector as the first storage status. 
         [0009]    In an exemplary embodiment of the disclosure, the data storing method further includes programming all the bits of the block as the second storage status if the block does not have the flag in which all the bits are the second storage status. 
         [0010]    In an exemplary embodiment of the disclosure, the data storing method further includes generating a corresponding error correction code for the update data of the first sector. 
         [0011]    In an exemplary embodiment of the disclosure, if all the bits of the update data stored in the first sector are the first storage status, all the bits of the corresponding error correction code are the first storage status. 
         [0012]    In an exemplary embodiment of the disclosure, the data storing method further includes determining the sizes of the sectors according to the sizes of the update data and original data. 
         [0013]    In an exemplary embodiment of the disclosure, the first storage status is 0 and the second storage status is 1. 
         [0014]    According to an exemplary embodiment of the disclosure, an embedded system, which includes a flash memory module and a controller, is provided. The flash memory module stores original data and at least includes a block. The block includes a plurality of sectors. Each of the sectors includes a plurality of bits. Each bit may be identified as the first storage status or the second storage status. Each of the sectors records a flag. The controller is coupled to a flash memory module. The controller gets a first sector of the sectors, wherein all the bits of the flag of the first sector are the second storage status. The controller writes the update data into the first sector, programs at least one bit of the flag of the first sector as the first storage status and programs at least one bit of the flag of the first sector as the second storage status. The controller identifies the second sector that stores the original data among the sectors, wherein at least one bit of the flag of the second sector is the first storage status and at least one other bit of the flag of the second sector is the second storage status. The controller programs all the bits of the second sector as the first storage status. 
         [0015]    In an exemplary embodiment of the disclosure, if the block does not have the flag in which all the bits are the second storage status, the controller programs all the bits of the block as the second storage status. 
         [0016]    In an exemplary embodiment of the disclosure, the controller further generates a corresponding error correction code for the update data stored in the first sector. 
         [0017]    In an exemplary embodiment of the disclosure, if all the bits of the update data stored in the first sector are the first storage status, all the bits of the corresponding error correction code are the first storage status. 
         [0018]    In an exemplary embodiment of the disclosure, the controller determines the sizes of the sectors according to the sizes of the update data and the original data. 
         [0019]    In an exemplary embodiment of the disclosure, the first storage status is 0 and the second storage status is 1. 
         [0020]    As indicated above, in the disclosure, the data writing method and the embedded system may search for the sector that is not written with data according to the flag and write the update data thereto, and set all the bits of the sector that stores the original data as the first storage status. Accordingly, every time when the data is updated, the sectors in the block may be used one by one for the update data to be written in. Only when all the sectors have been used, i.e. the block no longer has any sectors remained unwritten with data, the whole block is erased, thereby prolonging the service life of the flash memory. 
         [0021]    In order to make the aforementioned features and advantages of the disclosure more comprehensible, embodiments accompanying figures are described in detail below. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0022]      FIG. 1  is a block view illustrating an embedded system according to an exemplary embodiment of the disclosure. 
           [0023]      FIGS. 2A-2C  are schematic views illustrating accessing a sector of a flash memory module according to an exemplary embodiment of the disclosure. 
           [0024]      FIG. 3  is a flow chart illustrating a data storing method illustrated according to an exemplary embodiment of the disclosure. 
           [0025]      FIG. 4  is a flow chart illustrating data reading illustrated according to an exemplary embodiment of the disclosure. 
           [0026]      FIG. 5  is a schematic view illustrating accessing a sector of a flash memory module according to another exemplary embodiment of the disclosure. 
           [0027]      FIG. 6  is a schematic view illustrating accessing a sector of a flash memory module according to another exemplary embodiment of the disclosure. 
       
    
    
     DESCRIPTION OF EMBODIMENTS 
       [0028]    In order to make the embodiments of the disclosure more comprehensible, an NAND flash memory having a capacity of 256 MB is used as an example below for description. The flash memory may include 2048 blocks, and each block may include 64 pages; therefore, the size of each block is 128 KB, and the size of each page is 2 KB. 
         [0029]    The flash memory stores an error correction code (ECC) in an out of band (OOB) region of each page and uses the ECC to correct data error. In addition, each page may be divided into four chunks in a size of 512 bytes. The ECC is calculated in unit of chunks. Accordingly, every time when a chunk of data is corrected, the system calculates the corresponding ECC (16 bytes in size) of the chunk and stores ECC in the OOB region of the page of the chunk. Since one page in a size of 2 KB includes four chunks, each page in a size of 2 KB includes 64 bytes of OOB region. Therefore, the actual size of one page may be (2K+64) bytes, i.e. the data region pluses an OOB region. The actual size of each block may be 64×(2K+64) bytes=(128K+4K) bytes. It should be pointed out that the OOB region is used by the system to access the ECC corresponding to the page; therefore, the OOB region is not accessible by the user. Moreover, when data is written, the smallest unit for writing data into the flash memory is one byte. When data is erased, the smallest unit for erasing data from the flash memory is one block. 
         [0030]    The above descriptions related to the block, page and chunk of the flash memory and the amount and size of OOB are incorporated here only for the purpose of making the exemplary embodiments of the disclosure to be more comprehensible. Therefore, it should be understood that the disclosure is not limited thereto the above descriptions. In the disclosure, the block, page and chunk of the flash memory and the amount and size of and OOB may be arranged differently. The flash memory may also be an NOR flash memory or other types of flash memory. 
         [0031]      FIG. 1  is a block view illustrating an embedded system according to an exemplary embodiment of the disclosure, and  FIGS. 2A-2C  are schematic views illustrating accessing a sector of a flash memory module according to an exemplary embodiment of the disclosure. 
         [0032]    Referring to  FIGS. 1 and 2A , in the exemplary embodiment of the disclosure, an embedded system  100  includes a controller  102  and a flash memory module  104 , wherein the controller  102  is coupled to a flash memory module  104  and configured to perform write operation to write data into the flash memory module  104  or perform read operation to read data from the flash memory module  104 . 
         [0033]    In a case where 16 KB of data  211   a  is to be stored in a block  201  of the flash memory module  104 , the controller  102  identifies the size of the data  211   a  first. That is to say, the controller  102  determines that the data  211   a  may be stored in eight pages, wherein the size of each of the pages is 2 KB. Therefore, the controller  102  divides page 1 to page 64 of the block  201  into eight sectors, wherein the sector 1 corresponds to page 1˜page 8 and sector 2 corresponds to page 9˜page 16 and so forth. Here, the data may be a bootloader of the embedded system  100 , configuration of network setting (e.g. IP address, network mask, a gateway address) or a system profile of the embedded system  100  and the like. 
         [0034]    Here, it is assumed that the block  201  has not been written with data, and therefore the byte of all pages is recorded as 0xFF. Here, the controller  102  identifies the first bytes of sector 1 (i.e. the first bytes of page 1) as a flag  241 . Since the byte of all pages in the block  201  is 0xFF, the flag  241  is also 0xFF as well. In the exemplary embodiment, when the flag value of flag  241  is 0xFF, it represents that the sector 1 of the corresponding flag is a sector that has not been written with data. Likewise, the flags of sector 2˜sector 8, namely the first bytes of sector 2˜sector 8 are also 0xFF at this time. Although it is described above that the first byte of each sector is used as the flag, the disclosure is not limited thereto. In another exemplary embodiment, any one of bytes in the sector may be used as the flag, or any plurality of bytes in the sector may be used as the flag. 
         [0035]    Before the controller  102  writes the data  211   a  into the block  201 , the controller  102  searches for the sector that has not been written according to the flag of all sectors in the block  201 . In other words, the controller  102  looks for the flag with the flag value of 0xFF from flag  241 ˜flag  248  and writes the data  211   a  into the sector corresponding to the flag. At this time, since the flag values of flag  241 ˜flag  248  are 0xFF, the controller  102  chooses a first sector 1 (or first sector) to be written with the data  211   a.    
         [0036]    When the controller  102  writes the data  211   a  into the sector 1, the controller  102  modifies the flag  241  of the sector 1, namely the first bytes (or the first bytes of page 1) of the sector 1 to be 0x01 to show that currently the sector 1 stores valid data. At this time, the data  211   a  is stored in a data region  211  of page 1˜page 8. Although it is described above that the flag value of the flag  241  is modified as 0x01 to show that the corresponding sector stores valid data, the disclosure is not limited thereto. The flag value of the  241  may also be modified as other values that are neither 0x00 nor 0xFF such as 0x02, 0x03 . . . 0xFE and the like to show that the corresponding sector stores valid data. 
         [0037]    It should be mentioned that, when the data  211   a  is written into the data region  211  of page 1˜page 8, the controller  102  calculates ECC1˜ECC8 from page 1˜page 8 respectively and writes ECC1˜ECC8 into an OOB region  231  corresponding to page 1˜page 8 respectively. Here, ECC1˜ECC8 may be generated by using error-correct-algorithm such as BCH, RS or humming code. It should be noted that, in a block that has completely not been written with data, the ECC value corresponding to all the pages thereof is 0xFF. 
         [0038]    Referring to  FIG. 2B , the data  211   b  may be an update file of the data  211   a  or other types of file. Before the controller  102  writes the data  211   b  into the block  201 , the controller  102  searches for the sector that has not been written with data according to the flag of all sectors in the block  201 . In other words, the controller  102  looks for the flag with the value of 0xFF from the flag  241 ˜flag  248  and writes the data  211   a  into the sector corresponding to the flag. At this time, since the flag value of the flag  241  is 0x01, the values of flag  242 ˜flag  248  are 0xFF as well. Therefore, the controller  102  chooses the sector 2 (i.e., the first sector) among sector 2˜sector 8 to be written with the data  211   b.    
         [0039]    When the controller  102  writes the data  211   b  into the sector 2, the controller  102  modifies the flag value of flag  242  in the sector 2, namely the first byte of the sector 2 (or the first byte of page 9), to be 0x01 to show that currently the sector 2 stores valid data. At this time, the data  211   b  is stored in the data region  211  of page 9˜page 16. 
         [0040]    Likewise, when the data  211   b  is written into the data region  211  of page 9 page 16, the controller  102  calculates ECC9˜ECC16 of page 9˜page 16 respectively and writes the ECC9˜ECC16 into the OOB region  231  corresponding to page 9˜page 16 respectively. 
         [0041]    Apart from writing ECC9˜ECC16 into the OOB region  231  corresponding to page 9˜page 16 respectively, since the data  211   a  in the sector preceding the sector 2, namely the sector 1, is no longer valid data, the controller  102  further modify all the bytes in the data region  221  of the sector 1 (or second sector) as 0x00; in the meantime, the flag value of the flag  241  of sector 1 is modified as 0x00, it represents that the sector 1 has been written with data and currently does not have any valid data. 
         [0042]    When the controller  102  modifies all the bytes in the data region  221  of sector 1 (or second sector) as 0x00, the controller  102  calculates ECC 1˜ECC8 of page 1˜page 8 respectively. Since the byte of all the data is 0x00, the byte of all the generated ECC is also 0x00. The controller  102  writes all the bytes of the OOB  231  of page 1˜page 8 as 0x00. 
         [0043]    Referring to  FIG. 2C , when the data is written for several times using the method described by  FIGS. 2A and 2B  and the sector 8 in the block  201  has data, all the bytes in the data region  221  of sector 1˜sector 7 are modified as 0x00. Moreover, the flag value of flag  241 ˜flag  247  of sector 1˜sector 7 is also modified as 0x00. At this time, if the controller  102  is to write data  211   i  into the block  201 , the controller  102  checks the flag  241 ˜flag  248  of sector 1˜sector 8 in the block  201  first. Since the flag value of flag  241 ˜flag  247  is 0x00 and the flag value of the flag  248  is 0x01, the controller  102  does not find the sector with flag value of 0xFF, which represents that the block  201  no longer has any sector that has not been written with data. At this time, the controller  102  has to erase the whole block  201 , that is, erase all the bytes of sector 1˜sector 8 as 0xFF. Next, by using the same method as described by  FIG. 2A , the data  211   i  may be written into the sector 1, namely the data region  211  of page 1˜page 8. 
         [0044]    In the disclosure, the whole block  201  is erased only when all the sectors in the block  201  have performed write operation, that is, the block  201  is erased only when the eight sectors in the block  201  have stored data. Unlike conventional flash memories in which the whole block is erased when one sector is updated, under equivalent frequency of updating data in sector, the frequency of erasing the block  201  by using the write method of the disclosure is only one-eighth compared to the erasing frequency in the conventional flash memories. Accordingly, the write count of data by the flash memory may increases to eight times compared to the conventional flash memories, thereby prolonging the service life of the flash memory. 
         [0045]    It should be mentioned that the controller  102  in the exemplary embodiment may be a logic operation circuit so as to perform operation such as distributing sectors, searching for sectors that have not been written with data and setting the flag of sectors when the data is written; however, the disclosure is not limited thereto. In another embodiment, the above functions performed by the controller  102  may also be implemented using program codes of firmware or software. When the embedded system  100  is powered on, the program codes is loaded into the memory and executed by a micro controller unit (MCU) to complete the functions executed by the controller  102 . 
         [0046]      FIG. 3  is a flow chart illustrating a data storing method illustrated according to an exemplary embodiment of the disclosure. 
         [0047]    In step S 302 , the controller  102  sequentially checks the flag of each sector in the block to look for the unwritten sector having flag value of 0xFF, namely the sector that has not been written with data after the block is erased. 
         [0048]    If the block does not have any unwritten sector having the flag value of 0xFF, in step S 304 , the controller  102  erases the block to modify all the bytes in the block as 0xFF. 
         [0049]    If the block has an unwritten sector having the flag value of 0xFF, in step S 306 , the controller  102  writes update data into the unwritten sector having the flag value of 0xFF and sets the corresponding flag to be in a valid status, e.g. 0x01. In the meantime, when the update data is written into the sector, the controller  102  updates the ECC corresponding to the page. 
         [0050]    In step S 308 , the controller  102  determines whether the write operation is successful. 
         [0051]    If the write operation is not successful, in step S 310 , the controller  102  makes all the bytes in the current sector as 0x00 and generates a corresponding ECC (wherein all the bytes of ECC are 0x00 as well). Then, step S 302  is executed to look for the unwritten sector to be written with data again. 
         [0052]    If the write operation is successful, in step S 312 , the controller  102  makes all the bytes of the sectors that stores original data (i.e. old data) as 0x00 (including the flag of the sectors) and generates a corresponding ECC (wherein all the bytes of ECC are 0x00 as well). In that case, the controller  102  may determine that all the data previously stored in the sector is no longer valid according to the flag value 0x00. 
         [0053]      FIG. 4  is a flow chart illustrating data reading illustrated according to an exemplary embodiment of the disclosure. 
         [0054]    In step S 402 , when the controller  102  is to read data, the controller  102  searches for a valid sector in the block, namely the sector having a flag value that is neither 0x00 nor 0xFF. If the block does not have any valid sector, in step S 404 , the controller  102  sends a message indicating reading failure. 
         [0055]    If the block has a valid sector, in step S 406 , the controller  102  reads data from the data region of the valid sector, for example, reads the bootloader of the embedded system  100 , the configuration of network setting (e.g. IP address, network mask, gateway address), the system profile of the embedded system  100  and the like. 
         [0056]      FIG. 5  is a schematic view illustrating accessing a sector of a flash memory module according to another exemplary embodiment of the disclosure. 
         [0057]    Referring to  FIG. 5 , in comparisons to  FIGS. 2A-2C  in which a single block  201  is used for storing data,  FIG. 5  shows that two blocks, namely block  201  and block  202 , may be used for storing the data. 
         [0058]    Same as the first step shown by  FIG. 2A , when there is no data stored in the block  201  and  202 , all the bytes in the data region and OOB region of sector 1˜sector 16 are 0xFF. Certainly, the flag value of all the sectors is 0xFF, which represents that all the sectors are currently not written with data. Before the first data is to be written, the controller  102  finds out the unwritten sector 1 according to the flags of sector 1˜sector 16, writes the data into the sector 1 and modifies the flag of sector 1 as 0x01, representing that the sector 1 currently stores valid data. When the data is written into the sector 1, the controller  102  calculates the ECC in each page of the sector 1 and stores the ECC in the OOB region of the sector 1; such operation is the same as described in the second step illustrated by  FIG. 2A . 
         [0059]    When update data is to be written in, the controller  102  writes the data into the sector 2 and re-programs the data region and OOB region of the sector 1. In other words, all the bytes in the data region and the OOB region of the sector 1 is re-programmed as 0x00. 
         [0060]    Likewise, each update data is written into the sector 3˜the sector 16 in sequence, and the controller  102  re-programs the data region and the OOB region of the sector 2˜the sector 15 respectively. Thereafter, when the next update data is to be written and flag value of the sector (i.e. sector 1) following the sector 16 with flag value of 0x01 is 0x00, the controller  102  erases the block  201  to set all the bytes in the block  201  as 0xFF. Similarly, when the flag value of the sector 8 is 0x01 and the flag value of the other sectors is 0x00, the controller  102  erases the block  202  to set all the bytes in the block  202  as 0xFF. 
         [0061]    In another exemplary embodiment, the controller  102  may check all the flag values of the block after each data has been written in. If all the flag values of one block are 0x00, the block is erased. That is to say, after the update data has been written into the sector 9, the controller  102  may identify all the flag values of the block  201  as 0x00 and erase the block  201 . 
         [0062]    Although it is described above that two blocks are used for storing data, the disclosure is not limited thereto. In the disclosure, three or more blocks may be used for storing the data to prevent generation of bad blocks during operation or to meet the need for larger data storage capacity. 
         [0063]      FIG. 6  is a schematic view illustrating accessing a sector of a flash memory module according to another exemplary embodiment of the disclosure. 
         [0064]    Referring to  FIG. 6 , different from the above exemplary embodiments in which the size of the sectors is multiple times larger than the size of the pages, however, the size of the sectors may also be set to be a quarter of the size of one page (2 KB), namely the size of one chunk (512 bytes). 
         [0065]    Same as the above exemplary embodiments, when the first data is to be written, the controller  102  looks for the sector 1 which has not been written with data to write the data into the sector 1 and modified the flag value of the sector 1 as 0x01, showing that the sector 1 currently stores valid data. When the data is written into the sector 1, the controller  102  also calculates ECC1 of the sector 1 and stores ECC1 in the OOB region of the sector 1. Since the size of the current sector 1 is equivalent to the size of a chunk, the size of ECC1 is a quarter of the size of the ECC of one page (64 bytes), namely 16 bytes. 
         [0066]    Next, the operation shown by  FIG. 5  may be repeated until a sector  256  stores the valid data, that is, the flag value of the sector  256  is 0x01. At this point, all the bytes of the sector 1˜the sector  255  are 0x00. If new data is to be written in, the controller  102  erases the block  201  for the new data to be written into the sector 1. 
         [0067]    In the flash memory of the disclosure, the size of one chunk is 512 bytes and the chunk is the minimum unit for executing ECC calculation. Therefore, the size of the sectors is calculated in unit of 512 bytes which is the size of one chunk. Although it is described above that the chunk is used for describing the size of the sector, the disclosure is not limited thereto. The disclosure may also use two or more chunks for describing the size of the sectors. 
         [0068]    In summary, the exemplary embodiments provide the data storing method which divides the block of the flash memory into a plurality of sectors. Each sector has a flag. When the flag value is 0xFF, it represents that the sector has not been written with data. When the flag value is 0x00, it represents that the sector is in a blank status. When the flag value is neither 0xFF nor 0x00, it represents that the sector has valid data. In the flash memory, since the operation of programming a bit from 1 to 0 may be performed in unit of bytes, but the operation of programming a bit from 0 to 1 needs to be performed in unit of blocks, the exemplary embodiments use 0x00 to represent that the sector has been written with valid data ever but has currently been erased. In the exemplary embodiments, the data may be stored in sequence in an unwritten sector of the block. After the data is successfully stored, the sector that previously stores valid data is re-programmed as 0x00. The block is erased only when the block no longer has any sector with flag value of 0xFF. In comparisons with the conventional flash memory storage system in which the whole block has to be erased every time when data is updated when using a bootloader or configuring network setting, in the exemplary embodiments of the disclosure, the block may be erased after all the sectors in one block have been used. Accordingly, the disclosure may allow the life of the flash memory for the embedded system to be extended significantly. 
         [0069]    It will be apparent to those skilled in the art that various modifications and variations can be made to the structure of the disclosed embodiments without departing from the scope or spirit of the disclosure. In view of the foregoing, it is intended that the disclosure cover modifications and variations of this specification provided they fall within the scope of the following claims and their equivalents.