0

I am trying to blink an LED on LPC4088(Datasheet) and I have properly prepared my peripheral registers already. Now is the time to blink an LED and this is the loop which should do it:

blink:

    @ In register SET1 (0x20098038) we set bits:
    @ bit 13 = 1    >   sets pin P1_13 to high state
    ldr r0, =0x20098038
    ldr r1, [r0]
    ldr r2, =0x2000
    orr r1, r2
    str r1, [r0]

    @ NO DELAY NEEDED FOR DEBUG

    @ In register CLR1 (0x2009803C) we set bits:
    @ bit 13 = 1    >   sets pin P1_13 to low state
    ldr r0, =0x2009803C
    ldr r1, [r0]
    ldr r2, =0x2000
    orr r1, r2
    str r1, [r0]

    @ NO DELAY NEEDED FOR DEBUG

    b blink

When I checked the execution of the code in debugger I noticed that first part of the loop (before first @ NO DELAY NEEDED FOR DEBUG) executes fine and LED is turned on exactly when command str r1, [r0] is executed.

Now comes the second part of the loop (after first @ NO DELAY NEEDED FOR DEBUG) and when I try to execute ldr r0, =0x2009803C LED won't turn off. Instead my program directly jumps to the first part of the loop and would execute ldr r0, =0x20098038 in the next step. So instructions:

ldr r1, [r0]
ldr r2, =0x2000
orr r1, r2
str r1, [r0]

in the second part of the loop are never executed...

This code worked perfectly fine in C code previously... Does anyone spot a mistake, because I can't. Do I somehow overwrite registers? Could it be that my compiler optimises the code somehow?

6
  • 4
    assemblers dont optimize. not gnu at least.
    – old_timer
    Feb 8, 2018 at 21:19
  • have you enabled the clocks to the gpio? try only one of these without a loop, just turn on. then make a version that just turns off. try each separately (ideally without the debugger). If you cant force it on and stay on and cant force it off and stay off then you cant blink, you may be seeing it on for other reasons than you think.
    – old_timer
    Feb 8, 2018 at 21:21
  • I figured it out! Comments were messing my code! I deleted them and it works now... WHAT!???
    – 71GA
    Feb 8, 2018 at 21:26
  • 1
    a) delete or self answer. b) look at the lst file (the dump of the assembly), and see how your good and bad code is rendered. Feb 8, 2018 at 22:39
  • 1
    what did your disassembly show with and without comments? did it match your code (at least somewhat your ldr =0x... result in either a mov or a pc relative load with pool data.
    – old_timer
    Feb 9, 2018 at 15:11

1 Answer 1

1

You are not allowed to read from register CLR1 at address 2009803c, as it is write-only (see the user manual). As a result you get a fault (I'm not sure exactly which one, probably MemManageFault, or BusFault). Normally it would jump to a fault handler, but from the behavior you see it seems to restart the program.

The solution is not to read the SET or CLR registers, you should just write the pattern of bits you want to set or clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.