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.

The RI5 version of this module seems to be a lot like the STM32 port of Micropython, as you might expect given that the RI5 runs on a STM32F413. A lot of the RI5 specifics here line up with the code for that port.

A note of callbacks used by functions and class methods of machine module: all these callbacks should be considered as executing in an interrupt context. This is true for both physical devices with IDs >= 0 and “virtual” devices with negative IDs like -1 (these “virtual” devices are still thin shims on top of real hardware and real hardware interrupts). See Writing interrupt handlers.

Miscellaneous functions

machine.info([verbose])

Difference for RI5

A function specific to the RI5.

Prints various information, including:

  • ID = The hex of the unique_id()

  • S = System Clock frequency. See freq() above.

  • H = AHB (Advanced High-Performance Bus) Clock frequency. See freq() above.

  • P1 = APB1 (Advanced Peripheral Bus 1) Clock frequency. See freq() above.

  • P2 = APB2 (Advanced Peripheral Bus 2) Clock frequency. See freq() above.

  • “qstr” section with similar information to micropython.qstr_info()

  • “GC” section with similar information to micropython.mem_info()

  • If the verbose parameter is defined then it also prints the GC memory layout that you get from mem_info()’s verbose mode.

machine.unique_id()

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, pulse_level, timeout_us=1000000)

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).

Difference for RI5

Function rng() is not implemented for the RI5.

Memory Access

machine.mem8
machine.mem16
machine.mem32

Supports machine memory access in 1-byte, 2-byte or 4-byte chunks. Access is via indexing, where the index is the memory address of the beginning of the chunk. (Attempts to use a wrongly aligned address for the chunk size cause a ValueError.)

Beware that slicing doesn’t work properly and may cause a system failure!

On the RI5, be aware that 0 is a valid address: addressable memory goes from 0 to 1572863 (=0x17FFFF) inclusive, representing 1.5 MiB. Attempting to reference addresses outside of this causes a system restart. Addresses from 0x08a670 seem to all return 0xFF values though so I’m not sure how useful anything above this is…

The system lets you attempt to set address contents with memX[index] = value, but it doesn’t seem to be effective - subsequent reads just show the old value again.

Constants

Difference for RI5

IRQ wake value constants and wake-up reason constants are not present on the RI5.

machine.PWRON_RESET
machine.HARD_RESET
machine.WDT_RESET
machine.DEEPSLEEP_RESET
machine.SOFT_RESET

Reset causes.

Classes from default Micropython not present on the Hub

  • class ADCChannel - read analog values from internal or external sources

  • class SD - secure digital memory card