Rev 3332 Rev 3333
Line 1... Line 1...
1 ; ====================================================================== 1 ; ======================================================================
2 ; Calculate and append CRC 2 ; Calculate and append CRC
3 ; 3 ;
4 ; The CRC is calculated 4 bits at a time, using a precomputed table of 4 ; There are two versions of the CRC16 calculation, selectable by the
-   5 ; USBTINY_FAST_CRC macro. The default implementation calculates one bit
5 ; 16 values. Each value is 16 bits, but only the 8 significant bits are 6 ; at a time, and is compact but relatively slow. The "fast" version
6 ; stored. The table should not cross a 256-byte page. The check.py script 7 ; processes 4 bits at a time, and is about twice as fast, but 42 bytes
7 ; will check for this. 8 ; larger.
8 ; 9 ;
9 ; A bitwise algorithm would be a little smaller, but takes more time. 10 ; The fast version calculates 4 bits at a time, using a precomputed table
10 ; In fact, it takes too much time for the USB controller in my laptop. -  
11 ; The poll frequently is so high, that a lot of time is spent in the 11 ; of 16 values. Each value is 16 bits, but only the 8 significant bits
12 ; interrupt handler, sending NAK packets, leaving little time for the 12 ; are stored. The table should not cross a 256-byte page. The check.py
13 ; actual checksum calculation. An 8 bit algoritm would be even faster, 13 ; script will check for this. An 8 bit algoritm would be even faster,
14 ; but requires a lookup table of 512 bytes. 14 ; but requires a lookup table of 512 bytes.
15 ; 15 ;
16 ; Copyright (C) 2006 Dick Streefland 16 ; Copyright 2006-2010 Dick Streefland
17 ; 17 ;
18 ; This is free software, licensed under the terms of the GNU General 18 ; This is free software, licensed under the terms of the GNU General
19 ; Public License as published by the Free Software Foundation. 19 ; Public License as published by the Free Software Foundation.
20 ; ====================================================================== 20 ; ======================================================================
21   21  
22 #include "def.h" 22 #include "def.h"
23   23  
-   24 #if USBTINY_FAST_CRC
24 ; ---------------------------------------------------------------------- 25 ; ----------------------------------------------------------------------
25 ; void crc(unsigned char *data, unsigned char len); 26 ; void crc(unsigned char *data, unsigned char len);
26 ; ---------------------------------------------------------------------- 27 ; ----------------------------------------------------------------------
27 #define data r24 28 #define data r24
28 #define len r22 29 #define len r22
Line 39... Line 40...
39 crc: 40 crc:
40 ; crc = 0xffff 41 ; crc = 0xffff
41 movw XL, r24 42 movw XL, r24
42 ldi crc_h, 0xff 43 ldi crc_h, 0xff
43 ldi crc_l, 0xff 44 ldi crc_l, 0xff
44 lsl len -  
45 breq done -  
46 ldi zl, lo8(crc4tab) 45 ldi zl, lo8(crc4tab)
47 ldi ZH, hi8(crc4tab) 46 ldi ZH, hi8(crc4tab)
-   47 rjmp entry
48   48  
49 next_nibble: 49 next_byte:
50 ; b = (len & 1 ? b >> 4 : *data++) -  
51 swap b -  
52 sbrs len, 0 50 ; crc ^= b
53 ld b, X+ 51 ld b, X+
-   52 eor crc_l, b
54   53  
55 ; index = (crc ^ b) & 0x0f 54 ; index1 = crc & 0x0f
56 mov ZL, crc_l 55 mov ZL, crc_l
57 eor ZL, b -  
58 andi ZL, 0x0f 56 andi ZL, 0x0f
59   57  
-   58 ; tmp = crc4tab[index1]
-   59 add ZL, zl
-   60 lpm tmp, Z+
-   61  
-   62 ; index2 = (crc >> 4)
-   63 mov ZL, crc_l
-   64 swap ZL
-   65  
60 ; crc >>= 4 66 ; crc >>= 8
-   67 mov crc_l, crc_h
-   68  
-   69 ; index2 = (index2 ^ tmp) & 0xf
-   70 mov crc_h, tmp
-   71 andi tmp, 1
-   72 eor ZL, tmp
-   73 andi ZL, 0x0f
-   74  
-   75 ; treat upper byte of CRC remainder
61 swap crc_h 76 swap crc_h
62 swap crc_l -  
63 andi crc_l, 0x0f -  
64 mov tmp, crc_h 77 mov tmp, crc_h
65 andi tmp, 0xf0 -  
66 or crc_l, tmp -  
67 andi crc_h, 0x0f 78 andi crc_h, 0x0f
-   79 andi tmp, 0xe0
-   80 eor crc_l, tmp
68   81  
69 ; crc ^= crc4tab[index] 82 ; crc ^= crc4tab[index]
70 add ZL, zl 83 add ZL, zl
71 lpm tmp, Z+ 84 lpm tmp, Z+
72 eor crc_h, tmp 85 eor crc_h, tmp
73 andi tmp, 1 86 andi tmp, 1
74 eor crc_h, tmp 87 eor crc_h, tmp
75 eor crc_l, tmp 88 eor crc_l, tmp
76   89  
-   90 entry:
77 ; next nibble 91 ; next byte
78 dec len 92 dec len
79 brne next_nibble 93 brpl next_byte
80   94  
81 done: 95 done:
82 ; crc ^= 0xffff 96 ; crc ^= 0xffff
83 com crc_l 97 com crc_l
84 com crc_h 98 com crc_h
Line 91... Line 105...
91   105  
92 ; ---------------------------------------------------------------------- 106 ; ----------------------------------------------------------------------
93 ; CRC table. As bits 1..8 are always zero, omit them. 107 ; CRC table. As bits 1..8 are always zero, omit them.
94 ; ---------------------------------------------------------------------- 108 ; ----------------------------------------------------------------------
95 .section .progmem.crc,"a",@progbits 109 .section .progmem.crc,"a",@progbits
96 ;;; .align 4 ; avoid crossing a page boundary 110 ;;; .align 4 ; crude way to avoid crossing a page boundary
97 crc4tab: 111 crc4tab:
98 .byte 0x00+0x00 112 .byte 0x00+0x00
99 .byte 0xcc+0x01 113 .byte 0xcc+0x01
100 .byte 0xd8+0x01 114 .byte 0xd8+0x01
101 .byte 0x14+0x00 115 .byte 0x14+0x00
Line 119... Line 133...
119 crc >>= 1 133 crc >>= 1
120 if xor: 134 if xor:
121 crc ^= 0xA001 # X^16 + X^15 + X^2 + 1 (reversed) 135 crc ^= 0xA001 # X^16 + X^15 + X^2 + 1 (reversed)
122 print "\t.byte\t0x%02x+0x%02x" % (crc >> 8, crc & 0xff) 136 print "\t.byte\t0x%02x+0x%02x" % (crc >> 8, crc & 0xff)
123 \* ---------------------------------------------------------------------- */ 137 \* ---------------------------------------------------------------------- */
-   138 #else
-   139 ; ----------------------------------------------------------------------
-   140 ; void crc(unsigned char *data, unsigned char len);
-   141 ; ----------------------------------------------------------------------
-   142 #define data r24
-   143 #define len r22
-   144  
-   145 #define b r18
-   146 #define con_01 r19
-   147 #define con_a0 r20
-   148 #define crc_l r24
-   149 #define crc_h r25
-   150  
-   151 .text
-   152 .global crc
-   153 .type crc, @function
-   154 crc:
-   155 movw XL, r24
-   156 ldi crc_h, 0xff
-   157 ldi crc_l, 0xff
-   158 tst len
-   159 breq done1
-   160 ldi con_a0, 0xa0
-   161 ldi con_01, 0x01
-   162 next_byte:
-   163 ld b, X+
-   164 eor crc_l, b
-   165 ldi b, 8
-   166 next_bit:
-   167 lsr crc_h
-   168 ror crc_l
-   169 brcc noxor
-   170 eor crc_h, con_a0
-   171 eor crc_l, con_01
-   172 noxor:
-   173 dec b
-   174 brne next_bit
-   175 dec len
-   176 brne next_byte
-   177 done1:
-   178 com crc_l
-   179 com crc_h
-   180 st X+, crc_l
-   181 st X+, crc_h
-   182 ret
-   183 #endif