使用ARM组件的Flash Raspberry pi LED [英] Flash Raspberry pi LED using ARM assembly
问题描述
我在尝试将LED连接到覆盆子pi上的gpio引脚18时遇到问题.我已经用C测试了我的设置,并确认那不是问题,但是我的汇编代码就是问题.
I have a problem trying to get an LED connected to gpio pin 18 on the raspberry pi. I have tested my setup with C and have confirmed thats not the problem, but my assembly code is the problem.
这是我到目前为止的代码:
This is the code I have so far:
.global main
main:
SUB SP, SP, #16 @ Create 16 bytes storage
LDR R0, .addr_file @ get GPIO Controller addr
LDR R1, .flags @ set flag permissions
BL open @ call to get file handle
STR R0, [SP, #12] @ File handle number
LDR R3, [SP, #12] @ Get File handle
STR R3, [SP, #0] @ Store file handle on top stack
LDR R3, .gpiobase @ get GPIO_Base address
STR R3, [SP, #4] @ store on SP+4
MOV R0, #0 @ R0=0
MOV R1, #4096 @ R1=page
MOV R2, #3 @ R2=3
MOV R3, #1 @ R3=1 (stdouts)
BL mmap @ call libc fct for mmap
STR R0, [SP, #16] @ store virtual mem addr
LDR R3, [SP, #16] @ get virtual mem addr
fctsel:
ADD R3, R3, #4 @ add 4 for block 1 (GPFSEL1)
LDR R2, [SP, #16] @ get virtual mem addr
ADD R2, R2, #16 @ add 4 for block 1 (GPFSEL1)
LDR R2, [R2, #0] @ load R2 with value at R2
BIC R2, R2, #0b111<<24 @ Bitwise clear of three bits
STR R2, [R3, #0] @ Store result in Register [set input]
LDR R3, [SP, #16] @ Get virtual mem address
ADD R3, R3, #4 @ Add 4 for block 1 (GPFSEL1)
LDR R2, [SP, #16] @ Get virtual mem addr
ADD R2, R2, #4 @ add 4 for block 1 (GPFSEL1)
LDR R2, [R2, #0] @ Load R2 with value at R2
ORR R2, R2, #1<<24 @ Set bit....
STR R2, [R3, #0] @ ...and make output
on:
LDR R3, [SP, #16] @ get virt mem addr
MOV R4, #1 @ get 1
MOV R2, R4, LSL#18 @ Shift by pin number
STR R2, [R3, #0] @ write to memory
LDR R0, [SP, #12] @ get file handle
BL close @ close file
ADD SP, SP, #16 @ restore SP
MOV R7, #1
SWI 0
.addr_file: .word .file
.flags: .word 1576962
@.gpiobase: .word 0x20200000 @ GPIO_Base for Pi 1
.gpiobase: .word 0x3F200000 @ GPIO_Base for Pi 2
.data
.file: .ascii "/dev/mem\000"
我设法使其与GPIO 47引脚一起工作.但是,当我将其更改为与引脚18配合使用时,这就是我遇到的问题.在此先感谢您的帮助,不胜感激!
I managed to get it working with the GPIO 47 pin. However when I changed it to work with pin 18 this is where I ran into issues. Thanks in advance for any help, its greatly appreciated!
推荐答案
查看BCM2835文档-Broadcom BCM2835 ARM外设
Looking at BCM2835 documentation - Broadcom BCM2835 ARM Peripherals https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
似乎您的代码基于Raspberry Pi,汇编语言和Bruce Smith.
Is seems that your code is based on Raspberry Pi, Assembly Language, Bruce Smith.
我不是为什么要清除这3位,因为文档说是用于输入的设置.
I'm not why we are clearing the 3 bits since the docs say is sets for input.
***我确认它可以在我的RPi 2 B上使用3伏以上的电压.
*** I confirmed that it works on my RPi 2 B with 3+ volts on the pin.
目标引脚= 18 设置GPIO功能: 26-24 FSEL18 FSEL18-功能选择18 R/W 0 在GPIO备用功能选择寄存器1中 因此:0x 7E20 0004 GPFSEL1 GPIO功能选择1 32 R/W |偏移#0x4
Target pin = 18 Set GPIO Func: 26-24 FSEL18 FSEL18 - Function Select 18 R/W 0 Which is in GPIO Alternate function select register 1 So: 0x 7E20 0004 GPFSEL1 GPIO Function Select 1 32 R/W | offset #0x4
init_output: // init for OUTPUT
ldr r3, [sp, #8] // virt GPIO base
add r3, r3, #0x4 // offset to GPFSEL1
ldr r2, [r3] // get contents of GPFSEL1
orr r2, r2, #0b001<<18 // set 3 bits re FSEL18 output
str r2, [r3] // store set bits at GPFSEL1
设置输出: 31-0 SETn(n = 0..31)0 =无作用1 =设置GPIO引脚n R/W 0 在GPIO输出设置寄存器0中 因此:0x 7E20 001C GPSET0 GPIO引脚输出设置0 32 W |偏移#0x1C == 28
Set Output: 31-0 SETn (n=0..31) 0 = No effect 1 = Set GPIO pin n R/W 0 Which is in GPIO Output Set Register 0 So: 0x 7E20 001C GPSET0 GPIO Pin Output Set 0 32 W | offset #0x1C == 28
set_pin:
ldr r3, [sp, #8] // virt GPIO base
add r3, r3, #0x1C // GPSET0
ldr r2, [r3] // get content of GPSET0
orr r2, r2, #1<<18 // set PIN 18
str r2,[r3] // set PIN 18 @ GPSET0
清除输出: 31-0 CLRn(n = 0..31)0 =无效1 =清除GPIO引脚n R/W 0 哪个是GPIO输出清除寄存器0 因此:0x 7E20 0028 GPCLR0 GPIO引脚输出清除0 32 W |偏移量#0x28 == 40
Clear Output: 31-0 CLRn (n=0..31) 0 = No effect 1 = Clear GPIO pin n R/W 0 Which is GPIO Output Clear Register 0 So: 0x 7E20 0028 GPCLR0 GPIO Pin Output Clear 0 32 W | offset #0x28 == 40
clear_pin:
ldr r3, [sp, #8] // virt GPIO base
add r3, r3, #0x28 // GPCLR0
ldr r2, [r3] // get content of GPCLR0
orr r2, r2, #1<<18 // set PIN 18
str r2,[r3] // set PIN 18 @ GPCLR0
这篇关于使用ARM组件的Flash Raspberry pi LED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!