Machine (machine)

The modules described in this chapter provide the interface to the hardware interfaces in the machine module.

class machine.I2C(device: any, address: int)

An I2C (Inter-Integrated Circuit) object is used to interact with the I2C peripheral. The two-wire interface can can be used to communicate with multiple devices on the same bus. Device addresses may be specific to the device, selectable by input pins, or programmable.

Parameters:
  • device – [str] Device identifier

  • device – [3-tuple] (Device identifier, SDA pin name, SCL pin name)

  • address – Device address

Zephyr example: I2C(“i2c@40003000”, 0x40)

Silicon Labs (Lyra) example: I2C((“I2C0”, “PD03”, “PD02”), 0x40)

0x40 is the 7-bit address of the TI HDC1010 (https://download.mikroe.com/documents/datasheets/hdc1010.pdf) that is part of the temperature and humidity click board (https://www.mikroe.com/temp-hum-4-click) that can be populated in the mikroBUS socket.

read(length: int) any

Read from I2C device.

Parameters:

length – Number of bytes to read

set_address(address: int)

Set device address.

write(data: any) int

Write to I2C device.

Parameters:

data – Buffer containing bytes to write

write_read(data: any, length: int) any

Write-then-read I2C device (repeated start).

Parameters:
  • data – Buffer containing bytes to write

  • length – Number of bytes to read

class machine.Pin(port_pin: any, mode: int, pull: int = PULL_NONE)

A pin object is used to control I/O pins (also known as GPIO - general-purpose input/output). Pin objects are commonly associated with a physical pin that can drive an output voltage and read input voltages. The pin class has methods to set the mode of the pin (IN, OUT, etc) and methods to get and set the digital logic level.

Parameters:
configure_event(callback: any, type: int)

Configure GPIO pin event (interrupt).

Parameters:
  • callback – Function that is called when event occurs

  • typePin.EVENT_* type

high()

Set a pin to ‘1’.

low()

Set a pin to ‘0’.

off()

Set a pin to off (‘0’).

on()

Set a pin to on (‘1’).

reconfigure(port_pin: any, mode: int, pull: int = PULL_NONE)

Delete and re-initialize pin (potentially with a different configuration).

Parameters:
toggle()

Toggle a pin’s state.

value() int

Read an input pin. Negative on error.

ANALOG: int

Pin is configured as an analog input

EVENT_BOTH: int

Event on rising or falling edge

EVENT_FALLING: int

Event on falling edge (1 -> 0 transition)

EVENT_LEVEL_HIGH: int

Event on high level

EVENT_LEVEL_LOW: int

Event on low level

EVENT_NONE: int

No events

EVENT_RISING: int

Event on rising edge (0 -> 1 transition)

IN: int

Pin is an input

NO_CONNECT: int

Pin is disconnected internally

OPEN_DRAIN: int

Pin is an output, set to high impedance

OUT: int

Pin is an output

PULL_DOWN: int

Pin has a pull-down resistor enabled

PULL_NONE: int

Pin has no pull-up or pull-down resistor

PULL_UP: int

Pin has a pull-up resistor enabled

class machine.RTC

This object is used to interface with the real time clock peripheral. The real time clock is used to keep track of the current date and time. This class is a singleton, only one RTC can be instantiated at a time. Attempting to instantiate a second RTC will return the existing instance.

datetime(tuple=None) any

Get or set the date and time of the RTC.

With no arguments, this method returns an 8-tuple with the current date and time. With one argument (being an 8-tuple), it sets the date and time.

The 8-tuple has the following format:

(year, month, day, weekday, hours, minutes, seconds, milliseconds)

Parameters:

tuple – Optional 8-tuple of date and time

Returns:

8-tuple of date and time

class machine.SPI(device: any, pin: any)

This object is used to interact with the SPI peripheral. It is commonly used with 3 or 4 signals (clock, data out, data in, and chip select). The chip select can be controlled by the peripheral or by the application. When controlled by the application, multiple devices can exist on the same bus.

Parameters:
  • device – [str] Device identifier

  • device – [4-tuple] (Device identifier, clock pin name, data out pin name, data in pin name)

  • pin – [None] Optional chip select pin

  • pin – [object] Pin

  • pin – [3-tuple] (Port, pin number, active state)

  • pin – [2-tuple] (Pin name, active state)

Zephyr example - SPI(“spi@40023000”, Pin(“SPI_CS”, Pin.OUT, Pin.PULL_NONE)) or SPI(“spi@40023000”, (“gpio@50000000”, 20, SPI.CS_ACTIVE_LOW))

Silicon Labs (Lyra) example - SPI((“USART0”, “PC02”, “PC04”, “PC05”), (“PC03”, SPI.CS_ACTIVE_LOW))

configure(rate: int, polarity: int, phase: int, first_bit: int)

Change configuration from default (1M, 0, 0, MSB).

The underlying Silicon Labs (Lyra) driver can only support a single configuration for hardware peripheral.

Parameters:
  • rate – SCK rate in Hz

  • polarity – Clock polarity (0 or 1)

  • phase – Clock phase (0 or 1)

  • first_bit – Bit to send first. MSB or LSB

transceive(data: any) any

Write data to device and return equal sized buffer.

Parameters:

data – Bytes to write

CS_ACTIVE_HIGH: int

Configure the SPI bus to use an active high (‘1’) chip select

CS_ACTIVE_LOW: int

Configure the SPI bus to use an active low (‘0’) chip select

LSB: int

Configure the SPI bus to send the least significant byte first

MSB: int

Configure the SPI bus to send the most significant byte first