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)


  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */


  if (HAL_CAN_Start(&hcan1) != HAL_OK)

  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)

    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 
  /** 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.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 84;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  /** Initializes the CPU, AHB and APB busses clocks 
  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)

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 */
  /* 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)
#endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */

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

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 :)

  • \$\begingroup\$ Can you github your whole project? There are lots of things it can be. \$\endgroup\$
    – pgvoorhees
    Commented Jun 29, 2019 at 21:44
  • \$\begingroup\$ When you had it running in the debugger, did you remove all breakpoints before cutting the debugger loose? \$\endgroup\$
    – Ben Voigt
    Commented Jun 29, 2019 at 22:30

2 Answers 2


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


You have to be very carefull when using HAL_Delay(), or any other interrupt based delay for that matter. HAL_Delay() is being incremented by SysTick timer. If you set NVIC priorities of other interrupts infront of SysTick timer there is a possibility that the timer won't make the tick in the CPU cycle thus stucking in an endless loop. Be sure that when using HAL_Delay, the SysTick timer has higher priority then the rest of them.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.