Skip to content

Instantly share code, notes, and snippets.

@dimitriye98
Created April 4, 2016 09:11
Show Gist options
  • Save dimitriye98/433a17dd8a6fd9bcf65a65986b4132d8 to your computer and use it in GitHub Desktop.
Save dimitriye98/433a17dd8a6fd9bcf65a65986b4132d8 to your computer and use it in GitHub Desktop.
/*
* Returns the GPIO offset address
*
* ret: the GPIO offset address
*/
.globl GetGPIOAddress
GetGPIOAddress:
ldr r0, =0x3F200000
mov pc, lr
/*
* Sets the function of the given GPIO pin.
*
* args:
* r0 the pin to set
* r1 the function to set it to
*/
.globl SetGPIOFunction
SetGPIOFunction:
pinNum .req r0
pinFunc .req r1
cmp pinNum, #53
cmpls pinFunc, #7
movhi pc, lr
push {lr}
mov r2, pinNum
.unreq pinNum
pinNum .req r2
bl GetGPIOAddress
gpioAddr .req r0
modLoop$:
cmp pinNum, #9
subhi pinNum, #10
addhi gpioAddr, #4
bhi modLoop$
/* add pinNum,#10 */
add pinNum, pinNum, lsl #1
lsl pinFunc, pinNum
mask .req r3
ldr mask,=0b111
mvn mask, mask, lsl pinNum
.unreq pinNum
oldFunc .req r2
ldr oldFunc, [gpioAddr]
and oldFunc, mask
.unreq mask
orr pinFunc, oldFunc
.unreq oldFunc
str pinFunc, [gpioAddr]
.unreq pinFunc
.unreq gpioAddr
pop {pc}
/*
* Sets the value of the given GPIO pin.
*
* args:
* r0 the pin to set
* r1 a boolean indicating whether the pin should be on/off (on ifn 0)
*/
.globl SetGPIO
SetGPIO:
pinNum .req r0
pinVal .req r1
cmp pinNum, #53
movhi pc, lr
push {lr}
mov r2, pinNum
.unreq pinNum
pinNum .req r2
bl GetGPIOAddress
gpioAddr .req r0
pinBank .req r3
lsr pinBank, pinNum, #5
add gpioAddr, pinBank, lsl #2
.unreq pinBank
and pinNum,#0b11111
setBit .req r3
ldr setBit,=1
lsl setBit,pinNum
.unreq pinNum
teq pinVal,#0
.unreq pinVal
streq setBit,[gpioAddr, #40]
strne setBit,[gpioAddr, #28]
.unreq setBit
.unreq gpioAddr
pop {pc}
.section .init
.globl _start
_start:
b main
.section .text
main:
ldr sp,=0x8000
pinNum .req r0
ldr pinNum,=47
pinFunc .req r1
ldr pinFunc,=1
bl SetGPIOFunction
.unreq pinFunc
pinVal .req r1
countdown .req r2
loop$:
ldr r1,=0
bl SetGPIO
ldr countdown,=0x3F0000
wait$:
subs countdown,#1
bls wait$
ldr r1,=1
bl SetGPIO
ldr countdown,=0x3F0000
wait1$:
subs countdown,#1
bls wait1$
b loop$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment