Saturday, February 15, 2014

STM32F103 assembly code for just turning on LED (Keil MDK)

If you ask how pain it is for just using assembly language to turning on the led, I WOULD say very very very very....pain in the ass!!

Last year I was using STK200 which has atmega16 on it. I was able to display a simple digital clock by just using AVR assembly, which just took me several hours to write the code and display it on the Putty.

But I have to admit, Cortex-M3 is so different than that one. It would take several hours to review the ARM instructions and architecture. And probably it will still has compile error in the code.

Here's a little example:


GPIOC_CRL   EQU     0x40011000
GPIOC_CRH   EQU     0x40011004
GPIOC_IDR   EQU     0x40011008
GPIOC_ODR   EQU     0x4001100C
GPIOC_BSRR  EQU     0x40011010
GPIOC_BRR   EQU     0x40011014
GPIOC_LCKR  EQU     0x40011018
;STACK_TOP EQU 0X20002000
    AREA RESET,CODE,READONLY
;DCD STACK_TOP ;MSP pointer
DCD start  ;reset

            AREA    RESET, CODE, READONLY

            ENTRY
start
           
    LDR   r1, =GPIOC_CRH      ; Address for port c control register
            LDR   r0, [r1]
STR   r0, [r1]            ; Write to contorl register

            MOV32   r1, #GPIOC_ODR      ; Address for port c output data register
            MOV     r0, #0x0A00         ; Value for port c
            STR     r0, [r1]            ; Write value
loop
            B       loop
            ALIGN
            END

I ediited a little bit from the original version. You will know that it won't even debug step in for you since the crazy "MOVS R0,R0"....

You will never know how to deal with it unless you know this device, cortex-m3 will probably need to initialize the stack otherwise the debugger will not give a fxxx to you.
So if uncomment the two statements above in the code, the debugging will start working. I would said do not try using pure assembly in CORTEX-M4, that is a different device and more bugs waiting.








No comments:

Post a Comment