|
蓝牙模块的2/3FEC接口函数(在CCS环境下编译通过)
#include "config.h"
/* code_bit */ uint16 g[15]={0x1,0x2,0x4,0x8,0x10,0x20,0x40,0x80,0x100,0x200,0x365,0x1AF,0x35E,0x1D9,0x3B2};
void FEC_code(uint16 *code_in, uint16 *code_out, uint8 len) { uint8 i, j; uint16 a, val, code; uint16 mid[15]; uint16 code_bit[15]; for ( ; len > 0 ; len-- ) { val = *code_in++; for(i=0;i<15;i++) { a = val&g[i]; for(j=0;j<10;j++) { mid[j] = a&1; a = a>>1; } code_bit[i] = mid[0]; for(j=1;j<10;j++) code_bit[i] = code_bit[i]^mid[j]; } code = 0; for(i=0;i<15;i++) { code |= (code_bit[i]<<i); } *code_out++ = code; } }
/* decode and correct */ uint16 check[5]={0x765,0x9AF,0x135E,0x21D9,0x43B2}; /*the check array*/
void FEC_decode(uint16 *code_in, uint16 *code_out, uint8 len) { uint8 i, j; uint16 a, val, code; uint16 mid[15]; uint16 code_bit[15]; uint16 check_out[5]; uint16 errorflag,matchbit,matchflag,comparebit; for ( ; len > 0 ; len-- ) { val = *code_in++; a = val; for (i=0;i<15;i++) /*change the data received into bits*/ { code_bit[i] = a&1; a = a>>1; }
for(i=0;i<5;i++) /*caculate the cheksum*/ { a = val&check[i]; for(j=0;j<15;j++) /*get every bit of a */ { mid[j] = a&1; a=a>>1; } check_out[i] = mid[0]; for(j=1;j<15;j++) check_out[i]=check_out[i]^mid[j]; /*add the bits in mode 2*/ }
i=0; errorflag=0; do { if(check_out[i]==1) { errorflag=1; i=5; } else i++; } while (i<5); /* check if the check is all 0 */
if (errorflag==1) { i=0; matchbit=100; /* look for the same line*/ do { j=0; matchflag=1; do { comparebit=(check[j]>>i)&1; /* get the [j][i] bit of the array*/ if (check_out[j]!=comparebit) { j=5; matchflag=0; } /*not coincident,jump out at once*/ else j++; } while (j<5); if (matchflag==1) { matchbit=i; i=15; } /*if found,jump out at once*/ else i++; } while (i<15); if (matchbit<=14) { code_bit[matchbit]=code_bit[matchbit]^1; /*correct the error bit*/ } } code = 0; for(i=0;i<10;i++) { code |= (code_bit[i]<<i); } *code_out++ = code; } }
|