-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSysTick.c
77 lines (71 loc) · 3.36 KB
/
SysTick.c
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
69
70
71
72
73
74
75
76
77
// SysTick.c
// Runs on TM4C1294
// Provide functions that initialize the SysTick module, wait at least a
// designated number of clock cycles, and wait approximately a multiple
// of 10 milliseconds using busy wait. After a power-on-reset, the
// TM4C1294 gets its clock from the 16 MHz precision internal oscillator,
// which can vary by +/- 1% at room temperature and +/- 3% across all
// temperature ranges. If you are using this module, you may need more
// precise timing, so it is assumed that you are using the PLL to set
// the system clock to 120 MHz. This matters for the function
// SysTick_Wait10ms(), which will wait longer than 10 ms if the clock is
// slower.(but is modified in this version despite a slower clock to still wait
// 10ms!)
// Copyright information is at the bottom of the file
#include <stdint.h>
#include "SysTick.h"
#define NVIC_ST_CTRL_R (*((volatile uint32_t *)0xE000E010))
#define NVIC_ST_RELOAD_R (*((volatile uint32_t *)0xE000E014))
#define NVIC_ST_CURRENT_R (*((volatile uint32_t *)0xE000E018))
#define NVIC_ST_CTRL_COUNT 0x00010000 // Count flag
#define NVIC_ST_CTRL_CLK_SRC 0x00000004 // Clock Source
#define NVIC_ST_CTRL_INTEN 0x00000002 // Interrupt enable
#define NVIC_ST_CTRL_ENABLE 0x00000001 // Counter mode
#define NVIC_ST_RELOAD_M 0x00FFFFFF // Counter load value
// Initialize SysTick with busy wait running at bus clock.
void SysTick_Init(void){
NVIC_ST_CTRL_R = 0; // disable SysTick during setup
NVIC_ST_RELOAD_R = NVIC_ST_RELOAD_M; // maximum reload value
NVIC_ST_CURRENT_R = 0; // any write to current clears it
// enable SysTick with core clock
NVIC_ST_CTRL_R = NVIC_ST_CTRL_ENABLE+NVIC_ST_CTRL_CLK_SRC;
}
// Time delay using busy wait.
// The delay parameter is in units of the core clock. (units of 8.333 nsec for 120 MHz clock)
void SysTick_Wait(uint32_t delay){
volatile uint32_t elapsedTime;
uint32_t startTime = NVIC_ST_CURRENT_R;
do{
elapsedTime = (startTime-NVIC_ST_CURRENT_R)&0x00FFFFFF;
}
while(elapsedTime <= delay);
}
// Time delay using busy wait.
// This assumes 120 MHz system clock.
void SysTick_Wait10ms(uint32_t delay){
uint32_t i;
for(i=0; i<delay; i++){
SysTick_Wait(200000); // wait 10ms (assumes 20 MHz clock) -- uses scale factor of 100
}
}
// modified from:
// Daniel Valvano
// April 3, 2014
/* This example accompanies the books
"Embedded Systems: Introduction to ARM Cortex M Microcontrollers",
ISBN: 978-1469998749, Jonathan Valvano, copyright (c) 2014
Volume 1, Program 4.7
"Embedded Systems: Real Time Interfacing to ARM Cortex M Microcontrollers",
ISBN: 978-1463590154, Jonathan Valvano, copyright (c) 2014
Program 2.11, Section 2.6
Copyright 2014 by Jonathan W. Valvano, [email protected]
You may use, edit, run or distribute this file
as long as the above copyright notice remains
THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
VALVANO SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
For more information about my classes, my research, and my books, see
http://users.ece.utexas.edu/~valvano/
*/