ARMEBS4  revision-26.06.2015
delay.c
Go to the documentation of this file.
1 /************************************************************************//**
2  * \file heivs/delay.c
3  * \brief simple delays
4  * \author marc dot pignat at hevs dot ch
5  ***************************************************************************/
6 
7 #include "heivs/delay.h"
9 #include "stm32/stm32f4xx.h"
10 #include "cmsis/core_cm4.h"
11 
12 void delay_wait_ms(uint32_t ms)
13 {
14  uint32_t i;
15  for (i = 0 ; i < ms ; i++)
16  {
17  delay_wait_us(1000);
18  }
19 }
20 
21 /**
22  * \brief Delay loop
23  *
24  * \param n wait for 3*n CPU clocks
25  */
26 __attribute__ ((naked))
27 static void _delay_loop(uint32_t n)
28 {
29  asm volatile
30  (
31  "1: \n"
32  " subs.n %0, #1 \n" /* This is one clock */
33  " bne.n 1b \n" /* 1+P here is 2 clocks (destination is know and a 16-bit instruction) */
34  " bx lr \n"
35  :
36  : "r" (n)
37  : "cc"
38  );
39 }
40 
41 void delay_wait_us(uint32_t us)
42 {
43  uint32_t hz;
44  uint32_t mhz;
45  uint32_t i;
46 
47  if (us > 1000)
48  {
49  /**
50  * What a looooong wait, call the milisecond version after rounding up
51  */
52  delay_wait_ms((us+999)/1000);
53  return;
54  }
55 
56  /**
57  * Split clock between Hz and MHz
58  */
59  mhz = SystemClock.hclk / (1000*1000);
60  hz = SystemClock.hclk % (1000*1000);
61 
62  /* Multiply by us, can't overflow because us <= 1000 */
63  hz *= us;
64  hz /= (1000*1000);
65 
66  /* Convert to the number of iteration */
67  i = mhz * us;
68  i += (hz * us) / (1000 * 1000);
69 
70  /* Divide by the number of clocks in a loop, see: the _delay_loop ;) */
71  i /= 3;
72 
73  /**
74  * The wait loop is in the form (--i > 0){}, so if i is zero, this loop will
75  * be far too long.
76  * There is only two way of falling here, clock or us are zero.
77  */
78  if (i == 0)
79  {
80  return;
81  }
82 
83  _delay_loop(i);
84 }
85 
86 
87 
void delay_wait_ms(uint32_t ms)
Wait for at least that time.
Definition: delay.c:12
CMSIS Cortex-M4 Device Peripheral Access Layer Header File. This file contains all the peripheral reg...
simple delays
struct system_clock_t SystemClock
void delay_wait_us(uint32_t us)
Wait for at least that time.
Definition: delay.c:41
static void _delay_loop(uint32_t n)
Delay loop.
Definition: delay.c:27
CMSIS Cortex-M4 Device System Source File for STM32F4xx devices.