cancel
Showing results for 
Search instead for 
Did you mean: 

How to change the clock source using the register?

MSerg
Associate

So, There are two sourse of clocks in STM32.

1) Inner RC generator

2) External crystal.

I need to use second variant. I use asm.

I determined the address responsible for choosing the source of clocking. The address is 0x40023800. I wrote down the value of 0x00050000 at this address.

But the clock source does not change. The microcontroller uses an internal clock source.

What is a solution to this problem?

2 REPLIES 2

I don't know what part you are using.

I don't know how your code is working.

Try again

>>What is a solution to this problem?

Review the RM more carefully. Review what the C code and other libraries are doing in the context of what you're learnt from the RM

>> I wrote down the value of 0x00050000 at this address.

So perhaps enabled the HSE in BYPASS mode, where's it routed after that?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Thanks!

My code:

#define RCC_control_register *(unsigned long*)(0x40023800)
#define HSI_ON 0
 
#define HSE_ON 16
#define HSE_READY_FLAG 17
#define HSE_BYPASS 18
 
 
 
 
void pause (int time){
  for (int i=0; i!=time; i++){
  } 
}
 
int main(){
  
  // Step #1: HSE turn on
  RCC_control_register |= (1 << HSE_ON)|(1 << HSE_BYPASS);
  //Step @2: HSI turn off
  RCC_control_register &=! (1 << HSI_ON);
  
  //Step #3: delay for 5 clock of MCU
  asm("NOP");
  asm("NOP");
  asm("NOP");
  asm("NOP");
  asm("NOP");
  
  // Step #4: Ready the flag of stable clock
  while (!(RCC_control_register & (HSE_READY_FLAG)) == (1 << HSE_READY_FLAG));
   
 
 
  
 
 
  const int timeOfPause = 1000000;
  
  *(unsigned long*)(0x40023830) |= 0x8;
  
  asm("NOP");
  asm("NOP");
  asm("NOP");
  
 
  *(unsigned long*)(0x40020C00) = 0x55550000;
  *(unsigned long*)(0x40020C04) = 0;
  *(unsigned long*)(0x40020C08) = 0;
  *(unsigned long*)(0x40020C0C) = 0;
 
 
  
  while (1){
    *(unsigned long*)(0x40020C14) = 0xF000; 
     pause (timeOfPause);
      
    *(unsigned long*)(0x40020C14) = 0x0000;
     pause (timeOfPause);
    
    *(unsigned long*)(0x40020C14) = 0x1000;
     pause (timeOfPause);
    
    *(unsigned long*)(0x40020C14) = 0x2000;
     pause (timeOfPause);
    
    *(unsigned long*)(0x40020C14) = 0x4000;
     pause (timeOfPause);
    
    *(unsigned long*)(0x40020C14) = 0x8000;
     pause (timeOfPause);
     
       
       
       for (int k=0; k!=10; k++){
         *(unsigned long*)(0x40020C14) = 0x1000;
         pause(timeOfPause);
         *(unsigned long*)(0x40020C14) = (*(unsigned long*)(0x40020C14) << 1);
         pause(timeOfPause);
         *(unsigned long*)(0x40020C14) = (*(unsigned long*)(0x40020C14) << 1);
         pause(timeOfPause);
          *(unsigned long*)(0x40020C14) = (*(unsigned long*)(0x40020C14) << 1);
         pause(timeOfPause);
 
         
       }
     }
}

MCU use inner RC generator, not external