⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hamming.c

📁 ATMEL单片机可用的文件系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/***********************************************************************/
/*                                                                     */
/*   Module:  hamming.c                                                */
/*   Release: 2004.5                                                   */
/*   Version: 2002.4                                                   */
/*   Purpose: Implement Hamming 1 bit encoding and decoding            */
/*                                                                     */
/*---------------------------------------------------------------------*/
/*                                                                     */
/*                 Copyright 2004, Blunk Microsystems.                 */
/*                        ALL RIGHTS RESERVED                          */
/*                                                                     */
/*   Licensees have the non-exclusive right to use, modify, or extract */
/*   this computer program for software development at a single site.  */
/*   This program may be resold or disseminated in executable format   */
/*   only. The source code may not be redistributed or resold.         */
/*                                                                     */
/***********************************************************************/
#include "flashfsp.h"

#if INC_NAND_FS && NUM_FFS_VOLS

/***********************************************************************/
/* Symbol Definitions                                                  */
/***********************************************************************/
#define CODE_ERROR      1
#define CORRECTABLE     2

#define MASK1      0x11  /* 1st interleaving 0001 0001 (bits 0 and 4) */
#define MASK2      0x22  /* 2nd interleaving 0010 0010 (bits 1 and 5) */
#define MASK3      0x44  /* 3rd interleaving 0100 0100 (bits 2 and 6) */
#define MASK4      0x88  /* 4th interleaving 1000 1000 (bits 3 and 7) */

/***********************************************************************/
/* Type Definitions                                                    */
/***********************************************************************/
typedef union
{
  ui8  byte[4];
  ui16 half[2];
  ui32 lword;
} Container;

/***********************************************************************/
/* Global Variable Declarations                                        */
/***********************************************************************/
const ui8 NibXor[] =
{
  0x0^0x0, 0x0^0x1, 0x0^0x2, 0x0^0x3, 0x0^0x4, 0x0^0x5, 0x0^0x6, 0x0^0x7,
  0x0^0x8, 0x0^0x9, 0x0^0xA, 0x0^0xB, 0x0^0xC, 0x0^0xD, 0x0^0xE, 0x0^0xF,
  0x1^0x0, 0x1^0x1, 0x1^0x2, 0x1^0x3, 0x1^0x4, 0x1^0x5, 0x1^0x6, 0x1^0x7,
  0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x8, 0xF^0x9, 0xF^0xA, 0xF^0xB, 0xF^0xC, 0xF^0xD, 0xF^0xE, 0xF^0xF,
};

/***********************************************************************/
/* Global Function Definitions                                         */
/***********************************************************************/

/***********************************************************************/
/*   EncodeEcc: Generate ECC data for specified 512 bytes of user data */
/*                                                                     */
/*       Input: data = pointer to 512 bytes of user data               */
/*                                                                     */
/*      Output: ecc[10] = 10 byte array of ECC data                    */
/*                                                                     */
/***********************************************************************/
void EncodeEcc(const ui32 *data, ui8 *ecc)
{
  ui32 lword;
  Container every, every1[2], every2[2], every4[2], every8[2];
  Container every16[2], every32[2], every64[2], tmp;
  int i;

  /*-------------------------------------------------------------------*/
  /* Initially clear the checksums.                                    */
  /*-------------------------------------------------------------------*/
  every1[0].lword = every1[1].lword = every2[0].lword = every2[1].lword =
  every4[0].lword = every4[1].lword = every8[0].lword = every8[1].lword =
  every16[0].lword = every16[1].lword = 0; every32[0].lword =
  every32[1].lword = every64[0].lword = every64[1].lword = 0;

  /*-------------------------------------------------------------------*/
  /* Build the 32-bit checksums.                                       */
  /*-------------------------------------------------------------------*/
  for (i = 0; i < 128; ++i)
  {
    lword = data[i];

    /*-----------------------------------------------------------------*/
    /* Update 7 checksums each loop.                                   */
    /*-----------------------------------------------------------------*/
    every1[i & 1].lword ^= lword;
    every2[(i & 2) >> 1].lword ^= lword;
    every4[(i & 4) >> 2].lword ^= lword;
    every8[(i & 8) >> 3].lword ^= lword;
    every16[(i & 16) >> 4].lword ^= lword;
    every32[(i & 32) >> 5].lword ^= lword;
    every64[(i & 64) >> 6].lword ^= lword;
  }
  every.lword = every1[0].lword ^ every1[1].lword;

  /*-------------------------------------------------------------------*/
  /* Calculate the "every other bit" checksums.                        */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui16)(every.half[0] ^ every.half[1]);
  ecc[0] = (ui8)(tmp.byte[0] ^ tmp.byte[1]);
  /*
  ** ecc[0] contains
  ** bit 0: checksum of all even bits of 1st interleave plane
  ** bit 1: checksum of all even bits of 2nd interleave plane
  ** bit 2: checksum of all even bits of 3rd interleave plane
  ** bit 3: checksum of all even bits of 4th interleave plane
  ** bit 4: checksum of all odd bits of 1st interleave plane
  ** bit 5: checksum of all odd bits of 2nd interleave plane
  ** bit 6: checksum of all odd bits of 3rd interleave plane
  ** bit 7: checksum of all odd bits of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 2 bits" checksums.                           */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui16)(every.half[0] ^ every.half[1]);
  ecc[1] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[1]] << 4));
  /*
  ** ecc[1] contains
  ** bit 0: checksum of all even 2-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 2-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 2-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 2-bit groups of 4th interleave plane
  ** bit 4: checksum of all odd 2-bit groups of 1st interleave plane
  ** bit 5: checksum of all odd 2-bit groups of 2nd interleave plane
  ** bit 6: checksum of all odd 2-bit groups of 3rd interleave plane
  ** bit 7: checksum of all odd 2-bit groups of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 4 bits" checksums (2 byte groupings).        */
  /*-------------------------------------------------------------------*/
  tmp.byte[0] = (ui8)(every.byte[0] ^ every.byte[1]);
  tmp.byte[1] = (ui8)(every.byte[2] ^ every.byte[3]);
  ecc[2] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[1]] << 4));
  /*
  ** ecc[2] contains
  ** bit 0: checksum of all even 4-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 4-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 4-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 4-bit groups of 4th interleave plane
  ** bit 4: checksum of all odd 4-bit groups of 1st interleave plane
  ** bit 5: checksum of all odd 4-bit groups of 2nd interleave plane
  ** bit 6: checksum of all odd 4-bit groups of 3rd interleave plane
  ** bit 7: checksum of all odd 4-bit groups of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 8 bits" checksums (4 byte groupings).        */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui8)(every1[0].half[0] ^ every1[0].half[1]);
  tmp.byte[0] ^= tmp.byte[1];
  tmp.half[1] = (ui16)(every1[1].half[0] ^ every1[1].half[1]);
  tmp.byte[2] ^= tmp.byte[3];
  ecc[3] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[2]] << 4));
  /*
  ** ecc[3] contains
  ** bit 0: checksum of all even 8-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 8-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 8-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 8-bit groups of 4th interleave plane
  ** bit 4: checksum of all odd 8-bit groups of 1st interleave plane
  ** bit 5: checksum of all odd 8-bit groups of 2nd interleave plane
  ** bit 6: checksum of all odd 8-bit groups of 3rd interleave plane
  ** bit 7: checksum of all odd 8-bit groups of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 16 bits" checksums (every 2 longs).          */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui16)(every2[0].half[0] ^ every2[0].half[1]);
  tmp.byte[0] ^= tmp.byte[1];
  tmp.half[1] = (ui16)(every2[1].half[0] ^ every2[1].half[1]);
  tmp.byte[2] ^= tmp.byte[3];
  ecc[4] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[2]] << 4));
  /*
  ** ecc[4] contains
  ** bit 0: checksum of all even 16-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 16-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 16-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 16-bit groups of 4th interleave plane
  ** bit 4: checksum of all odd 16-bit groups of 1st interleave plane
  ** bit 5: checksum of all odd 16-bit groups of 2nd interleave plane
  ** bit 6: checksum of all odd 16-bit groups of 3rd interleave plane
  ** bit 7: checksum of all odd 16-bit groups of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 32 bits" checksums (every 4 longs).          */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui16)(every4[0].half[0] ^ every4[0].half[1]);
  tmp.byte[0] ^= tmp.byte[1];
  tmp.half[1] = (ui16)(every4[1].half[0] ^ every4[1].half[1]);
  tmp.byte[2] ^= tmp.byte[3];
  ecc[5] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[2]] << 4));
  /*
  ** ecc[5] contains
  ** bit 0: checksum of all even 32-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 32-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 32-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 32-bit groups of 4th interleave plane
  ** bit 4: checksum of all odd 32-bit groups of 1st interleave plane
  ** bit 5: checksum of all odd 32-bit groups of 2nd interleave plane
  ** bit 6: checksum of all odd 32-bit groups of 3rd interleave plane
  ** bit 7: checksum of all odd 32-bit groups of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 64 bits" checksums (every 8 longs).          */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui16)(every8[0].half[0] ^ every8[0].half[1]);
  tmp.byte[0] ^= tmp.byte[1];
  tmp.half[1] = (ui16)(every8[1].half[0] ^ every8[1].half[1]);
  tmp.byte[2] ^= tmp.byte[3];
  ecc[6] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[2]] << 4));
  /*
  ** ecc[6] contains
  ** bit 0: checksum of all even 32-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 32-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 32-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 32-bit groups of 4th interleave plane
  ** bit 4: checksum of all odd 32-bit groups of 1st interleave plane
  ** bit 5: checksum of all odd 32-bit groups of 2nd interleave plane
  ** bit 6: checksum of all odd 32-bit groups of 3rd interleave plane
  ** bit 7: checksum of all odd 32-bit groups of 4th interleave plane
  */

  /*-------------------------------------------------------------------*/
  /* Calculate the "every 128 bits" checksums (every 16 longs).        */
  /*-------------------------------------------------------------------*/
  tmp.half[0] = (ui16)(every16[0].half[0] ^ every16[0].half[1]);
  tmp.byte[0] ^= tmp.byte[1];
  tmp.half[1] = (ui16)(every16[1].half[0] ^ every16[1].half[1]);
  tmp.byte[2] ^= tmp.byte[3];
  ecc[7] = (ui8)(NibXor[tmp.byte[0]] | (NibXor[tmp.byte[2]] << 4));
  /*
  ** ecc[7] contains
  ** bit 0: checksum of all even 128-bit groups of 1st interleave plane
  ** bit 1: checksum of all even 128-bit groups of 2nd interleave plane
  ** bit 2: checksum of all even 128-bit groups of 3rd interleave plane
  ** bit 3: checksum of all even 128-bit groups of 4th interleave plane

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -