In order to detect a single bit error, the data of every 256 bytes is divided into a 2048-bit table of 8 X, which uses the 22-bit verification code for verification and the 16-bit row verification code, perform horizontal verification. The six-digit column verification code is used for vertical verification. Describes the principle of generating the 22-bit verification code:
Column validation:
Cp0: Result of an exclusive or operation on bits 6, 4, 2, 0 of all bytes
CP1: the result of an exclusive or operation on bits, 5, 3, and 1 of all bytes.
.
.
Cp5: Result of an exclusive or operation on bits, 6, 5, and 4 of all bytes
Row Verification:
Lp0: returns the result of an exclusive or operation on all bits of 254, 4, 6 ,...
Lp1: returns the result of an exclusive or operation on all bits of 1, 3, 5, 7... 255 bytes.
.
.
Lp15: for 128 ~ Result of performing an exclusive or operation on all bits of 255 bytes
The 22-bit verification code must be saved in three bytes. The first two bytes must contain 16-bit line verification codes (lp0 ~ Lp15, the third byte is saved as 6 in the checkcode cp0 ~ Cp5, remaining 2 position 1:
When writing data to NAND Flash, we generate a verification code and save it to the spare data area in flash. When reading data from flash, a new ECC verification code is generated, which is different from the old one read by the spare data area:
Result 0: The data is correct.
The result contains 11 bits and 1 bits. The data has a 1-bit error (which can be corrected)
Other results: an error occurs when the data exceeds one digit and cannot be corrected.
The following is the c ++ implementation code of the ECC generation algorithm (only applicable to the intended use. In order to facilitate understanding and without optimization, a large number of exclusive or operations can be converted into table queries in actual use)
Static inline void setbit (unsigned Int & dat, int bit, int v) <br/>{< br/> If (V) <br/> dat | = (1 <bit); <br/> else <br/> dat & = ~ (1 <bit); <br/>}</P> <p> static inline int getbit (unsigned int dat, int bit) <br/>{< br/> If (DAT & (1 <bit) <br/> return 1; <br/> else <br/> return 0; <br/>}</P> <p> static inline int xorlp (unsigned char c) <br/>{< br/> return (getbit (C, 7) ^ getbit (C, 6) ^ getbit (C, 5) ^ getbit (C, 4) ^ getbit (C, 3) ^ getbit (C, 2) ^ getbit (C, 1) ^ getbit (C, 0); <br/>}</P> <p> static inline int xorcp0 (unsigned char C) <br/>{< br/> return (getbit (C, 6) ^ getbit (C, 4) ^ getbit (C, 2) ^ getbit (C, 0 )); <br/>}</P> <p> static inline int xorcp1 (unsigned char c) <br/>{< br/> return (getbit (C, 7) ^ getbit (C, 5) ^ getbit (C, 3) ^ getbit (C, 1 )); <br/>}</P> <p> static inline int xorcp2 (unsigned char c) <br/>{< br/> return (getbit (C, 5) ^ getbit (C, 4) ^ getbit (C, 1) ^ getbit (C, 0 )); <br/>}</P> <p> static inline int xorcp3 (unsigned char c) <br/>{< br/> return (getbit (C, 7) ^ getbit (C, 6) ^ getbit (C, 3) ^ getbit (C, 2 )); <br/>}</P> <p> static inline int xorcp4 (unsigned char c) <br/>{< br/> return (getbit (C, 3) ^ getbit (C, 2) ^ getbit (C, 1) ^ getbit (C, 0 )); <br/>}</P> <p> static inline int xorcp5 (unsigned char c) <br/>{< br/> return (getbit (C, 7) ^ getbit (C, 6) ^ getbit (C, 5) ^ getbit (C, 4 )); <br/>}</P> <p> unsigned int ECC (const unsigned char * Data) <br/>{< br/> unsigned int ECC = 0 xffffffff; </P> <p> for (size_t I = 0; I <256; I ++) <br/>{< br/> unsigned char c = data [I]; </P> <p> int Lp = xorlp (c); </P> <p> // bit 0 <br/> if (I & 0x01) <br/> setbit (ECC, 1, LP ^ getbit (ECC, 1); <br/> else <br/> setbit (ECC, 0, LP ^ getbit (ECC, 0); </P> <p> // bit 1 <br/> if (I & 0x02) <br/> setbit (ECC, 3, LP ^ getbit (ECC, 3); <br/> else <br/> setbit (ECC, 2, LP ^ getbit (ECC, 2); </P> <p> // bit 2 <br/> if (I & 0x04) <br/> setbit (ECC, 5, LP ^ getbit (ECC, 5); <br/> else <br/> setbit (ECC, 4, LP ^ getbit (ECC, 4); </P> <p> // bit 3 <br/> if (I & 0x08) <br/> setbit (ECC, 7, LP ^ getbit (ECC, 7); <br/> else <br/> setbit (ECC, 6, LP ^ getbit (ECC, 6); </P> <p> // bit 4 <br/> if (I & 0x10) <br/> setbit (ECC, 9, LP ^ getbit (ECC, 8); <br/> else <br/> setbit (ECC, 8, LP ^ getbit (ECC, 8); </P> <p> // bit 5 <br/> if (I & 0x20) <br/> setbit (ECC, 11, LP ^ getbit (ECC, 11); <br/> else <br/> setbit (ECC, 10, LP ^ getbit (ECC, 10); </P> <p> // bit 6 <br/> if (I & 0x40) <br/> setbit (ECC, 13, LP ^ getbit (ECC, 13); <br/> else <br/> setbit (ECC, 12, LP ^ getbit (ECC, 12); </P> <p> // bit 7 <br/> if (I & 0x80) <br/> setbit (ECC, 15, LP ^ getbit (ECC, 15); <br/> else <br/> setbit (ECC, 14, LP ^ getbit (ECC, 14); </P> <p> setbit (ECC, 18, xorcp0 (c) ^ getbit (ECC, 18 )); <br/> setbit (ECC, 19, xorcp1 (c) ^ getbit (ECC, 19); <br/> setbit (ECC, 20, xorcp2 (c) ^ getbit (ECC, 20); <br/> setbit (ECC, 21, xorcp3 (c) ^ getbit (ECC, 21); <br/> setbit (ECC, 22, xorcp4 (c) ^ getbit (ECC, 22); <br/> setbit (ECC, 23, xorcp5 (c) ^ getbit (ECC, 23 )); <br/>}</P> <p> setbit (ECC, 16, 1); <br/> setbit (ECC, 17, 1 ); </P> <p> return ECC; <br/>}< br/>