Skip to main content
vbk22398
Senior
October 23, 2024
Question

Bare Metal STM32L496

void SystemClock_Config(void)
{
// Step 1: Enable HSI
RCC->CR |= RCC_CR_HSION; // Enable HSI
while (!(RCC->CR & RCC_CR_HSIRDY)); // Wait until HSI is ready

// Step 2: Configure the voltage scaling
PWR->CR1 |= PWR_CR1_VOS_0; // Set voltage scale to 1 (VOS = 1)

// Step 3: Configure the PLL
RCC->PLLCFGR = 0; // Clear PLL configuration register
RCC->PLLCFGR |= (1 << RCC_PLLCFGR_PLLM_Pos) | // PLLM = 1
(10 << RCC_PLLCFGR_PLLN_Pos) | // PLLN = 10 (HSI frequency * 10 = 80 MHz)
(0 << RCC_PLLCFGR_PLLP_Pos) | // PLLP = 2 (DIV2)
(2 << RCC_PLLCFGR_PLLQ_Pos) | // PLLQ = 2 (DIV2)
(2 << RCC_PLLCFGR_PLLR_Pos) | // PLLR = 2 (DIV2)
RCC_PLLCFGR_PLLSRC_HSI; // PLL source = HSI

// Step 4: Enable the PLL
RCC->CR |= RCC_CR_PLLON; // Enable PLL
while (!(RCC->CR & RCC_CR_PLLRDY)); // Wait until PLL is ready

// Step 5: Set PLL as the system clock source
RCC->CFGR &= ~RCC_CFGR_SW; // Clear SW bits
RCC->CFGR |= RCC_CFGR_SW_PLL; // Set PLL as the system clock source
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); // Wait until PLL is used as system clock

// Step 6: Configure the AHB and APB prescalers
RCC->CFGR &= ~RCC_CFGR_HPRE; // AHB prescaler = 1
RCC->CFGR &= ~RCC_CFGR_PPRE1; // APB1 prescaler = 1
RCC->CFGR &= ~RCC_CFGR_PPRE2; // APB2 prescaler = 1

// Step 7: Configure flash latency
FLASH->ACR &= ~FLASH_ACR_LATENCY; // Clear latency bits
FLASH->ACR |= FLASH_ACR_LATENCY_4WS; // Set latency to 4 wait states for 80 MHz
}


The code doesn't break out of while loop.
I am struck in the step 5. unable to resolve the issue.
// Step 5: Set PLL as the system clock source. Help me resolve this issue.

This topic has been closed for replies.

9 replies

waclawek.jan
Super User
October 23, 2024

You have to set APB prescalers, and, more importantly, the FLASH waitstates, *before* you switch the system clock to PLL in RCC_CFGR.

JW

gbm
Super User
October 23, 2024

PWR must be enabled in RCC before use.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
waclawek.jan
Super User
October 23, 2024

> PWR must be enabled in RCC before use.

Correct; however, Range 1 is the default VOS setting anyway.

JW

gbm
Super User
October 23, 2024

Also, the value written to PLLM, PLLP, PLLQ and PLLR fields is the divisor value - 1.

You may find the working clock setup code in my USB stuff, link in signature - Example/Inc/L4 folder.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
waclawek.jan
Super User
October 23, 2024

In the mid-range 'L4, the Q and R dividers are 2 * (value + 1), and P is quite bizarre, 7 or 17...  and HSI frequency is 16MHz. These things tend to change from STM32 to STM32 (sub)family.

> link in signature

I don't see any signature in this thread (https://community.st.com/t5/stm32-mcus-products/bare-metal-stm32l496/m-p/734800 ).

JW

Andrew Neil
Super User
October 23, 2024

@waclawek.jan wrote:

> link in signature

I don't see any signature


Here:

AndrewNeil_0-1729685120919.png

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
mƎALLEm
ST Technical Moderator
October 23, 2024

Hello @vbk22398 ,

In next time please use </> button to post your code. See these tips on posting a thread.

Thank you for your understanding.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
waclawek.jan
Super User
October 23, 2024

Interesting. I don't see it.

waclawekjan_0-1729688586061.png

But by this point I gave up any attempts to comment on the state of affairs; it's just waste of time and energy. I have better places to be ignored.

JW

waclawek.jan
Super User
November 12, 2024

I was wrong about the signature, and it's my fault.

I've been approached by an Admin who told me, that I have switched off displaying signatures in my settings - which I indeed have done back then when Khoros was new, in order to decrease visual noise and try to squeeze in as much relevant information on a screenful as possible - and have forgotten about it since then.

(And I've been also confused by the fact that I do see signatures, when I am not logged in.)

JW