My STM32F031 Refuses to Run - Page 1
Forum section: electronics / general

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: October 28, 2023, 10:33:16 pm
content: I'm trying to expand my base a bit by getting into STM micros. I have a few STM32F031 micros that I have been trying to blink an LED on. I'm using STM IDE, STM PRG and ST Link V2 on Apple M1 silicon. PRG will connect to the mico only if all the ST Link lines are connected. I can download the hex file and confirm by reading it back out through PRG. IDE will only see the device if I first connect to it in PRG then disconnect and then it connects maybe once every dozen attempts. On the rare occasion that IDE does see the device it will download the file.The micro setup is to use the internal oscillator with clock output on pin 29. I have also tried an external crystal. Download is SWD.The code is a simple HAL toggle and HAL delay to flash the LED. It's a dirt simple program taken from an internet example. The device was setup through IOC.Once the file is downloaded the LED does not flash and the clock output pin is dead. I try a restart, reset pin low and high, Boot 0 high and low. I go to the CPU screen in PRG and can step through the program and see the PC register increment. If I select Run, I lose the connection. I've tried multiple MCUs with the same result; IT WILL NOT RUN. I'm sure it's some simple bone head mistake, but so far it eludes me.main.c belowCode:/* USER CODE BEGIN Header */#include "main.h"/* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);int main(void){HAL_Init();SystemClock_Config();MX_GPIO_Init();while (1){HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);HAL_Delay(500);}}void SystemClock_Config(void){RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK){Error_Handler();}HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);}static void MX_GPIO_Init(void){GPIO_InitTypeDef GPIO_InitStruct = {0};/* GPIO Ports Clock Enable */__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOF_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();/*Configure GPIO pin Output Level */HAL_GPIO_WritePin(LED_1_GPIO_Port, LED_1_Pin, GPIO_PIN_SET);/*Configure GPIO pin : LED_1_Pin */GPIO_InitStruct.Pin = LED_1_Pin;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;HAL_GPIO_Init(LED_1_GPIO_Port, &GPIO_InitStruct);/*Configure GPIO pin : PA8 */GPIO_InitStruct.Pin = GPIO_PIN_8;GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;GPIO_InitStruct.Alternate = GPIO_AF0_MCO;HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);}void Error_Handler(void){__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */}#ifdef USE_FULL_ASSERT/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/void assert_failed(uint8_t *file, uint32_t line){/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */}#endif /* USE_FULL_ASSERT */

author: wek
author_rank: Frequent Contributor
timestamp: October 29, 2023, 07:29:42 am
content: What hardware, a "known-good" board like Nucleo or Disco, or your own? What is the power arrangement, decoupling? Are *all* gound and power pins connected (including VDDA/VSSA if present on given package)?JW

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 29, 2023, 08:57:20 am
content: Post the ioc file!Likely a bad connection somewhere, post pictures of your setup.As Wek said, which board?Ensure SWD (Serial Wire Debug) and systick are enabled in System Core / SYS tab.Without SWD you will always need nRST to access the mcu (This need adjusting in Launch configuration / Reset behaviour / Type=Hardware reset) , while HAL_Delay depends on Systick.With SWD enabled it will be a lot easier, just make sure SWD pins (PA13/PA14) aren't used by anything else!

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: October 29, 2023, 11:39:32 am
content: Pics below with IOC file. I have verified all signals and power to the chip pin. As I said earlier, I can read back the code after download, so its at least getting there.Code:#MicroXplorer Configuration settings - do not modifyCAD.formats=CAD.pinconfig=CAD.provider=File.Version=6GPIO.groupedBy=Group By PeripheralsKeepUserPlacement=falseMcu.CPN=STM32F031C4T6Mcu.Family=STM32F0Mcu.IP0=NVICMcu.IP1=RCCMcu.IP2=SYSMcu.IPNb=3Mcu.Name=STM32F031C(4-6)TxMcu.Package=LQFP48Mcu.Pin0=PC13Mcu.Pin1=PA0Mcu.Pin2=PA8Mcu.Pin3=PA13Mcu.Pin4=PA14Mcu.Pin5=VP_SYS_VS_SystickMcu.PinsNb=6Mcu.ThirdPartyNb=0Mcu.UserConstants=Mcu.UserName=STM32F031C4TxMxCube.Version=6.9.2MxDb.Version=DB.6.0.92NVIC.ForceEnableDMAVector=trueNVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:falseNVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:falseNVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:falseNVIC.SVC_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:trueNVIC.SysTick_IRQn=true\:3\:0\:false\:false\:true\:false\:true\:falsePA0.Mode=SYS_WakeUp0PA0.Signal=SYS_WKUP1PA13.Locked=truePA13.Mode=Serial_WirePA13.Signal=SYS_SWDIOPA14.Mode=Serial_WirePA14.Signal=SYS_SWCLKPA8.Mode=Clock-outPA8.Signal=RCC_MCOPC13.GPIOParameters=GPIO_Speed,PinState,GPIO_Label,GPIO_ModeDefaultOutputPPPC13.GPIO_Label=LED_1PC13.GPIO_ModeDefaultOutputPP=GPIO_MODE_OUTPUT_PPPC13.GPIO_Speed=GPIO_SPEED_FREQ_LOWPC13.Locked=truePC13.PinState=GPIO_PIN_SETPC13.Signal=GPIO_OutputPinOutPanel.RotationAngle=0ProjectManager.AskForMigrate=trueProjectManager.BackupPrevious=falseProjectManager.CompilerOptimize=6ProjectManager.ComputerToolchain=falseProjectManager.CoupleFile=falseProjectManager.CustomerFirmwarePackage=ProjectManager.DefaultFWLocation=trueProjectManager.DeletePrevious=trueProjectManager.DeviceId=STM32F031C4TxProjectManager.FirmwarePackage=STM32Cube FW_F0 V1.11.4ProjectManager.FreePins=falseProjectManager.HalAssertFull=falseProjectManager.HeapSize=0x200ProjectManager.KeepUserCode=trueProjectManager.LastFirmware=trueProjectManager.LibraryCopy=1ProjectManager.MainLocation=Core/SrcProjectManager.NoMain=falseProjectManager.PreviousToolchain=ProjectManager.ProjectBuild=falseProjectManager.ProjectFileName=STM32_Again.iocProjectManager.ProjectName=STM32_AgainProjectManager.ProjectStructure=ProjectManager.RegisterCallBack=ProjectManager.StackSize=0x400ProjectManager.TargetToolchain=STM32CubeIDEProjectManager.ToolChainLocation=ProjectManager.UAScriptAfterPath=ProjectManager.UAScriptBeforePath=ProjectManager.UnderRoot=trueProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-trueRCC.AHBFreq_Value=32000000RCC.APB1Freq_Value=32000000RCC.APB1TimFreq_Value=32000000RCC.FCLKCortexFreq_Value=32000000RCC.FamilyName=MRCC.HCLKFreq_Value=32000000RCC.I2SFreq_Value=32000000RCC.IPParameters=AHBFreq_Value,APB1Freq_Value,APB1TimFreq_Value,FCLKCortexFreq_Value,FamilyName,HCLKFreq_Value,I2SFreq_Value,MCOFreq_Value,PLLCLKFreq_Value,PLLDivider,PLLMCOFreq_Value,PLLMUL,SYSCLKFreq_VALUE,SYSCLKSource,TimSysFreq_Value,USART1Freq_Value,VCOOutput2Freq_ValueRCC.MCOFreq_Value=32000000RCC.PLLCLKFreq_Value=32000000RCC.PLLDivider=RCC_PREDIV_DIV2RCC.PLLMCOFreq_Value=32000000RCC.PLLMUL=RCC_PLL_MUL8RCC.SYSCLKFreq_VALUE=32000000RCC.SYSCLKSource=RCC_SYSCLKSOURCE_PLLCLKRCC.TimSysFreq_Value=32000000RCC.USART1Freq_Value=32000000RCC.VCOOutput2Freq_Value=4000000VP_SYS_VS_Systick.Mode=SysTickVP_SYS_VS_Systick.Signal=SYS_VS_Systickboard=customisbadioc=false[ Specified attachment is not available ]Cube IDE was able to connect this morning. Here's the output:Code:Log output file: /tmp/STM32CubeProgrammer_mhHHxm.logST-LINK SN : 38FF6E06304E4B3014171943ST-LINK FW : V2J42S7Board : --Voltage : 3.29VSWD freq : 4000 KHzConnect mode: Under ResetReset mode : Hardware resetDevice ID : 0x444Revision ID : Rev 1.0Device name : STM32F03xFlash size : 16 KBytesDevice type : MCUDevice CPU : Cortex-M0BL Version : 0x10Memory Programming ...Opening and parsing file: ST-LINK_GDB_server_Na7huQ.srecFile : ST-LINK_GDB_server_Na7huQ.srecSize : 5.00 KBAddress : 0x08000000Erasing memory corresponding to segment 0:Erasing internal memory sectors [0 4]Download in Progress:File download completeTime elapsed during download operation: 00:00:00.501Verifying ...Download verified successfullyShutting down...Exit.Still won't run. When I go back and connect with PRG and look at CPU page the core state reports Halted. Someting strange though. Voltage on reset pin 7 is .7 V. I attached a 56K pullup and no change to voltage.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 29, 2023, 12:48:57 pm
content: Add a 100nF cap between nRST-GND for noise stability, no need for pullup.BOOT0 should be always low to run from the flash.You forgot VDDA/VSSA pins. VDDA is not only for the ADC, but also for the PLL!Also follow the reference power decoupling, it seems you're using only 2x 100nF?At least, put 1uF+100nF to every VDD(A) pin.

author: Ground_Loop
author_rank: Frequent Contributor
timestamp: October 29, 2023, 01:38:54 pm
content: As I suspected, bone head mistake. VSSA and VDDA were the problem. Thanks David.Quote from: DavidAlfa on October 29, 2023, 12:48:57 pm

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 30, 2023, 12:27:07 am
content: Quote from: Ground_Loop on October 29, 2023, 01:38:54 pmThis mistake is more common than you'd think. Many people tend to assume that the "analog" power supply pins of MCUs can be left open if no analog peripheral is used, and this is rarely the case in practice.Same for Microchip MCUs. Here you can suspect that the PLL was just not running as DavidAlfa pointed out. In some datasheets, while they usually recommend a powering scheme in which VDDA pins are connected, they do not always explain why, and I've already seen engineers do the same mistake as you did here.