This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

My first STM32 code - please criticize me

I have just wrote my first code on STM32 using Keil software - blinking LED. It combines a fragments from different sources so it can look really horrible. Please criticize me so I can learn how to write proper code and don't learn stupid habits.

Thank you!

#include "stm32f30x.h"

void SysTick_Handler(void);
void TimingDelay_Decrement(void);
void Delay(__IO uint32_t nTime);

static __IO uint32_t TimingDelay;

int main(void)
{
  RCC->AHBENR  |=  ((1UL << 21) );                  // Enable GPIOE clock??
  GPIOE->MODER |= (1 << 2*8);                       // PIN 9 as output

//GPIOE->BSRR = 1 << 9;                             // LED ON
//GPIOE->BSRR = 1 << 9 << 16;                       // LED OFF
//GPIOE->BRR = 1 << 9;                              // LED OFF

    if (SysTick_Config(SystemCoreClock / 1000))
    {
    /* Capture error */
    while (1);
  }

    while(1)
    {
        GPIOE->BSRR = 1 << 8;
        Delay(50);
        GPIOE->BRR = 1 << 8;
        Delay(50);
    }
}

void SysTick_Handler(void)
{
  TimingDelay_Decrement();
}

void Delay(__IO uint32_t nTime)
{
  TimingDelay = nTime;

  while(TimingDelay != 0);
}

void TimingDelay_Decrement(void)
{
  if (TimingDelay != 0x00)
  {
    TimingDelay--;
  }

}

Why I have to enable GPIOE clock? And what's the difference between "1UL << 21" and "1 << 21"?

Parents
  • You can gate the clock on or off to stop sections of the silicon from consuming power. Unfortunately synchronous logic designs need a clock to function, so in order to write to registers in the GPIOE peripheral domain, you need to enable the clocking of that section.

    In this context 1UL is the same a 1, if you were shifting left by 31 a signed integer gets into the sign bit. It will work, but might give you a compiler warning, or static analyzer hit. This would be more of an issue on a 16-bit compiler.

Reply
  • You can gate the clock on or off to stop sections of the silicon from consuming power. Unfortunately synchronous logic designs need a clock to function, so in order to write to registers in the GPIOE peripheral domain, you need to enable the clocking of that section.

    In this context 1UL is the same a 1, if you were shifting left by 31 a signed integer gets into the sign bit. It will work, but might give you a compiler warning, or static analyzer hit. This would be more of an issue on a 16-bit compiler.

Children
No data