cancel
Showing results for 
Search instead for 
Did you mean: 

Code works only in Debug Mode

DPila.1
Associate II

The code runs in debug mode, however when I disconnect debugger and run the code normally it does not work as working in debug mode?

I am using STM32H7xx single core microcontroller.

Awaiting to hearing from you.

6 REPLIES 6
Rim LANDOLSI
ST Employee

Hello @DPila.1​ and welcome to community,

Could you, please, provide screenshot on how you run the code and if there are errors occurred.

It could be, also, helpful to provide which CubeIDE version used for this project.

Thanks,

Rim

CubeIDE version 1.12.0

Below is my Booting section.

int main(void)
{
  /* USER CODE BEGIN 1 */
  int8_t mainStatus = 0x0F;
  uint8_t readValue = 0;
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
  SCB_InvalidateDCache();
  __disable_irq();
  __disable_fault_irq();
 
  __DSB();
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
  SCB_EnableDCache();
  __enable_irq();
  __enable_fault_irq();
 
  GPIO_Flash_Pins_Reset();
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_OCTOSPI1_Init();
  MX_UART4_Init();
  /* USER CODE BEGIN 2 */
  __DSB();
 
  /* Initialization Complete */
  while((HAL_UART_GetState(UART_GetHandle(4)) & HAL_UART_STATE_BUSY_TX) == HAL_UART_STATE_BUSY_TX)
  {}
 
  if(HAL_UART_Transmit(UART_GetHandle(4), (uint8_t *)"Main Started!\r\n\0", 16, UART4_TIMEOUT) != HAL_OK)
  {
	  return (HAL_ERROR);
  }
 
  mainStatus = OSPI_SetupFlash(OSPI_GetHandle(1), 0);
  (void)mainStatus;
 
  /* Flash Setup Complete */
  if(HAL_UART_Transmit(UART_GetHandle(4), (uint8_t *)"QSPI Flash Initialized!\r\n\0", 26, UART4_TIMEOUT) != HAL_OK)
  {
	  return (HAL_ERROR);
  }
 
//  readValue = (*(uint8_t *)(0x92000008));
  (void)readValue;
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

Code does not reach line 52 in this case. It reaches up to line 48. However in case of debug mode it reaches line 64 without any issue.

One more thing as input:

My QSPI reads correctly in debug mode and when I run standalone it reads 0xFF for all the transactions over QSPI line.

Hope this will help understanding the condition.

Check BOOT0 configuration on board.

SCB_InvalidatedDCache() is almost always the wrong choice..

Disabling interrupts tends to break SysTick based timeouts.

Make sure you enable all the clocks you expect to be running. Perhaps dump RCC in working/not-working cases. The debug tends to enable things it needs to facilitate it's work, without it you're expected to do all the initialization you need.

Not sure what's going on with QSPI/OSPI, the presentation here is very superficial.. Usually the initialization is multi-phase, decompose/bisect until you identify what specifically fails. Most IC lack an async reset, many have non-volatile settings, and expectations that commands will complete before the next one is issued.

Make a string printing function that computes the length on the fly, hard coding values seems such a poor choice.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

QSPI Flash I am using can work up to speeds of 133MHz. I am giving clock to QSPI through PLL2Q which is 190MHz further the clock goes through QSPI prescalar of 4 before applied to Flash.

As soon as QSPI is initialized I turn ON power to Flash Chip through P-MOS, post that I wait for 10 milliseconds before issuing first command i.e. read status register command to check if flash is busy or not.

void MX_OCTOSPI1_Init(void)
{
 
  /* USER CODE BEGIN OCTOSPI1_Init 0 */
  /* This is to ensure that the peripheral is not with any pre-condition */
  __HAL_RCC_OSPI1_FORCE_RESET();
  __HAL_RCC_OSPI1_RELEASE_RESET();
  /* USER CODE END OCTOSPI1_Init 0 */
 
  OSPIM_CfgTypeDef sOspiManagerCfg = {0};
 
  /* USER CODE BEGIN OCTOSPI1_Init 1 */
 
  /* USER CODE END OCTOSPI1_Init 1 */
  hospi1.Instance = OCTOSPI1;
  hospi1.Init.FifoThreshold = 4;
  hospi1.Init.DualQuad = HAL_OSPI_DUALQUAD_DISABLE;
  hospi1.Init.MemoryType = HAL_OSPI_MEMTYPE_MICRON;
  hospi1.Init.DeviceSize = 24;
  hospi1.Init.ChipSelectHighTime = 1;
  hospi1.Init.FreeRunningClock = HAL_OSPI_FREERUNCLK_DISABLE;
  hospi1.Init.ClockMode = HAL_OSPI_CLOCK_MODE_0;
  hospi1.Init.WrapSize = HAL_OSPI_WRAP_NOT_SUPPORTED;
  hospi1.Init.ClockPrescaler = 4;
  hospi1.Init.SampleShifting = HAL_OSPI_SAMPLE_SHIFTING_NONE;
  hospi1.Init.DelayHoldQuarterCycle = HAL_OSPI_DHQC_ENABLE;
  hospi1.Init.ChipSelectBoundary = 0;
  hospi1.Init.DelayBlockBypass = HAL_OSPI_DELAY_BLOCK_USED;
  hospi1.Init.MaxTran = 0;
  hospi1.Init.Refresh = 0;
  if (HAL_OSPI_Init(&hospi1) != HAL_OK)
  {
    Error_Handler();
  }
  sOspiManagerCfg.ClkPort = 1;
  sOspiManagerCfg.NCSPort = 1;
  sOspiManagerCfg.IOLowPort = HAL_OSPIM_IOPORT_1_LOW;
  if (HAL_OSPIM_Config(&hospi1, &sOspiManagerCfg, HAL_OSPI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN OCTOSPI1_Init 2 */
 
  if(hospi1.Instance == OCTOSPI1)
  {
	  /* This is done to match the working source code from Keil Environment */
	  OCTOSPI1->CR &= ~(OCTOSPI_CR_EN);
	  OCTOSPI1->CR &= ~(OCTOSPI_CR_FMODE);
	  OCTOSPI1->CR |= OCTOSPI_CR_FMODE_0;
	  OCTOSPI1->CR |= OCTOSPI_CR_EN;
  }
 
  if(hospi1.State != HAL_OSPI_STATE_READY)
  {
	  /* Making sure SPI peripheral is in ready state */
	  HAL_OSPI_Abort(&hospi1);
  }
 
  /* Turning ON QSPI Flash Power through P-MOS */
  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
  HAL_Delay(1);
 
  /* USER CODE END OCTOSPI1_Init 2 */
 
}

This is my code of OCTOSPI1 Initialization