Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if the user button is pressed on the STM32F disco in Assembly

Tags:

assembly

stm32

I am new to assembly programming on the stm32f discovery board. I am attempting to write a program (.S file) in assembly that I can call in C. I want the assembly program to check if the user button is pressed.

I've done some research and found that the user button is found in the GPIOA port and it's data can be accessed from the IDR space. Specifically, I believe that the first bit of GPIOA->IDR toggles to 1 when the user button is pressed.

Here's the code I wrote:

.global checkB1
.thumb_func
checkB1: 

@; accessing B1
ldr r3,=GPIOA_BASE

ldr r2, [r3,#IDR]
and r0, r2, #GPIO_IDR_IDR_0     @; check if 1 and put in r0

bx lr

I'm having no problem calling the function in C, but r0 never changes to 1 when the user button is pressed?

Im pretty lost on what I am doing wrong and everything Ive researched does the entire process in C and it hasn't really helped me. If anybody knows the error it would be appreciated.

EDIT:

I'd also like to include my initialization code which configures GPIOA:

.global initB1
.thumb_func
initB1: @;configure B1 as an input

@; make sure GPIOA is enabled
ldr r3,=RCC_BASE
ldr r2,[r3,#RCC_AHB1ENR]
orr r2,#1   @; set enable bit
str r2,[r3,#RCC_AHB1ENR]

@; configuring B1
ldr r3,=GPIOA_BASE

@; configure B1 as an input
ldr r2,[r3,#MODER]
bic r2,#3   @;clear current value if any of A0 mode
            @; new value of A0 mode is general purpose input
str r2,[r3,#MODER]  @; ..

@; configure input of B1 as pulldown
ldr r2,[r3,#OPUPDR]
bic r2,#3   @;clear current value if any of control bits
orr r2,#2   @; pulldown mode (bit value: 10)
str r2,[r3,#OPUPDR] @; ..

bx lr
like image 974
DarkLink Avatar asked Dec 06 '25 04:12

DarkLink


1 Answers

The problem with my code was I wasn't enabling the correct port in my initializations!

@; make sure GPIOA is enabled
ldr r3,=RCC_BASE
ldr r2,[r3,#RCC_AHB1ENR]
orr r2,#1   @; set enable bit <--------------
str r2,[r3,#RCC_AHB1ENR]

Port A corresponding to #1 up Port F corresponding to #(1<<7)

Learning new syntax can sure be frustrating. The code in my question has been edited and works to detect the user button. I'm sure my struggle with this could come in handy to someone someday!

like image 141
DarkLink Avatar answered Dec 08 '25 21:12

DarkLink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!