miniADSB receiver project - CRC calculation

Updated: September 06, 2009

Information on this page may be subject to copyrights or trademarks of third parties

Thread: http://www.mikrocontroller.net/topic/135459#new

PDF download  EUROCONTROL - CRC Calculation for Mode-S Transponders

For a simpler solution look here: http://miniadsb.forumprofi.de/viewtopic.php?f=3&t=95


Generic implementation in Pascal 

function parity112bit(st:string):string;
const poly:cardinal=FFFA0480;
var i,data,data1,data2:cardinal;
begin
       result:=000000;
       if length(st)<>11 then exit;
       data:=(ord(st[1])shl 24) OR (ord (st[2])shl 16) OR
        (ord (st[3])shl 8) or (ord(st[4]));
       data1:=(ord(st[5])shl 24) OR (ord (st[6])shl 16) OR
        (ord (st[7])shl 8) or (ord(st[8]));
       data2:=(ord(st[9])shl 24) OR (ord (st[10])shl 16) OR
        (ord (st[11])shl 8);

       for i:=1 to 88 do
       begin
          if (data and 80000000) <> 0 then
                data:=data xor poly;
          data:=data shl 1;
          if (data1 and 80000000) <>0 then data:=data or 1;
          data1:=data1 shl 1;
          if (data2 and 80000000) <>0 then data1:=data1 or 1;
          data2:=data2 shl 1;
       end;
       result:=copy(inttohex(data,8),1,6);
end;

function parity56bit(st:string):string;
const poly:cardinal=FFFA0480;
var i,data,data1:cardinal;
begin
       result:=000000;
       if length(st)<>4 then exit;
       data:=(ord(st[1])shl 24) OR (ord (st[2])shl 16) OR
        (ord (st[3])shl 8) or (ord(st[4]));
       for i:=1 to 32 do
       begin
          if (data and 80000000) <> 0 then
                data:=data xor poly;
          data:=data shl 1;
       end;
       result:=copy(inttohex(data,8),1,6);
end;


Implementation for AVR processors 

.include "m48def.inc"

.MACRO XLDI                  ;Start macro definition
        ldi r16,@1        
        mov @0,r16         
.ENDMACRO                    ;End macro definition


;Test patterns
;==============
test88:
    ldi     r16,high(RAMEND)    ;init stack
    out     SPH,r16
    ldi     r16,low(RAMEND)
    out     SPL,r16

    rcall  setup

;5D 3C6614 => C315D2
    xldi    r0,0x5D
    xldi    r1,0x3C
    xldi    r2,0x66
    xldi    r3,0x14
    rcall  crc32

;8F 45AC52 60BDF348222A58 => B98284
    xldi    r0,0x8F
    xldi    r1,0x45
    xldi    r2,0xAC
    xldi    r3,0x52
    xldi    r4,0x60
    xldi    r5,0xBD
    xldi    r6,0xF3
    xldi    r7,0x48
    xldi    r8,0x22
    xldi    r9,0x2A
    xldi    r10,0x58
    rcall  crc88

test881:
    rjmp   test881

;setup CRC constant FFFA0480
;============================
setup:
    xldi    r11,0xff
    xldi    r12,0xfa
    xldi    r13,0x04
    xldi    r14,0x80
    ret

;CRC32 - calculate a CRC value for a 56-bit message (over 32-bit)
;================================================================
;Input: r0..r3, r11..r14
;Output: r0..r2
;Used: r16
;Duration: 50% 0/1 = 32x10+16x4+1=365 cycles = 18.25 Ás @ 20 MHz clock
crc32:
    ldi    r16,32    ;1
crc322:
    sbrs   r0,7      ;2
    rjmp   crc321
    eor    r0,r11    ;1
    eor    r1,r12    ;1
    eor    r2,r13    ;1
    eor    r3,r14    ;1
crc321: 
    clc              ;1
    rol    r3        ;1
    rol    r2        ;1
    rol    r1        ;1
    rol    r0        ;1
    dec    r16       ;1
    brne   crc322    ;2
   ret

;CRC88 - calculate a CRC value for a 112-bit message (over 88-bit)
;=================================================================
;Input: r0..r10, r11..r14
;Output: r0..r2
;Used: r16
;Duration: 50% 0/1 = 88x14+44x4+1= 1409 cycles = 70,45 cycles
crc88:
    ldi    r16,88
crc882:
    sbrs   r0,7
    rjmp   crc881
    eor    r0,r11
    eor    r1,r12
    eor    r2,r13
    eor    r3,r14
crc881:    clc
    rol    r10
    rol    r9
    rol    r8
    rol    r7
    rol    r6
    rol    r5
    rol    r4
    rol    r3
    rol    r2
    rol    r1
    rol    r0
    dec    r16
    brne   crc882
    ret    



Downlink format Byte 0 Content CRC
DF0 (56 bit) 00000=0L Short Air to Air ACAS CRC32 xor ACID
DF4 (56 bit) 00100=2L Surveillance (roll call) Altitude CRC32 xor ACID
DF5 (56 bit) 00101=2H Surveillance (roll call) IDENT Reply CRC32 xor ACID
DF11 (56 bit) 01011=5H Mode S Only All-Call Reply (Acq. Squitter if II=0) CRC32 xor IRID or 000000
DF16 (112 bit) 10000=8L Long Air to Air ACAS
DF17 (112 bit) 10001=8H ADS-B Extended Squitter CRC88 xor 000000
DF18 (112 bit) 10001=9LH TIS-B CRC88 xor 000000
DF19 (112 bit) 10011=9H ADS-M Military Extended Squitter CRC88 xor 000000
DF20 (112 bit) 10100=AL Comm B Altitude Reply CRC88 xor ACID
DF21 (112 bit) 10101=AH Comm. B IDENT Reply CRC88 xor ACID
DF22 10110=BL Military use only
DF24 11000=CL Comm. D Extended Length Message (ELM)



 




Free Homepage by web99.de