blob: e3779052ff3fdf49d9cdfe1dc2807495237c5479 (
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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
//go:build mimxrt1062
package runtime
import (
"device/arm"
"device/nxp"
"runtime/interrupt"
"runtime/volatile"
"unsafe"
)
type timeUnit int64
const (
lastCycle = SYSTICK_FREQ/1000 - 1
cyclesPerMicro = CORE_FREQ / 1000000
)
const (
pitFreq = OSC_FREQ // PIT/GPT are muxed to 24 MHz OSC
pitCyclesPerMicro = pitFreq / 1000000
pitSleepTimer = 0 // x4 32-bit PIT timers [0..3]
)
var (
tickCount volatile.Register64
cycleCount volatile.Register32
pitActive volatile.Register32
pitTimeout interrupt.Interrupt
)
var (
// debug exception and monitor control
DEM_CR = (*volatile.Register32)(unsafe.Pointer(uintptr(0xe000edfc)))
DWT_CR = (*volatile.Register32)(unsafe.Pointer(uintptr(0xe0001000)))
DWT_CYCCNT = (*volatile.Register32)(unsafe.Pointer(uintptr(0xe0001004)))
)
func ticksToNanoseconds(ticks timeUnit) int64 {
return int64(ticks) * 1000
}
func nanosecondsToTicks(ns int64) timeUnit {
return timeUnit(ns / 1000)
}
func initSysTick() {
const (
traceEnable = 0x01000000 // enable debugging & monitoring blocks
cycleCountEnable = 0x00000001 // cycle count register
)
// disable SysTick if already running
if arm.SYST.SYST_CSR.HasBits(arm.SYST_CSR_ENABLE_Msk) {
arm.SYST.SYST_CSR.ClearBits(arm.SYST_CSR_ENABLE_Msk)
}
// zeroize the counter
tickCount.Set(0)
arm.SYST.SYST_RVR.Set(lastCycle)
arm.SYST.SYST_CVR.Set(0)
arm.SYST.SYST_CSR.Set(arm.SYST_CSR_TICKINT | arm.SYST_CSR_ENABLE)
// set SysTick and PendSV priority to 32
nxp.SystemControl.SHPR3.Set((0x20 << nxp.SCB_SHPR3_PRI_15_Pos) |
(0x20 << nxp.SCB_SHPR3_PRI_14_Pos))
// turn on cycle counter
DEM_CR.SetBits(traceEnable)
DWT_CR.SetBits(cycleCountEnable)
cycleCount.Set(DWT_CYCCNT.Get())
// enable PIT, disable counters
nxp.PIT.MCR.Set(0)
for i := range nxp.PIT.TIMER {
nxp.PIT.TIMER[i].TCTRL.Set(0)
}
// register sleep timer interrupt
pitTimeout = interrupt.New(nxp.IRQ_PIT, timerWake)
pitTimeout.SetPriority(0x21)
pitTimeout.Enable()
}
func initRTC() {
if !nxp.SNVS.LPCR.HasBits(nxp.SNVS_LPCR_SRTC_ENV) {
// if SRTC isn't running, start it with default Jan 1, 2019
nxp.SNVS.LPSRTCLR.Set(uint32((0x5c2aad80 << 15) & 0xFFFFFFFF))
nxp.SNVS.LPSRTCMR.Set(uint32(0x5c2aad80 >> 17))
nxp.SNVS.LPCR.SetBits(nxp.SNVS_LPCR_SRTC_ENV)
}
}
//go:export SysTick_Handler
func tick() {
tickCount.Set(tickCount.Get() + 1)
cycleCount.Set(DWT_CYCCNT.Get())
}
func ticks() timeUnit {
mask := arm.DisableInterrupts()
tick := tickCount.Get()
cycs := cycleCount.Get()
curr := DWT_CYCCNT.Get()
arm.EnableInterrupts(mask)
var diff uint32
if curr < cycs { // cycle counter overflow/rollover occurred
diff = (0xFFFFFFFF - cycs) + curr
} else {
diff = curr - cycs
}
frac := uint64(diff*0xFFFFFFFF/cyclesPerMicro) >> 32
if frac > 1000 {
frac = 1000
}
return timeUnit(1000*tick + frac)
}
func sleepTicks(duration timeUnit) {
if duration >= 0 {
curr := ticks()
last := curr + duration // 64-bit overflow unlikely
for curr < last {
cycles := timeUnit((last - curr) / pitCyclesPerMicro)
if cycles > 0xFFFFFFFF {
cycles = 0xFFFFFFFF
}
if !timerSleep(uint32(cycles)) {
return // return early due to interrupt
}
curr = ticks()
}
}
}
func timerSleep(cycles uint32) bool {
pitActive.Set(1)
nxp.PIT.TIMER[pitSleepTimer].LDVAL.Set(cycles)
nxp.PIT.TIMER[pitSleepTimer].TCTRL.Set(nxp.PIT_TIMER_TCTRL_TIE) // enable interrupts
nxp.PIT.TIMER[pitSleepTimer].TCTRL.SetBits(nxp.PIT_TIMER_TCTRL_TEN) // start timer
for {
//arm.Asm("wfi") // TODO: causes hardfault! why?
if pitActive.Get() == 0 {
return true
}
if hasScheduler {
break // some other interrupt occurred and needs servicing
}
}
timerWake(interrupt.Interrupt{}) // clear and disable timer
return false
}
func timerWake(interrupt.Interrupt) {
pitActive.Set(0)
// TFLGn[TIF] are set to 1 when a timeout occurs on the associated timer, and
// are cleared to 0 by writing a 1 to the corresponding TFLGn[TIF].
nxp.PIT.TIMER[pitSleepTimer].TFLG.Set(nxp.PIT_TIMER_TFLG_TIF) // clear interrupt flag
nxp.PIT.TIMER[pitSleepTimer].TCTRL.Set(0) // disable timer/interrupt enable flags
}
|