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

     1  //go:build esp32
     2  
     3  package runtime
     4  
     5  import (
     6  	"device"
     7  	"device/esp"
     8  	"machine"
     9  )
    10  
    11  // This is the function called on startup right after the stack pointer has been
    12  // set.
    13  //
    14  //export main
    15  func main() {
    16  	// Disable the protection on the watchdog timer (needed when started from
    17  	// the bootloader).
    18  	esp.RTC_CNTL.WDTWPROTECT.Set(0x050D83AA1)
    19  
    20  	// Disable both watchdog timers that are enabled by default on startup.
    21  	// Note that these watchdogs can be protected, but the ROM bootloader
    22  	// doesn't seem to protect them.
    23  	esp.RTC_CNTL.WDTCONFIG0.Set(0)
    24  	esp.TIMG0.WDTCONFIG0.Set(0)
    25  
    26  	// Switch SoC clock source to PLL (instead of the default which is XTAL).
    27  	// This switches the CPU (and APB) clock from 40MHz to 80MHz.
    28  	// Options:
    29  	//   RTC_CNTL_CLK_CONF_SOC_CLK_SEL:       PLL (1)       (default XTAL)
    30  	//   RTC_CNTL_CLK_CONF_CK8M_DIV_SEL:      2             (default)
    31  	//   RTC_CNTL_CLK_CONF_DIG_CLK8M_D256_EN: Enable        (default)
    32  	//   RTC_CNTL_CLK_CONF_CK8M_DIV:          divide by 256 (default)
    33  	// The only real change made here is modifying RTC_CNTL_CLK_CONF_SOC_CLK_SEL,
    34  	// but setting a fixed value produces smaller code.
    35  	esp.RTC_CNTL.CLK_CONF.Set((1 << esp.RTC_CNTL_CLK_CONF_SOC_CLK_SEL_Pos) |
    36  		(2 << esp.RTC_CNTL_CLK_CONF_CK8M_DIV_SEL_Pos) |
    37  		(1 << esp.RTC_CNTL_CLK_CONF_DIG_CLK8M_D256_EN_Pos) |
    38  		(1 << esp.RTC_CNTL_CLK_CONF_CK8M_DIV_Pos))
    39  
    40  	// Switch CPU from 80MHz to 160MHz. This doesn't affect the APB clock,
    41  	// which is still running at 80MHz.
    42  	esp.DPORT.CPU_PER_CONF.Set(1) // PLL_CLK / 2, see table 3-3 in the reference manual
    43  
    44  	// Clear .bss section. .data has already been loaded by the ROM bootloader.
    45  	// Do this after increasing the CPU clock to possibly make startup slightly
    46  	// faster.
    47  	clearbss()
    48  
    49  	// Initialize UART.
    50  	machine.InitSerial()
    51  
    52  	// Initialize main system timer used for time.Now.
    53  	initTimer()
    54  
    55  	// Initialize the heap, call main.main, etc.
    56  	run()
    57  
    58  	// Fallback: if main ever returns, hang the CPU.
    59  	exit(0)
    60  }
    61  
    62  //go:extern _sbss
    63  var _sbss [0]byte
    64  
    65  //go:extern _ebss
    66  var _ebss [0]byte
    67  
    68  func abort() {
    69  	for {
    70  		device.Asm("waiti 0")
    71  	}
    72  }