# `pyb` — functions related to the board

The `pyb` module contains specific functions related to the board.

## Time related functions

pyb.delay(ms)

  Delay for the given number of milliseconds.

pyb.udelay(us)

  Delay for the given number of microseconds.

pyb.millis()

  Returns the number of milliseconds since the board was last reset.

  The result is always a MicroPython smallint (31-bit signed number), so after 2^30 milliseconds (about 12.4 days) this will start to return negative numbers.

  Note that if `pyb.stop()` is issued the hardware counter supporting this function will pause for the duration of the “sleeping” state. This will affect the outcome of `pyb.elapsed_millis()`.

pyb.micros()

  Returns the number of microseconds since the board was last reset.

  The result is always a MicroPython smallint (31-bit signed number), so after 2^30 microseconds (about 17.8 minutes) this will start to return negative numbers.

  Note that if `pyb.stop()` is issued the hardware counter supporting this function will pause for the duration of the “sleeping” state. This will affect the outcome of `pyb.elapsed_micros()`.

pyb.elapsed_millis(start)

  Returns the number of milliseconds which have elapsed since `start`.

  This function takes care of counter wrap, and always returns a positive number. This means it can be used to measure periods up to about 12.4 days.

  Example:

      start = pyb.millis()
      while pyb.elapsed_millis(start) < 1000:
          # Perform some operation

pyb.elapsed_micros(start)

  Returns the number of microseconds which have elapsed since `start`.

  This function takes care of counter wrap, and always returns a positive number. This means it can be used to measure periods up to about 17.8 minutes.

  Example:

      start = pyb.micros()
      while pyb.elapsed_micros(start) < 1000:
          # Perform some operation
          pass

## Reset related functions

pyb.hard_reset()

  Resets the OpenMV Cam in a manner similar to pushing the external RESET button.

pyb.bootloader()

  Activate the bootloader without BOOT* pins.

pyb.fault_debug(value)

  Enable or disable hard-fault debugging. A hard-fault is when there is a fatal error in the underlying system, like an invalid memory access.

  If the value argument is `False` then the board will automatically reset if there is a hard fault.

  If value is `True` then, when the board has a hard fault, it will print the registers and the stack trace, and then cycle the LEDs indefinitely.

  The default value is disabled, i.e. to automatically reset.

## Interrupt related functions

pyb.disable_irq()

  Disable interrupt requests. Returns the previous IRQ state: `False`/`True` for disabled/enabled IRQs respectively. This return value can be passed to enable_irq to restore the IRQ to its original state.

pyb.enable_irq(state=True)

  Enable interrupt requests. If `state` is `True` (the default value) then IRQs are enabled. If `state` is `False` then IRQs are disabled. The most common use of this function is to pass it the value returned by `disable_irq` to exit a critical section.

## Power related functions

pyb.wfi()

  Wait for an internal or external interrupt.

  This executes a `wfi` instruction which reduces power consumption of the MCU until any interrupt occurs (be it internal or external), at which point execution continues. Note that the system-tick interrupt occurs once every millisecond (1000Hz) so this function will block for at most 1ms.

pyb.stop()

  Put the OpenMV Cam in a “sleeping” state.

  This reduces power consumption to less than 500 uA. To wake from this sleep state requires an external interrupt or a real-time-clock event. Upon waking execution continues where it left off.

  See `rtc.wakeup()` to configure a real-time-clock wakeup event.

pyb.standby()

  Put the OpenMV Cam into a “deep sleep” state.

  This reduces power consumption to less than 50 uA. To wake from this sleep state requires a real-time-clock event. Upon waking the system undergoes a hard reset.

  See `rtc.wakeup()` to configure a real-time-clock wakeup event.

## Miscellaneous functions

pyb.have_cdc()

  Return True if USB is connected as a serial device, False otherwise.

  Note

  This function is deprecated. Use pyb.USB_VCP().isconnected() instead.

pyb.hid((buttons, x, y, z))

  Takes a 4-tuple (or list) and sends it to the USB host (the PC) to signal a HID mouse-motion event.

  Note

  This function is deprecated. Use `pyb.USB_HID.send()` instead.

pyb.info([dump_alloc_table])

  Print out lots of information about the board.

pyb.main(filename)

  Set the filename of the main script to run after boot.py is finished. If this function is not called then the default file main.py will be executed.

  It only makes sense to call this function from within boot.py.

pyb.mount(device, mountpoint, *, readonly=False, mkfs=False)

  Note

  This function is deprecated. Mounting and unmounting devices should be performed by `os.mount()` and `os.umount()` instead.

  Mount a block device and make it available as part of the filesystem. `device` must be an object that provides the block protocol. (The following is also deprecated. See `os.AbstractBlockDev` for the correct way to create a block device.)

  > - `readblocks(self, blocknum, buf)`

  > - `writeblocks(self, blocknum, buf)` (optional)

  > - `count(self)`

  > - `sync(self)` (optional)

  `readblocks` and `writeblocks` should copy data between `buf` and the block device, starting from block number `blocknum` on the device. `buf` will be a bytearray with length a multiple of 512. If `writeblocks` is not defined then the device is mounted read-only. The return value of these two functions is ignored.

  `count` should return the number of blocks available on the device. `sync`, if implemented, should sync the data on the device.

  The parameter `mountpoint` is the location in the root of the filesystem to mount the device. It must begin with a forward-slash.

  If `readonly` is `True`, then the device is mounted read-only, otherwise it is mounted read-write.

  If `mkfs` is `True`, then a new filesystem is created if one does not already exist.

pyb.repl_uart(uart)

  Get or set the UART object where the REPL is repeated on.

pyb.rng()

  Return a 30-bit hardware generated random number.

pyb.sync()

  Sync all file systems.

pyb.unique_id()

  Returns a string of 12 bytes (96 bits), which is the unique ID of the MCU.

pyb.usb_mode([modestr, ]port=-1, vid=0xf055, pid=-1, msc=(), hid=pyb.hid_mouse, high_speed=False)

  If called with no arguments, return the current USB mode as a string.

  If called with modestr provided, attempts to configure the USB mode. The following values of modestr are understood:

  - `None`: disables USB

  - `'VCP'`: enable with VCP (Virtual COM Port) interface

  - `'MSC'`: enable with MSC (mass storage device class) interface

  - `'VCP+MSC'`: enable with VCP and MSC

  - `'VCP+HID'`: enable with VCP and HID (human interface device)

  - `'VCP+MSC+HID'`: enabled with VCP, MSC and HID (only available on PYBD boards)

  For backwards compatibility, `'CDC'` is understood to mean `'VCP'` (and similarly for `'CDC+MSC'` and `'CDC+HID'`).

  The port parameter should be an integer (0, 1, …) and selects which USB port to use if the board supports multiple ports. A value of -1 uses the default or automatically selected port.

  The vid and pid parameters allow you to specify the VID (vendor id) and PID (product id). A pid value of -1 will select a PID based on the value of modestr.

  If enabling MSC mode, the msc parameter can be used to specify a list of SCSI LUNs to expose on the mass storage interface. For example `msc=(pyb.Flash(), pyb.SDCard())`.

  If enabling HID mode, you may also specify the HID details by passing the hid keyword parameter. It takes a tuple of (subclass, protocol, max packet length, polling interval, report descriptor). By default it will set appropriate values for a USB mouse. There is also a `pyb.hid_keyboard` constant, which is an appropriate tuple for a USB keyboard.

  The high_speed parameter, when set to `True`, enables USB HS mode if it is supported by the hardware.

## Constants

pyb.hid_mouse
pyb.hid_keyboard

  A tuple of (subclass, protocol, max packet length, polling interval, report descriptor) to set appropriate values for a USB mouse or keyboard.

## Classes

- class ADC – analog to digital conversion
- class CAN – controller area network communication bus
- class DAC – digital to analog conversion
- class ExtInt – configure I/O pins to interrupt on external events
- class Flash – access to built-in flash storage
- class I2C – a two-wire serial protocol
- class LED – LED object
- class Pin – control I/O pins
- class PinAF – Pin Alternate Functions
- class RTC – real time clock
- class Servo – 3-wire hobby servo driver
- class SPI – a controller-driven serial protocol
- class Timer – control internal timers
- class TimerChannel — setup a channel for a timer
- class UART – duplex serial communication bus
- class USB_HID – USB Human Interface Device (HID)
- class USB_VCP – USB virtual comm port

Language and External Links

Language
  English
  中文

External links
  openmv.io
  forums.openmv.io
  github.com/openmv/openmv
  micropython.org
  forum.micropython.org
  github.com/micropython/micropython