new Blackpill STM32G431CBU6

Post here first, or if you can't find a relevant section!
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: new Blackpill STM32G431CBU6

Post by dannyf »

I tried this on PY32, but untested on G0/G4.

Code: Select all

//simplest routine to switch to HSE
//no error checking
void SystemCoreClock2HSE(void) {
	//turn on hse
	RCC->CR |= RCC_CR_HSEON;				//1->turn on HSE, 0->turn off HSE
	//wait for HSE to be ready - error if stuck here
	while ((RCC->CR & RCC_CR_HSERDY)==0);		//1->HSE ready, 0->HSE not ready
	//switch to HSE
	RCC->CFGR = (RCC->CFGR &~RCC_CFGR_SW) |	//CLEAR_BIT SW bits
				(1 << RCC_CFGR_SW_Pos);		//0->HSI, 1->HSE, 2->PLLRCLK, 3->LSI, 4->LSE, others->reserved
	//wait for the switch to take place
	//while ((RCC->CFGR & RCC_CFGR_SWS) != (1<< RCC_CFGR_SWS_Pos));	//if stuck here, there is an error
	SystemCoreClockUpdate();				//update systemcoreclock
}

it can be easily changed to switch to HSI or other sources.

caution: zero error handing.
trimarco232
Posts: 14
Joined: Wed Jul 12, 2023 11:09 am
Answers: 1

Re: new Blackpill STM32G431CBU6

Post by trimarco232 »

thanks !
maybe it works , but 10 time too slow ...
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: new Blackpill STM32G431CBU6

Post by dannyf »

That means it is working. As the crystal is like a few mhz and your default oscillator likely runs at 10s of mhz.
Post Reply

Return to “General discussion”