|
| 1 | +// +build esp8266 |
| 2 | + |
| 3 | +package runtime |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/esp" |
| 7 | + "machine" |
| 8 | + "unsafe" |
| 9 | +) |
| 10 | + |
| 11 | +type timeUnit int64 |
| 12 | + |
| 13 | +const tickMicros = 3200 // time.Second / (80MHz / 256) |
| 14 | + |
| 15 | +var currentTime timeUnit = 0 |
| 16 | + |
| 17 | +func putchar(c byte) { |
| 18 | + machine.UART0.WriteByte(c) |
| 19 | +} |
| 20 | + |
| 21 | +// Write to the internal control bus (using I2C?). |
| 22 | +// Signature found here: |
| 23 | +// https://github.com/espressif/esp-idf/blob/64654c04447914610586e1ac44457510a5bf7191/components/soc/esp32/i2c_rtc_clk.h#L32 |
| 24 | +//export rom_i2c_writeReg |
| 25 | +func rom_i2c_writeReg(block, host_id, reg_add, data uint8) |
| 26 | + |
| 27 | +//export main |
| 28 | +func main() { |
| 29 | + // Clear .bss section. .data has already been loaded by the ROM bootloader. |
| 30 | + preinit() |
| 31 | + |
| 32 | + // Initialize PLL. |
| 33 | + // I'm not quite sure what this magic incantation means, but it does set the |
| 34 | + // esp8266 to the right clock speed. Without this, it is running too slow. |
| 35 | + rom_i2c_writeReg(103, 4, 1, 136) |
| 36 | + rom_i2c_writeReg(103, 4, 2, 145) |
| 37 | + |
| 38 | + // Initialize UART. |
| 39 | + machine.UART0.Configure(machine.UARTConfig{}) |
| 40 | + |
| 41 | + // Initialize timer. Bits: |
| 42 | + // 7: timer enable |
| 43 | + // 6: automatically reload when hitting 0 |
| 44 | + // 3-2: divide by 256 |
| 45 | + esp.FRC1.CTRL.Set(1<<7 | 1<<6 | 2<<2) |
| 46 | + esp.FRC1.LOAD.Set(0x3fffff) // set all 22 bits to 1 |
| 47 | + esp.FRC1.COUNT.Set(0x3fffff) // set all 22 bits to 1 |
| 48 | + |
| 49 | + // Run init functions in all packages. |
| 50 | + initAll() |
| 51 | + |
| 52 | + // Call main.main. |
| 53 | + callMain() |
| 54 | + |
| 55 | + // Fallback: if main ever returns, hang the CPU. |
| 56 | + abort() |
| 57 | +} |
| 58 | + |
| 59 | +//go:extern _sbss |
| 60 | +var _sbss [0]byte |
| 61 | + |
| 62 | +//go:extern _ebss |
| 63 | +var _ebss [0]byte |
| 64 | + |
| 65 | +func preinit() { |
| 66 | + // Initialize .bss: zero-initialized global variables. |
| 67 | + ptr := unsafe.Pointer(&_sbss) |
| 68 | + for ptr != unsafe.Pointer(&_ebss) { |
| 69 | + *(*uint32)(ptr) = 0 |
| 70 | + ptr = unsafe.Pointer(uintptr(ptr) + 4) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func ticks() timeUnit { |
| 75 | + // Get the counter value of the timer. It is 22 bits and starts with all |
| 76 | + // ones (0x3fffff). To make it easier to work with, let it count upwards. |
| 77 | + count := 0x3fffff - esp.FRC1.COUNT.Get() |
| 78 | + |
| 79 | + // Replace the lowest 22 bits of the current time with the counter. |
| 80 | + newTime := (currentTime &^ 0x3fffff) | timeUnit(count) |
| 81 | + |
| 82 | + // If there was an overflow, the new time will be lower than the current |
| 83 | + // time, so will need to add (1<<22). |
| 84 | + if newTime < currentTime { |
| 85 | + newTime += 0x400000 |
| 86 | + } |
| 87 | + |
| 88 | + // Update the timestamp for the next call to ticks(). |
| 89 | + currentTime = newTime |
| 90 | + |
| 91 | + return currentTime |
| 92 | +} |
| 93 | + |
| 94 | +const asyncScheduler = false |
| 95 | + |
| 96 | +// sleepTicks busy-waits until the given number of ticks have passed. |
| 97 | +func sleepTicks(d timeUnit) { |
| 98 | + sleepUntil := ticks() + d |
| 99 | + for ticks() < sleepUntil { |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func abort() |
| 104 | + |
| 105 | +// Implement memset for LLVM and compiler-rt. |
| 106 | +//go:export memset |
| 107 | +func libc_memset(ptr unsafe.Pointer, c byte, size uintptr) { |
| 108 | + for i := uintptr(0); i < size; i++ { |
| 109 | + *(*byte)(unsafe.Pointer(uintptr(ptr) + i)) = c |
| 110 | + } |
| 111 | +} |
0 commit comments