2021-01-01 08:57 AM
Hello.
I am using stm32f765 series mcu with iar compiler. A strange thing was caught during development and I left an inquiry.
The problem I'm facing seems to be the difference between the system clock right after the firmware is uploaded and the system clock after a reset or power-up.
For example, if i create an 'cnt' variable in the while statement and do 'cnt++', 100 is output immediately after firmware is uploaded, However, when reset or power is re-applied, 80 is output.
Could this happen? If so, when will it occur?
Below is the code for setting my system clock.
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
/** 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 = 216;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != 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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART3;
PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
}
Below is the problem area. There is a difference between the value of the 'pp' variable immediately after firmware upload and after the reset. Immediately after uploading, a value of 2400 or higher is displayed, and immediately after reset, a value of about 1200 is output.
while(1){
pp++;
SPI_read_data(&hspi3, 0x0B, SPI3_Rx_data, sizeof(SPI3_Rx_data));
for(int i=0 ; i<DATA_PAD ; i++){
Data[i] = (SPI3_Rx_data[2 * i] << 8 | SPI3_Rx_data[2 * i + 1]);
SPI3_Rx_data[2 * i] = SPI3_Rx_data[(2 * i) + 1] = 0;
}
HAL_ADC_Start_IT(&hadc1);
while(ADC1_flag == 0);
HAL_ADC_Stop_IT(&hadc1);
ADC1_flag = 0;
HAL_ADC_Start_IT(&hadc2);
while(ADC2_flag == 0);
HAL_ADC_Stop_IT(&hadc2);
printf("%d\r\n",pp);
...
2021-01-01 10:27 AM
Might be simpler just to print out the current SYSCLK,/AHB APB1CLK, and APB2CLK values are understood on the processor side.
The System Loader will start if the device is blank, several newer models need a power cycle to clear the "BOOT" latch. Programming doesn't clear this.
A debugger may hide the fact it really starts execution from the 0x1FFF0000 vectors.
The SystemInit() or SystemClock_Config() may not handle non-reset conditions well.
2021-01-01 12:18 PM
> There is a difference between the value of the 'pp' variable immediately after firmware upload and after the reset.
Okay, and how is this variable initialized? Seems like the first output should be 1 if it's zero initialized, not 1200 or 2400.