machine.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. :mod:`machine` --- functions related to the hardware
  2. ====================================================
  3. .. module:: machine
  4. :synopsis: functions related to the hardware
  5. The ``machine`` module contains specific functions related to the hardware
  6. on a particular board. Most functions in this module allow to achieve direct
  7. and unrestricted access to and control of hardware blocks on a system
  8. (like CPU, timers, buses, etc.). Used incorrectly, this can lead to
  9. malfunction, lockups, crashes of your board, and in extreme cases, hardware
  10. damage.
  11. .. _machine_callbacks:
  12. A note of callbacks used by functions and class methods of :mod:`machine` module:
  13. all these callbacks should be considered as executing in an interrupt context.
  14. This is true for both physical devices with IDs >= 0 and "virtual" devices
  15. with negative IDs like -1 (these "virtual" devices are still thin shims on
  16. top of real hardware and real hardware interrupts). See :ref:`isr_rules`.
  17. Reset related functions
  18. -----------------------
  19. .. function:: reset()
  20. Resets the device in a manner similar to pushing the external RESET
  21. button.
  22. .. function:: reset_cause()
  23. Get the reset cause. See :ref:`constants <machine_constants>` for the possible return values.
  24. Interrupt related functions
  25. ---------------------------
  26. .. function:: disable_irq()
  27. Disable interrupt requests.
  28. Returns the previous IRQ state which should be considered an opaque value.
  29. This return value should be passed to the `enable_irq()` function to restore
  30. interrupts to their original state, before `disable_irq()` was called.
  31. .. function:: enable_irq(state)
  32. Re-enable interrupt requests.
  33. The *state* parameter should be the value that was returned from the most
  34. recent call to the `disable_irq()` function.
  35. Power related functions
  36. -----------------------
  37. .. function:: freq()
  38. Returns CPU frequency in hertz.
  39. .. function:: idle()
  40. Gates the clock to the CPU, useful to reduce power consumption at any time during
  41. short or long periods. Peripherals continue working and execution resumes as soon
  42. as any interrupt is triggered (on many ports this includes system timer
  43. interrupt occurring at regular intervals on the order of millisecond).
  44. .. function:: sleep()
  45. Stops the CPU and disables all peripherals except for WLAN. Execution is resumed from
  46. the point where the sleep was requested. For wake up to actually happen, wake sources
  47. should be configured first.
  48. .. function:: deepsleep()
  49. Stops the CPU and all peripherals (including networking interfaces, if any). Execution
  50. is resumed from the main script, just as with a reset. The reset cause can be checked
  51. to know that we are coming from `machine.DEEPSLEEP`. For wake up to actually happen,
  52. wake sources should be configured first, like `Pin` change or `RTC` timeout.
  53. .. function:: wake_reason()
  54. Get the wake reason. See :ref:`constants <machine_constants>` for the possible return values.
  55. Availability: ESP32, WiPy.
  56. Miscellaneous functions
  57. -----------------------
  58. .. function:: unique_id()
  59. Returns a byte string with a unique identifier of a board/SoC. It will vary
  60. from a board/SoC instance to another, if underlying hardware allows. Length
  61. varies by hardware (so use substring of a full value if you expect a short
  62. ID). In some MicroPython ports, ID corresponds to the network MAC address.
  63. .. function:: time_pulse_us(pin, pulse_level, timeout_us=1000000)
  64. Time a pulse on the given *pin*, and return the duration of the pulse in
  65. microseconds. The *pulse_level* argument should be 0 to time a low pulse
  66. or 1 to time a high pulse.
  67. If the current input value of the pin is different to *pulse_level*,
  68. the function first (*) waits until the pin input becomes equal to *pulse_level*,
  69. then (**) times the duration that the pin is equal to *pulse_level*.
  70. If the pin is already equal to *pulse_level* then timing starts straight away.
  71. The function will return -2 if there was timeout waiting for condition marked
  72. (*) above, and -1 if there was timeout during the main measurement, marked (**)
  73. above. The timeout is the same for both cases and given by *timeout_us* (which
  74. is in microseconds).
  75. .. function:: rng()
  76. Return a 24-bit software generated random number.
  77. Availability: WiPy.
  78. .. _machine_constants:
  79. Constants
  80. ---------
  81. .. data:: machine.IDLE
  82. machine.SLEEP
  83. machine.DEEPSLEEP
  84. IRQ wake values.
  85. .. data:: machine.PWRON_RESET
  86. machine.HARD_RESET
  87. machine.WDT_RESET
  88. machine.DEEPSLEEP_RESET
  89. machine.SOFT_RESET
  90. Reset causes.
  91. .. data:: machine.WLAN_WAKE
  92. machine.PIN_WAKE
  93. machine.RTC_WAKE
  94. Wake-up reasons.
  95. Classes
  96. -------
  97. .. toctree::
  98. :maxdepth: 1
  99. machine.Pin.rst
  100. machine.Signal.rst
  101. machine.ADC.rst
  102. machine.UART.rst
  103. machine.SPI.rst
  104. machine.I2C.rst
  105. machine.RTC.rst
  106. machine.Timer.rst
  107. machine.WDT.rst
  108. machine.SD.rst