; main.

asm
; Runs on LM4F120/TM4C123
; Test the GPIO initialization functions by setting the LED
; color according to the status of the switches.
; The Reflex Test (no longer supported; each LED turns others off):
; This program is functionally similar to SwitchTestMain.c
; in Switch_4F120asm. When switch #1 is pressed, the blue
; LED comes on. When switch #2 is pressed, the red LED
; comes on. When both switches are pressed, the green LED
; comes on. A short delay is inserted between
; polls of the buttons to compensate for your reflexes and
; the button bounce. The following color combinations can
; be made:
; Color LED(s) Illumination Method
; dark --- release both buttons
; red R-- press right button (#2)
; blue --B press left button (#1)
; green -G- press both buttons exactly together
; yellow RG- press right button, then press left button
; sky blue -GB press left button, then press right button
; white RGB press either button, then press the other
; button, then release the first button
; pink R-B press either button, then release the
; first button and immediately press the
; other button
; Daniel Valvano
; May 3, 2015
; This example accompanies the book
; "Embedded Systems: Introduction to ARM Cortex M Microcontrollers"
; ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2015
; Section 4.2 Program 4.1
;
;Copyright 2015 by Jonathan W. Valvano, valvano@mail.utexas.edu
; You may use, edit, run or distribute this file
; as long as the above copyright notice remains
;THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
;OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
;MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
;VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
;OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
;For more information about my classes, my research, and my books, see
;http://users.ece.utexas.edu/~valvano/
; negative logic switch #2 connected to PF0 on the Launchpad
; red LED connected to PF1 on the Launchpad
; blue LED connected to PF2 on the Launchpad
; green LED connected to PF3 on the Launchpad
; negative logic switch #1 connected to PF4 on the Launchpad
; NOTE: The NMI (non-maskable interrupt) is on PF0. That means that
; the Alternate Function Select, Pull-Up Resistor, Pull-Down Resistor,
; and Digital Enable are all locked for PF0 until a value of 0x4C4F434B
; is written to the Port F GPIO Lock Register. After Port F is
; unlocked, bit 0 of the Port F GPIO Commit Register must be set to
; allow access to PF0's control registers. On the LM4F120, the other
; bits of the Port F GPIO Commit Register are hard-wired to 1, meaning
; that the rest of Port F can always be freely re-configured at any
; time. Requiring this procedure makes it unlikely to accidentally
; re-configure the JTAG and NMI pins as GPIO, which can lock the
; debugger out of the processor and make it permanently unable to be

R0 == 0x01? BEQ sw2pressed . if so.equ 0x01 .equ 0x08 SW1 .field 0x40004528.32 GPIO_PORTF_PCTL_R . on the right side of the Launchpad board SYSCTL_RCGCGPIO_R .32 GPIO_PORTA_DEN_R .#0 loop LDR R0.32 GPIO_LOCK_KEY .32 GPIO_PORTF_CR_R .equ 0x02 BLUE .field 0x40005520.field 0x400053FC.field 0x4002552C.field 0x40025520.32 GPIO_PORTB_PUR_R .32 GPIO_PORTA_LOCK_R . turn the blue LED on BL PortF_Input CMP R0.field 0x400FE608. initialize input and output pins of Port F MOV R12. debugged or re-programmed.32 GPIO_PORTF_DEN_R .32 GPIO_PORTF_PUR_R .32 GPIO_PORTF_DIR_R .field 0x40005524.32 RED .32 GPIO_PORTF_AMSEL_R .align 2 GPIO_PORTF_DATA_R .field 0x40004420. CMP R0.32 GPIO_PORTB_DIR_R .field 0x40004510.32 GPIO_PORTB_DEN_R .32 GPIO_PORTA_PCTL_R .field 0x4000451C.32 .field 0x40005510.equ 0x04 GREEN .field 0x40004400..32 GPIO_PORTA_DIR_R . R0 = BLUE (blue LED on) BL PortF_Output . #BLUE . R0 = FIFTHSEC (delay 0.32 GPIO_PORTA_AFSEL_R .32 GPIO_PORTB_LOCK_R .text . FIFTHSEC . switch 1 pressed . .thumb .asmfunc BL Port_Init . Unlocks the GPIO_CR register GPIO_PORTB_DATA_R .field 0x4000552C.32 GPIO_PORTF_LOCK_R . if so.equ 0x10 . delay at least (3*R0) cycles MOV R0.field 0x40005528.field 0x400253FC.field 0x4002551C.field 0x40025528.32 GPIO_PORTA_DATA_R .field 0x40005400.field 0x4000551C. on the left side of the Launchpad board SW2 .field 0x40025524.32 GPIO_PORTA_PUR_R .field 0x4C4F434B.32 GPIO_PORTF_AFSEL_R . switch 2 pressed .field 0x40025420. R0 == 0x10? .32 GPIO_PORTB_CR_R .32 GPIO_PORTB_PCTL_R .field 0x40025400.field 0x40004520.field 0x40004524.field 0x40005420.field 0x40025510.2 second) BL delay .32 .32 GPIO_PORTA_CR_R .field 0x400043FC.global main main: . #0x00 . #0x10 .field 0x4000452C.32 GPIO_PORTB_AFSEL_R . BEQ sw2pressed .32 GPIO_PORTA_AMSEL_R .32 GPIO_PORTB_AMSEL_R .

R0 = (RED|GREEN|BLUE) (all LEDs on) . if so. R0 == 0x00? . BEQ nopressed . #2 . MOV R4.R2 . R0 == 0x11? .R12. read all of the switches on Port F CMP R12. R0 = FIFTHSEC (delay 0. . #BLUE . #0x11 . #3 BEQ IN4 . ONESEC . BL PortF_Output .R3 . if so. R0 = BLUE (blue LED on) BL PortF_Output . MOV R0. BL PortB_Output B L1 IN3 MOV R3. neither switch pressed .2 second) BL delay . add R7.#04 ADD R2. BL PortB_Output B L1 L1 ADD R12.R0 . BL PortB_Output .R2. #1 .R3 . turn all of the LEDs off BL PortA_Input .R3.#01 .R0 . R0 == 0x10? BEQ IN2 CMP R12.INC R7.R0 . CMP R0. MOV R4. BL PortB_Output B L1 IN2 LSL R0. MOV R4.#1 . if none of the above. R0 = 0 (no LEDs on) BL PortF_Output .R2 . delay at least (3*R0) cycles MOV R0. R0 == 0x01? BEQ IN1 CMP R12. BEQ bothpressed .#04 ADD R3. #0 .#01 LDR R0. CMP R0. unexpected return va lue . turn the blue LED on . B loop IN1 MOV R2. MOV R4. BL PortB_Output B L1 IN4 LSL R0. turn all of the LEDs on B loop sw2pressed MOV R0. #0 . both switches pressed .R0 . R0 == 0x00? BEQ IN3 CMP R12. #0x00 . #(RED+GREEN+BLUE) .

STORE IN R2 B loop L2 MOV R7.#0x7F AND R6.R5 AND R11.R8 AND R9.R9. #4 BEQ L2 .R6.#8 .#8 BL PortB_Output LDR R0.R2 BL PortB_Output LDR R0.R9 BL PortB_Output LDR R0.#0x00FF ADD R9.#0 .R11.R8 LSL R8.R9.R2 .R5.R11 .R3 BL PortB_Output LDR R0.#0x3F00 ADD R11.#07 ADD R6.#0xC0 MOV R10.#128 BNE MULTIPLY SUBS R9.R8 AND R4.R6.COPYING BNE ADDITION MOV R4.#3F .FIVESEC BL delay B L3 SUBTRACT CMP R10.#01 AND R7.R6.R9.R6 ADD R9.R6.R5.#01 .R4. CMP R12.FIVESEC BL delay MOV R4.#0xFF00 LSR R4.#0 MOV R5. AND R6.R3 . lOWER BYTE MOV R6.R9. LSL LSL R6.#0x80 LSR R7. UPPER BYTE AND R8.R6.#64 BNE SUBTRACT ADD R9.FIVESEC BL delay MOV R4.FIVESEC BL delay B L3 ADDITION .R9.#0x7F CMP R10. LSL R6.R7 AND R5.ADDITION CMP R10.

------------PortF_Init------------ .field 1066666.1 (count = count .field 26666665.endasmfunc . Input: R0 count .asmfunc SUBS R0. MOV R0. which delays about 3*count cycles.R5 ADD R9. R0.R9. BL PortF_Output .endasmfunc . turn all of the LEDs off .FIVESEC BL delay MOV R4.32 .1) BNE delay .FIVESEC BL delay L3 MOV R12.R6. #0 .R8 AND R4.32 . AND R4. turn the green LED on . R0 = RED (red LED on) . #RED . B loop .32 ONESEC . MOV R0. #1 .------------delay------------ .R9 BL PortB_Output LDR R0. skip to 'delay' BX LR .sw2pressed . R0 = R0 .FIVESEC BL delay MOV R4.32 .#8 BL PortB_Output LDR R0.#0xFF00 LSR R4.25s delay at ~16 MHz clock FIFTHSEC .R9 BL PortB_Output LDR R0. approximately 0.bothpressed .#8 BL PortB_Output LDR R0. if count (R0) != 0. turn the red LED on . BL PortF_Output .R4. BL PortF_Output . #GREEN . R0 = GREEN (green LED on) . Output: none FIVESEC . approximately 0. B loop .field 5333333.R4.#0xFF00 LSR R4. Delay function for testing.R9. B loop .nopressed .2s delay at ~16 MHz clock delay: . Initialize GPIO Port F for negative logic switches on PF0 and .#0 B loop . R0 = 0 (no LEDs on) . MOV R0. approximately 1s delay at ~16 MHz cl ock QUARTERSEC .field 1333333. .R9.FIVESEC BL delay B L3 MULTIPLY MUL R9. return .

GPIO_PORTF_DIR_R . GPIO_PORTF_AMSEL_R . PF3-1 output STR R0. GPIO_PORTB_DIR_R . #0xFF . enable weak pull-up on PF0 and PF4 STR R0. set bit 5 to turn on clock STR R0. disabled. 4) configure as GPIO MOV R0. 1) activate clock for Port F LDR R0. 4) configure as GPIO MOV R0.PF0 MOV R0. 2) unlock the lock register LDR R0. PF0 and PF7-4 input. 0 means disable alternate function STR R0. 5) set direction register MOV R0. unlock GPIO Port F Commit Register STR R0. allow time for clock to finish LDR R1. resistors are enabled. Modifies: R0. GPIO_PORTF_LOCK_R . [R1] LDR R1. GPIO_PORTB_CR_R . GPIO_PORTF_CR_R . set bit 5 to turn on clock STR R0. #0x11 . Output: none . [R1] LDR R1. [R1] ORR R0. R1. R0. #0x00000000 . enable commit for Port F MOV R0. #0x02 . 5) set direction register MOV R0. PF0 and PF7-4 input.#0xFF . [R1] LDR R1. 1 means enable digital I/O STR R0. 6) regular port function MOV R0. [R1] ORR R0. Input: none . GPIO_PORTF_DEN_R . pull-up resistors for PF4. #0 . [R1] LDR R1. 3) disable analog functionality MOV R0. GPIO_PORTF_PCTL_R . 0 means configure Port F as GPIO STR R0. Make the RGB LED's pins outputs. [R1] LDR R1. GPIO_PORTF_PUR_R . 0 means analog is off STR R0. GPIO_PORTB_AMSEL_R . #0x00000000 . 2) unlock the lock register LDR R0. [R1] LDR R1. 1 means allow access STR R0. #0 . GPIO_LOCK_KEY . 7) enable Port F digital port MOV R0. . [R1] NOP NOP LDR R1. [R1] NOP NOP . [R1] LDR R1. PF3-1 output STR R0. GPIO_LOCK_KEY . Weak internal pull-up . [R1] LDR R1. [R1] LDR R1. #0xFF . #0x20 .. #0xFF . [R1] . GPIO_PORTF_AFSEL_R . GPIO_PORTB_PCTL_R . SYSCTL_RCGCGPIO_R . [R1] LDR R1. 1 means allow access STR R0. PF4 as the Launchpad is wired. GPIO_PORTB_LOCK_R . [R1] LDR R1. R0. 0 means analog is off STR R0. [R1] LDR R1.asmfunc LDR R1. #0 . R2 Port_Init: . unlock GPIO Port F Commit Register STR R0. 1) activate clock for Port F LDR R0.#0x0E . SYSCTL_RCGCGPIO_R . and the NMI functionality on PF0 is . 0 means configure Port F as GPIO STR R0. 3) disable analog functionality MOV R0. enable commit for Port F MOV R0.

R0 0x00 if both switches are pressed . [R1] BX LR . [R1] . SYSCTL_RCGCGPIO_R . 0 means disable alternate function STR R0. 6) regular port function MOV R0. #0xFF . GPIO_PORTB_AFSEL_R . GPIO_PORTB_DEN_R . read all of Port F AND R0. [R1] LDR R1. GPIO_PORTA_DIR_R . [R1] LDR R1. 5) set direction register MOV R0. #0xFF . #0x01 . [R1] LDR R1. 7) enable Port F digital port MOV R0. enable commit for Port F MOV R0.PA.#2 . R0 0x11 if no switches are pressed .#0x00 . Output: R0 0x01 if only Switch 1 is pressed .asmfunc LDR R1. GPIO_LOCK_KEY . enable weak pull-up on PF0 and PF4 STR R0. Modifies: R1 PortA_Input: . [R1] LDR R1. #0 . . [R1] NOP NOP LDR R1. set bit 5 to turn on clock STR R0.PF0 MOV R0. GPIO_PORTB_PUR_R . unlock GPIO Port F Commit Register STR R0. enable weak pull-up on PF0 and PF4 STR R0. Read and return the status of the switches.5 . 4) configure as GPIO MOV R0. #0 . GPIO_PORTA_AFSEL_R . #0 .------------PortF_Input------------ . 2) unlock the lock register LDR R0. GPIO_PORTA_PCTL_R . [R1] LDR R1. 3) disable analog functionality MOV R0. [R1] LDR R1.PF0 MOV R0. [R1] LDR R1. GPIO_PORTA_DEN_R . Input: none . PF0 and PF7-4 input. PF3-1 output STR R0. [R1] LDR R1. pointer to Port F data LDR R0.2 LSR R0. pull-up resistors for PF4. 6) regular port function MOV R0.endasmfunc . #0x00 . R0 0x10 if only Switch 2 is pressed . GPIO_PORTA_AMSEL_R . 0 means analog is off STR R0. [R1] ORR R0. pull-up resistors for PF4. GPIO_PORTA_DATA_R . 1) activate clock for Port F LDR R0. 0 means disable alternate function STR R0. [R1] LDR R1. GPIO_PORTA_CR_R . 1 means allow access STR R0. R0. 1 means enable digital I/O STR R0.#0x3C . 1 means enable digital I/O STR R0. [R1] LDR R1. GPIO_PORTA_PUR_R . 0 means configure Port F as GPIO STR R0. #0xFF . #0x00000000 . just the input pins PA. LDR R1. 7) enable Port F digital port MOV R0. #0x3C .R0. GPIO_PORTA_LOCK_R .

Input: R0 new state of PF . write to PF3-1 BX LR .endasmfunc PortB_Output: . [R1] . Set the output state of PF3-1.endasmfunc . GPIO_PORTB_DATA_R . .end . pointer to Port F data STR R4.R0. just the input pins PF0 and PF4 BX LR . write to PF3-1 BX LR . GPIO_PORTF_DATA_R .------------PortF_Output------------ .asmfunc LDR R1. Modifies: R1 PortF_Output: . pointer to Port F data STR R0.endasmfunc PortF_Input: . pointer to Port F data LDR R0. end of file . read all of Port F AND R0.#0x01 . GPIO_PORTF_DATA_R .endasmfunc . [R1] .asmfunc LDR R1.asmfunc LDR R1. return R0 with inputs . BX LR . [R1] . Output: none . return R0 with inputs .