This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Code crashes when Optimiser is set to -O0

Keil uVision 5.28a

Target: ST Nucleo  F429ZI (Cortex M4)

My colleague created a project using Keil uVision 5.28a. The only options used in the project creation wizzard were startup.s and CMSIS->Core and DEVICE->Startup

The only code (in main.c) is shown below

#include <stm32f4xx.h>		//INCLUDE THE HEADER FILE FOR THIS MCU FAMILY
															//this file contains the definitions for register addresses and values etc...


int main(void)
{

		//ENABLE PORT(S)
	RCC->AHB1ENR|=RCC_AHB1ENR_GPIOBEN;		//GPIO B clock enable
	
			//CONFIGURE PORT PIN FUNCTIONS
	GPIOB->MODER =(1<<(2*0));							//set new pin functions on GPIOB0

	while(1)		//ENTER 'FOREVER' LOOP - THIS LOOP WILL NEVER EXIT
	{
		GPIOB->ODR=(1<<0);									//LED ON 
		for(unsigned i=0;i<1000000;i++)
			{__nop();} 						//delay
		GPIOB->ODR=0;												//LED OFF
		for(unsigned i=0;i<1000000;i++)
			{__nop();}  						//delay
		
	}
}

This should simply flash the on-board LED.

We test by a full compile and download (no debugging)

  • When the optimisation setting is set to -O1, it works fine
  • When the optimisation setting is set to -O0, it seems to hang (no flashing LED

We've both stepped through all the code to try and locate the issue.

Note that when the code is run using the debugger, it works for both settings!

Any ideas?