summaryrefslogtreecommitdiff
path: root/project/Core/Src/System/systick.c
blob: 5d4d259de0a16838b1045af1f3694f96a54893c6 (plain) (blame)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "systick.h"
#include "player_actions.h"

systick_t *get_systick() {
	return (systick_t *) 0xE000E010;
}

// This function is to Initialize SysTick registers
void init_systick()
{
	// Use the SysTick global structure pointer to do the following in this
	// exact order with separate lines for each step:
	//
	// Disable SysTick by clearing the CTRL (CSR) register.
	// Set the LOAD (RVR) to 8 million to give us a 100 milliseconds timer.
	// Set the clock source bit in the CTRL (CSR) to the internal clock.
	// Set the enable bit in the CTRL (CSR) to start the timer.
	systick_t *s = get_systick();

	s->CSR &= ~1;
	s->CSR |= 1<<2;
	s->CSR |= 1<<1; // enable interrupt
	s->RVR = 79;
	s->CSR |= 1;
}



// This fuction is to create delay using SysTick timer counter
void delay_systick()
{
	// Using the SysTick global structure pointer do the following:
	// Create a for loop that loops 10 times
	// Inside that for loop check the COUNTFLAG bit in the CTRL (CSR)
	// register in a loop. When that bit is set exit this inner loop
	// to do another pass in the outer loop of 10.
	systick_t *s = get_systick();

	for (int i = 0; i < 10; i++) {
		while (~s->CSR & (1<<16))
			;
	}
}

int check_systick() {
	static int i = 0;

	systick_t *s = get_systick();
	if (!(~s->CSR & (1<<16))) {
		i++;
	}

	if (i >= 10) {
		i = 0;
		return 1;
	} else {
		return 0;
	}
}

static uint32_t count = 0;
void SysTick_Handler() {
	count++;
}

uint32_t systick_get_count() {
	return count;
}