I want to write code for STM32F446 MCU with registers (no HAL functions). Here is my code, but I can not define any variable in this code. Any variable I define in this code, is not executable. For example, I defined a variable "timer" in the end lines of my code which is being increased in an infinite while loop. But in debugging, the pointer jumps from line "timer++" and does not execute it. How can I fix it?
#include "stm32f446xx.h" // Device header
void sysClockConfig (void);
void GPIO_Config (void);
void sysClockConfig (void)
{
#define PLL_M 8
#define PLL_N 72
#define PLL_P 2
// 1. Enable HSE and wait for the HSE to be ready
RCC->CR |= RCC_CR_HSION;
while (!(RCC->CR & RCC_CR_HSIRDY))
;
// 2. Set the power enable clock and the voltage regulator
RCC->APB1ENR |= RCC_APB1ENR_PWREN;
PWR->CR |= PWR_CR_VOS;
// 3. Configure the flash prefetch and the LATENCY related setting
FLASH->ACR |= FLASH_ACR_ICEN | FLASH_ACR_DCEN | FLASH_ACR_PRFTEN | FLASH_ACR_LATENCY_2WS;
// 4. Configure prescaler HCLK, PCLK1, PCLK2
// AHB PR
RCC->CFGR |= RCC_CFGR_HPRE_DIV1;
// APB1 PR
RCC->CFGR |= RCC_CFGR_PPRE1_DIV2;
// APB2 PR
RCC->CFGR |= RCC_CFGR_PPRE2_DIV2;
// 5. Configure the main PLL
RCC->PLLCFGR = (PLL_M << 0) | (PLL_N << 6) | (PLL_P << 16) | (RCC_PLLCFGR_PLLSRC_HSI);
// 6. Enable PLL and wait for it to become ready
RCC->CR |= RCC_CR_PLLON;
while (!(RCC->CR & RCC_CR_PLLRDY))
;
// 7. Select the clock source and wait for it to be set
RCC->CFGR |= RCC_CFGR_SW_PLL;
while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL)
;
}
void GPIO_Config (void)
{
// 1. Enable the GPIO clock
RCC->AHB1ENR |= (1<<0);
// 2. Set the pin as output
GPIOA->MODER |= (1<<10); // Pin PA5 (bits 11:10) as output (01)
// 3. Configure the output mode
GPIOA->OTYPER = 0;
GPIOA->OSPEEDR = 0;
}
int main(void)
{
int timer = 100;
GPIO_Config();
sysClockConfig();
while(1)
{
GPIOA->BSRR |= (1<<5); // Set PA5
timer++;
GPIOA->BSRR |= ((1<<5) <<16); // Reset PA5
}
}
Unless you do something with the vaiable timer (such print it out) then the compiler can tell that you don't need it and saves time by not really accessing it.
If the reason that you are using it is to waste some time, try declaring it volatile int timer.
Also, note that you are wasting time between setting and clearing PA5, but not between clearing and setting. Maybe the jump instruction to go back to the start of the loop will waste some time, but maybe this will not be where you expect it.