github.com/aykevl/tinygo@v0.5.0/src/runtime/runtime_avr.go (about)

     1  // +build avr
     2  
     3  package runtime
     4  
     5  import (
     6  	"device/avr"
     7  	"machine"
     8  	"unsafe"
     9  )
    10  
    11  const BOARD = "arduino"
    12  
    13  type timeUnit uint32
    14  
    15  var currentTime timeUnit
    16  
    17  const tickMicros = 1024 * 16384
    18  
    19  // Watchdog timer periods. These can be off by a large margin (hence the jump
    20  // between 64ms and 125ms which is not an exact double), so don't rely on this
    21  // for accurate time keeping.
    22  const (
    23  	WDT_PERIOD_16MS = iota
    24  	WDT_PERIOD_32MS
    25  	WDT_PERIOD_64MS
    26  	WDT_PERIOD_125MS
    27  	WDT_PERIOD_250MS
    28  	WDT_PERIOD_500MS
    29  	WDT_PERIOD_1S
    30  	WDT_PERIOD_2S
    31  )
    32  
    33  //go:extern _sbss
    34  var _sbss unsafe.Pointer
    35  
    36  //go:extern _ebss
    37  var _ebss unsafe.Pointer
    38  
    39  //go:export main
    40  func main() {
    41  	preinit()
    42  	initAll()
    43  	postinit()
    44  	callMain()
    45  	abort()
    46  }
    47  
    48  func preinit() {
    49  	// Initialize .bss: zero-initialized global variables.
    50  	ptr := uintptr(unsafe.Pointer(&_sbss))
    51  	for ptr != uintptr(unsafe.Pointer(&_ebss)) {
    52  		*(*uint8)(unsafe.Pointer(ptr)) = 0
    53  		ptr += 1
    54  	}
    55  }
    56  
    57  func postinit() {
    58  	// Enable interrupts after initialization.
    59  	avr.Asm("sei")
    60  }
    61  
    62  func init() {
    63  	initUART()
    64  }
    65  
    66  func initUART() {
    67  	machine.UART0.Configure(machine.UARTConfig{})
    68  }
    69  
    70  func putchar(c byte) {
    71  	machine.UART0.WriteByte(c)
    72  }
    73  
    74  const asyncScheduler = false
    75  
    76  // Sleep this number of ticks of 16ms.
    77  //
    78  // TODO: not very accurate. Improve accuracy by calibrating on startup and every
    79  // once in a while.
    80  func sleepTicks(d timeUnit) {
    81  	currentTime += d
    82  	for d != 0 {
    83  		sleepWDT(WDT_PERIOD_16MS)
    84  		d -= 1
    85  	}
    86  }
    87  
    88  func ticks() timeUnit {
    89  	return currentTime
    90  }
    91  
    92  func abort() {
    93  	for {
    94  		sleepWDT(WDT_PERIOD_2S)
    95  	}
    96  }