Hi freaks,
So I wanted to use USART to interface a ps2 keyboard on a AVR128DB28. With the code I've written to test received data, I read scan codes successfully. However, once I enable loop-back (without any further modifications to the code). I start getting regular parity/frame errors (the data itself is also corrupted). Anyone has a clue why?
The code:
.dseg ps2ptr: .byte 2 .cseg ps2init: push r16 ; setup usart module ldi r16, 0b1000_1_000 ; RX Complete ISR and Loop-back Mode sts USART0_CTRLA, r16 ldi r16, 0b1000_1_000 ; RX enable and open drain sts USART0_CTRLB, r16 ldi r16, 0b01_11_0_011 ; SYNC, ODD parity, 1 Sp, 8bit sts USART0_CTRLC, r16 ldi r16, 0b00_00_00_01 ; usart to pins: P4-P7 sts PORTMUX_USARTROUTEA, r16 ; setup clock pin ldi r16, 0b0_0_00_1_000 ; ~invert and enable pullup sts PORTA_PIN6CTRL, r16 ldi r16, 0b01000000 ; set xck as input sts PORTA_DIRCLR, r16 ; setup data pin ldi r16, 0b0_0_00_1_000 ; enable pullup sts PORTA_PIN5CTRL, r16 ; setup test pointer ldi r16, LOW(vgafb) ; initializes ps2ptr with start of sram for testing purposes (vgafb is defined on another file) sts ps2ptr, r16 ldi r16, HIGH(vgafb) sts ps2ptr+1, r16 pop r16 ret ; Receive Complete (RXC) Interrupt ips2rx: push r16 push ZL push ZH lds r16, USART0_RXDATAH ; load flags ldi ZL, LOW(vgafb) ldi ZH, HIGH(vgafb) st Z, r16 ; save flags to start of ram andi r16, 0x7F brne _ips2rxend ; if error flags are set, end procedure lds ZL, ps2ptr lds ZH, ps2ptr+1 lds r16, USART0_RXDATAL st Z+, r16 ; read received data, and store it in ram sequentially cpi ZL, 0x80 brne _ips2rxend cpi ZH, 0x65 brne _ips2rxend ldi r16, LOW(vgafb) sts ps2ptr, r16 ldi r16, HIGH(vgafb) sts ps2ptr+1, r16 ; don't store outside of vgafb (for testing purposes) _ips2rxend: sts ps2ptr, ZL sts ps2ptr+1, ZH lds r16, USART0_RXDATAL ; ensure RXDATA is emptied pop ZH pop ZL pop r16 reti
-Miguel
Edit: Added some comments to the code for readability