Skip to main content
CGalv
Associate III
October 6, 2018
Question

STM32F745 - Simple LED ON not working with -O2 compiler flag

  • October 6, 2018
  • 4 replies
  • 890 views

Hi,

I am starting to program STM32 devices and I have an STM32F745 chip. Since I want to learn all the details I'm programming it from scratch, using only the definitions from CMSIS.

I have a very simple program where I have 2 LEDs on PB6 and PB7 respectively. Right now I only want to turn them on. I have achieved this compiling the code with "-O0" optimization flag, but when I change it to "-O2" then it doesn't work, the LEDs are off. Here's the code:

#include <stm32f745xx.h>
 
int main()
{
 // Blue LED is on PB7, Red LED is on PB6
 // 1) Enable clock for Port B, since input/output are latched
 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
 
 // 2) Configure PB6 and PB7 as output. Push-pull is default conf on reset
 // PortB has value 0x0000 0280 on reset
 GPIOB->MODER = 0x0000'5280;
 
 // 3) Wait indefinitely
 while(1){}
}

Compiling with "-O0" optimization flag, the chip behaves as expected and both LEDs are turned on. I get the following assembly:

08000208 <main>:
 8000208:	b480 	push	{r7}
 800020a:	af00 	add	r7, sp, #0
 800020c:	4b05 	ldr	r3, [pc, #20]	; (8000224 <main+0x1c>)
 800020e:	6b1b 	ldr	r3, [r3, #48]	; 0x30
 8000210:	4a04 	ldr	r2, [pc, #16]	; (8000224 <main+0x1c>)
 8000212:	f043 0302 	orr.w	r3, r3, #2
 8000216:	6313 	str	r3, [r2, #48]	; 0x30
 8000218:	4b03 	ldr	r3, [pc, #12]	; (8000228 <main+0x20>)
 800021a:	f44f 42a5 	mov.w	r2, #21120	; 0x5280
 800021e:	601a 	str	r2, [r3, #0]
 8000220:	e7fe 	b.n	8000220 <main+0x18>
 8000222:	bf00 	nop
 8000224:	40023800 	.word	0x40023800
 8000228:	40020400 	.word	0x40020400	

When I compile with "-O2", the LEDs are off. I get the following assembly:

08000208 <main>:
 8000208:	4a04 	ldr	r2, [pc, #16]	; (800021c <main+0x14>)
 800020a:	f44f 40a5 	mov.w	r0, #21120	; 0x5280
 800020e:	4904 	ldr	r1, [pc, #16]	; (8000220 <main+0x18>)
 8000210:	6b13 	ldr	r3, [r2, #48]	; 0x30
 8000212:	f043 0302 	orr.w	r3, r3, #2
 8000216:	6313 	str	r3, [r2, #48]	; 0x30
 8000218:	6008 	str	r0, [r1, #0]
 800021a:	e7fe 	b.n	800021a <main+0x12>
 800021c:	40023800 	.word	0x40023800
 8000220:	40020400 	.word	0x40020400

In both cases I can see that it's storing the correct values in the registries so I don't understand why the LEDs are not being turned on!

Am I missing something obvious?

Thanks!!

    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    October 6, 2018

    There is a documented hazard for writing to a peripheral registers immediately after enabling the clock

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    henry.dick
    Associate II
    October 6, 2018

    "both LEDs are turned on."

    a pin can be set/cleared for other reasons. it is much more reliable to test code execution by flipping them constantly.

    waclawek.jan
    Super User
    October 6, 2018

    > There is a documented hazard for writing to a peripheral registers immediately after enabling the clock

    RM0385, 5.2.12 Peripheral clock enable register (RCC_AHBxENR, RCC_APBxENRy):

    > Caution: Just after enabling the clock for a peripheral, software must wait for a 2 peripheral clock

    > cycles delay before accessing the peripheral registers.

    CGalv
    CGalvAuthor
    Associate III
    October 7, 2018

    That did the trick, thanks a lot!