machine — functions related to the hardware

The machine module contains specific functions related to the hardware on a particular board. Most functions in this module allow to achieve direct and unrestricted access to and control of hardware blocks on a system (like CPU, timers, buses, etc.). Used incorrectly, this can lead to malfunction, lockups, crashes of your board, and in extreme cases, hardware damage.

Memory access

The module exposes three subscriptable objects used for raw memory access. Each behaves like a sparse array indexed by byte address: value = memN[addr] reads, memN[addr] = value writes. The address is always a byte address, regardless of the access width.

machine.mem8

Subscriptable 8-bit memory accessor. mem8[addr] reads an int in the range 0-255 from the byte at addr; mem8[addr] = value writes the low 8 bits of value. addr must be aligned to 1 byte (any address).

machine.mem16

Subscriptable 16-bit (halfword) memory accessor. mem16[addr] reads an int in the range 0-65535; mem16[addr] = value writes the low 16 bits. addr must be aligned to 2 bytes.

machine.mem32

Subscriptable 32-bit (word) memory accessor. mem32[addr] reads an int in the range 0-0xFFFFFFFF; mem32[addr] = value writes the low 32 bits. addr must be aligned to 4 bytes.

Example use (registers are specific to an stm32 microcontroller):

import machine
from micropython import const

GPIOA = const(0x48000000)
GPIO_BSRR = const(0x18)
GPIO_IDR = const(0x10)

# set PA2 high
machine.mem32[GPIOA + GPIO_BSRR] = 1 << 2

# read PA3
value = (machine.mem32[GPIOA + GPIO_IDR] >> 3) & 1

Miscellaneous functions

machine.unique_id() bytes

Returns a byte string with a unique identifier of a board/SoC. It will vary from a board/SoC instance to another, if underlying hardware allows. Length varies by hardware (so use substring of a full value if you expect a short ID). In some MicroPython ports, ID corresponds to the network MAC address.

machine.time_pulse_us(pin: Pin, pulse_level: int, timeout_us: int = 1000000, /) int

Time a pulse on the given pin, and return the duration of the pulse in microseconds. The pulse_level argument should be 0 to time a low pulse or 1 to time a high pulse.

If the current input value of the pin is different to pulse_level, the function first (*) waits until the pin input becomes equal to pulse_level, then (**) times the duration that the pin is equal to pulse_level. If the pin is already equal to pulse_level then timing starts straight away.

The function will return -2 if there was timeout waiting for condition marked (*) above, and -1 if there was timeout during the main measurement, marked (**) above. The timeout is the same for both cases and given by timeout_us (which is in microseconds).

machine.bitstream(pin: Pin, encoding: int, timing: tuple, data: bytes, /) None

Transmits data by bit-banging the specified pin. The encoding argument specifies how the bits are encoded, and timing is an encoding-specific timing specification.

The supported encodings are:

  • 0 for “high low” pulse duration modulation. This will transmit 0 and 1 bits as timed pulses, starting with the most significant bit. The timing must be a four-tuple of nanoseconds in the format (high_time_0, low_time_0, high_time_1, low_time_1). For example, (400, 850, 800, 450) is the timing specification for WS2812 RGB LEDs at 800kHz.

The accuracy of the timing varies between ports. On Cortex M0 at 48MHz, it is at best +/- 120ns, however on faster MCUs (ESP8266, ESP32, STM32, Pyboard), it will be closer to +/-30ns.

Note

For controlling WS2812 / NeoPixel strips, see the neopixel module for a higher-level API.

Constants

machine.IDLE: int
machine.SLEEP: int
machine.DEEPSLEEP: int

IRQ wake values.

machine.PWRON_RESET: int
machine.HARD_RESET: int
machine.WDT_RESET: int
machine.DEEPSLEEP_RESET: int
machine.SOFT_RESET: int

Reset causes.

machine.WLAN_WAKE: int
machine.PIN_WAKE: int
machine.RTC_WAKE: int

Wake-up reasons.

Classes