2017-01-28 09:08:25 +00:00
|
|
|
:mod:`machine` --- functions related to the hardware
|
|
|
|
====================================================
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. module:: machine
|
2017-01-28 09:08:25 +00:00
|
|
|
:synopsis: functions related to the hardware
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2017-01-28 09:08:25 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
.. _machine_callbacks:
|
|
|
|
|
|
|
|
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 intrerrupts). See :ref:`isr_rules`.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
Reset related functions
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
.. function:: reset()
|
|
|
|
|
2016-04-03 18:40:27 +01:00
|
|
|
Resets the device in a manner similar to pushing the external RESET
|
2015-10-14 11:32:01 +01:00
|
|
|
button.
|
|
|
|
|
2016-04-17 15:40:08 +01:00
|
|
|
.. function:: reset_cause()
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-04-17 15:40:08 +01:00
|
|
|
Get the reset cause. See :ref:`constants <machine_constants>` for the possible return values.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-20 13:31:44 +01:00
|
|
|
Interrupt related functions
|
|
|
|
---------------------------
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-20 13:31:44 +01:00
|
|
|
.. function:: disable_irq()
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-20 13:31:44 +01:00
|
|
|
Disable interrupt requests.
|
2016-09-23 04:15:58 +01:00
|
|
|
Returns the previous IRQ state which should be considered an opaque value.
|
|
|
|
This return value should be passed to the ``enable_irq`` function to restore
|
|
|
|
interrupts to their original state, before ``disable_irq`` was called.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-09-23 04:15:58 +01:00
|
|
|
.. function:: enable_irq(state)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-09-23 04:15:58 +01:00
|
|
|
Re-enable interrupt requests.
|
|
|
|
The ``state`` parameter should be the value that was returned from the most
|
|
|
|
recent call to the ``disable_irq`` function.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
Power related functions
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
.. function:: freq()
|
|
|
|
|
2016-04-15 15:06:11 +01:00
|
|
|
.. only:: not port_wipy
|
|
|
|
|
|
|
|
Returns CPU frequency in hertz.
|
|
|
|
|
|
|
|
.. only:: port_wipy
|
|
|
|
|
|
|
|
Returns a tuple of clock frequencies: ``(sysclk,)``
|
|
|
|
These correspond to:
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-04-15 15:06:11 +01:00
|
|
|
- sysclk: frequency of the CPU
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. function:: idle()
|
|
|
|
|
|
|
|
Gates the clock to the CPU, useful to reduce power consumption at any time during
|
|
|
|
short or long periods. Peripherals continue working and execution resumes as soon
|
2016-05-03 10:53:57 +01:00
|
|
|
as any interrupt is triggered (on many ports this includes system timer
|
2016-08-01 00:52:00 +01:00
|
|
|
interrupt occurring at regular intervals on the order of millisecond).
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. function:: sleep()
|
|
|
|
|
|
|
|
Stops the CPU and disables all peripherals except for WLAN. Execution is resumed from
|
2016-05-03 10:48:20 +01:00
|
|
|
the point where the sleep was requested. For wake up to actually happen, wake sources
|
|
|
|
should be configured first.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. function:: deepsleep()
|
|
|
|
|
2016-05-03 10:48:20 +01:00
|
|
|
Stops the CPU and all peripherals (including networking interfaces, if any). Execution
|
|
|
|
is resumed from the main script, just as with a reset. The reset cause can be checked
|
|
|
|
to know that we are coming from ``machine.DEEPSLEEP``. For wake up to actually happen,
|
|
|
|
wake sources should be configured first, like ``Pin`` change or ``RTC`` timeout.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
.. only:: port_wipy
|
|
|
|
|
|
|
|
.. function:: wake_reason()
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
Get the wake reason. See :ref:`constants <machine_constants>` for the possible return values.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
Miscellaneous functions
|
|
|
|
-----------------------
|
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
.. only:: port_wipy
|
|
|
|
|
|
|
|
.. function:: main(filename)
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
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.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
It only makes sense to call this function from within boot.py.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
.. function:: rng()
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-03 10:15:29 +01:00
|
|
|
Return a 24-bit software generated random number.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
|
|
|
.. function:: unique_id()
|
|
|
|
|
2016-08-01 00:52:00 +01:00
|
|
|
Returns a byte string with a unique identifier of a board/SoC. It will vary
|
2016-05-03 10:15:29 +01:00
|
|
|
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.
|
2015-10-14 11:32:01 +01:00
|
|
|
|
2016-05-31 14:06:33 +01:00
|
|
|
.. function:: 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.
|
|
|
|
|
|
|
|
The function first waits while the pin input is different to the `pulse_level`
|
|
|
|
parameter, 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 raise an OSError with ETIMEDOUT if either of the waits is
|
|
|
|
longer than the given timeout value (which is in microseconds).
|
|
|
|
|
2015-10-14 11:32:01 +01:00
|
|
|
.. _machine_constants:
|
|
|
|
|
|
|
|
Constants
|
|
|
|
---------
|
|
|
|
|
|
|
|
.. data:: machine.IDLE
|
|
|
|
.. data:: machine.SLEEP
|
|
|
|
.. data:: machine.DEEPSLEEP
|
|
|
|
|
|
|
|
irq wake values
|
|
|
|
|
2016-09-08 03:50:38 +01:00
|
|
|
.. data:: machine.PWRON_RESET
|
2015-10-14 11:32:01 +01:00
|
|
|
.. data:: machine.HARD_RESET
|
|
|
|
.. data:: machine.WDT_RESET
|
|
|
|
.. data:: machine.DEEPSLEEP_RESET
|
|
|
|
.. data:: machine.SOFT_RESET
|
|
|
|
|
|
|
|
reset causes
|
|
|
|
|
|
|
|
.. data:: machine.WLAN_WAKE
|
|
|
|
.. data:: machine.PIN_WAKE
|
|
|
|
.. data:: machine.RTC_WAKE
|
|
|
|
|
|
|
|
wake reasons
|
|
|
|
|
|
|
|
Classes
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. toctree::
|
|
|
|
:maxdepth: 1
|
|
|
|
|
|
|
|
machine.ADC.rst
|
|
|
|
machine.I2C.rst
|
|
|
|
machine.Pin.rst
|
|
|
|
machine.RTC.rst
|
|
|
|
machine.SD.rst
|
|
|
|
machine.SPI.rst
|
|
|
|
machine.Timer.rst
|
|
|
|
machine.UART.rst
|
|
|
|
machine.WDT.rst
|