github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/runtime_rp2040.go (about)

     1  //go:build rp2040
     2  
     3  package runtime
     4  
     5  import (
     6  	"device/arm"
     7  	"machine"
     8  	"machine/usb/cdc"
     9  )
    10  
    11  // machineTicks is provided by package machine.
    12  func machineTicks() uint64
    13  
    14  // machineLightSleep is provided by package machine.
    15  func machineLightSleep(uint64)
    16  
    17  type timeUnit uint64
    18  
    19  // ticks returns the number of ticks (microseconds) elapsed since power up.
    20  func ticks() timeUnit {
    21  	t := machineTicks()
    22  	return timeUnit(t)
    23  }
    24  
    25  func ticksToNanoseconds(ticks timeUnit) int64 {
    26  	return int64(ticks) * 1000
    27  }
    28  
    29  func nanosecondsToTicks(ns int64) timeUnit {
    30  	return timeUnit(ns / 1000)
    31  }
    32  
    33  func sleepTicks(d timeUnit) {
    34  	if d == 0 {
    35  		return
    36  	}
    37  
    38  	if hasScheduler {
    39  		// With scheduler, sleepTicks may return early if an interrupt or
    40  		// event fires - so scheduler can schedule any go routines now
    41  		// eligible to run
    42  		machineLightSleep(uint64(d))
    43  		return
    44  	}
    45  
    46  	// Busy loop
    47  	sleepUntil := ticks() + d
    48  	for ticks() < sleepUntil {
    49  	}
    50  }
    51  
    52  func waitForEvents() {
    53  	arm.Asm("wfe")
    54  }
    55  
    56  func putchar(c byte) {
    57  	machine.Serial.WriteByte(c)
    58  }
    59  
    60  func getchar() byte {
    61  	for machine.Serial.Buffered() == 0 {
    62  		Gosched()
    63  	}
    64  	v, _ := machine.Serial.ReadByte()
    65  	return v
    66  }
    67  
    68  func buffered() int {
    69  	return machine.Serial.Buffered()
    70  }
    71  
    72  // machineInit is provided by package machine.
    73  func machineInit()
    74  
    75  func init() {
    76  	machineInit()
    77  
    78  	cdc.EnableUSBCDC()
    79  	machine.USBDev.Configure(machine.UARTConfig{})
    80  	machine.InitSerial()
    81  }
    82  
    83  //export Reset_Handler
    84  func main() {
    85  	preinit()
    86  	run()
    87  	exit(0)
    88  }