Hardware API¶
import mcu; brings in lib/mcu.goose. Every function there is an
extern fn bound to a C function in the target's HAL:
components/microgoose/mg_hal.c on the ESP32s, ports/stm32h7/src/mg_h7.c
on the H7 and ports/rp2350/src/mg_hal.c on the Pico 2 W. The names are
the same on every board. A function a board can't support returns a
harmless value instead of failing to build.
MicroGoose 0.1 called the module esp. import esp; still works and brings
in the same functions, but the build prints a note to switch to import mcu;.
Time¶
| Function | Returns | Notes |
|---|---|---|
delay_ms(ms: i32) |
— | sleeps for at least ms; other tasks run meanwhile |
millis() |
i64 |
milliseconds since boot |
micros() |
i64 |
microseconds since boot |
yield_now() |
— | lets other tasks run |
GPIO¶
| Function | Notes |
|---|---|
pin_output(pin: i32) |
|
pin_input(pin: i32, pull: i32) |
PULL_NONE, PULL_UP, PULL_DOWN |
pin_write(pin: i32, level: i32) |
0 or 1 |
pin_read(pin: i32) -> i32 |
0 or 1 |
Pin numbers are the chip's GPIO numbers on the ESP32s and the Pico 2 W. On
the STM32 they are port × 16 + n (PA0 = 0, PB0 = 16, …).
On the Pico 2 W, the pins are the ones on its header: GPIO0–22 and 26–28.
GPIO23–25 and 29 belong to the WiFi chip, so calls on them do nothing and
pin_read returns −1. An RP2350 erratum (E9) can hold a pin with only the
internal pull-down near 2 V after something drove it high and let go, so it
reads 1. pin_input(pin, PULL_DOWN) brings the pin low when it's called,
but a pin something drives and then releases needs a pull-down resistor of
8.2 kΩ or less on the board, as the datasheet advises.
LED strips (WS2812, NeoPixel)¶
| Function | Notes |
|---|---|
leds_open(pin: i32, count: i32) -> i32 |
a strip of count LEDs, all off until the first leds_show; returns the strip, or −1. A strip already on the pin closes first |
leds_set(strip: i32, i: i32, r: i32, g: i32, b: i32) -> bool |
LED i's colour, 0–255 each, sent at the next leds_show |
leds_show(strip: i32) -> bool |
sends every LED's colour; returns when the last bit is out |
leds_write(strip: i32, data: const u8[:]) -> bool |
sends data as it is: for another colour order, or a white byte |
leds_timing(strip: i32, t0h_ns: i32, t1h_ns: i32, bit_ns: i32, reset_us: i32) -> bool |
the bit timing, for chips other than the WS2812 (below) |
leds_close(strip: i32) |
the pin becomes an output again, low |
rgb_init(pin: i32) |
one WS2812, such as the ESP32-S3 DevKitC-1's own (GPIO48 on v1.0, GPIO38 on v1.1): a strip of one LED |
rgb(r: i32, g: i32, b: i32) |
its colour, 0–255 each |
let strip = leds_open(5, 30); // 30 LEDs, data in on GPIO5
for i: i32 in 30 {
leds_set(strip, i, 8 * i, 0, 255 - 8 * i); // red to blue
}
leds_show(strip);
A WS2812 takes 24 bits per LED, green, red, blue, most significant bit
first. Each bit is 1.25 µs: high for 350 ns for a 0, 700 ns for a 1, then
low. A low of 300 µs, the reset time, makes the strip show what it took.
leds_show returns when the last bit is out, and the next leds_show waits
out the reset time first, so an animation can call it as fast as it likes.
A frame takes 30 µs per LED: 3 ms for 100.
Other chips:
- A strip in RGB order (some WS2811s): swap
randginleds_set. - SK6812 RGBW: four bytes per LED, green, red, blue, white, through
leds_write. - WS2811 at 400 kHz:
leds_timing(strip, 500, 1200, 2500, 300).
The default timing, 350, 700, 1250 and 300, suits the WS2812B, the WS2813
and the SK6812. leds_timing returns false unless t0h, t1h − t0h and
bit − t1h are each 100 ns or more, the bit is 1 ms at most, the reset
time 1 s at most, and the board comes within 10% of each time.
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Pins | any output GPIO | any output GPIO | the PWM pins on TIM1–TIM4 (see PWM) | any header GPIO |
| Strips at once | 4 | 4 | 4, one per timer | 4 |
| Sent by | RMT, the first strip with DMA | RMT | the pin's timer, fed by DMA | PIO, fed by DMA |
| Timing steps | 50 ns | 50 ns | 5 ns | the bit's length / 3 to 48 |
On the H7 the strip has its timer to itself: pwm_start on the timer's
other pins returns −1, and leds_open returns −1 on a timer that PWM is
using. rgb_init's LED is one of the four strips.
A WS2812 draws up to 60 mA at full white, so a long strip needs its own 5 V supply, with its ground joined to the board's. Its data input expects 0.7 × 5 V for a high: the 3.3 V of these boards usually works with a short wire to the first LED, and a level shifter such as a 74AHCT125 makes sure.
tests/stress/leds_frames.goose
checks all of it with nothing wired. At a slow timing it reads each frame
back from the data pin's own edges, bit for bit. At the WS2812 timing,
which is too fast to watch, it checks that 200 more LEDs take 6000 µs more.
ADC¶
| Function | Returns |
|---|---|
adc_read_mv(pin: i32) |
the pin's voltage in mV |
adc_read_raw(pin: i32) |
the reading, 0 to 2^adc_bits() − 1 |
adc_to_mv(pin: i32, raw: i32) |
a raw reading of that pin in mV |
adc_average_mv(pin: i32, n: i32) |
the mean of n readings in mV, steadier than one |
adc_bits() |
12 on the ESP32s and the Pico 2 W, 16 on the H7 |
adc_range(pin: i32, max_mv: i32) |
sets the narrowest range that reaches max_mv; returns its top in mV |
adc_capture(pin: i32, rate_hz: i32, out: u16[:]) |
fills out with raw readings rate_hz apart; returns the rate it kept, in Hz |
Every call returns −1 on a pin without an ADC.
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Pins | GPIO1–10 (ADC1), 11–20 (ADC2) | GPIO32–39 (ADC1); 0, 2, 4, 12–15, 25–27 (ADC2) | PA0–PA7, PB0, PB1, PC0, PC1, PC4, PC5 (pins 0–7, 16, 17, 32, 33, 36, 37) | GPIO26–28 |
Ranges (adc_range) |
950, 1250, 1750, 3100 mV | 950, 1250, 1750, 2450 mV | 0 to VDDA, measured | 0 to 3300 mV |
| mV from | the chip's eFuse calibration | the chip's eFuse calibration | VDDA, measured against the factory-calibrated VREFINT | the 3.3 V supply, taken as exact |
| Fastest capture | about 21 kHz | about 21 kHz | about 230 kHz | 500 kHz |
Each pin starts at full range. The classic ESP32 reads nothing below about 130 mV: lower voltages read as raw 0. Narrowing the range reads small voltages more finely: a 1 V signal on the 1250 mV range uses most of the 12 bits.
On the classic ESP32, ADC2 belongs to WiFi while WiFi runs: reads on ADC2 pins return −1 in a networked program. Use the ADC1 pins (GPIO32–39) there.
adc_capture paces its readings with the microsecond clock, polling the
ADC, so it holds the program until out is full. If the ADC can't keep up,
the readings come back to back and the call returns the rate it reached.
On the Pico 2 W the ADC paces itself and DMA collects the readings, so the
rate holds while other tasks run; below 733 Hz the CPU times them instead.
adc_to_mv turns them into millivolts:
var wave: u16[>..] = [];
for i in 500 { wave.push(0); }
let hz = adc_capture(34, 10000, wave); // 50 ms of GPIO34 at 10 kHz
print(hz, " Hz, first ", adc_to_mv(34, wave[0] as i32), " mV");
DAC¶
| Function | Notes |
|---|---|
dac_write(pin: i32, value: i32) |
0 to 2^dac_bits() − 1; false on a pin without a DAC |
dac_write_mv(pin: i32, mv: i32) |
the same, in mV |
dac_off(pin: i32) |
stops driving the pin |
dac_bits() |
8 on the ESP32, 12 on the H7, 0 on the ESP32-S3 and the Pico 2 W |
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Pins | none | GPIO25, GPIO26 | PA4, PA5 (pins 4 and 5) | none |
| Output | — | 0 to about 3.3 V | about 20 mV to VDDA; most linear from 0.2 V to VDDA − 0.2 V | — |
A DAC pin can be read back with the ADC, without a wire: GPIO25 and 26 are
ADC2 pins on the ESP32, and PA4 and PA5 are ADC1 pins on the H7.
examples/analog.goose
does this.
PWM¶
| Function | Notes |
|---|---|
pwm_start(pin: i32, freq_hz: i32) |
starts a square wave, low until a duty is set; returns the frequency the hardware hit, or −1 |
pwm_duty(pin: i32, duty: f32) |
the share of each period spent high: 0.0 to 1.0 |
pwm_pulse_us(pin: i32, us: i32) |
the high time of each period instead: servos take 1000–2000 µs at 50 Hz |
pwm_stop(pin: i32) |
ends the wave; the pin stays an output, low |
pwm_start(4, 50); // a servo on GPIO4
pwm_pulse_us(4, 1500); // to its middle
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Pins | any output GPIO | any output GPIO | PA0–PA3, PA5–PA8, PA15, PB0, PB1, PB3–PB11, PC6–PC9, PD12–PD15, PE9, PE11, PE13, PE14 | any header GPIO |
| At once | 8 pins, 4 frequencies | 8 pins, 4 frequencies | one frequency per timer (TIM1–TIM4) | one frequency per slice (8 slices of two pins) |
| Frequencies | 2 Hz to 40 MHz | 1 Hz to 40 MHz | 1 Hz to 100 MHz | 9 Hz to 75 MHz |
| Duty steps | 80 MHz / freq, at most 2^14 | 80 MHz / freq, at most 2^20 | 200 MHz / freq | 150 MHz / freq, at most 65535 |
The returned frequency can differ a little from the one asked for: 998 Hz
for 1000 on the ESP32. Pins started at the same frequency share a timer. On
the ESP32s a fifth frequency returns −1 while four run. On the H7 the pins
of one timer share its frequency, so starting one at another frequency
returns −1 while the others run. Pins on one timer channel (PA0, PA5 and
PA15 are all TIM2 channel 1) can't run at once. On the Pico 2 W, GPIO n
and n + 1 (n even) are one slice's two channels and share its frequency,
and GPIO n and n + 16 are the same channel, so they can't run at once.
Calling pwm_start on a running pin changes its frequency.
| H7 timer | Pins (channels 1, 2, 3, 4) |
|---|---|
| TIM1 | PA8 or PE9, PE11, PE13, PE14 |
| TIM2 (32-bit) | PA0, PA5 or PA15; PA1 or PB3; PA2 or PB10; PA3 or PB11 |
| TIM3 | PA6, PB4 or PC6; PA7, PB5 or PC7; PB0 or PC8; PB1 or PC9 |
| TIM4 | PB6 or PD12; PB7 or PD13; PB8 or PD14; PB9 or PD15 |
A new duty or frequency takes effect at the end of the current period, so at low frequencies it shows only after a while.
pin_read() reads a PWM pin's level, so a program can check its own wave
without a wire.
examples/pwm.goose
measures the frequency and duty that way.
UART¶
| Function | Notes |
|---|---|
uart_open(tx: i32, rx: i32, baud: i32) |
a serial port, 8N1; returns the port for the calls below, or −1 |
uart_write(port: i32, data: const u8[:]) |
returns the bytes sent, or −1 |
uart_read(port: i32, out: u8[:], timeout_ms: i32) |
fills out, waiting at most timeout_ms (0: only what has arrived, −1: no limit); returns the bytes read |
uart_read_line(port: i32, line: u8[>..]&, timeout_ms: i32) |
one line, without its \n or \r\n; false on the timeout |
uart_available(port: i32) |
the bytes waiting to be read |
uart_close(port: i32) |
|
uart_format(port: i32, bits: i32, parity: i32, stop_bits: i32) -> bool |
the frame: 5 to 8 data bits (7 or 8 on the H7), PARITY_NONE, PARITY_EVEN or PARITY_ODD, and 1 or 2 stop bits |
uart_invert(port: i32, tx: bool, rx: bool) -> bool |
inverts the TX line, the RX line or both, so they idle low |
uart_break(port: i32, us: i32) -> bool |
holds TX at a 0 bit's level for at least us µs, after what was written |
uart_rs485(port: i32, pin: i32) -> bool |
drives pin high while the port sends, for an RS-485 transceiver; −1 stops |
let gps = uart_open(17, 16, 9600);
var line: u8[>..] = [];
while uart_read_line(gps, line, 2000) { print(line); }
Received bytes wait in a buffer until the program reads them: 1 KB on the
ESP32s, 256 bytes on the H7 and the Pico 2 W. What arrives while the buffer
is full is lost. Either pin can be −1 for one direction. With the same pin
as tx and rx, the port is a one-wire link: the pin is open-drain with
its pull-up on, and the port hears what it sends. That checks a port with
no wiring, up to 1 Mbaud: beyond that the chip's pull-up is too weak, and a
one-wire bus needs a resistor (1 to 4.7 kΩ to 3.3 V). The Pico 2 W has no
one-wire mode: a pin there is a TX or an RX, so the same pin for both
returns −1.
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Ports | UART1, UART2 (UART0 is the console) | UART1, UART2 | USART1–3, UART4–5, USART6, UART7–8 | UART0, UART1 (the console is USB) |
| Pins | any | any | by port: see below | by port: see below |
| Baud | 300 to 5 Mbaud | 300 to 5 Mbaud | 1200 to 6.25 Mbaud | 144 baud to 9.375 Mbaud |
| H7 port | TX | RX |
|---|---|---|
| USART1 | PA9, PB6, PB14 | PA10, PB7, PB15 |
| USART2 | PA2, PD5 | PA3, PD6 |
| USART3 | PB10, PC10, PD8 | PB11, PC11, PD9 |
| UART4 | PA0, PA12, PB9, PC10, PD1 | PA1, PA11, PB8, PC11, PD0 |
| UART5 | PB6, PB13, PC12 | PB5, PB12, PD2 |
| USART6 | PC6 | PC7 |
| UART7 | PA15, PB4, PE8 | PA8, PB3, PE7 |
| UART8 | PE1 | PE0 |
| Pico 2 W port | TX | RX |
|---|---|---|
| UART0 | GPIO0, 2, 12, 14, 16, 18, 28 | GPIO1, 3, 13, 15, 17, 19 |
| UART1 | GPIO4, 6, 8, 10, 20, 22, 26 | GPIO5, 7, 9, 11, 21, 27 |
On the H7 and the Pico 2 W the pins pick the port, so tx and rx must
belong to the same one. On the ESP32s any pins work except the ones the chip keeps for its
flash and PSRAM (GPIO6–11 on the ESP32, and 16–17 in MicroGoose's ESP32
firmware, which supports WROVER modules; GPIO26–32 on the S3, and 33–37
with octal PSRAM): there, and on a pin another bus holds, every *_open
call here returns −1.
Frames, inverted lines and breaks¶
A port opens at 8N1: 8 data bits, no parity bit, 1 stop bit.
uart_format sets another frame. It and uart_invert first wait for the
bytes already written to go out, so those leave as they were written. Each
call returns false on a closed port, or for a frame the board can't make:
the H7 takes 7 or 8 data bits, the other boards 5 to 8. With fewer than 8,
the bytes read have their top bits clear.
let bus = uart_open(17, 16, 19200);
uart_format(bus, 8, PARITY_EVEN, 1); // Modbus RTU's frame
uart_invert flips a line so it idles low and a 0 bit is high, as SBUS
receivers send. An inverted RX pin gets a pull-down in place of its
pull-up, so with nothing connected it still reads as idle. On the H7 a
one-wire port can't be inverted: between bytes it lets go of the line, and
the pull-up holds it high. There uart_invert returns false for anything
but false, false on a one-wire port.
let rc = uart_open(-1, 16, 100000);
uart_format(rc, 8, PARITY_EVEN, 2); // SBUS: 100 kbaud, 8E2,
uart_invert(rc, false, true); // inverted
uart_break sends a break. Once what was written has gone out, TX holds
the level of a 0 bit (low, or high when inverted) for at least us
microseconds, then idles for a bit time, 12 µs at least, before the call
returns. A DMX512 packet is a break of at least 92 µs, that idle, then the
start code and 512 channels at 250 kbaud, 8N2:
let dmx = uart_open(17, -1, 250000);
uart_format(dmx, 8, PARITY_NONE, 2);
var packet: u8[>..] = [0]; // the start code
for i in 512 { packet.push(0); }
packet[1] = 255; // channel 1 full on
uart_break(dmx, 100);
uart_write(dmx, packet);
RS-485¶
An RS-485 transceiver (a 3.3 V one, such as the MAX3485 or SP3485) puts a
port on a two-wire bus that several devices share, one sending at a time.
uart_rs485(port, pin) drives pin, wired to the transceiver's DE and
/RE tied together, high from before the first start bit of each
uart_write until its last stop bit is out, and through a break and the
idle after it. The rest of the time the pin is low and the transceiver
listens. With a direction pin, uart_write returns once its last byte is
out. uart_rs485(port, -1) stops, and leaves the pin an output, low, as
uart_close does. With /RE held low instead of tied to DE, the port hears
its own bytes as it sends them.
The pin goes low 6 µs after the last stop bit on the H7, 7 µs on the Pico 2 W, 40 µs on the ESP32 (WROOM-32) and 48 µs on the ESP32-S3, where ESP-IDF reports the end of a send through an interrupt. A device that answers on the bus waits longer than that: Modbus RTU devices wait 3.5 characters, 1.75 ms or more.
let bus = uart_open(17, 16, 19200);
uart_format(bus, 8, PARITY_EVEN, 1);
uart_rs485(bus, 4); // DE and /RE
let ask: u8[8] = [0x01, 0x03, 0x00, 0x00, 0x00, 0x02, 0xC4, 0x0B];
uart_write(bus, ask); // Modbus: device 1, 2 registers
var answer: u8[9] = [0, 0, 0, 0, 0, 0, 0, 0, 0];
let n = uart_read(bus, answer, 100);
tests/stress/uart_frames.goose
checks all of this on a board with no wiring: it reads each frame back
from the TX pin's own edges, bit by bit.
I2C¶
| Function | Notes |
|---|---|
i2c_open(sda: i32, scl: i32, hz: i32) |
a bus master; returns the bus for the calls below, or −1 |
i2c_write(bus: i32, addr: i32, data: const u8[:]) |
|
i2c_read(bus: i32, addr: i32, out: u8[:]) |
|
i2c_write_read(bus: i32, addr: i32, data: const u8[:], out: u8[:]) |
writes, then reads after a repeated start: how most devices' registers are read |
i2c_probe(bus: i32, addr: i32) |
whether a device answers at addr |
i2c_close(bus: i32) |
Addresses are 7-bit, 0x00 to 0x7F. Each call returns false when nothing
answers, or when the bus stalls: 100 ms past the time the transfer takes. A device may
hold SCL low while it works (clock stretching): up to 10 ms at a time on
the ESP32s, and within that limit on the H7. The pins get the chip's internal pull-ups, which are weak: a real
bus wants a 2.2 to 10 kΩ resistor from each line to 3.3 V.
let bus = i2c_open(5, 18, 400000);
let reg: u8[1] = [0xD0]; // a BME280's ID register
var id: u8[1] = [0];
if i2c_write_read(bus, 0x76, reg, id) { print("chip id ", id[0]); }
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Buses | 2 | 2 | I2C1–I2C4 | I2C0, I2C1 |
| Pins | any | any | I2C1: SCL PB6 or PB8, SDA PB7 or PB9; I2C2: PB10, PB11; I2C3: PA8, PC9; I2C4: PD12, PD13 (or PB6–PB9) | SDA on an even GPIO, SCL on an odd one: GPIO 4k and 4k + 1 are I2C0, 4k + 2 and 4k + 3 are I2C1 |
| Rates | up to 800 kHz | up to 400 kHz, as rated | 15 kHz to 1 MHz | 10 kHz to 1 MHz |
The Pico 2 W's controller can't send an address alone, so i2c_probe
reads one byte from the device there.
SPI¶
| Function | Notes |
|---|---|
spi_open(sck: i32, mosi: i32, miso: i32, hz: i32, mode: i32) |
a bus master at hz or the nearest rate below, mode 0–3, MSB first; returns the bus, or −1 |
spi_transfer(bus: i32, cs: i32, tx: const u8[:], rx: u8[:]) |
sends tx while reading rx: as many bytes as the longer one |
spi_write(bus: i32, cs: i32, data: const u8[:]) |
|
spi_write_read(bus: i32, cs: i32, data: const u8[:], out: u8[:]) |
sends data, then reads out |
spi_close(bus: i32) |
Each call holds the chip-select pin cs low throughout; −1 for none. Past
the end of the data it has to send, the bus sends 0xFF. mosi or miso
can be −1 for one direction.
let flash = spi_open(14, 13, 12, 10000000, 0);
let cmd: u8[1] = [0x9F]; // JEDEC ID
var id: u8[3] = [0, 0, 0];
spi_write_read(flash, 15, cmd, id);
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Buses | SPI2, SPI3 | SPI2 (HSPI), SPI3 (VSPI) | SPI1–SPI4 | SPI0, SPI1 |
| Pins | any | any | SPI1: SCK PA5 or PB3, MISO PA6 or PB4, MOSI PA7, PB5 or PD7; SPI2: SCK PA9, PA12, PB10, PB13 or PD3, MISO PB14 or PC2, MOSI PB15, PC1 or PC3; SPI3: SCK PB3 or PC10, MISO PB4 or PC11, MOSI PB5, PC12 or PD6; SPI4: SCK PE2 or PE12, MISO PE5 or PE13, MOSI PE6 or PE14 | SPI0: SCK GPIO2, 6, 18 or 22, MOSI 3, 7 or 19, MISO 0, 4, 16 or 20; SPI1: SCK GPIO10, 14 or 26, MOSI 11, 15 or 27, MISO 8, 12 or 28 |
| Rates | up to 80 MHz | up to 80 MHz on the bus's own pins (HSPI: SCK 14, MOSI 13, MISO 12; VSPI: 18, 23, 19), up to 26.7 MHz on others | 100 MHz / 2, 4, …, 256: 50 MHz to 391 kHz | 150 MHz / 2 or more: 75 MHz to 2.3 kHz |
A rate above the chip's limit runs at the limit. On the ESP32s and the Pico
2 W, miso equal to mosi reads back what the bus sends, with no wiring:
on the classic ESP32 up to 18 MHz, on the Pico through the controller's
loopback at any rate. The H7 can't route a pin to both, and returns −1. examples/serial.goose
checks UART and SPI that way and scans the I2C bus.
CAN¶
Classic CAN frames: an 11-bit id (or 29 bits with CAN_EXT) and up to 8
bytes. On a real bus, each board needs a CAN transceiver (a 3.3 V one, such
as the SN65HVD230) between its pins and the bus.
| Function | Notes |
|---|---|
can_open(tx: i32, rx: i32, bitrate: i32, mode: i32) -> i32 |
CAN_NORMAL, CAN_LOOPBACK or CAN_LISTEN; returns the bus for the calls below, or −1 |
can_send(bus: i32, id: i32, data: const u8[:], flags: i32, timeout_ms: i32) -> bool |
queues a frame, waiting at most timeout_ms for room; flags CAN_EXT, CAN_RTR |
can_receive(bus: i32, out: u8[:], timeout_ms: i32) -> i32 |
the next frame's data into out; returns its length (0 to 8), or −1 if none came |
can_read(bus: i32, out: u8[:], timeout_ms: i32) -> i32, i32, i32 |
the same, with the frame's id and flags |
can_last_id(bus: i32) -> i32, can_last_flags(bus: i32) -> i32 |
the id and flags of the frame can_receive returned last |
can_available(bus: i32) -> i32 |
frames waiting to be read |
can_dropped(bus: i32) -> i64 |
frames lost because 64 were already waiting |
can_filter(bus: i32, id: i32, mask: i32, flags: i32) -> bool |
keeps only frames whose id matches id on the bits set in mask |
can_state(bus: i32) -> i32 |
0 error active, 1 warning, 2 error passive, 3 bus off |
can_tx_errors(bus: i32) -> i32, can_rx_errors(bus: i32) -> i32 |
the error counters, TEC and REC |
can_recover(bus: i32) -> bool |
rejoins the bus after bus off |
can_close(bus: i32) |
let bus = can_open(5, 4, 500000, CAN_NORMAL); // a transceiver on GPIO5 (TX) and GPIO4 (RX)
let rpm: u8[2] = [0x0B, 0xB8];
can_send(bus, 0x123, rpm, 0, 100);
var data: u8[8] = [0, 0, 0, 0, 0, 0, 0, 0];
while true {
let n, id, flags = can_read(bus, data, 1000);
if n >= 0 { print("id ", id, ": ", n, " bytes"); }
}
A remote frame (CAN_RTR) asks for data and carries none: it is sent with
the length of data, and comes back with that length and nothing written
to out. Received frames wait for the program in a ring of 64; beyond
that, new frames are dropped and counted by can_dropped. Up to 16 frames
wait to go out.
CAN resends a frame until another node acknowledges it. A node alone on
the bus never gets an ACK, so its frame stays queued and the node goes
error passive (can_state 2) or, with nothing on its RX pin, bus off (3).
can_recover brings a bus-off node back, and a frame that never went out
is tried again. can_close drops the frames still waiting, after 100 ms
on the ESP32s.
Modes.
CAN_LOOPBACK: the controller hears its own frames and needs no ACK, so a board can check itself with no transceiver.- ESP32s: pass the same pin as
txandrx. The frames go out on it as well. - H7: the frames go out on TX too, and RX is ignored.
- ESP32s: pass the same pin as
CAN_LISTEN: the controller receives frames, but never sends or acknowledges one. On the ESP32s it reads as error passive (REC 128): ESP-IDF sets that so a chip erratum can't send an error frame.
Without transceivers. On a bench, ESP32s can share one wire instead of
a bus. With the same pin as tx and rx, the pin is open-drain with its
pull-up, so it only ever pulls the wire low. The H7's TX pins drive the
line both ways, so an H7 needs a transceiver to share a bus. The internal
pull-ups are weak, so keep such a wire short and the bit rate low: an
ESP32-S3 and an ESP32 alone on a wire passed at 500 kbit/s and went bus
off at 1 Mbit/s.
Filters. A filter keeps one kind of id: 11-bit, or 29-bit when flags
has CAN_EXT. A frame passes when its id has the same bits as id
wherever mask has a 1, and frames of the other kind are dropped.
can_filter(bus, 0x120, 0x7F0, 0) keeps 0x120 to 0x12F. Each bus has one
filter, and can_filter(bus, 0, 0, 0) takes every frame again.
The Pico 2 W has no CAN controller: can_open returns −1 there.
| ESP32-S3 | ESP32 | STM32H7 | |
|---|---|---|---|
| Buses | one (bus 0) | one (bus 0) | FDCAN1 (bus 0), FDCAN2 (bus 1) |
| Pins | any | any | FDCAN1: TX PA12, PB9 or PD1, RX PA11, PB8 or PD0; FDCAN2: TX PB6 or PB13, RX PB5 or PB12 |
| Bit rates (checked) | 10 kbit/s to 1 Mbit/s | 25 kbit/s to 1 Mbit/s | 10 kbit/s to 1 Mbit/s, except 800 kbit/s |
The H7 clocks CAN from its 25 MHz crystal (HSE) when it starts, so a bit rate must divide 25 MHz evenly: 800 kbit/s can't. A board without the crystal falls back to 100 MHz from the internal oscillator, which can be about 1% off. That's fine for loopback, but on a long bus it can be too far off. On the DevEBox, the CAN bit rate measured within 0.3% of the CPU clock.
examples/can.goose
runs a bus in loopback, with no wiring.
Interrupts¶
Pin edges and timer ticks reach the program as events, in the order they
happened. Goose code doesn't run inside an interrupt: the interrupt
stamps each event with its time and queues it, and event_wait hands the
program the next one.
| Function | Notes |
|---|---|
pin_watch(pin: i32, edges: i32, debounce_us: i32) -> bool |
RISING, FALLING or CHANGE, maybe or-ed with COUNT_ONLY; 0 stops. False if the pin can't interrupt |
pin_count(pin: i32) -> i64 |
the edges taken since pin_watch, queued or not; kept after a stop |
timer_every(period_us: i32) -> i32 |
a tick every period_us (at least 100); returns the timer's id, or −1 when all 4 run |
timer_stop(id: i32) |
|
event_wait(timeout_ms: i32) -> i32 |
the next event's source (its pin, or a timer's id), waiting at most timeout_ms (0: only what has arrived, −1: no limit); −1 if none came |
event_next(timeout_ms: i32) -> i32, i64, i32 |
the same, with the event's time and level |
event_us() -> i64 |
when the last event happened, on the micros() clock |
event_level() -> i32 |
the pin's level then: 0 or 1; 1 for a timer; −1 when the watch stopped itself |
event_pending() -> i32 |
events waiting |
event_dropped() -> i64 |
events lost to a full queue |
pin_input(0, PULL_UP); // a button to ground on GPIO0
pin_watch(0, FALLING, 20000); // presses, minus 20 ms of bounce
let tick = timer_every(1000000);
while true {
let src, us, level = event_next(-1);
if src == tick { print("presses so far: ", pin_count(0)); }
else { print("pressed at ", us, " us"); }
}
Timer ids start at 1000, so they never equal a pin. debounce_us skips
the edges that come less than that long after the last one taken.
COUNT_ONLY counts edges without queueing them, which suits a fast
signal: pin_count over a known time gives its frequency. An output or
PWM pin can watch itself, so a program can check its own signals with no
wiring.
examples/interrupts.goose
does that.
The queue holds 255 events. When it's full, new events are dropped and
counted by event_dropped, but pin_count still counts them. Each
pin_watch call drops the pin's queued events, and timer_stop drops
the timer's ticks, so an id that's used again starts clean.
On the ESP32s an event that comes while a thread waits in event_wait
goes straight to that thread, not through the queue, and the thread
that has waited longest gets it. So a stop from another thread doesn't
take back an event already given to a waiting thread: it came before the
stop. Four threads can wait at once there; a fifth looks for events
again at each tick, so it can have one up to 1 ms late.
Every edge costs the chip some time, queued or not. If a pin edges faster
than the limit below over 10 ms, its watch stops itself: it queues one
event of level −1 (that event always fits), and pin_count stops
growing. Without that stop, a 500 kHz signal reset the S3 through its
interrupt watchdog, and a 3 MHz one stalled the H7.
| ESP32-S3 | ESP32 | STM32H7 | Pico 2 W | |
|---|---|---|---|---|
| Pins | any GPIO | any GPIO | PA0–PE15, one port per pin number: PA3 and PB3 can't both be watched | any header GPIO |
| Fastest edges | 100 kHz | 100 kHz | 1 MHz | 1 MHz |
| Timers | 4: the chip's general-purpose timers | 4: the chip's general-purpose timers | 4: TIM5, TIM6, TIM7, TIM12 | 4: alarms on the chip's microsecond timer |
| Timer period | 100 µs to 35 minutes | 100 µs to 35 minutes | 100 µs to 35 minutes; one timer at a time above 21 s (TIM5, the 32-bit one) | 100 µs to 35 minutes |
| Event to a waiting program (measured average) | 8 µs | 9–10 µs | under 1 µs | 3 µs (a timer's tick) |
On the H7, the event's time comes from the cycle counter, and the timers
are crystal-exact: a 1 ms timer measured 1000 µs between every tick. On
the ESP32s each timer is one of the chip's four general-purpose timers,
and its interrupt stamps the tick: a 1 ms timer measured 1000 µs between
every tick there too. C code built into a program (--include) that
takes one of those timers leaves three for timer_every. On the Pico 2 W
each timer is an alarm in the Pico SDK's alarm pool, set for the previous
tick's time plus the period, so it doesn't drift; its interrupt stamps the
tick 0–2 µs after it's due.
On the ESP32s, an event that comes while a worker computes on the
program's core waits for the scheduler's tick, up to 1 ms. event_wait
then moves that worker to the other core if it is idle, so one event is
late and the ones after it aren't. With both cores busy, every event
waits for the tick. The measurements are in
Optimizations, section 8.
Events ahead of the workers¶
Every thread of a program starts at one priority, so a thread that waits for an event takes turns with the workers that are computing. A thread that must answer each event as it comes says so:
task_priority(1); // this thread now runs ahead of the workers
let tick = timer_every(1000);
while true {
let src = event_wait(-1); // back here within microseconds of the event
... // handle it, then wait again
}
task_priority(up) puts the calling thread up steps above the priority
threads start at, and task_priority(0) puts it back. It returns the
level it set: at most 3 on the ESP32s and 1 on the STM32H7. The Pico 2 W
doesn't have it yet, and returns 0. Any thread can call it, the main
program or a worker.
A thread one step up takes its core the moment an event's interrupt wakes it. With a worker computing on each core of an ESP32, a timer's tick or a pin's edge then reaches the program in 10 to 12 µs, 17 at the slowest, where at 0 it takes a median of 0.8 to 1.1 ms. C with its waiting task raised the same way takes 8 to 10 µs (Optimizations, section 11).
What it costs is the workers' time: while the raised thread runs, a worker on its core waits, for as long as it runs. So raise the thread that waits for events and does little with each one, and leave long computing at 0. Two threads at the same raised level take turns with each other, like any two threads of one priority. A raised thread that waits for a lock a worker holds, a queue's or the console's, waits for that worker like any other thread.
On the STM32H7 a waiting thread already has its event at once, at 0 too. There the step decides what happens after: at 0 the thread takes turns with the workers again from the scheduler's next tick, and one step up it keeps the chip until it next waits.
Sensors and system¶
| Function | Returns | ESP32-S3 | ESP32 | STM32H7 | Pico 2 W |
|---|---|---|---|---|---|
chip_temp_c() |
f32 |
°C | −273 (no sensor) | °C, from the factory calibration | °C, from the datasheet's typical line |
hw_random() |
u32 |
hardware RNG | hardware RNG | xorshift (not secure) | hardware RNG |
free_heap() |
i32 |
bytes | bytes | bytes | bytes |
cpu_mhz() |
i32 |
240 | 240 | 400 | 150 |
core_id() |
i32 |
0 or 1 | 0 or 1 | 0 | 0 |
task_priority(up: i32) |
i32: the level set |
0 to 3 | 0 to 3 | 0 or 1 | 0: not yet |
stack_report() |
nothing | prints | prints | prints | says it can't yet |
task_priority sets the calling thread's priority, in steps above the one
every thread starts at: see Events ahead of the workers.
stack_report prints how much of each stack the program has used so far.
A program built with --stack-report measures its data stacks and its
workers' stacks for it
(Measuring stacks).
Adding a binding¶
To add a binding, write one C function with plain scalar arguments:
// components/microgoose/mg_hal.c
int32_t mg_hall_read(void) { return /* ... */ 0; }
and one line of Goose:
// lib/mcu.goose
extern fn hall_read() -> i32 = "mg_hall_read";
Goose passes integers, floats and flat structs to C unchanged, and slices as
{data, len}. Declare the C prototype in include/microgoose.h as well.
Every port must then provide the same symbol, even if only as a stub.