2019-02-09 06:24 AM
Hi All,
I am working with the STM32F302R8. I want to set the system clock as the PLL through derived from the HSI. I want a clock of 64MHz which I should have set through the appropriate registers.
I think I have set all I need however, after the RCC configration is done, the system clock switch status (SWS) in the RCC->CFGR register still indicates 0x00, initiating that the HSI is used as a clock directly.
I have a blinky at the end which currently the LED stays on while the code is running and never turns on when I am debugging.
#include "stm32f302x8.h"
int x =0;
void Delay (uint32_t nTime);
uint16_t ADC1ConvertedValue = 0;
uint16_t ADC1ConvertedVoltage = 0;
uint16_t calibration_value = 0;
volatile uint32_t TimingDelay = 0;
int init ()
{
//RCC->CR = 0x1; //HSI on
RCC->CFGR = 0x003C0002; //PLLSCr = HSI/2 , PLLMUL = x116 (62Mhz), PLL = sys clock
RCC->CR |= 0x01000000; //PLL on
RCC->AHBENR |= (1<<18); //Enabling AHB GPIOB
//RCC->AHBENR |= (1<<28); //Enabling ADC Clocks
// At this stage the microcontroller clock tree is already configured
// RCC->CFGR2 &= 0x00000013; //ADC12 clock diveded by 6
GPIOB->MODER |= 0x00000010; // PB2 Output
GPIOB->OSPEEDR |= 0x0; //Low speed output on all of port B
GPIOB->PUPDR |= 0x30; // PB2 pull down
GPIOB->MODER |= 0x0000000C; // GPIOB PB1 set to Analog Mode
GPIOB->PUPDR |= 0x0000000B; // GPIOB PB1 set to Pull dowm
return(0);
}
int main()
{
init();
while (1)
{
GPIOB->BSRR = (1<<2) ;
for ( x = 0; x<500000; x++);
GPIOB->BSRR = (1<<(2+16));
for ( x = 0; x<500000; x++);
}
}
Solved! Go to Solution.
2019-05-14 10:09 AM
So I "fixed" it.
I set the data bits on the PC side to 7 bits and it works. However my STM is clearly set to 8 bits as shown below.
The datasheet clearly shows that bits 28 and 12, M1 and M0 respectively, set the word length. So when they are set to 00 the data bits are 8 bits.
So I debugged the code and check it. Below it shows clearly that the register value indicates that both of them are 0. However as shown in the debugger view while the register value reflects this, there is only one "M" bit set and it is bit 12. Bit 28 is not show there at all. So basically in 7 bit mode it works flawlessly however in 8 bit mode it doesn't work at all despite the reference manual saying otherwise.
Can anyone shed some light on this?
2019-02-09 06:33 AM
You have to stage the configuration, where you get the PLL running, wait for it to lock, and then select it as a source for the micro-controller, and wait for that to select. The flash wait states would also need to accommodate the faster clock, ie above 24 MHz, and need to be set before the clock switches.
2019-02-11 11:57 AM
HI Thanks so much for your help. I got it working thanks to you. Is there any way that I can confirm that the system clock is infact now 64Mhz? I can tell the frequency has changed cause the blink is faster but can I confirm the correct number?
Below is the code I used for setting it as you suggested.
RCC->CR = 0x1; //HSI on
while((RCC->CR & RCC_CR_HSIRDY) != RCC_CR_HSIRDY);
RCC->CR &= ~RCC_CR_PLLON; //Disable PLL
while((RCC->CR & RCC_CR_PLLRDY)== RCC_CR_PLLRDY); // wait for pll to turn off
RCC->CFGR = RCC_CFGR_PLLSRC_HSI_DIV2 | RCC_CFGR_PLLMUL16 ; //PLLSCr = HSI/2 , PLLMUL = x6 (62Mhz),
RCC->CR |= 0x01000000; //PLL on
while((RCC->CR & RCC_CR_PLLRDY) != RCC_CR_PLLRDY); // wait for PLL to be ready
RCC->CFGR = RCC_CFGR_SW_1; //PLL as system clock
while((RCC->CFGR & RCC_CFGR_SWS_1) != RCC_CFGR_SWS_1); //wait fpr pll to be used as syste, clock
Thanks
2019-05-14 10:09 AM
So I "fixed" it.
I set the data bits on the PC side to 7 bits and it works. However my STM is clearly set to 8 bits as shown below.
The datasheet clearly shows that bits 28 and 12, M1 and M0 respectively, set the word length. So when they are set to 00 the data bits are 8 bits.
So I debugged the code and check it. Below it shows clearly that the register value indicates that both of them are 0. However as shown in the debugger view while the register value reflects this, there is only one "M" bit set and it is bit 12. Bit 28 is not show there at all. So basically in 7 bit mode it works flawlessly however in 8 bit mode it doesn't work at all despite the reference manual saying otherwise.
Can anyone shed some light on this?