cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103xx Assembly - unable to run application

Praveen1
Associate II

I am "Learning" assembly, So I have tried to program my stm32f103 in assembly (a simple program to blink a led). The problem is that the program runs when uploaded using st-flash utility. But, If I press reset button, the program stops working.

Here are few things that I debugged in STM32CubeProgrammer.

  1. The program seems runs if do a reset from the programmer but not on board.
  2. I get a unable to run error If I try to run the CPU from CubePrg after manual button reset.
  3. Moreover the Program Counter register seems to jump to 0xFFFFFFFE in the above case (Not always).

I am using Ubuntu and Nucleo-64 board.

PS. I didnt use any startup_*.s file. Instead placed the 2 vector address by using word and main program at 0x08000200 using .org directive

EDIT:

The program counter now always goes to 0.

Code:

.equ RCC_BASE,       0x40021000
.equ APB2ENB,        RCC_BASE + 0x18
.equ GPIOA_BASE,     0x40010800
.equ GPIOA_CONF_L,   GPIOA_BASE + 0x0
.equ GPIOA_BSSR,   GPIOA_BASE + 0x10
.equ GPIOA_ODR,      GPIOA_BASE + 0xC
 
.syntax unified
.cpu cortex-m3
.thumb
 
// Global memory locations.
.global reset
.word 0x20001000
.word reset
    .text
    .entry
    
reset: .proc
  ;LDR  r3, =_estack
  ;MOV  sp, r3
  push  {r7}
  ADD r7, sp, #0
  B _main
  
_main:
  // Enable Clock APH
  LDR r3, =APB2ENB
  LDR r2, [r3]
  ORR r2,r2,#0x4
  STR r2, [r3]
  // Reset Pin PA5
  LDR r3, =GPIOA_BSSR
  LDR r2, [r3]
  MOV r2, #0x20
  STR r2, [r3]
  //Set PinMode PA5 as OUTPUT
  LDR r3, =GPIOA_CONF_L
  LDR r2, [r3]
  MOV r2, #0x100000
  STR r2, [r3]
  // Set Default High on PA5
  LDR r3, =GPIOA_ODR
  LDR r2, [r3]
  MOV r2,#0x20
  STR r2, [r3]
  mov r3, #0
  B loop
  .endp
  
loop:
  LDR r3, =GPIOA_ODR
  LDR r2, [r3]
  ORR r2,r2,#0x20
  STR r2, [r3]
  MOV r4,#0xFF40
  BL _delay
  LDR r3, =GPIOA_ODR
  LDR r2, [r3]
  AND r2,r2,#0xFFFFFFDF
  STR r2, [r3]
  MOV r4,#0xFF40
  BL _delay
  B loop
  
_delay:
  SUB r4,r4,#0x1
  CMP r4,#0
  BNE _delay
  BX lr
  .align
  .end

  Finally, got it working. The problem is the address of the reset handler. I set second vector address to reset_label+1. But, why?

Still curious how it worked the first time (when uploaded).

3 REPLIES 3
Peter BENSCH
ST Employee

There are some Youtube videos that were originally created for a workshop to support the transition from simple old 8-bit architectures to modern 32-bit Cortex-M architectures. You will also find the material of the workshop in the comment section of the videos. Video 5 in particular contains some interesting approaches on how to write an assembler program very easily - without a startup_*.s file, as you would like to do.:

Good luck!

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

>>But, If I press reset button, the program stops working.

As no code is presented, I'd guess you're not enabling one or more clock on the RCC that the debugger is doing, but are off by default out of reset.

GPIOA?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Praveen1
Associate II

I am Sorry. I forgot to paste the code. Anyway, I have Uploaded it now.