aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/delay.h
blob: 03d5c1ed10c9183cdd1bd29fdcae496dad253657 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * @file delay.h
 * @brief Delay implementation
 */

#ifndef _DELAY_H_
#define _DELAY_H_

/**
 * @brief Delay the given number of microseconds.
 *
 * Note that this function currently assumes you are on a LeafLabs
 * board, and will only work properly if you follow the LeafLabs RCC
 * configuration.
 *
 * @param us Number of microseconds to delay.
 */
static inline void delay_us(uint32 us) {
    /* TODO this makes unwarranted assumptions about the RCC
     * config; add a hook so users can make their own decisions. */
    /* So (2^32)/12 micros max, or less than 6 minutes */
    us *= 12;

    /* fudge for function call overhead  */
    us--;
    asm volatile("   mov r0, %[us]          \n\t"
                 "1: subs r0, #1            \n\t"
                 "   bhi 1b                 \n\t"
                 :
                 : [us] "r" (us)
                 : "r0");
}
#endif