Skip to content

Commit

Permalink
Refactor gpio.s
Browse files Browse the repository at this point in the history
  • Loading branch information
apinsard committed Nov 11, 2014
1 parent 958ded1 commit ebf3fe9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 65 deletions.
Binary file modified kernel.img
Binary file not shown.
77 changes: 37 additions & 40 deletions kernel.list
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ build/output.elf: file format elf32-littlearm
Disassembly of section .init:

00008000 <_start>:
8000: ea000023 b 8094 <main>
8000: ea000020 b 8088 <main>
Disassembly of section .text:

00008004 <GetGpioAddress>:
8004: e59f0084 ldr r0, [pc, #132] ; 8090 <SetGpio+0x3c>
8004: e59f0078 ldr r0, [pc, #120] ; 8084 <SetGpio+0x30>
8008: e1a0f00e mov pc, lr

0000800c <SetGpioFunction>:
800c: e3500035 cmp r0, #53 ; 0x35
8010: 93500007 cmpls r0, #7 ; 0x7
8014: 81a0f00e movhi pc, lr
8018: e59f2070 ldr r2, [pc, #112] ; 8090 <SetGpio+0x3c>
8018: e59f2064 ldr r2, [pc, #100] ; 8084 <SetGpio+0x30>

0000801c <funcCalcLoop$>:
801c: e3500009 cmp r0, #9 ; 0x9
Expand All @@ -36,44 +36,41 @@ Disassembly of section .text:
00008054 <SetGpio>:
8054: e3500035 cmp r0, #53 ; 0x35
8058: 81a0f00e movhi pc, lr
805c: e92d4000 push {lr}
8060: e1a02000 mov r2, r0
8064: ebffffe6 bl 8004 <GetGpioAddress>
8068: e1a032a2 lsr r3, r2, #5
806c: e1a03103 lsl r3, r3, #2
8070: e0800003 add r0, r0, r3
8074: e202201f and r2, r2, #31 ; 0x1f
8078: e3a03001 mov r3, #1 ; 0x1
807c: e1a03213 lsl r3, r3, r2
8080: e3310000 teq r1, #0 ; 0x0
8084: 05803028 streq r3, [r0, #40]
8088: 1580301c strne r3, [r0, #28]
808c: e8bd8000 pop {pc}
8090: 20200000 .word 0x20200000
805c: e59f2020 ldr r2, [pc, #32] ; 8084 <SetGpio+0x30>
8060: e1a032a0 lsr r3, r0, #5
8064: e0822103 add r2, r2, r3, lsl #2
8068: e200001f and r0, r0, #31 ; 0x1f
806c: e3a03001 mov r3, #1 ; 0x1
8070: e1a03013 lsl r3, r3, r0
8074: e3310000 teq r1, #0 ; 0x0
8078: 05823028 streq r3, [r2, #40]
807c: 1582301c strne r3, [r2, #28]
8080: e1a0f00e mov pc, lr
8084: 20200000 .word 0x20200000

00008094 <main>:
8094: e3a0d902 mov sp, #32768 ; 0x8000
8098: e3a00010 mov r0, #16 ; 0x10
809c: e3a01001 mov r1, #1 ; 0x1
80a0: ebffffd9 bl 800c <SetGpioFunction>
00008088 <main>:
8088: e3a0d902 mov sp, #32768 ; 0x8000
808c: e3a00010 mov r0, #16 ; 0x10
8090: e3a01001 mov r1, #1 ; 0x1
8094: ebffffdc bl 800c <SetGpioFunction>

000080a4 <mainloop$>:
80a4: e3a00010 mov r0, #16 ; 0x10
80a8: e3a01000 mov r1, #0 ; 0x0
80ac: ebffffe8 bl 8054 <SetGpio>
80b0: e3a0283f mov r2, #4128768 ; 0x3f0000
00008098 <mainloop$>:
8098: e3a00010 mov r0, #16 ; 0x10
809c: e3a01000 mov r1, #0 ; 0x0
80a0: ebffffeb bl 8054 <SetGpio>
80a4: e3a0283f mov r2, #4128768 ; 0x3f0000

000080b4 <wait1$>:
80b4: e2422001 sub r2, r2, #1 ; 0x1
80b8: e3520000 cmp r2, #0 ; 0x0
80bc: 1afffffc bne 80b4 <wait1$>
80c0: e3a00010 mov r0, #16 ; 0x10
80c4: e3a01001 mov r1, #1 ; 0x1
80c8: ebffffe1 bl 8054 <SetGpio>
80cc: e3a0283f mov r2, #4128768 ; 0x3f0000
000080a8 <wait1$>:
80a8: e2422001 sub r2, r2, #1 ; 0x1
80ac: e3520000 cmp r2, #0 ; 0x0
80b0: 1afffffc bne 80a8 <wait1$>
80b4: e3a00010 mov r0, #16 ; 0x10
80b8: e3a01001 mov r1, #1 ; 0x1
80bc: ebffffe4 bl 8054 <SetGpio>
80c0: e3a0283f mov r2, #4128768 ; 0x3f0000

000080d0 <wait2$>:
80d0: e2422001 sub r2, r2, #1 ; 0x1
80d4: e3520000 cmp r2, #0 ; 0x0
80d8: 1afffffc bne 80d0 <wait2$>
80dc: eafffff0 b 80a4 <mainloop$>
000080c4 <wait2$>:
80c4: e2422001 sub r2, r2, #1 ; 0x1
80c8: e3520000 cmp r2, #0 ; 0x0
80cc: 1afffffc bne 80c4 <wait2$>
80d0: eafffff0 b 8098 <mainloop$>
6 changes: 3 additions & 3 deletions kernel.map
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ LOAD build/main.o
.init 0x00008000 0x4 build/main.o
0x00008000 _start

.text 0x00008004 0xdc
.text 0x00008004 0xd0
*(.text)
.text 0x00008004 0x90 build/gpio.o
.text 0x00008004 0x84 build/gpio.o
0x0000800c SetGpioFunction
0x00008004 GetGpioAddress
0x00008054 SetGpio
.text 0x00008094 0x4c build/main.o
.text 0x00008088 0x4c build/main.o

/DISCARD/
*(*)
Expand Down
52 changes: 30 additions & 22 deletions src/gpio.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
.globl GPIO_NB_FUNCS
.globl GPIO_BLOCK_SIZE
.globl GPIO_PINS_PER_BLOCK
.globl GPIO_PIN_TURN_ON
.globl GPIO_PIN_TURN_OFF
.globl GPIO_PIN_INPUT
.globl GetGpioAddress
.globl SetGpioFunction
.globl SetGpio

.equiv GPIO_ADDR, 0x20200000
.equiv GPIO_NB_PINS, 54
.equiv GPIO_NB_FUNCS, 8
.equiv GPIO_BLOCK_SIZE, 4
.equiv GPIO_ADDR , 0x20200000
.equiv GPIO_NB_PINS , 54
.equiv GPIO_NB_FUNCS , 8
.equiv GPIO_BLOCK_SIZE , 4
.equiv GPIO_PINS_PER_BLOCK, 10
.equiv GPIO_PIN_TURN_ON , 28
.equiv GPIO_PIN_TURN_OFF, 40
.equiv GPIO_PIN_INPUT , 52

@
@ Load the GPIO Address in memory.
Expand All @@ -37,8 +43,8 @@ SetGpioFunction:
cmpls pinNum, #[GPIO_NB_FUNCS-1]
movhi pc, lr @ <Return

ldr r2, =GPIO_ADDR
funcPinAddr .req r2
ldr funcPinAddr, =GPIO_ADDR

@ Calculate the function pin address (which block of 4 bytes?) in funcPinAddr
@ and which set of 3 bits in pinNum.
Expand Down Expand Up @@ -78,40 +84,42 @@ SetGpioFunction:

mov pc, lr @ <Return

@
@ Turn on/off a GPIO pin.
@ r0: GPIO pin [0, GPIO_NB_PINS-1[
@ r1: Turn off? (0) or turn on? (any other value)
@
SetGpio:
pinNum .req r0
pinVal .req r1
pinNum .req r0 @ <- GPIO pin [0, GPIO_NB_PINS[
pinVal .req r1 @ <- 0: Turn OFF ; Else: Turn ON

@ Check pre-conditions and break if not satisfied.
cmp pinNum, #[GPIO_NB_PINS-1]
movhi pc, lr @ Break if pre-cond not satisfied
movhi pc, lr @ <Return

push {lr}
mov r2, pinNum
.unreq pinNum
pinNum .req r2
bl GetGpioAddress
gpioAddr .req r0
gpioPinAddr .req r2
ldr gpioPinAddr, =GPIO_ADDR

@ Divide the GPIO pin number by 32 to get in which of the two sets of 4 bytes
@ it is (0 or 1). And then multiply by 4 to get the address.
pinBank .req r3
lsr pinBank, pinNum, #5
lsl pinBank, #2
add gpioAddr, pinBank
add gpioPinAddr, pinBank, lsl #2
.unreq pinBank

@ The bit to set at the given address is the nth bit where n is the remainder
@ of the previous division by 32.
and pinNum, #31
setBit .req r3
mov setBit, #1
lsl setBit, pinNum
.unreq pinNum

@ Turn ON or OFF within pinVal
teq pinVal, #0
.unreq pinVal
streq setBit,[gpioAddr,#40]
strne setBit,[gpioAddr,#28]
streq setBit,[gpioPinAddr,#GPIO_PIN_TURN_OFF]
strne setBit,[gpioPinAddr,#GPIO_PIN_TURN_ON]
.unreq setBit
.unreq gpioAddr
pop {pc}
.unreq gpioPinAddr

mov pc, lr @ <Return

0 comments on commit ebf3fe9

Please sign in to comment.