stm32f103rb_hal_blink

Non-blocking, event triggered blink example using HAL library
Index Commits Files Refs README
app/src/systick.c (1258B)
   1 /*
   2  * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
   3  *
   4  * See file `LICENSE` for full details
   5  */
   6 
   7 #include "main.h"
   8 
   9 /* blocking delay in microseconds using the SysTick timer */
  10 void systick_delay_us(uint32_t delay_us)
  11 {
  12     uint32_t start, current, target, elapsed;
  13 
  14     if (0 == delay_us)
  15         return;
  16 
  17     /* Get the start value of the SysTick counter */
  18     start = SysTick->VAL;
  19 
  20     /* Calculate the total number of SysTick counts required for the delay */
  21     target = delay_us * (SystemCoreClock / 1000000UL);
  22 
  23     /* Loop until the required counts have elapsed */
  24     while (1)
  25     {
  26         /* Get the current value of the SysTick counter */
  27         current = SysTick->VAL;
  28 
  29         /* Handle the case where the SysTick counter wraps around, */
  30         /* counts down to 0 and reloads */
  31         if (current <= start)
  32         {
  33             elapsed = start - current;
  34         }
  35         else
  36         {
  37             /* Counter wrapped around, */
  38             /* add the reload value to account for the wrap */
  39             elapsed = SysTick->LOAD + start - current;
  40         }
  41 
  42         /* Exit the loop when the desired delay is reached */
  43         if (elapsed >= target)
  44         {
  45             break;
  46         }
  47     }
  48 }