R32V2020 UART loopback Example

From Land Boards Wiki
Jump to navigation Jump to search

Synopsis of UART loopback example

  • Uses an ACIA (UART) to receive and send characters
  • Loopback picks up ACIA receive value and sends it out the ACIA transmit
  • Useful for testing a lot of stuff with very limited hardware

Psuedo-Code

  • Read the status register of the ACIA (UART)
  • Test receive data present bit to see if there is data in the receive buffer
  • If there is receive data load the data into register rX
  • Test the transmit buffer ready bit
  • If the transmit buffer is full loop until transmit buffer is not full
  • Send out the received data
  • Loop back to the start

Assembly Code

; starts with all registers initialized
startLoop:
  ADS r8,r0,r0       ; Clear r8 - Holds the peripheral status register address
  ADS r9,r0,r0       ; Clear r9 - Holds the peripheral data register address
  ADS r10,r0,r0       ; Clear r10 
  ADS r11,r0,r0       ; Clear r11
  LIL r8,0x1230      ; peripheral's status register address, held in r8 for loop
  LIL r9,0x1234      ; peripheral's data register address, held in r9 for loop
  LIL r10,0x0001     ; bit to test for receive data present (1 = data is present in receiver)
  LIL r11,0x0002     ; bit to test for transmit buffer empty (1 = transmitter ready to take data)
  ADS r5,r8,r0       ; transfers the contents of r8 into r5 (peripheral address register) (since r0 is always 0)
waitForRxData:
  LPB r12            ; read the status register from the peripheral bus
  ADS r12,r10,r12    ; AND the receive data present bit with the status register bit that was read (r10 AND r12 => R12)
                     ; the AND set the condition code register
  BEQ waitForRxData  ; tests the condition code register EQ=0 bit and jumps if there's no receive data yet
  ADS r5,r9,r0       ; set the peripheral address register to the peripheral data register address
  LDP r13            ; r13 will have the character that is in the receive buffer
  ADS r5,r9,r0       ; set the peripheral address to the peripheral's status register
waitForTxReady:
  LDP r12            ; read the status register from the peripheral bus
  ANS r12,r11,r12    ; AND the transmit data buffer empty bit with the status register but that was read
  BEQ waitForTxReady ; tests the condition code register EQ=0 bit and jumps if the transmit buffer is still full
  ADS r5,r9,r0       ; set the peripheral address register to the peripheral data register address
  SPB r13            ; store the data that was read from the receiver into the transmitter
  BRA waitForRxData  ; go get the next character

Opcodes used

  • lri - load register with immediate value
  • add - add two registers and store into a register (could be the same)
    • Can also be used in conjunction with r0 (always contains 0x0000 to copy one register to another register
  • ldp - load a register from the peripheral addressed by r5 (the peripheral address register)
  • beq - flow control branch if the carry flag is set in the Register-File#r3__Condition_Code_Register CCR (r3)
  • stp - store rX to peripheral pointed to by r5 (the peripheral address register)
  • bra - Branch always (branches don't affect the stack)