summaryrefslogtreecommitdiffstats
path: root/src/target/firmware/lib/delay.c
blob: 112e956a5a4ecd820af1ced3fc147d8f5c05aa48 (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
35
36
37
38
39
40
41
#include <delay.h>

#define CALYPSO_CLK		52000000
#define CYCLES_PER_LOOP		4
#define COUNTS_PER_MS		(CALYPSO_CLK / CYCLES_PER_LOOP) / 1000
#define COUNTS_PER_US		COUNTS_PER_MS/1000

void delay_us(unsigned int us)
{
	unsigned int counts = COUNTS_PER_US * us;

	asm volatile
	(
		"mov r3, %[counts]\n\t"
		"usloop:\n\t"
			"subs r3, #1\n\t"
			"bne usloop\n\t"
		: /* we have no output, list empty */
		: [counts] "r" (counts)
		/* r3 and flags are clobbered */
		: "r3", "cc"
	);
}

void delay_ms(unsigned int ms)
{
	unsigned int counts = COUNTS_PER_MS;
	while (ms--) {
		asm volatile
		(
			"mov r3, %[counts]\n\t"
			"msloop:\n\t"
				"subs r3, #1\n\t"
				"bne msloop\n\t"
			: /* we have no output, list empty */
			: [counts] "r" (counts)
			/* r3 and flags are clobbered */
			: "r3", "cc"
		);
	}
}