Core Classes
This page documents the core hardware interface classes provided by mock_machine.
Pin
Pin(id, mode=0, pull=0, value=None, drive=0, alt=-1)
Unittest support class for machine.Pin
Allows manual setting of input or output pin's value.
https://docs.micropython.org/en/latest/library/machine.Pin.html
Source code in mock_machine.py
Functions
init(mode=0, pull=0, alt=0, value=None)
value(new_value=None)
Source code in mock_machine.py
irq(handler=None, trigger=IRQ_FALLING | IRQ_RISING, priority=1, wake=None, hard=False)
Pin Constants
Pin.IN- Input modePin.OUT- Output modePin.OPEN_DRAIN- Open drain modePin.ALT- Alternate function modePin.ALT_OPEN_DRAIN- Alternate open drain modePin.ANALOG- Analog modePin.PULL_UP- Enable pull-up resistorPin.PULL_DOWN- Enable pull-down resistorPin.IRQ_RISING- Interrupt on rising edgePin.IRQ_FALLING- Interrupt on falling edge
I2C
I2C(*args, id=None, scl=None, sda=None, freq=400000)
Unittest support class for machine.I2C.
https://docs.micropython.org/en/latest/library/machine.I2C.html
Construct a new I2C object.
Initialise mock I2C with register values.
Source code in mock_machine.py
Functions
init(scl, sda, *args, freq=400000)
deinit()
scan()
Scan I2C for responding devices.
Scans all I2C addresses between 0x08 and 0x77 inclusive and return a list of those that respond.
Returns a list of addresses that responded to the scan.
Source code in mock_machine.py
readfrom(addr, nbytes, stop=True)
Read nbytes from the peripheral specified by addr.
If stop is true then a STOP condition is generated at the end of the transfer.
Returns a bytes object with the data read.
Source code in mock_machine.py
readfrom_into(addr, buf, stop=True)
Read into buf from the peripheral specified by addr.
The number of bytes read will be the length of buf. If stop is true then a STOP condition is generated at the end of the transfer.
The method returns None.
Source code in mock_machine.py
writeto(addr, buf, stop=True)
Write the bytes from buf to the peripheral specified by addr.
If a NACK is received following the write of a byte from buf then the remaining bytes are not sent. If stop is true then a STOP condition is generated at the end of the transfer, even if a NACK is received.
The function returns the number of ACKs that were received.
Source code in mock_machine.py
readfrom_mem(addr, memaddr, nbytes, *args, addrsize=8)
Read nbytes from the peripheral specified by addr, starting at memaddr.
Starting from the memory address specified by memaddr. The argument addrsize specifies the address size in bits.
Returns a bytes object with the data read.
Source code in mock_machine.py
readfrom_mem_into(addr, memaddr, buf, *args, addrsize=8)
Read into buf from the peripheral specified by addr, starting at memaddr.
The number of bytes read is the length of buf. The argument addrsize specifies the address size in bits.
The method returns None.
Source code in mock_machine.py
writeto_mem(addr, memaddr, buf, *args, addrsize=8)
Write buf to the peripheral specified by addr, starting at memaddr.
The number of bytes written is the length of buf. The argument addrsize specifies the address size in bits.
The method returns None.
Source code in mock_machine.py
add_device(device)
Add I2CDevice at address I2CDevice.address.
SPI
SPI(id=None)
Unittest support class for machine.SPI
https://docs.micropython.org/en/latest/library/machine.SPI.html
Construct an SPI object on the given bus, id.
Source code in mock_machine.py
Functions
init(baudrate=1000000, *, polarity=0, phase=0, bits=8, firstbit=None, sck=None, mosi=None, miso=None, pins=None)
Initialise the SPI bus with the given parameters
deinit()
read(nbytes, write=0)
Read a number of bytes specified by nbytes.
Continuously writes the single byte given by write, to ensure read data is clocked.
Returns a bytes object with the data that was read.
Source code in mock_machine.py
readinto(buf, write=0)
Read into the buffer specified by buf.
Number of bytes read is the length of the buffer. Continuously writing the single byte given by write, to ensure read data is clocked.
Returns None.
Source code in mock_machine.py
write(buf)
Write the bytes contained in buf.
Returns None.
Source code in mock_machine.py
write_readinto(write_buf, read_buf)
Write the bytes from write_buf while reading into read_buf.
The buffers can be the same or different, but both buffers must have the same length.
Returns None.
Source code in mock_machine.py
ADC
ADC(pin)
Functions
init(*, sample_ns, atten)
block()
read_u16()
Take an analog reading and return an integer in the range 0-65535.
The return value represents the raw reading taken by the ADC.
Source code in mock_machine.py
read_uv()
Take an analog reading and return an integer value with units of microvolts.
It is up to the particular port whether or not this value is calibrated, and how calibration is done. Note: stm32 port does not include this functionality as of August 2023.
Source code in mock_machine.py
PWM
PWM(dest, *, freq, duty_u16=None, duty_ns=None, invert=False)
https://docs.micropython.org/en/latest/library/machine.PWM.html
Source code in mock_machine.py
Functions
init(freq, duty_u16, duty_ns)
deinit()
freq(value=None)
duty_u16(value=None)
UART
UART(id=None, baudrate=9600, bits=8, parity=None, stop=1, tx=None, rx=None, txbuf=256, rxbuf=256, timeout=0, timeout_char=0, invert=0, flow=0, read_buf_len=256, data_for_read=b'')
Bases: IOBase
Mock UART https://docs.micropython.org/en/latest/library/machine.UART.html
Source code in mock_machine.py
Functions
write(data)
read(nbytes=-1)
readinto(buf, nbytes=None)
readline()
ioctl(op, arg)
Source code in mock_machine.py
Timer
Timer(id=0, channel=None, mode=None, period=None, callback=None)
https://docs.micropython.org/en/latest/library/machine.Timer.html
Source code in mock_machine.py
Functions
init(freq=1000, mode=PERIODIC, period=-1, callback=None)
Source code in mock_machine.py
Timer Constants
Timer.ONE_SHOT- One-shot timer modeTimer.PERIODIC- Periodic timer mode
WDT
WDT(timeout)
https://docs.micropython.org/en/latest/library/machine.WDT.html
Source code in mock_machine.py
RTC
RTC()
https://docs.micropython.org/en/latest/library/machine.RTC.html
Source code in mock_machine.py
Functions
wakeup(timeout=None, callback=None)
Register a wakeup timer.
:param timeout: Wakeup interval in ms :param callback: Function to call on wakeup
Source code in mock_machine.py
stop()
info()
staticmethod
Signal
Signal(pin, invert)
https://docs.micropython.org/en/latest/library/machine.Signal.html