Salted Fish Micropython— machine library

Provide hardware-related functions

This module contains various functions related to specific hardware, it can directly access the system's hardware functions (such as CPU, timer, bus, etc.) without restrictions. If used improperly, it will cause system failure, crash, crash, or even hardware damage. (BOOM)
When using callback functions, please note that all callback functions will run in the interrupt, including real devices (ID greater than 0) and virtual devices (ID is -1). For more information, please refer to the section on writing interrupt handlers.

Reset related functions
function Explanation
machine.reset() The function of this function is to reset the system as if the reset key was pressed. Note that when using USB_VCP as the REPL, the USB connection will be lost when executing this function
machine.reset_cause() Reason for getting reset

Reset related constants

Reason for reset Explanation
machine.PWRON_RESET Power reset
machine.HARD_RESET Hard reset (power off)
machine.WDT_RESET Watchdog reset
machine.DEEPSLEEP_RESET Deep sleeping
machine.SOFT_RESET Soft reset
Interrupt related functions
function Explanation
machine.disable_irq() Interrupts are prohibited. The return value is the IRQ state before calling the function. It can be used as a parameter of the enable_irq () function to restore the previous interrupt state.
machine.enable_irq(state) Allow (resume) interruption. The state parameter should be obtained by the last call to the disable_irq () function.

Interrupt wakeup constant

Interrupt wake-up value Explanation
machine.IDLE CPU clock stopped
machine.SLEEP Sleep
machine.DEEPSLEEP Deep sleep
Power management function
function Explanation
machine.freq() Returns the CPU frequency (Hz).
machine.idle() Stop the CPU clock to reduce system power consumption. Peripherals continue to work and resume operation after any interruption occurs (in most versions, the system timer interrupt is included).
machine.sleep() Stop the CPU and disable all peripherals except WLAN (if present).
machine.deepsleep() Stop the CPU and all peripherals (including the network). After waking up, it starts to run from main, just like reset, you can see where to run from reset_cause.

annotation:

Not recommended for .sleep()use .lightsleep ()in place of function without arguments
to stop execution when trying to enter a low power state, if the specified time ms, then this will be based on milliseconds maximum duration of sleep units.
Regardless of whether there is a delay, if there is an event that needs to be processed, execution can be resumed at any time. These events or wakeup sources should be configured before sleep , such as pin change or rtc timeout.
The precise behavior and energy saving capabilities of lightsleep and deepsleep are highly dependent on the underlying hardware, and the general characteristics are:

  • lightsleep has complete memory and state retention. When the wake-up is executed, all sub-system operations are resumed from where sleep is requested.
  • deepsleep may not retain memory or any other state of the system (such as peripherals or network interfaces). The wake-up execution is resumed from the main script, similar to a hard or power reset. Function reset cause ()returns machine.deepsleep, which can be used to distinguish and other deepsleep wake reset.

Wakeup constant

Reason for wake up Explanation
machine.WLAN_WAKE WLAN
machine.PIN_WAKE Pin level
machine.RTC_WAKE RTC timeout
Other functions

machine.unique_id()

Get the unique serial number of the board / SoC. If the underlying hardware allows this function, the ID of each board is different, and the length of the ID is determined by the hardware (if you want to use a shorter id, you can use a substring of the full value). In some micro-python ports, the network MAC address is used to represent the ID.

For example: I use pyboard. Some friends can try it (I also tried K210)

#pyboard显示
machine.unique_id()
b'L\x00@\x00\x05PMMN97 '
#下面的图是K210的

Insert picture description here
machine.time_pulse_us(pin, pulse_level, timeout_us=1000000)

Output a pulse on the specified pin and return the pulse duration (microseconds). The pulse level parameter should be 0 to time low pulse or 1 to time high pulse.
If the level on the pin and pulse_level are different, it will wait for the same level before starting the measurement, otherwise it will immediately start measuring the pulse time. The function will raise an ETIMEDOUT exception after the wait time expires.
If the timeout occurs while waiting for the same level, the function will return -2; if the measurement pulse times out, it will return -1. The timeout period in both cases is set by the timeout_us parameter.

Related class

Here are some of the classes included in the machine module and how to use them. Some functions are similar to the pyb module, but not exactly the same

class Explanation
class Pin – control I/O pins IO control
class UART – duplex serial communication bus Duplex serial communication bus
class I2C – a two-wire serial protocol Two-wire serial protocol
class RTC – real time clock Real Time Clock
class Timer – control hardware timers Control hardware timer
class WDT – watchdog timer Watchdog timer
class SPI – a Serial Peripheral Interface bus protocol (master side) Serial port external device bus (SPI, master device side)
class SD – secure digital memory card (cc3200 port only) Only cc3200 port memory card
class SDCard – secure digital memory card Secure digital memory card

References:
micropython.org: docs.micropython.org/en

Please correct me if something is wrong

Published 166 original articles · 22 praises · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_45020839/article/details/102482582