--- /dev/null
+/* ----------------------------------------------------------------------------\r
+ * SAM Software Package License\r
+ * ----------------------------------------------------------------------------\r
+ * Copyright (c) 2012, Atmel Corporation\r
+ *\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions are met:\r
+ *\r
+ * - Redistributions of source code must retain the above copyright notice,\r
+ * this list of conditions and the disclaimer below.\r
+ *\r
+ * Atmel's name may not be used to endorse or promote products derived from\r
+ * this software without specific prior written permission.\r
+ *\r
+ * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
+ * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
+ * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * ----------------------------------------------------------------------------\r
+ */\r
+\r
+/*----------------------------------------------------------------------------\r
+ * Headers\r
+ *----------------------------------------------------------------------------*/\r
+\r
+#include "board.h"\r
+\r
+/*----------------------------------------------------------------------------\r
+ * Internal function\r
+ *----------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * Counts and return the number of bits set to '1' in the given byte.\r
+ * \param byte Byte to count.\r
+ */\r
+static uint8_t CountBitsInByte(uint8_t byte)\r
+{\r
+ uint8_t count = 0;\r
+\r
+ while (byte > 0)\r
+ {\r
+ if (byte & 1)\r
+ {\r
+ count++;\r
+ }\r
+ byte >>= 1;\r
+ }\r
+\r
+ return count;\r
+}\r
+\r
+/**\r
+ * Counts and return the number of bits set to '1' in the given hamming code.\r
+ * \param code Hamming code.\r
+ */\r
+static uint8_t CountBitsInCode256(uint8_t *code)\r
+{\r
+ return CountBitsInByte(code[0]) + CountBitsInByte(code[1]) + CountBitsInByte(code[2]);\r
+}\r
+\r
+/**\r
+ * Calculates the 22-bit hamming code for a 256-bytes block of data.\r
+ * \param data Data buffer to calculate code for.\r
+ * \param code Pointer to a buffer where the code should be stored.\r
+ */\r
+static void Compute256(const uint8_t *data, uint8_t *code)\r
+{\r
+ uint32_t i;\r
+ uint8_t columnSum = 0;\r
+ uint8_t evenLineCode = 0;\r
+ uint8_t oddLineCode = 0;\r
+ uint8_t evenColumnCode = 0;\r
+ uint8_t oddColumnCode = 0;\r
+\r
+ // Xor all bytes together to get the column sum;\r
+ // At the same time, calculate the even and odd line codes\r
+ for (i=0; i < 256; i++)\r
+ {\r
+ columnSum ^= data[i];\r
+\r
+ // If the xor sum of the byte is 0, then this byte has no incidence on\r
+ // the computed code; so check if the sum is 1.\r
+ if ((CountBitsInByte(data[i]) & 1) == 1)\r
+ {\r
+ // Parity groups are formed by forcing a particular index bit to 0\r
+ // (even) or 1 (odd).\r
+ // Example on one byte:\r
+ //\r
+ // bits (dec) 7 6 5 4 3 2 1 0\r
+ // (bin) 111 110 101 100 011 010 001 000\r
+ // '---'---'---'----------.\r
+ // |\r
+ // groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4 |\r
+ // P2' ooooooo eeeeeee ooooooo eeeeeee P2 |\r
+ // P1' ooo eee ooo eee ooo eee ooo eee P1 |\r
+ // |\r
+ // We can see that: |\r
+ // - P4 -> bit 2 of index is 0 --------------------'\r
+ // - P4' -> bit 2 of index is 1.\r
+ // - P2 -> bit 1 of index if 0.\r
+ // - etc...\r
+ // We deduce that a bit position has an impact on all even Px if\r
+ // the log2(x)nth bit of its index is 0\r
+ // ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3)\r
+ // and on all odd Px' if the log2(x)nth bit of its index is 1\r
+ // ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5)\r
+ //\r
+ // As such, we calculate all the possible Px and Px' values at the\r
+ // same time in two variables, evenLineCode and oddLineCode, such as\r
+ // evenLineCode bits: P128 P64 P32 P16 P8 P4 P2 P1\r
+ // oddLineCode bits: P128' P64' P32' P16' P8' P4' P2' P1'\r
+ //\r
+ evenLineCode ^= (255 - i);\r
+ oddLineCode ^= i;\r
+ }\r
+ }\r
+\r
+ // At this point, we have the line parities, and the column sum. First, We\r
+ // must caculate the parity group values on the column sum.\r
+ for (i=0; i < 8; i++)\r
+ {\r
+ if (columnSum & 1)\r
+ {\r
+ evenColumnCode ^= (7 - i);\r
+ oddColumnCode ^= i;\r
+ }\r
+ columnSum >>= 1;\r
+ }\r
+\r
+ // Now, we must interleave the parity values, to obtain the following layout:\r
+ // Code[0] = Line1\r
+ // Code[1] = Line2\r
+ // Code[2] = Column\r
+ // Line = Px' Px P(x-1)- P(x-1) ...\r
+ // Column = P4' P4 P2' P2 P1' P1 PadBit PadBit\r
+ code[0] = 0;\r
+ code[1] = 0;\r
+ code[2] = 0;\r
+\r
+ for (i=0; i < 4; i++)\r
+ {\r
+ code[0] <<= 2;\r
+ code[1] <<= 2;\r
+ code[2] <<= 2;\r
+\r
+ // Line 1\r
+ if ((oddLineCode & 0x80) != 0)\r
+ {\r
+ code[0] |= 2;\r
+ }\r
+\r
+ if ((evenLineCode & 0x80) != 0)\r
+ {\r
+ code[0] |= 1;\r
+ }\r
+\r
+ // Line 2\r
+ if ((oddLineCode & 0x08) != 0)\r
+ {\r
+ code[1] |= 2;\r
+ }\r
+\r
+ if ((evenLineCode & 0x08) != 0)\r
+ {\r
+ code[1] |= 1;\r
+ }\r
+\r
+ // Column\r
+ if ((oddColumnCode & 0x04) != 0)\r
+ {\r
+ code[2] |= 2;\r
+ }\r
+\r
+ if ((evenColumnCode & 0x04) != 0)\r
+ {\r
+ code[2] |= 1;\r
+ }\r
+\r
+ oddLineCode <<= 1;\r
+ evenLineCode <<= 1;\r
+ oddColumnCode <<= 1;\r
+ evenColumnCode <<= 1;\r
+ }\r
+\r
+ // Invert codes (linux compatibility)\r
+ code[0] = (~(uint32_t)code[0]);\r
+ code[1] = (~(uint32_t)code[1]);\r
+ code[2] = (~(uint32_t)code[2]);\r
+\r
+ TRACE_DEBUG("Computed code = %02X %02X %02X\n\r",\r
+ code[0], code[1], code[2]);\r
+}\r
+\r
+/**\r
+ * Verifies and corrects a 256-bytes block of data using the given 22-bits\r
+ * hamming code.\r
+ *\r
+ * \param data Data buffer to check.\r
+ * \param originalCode Hamming code to use for verifying the data.\r
+ *\r
+ * \return 0 if there is no error, otherwise returns a HAMMING_ERROR code.\r
+ */\r
+static uint8_t Verify256( uint8_t* pucData, const uint8_t* pucOriginalCode )\r
+{\r
+ /* Calculate new code */\r
+ uint8_t computedCode[3] ;\r
+ uint8_t correctionCode[3] ;\r
+\r
+ Compute256( pucData, computedCode ) ;\r
+\r
+ /* Xor both codes together */\r
+ correctionCode[0] = computedCode[0] ^ pucOriginalCode[0] ;\r
+ correctionCode[1] = computedCode[1] ^ pucOriginalCode[1] ;\r
+ correctionCode[2] = computedCode[2] ^ pucOriginalCode[2] ;\r
+\r
+ TRACE_DEBUG( "Correction code = %02X %02X %02X\n\r", correctionCode[0], correctionCode[1], correctionCode[2] ) ;\r
+\r
+ // If all bytes are 0, there is no error\r
+ if ( (correctionCode[0] == 0) && (correctionCode[1] == 0) && (correctionCode[2] == 0) )\r
+ {\r
+ return 0 ;\r
+ }\r
+\r
+ /* If there is a single bit error, there are 11 bits set to 1 */\r
+ if ( CountBitsInCode256( correctionCode ) == 11 )\r
+ {\r
+ // Get byte and bit indexes\r
+ uint8_t byte ;\r
+ uint8_t bit ;\r
+ \r
+ byte = correctionCode[0] & 0x80;\r
+ byte |= (correctionCode[0] << 1) & 0x40;\r
+ byte |= (correctionCode[0] << 2) & 0x20;\r
+ byte |= (correctionCode[0] << 3) & 0x10;\r
+\r
+ byte |= (correctionCode[1] >> 4) & 0x08;\r
+ byte |= (correctionCode[1] >> 3) & 0x04;\r
+ byte |= (correctionCode[1] >> 2) & 0x02;\r
+ byte |= (correctionCode[1] >> 1) & 0x01;\r
+\r
+ bit = (correctionCode[2] >> 5) & 0x04;\r
+ bit |= (correctionCode[2] >> 4) & 0x02;\r
+ bit |= (correctionCode[2] >> 3) & 0x01;\r
+\r
+ /* Correct bit */\r
+ TRACE_DEBUG("Correcting byte #%d at bit %d\n\r", byte, bit ) ;\r
+ pucData[byte] ^= (1 << bit) ;\r
+\r
+ return Hamming_ERROR_SINGLEBIT ;\r
+ }\r
+\r
+ /* Check if ECC has been corrupted */\r
+ if ( CountBitsInCode256( correctionCode ) == 1 )\r
+ {\r
+ return Hamming_ERROR_ECC ;\r
+ }\r
+ /* Otherwise, this is a multi-bit error */\r
+ else\r
+ {\r
+ return Hamming_ERROR_MULTIPLEBITS ;\r
+ }\r
+}\r
+\r
+/*----------------------------------------------------------------------------\r
+ * Exported functions\r
+ *----------------------------------------------------------------------------*/\r
+\r
+/**\r
+ * Computes 3-bytes hamming codes for a data block whose size is multiple of\r
+ * 256 bytes. Each 256 bytes block gets its own code.\r
+ * \param data Data to compute code for.\r
+ * \param size Data size in bytes.\r
+ * \param code Codes buffer.\r
+ */\r
+void Hamming_Compute256x( const uint8_t *pucData, uint32_t dwSize, uint8_t* puCode )\r
+{\r
+ TRACE_DEBUG("Hamming_Compute256x()\n\r");\r
+\r
+ while ( dwSize > 0 )\r
+ {\r
+ Compute256( pucData, puCode ) ;\r
+\r
+ pucData += 256;\r
+ puCode += 3;\r
+ dwSize -= 256;\r
+ }\r
+}\r
+\r
+/**\r
+ * Verifies 3-bytes hamming codes for a data block whose size is multiple of\r
+ * 256 bytes. Each 256-bytes block is verified with its own code.\r
+ *\r
+ * \return 0 if the data is correct, Hamming_ERROR_SINGLEBIT if one or more\r
+ * block(s) have had a single bit corrected, or either Hamming_ERROR_ECC\r
+ * or Hamming_ERROR_MULTIPLEBITS.\r
+ *\r
+ * \param data Data buffer to verify.\r
+ * \param size Size of the data in bytes.\r
+ * \param code Original codes.\r
+ */\r
+uint8_t Hamming_Verify256x( uint8_t* pucData, uint32_t dwSize, const uint8_t* pucCode )\r
+{\r
+ uint8_t error ;\r
+ uint8_t result = 0 ;\r
+\r
+ TRACE_DEBUG( "Hamming_Verify256x()\n\r" ) ;\r
+\r
+ while ( dwSize > 0 )\r
+ {\r
+ error = Verify256( pucData, pucCode ) ;\r
+\r
+ if ( error == Hamming_ERROR_SINGLEBIT )\r
+ {\r
+ result = Hamming_ERROR_SINGLEBIT ;\r
+ }\r
+ else\r
+ {\r
+ if ( error )\r
+ {\r
+ return error ;\r
+ }\r
+ }\r
+\r
+ pucData += 256;\r
+ pucCode += 3;\r
+ dwSize -= 256;\r
+ }\r
+\r
+ return result ;\r
+}\r
+\r