Skip to content

Commit 61a7a21

Browse files
committed
Update teensy support after rebase
1 parent 3eee51d commit 61a7a21

File tree

4 files changed

+82
-84
lines changed

4 files changed

+82
-84
lines changed

src/machine/board_teensy36.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,3 @@ const (
104104
defaultUART4RX = NoPin // D34
105105
defaultUART4TX = NoPin // D33
106106
)
107-

src/machine/machine_nxpmk66f18.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,3 +284,17 @@ func (p FastPin) Clear() { p.PCOR.Set(true) }
284284
func (p FastPin) Toggle() { p.PTOR.Set(true) }
285285
func (p FastPin) Write(v bool) { p.PDOR.Set(v) }
286286
func (p FastPin) Read() bool { return p.PDIR.Get() }
287+
288+
// SysTick is configured in runtime_nxpmk66f18.go
289+
290+
// number of systick irqs (milliseconds) since boot
291+
var systickCount volatile.Register32
292+
293+
//go:export SysTick_Handler
294+
func tick() {
295+
systickCount.Set(systickCount.Get() + 1)
296+
}
297+
298+
func MillisSinceBoot() uint32 {
299+
return systickCount.Get()
300+
}

src/machine/uart_nxpmk66f18.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,35 @@ var (
6262
//go:linkname gosched runtime.Gosched
6363
func gosched()
6464

65+
// PutcharUART writes a byte to the UART synchronously, without using interrupts
66+
// or calling the scheduler
67+
func PutcharUART(u UART, c byte) {
68+
// ensure the UART has been configured
69+
if !u.SCGC.HasBits(u.SCGCMask) {
70+
u.configure(UARTConfig{}, false)
71+
}
72+
73+
for u.TCFIFO.Get() > 0 {
74+
// busy wait
75+
}
76+
u.D.Set(c)
77+
u.C2.Set(uartC2TXActive)
78+
}
79+
80+
// PollUART manually checks a UART status and calls the ISR. This should only be
81+
// called by runtime.abort.
82+
func PollUART(u UART) {
83+
if u.SCGC.HasBits(u.SCGCMask) {
84+
u.handleStatusInterrupt(u.Interrupt)
85+
}
86+
}
87+
6588
type UART = *UARTData
6689

6790
type UARTData struct {
6891
*nxp.UART_Type
69-
SCGC *volatile.Register32
70-
SCGCMask uint32
71-
IRQNumber uint32
92+
SCGC *volatile.Register32
93+
SCGCMask uint32
7294

7395
DefaultRX Pin
7496
DefaultTX Pin
@@ -81,11 +103,11 @@ type UARTData struct {
81103
Interrupt interrupt.Interrupt
82104
}
83105

84-
var UART0 = UARTData{UART_Type: nxp.UART0, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART0, IRQNumber: nxp.IRQ_UART0_RX_TX, DefaultRX: defaultUART0RX, DefaultTX: defaultUART0TX}
85-
var UART1 = UARTData{UART_Type: nxp.UART1, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART1, IRQNumber: nxp.IRQ_UART1_RX_TX, DefaultRX: defaultUART1RX, DefaultTX: defaultUART1TX}
86-
var UART2 = UARTData{UART_Type: nxp.UART2, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART2, IRQNumber: nxp.IRQ_UART2_RX_TX, DefaultRX: defaultUART2RX, DefaultTX: defaultUART2TX}
87-
var UART3 = UARTData{UART_Type: nxp.UART3, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART3, IRQNumber: nxp.IRQ_UART3_RX_TX, DefaultRX: defaultUART3RX, DefaultTX: defaultUART3TX}
88-
var UART4 = UARTData{UART_Type: nxp.UART4, SCGC: &nxp.SIM.SCGC1, SCGCMask: nxp.SIM_SCGC1_UART4, IRQNumber: nxp.IRQ_UART4_RX_TX, DefaultRX: defaultUART4RX, DefaultTX: defaultUART4TX}
106+
var UART0 = UARTData{UART_Type: nxp.UART0, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART0, DefaultRX: defaultUART0RX, DefaultTX: defaultUART0TX}
107+
var UART1 = UARTData{UART_Type: nxp.UART1, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART1, DefaultRX: defaultUART1RX, DefaultTX: defaultUART1TX}
108+
var UART2 = UARTData{UART_Type: nxp.UART2, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART2, DefaultRX: defaultUART2RX, DefaultTX: defaultUART2TX}
109+
var UART3 = UARTData{UART_Type: nxp.UART3, SCGC: &nxp.SIM.SCGC4, SCGCMask: nxp.SIM_SCGC4_UART3, DefaultRX: defaultUART3RX, DefaultTX: defaultUART3TX}
110+
var UART4 = UARTData{UART_Type: nxp.UART4, SCGC: &nxp.SIM.SCGC1, SCGCMask: nxp.SIM_SCGC1_UART4, DefaultRX: defaultUART4RX, DefaultTX: defaultUART4TX}
89111

90112
func init() {
91113
UART0.Interrupt = interrupt.New(nxp.IRQ_UART0_RX_TX, UART0.handleStatusInterrupt)
@@ -97,6 +119,10 @@ func init() {
97119

98120
// Configure the UART.
99121
func (u UART) Configure(config UARTConfig) {
122+
u.configure(config, true)
123+
}
124+
125+
func (u UART) configure(config UARTConfig, canSched bool) {
100126
// from: serial_begin
101127

102128
if !u.Configured {
@@ -124,8 +150,12 @@ func (u UART) Configure(config UARTConfig) {
124150

125151
if u.Configured {
126152
// don't change baud rate mid transmit
127-
for u.Transmitting.Get() != 0 {
128-
// busy wait flush, for compatibility with putchar
153+
if canSched {
154+
u.Flush()
155+
} else {
156+
for u.Transmitting.Get() != 0 {
157+
// busy wait flush
158+
}
129159
}
130160
}
131161

src/runtime/runtime_nxpmk66f18.go

Lines changed: 28 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
"device/nxp"
3737
"machine"
3838
"runtime/interrupt"
39-
"runtime/volatile"
4039
)
4140

4241
const (
@@ -47,10 +46,10 @@ const (
4746
_DEFAULT_FTM_PRESCALE = 1
4847
)
4948

50-
var (
51-
_SIM_SOPT2_IRC48SEL = uint32(3 << nxp.SIM_SOPT2_PLLFLLSEL_Pos)
52-
_SMC_PMCTRL_HSRUN = uint8(3 << nxp.SMC_PMCTRL_RUNM_Pos)
53-
_SMC_PMSTAT_HSRUN = uint8(0x80 << nxp.SMC_PMSTAT_PMSTAT_Pos)
49+
const (
50+
_SIM_SOPT2_IRC48SEL = 3 << nxp.SIM_SOPT2_PLLFLLSEL_Pos
51+
_SMC_PMCTRL_HSRUN = 3 << nxp.SMC_PMCTRL_RUNM_Pos
52+
_SMC_PMSTAT_HSRUN = 0x80 << nxp.SMC_PMSTAT_PMSTAT_Pos
5453
)
5554

5655
//go:export Reset_Handler
@@ -143,7 +142,7 @@ func initSystem() {
143142

144143
// now program the clock dividers
145144
// config divisors: 180 MHz core, 60 MHz bus, 25.7 MHz flash, USB = IRC48M
146-
nxp.SIM.CLKDIV1.Set((0 << nxp.SIM_CLKDIV1_OUTDIV1_Pos) | (2 << nxp.SIM_CLKDIV1_OUTDIV2_Pos) | (6 << nxp.SIM_CLKDIV1_OUTDIV4_Pos))
145+
nxp.SIM.CLKDIV1.Set((0 << nxp.SIM_CLKDIV1_OUTDIV1_Pos) | (2 << nxp.SIM_CLKDIV1_OUTDIV2_Pos) | (0 << nxp.SIM_CLKDIV1_OUTDIV1_Pos) | (6 << nxp.SIM_CLKDIV1_OUTDIV4_Pos))
147146
nxp.SIM.CLKDIV2.Set((0 << nxp.SIM_CLKDIV2_USBDIV_Pos))
148147

149148
// switch to PLL as clock source, FLL input = 16 MHz / 512
@@ -205,6 +204,7 @@ func initInternal() {
205204
nxp.FTM1.C1SC.Set(0x28)
206205
nxp.FTM1.SC.Set((1 << nxp.FTM_SC_CLKS_Pos) | (_DEFAULT_FTM_PRESCALE << nxp.FTM_SC_PS_Pos))
207206

207+
// causes a data bus error for unknown reasons
208208
// nxp.FTM2.CNT.Set(0)
209209
// nxp.FTM2.MOD.Set(_DEFAULT_FTM_MOD)
210210
// nxp.FTM2.C0SC.Set(0x28)
@@ -231,78 +231,40 @@ func initInternal() {
231231
interrupt.New(nxp.IRQ_LPTMR0, wake).Enable()
232232

233233
// analog_init();
234-
235-
// #if !defined(TEENSY_INIT_USB_DELAY_BEFORE)
236-
// #if TEENSYDUINO >= 142
237-
// #define TEENSY_INIT_USB_DELAY_BEFORE 25
238-
// #else
239-
// #define TEENSY_INIT_USB_DELAY_BEFORE 50
240-
// #endif
241-
// #endif
242-
243-
// #if !defined(TEENSY_INIT_USB_DELAY_AFTER)
244-
// #if TEENSYDUINO >= 142
245-
// #define TEENSY_INIT_USB_DELAY_AFTER 275
246-
// #else
247-
// #define TEENSY_INIT_USB_DELAY_AFTER 350
248-
// #endif
249-
// #endif
250-
251-
// // for background about this startup delay, please see these conversations
252-
// // https://forum.pjrc.com/threads/36606-startup-time-(400ms)?p=113980&viewfull=1#post113980
253-
// // https://forum.pjrc.com/threads/31290-Teensey-3-2-Teensey-Loader-1-24-Issues?p=87273&viewfull=1#post87273
254-
255-
// delay(TEENSY_INIT_USB_DELAY_BEFORE);
256-
// usb_init();
257-
// delay(TEENSY_INIT_USB_DELAY_AFTER);
258234
}
259235

260236
func postinit() {}
261237

262238
func putchar(c byte) {
263-
u := &machine.UART0
264-
265-
// ensure the UART has been configured
266-
if !u.SCGC.HasBits(u.SCGCMask) {
267-
u.Configure(machine.UARTConfig{})
268-
}
269-
270-
for u.TCFIFO.Get() > 0 {
271-
// busy wait
272-
}
273-
u.D.Set(c)
239+
machine.PutcharUART(&machine.UART0, c)
274240
}
275241

276242
// ???
277243
const asyncScheduler = false
278244

279-
// convert from ticks (us) to time.Duration (ns)
280-
const tickMicros = 1000
281-
282-
var cyclesPerMilli = machine.CPUFrequency() / 1000
283-
284245
// cyclesPerMilli-1 is used for the systick reset value
285246
// the systick current value will be decremented on every clock cycle
286247
// an interrupt is generated when the current value reaches 0
287248
// a value of freq/1000 generates a tick (irq) every millisecond (1/1000 s)
249+
var cyclesPerMilli = machine.CPUFrequency() / 1000
288250

289-
// number of systick irqs (milliseconds) since boot
290-
var systickCount volatile.Register32
251+
type timeUnit int64
291252

292-
//go:export SysTick_Handler
293-
func tick() {
294-
systickCount.Set(systickCount.Get() + 1)
253+
func ticksToNanoseconds(ticks timeUnit) int64 {
254+
return int64(ticks) * 1000
295255
}
296256

297-
type timeUnit int64
257+
func nanosecondsToTicks(ns int64) timeUnit {
258+
return timeUnit(ns / 1000)
259+
}
298260

299261
// ticks are in microseconds
300262
func ticks() timeUnit {
301-
m := arm.DisableInterrupts()
263+
mask := arm.DisableInterrupts()
302264
current := nxp.SysTick.CVR.Get() // current value of the systick counter
303-
count := systickCount.Get() // number of milliseconds since boot
265+
count := machine.MillisSinceBoot() // number of milliseconds since boot
304266
istatus := nxp.SystemControl.ICSR.Get() // interrupt status register
305-
arm.EnableInterrupts(m)
267+
arm.EnableInterrupts(mask)
306268

307269
micros := count * 1000 // a tick (1ms) = 1000 us
308270

@@ -361,28 +323,21 @@ func abort() {
361323

362324
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
363325

326+
var v bool
364327
for {
365-
start := systickCount.Get()
328+
machine.LED.Set(v)
329+
v = !v
366330

367-
machine.LED.Set(true)
368-
for systickCount.Get()-start < 60 {
331+
t := machine.MillisSinceBoot()
332+
for machine.MillisSinceBoot()-t < 60 {
369333
arm.Asm("wfi")
370334
}
371335

372-
machine.LED.Set(false)
373-
for systickCount.Get()-start < 120 {
374-
arm.Asm("wfi")
375-
}
336+
// keep polling some communication while in fault
337+
// mode, so we don't completely die.
338+
// machine.PollUSB(&machine.USB0)
339+
machine.PollUART(&machine.UART0)
340+
machine.PollUART(&machine.UART1)
341+
machine.PollUART(&machine.UART2)
376342
}
377-
378-
// teensy core services ISRs in this loop:
379-
380-
// for {
381-
// // keep polling some communication while in fault
382-
// // mode, so we don't completely die.
383-
// if nxp.SIM.SCGC4.HasBits(nxp.SIM_SCGC4_USBOTG) usb_isr();
384-
// if nxp.SIM.SCGC4.HasBits(nxp.SIM_SCGC4_UART0) uart0_status_isr();
385-
// if nxp.SIM.SCGC4.HasBits(nxp.SIM_SCGC4_UART1) uart1_status_isr();
386-
// if nxp.SIM.SCGC4.HasBits(nxp.SIM_SCGC4_UART2) uart2_status_isr();
387-
// }
388343
}

0 commit comments

Comments
 (0)