Advertisement
cr88192

Simple 8->12 / 16->24 ECC scheme

Apr 3rd, 2019
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.46 KB | None | 0 0
  1. /*
  2. Expands 16 bits to 24 bits by generating a parity byte.
  3. This parity byte allows recovering from any single-bit errors during transmission.
  4. Both the data and parity bytes would be transmitted.
  5.  
  6. This could be used, in addition to CRC or similar, as a first line of defense against transmission errors.
  7. Small single-bit errors could potentially be recovered, rather than requiring the CRC check to fail and the message needing to be resent.
  8.  
  9.  
  10. This scheme is derived from Hamming Codes, but differs mostly in that bits are organized into a "sane" ordering (with data bits left in their original order).
  11. */
  12.  
  13. #if 0
  14. /** Generate Parity Byte via XOR */
  15. byte H16_PByteForByte(byte v)
  16. {
  17.     byte p0, p1, p2, p3;
  18.     byte p4, p5, p6, p7;
  19.     byte p;
  20.    
  21.     p0=(v>>0)^(v>>1)^(v>>3)^(v>>4)^(v>>6);
  22.     p1=(v>>0)^(v>>2)^(v>>3)^(v>>5)^(v>>6);
  23.     p2=(v>>1)^(v>>2)^(v>>3)^(v>>7);
  24.     p3=(v>>4)^(v>>5)^(v>>6)^(v>>7);
  25.     p0&=1;  p1&=1;  p2&=1;  p3&=1;
  26.    
  27.     p=p0|(p1<<1)|(p2<<2)|(p3<<3);
  28.     return(p);
  29. }
  30. #endif
  31.  
  32. #if 1
  33. /** Generate Parity Byte via lookup table */
  34. byte H16_PByteForByte(byte v)
  35. {
  36.     static const byte pbtab[64]={
  37.         0x30,0x65,0x56,0x03,0x47,0x12,0x21,0x74,
  38.         0xA9,0xFC,0xCF,0x9A,0xDE,0x8B,0xB8,0xED,
  39.         0x9A,0xCF,0xFC,0xA9,0xED,0xB8,0x8B,0xDE,
  40.         0x03,0x56,0x65,0x30,0x74,0x21,0x12,0x47,
  41.         0x8B,0xDE,0xED,0xB8,0xFC,0xA9,0x9A,0xCF,
  42.         0x12,0x47,0x74,0x21,0x65,0x30,0x03,0x56,
  43.         0x21,0x74,0x47,0x12,0x56,0x03,0x30,0x65,
  44.         0xB8,0xED,0xDE,0x8B,0xCF,0x9A,0xA9,0xFC
  45.     };
  46.  
  47.     byte p;
  48.     p=(v&0x80)?pbtab[(0xFF-v)>>1]:pbtab[v>>1];
  49.     p=(v&0x01)?(p>>4):(p&15);
  50.     return(p);
  51. }
  52. #endif
  53.  
  54. /** Get parity byte for a pair of bytes. */
  55. byte H16_PByteForPair(u16 v)
  56. {
  57.     byte p0, p1, p;
  58.     p0=H16_PByteForByte((v   )&255);
  59.     p1=H16_PByteForByte((v>>8)&255);
  60.     p=p0|(p1<<4);
  61.     return(p);
  62. }
  63.  
  64. /* Recover a byte given parity bits. */
  65. u16 H16_RecoverByte(byte vi, byte pi, byte pj)
  66. {
  67.     static const byte xmtab[16]={
  68.         0x00, 0xFF, 0xFF, 0x01, 0xFF, 0x02, 0x04, 0x08,
  69.         0xFF, 0x10, 0x20, 0x40, 0x80, 0xFF, 0xFF, 0xFF
  70.     };
  71.  
  72.     byte xm;
  73.     xm=xmtab[(pi^pj)&15];
  74.  
  75.     if(xm==0xFF) /* Invalid combination of parity bits. */
  76.         { xm=0x00; }
  77.  
  78.     return(vi^xm);
  79. }
  80.  
  81. /** Recover a pair of bytes given the parity byte. */
  82. u16 H16_RecoverPair(u16 vi, byte pi)
  83. {
  84.     int v0, v1, v;
  85.     byte pj;
  86.  
  87.     pj=H16_PByteForPair(vi);
  88.     if(pi==pj)
  89.     {
  90.         /* If parity bytes agree, assume there is no error. */
  91.         return(vi);
  92.     }
  93.        
  94.     v0=H16_RecoverByte(vi>>0, pi>>0, pj>>0);
  95.     v1=H16_RecoverByte(vi>>8, pi>>4, pj>>4);
  96.     v=v0|(v1<<8);
  97.     return(v);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement