@@ -36,7 +36,6 @@ import (
3636 "device/nxp"
3737 "machine"
3838 "runtime/interrupt"
39- "runtime/volatile"
4039)
4140
4241const (
@@ -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
260236func postinit () {}
261237
262238func 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// ???
277243const 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
300262func 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