SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */ SysTick->LOAD = ticks - 1; /* set reload register */ SysTick->VAL = 0; /* Load the SysTick Counter Value */ SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */ return (0); "> SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */ SysTick->LOAD = ticks - 1; /* set reload register */ SysTick->VAL = 0; /* Load the SysTick Counter Value */ SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */ return (0); "> SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */ SysTick->LOAD = ticks - 1; /* set reload register */ SysTick->VAL = 0; /* Load the SysTick Counter Value */ SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */ return (0); ">
/* Juliusz Tarnowski */

/* This solution is not optimal. */
/* LEDs should not be trigger in the interrupt handler, but in the main loop. */
/* Interrupt should be shortes as possible. */

#include "stm32f4xx.h"
#include "system_stm32f4xx.h"

volatile int flag = 0;

// __STATIC_INLINE is keil keyword
// Code taken from <https://elektronika327.blogspot.com/2016/10/5-stm32f4-systick-na-rejestrach.html>
__STATIC_INLINE uint32_t _SysTick_Config(uint32_t ticks)
{
  if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk)  return (1);        /* Reload value impossible */
  SysTick->LOAD  = ticks - 1;                                    /* set reload register */
  SysTick->VAL   = 0;                                            /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                      /* Enable SysTick IRQ and SysTick Timer */
  return (0);                                                    /* Function successful */
}

void led_blue()   { GPIOD->ODR ^= GPIO_ODR_OD15; }
void led_red()    { GPIOD->ODR ^= GPIO_ODR_OD14; }
void led_orange() { GPIOD->ODR ^= GPIO_ODR_OD13; }
void led_green()  { GPIOD->ODR ^= GPIO_ODR_OD12; }

void turn_led(uint32_t j) 
{
  switch(j) {
    case 0:
      led_blue();   break;
    case 1:
      led_red();    break;
    case 2:
      led_orange(); break;
    case 3:
      led_green();  break;
    default:
      led_blue();
      led_red();
      led_orange();
      led_green();
      break;
    }
}

void SysTick_Handler(void) 
{
  if(flag >= 4) { flag = 0; }
  turn_led(flag);
  flag++;
}

void setup(void) 
{
  RCC -> AHB1ENR |= RCC_AHB1ENR_GPIODEN; // Enable output on D port
  GPIOD -> MODER |= GPIO_MODER_MODER15_0;  
  GPIOD -> MODER |= GPIO_MODER_MODER14_0;  
  GPIOD -> MODER |= GPIO_MODER_MODER13_0;  
  GPIOD -> MODER |= GPIO_MODER_MODER12_0; 

// SysTick_Config comes from system_stm32f4xx.h
//  SysTick_Config(16000000);

// _SysTick_Config is implemented publicly in code
// Every interrupt of SysTick trigger SysTick_Handler fuction which flashes diodes.
// 16 MHz * 16kk = 1s
// 16 MHz is processor clock frequency
  _SysTick_Config(16000000);
}

int main(void) 
{
  setup();
  while(1) {}
  return 0;
}
/* Juliusz Tarnowski */
#include "stm32f4xx.h"

/* Solution for stm32f469 */

int flag1 = 0;
int flag2 = 0;

// __STATIC_INLINE is keil keyword
// Code taken from <https://elektronika327.blogspot.com/2016/10/5-stm32f4-systick-na-rejestrach.html>
__STATIC_INLINE uint32_t _SysTick_Config(uint32_t ticks)
{
  if ((ticks - 1) > SysTick_LOAD_RELOAD_Msk)  return (1);        /* Reload value impossible */
  SysTick->LOAD  = ticks - 1;                                    /* set reload register */
  SysTick->VAL   = 0;                                            /* Load the SysTick Counter Value */
  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
                   SysTick_CTRL_TICKINT_Msk   |
                   SysTick_CTRL_ENABLE_Msk;                      /* Enable SysTick IRQ and SysTick Timer */
  return (0);                                                    /* Function successful */
}

void led_green()  { GPIOG->ODR ^= GPIO_ODR_OD6; }
void led_orange() { GPIOD->ODR ^= GPIO_ODR_OD4; }
void led_red()    { GPIOD->ODR ^= GPIO_ODR_OD5; }
void led_blue()   { GPIOK->ODR ^= GPIO_ODR_OD3; }

void turn_led(uint32_t j) 
{
  switch(j) {
    case 0:
      led_blue();   break;
    case 1:
      led_red();    break;
    case 2:
      led_orange(); break;
    case 3:
      led_green();  break;
    default:
      led_blue();
      led_red();
      led_orange();
      led_green();
      break;
    }
}

void SysTick_Handler(void) 
{
	flag2 = 1;
}

void setup(void) 
{
  RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOGEN; // Enable output on G port
  RCC -> AHB1ENR |= RCC_AHB1ENR_GPIODEN; // Enable output on D port
  RCC -> AHB1ENR |= RCC_AHB1ENR_GPIOKEN; // Enable output on K port

  GPIOG -> MODER |= GPIO_MODER_MODER6_0;  
  GPIOD -> MODER |= GPIO_MODER_MODER4_0;  
  GPIOD -> MODER |= GPIO_MODER_MODER5_0;  
  GPIOK -> MODER |= GPIO_MODER_MODER3_0; 
	
  _SysTick_Config(8000000);
}

void loop(void) 
{
  if(flag2 == 1) 
  {
    if(flag1 >= 4) { flag1 = 0; }
    turn_led(flag1);
    flag1++;
    flag2 = 0;
  }
}

int main(void) 
{
  setup();
  while(1) 
  {
    loop();
  }
  return 0;
}