; ======================================================================; Calculate and append CRC;; The CRC is calculated 4 bits at a time, using a precomputed table of; 16 values. Each value is 16 bits, but only the 8 significant bits are; stored. The table should not cross a 256-byte page. The check.py script; will check for this.;; A bitwise algorithm would be a little smaller, but takes more time.; In fact, it takes too much time for the USB controller in my laptop.; The poll frequently is so high, that a lot of time is spent in the; interrupt handler, sending NAK packets, leaving little time for the; actual checksum calculation. An 8 bit algoritm would be even faster,; but requires a lookup table of 512 bytes.;; Copyright (C) 2006 Dick Streefland;; This is free software, licensed under the terms of the GNU General; Public License as published by the Free Software Foundation.; ======================================================================#include "def.h"; ----------------------------------------------------------------------; void crc(unsigned char *data, unsigned char len);; ----------------------------------------------------------------------#define data r24#define len r22#define b r18#define tmp r19#define zl r20#define crc_l r24#define crc_h r25.text.global crc.type crc, @functioncrc:; crc = 0xffffmovw XL, r24ldi crc_h, 0xffldi crc_l, 0xfflsl lenbreq doneldi zl, lo8(crc4tab)ldi ZH, hi8(crc4tab)next_nibble:; b = (len & 1 ? b >> 4 : *data++)swap bsbrs len, 0ld b, X+; index = (crc ^ b) & 0x0fmov ZL, crc_leor ZL, bandi ZL, 0x0f; crc >>= 4swap crc_hswap crc_landi crc_l, 0x0fmov tmp, crc_handi tmp, 0xf0or crc_l, tmpandi crc_h, 0x0f; crc ^= crc4tab[index]add ZL, zllpm tmp, Z+eor crc_h, tmpandi tmp, 1eor crc_h, tmpeor crc_l, tmp; next nibbledec lenbrne next_nibbledone:; crc ^= 0xffffcom crc_lcom crc_h; append crc to bufferst X+, crc_lst X+, crc_hret; ----------------------------------------------------------------------; CRC table. As bits 1..8 are always zero, omit them.; ----------------------------------------------------------------------.section .progmem.crc,"a",@progbits;;; .align 4 ; avoid crossing a page boundarycrc4tab:.byte 0x00+0x00.byte 0xcc+0x01.byte 0xd8+0x01.byte 0x14+0x00.byte 0xf0+0x01.byte 0x3c+0x00.byte 0x28+0x00.byte 0xe4+0x01.byte 0xa0+0x01.byte 0x6c+0x00.byte 0x78+0x00.byte 0xb4+0x01.byte 0x50+0x00.byte 0x9c+0x01.byte 0x88+0x01.byte 0x44+0x00/* ---------------------------------------------------------------------- *\#!/usr/bin/pythonfor crc in range(16):for bit in range(4):xor = crc & 1crc >>= 1if xor:crc ^= 0xA001 # X^16 + X^15 + X^2 + 1 (reversed)print "\t.byte\t0x%02x+0x%02x" % (crc >> 8, crc & 0xff)\* ---------------------------------------------------------------------- */