本文介绍了使用 ARM 组件的 Flash Raspberry pi LED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试将 LED 连接到树莓派上的 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.

这是我目前的代码:

.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 引脚一起工作.但是,当我将其更改为使用 pin 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 Peripherals https://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf

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 = 18Set GPIO Func:26-24 FSEL18 FSEL18 - Function Select 18 R/W 0Which is in GPIO Alternate function select register 1So: 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 0Which is in GPIO Output Set Register 0So: 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 0Which is GPIO Output Clear Register 0So: 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 21:11