//go:build esp8266 // +build esp8266 package runtime import ( "device" "device/esp" "machine" "unsafe" ) type timeUnit int64 var currentTime timeUnit = 0 func putchar(c byte) { machine.Serial.WriteByte(c) } func getchar() byte { for machine.Serial.Buffered() == 0 { Gosched() } v, _ := machine.Serial.ReadByte() return v } func buffered() int { return machine.Serial.Buffered() } // Write to the internal control bus (using I2C?). // Signature found here: // https://github.com/espressif/ESP8266_RTOS_SDK/blob/14171de0/components/esp8266/include/esp8266/rom_functions.h#L54 // //export rom_i2c_writeReg func rom_i2c_writeReg(block, host_id, reg_add, data uint8) //export main func main() { // Clear .bss section. .data has already been loaded by the ROM bootloader. preinit() // Initialize PLL. // I'm not quite sure what this magic incantation means, but it does set the // esp8266 to the right clock speed. Without this, it is running too slow. rom_i2c_writeReg(103, 4, 1, 136) rom_i2c_writeReg(103, 4, 2, 145) // Initialize UART. machine.InitSerial() // Initialize timer. Bits: // ENABLE: timer enable // ROLLOVER: automatically reload when hitting 0 // PRESCALE: divide by 256 esp.TIMER.FRC1_CTRL.Set( esp.TIMER_FRC1_CTRL_TIMER_ENABLE | esp.TIMER_FRC1_CTRL_ROLLOVER | esp.TIMER_FRC1_CTRL_PRESCALE_DIVIDER_DEVIDED_BY_256<