mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 20:13:40 +00:00
esp: add support for the Espressif ESP32 chip
This is only very minimal support. More support (such as tinygo flash, or peripheral access) should be added in later commits, to keep this one focused. Importantly, this commit changes the LLVM repo from llvm/llvm-project to tinygo-org/llvm-project. This provides a little bit of versioning in case something changes in the Espressif fork. If we want to upgrade to LLVM 11 it's easy to switch back to llvm/llvm-project until Espressif has updated their fork.
This commit is contained in:
committed by
Ron Evans
parent
da7db81087
commit
3ee47a9c1b
@@ -0,0 +1,52 @@
|
||||
|
||||
// The following definitions were copied from:
|
||||
// esp-idf/components/xtensa/include/xtensa/corebits.h
|
||||
#define PS_WOE_MASK 0x00040000
|
||||
#define PS_OWB_MASK 0x00000F00
|
||||
#define PS_CALLINC_MASK 0x00030000
|
||||
#define PS_WOE PS_WOE_MASK
|
||||
|
||||
// Only calling it call_start_cpu0 for consistency with ESP-IDF.
|
||||
.section .text.call_start_cpu0
|
||||
1:
|
||||
.long _stack_top
|
||||
.global call_start_cpu0
|
||||
call_start_cpu0:
|
||||
// We need to set the stack pointer to a different value. This is somewhat
|
||||
// complicated in the Xtensa architecture. The code below is a modified
|
||||
// version of the following code:
|
||||
// https://github.com/espressif/esp-idf/blob/c77c4ccf/components/xtensa/include/xt_instr_macros.h#L47
|
||||
|
||||
// Disable WOE.
|
||||
rsr.ps a2
|
||||
movi a3, ~(PS_WOE_MASK)
|
||||
and a2, a2, a3
|
||||
wsr.ps a2
|
||||
rsync
|
||||
|
||||
// Set WINDOWBASE to 1 << WINDOWSTART.
|
||||
rsr.windowbase a2
|
||||
ssl a2
|
||||
movi a2, 1
|
||||
sll a2, a2
|
||||
wsr.windowstart a2
|
||||
rsync
|
||||
|
||||
// Load new stack pointer.
|
||||
l32r sp, 1b
|
||||
|
||||
// Re-enable WOE.
|
||||
rsr.ps a2
|
||||
movi a3, PS_WOE
|
||||
or a2, a2, a3
|
||||
wsr.ps a2
|
||||
rsync
|
||||
|
||||
// Jump to the runtime start function written in Go.
|
||||
j main
|
||||
|
||||
.section .text.tinygo_scanCurrentStack
|
||||
.global tinygo_scanCurrentStack
|
||||
tinygo_scanCurrentStack:
|
||||
// TODO: save callee saved registers on the stack
|
||||
j tinygo_scanstack
|
||||
@@ -0,0 +1,44 @@
|
||||
// +build esp32
|
||||
|
||||
package machine
|
||||
|
||||
import "device/esp"
|
||||
|
||||
const peripheralClock = 80000000 // 80MHz
|
||||
|
||||
type PinMode uint8
|
||||
|
||||
const (
|
||||
PinOutput PinMode = iota
|
||||
PinInput
|
||||
)
|
||||
|
||||
func (p Pin) Set(value bool)
|
||||
|
||||
var (
|
||||
UART0 = UART{Bus: esp.UART0, Buffer: NewRingBuffer()}
|
||||
UART1 = UART{Bus: esp.UART1, Buffer: NewRingBuffer()}
|
||||
UART2 = UART{Bus: esp.UART2, Buffer: NewRingBuffer()}
|
||||
)
|
||||
|
||||
type UART struct {
|
||||
Bus *esp.UART_Type
|
||||
Buffer *RingBuffer
|
||||
}
|
||||
|
||||
func (uart UART) Configure(config UARTConfig) {
|
||||
if config.BaudRate == 0 {
|
||||
config.BaudRate = 115200
|
||||
}
|
||||
uart.Bus.CLKDIV.Set(peripheralClock / config.BaudRate)
|
||||
}
|
||||
|
||||
func (uart UART) WriteByte(b byte) error {
|
||||
for (uart.Bus.STATUS.Get()>>16)&0xff >= 128 {
|
||||
// Read UART_TXFIFO_CNT from the status register, which indicates how
|
||||
// many bytes there are in the transmit buffer. Wait until there are
|
||||
// less than 128 bytes in this buffer (the default buffer size).
|
||||
}
|
||||
uart.Bus.TX_FIFO.Set(b)
|
||||
return nil
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// +build avr nrf sam sifive stm32 k210 nxp
|
||||
// +build avr esp nrf sam sifive stm32 k210 nxp
|
||||
|
||||
package machine
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// +build xtensa
|
||||
|
||||
package runtime
|
||||
|
||||
const GOARCH = "arm" // xtensa pretends to be arm
|
||||
|
||||
// The bitness of the CPU (e.g. 8, 32, 64).
|
||||
const TargetBits = 32
|
||||
|
||||
// Align on a word boundary.
|
||||
func align(ptr uintptr) uintptr {
|
||||
return (ptr + 3) &^ 3
|
||||
}
|
||||
|
||||
func getCurrentStackPointer() uintptr
|
||||
@@ -0,0 +1,31 @@
|
||||
// +build xtensa
|
||||
|
||||
package interrupt
|
||||
|
||||
import "device"
|
||||
|
||||
// State represents the previous global interrupt state.
|
||||
type State uintptr
|
||||
|
||||
// Disable disables all interrupts and returns the previous interrupt state. It
|
||||
// can be used in a critical section like this:
|
||||
//
|
||||
// state := interrupt.Disable()
|
||||
// // critical section
|
||||
// interrupt.Restore(state)
|
||||
//
|
||||
// Critical sections can be nested. Make sure to call Restore in the same order
|
||||
// as you called Disable (this happens naturally with the pattern above).
|
||||
func Disable() (state State) {
|
||||
return State(device.AsmFull("rsil {}, 15", nil))
|
||||
}
|
||||
|
||||
// Restore restores interrupts to what they were before. Give the previous state
|
||||
// returned by Disable as a parameter. If interrupts were disabled before
|
||||
// calling Disable, this will not re-enable interrupts, allowing for nested
|
||||
// cricital sections.
|
||||
func Restore(state State) {
|
||||
device.AsmFull("wsr {state}, PS", map[string]interface{}{
|
||||
"state": state,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
// +build esp32
|
||||
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"device"
|
||||
"device/esp"
|
||||
"machine"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type timeUnit int64
|
||||
|
||||
var currentTime timeUnit
|
||||
|
||||
func putchar(c byte) {
|
||||
machine.UART0.WriteByte(c)
|
||||
}
|
||||
|
||||
func postinit() {}
|
||||
|
||||
// This is the function called on startup right after the stack pointer has been
|
||||
// set.
|
||||
//export main
|
||||
func main() {
|
||||
// Disable both watchdog timers that are enabled by default on startup.
|
||||
// Note that these watchdogs can be protected, but the ROM bootloader
|
||||
// doesn't seem to protect them.
|
||||
esp.RTCCNTL.WDTCONFIG0.Set(0)
|
||||
esp.TIMG0.WDTCONFIG0.Set(0)
|
||||
|
||||
// Switch SoC clock source to PLL (instead of the default which is XTAL).
|
||||
// This switches the CPU (and APB) clock from 40MHz to 80MHz.
|
||||
// Options:
|
||||
// RTCCNTL_CLK_CONF_SOC_CLK_SEL: PLL (default XTAL)
|
||||
// RTCCNTL_CLK_CONF_CK8M_DIV_SEL: 2 (default)
|
||||
// RTCCNTL_CLK_CONF_DIG_CLK8M_D256_EN: Enable (default)
|
||||
// RTCCNTL_CLK_CONF_CK8M_DIV: DIV256 (default)
|
||||
// The only real change made here is modifying RTCCNTL_CLK_CONF_SOC_CLK_SEL,
|
||||
// but setting a fixed value produces smaller code.
|
||||
esp.RTCCNTL.CLK_CONF.Set((esp.RTCCNTL_CLK_CONF_SOC_CLK_SEL_PLL << esp.RTCCNTL_CLK_CONF_SOC_CLK_SEL_Pos) |
|
||||
(2 << esp.RTCCNTL_CLK_CONF_CK8M_DIV_SEL_Pos) |
|
||||
(esp.RTCCNTL_CLK_CONF_DIG_CLK8M_D256_EN_Enable << esp.RTCCNTL_CLK_CONF_DIG_CLK8M_D256_EN_Pos) |
|
||||
(esp.RTCCNTL_CLK_CONF_CK8M_DIV_DIV256 << esp.RTCCNTL_CLK_CONF_CK8M_DIV_Pos))
|
||||
|
||||
// Switch CPU from 80MHz to 160MHz. This doesn't affect the APB clock,
|
||||
// which is still running at 80MHz.
|
||||
esp.DPORT.CPU_PER_CONF.Set(esp.DPORT_CPU_PER_CONF_CPUPERIOD_SEL_SEL_160)
|
||||
|
||||
// Clear .bss section. .data has already been loaded by the ROM bootloader.
|
||||
// Do this after increasing the CPU clock to possibly make startup slightly
|
||||
// faster.
|
||||
preinit()
|
||||
|
||||
// Initialize UART.
|
||||
machine.UART0.Configure(machine.UARTConfig{})
|
||||
|
||||
// Configure timer 0 in timer group 0, for timekeeping.
|
||||
// EN: Enable the timer.
|
||||
// INCREASE: Count up every tick (as opposed to counting down).
|
||||
// DIVIDER: 16-bit prescaler, set to 2 for dividing the APB clock by two
|
||||
// (40MHz).
|
||||
esp.TIMG0.T0CONFIG.Set(esp.TIMG_T0CONFIG_T0_EN | esp.TIMG_T0CONFIG_T0_INCREASE | 2<<esp.TIMG_T0CONFIG_T0_DIVIDER_Pos)
|
||||
|
||||
// Set the timer counter value to 0.
|
||||
esp.TIMG0.T0LOADLO.Set(0)
|
||||
esp.TIMG0.T0LOADHI.Set(0)
|
||||
esp.TIMG0.T0LOAD.Set(0) // value doesn't matter.
|
||||
|
||||
run()
|
||||
|
||||
// Fallback: if main ever returns, hang the CPU.
|
||||
abort()
|
||||
}
|
||||
|
||||
//go:extern _sbss
|
||||
var _sbss [0]byte
|
||||
|
||||
//go:extern _ebss
|
||||
var _ebss [0]byte
|
||||
|
||||
func preinit() {
|
||||
// Initialize .bss: zero-initialized global variables.
|
||||
// The .data section has already been loaded by the ROM bootloader.
|
||||
ptr := unsafe.Pointer(&_sbss)
|
||||
for ptr != unsafe.Pointer(&_ebss) {
|
||||
*(*uint32)(ptr) = 0
|
||||
ptr = unsafe.Pointer(uintptr(ptr) + 4)
|
||||
}
|
||||
}
|
||||
|
||||
func ticks() timeUnit {
|
||||
// First, update the LO and HI register pair by writing any value to the
|
||||
// register. This allows reading the pair atomically.
|
||||
esp.TIMG0.T0UPDATE.Set(0)
|
||||
// Then read the two 32-bit parts of the timer.
|
||||
return timeUnit(uint64(esp.TIMG0.T0LO.Get()) | uint64(esp.TIMG0.T0HI.Get())<<32)
|
||||
}
|
||||
|
||||
const asyncScheduler = false
|
||||
|
||||
func nanosecondsToTicks(ns int64) timeUnit {
|
||||
// Calculate the number of ticks from the number of nanoseconds. At a 80MHz
|
||||
// APB clock, that's 25 nanoseconds per tick with a timer prescaler of 2:
|
||||
// 25 = 1e9 / (80MHz / 2)
|
||||
return timeUnit(ns / 25)
|
||||
}
|
||||
|
||||
func ticksToNanoseconds(ticks timeUnit) int64 {
|
||||
// See nanosecondsToTicks.
|
||||
return int64(ticks) * 25
|
||||
}
|
||||
|
||||
// sleepTicks busy-waits until the given number of ticks have passed.
|
||||
func sleepTicks(d timeUnit) {
|
||||
sleepUntil := ticks() + d
|
||||
for ticks() < sleepUntil {
|
||||
// TODO: suspend the CPU to not burn power here unnecessarily.
|
||||
}
|
||||
}
|
||||
|
||||
func abort() {
|
||||
for {
|
||||
device.Asm("waiti 0")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user