pyb.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. :mod:`pyb` --- functions related to the board
  2. =============================================
  3. .. module:: pyb
  4. :synopsis: functions related to the board
  5. The ``pyb`` module contains specific functions related to the board.
  6. Time related functions
  7. ----------------------
  8. .. function:: delay(ms)
  9. Delay for the given number of milliseconds.
  10. .. function:: udelay(us)
  11. Delay for the given number of microseconds.
  12. .. function:: millis()
  13. Returns the number of milliseconds since the board was last reset.
  14. The result is always a MicroPython smallint (31-bit signed number), so
  15. after 2^30 milliseconds (about 12.4 days) this will start to return
  16. negative numbers.
  17. Note that if :meth:`pyb.stop()` is issued the hardware counter supporting this
  18. function will pause for the duration of the "sleeping" state. This
  19. will affect the outcome of :meth:`pyb.elapsed_millis()`.
  20. .. function:: micros()
  21. Returns the number of microseconds since the board was last reset.
  22. The result is always a MicroPython smallint (31-bit signed number), so
  23. after 2^30 microseconds (about 17.8 minutes) this will start to return
  24. negative numbers.
  25. Note that if :meth:`pyb.stop()` is issued the hardware counter supporting this
  26. function will pause for the duration of the "sleeping" state. This
  27. will affect the outcome of :meth:`pyb.elapsed_micros()`.
  28. .. function:: elapsed_millis(start)
  29. Returns the number of milliseconds which have elapsed since ``start``.
  30. This function takes care of counter wrap, and always returns a positive
  31. number. This means it can be used to measure periods up to about 12.4 days.
  32. Example::
  33. start = pyb.millis()
  34. while pyb.elapsed_millis(start) < 1000:
  35. # Perform some operation
  36. .. function:: elapsed_micros(start)
  37. Returns the number of microseconds which have elapsed since ``start``.
  38. This function takes care of counter wrap, and always returns a positive
  39. number. This means it can be used to measure periods up to about 17.8 minutes.
  40. Example::
  41. start = pyb.micros()
  42. while pyb.elapsed_micros(start) < 1000:
  43. # Perform some operation
  44. pass
  45. Reset related functions
  46. -----------------------
  47. .. function:: hard_reset()
  48. Resets the pyboard in a manner similar to pushing the external RESET
  49. button.
  50. .. function:: bootloader()
  51. Activate the bootloader without BOOT\* pins.
  52. .. function:: fault_debug(value)
  53. Enable or disable hard-fault debugging. A hard-fault is when there is a fatal
  54. error in the underlying system, like an invalid memory access.
  55. If the *value* argument is ``False`` then the board will automatically reset if
  56. there is a hard fault.
  57. If *value* is ``True`` then, when the board has a hard fault, it will print the
  58. registers and the stack trace, and then cycle the LEDs indefinitely.
  59. The default value is disabled, i.e. to automatically reset.
  60. Interrupt related functions
  61. ---------------------------
  62. .. function:: disable_irq()
  63. Disable interrupt requests.
  64. Returns the previous IRQ state: ``False``/``True`` for disabled/enabled IRQs
  65. respectively. This return value can be passed to enable_irq to restore
  66. the IRQ to its original state.
  67. .. function:: enable_irq(state=True)
  68. Enable interrupt requests.
  69. If ``state`` is ``True`` (the default value) then IRQs are enabled.
  70. If ``state`` is ``False`` then IRQs are disabled. The most common use of
  71. this function is to pass it the value returned by ``disable_irq`` to
  72. exit a critical section.
  73. Power related functions
  74. -----------------------
  75. .. function:: freq([sysclk[, hclk[, pclk1[, pclk2]]]])
  76. If given no arguments, returns a tuple of clock frequencies:
  77. (sysclk, hclk, pclk1, pclk2).
  78. These correspond to:
  79. - sysclk: frequency of the CPU
  80. - hclk: frequency of the AHB bus, core memory and DMA
  81. - pclk1: frequency of the APB1 bus
  82. - pclk2: frequency of the APB2 bus
  83. If given any arguments then the function sets the frequency of the CPU,
  84. and the busses if additional arguments are given. Frequencies are given in
  85. Hz. Eg freq(120000000) sets sysclk (the CPU frequency) to 120MHz. Note that
  86. not all values are supported and the largest supported frequency not greater
  87. than the given value will be selected.
  88. Supported sysclk frequencies are (in MHz): 8, 16, 24, 30, 32, 36, 40, 42, 48,
  89. 54, 56, 60, 64, 72, 84, 96, 108, 120, 144, 168.
  90. The maximum frequency of hclk is 168MHz, of pclk1 is 42MHz, and of pclk2 is
  91. 84MHz. Be sure not to set frequencies above these values.
  92. The hclk, pclk1 and pclk2 frequencies are derived from the sysclk frequency
  93. using a prescaler (divider). Supported prescalers for hclk are: 1, 2, 4, 8,
  94. 16, 64, 128, 256, 512. Supported prescalers for pclk1 and pclk2 are: 1, 2,
  95. 4, 8. A prescaler will be chosen to best match the requested frequency.
  96. A sysclk frequency of
  97. 8MHz uses the HSE (external crystal) directly and 16MHz uses the HSI
  98. (internal oscillator) directly. The higher frequencies use the HSE to
  99. drive the PLL (phase locked loop), and then use the output of the PLL.
  100. Note that if you change the frequency while the USB is enabled then
  101. the USB may become unreliable. It is best to change the frequency
  102. in boot.py, before the USB peripheral is started. Also note that sysclk
  103. frequencies below 36MHz do not allow the USB to function correctly.
  104. .. function:: wfi()
  105. Wait for an internal or external interrupt.
  106. This executes a ``wfi`` instruction which reduces power consumption
  107. of the MCU until any interrupt occurs (be it internal or external),
  108. at which point execution continues. Note that the system-tick interrupt
  109. occurs once every millisecond (1000Hz) so this function will block for
  110. at most 1ms.
  111. .. function:: stop()
  112. Put the pyboard in a "sleeping" state.
  113. This reduces power consumption to less than 500 uA. To wake from this
  114. sleep state requires an external interrupt or a real-time-clock event.
  115. Upon waking execution continues where it left off.
  116. See :meth:`rtc.wakeup` to configure a real-time-clock wakeup event.
  117. .. function:: standby()
  118. Put the pyboard into a "deep sleep" state.
  119. This reduces power consumption to less than 50 uA. To wake from this
  120. sleep state requires a real-time-clock event, or an external interrupt
  121. on X1 (PA0=WKUP) or X18 (PC13=TAMP1).
  122. Upon waking the system undergoes a hard reset.
  123. See :meth:`rtc.wakeup` to configure a real-time-clock wakeup event.
  124. Miscellaneous functions
  125. -----------------------
  126. .. function:: have_cdc()
  127. Return True if USB is connected as a serial device, False otherwise.
  128. .. note:: This function is deprecated. Use pyb.USB_VCP().isconnected() instead.
  129. .. function:: hid((buttons, x, y, z))
  130. Takes a 4-tuple (or list) and sends it to the USB host (the PC) to
  131. signal a HID mouse-motion event.
  132. .. note:: This function is deprecated. Use :meth:`pyb.USB_HID.send()` instead.
  133. .. function:: info([dump_alloc_table])
  134. Print out lots of information about the board.
  135. .. function:: main(filename)
  136. Set the filename of the main script to run after boot.py is finished. If
  137. this function is not called then the default file main.py will be executed.
  138. It only makes sense to call this function from within boot.py.
  139. .. function:: mount(device, mountpoint, \*, readonly=False, mkfs=False)
  140. Mount a block device and make it available as part of the filesystem.
  141. ``device`` must be an object that provides the block protocol:
  142. - ``readblocks(self, blocknum, buf)``
  143. - ``writeblocks(self, blocknum, buf)`` (optional)
  144. - ``count(self)``
  145. - ``sync(self)`` (optional)
  146. ``readblocks`` and ``writeblocks`` should copy data between ``buf`` and
  147. the block device, starting from block number ``blocknum`` on the device.
  148. ``buf`` will be a bytearray with length a multiple of 512. If
  149. ``writeblocks`` is not defined then the device is mounted read-only.
  150. The return value of these two functions is ignored.
  151. ``count`` should return the number of blocks available on the device.
  152. ``sync``, if implemented, should sync the data on the device.
  153. The parameter ``mountpoint`` is the location in the root of the filesystem
  154. to mount the device. It must begin with a forward-slash.
  155. If ``readonly`` is ``True``, then the device is mounted read-only,
  156. otherwise it is mounted read-write.
  157. If ``mkfs`` is ``True``, then a new filesystem is created if one does not
  158. already exist.
  159. To unmount a device, pass ``None`` as the device and the mount location
  160. as ``mountpoint``.
  161. .. function:: repl_uart(uart)
  162. Get or set the UART object where the REPL is repeated on.
  163. .. function:: rng()
  164. Return a 30-bit hardware generated random number.
  165. .. function:: sync()
  166. Sync all file systems.
  167. .. function:: unique_id()
  168. Returns a string of 12 bytes (96 bits), which is the unique ID of the MCU.
  169. .. function:: usb_mode([modestr], vid=0xf055, pid=0x9801, hid=pyb.hid_mouse)
  170. If called with no arguments, return the current USB mode as a string.
  171. If called with ``modestr`` provided, attempts to set USB mode.
  172. This can only be done when called from ``boot.py`` before
  173. :meth:`pyb.main()` has been called. The following values of
  174. ``modestr`` are understood:
  175. - ``None``: disables USB
  176. - ``'VCP'``: enable with VCP (Virtual COM Port) interface
  177. - ``'MSC'``: enable with MSC (mass storage device class) interface
  178. - ``'VCP+MSC'``: enable with VCP and MSC
  179. - ``'VCP+HID'``: enable with VCP and HID (human interface device)
  180. For backwards compatibility, ``'CDC'`` is understood to mean
  181. ``'VCP'`` (and similarly for ``'CDC+MSC'`` and ``'CDC+HID'``).
  182. The ``vid`` and ``pid`` parameters allow you to specify the VID
  183. (vendor id) and PID (product id).
  184. If enabling HID mode, you may also specify the HID details by
  185. passing the ``hid`` keyword parameter. It takes a tuple of
  186. (subclass, protocol, max packet length, polling interval, report
  187. descriptor). By default it will set appropriate values for a USB
  188. mouse. There is also a ``pyb.hid_keyboard`` constant, which is an
  189. appropriate tuple for a USB keyboard.
  190. Classes
  191. -------
  192. .. toctree::
  193. :maxdepth: 1
  194. pyb.Accel.rst
  195. pyb.ADC.rst
  196. pyb.CAN.rst
  197. pyb.DAC.rst
  198. pyb.ExtInt.rst
  199. pyb.I2C.rst
  200. pyb.LCD.rst
  201. pyb.LED.rst
  202. pyb.Pin.rst
  203. pyb.RTC.rst
  204. pyb.Servo.rst
  205. pyb.SPI.rst
  206. pyb.Switch.rst
  207. pyb.Timer.rst
  208. pyb.UART.rst
  209. pyb.USB_HID.rst
  210. pyb.USB_VCP.rst