2021-07-21 02:52 AM
#include "stm32f4xx.h"
#define PLLM 25
#define PLLN 320
#define PLLP 1//divide 4
void SysConfigure()
{
RCC->CR|=RCC_CR_HSEON;//External Crystal Oscillator
while(!(RCC->CR & RCC_CR_HSERDY));// External Crystal oscillator status flag checking
RCC->APB1ENR|=RCC_APB1ENR_PWREN;// power Enable
PWR->CR|=PWR_CR_VOS;// voltage scaling
FLASH->ACR=(FLASH_ACR_DCEN|FLASH_ACR_ICEN|FLASH_ACR_PRFTEN|FLASH_ACR_LATENCY_2WS);
RCC->CFGR|=(RCC_CFGR_PPRE2_DIV1|RCC_CFGR_PPRE1_DIV2|RCC_CFGR_HPRE_DIV1);
RCC->PLLCFGR=((PLLM<<0)|(PLLN<<6)|(PLLP<<16)|(1<<22));
RCC->CR|=RCC_CR_PLLON;
while(!(RCC->CR & RCC_CR_PLLRDY));
RCC->CFGR|=RCC_CFGR_SW_PLL;
while(!(RCC->CFGR & RCC_CFGR_SWS_PLL));
}
void GpioConfigure()
{
RCC->AHB1ENR|=RCC_AHB1ENR_GPIOCEN;
GPIOC->MODER|=(1<<26);
GPIOC->OTYPER|=0;
GPIOC->OSPEEDR|=0;
}
void Delay(int x)
{
for(int i=0;i<x;i++);
}
int main()
{
SysConfigure();
GpioConfigure();
while(1)
{
GPIOC->BSRR=(1<<13);
Delay(1000);
GPIOC->BSRR=(1<<29);
Delay(1000);
}
return 0;
}
2021-07-21 02:59 AM
JW
2021-07-21 05:17 AM
@waclawek.jan (Community Member)
The board is stm32f401cc black pill .
sir i already worked with hal gpio firmware and that was fine. i need to work with register level programming so i should learn reference manual.
why did you say "avoid initial clock setup"?
wright now i am working with stm32cube ide.
2021-07-21 05:24 AM
Delay unlikely to be long enough to be human visible. Check signal toggle with a scope.
Double check register settings with debugger.
ORing with zero seems pointless.
Watch for hazard related to enabling APB clocks and touching just enabled peripheral.
2021-07-21 05:35 AM
Avoid clock setup so you can see if the code there is problematic, ie stuck in loop or whatever.
If you skip that function part will simply continue clocking from 16 MHz HSI
2021-07-21 06:32 AM
@Tesla DeLorean (Community Member),waclawek.jan (Community Member)
hey brothers,
i have gotten a led blink program with internal oscillator. it has given below and it's fine.
But we know the internal oscillator's accuracy is a bit low than the external frequency .
so could you pls give me a little bit about configuration of external oscillator .
could you pls give me a sample pll configuration code because i have tried many times on the internet but i couldn't reach correct tutorials.
#include "stm32f4xx.h"
#define DELAY_COUNT 10000000
static void delay( void )
{
uint32_t i = 0;
for( i=0; i<=DELAY_COUNT; i++ );
}
int main(void){
RCC->AHB1ENR|= RCC_AHB1ENR_GPIOCEN;
GPIOC->MODER = 0x55555555;
while(1){
GPIOC->BSRR = 0x0000FFFF;
delay();
GPIOC->BSRR = 0xFFFF0000;
delay();
}
}
2021-07-21 07:37 AM
@Tesla DeLorean (Community Member),waclawek.jan (Community Member)
hey brothers,
now i have gotten the correct output .that was the delay issues.@Tesla DeLorean (Community Member) ,your assumption is absolutely true.i couldn't see the difference with my delay .
anyway a special thanks to everyone to spent some more times for me.
now i have another doubt could you clear that ?
doubt
can i read the current clock speed from stm32 mcu ,thus which topic should i learn?
2021-07-21 09:51 AM
>>can i read the current clock speed from stm32 mcu ,thus which topic should i learn?
You can unpack the settings for the sources and PLL at the register level.
The IC doesn't know what the HSE clock source frequency is, this is conveyed via HSE_VALUE define which you provide at a project level to describe the hardware you're using.
Examples where the HAL unpack the speed, look at the UART baud rate computation code, it works out what the APB clock is for the given UART, and then divides that down.
printf("APB1=%d\n", HAL_RCC_GetPCLK1Freq());
printf("APB2=%d\n", HAL_RCC_GetPCLK2Freq());