aboutsummaryrefslogtreecommitdiffstats
path: root/examples/cxx-api
ModeNameSize
-rw-r--r--demomain.cc536logstatsplain
-rw-r--r--evaldemo.cc1350logstatsplain
'#n19'>19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
/* Name: usbdrvasm18.inc
 * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers
 * Author: Lukas Schrittwieser (based on 20 MHz usbdrvasm20.inc by Jeroen Benschop)
 * Creation Date: 2009-01-20
 * Tabsize: 4
 * Copyright: (c) 2008 by Lukas Schrittwieser and OBJECTIVE DEVELOPMENT Software GmbH
 * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
 * Revision: $Id: usbdrvasm18-crc.inc 740 2009-04-13 18:23:31Z cs $
 */

/* Do not link this file! Link usbdrvasm.S instead, which includes the
 * appropriate implementation!
 */

/*
General Description:
This file is the 18 MHz version of the asssembler part of the USB driver. It
requires a 18 MHz crystal (not a ceramic resonator and not a calibrated RC
oscillator).

See usbdrv.h for a description of the entire driver.

Since almost all of this code is timing critical, don't change unless you
really know what you are doing! Many parts require not only a maximum number
of CPU cycles, but even an exact number of cycles!
*/


;max stack usage: [ret(2), YL, SREG, YH, [sofError], bitcnt(x5), shift, x1, x2, x3, x4, cnt, ZL, ZH] = 14 bytes
;nominal frequency: 18 MHz -> 12 cycles per bit
; Numbers in brackets are clocks counted from center of last sync bit
; when instruction starts
;register use in receive loop to receive the data bytes:
; shift assembles the byte currently being received
; x1 holds the D+ and D- line state
; x2 holds the previous line state
; cnt holds the number of bytes left in the receive buffer
; x3 holds the higher crc byte (see algorithm below)
; x4 is used as temporary register for the crc algorithm
; x5 is used for unstuffing: when unstuffing the last received bit is inverted in shift (to prevent further
;    unstuffing calls. In the same time the corresponding bit in x5 is cleared to mark the bit as beening iverted
; zl lower crc value and crc table index
; zh used for crc table accesses

;--------------------------------------------------------------------------------------------------------------
; CRC mods:
;  table driven crc checker, Z points to table in prog space
;   ZL is the lower crc byte, x3 is the higher crc byte
;	x4 is used as temp register to store different results
;	the initialization of the crc register is not 0xFFFF but 0xFE54. This is because during the receipt of the
;	first data byte an virtual zero data byte is added to the crc register, this results in the correct initial
;	value of 0xFFFF at beginning of the second data byte before the first data byte is added to the crc.
;	The magic number 0xFE54 results form the crc table: At tabH[0x54] = 0xFF = crcH (required) and
;	tabL[0x54] = 0x01  ->  crcL = 0x01 xor 0xFE = 0xFF
;  bitcnt is renamed to x5 and is used for unstuffing purposes, the unstuffing works like in the 12MHz version
;--------------------------------------------------------------------------------------------------------------
; CRC algorithm:
;	The crc register is formed by x3 (higher byte) and ZL (lower byte). The algorithm uses a 'reversed' form
;	i.e. that it takes the least significant bit first and shifts to the right. So in fact the highest order
;	bit seen from the polynomial devision point of view is the lsb of ZL. (If this sounds strange to you i
;	propose a research on CRC :-) )
;	Each data byte received is xored to ZL, the lower crc byte. This byte now builds the crc
;	table index. Next the new high byte is loaded from the table and stored in x4 until we have space in x3
;	(its destination).
;	Afterwards the lower table is loaded from the table and stored in ZL (the old index is overwritten as
;	we don't need it anymore. In fact this is a right shift by 8 bits.) Now the old crc high value is xored
;	to ZL, this is the second shift of the old crc value. Now x4 (the temp reg) is moved to x3 and the crc
; 	calculation is done.
;	Prior to the first byte the two CRC register have to be initialized to 0xFFFF (as defined in usb spec)
;	however the crc engine also runs during the receipt of the first byte, therefore x3 and zl are initialized
;	to a magic number which results in a crc value of 0xFFFF after the first complete byte.
;
;	This algorithm is split into the extra cycles of the different bits:
;	bit7:	XOR the received byte to ZL
;	bit5:	load the new high byte to x4
;	bit6:	load the lower xor byte from the table, xor zl and x3, store result in zl (=the new crc low value)
;			move x4 (the new high byte) to x3, the crc value is ready
;


macro POP_STANDARD ; 18 cycles
    pop		ZH
    pop		ZL
	pop     cnt
    pop     x5
    pop     x3
    pop     x2
    pop     x1
    pop     shift
    pop     x4
    endm
macro POP_RETI     ; 7 cycles
    pop     YH
    pop     YL
    out     SREG, YL
    pop     YL
    endm

macro CRC_CLEANUP_AND_CHECK
	; the last byte has already been xored with the lower crc byte, we have to do the table lookup and xor
	; x3 is the higher crc byte, zl the lower one
	ldi		ZH, hi8(usbCrcTableHigh);[+1] get the new high byte from the table
	lpm		x2, Z				;[+2][+3][+4]
	ldi		ZH, hi8(usbCrcTableLow);[+5] get the new low xor byte from the table
	lpm		ZL, Z				;[+6][+7][+8]
	eor		ZL, x3				;[+7] xor the old high byte with the value from the table, x2:ZL now holds the crc value
	cpi		ZL, 0x01			;[+8] if the crc is ok we have a fixed remainder value of 0xb001 in x2:ZL (see usb spec)
	brne	ignorePacket		;[+9] detected a crc fault -> paket is ignored and retransmitted by the host
	cpi		x2, 0xb0			;[+10]
	brne	ignorePacket		;[+11] detected a crc fault -> paket is ignored and retransmitted by the host
    endm


USB_INTR_VECTOR:
;order of registers pushed: YL, SREG, YH, [sofError], x4, shift, x1, x2, x3, x5, cnt, ZL, ZH
    push    YL                  ;[-28] push only what is necessary to sync with edge ASAP
    in      YL, SREG            ;[-26]
    push    YL                  ;[-25]
    push    YH                  ;[-23]
;----------------------------------------------------------------------------
; Synchronize with sync pattern:
;----------------------------------------------------------------------------
;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K]
;sync up with J to K edge during sync pattern -- use fastest possible loops
;The first part waits at most 1 bit long since we must be in sync pattern.
;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to
;waitForJ, ensure that this prerequisite is met.
waitForJ:
    inc     YL
    sbis    USBIN, USBMINUS
    brne    waitForJ        ; just make sure we have ANY timeout
waitForK:
;The following code results in a sampling window of < 1/4 bit which meets the spec.
    sbis    USBIN, USBMINUS     ;[-17]
    rjmp    foundK              ;[-16]
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
    sbis    USBIN, USBMINUS
    rjmp    foundK
#if USB_COUNT_SOF
    lds     YL, usbSofCount
    inc     YL
    sts     usbSofCount, YL
#endif  /* USB_COUNT_SOF */
#ifdef USB_SOF_HOOK
    USB_SOF_HOOK
#endif
    rjmp    sofError
foundK:                         ;[-15]
;{3, 5} after falling D- edge, average delay: 4 cycles
;bit0 should be at 30  (2.5 bits) for center sampling. Currently at 4 so 26 cylces till bit 0 sample
;use 1 bit time for setup purposes, then sample again. Numbers in brackets
;are cycles from center of first sync (double K) bit after the instruction
    push    x4                  ;[-14]
;   [---]                       ;[-13]
    lds     YL, usbInputBufOffset;[-12] used to toggle the two usb receive buffers
;   [---]                       ;[-11]
    clr     YH                  ;[-10]
    subi    YL, lo8(-(usbRxBuf));[-9] [rx loop init]
    sbci    YH, hi8(-(usbRxBuf));[-8] [rx loop init]
    push    shift               ;[-7]
;   [---]                       ;[-6]
    ldi		shift, 0x80			;[-5] the last bit is the end of byte marker for the pid receiver loop
    clc			      	      	;[-4] the carry has to be clear for receipt of pid bit 0
    sbis    USBIN, USBMINUS     ;[-3] we want two bits K (sample 3 cycles too early)
    rjmp    haveTwoBitsK        ;[-2]
    pop     shift               ;[-1] undo the push from before
    pop     x4                  ;[1]
    rjmp    waitForK            ;[3] this was not the end of sync, retry
; The entire loop from waitForK until rjmp waitForK above must not exceed two
; bit times (= 24 cycles).

;----------------------------------------------------------------------------
; push more registers and initialize values while we sample the first bits:
;----------------------------------------------------------------------------
haveTwoBitsK:
    push    x1                  ;[0]
    push    x2                  ;[2]
    push    x3                  ;[4] crc high byte
    ldi     x2, 1<<USBPLUS      ;[6] [rx loop init] current line state is K state. D+=="1", D-=="0"
    push    x5                  ;[7]
    push    cnt                 ;[9]
    ldi     cnt, USB_BUFSIZE    ;[11]


;--------------------------------------------------------------------------------------------------------------
; receives the pid byte
; there is no real unstuffing algorithm implemented here as a stuffing bit is impossible in the pid byte.
; That's because the last four bits of the byte are the inverted of the first four bits. If we detect a
; unstuffing condition something went wrong and abort
; shift has to be initialized to 0x80
;--------------------------------------------------------------------------------------------------------------

; pid bit 0 - used for even more register saving (we need the z pointer)
	in      x1, USBIN           ;[0] sample line state
    andi    x1, USBMASK         ;[1] filter only D+ and D- bits
    eor		x2, x1				;[2] generate inverted of actual bit
	sbrc	x2, USBMINUS		;[3] if the bit is set we received a zero
	sec							;[4]
	ror		shift				;[5] we perform no unstuffing check here as this is the first bit
	mov		x2, x1				;[6]
	push	ZL					;[7]
								;[8]
	push	ZH					;[9]
								;[10]
	ldi		x3, 0xFE			;[11] x3 is the high order crc value


bitloopPid:						
	in      x1, USBIN           ;[0] sample line state
   	andi    x1, USBMASK         ;[1] filter only D+ and D- bits
    breq    nse0                ;[2] both lines are low so handle se0	
	eor		x2, x1				;[3] generate inverted of actual bit
	sbrc	x2, USBMINUS		;[4] set the carry if we received a zero
	sec							;[5]
	ror		shift				;[6]
	ldi		ZL, 0x54			;[7] ZL is the low order crc value
	ser		x4					;[8] the is no bit stuffing check here as the pid bit can't be stuffed. if so
								; some error occured. In this case the paket is discarded later on anyway.
	mov		x2, x1				;[9] prepare for the next cycle
	brcc	bitloopPid			;[10] while 0s drop out of shift we get the next bit
	eor		x4, shift			;[11] invert all bits in shift and store result in x4

;--------------------------------------------------------------------------------------------------------------
; receives data bytes and calculates the crc
; the last USBIN state has to be in x2
; this is only the first half, due to branch distanc limitations the second half of the loop is near the end
; of this asm file
;--------------------------------------------------------------------------------------------------------------

rxDataStart:
    in      x1, USBIN           ;[0] sample line state (note: a se0 check is not useful due to bit dribbling)
    ser		x5					;[1] prepare the unstuff marker register
    eor		x2, x1             	;[2] generates the inverted of the actual bit
    bst		x2, USBMINUS       	;[3] copy the bit from x2
    bld		shift, 0	        ;[4] and store it in shift
    mov		x2, shift	     	;[5] make a copy of shift for unstuffing check
    andi	x2, 0xF9	      	;[6] mask the last six bits, if we got six zeros (which are six ones in fact)
    breq	unstuff0	      	;[7] then Z is set now and we branch to the unstuffing handler
didunstuff0:
	subi    cnt, 1         		;[8] cannot use dec because it doesn't affect the carry flag
    brcs    nOverflow    		;[9] Too many bytes received. Ignore packet							
    st		Y+, x4				;[10] store the last received byte
								;[11] st needs two cycles

; bit1							
	in		x2, USBIN			;[0] sample line state
    andi	x1, USBMASK			;[1] check for se0 during bit 0
    breq	nse0				;[2]
    andi	x2, USBMASK			;[3] check se0 during bit 1
    breq	nse0				;[4]
	eor		x1, x2				;[5]
    bst		x1, USBMINUS		;[6]
    bld 	shift, 1	 		;[7]
    mov		x1, shift			;[8]
    andi	x1, 0xF3			;[9]
    breq	unstuff1			;[10]
didunstuff1:
	nop							;[11]	

; bit2
	in      x1, USBIN           ;[0] sample line state
    andi	x1, USBMASK			;[1] check for se0 (as there is nothing else to do here
	breq	nOverflow	 		;[2]
    eor		x2, x1              ;[3] generates the inverted of the actual bit
    bst		x2, USBMINUS		;[4]
    bld		shift, 2			;[5] store the bit
    mov		x2, shift			;[6]
    andi	x2, 0xE7			;[7] if we have six zeros here (which means six 1 in the stream)
    breq	unstuff2			;[8] the next bit is a stuffing bit
didunstuff2:
	nop2						;[9]
								;[10]
	nop							;[11]					
					
; bit3							
	in		x2, USBIN			;[0] sample line state
    andi	x2, USBMASK			;[1] check for se0
    breq	nOverflow           ;[2]
    eor		x1, x2				;[3]
    bst		x1, USBMINUS		;[4]
    bld 	shift, 3	 		;[5]
    mov		x1, shift			;[6]
    andi	x1, 0xCF			;[7]
    breq	unstuff3			;[8]
didunstuff3:
	nop							;[9]
	rjmp 	rxDataBit4			;[10]
								;[11]				

; the avr branch instructions allow an offset of +63 insturction only, so we need this
; 'local copy' of se0
nse0:		
	rjmp	se0					;[4]
								;[5]
; the same same as for se0 is needed for overflow and StuffErr
nOverflow:
stuffErr:
	rjmp	overflow


unstuff0:						;[8] this is the branch delay of breq unstuffX
	andi	x1, USBMASK			;[9] do an se0 check here (if the last crc byte ends with 5 one's we might end up here
	breq	didunstuff0			;[10] event tough the message is complete -> jump back and store the byte
	ori		shift, 0x01			;[11] invert the last received bit to prevent furhter unstuffing
	in		x2, USBIN			;[0] we have some free cycles so we could check for bit stuffing errors
	andi	x5, 0xFE			;[1] mark this bit as inverted (will be corrected before storing shift)
	eor		x1, x2				;[2] x1 and x2 have to be different because the stuff bit is always a zero
	andi	x1, USBMASK			;[3] mask the interesting bits
	breq	stuffErr			;[4] if the stuff bit is a 1-bit something went wrong
	mov 	x1, x2				;[5] the next bit expects the last state to be in x1
	rjmp 	didunstuff0			;[6]
								;[7] jump delay of rjmp didunstuffX	

unstuff1:						;[11] this is the jump delay of breq unstuffX
	in		x1, USBIN			;[0] we have some free cycles so we could check for bit stuffing errors
	ori		shift, 0x02			;[1] invert the last received bit to prevent furhter unstuffing
	andi	x5, 0xFD			;[2] mark this bit as inverted (will be corrected before storing shift)
	eor		x2, x1				;[3] x1 and x2 have to be different because the stuff bit is always a zero
	andi	x2, USBMASK			;[4] mask the interesting bits
	breq	stuffErr			;[5] if the stuff bit is a 1-bit something went wrong
	mov 	x2, x1				;[6] the next bit expects the last state to be in x2
	nop2						;[7]
								;[8]
	rjmp 	didunstuff1			;[9]
								;[10] jump delay of rjmp didunstuffX		

unstuff2:						;[9] this is the jump delay of breq unstuffX
	ori		shift, 0x04			;[10] invert the last received bit to prevent furhter unstuffing
	andi	x5, 0xFB			;[11] mark this bit as inverted (will be corrected before storing shift)
	in		x2, USBIN			;[0] we have some free cycles so we could check for bit stuffing errors
	eor		x1, x2				;[1] x1 and x2 have to be different because the stuff bit is always a zero
	andi	x1, USBMASK			;[2] mask the interesting bits
	breq	stuffErr			;[3] if the stuff bit is a 1-bit something went wrong
	mov 	x1, x2				;[4] the next bit expects the last state to be in x1
	nop2						;[5]
								;[6]
	rjmp 	didunstuff2			;[7]
								;[8] jump delay of rjmp didunstuffX	

unstuff3:						;[9] this is the jump delay of breq unstuffX
	ori		shift, 0x08			;[10] invert the last received bit to prevent furhter unstuffing
	andi	x5, 0xF7			;[11] mark this bit as inverted (will be corrected before storing shift)
	in		x1, USBIN			;[0] we have some free cycles so we could check for bit stuffing errors
	eor		x2, x1				;[1] x1 and x2 have to be different because the stuff bit is always a zero
	andi	x2, USBMASK			;[2] mask the interesting bits
	breq	stuffErr			;[3] if the stuff bit is a 1-bit something went wrong
	mov 	x2, x1				;[4] the next bit expects the last state to be in x2
	nop2						;[5]
								;[6]
	rjmp 	didunstuff3			;[7]
								;[8] jump delay of rjmp didunstuffX			



; the include has to be here due to branch distance restirctions
#define __USE_CRC__
#include "asmcommon.inc"

	

; USB spec says:
; idle = J
; J = (D+ = 0), (D- = 1)
; K = (D+ = 1), (D- = 0)