This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Receiving then transmitting from keyboard input

I have this code that needs modifying to be able to accept a keyboard input and then transmit to serial window UART #1, 2 or 3 by creating a loop that receives from UART 1 then Transmit to UART 1. I am a little perplexed on how to begin this code because I have only been dealing with loading string texts loaded in memory. Any assistance would be helpful.

;   modified UART code for LPC2104 from Hohl and Hinds, Section 16.2.5
;   The modifications are to use push/pop instead of stmia/ldmdb

        area uartdemo, code, readonly
pinsel0  equ  0xe002c000                ; controls function of pins
u1start  equ  0xe000c000                ; start of UART0 registers
lcr0  equ  0xc                  ; line control register for UART0
lsr0  equ  0x14                 ; line status register for UART0
ramstart  equ  0x40000000       ; start of onboard RAM for 2104
stackstart  equ  0x40000200     ; start of stack

        entry

start
        ldr sp, = stackstart    ; set up stack pointer
        bl UARTConfig           ; initialize/configure UART0
        ldr r1, = CharData      ; starting address of characters

;input



loop
        ldrb r0, [r1], #1               ; load character, increment address
        cmp r0, #0              ; null terminated?
        blne Transmit           ; send character to UART
        bne loop                ; continue if not a '0'
done    b done                  ; otherwise, we are done

; subroutine UARTConfig
;   Configures the I/O pins first.
;   Then sets up the UART control register.
;   Parameters set to 8 bits, no parity, and 1 stop bit.
;   Registers used:
;   r5 - scratch register
;   r6 - scratch register
;   inputs:  none
;   outputs:  none

UARTConfig
        push {r5, r6, lr}
        ldr r5, = pinsel0       ; base address of register
        ldr r6, [r5]            ; get contents
        bic r6, r6, #0xf        ; clear out lower nibble
        orr r6, r6, #0x5        ; sets P0.0 to Tx0 and P0.1 to Rx0
        str r6, [r5]            ; r/modify/w back to register
        ldr r5, = u1start
        mov r6, #0x83           ; set 8 bits, no parity, 1 stop bit
        strb r6, [r5, #lcr0]    ; write control byte to LCR
        mov r6, #0x61           ; 9600 baud @ 15MHz VPB clock
        strb r6, [r5]           ; store control byte
        mov r6, #3              ; set DLAB = 0
        strb r6, [r5, #lcr0]    ; Tx and Rx buffers set up
        pop {r5, r6, pc}

; subroutine Transmit
;   Puts one byte into the UART for transmitting
;   Registers used:
;   r5 - scratch register
;   r6 - scratch register
;   inputs:  r0 - byte to transmit
;   outputs:  none

Transmit
        push {r5, r6, lr}
        ldr r5, = u1start
wait
        ldrb r6, [r5, #lsr0]    ; get status of buffer
        cmp r6, #0x20           ; buffer empty?
        beq wait                ; spin until buffer is empty
        strb r0, [r5]
        pop {r5, r6, pc}

CharData  dcb  "Hello Professor! How are you?", 0

        end

  • So start with a loop.
    Not too hard to implement.
    Expand the loop to check if there are any incoming data from any UART to pick up and place in a receive buffer.
    Expand the loop to check if there are any buffered data that should be sent to any UART - and if that UART is ready to accept more.
    Expand the loop to scan a keyboard, and if a key is detected store in a receive buffer.

    What exact problems do you see with implementing a basic program skeleton and then add features one at a time? All software is created by the creation of individual blocks that are merged into larger and larger programs - or maybe into macro-blocks that are then merged into the program. So it's similar to running a marathon - you do it one step at a time.

  • Ok, so far I have the following code, but still unable to see any results in serial window uart #1 or #2. I am also unable to type any characters in either of the mentioned windows.
    so even if the code was incorrect, how do I fix the problem of being able to type characters in the uart windows? I can get the cursor to flash in the windows but it does not receive any texts from the keyboard.

    ; modified UART code for LPC2104 from Hohl and Hinds, Section 16.2.5
    ;   The modifications are to use push/pop instead of stmia/ldmdb
    
            area uartdemo, code, readonly
    pinsel0  equ  0xe002C000        ; controls function of pins
    u1start  equ  0xE0010000        ; start of UART1 registers
    
    
    lcr0  equ  0xC          ; line control register for UART0
    lsr0  equ  0x14         ; line status register for UART0
    ramstart  equ  0x40000000       ; start of onboard RAM for 2104
    stackstart  equ  0x40000200  ; start of stack
    
            entry
    
    start
            ldr sp, = stackstart    ; set up stack pointer
            bl UARTConfig   ; initialize/configure UART0
            ;
    go_again
            bl Transmit
            bl send
            bl go_again
    ;loop
            ;ldrb r0, [r1], #1      ; load character, increment address
            ;cmp r0, #0             ; null terminated?
            ;blne Transmit          ; send character to UART
            ;bne loop               ; continue if not a '0'
    ;done   b done                  ; otherwise, we are done
    
    ; subroutine UARTConfig
    ;   Configures the I/O pins first.
    ;   Then sets up the UART control register.
    ;   Parameters set to 8 bits, no parity, and 1 stop bit.
    ;   Registers used:
    ;   r5 - scratch register
    ;   r6 - scratch register
    ;   inputs:  none
    ;   outputs:  none
    
    UARTConfig
            push {r5, r6, lr}
            ldr r5, = pinsel0       ; base address of register
            ldr r6, [r5]            ; get contents
            bic r6, r6, #0xF0000            ; clear out lower nibble
            bic r6, r6, #0x0000F            ; clear out lower nibble
            orr r6, r6, #0x50000    ; sets P0.8 and P0.9
            orr r6, r6, #0x00005    ; sets P0.0 to Tx0 and P0.1 to Rx0
            str r6, [r5]            ; r/modify/w back to register
            ldr r5, = u1start
            mov r6, #0x83           ; set 8 bits, no parity, 1 stop bit
            strb r6, [r5, #lcr0]    ; write control byte to LCR
            mov r6, #0x61           ; 9600 baud @ 15MHz VPB clock
            strb r6, [r5]           ; store control byte
            mov r6, #3              ; set DLAB = 0
            strb r6, [r5, #lcr0]    ; Tx and Rx buffers set up
            pop {r5, r6, pc}
    
    ; subroutine Transmit
    ;   Puts one byte into the UART for transmitting
    ;   Registers used:
    ;   r5 - scratch register
    ;   r6 - scratch register
    ;   inputs:  r0 - byte to transmit
    ;   outputs:  none
    
    Transmit
            push {r5, r6, lr}
            ldr r5, = u1start
    
    send
                    ldr r5, = u1start
                    LDRB r6, [r5, #lsr0]
                    tst      r0, #0x5f
                    beq wait
                    LDRB r0, [r5]
                    ;LDR r5,=u1start
    
    
    wait    ldrb r6, [r5, #lsr0]    ; get status of buffer
                    tst r6, #0x20           ; buffer empty?
                    beq wait                ; spin until buffer is empty
                    strb r0, [r5]
                    pop {r5, r6, pc}
                    b       Transmit
    
    
    
    
    
            end