Electronic – STM32F4 Discovery board only works in DEBUG mode

debuggingembeddedperipheralstm32

I generated my project with STMCUBEMX with CAN, SPI, I2C peripherals and did some coding on top of it. Code basicly polls the CANBUS, stores data to some IC over i2c and sends the processed data to the connected nodemcu over the SPI.

It was working as it should until I decided to stop working in debugging mode and use it as a standalone circuit. So After flashing the code I unplugged the USB cable and plugged it back in. And that is when strange behaviors began. When I tried to debug through LEDs and I found out that any operation regarding SPI or I2C blocks the code and no LED is lit after that point. A normal function which only does arithmetic operations and doesnt use any peripheral doesn't prevent the LEDs also the CAN and push button interrupts work as I observed through the LEDs( They always work even when the circuit seems stuck ). As soon as I flash the code it goes back to working as expected.

So far I have tried :

  • Changing the crystal from High Speed External to High Speed Internal
  • Powering the board from an external source (USB cable attached to a phone charger adapter.)
  • Switching to ST-Link instead of Jlink.
  • Removing the SPI and I2C slaves from the circuit by unplugging the cables.

Since it is the peripherals that causing the problem , I figured it could have something to do with peripheral clocks. But basicly got stuck after this point.

I am using stm32f4 disco board at 84 mhz. I updated my debugger to jlink from st-link and I am debugging through the VS code extension cortex-debug

Start of my main

      int main(void)
    {

      HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();
  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_CAN1_Init();
  MX_SPI2_Init();
  MX_I2C1_Init();
  InitializeTimer();

  /* USER CODE BEGIN 2 */
  Can_Setup();

  if (HAL_CAN_Start(&hcan1) != HAL_OK)
  {
    Error_Handler();
  }

  HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);

  uint8_t Atr[64];
  uint16_t AtrLen = sizeof(Atr);
  int sw = smComSCI2C_Open(ESTABLISH_SCI2C, 0x00, Atr, &AtrLen);  // I2c peripheral init function, this is succesfull but not very stable. 
  if(sw == SW_OK)
  {
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12,GPIO_PIN_SET);
  }

    generateIdentity(); // uses i2c, the code stucks after this point
    generateAddress();  // uses i2c
    while(connectWifi() != 0 );  //uses SPI
    getNonce(&storedNonce);  //uses SPI

My clock function

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 84;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

System Init Function

void SystemInit(void)
{
  /* FPU settings ------------------------------------------------------------*/
  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
  #endif
  /* Reset the RCC clock configuration to the default reset state ------------*/
  /* Set HSION bit */
  RCC->CR |= (uint32_t)0x00000001;

  /* Reset CFGR register */
  RCC->CFGR = 0x00000000;

  /* Reset HSEON, CSSON and PLLON bits */
  RCC->CR &= (uint32_t)0xFEF6FFFF;

  /* Reset PLLCFGR register */
  RCC->PLLCFGR = 0x24003010;

  /* Reset HSEBYP bit */
  RCC->CR &= (uint32_t)0xFFFBFFFF;

  /* Disable all interrupts */
  RCC->CIR = 0x00000000;

#if defined (DATA_IN_ExtSRAM) || defined (DATA_IN_ExtSDRAM)
  SystemInit_ExtMemCtl(); 
#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */

  /* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
}

I have been stuck in this issue for the last couple of days, and the deadline for the project is very soon.All helps are much appreciated 🙂

Best Answer

remove the DWT_Delay() It seems it causes the issues in release mode.