Skip to content

Optimal 64 MHz clock for STM32F103 ... #131

@RoboDurden

Description

@RoboDurden

@WestlingPi , @hoverboardhavoc let's continue here with this clock issue.

Indeed: >When the HSI (=high-speed internal clock signal) is used as a PLL clock input, the maximum system clock frequency that can be achieved is 64 MHz.

Please no modifications to system files like system_gd32f10x.c. I think these get easily replace while making changes to the RTE runtime enviroment ?

Better add a new Keil target and add the define there:
Image

#ifdef __SYSTEM_CLOCK_48M_PLL_IRC8M
uint32_t SystemCoreClock = __SYSTEM_CLOCK_48M_PLL_IRC8M;

comes first, so the default 72M are ignored.

With this i get
Image
on my Gen1 board and msTicks again verified with stop watch.
But 48MHz is not optimal anyway ? How would you achieve 64MHz ?

So i asked ChatGpt5 again:

please give me the Keil init code for STM32F103C8 to run without external oscillator at the max clock of 64 MHz In the datasheet i read: When the HSI is used as a PLL clock input, the maximum system clock frequency that can be achieved is 64 MHz.
I do not want to touch system_stm32f10x.c . And the code is also used for GD32f103, so my system file is system_gd32f10x.c Is it possible to detect at the beginning of main() that the mcu is a stm32f103 and then set the clock from the default 72 Mhz to 64 Mhz ?

This is the final code that might work for stm32f103:

#if TARGET==2		
		
	void clock_init_stm32(void)
	{
		/* 1. Enable internal 8 MHz oscillator (IRC8M = HSI) */
		RCU_CTL |= RCU_CTL_IRC8MEN;
		while((RCU_CTL & RCU_CTL_IRC8MSTB) == 0);

		/* 2. Configure Flash wait states for 64 MHz 
			 (2 wait states needed for 48–72 MHz range) */
		FMC_WS &= ~0x7;   // clear WSCNT[2:0]
		FMC_WS |= 0x2;    // 2 wait states

		/* 3. Configure PLL: IRC8M / 2 * 16 = 64 MHz */
		RCU_CFG0 &= ~(RCU_CFG0_PLLMF | RCU_CFG0_PLLSEL);
		RCU_CFG0 |= (RCU_PLLSRC_IRC8M_DIV2 | RCU_PLL_MUL16);

		/* 4. Set prescalers: 
					AHB = /1 (64 MHz), 
					APB1 = /2 (32 MHz, must be =36 MHz), 
					APB2 = /1 (64 MHz) */
		RCU_CFG0 &= ~(RCU_CFG0_AHBPSC | RCU_CFG0_APB1PSC | RCU_CFG0_APB2PSC);
		RCU_CFG0 |= (RCU_AHB_CKSYS_DIV1 | RCU_APB1_CKAHB_DIV2 | RCU_APB2_CKAHB_DIV1);

		/* 5. Enable PLL */
		RCU_CTL |= RCU_CTL_PLLEN;
		while((RCU_CTL & RCU_CTL_PLLSTB) == 0);

		/* 6. Switch system clock to PLL */
		RCU_CFG0 &= ~RCU_CFG0_SCS;
		RCU_CFG0 |= RCU_CKSYSSRC_PLL;
		while((RCU_CFG0 & RCU_SCSS_PLL) == 0);

		/* 7. Update SystemCoreClock variable */
		SystemCoreClock = 64000000UL;
	}
	uint32_t dev_id = 0;	
	uint32_t pll_mul = 0;
#endif
		
int main (void)
{
	iBug = 1;
	ConfigRead();		// reads oConfig defined in defines.h from flash
	
	#if TARGET==2
		#define DBGMCU_IDCODE   (*(volatile uint32_t*)0xE0042000)
		#define DEV_ID_MASK     0x00000FFF
		#define STM32F103_DEV   0x410   // STM32F103
		// GD32F103 will read as something else (typically 0x419)
		dev_id = DBGMCU_IDCODE & DEV_ID_MASK;		// will be 1044 for GD32F103RC
		if (dev_id == STM32F103_DEV) 
			clock_init_stm32();   // 64 MHz
		else
			SystemCoreClockUpdate();  // ensure GD32 sets 72 MHz correctly

		pll_mul = (RCU_CFG0 & RCU_CFG0_PLLMF) >> 18;  // bits differ per header, check what value you actually get		
			// will be 1 on GD32F103, should be 14 on true stm32f103.
	#else
		SystemCoreClockUpdate();
	#endif
  SysTick_Config(SystemCoreClock / 1000);	//  Configure SysTick to generate an interrupt every millisecond

	#ifdef MASTER_OR_SINGLE
		FlagStatus chargeStateLowActive = SET;
		int16_t pwmMaster = 0;
		int16_t scaledSpeed = 0;
		int16_t scaledSteer  = 0;
		float expo = 0;
		float steerAngle = 0;
		float xScale = 0;
	#endif

I get this for my 2.2.7 with gd32f103RC when i force clock_init_stm32() function:

Image

But for 10000ms msTicks, my stop watch only gives 9 seconds.
ChatGpt then made me check pll_mul and this confirmed that for gd32f103, you still get 72M.
But for stm32f103, it should work.

Please test :-)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions