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
163
|
// Derivative work of Teensyduino Core Library
// http://www.pjrc.com/teensy/
// Copyright (c) 2017 PJRC.COM, LLC.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// 1. The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// 2. If the Software is incorporated into a build system that allows
// selection among a list of target devices, then similar target
// devices manufactured by PJRC.COM must be included in the list of
// target devices and selectable in the same manner.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//go:build nxp && mk66f18
package runtime
import (
"device/arm"
"device/nxp"
"machine"
"runtime/interrupt"
"runtime/volatile"
)
type timeUnit int64
func ticksToNanoseconds(ticks timeUnit) int64 {
return int64(ticks) * 1000
}
func nanosecondsToTicks(ns int64) timeUnit {
return timeUnit(ns / 1000)
}
// cyclesPerMilli-1 is used for the systick reset value.
// The systick current value will be decremented on every clock cycle.
// An interrupt is generated when the current value reaches 0.
// A value of freq/1000 generates a tick (irq) every millisecond (1/1000 s).
var cyclesPerMilli = machine.CPUFrequency() / 1000
// number of systick irqs (milliseconds) since boot
var systickCount volatile.Register64
func millisSinceBoot() uint64 {
return systickCount.Get()
}
func initSysTick() {
nxp.SysTick.RVR.Set(cyclesPerMilli - 1)
nxp.SysTick.CVR.Set(0)
nxp.SysTick.CSR.Set(nxp.SysTick_CSR_CLKSOURCE | nxp.SysTick_CSR_TICKINT | nxp.SysTick_CSR_ENABLE)
nxp.SystemControl.SHPR3.Set((32 << nxp.SystemControl_SHPR3_PRI_15_Pos) | (32 << nxp.SystemControl_SHPR3_PRI_14_Pos)) // set systick and pendsv priority to 32
}
func initSleepTimer() {
nxp.SIM.SCGC5.SetBits(nxp.SIM_SCGC5_LPTMR)
nxp.LPTMR0.CSR.Set(nxp.LPTMR0_CSR_TIE)
timerInterrupt = interrupt.New(nxp.IRQ_LPTMR0, timerWake)
timerInterrupt.Enable()
}
//go:export SysTick_Handler
func tick() {
systickCount.Set(systickCount.Get() + 1)
}
// ticks are in microseconds
func ticks() timeUnit {
mask := arm.DisableInterrupts()
current := nxp.SysTick.CVR.Get() // current value of the systick counter
count := millisSinceBoot() // number of milliseconds since boot
istatus := nxp.SystemControl.ICSR.Get() // interrupt status register
arm.EnableInterrupts(mask)
micros := timeUnit(count * 1000) // a tick (1ms) = 1000 us
// if the systick counter was about to reset and ICSR indicates a pending systick irq, increment count
if istatus&nxp.SystemControl_ICSR_PENDSTSET != 0 && current > 50 {
micros += 1000
} else {
cycles := cyclesPerMilli - 1 - current // number of cycles since last 1ms tick
cyclesPerMicro := machine.CPUFrequency() / 1000000
micros += timeUnit(cycles / cyclesPerMicro)
}
return micros
}
// sleepTicks spins for a number of microseconds
func sleepTicks(duration timeUnit) {
now := ticks()
end := duration + now
cyclesPerMicro := machine.ClockFrequency() / 1000000
if duration <= 0 {
return
}
nxp.LPTMR0.PSR.Set((3 << nxp.LPTMR0_PSR_PCS_Pos) | nxp.LPTMR0_PSR_PBYP) // use 16MHz clock, undivided
for now < end {
count := uint32(end-now) / cyclesPerMicro
if count > 65535 {
count = 65535
}
if !timerSleep(count) {
// return early due to interrupt
return
}
now = ticks()
}
}
var timerInterrupt interrupt.Interrupt
var timerActive volatile.Register32
func timerSleep(count uint32) bool {
timerActive.Set(1)
nxp.LPTMR0.CMR.Set(count) // set count
nxp.LPTMR0.CSR.SetBits(nxp.LPTMR0_CSR_TEN) // enable
for {
arm.Asm("wfi")
if timerActive.Get() == 0 {
return true
}
if hasScheduler {
// bail out, as the interrupt may have awoken a goroutine
break
}
// if there is no scheduler, block for the entire count
}
timerWake(timerInterrupt)
return false
}
func timerWake(interrupt.Interrupt) {
timerActive.Set(0)
nxp.LPTMR0.CSR.Set(nxp.LPTMR0.CSR.Get()&^nxp.LPTMR0_CSR_TEN | nxp.LPTMR0_CSR_TCF) // clear flag and disable
}
|