Slurre / PES-Project

0 stars 0 forks source link

Strange behavior when using delay inside app #21

Open ialokim opened 2 years ago

ialokim commented 2 years ago
void blink_leds_delay(void) {
    /* Toggle all LEDs. */
    bsp_board_leds_on();
    nrf_delay_ms(500);
    bsp_board_leds_off();
}

in the app generates

00030010 <blink_leds_delay>:
   30010:   b538        push    {r3, r4, r5, lr}
   30012:   f000 f811   bl  30038 <__bsp_board_leds_on_veneer>
   30016:   4d07        ldr r5, [pc, #28]   ; (30034 <blink_leds_delay+0x24>)
   30018:   f44f 74fa   mov.w   r4, #500    ; 0x1f4
   3001c:   f045 0501   orr.w   r5, r5, #1
   30020:   f44f 407a   mov.w   r0, #64000  ; 0xfa00
   30024:   47a8        blx r5
   30026:   3c01        subs    r4, #1
   30028:   d1fa        bne.n   30020 <blink_leds_delay+0x10>
   3002a:   e8bd 4038   ldmia.w sp!, {r3, r4, r5, lr}
   3002e:   f000 b807   b.w 30040 <__bsp_board_leds_off_veneer>
   30032:   bf00        nop
   30034:   00000050    .word   0x00000050

00030038 <__bsp_board_leds_on_veneer>:
   30038:   f85f f000   ldr.w   pc, [pc]    ; 3003c <__bsp_board_leds_on_veneer+0x4>
   3003c:   000003d5    .word   0x000003d5

00030040 <__bsp_board_leds_off_veneer>:
   30040:   f85f f000   ldr.w   pc, [pc]    ; 30044 <__bsp_board_leds_off_veneer+0x4>
   30044:   000003a9    .word   0x000003a9

Here, the app does not crash, but the LEDs are only turned on for a very short time before the are turned off again (i.e., the delay is not working as expected).

For comparison, the asm of the main function using the same delay function:

00000340 <main>:
 340:   b570        push    {r4, r5, r6, lr}
 342:   2001        movs    r0, #1
 344:   4c07        ldr r4, [pc, #28]   ; (364 <main+0x24>)
 346:   4e08        ldr r6, [pc, #32]   ; (368 <main+0x28>)
 348:   f000 f85a   bl  400 <bsp_board_init>
 34c:   f044 0401   orr.w   r4, r4, #1
 350:   47b0        blx r6
 352:   f44f 75fa   mov.w   r5, #500    ; 0x1f4
 356:   f44f 407a   mov.w   r0, #64000  ; 0xfa00
 35a:   47a0        blx r4
 35c:   3d01        subs    r5, #1
 35e:   d1fa        bne.n   356 <main+0x16>
 360:   e7f6        b.n 350 <main+0x10>
 362:   bf00        nop
 364:   00000830    .word   0x00000830
 368:   00030001    .word   0x00030001
...
00000830 <delay_machine_code.8023>:
 830:   3803 d8fd 4770 0000                         .8..pG..

apparently the delay_machine_code part is not generated/used as it should from within the app

ialokim commented 2 years ago

actually it is present as well in the app code, but obviously at the wrong offset:

Disassembly of section .rodata.delay_machine_code.8023:

00030050 <delay_machine_code.8023>:
   30050:   d8fd3803    ldmle   sp!, {r0, r1, fp, ip, sp}^
   30054:   Address 0x0000000000030054 is out of bounds.

Also, apparently it is generated in .rodata instead of in .text as in the base firmware.

Looking at the symbol/map file, it can be seen that the corresponding label is actually defined twice in the app code:

delay_machine_code.8023 = 0x00030050 
delay_machine_code.8023 = 0x00000830 

A fix might be to somehow force the linker to use the one provided by the base firmware instead of the one in the app. Perhaps the nrf_delay.h file emits some code instead of only serving as header file?

ialokim commented 2 years ago

You can play with it yourself on the branch https://github.com/Slurre/PES-Project/tree/symbol-def-file