summaryrefslogtreecommitdiffstatshomepage
path: root/docs/library/pyb.rst
blob: 2fab24759290d72416bc6efb51f4c5d4cb12fb1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
:mod:`pyb` --- functions related to the pyboard
===============================================

.. module:: pyb
   :synopsis: functions related to the pyboard

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

Time related functions
----------------------

.. function:: delay(ms)

   Delay for the given number of milliseconds.

.. function:: udelay(us)

   Delay for the given number of microseconds.

.. function:: 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.

.. function:: 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.

.. function:: 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 upto about 12.4 days.
   
   Example::

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

.. function:: 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 upto about 17.8 minutes.
   
   Example::

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

Reset related functions
-----------------------

.. function:: hard_reset()

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

.. function:: bootloader()

   Activate the bootloader without BOOT\* pins.

Interrupt related functions
---------------------------

.. function:: 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.

.. function:: 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
-----------------------

.. function:: freq([sysclk[, hclk[, pclk1[, pclk2]]]])

   If given no arguments, returns a tuple of clock frequencies:
   (sysclk, hclk, pclk1, pclk2).
   These correspond to:

    - sysclk: frequency of the CPU
    - hclk: frequency of the AHB bus, core memory and DMA
    - pclk1: frequency of the APB1 bus
    - pclk2: frequency of the APB2 bus

   If given any arguments then the function sets the frequency of the CPU,
   and the busses if additional arguments are given.  Frequencies are given in
   Hz.  Eg freq(120000000) sets sysclk (the CPU frequency) to 120MHz.  Note that
   not all values are supported and the largest supported frequency not greater
   than the given value will be selected.

   Supported sysclk frequencies are (in MHz): 8, 16, 24, 30, 32, 36, 40, 42, 48,
   54, 56, 60, 64, 72, 84, 96, 108, 120, 144, 168.

   The hclk, pclk1 and pclk2 frequencies are derived from the sysclk frequency
   using a prescaler (divider).  Supported prescalers for hclk are: 1, 2, 4, 8,
   16, 64, 128, 256, 512.  Supported prescalers for pclk1 and pclk2 are: 1, 2,
   4, 8.  A prescaler will be chosen to best match the requested frequency.

   A sysclk frequency of
   8MHz uses the HSE (external crystal) directly and 16MHz uses the HSI
   (internal oscillator) directly.  The higher frequencies use the HSE to
   drive the PLL (phase locked loop), and then use the output of the PLL.

   Note that if you change the frequency while the USB is enabled then
   the USB may become unreliable.  It is best to change the frequency
   in boot.py, before the USB peripheral is started.  Also note that sysclk
   frequencies below 36MHz do not allow the USB to function correctly.

.. function:: wfi()

   Wait for an interrupt.
   This executies a ``wfi`` instruction which reduces power consumption
   of the MCU until an interrupt occurs, at which point execution continues.

.. function:: standby()


.. function:: stop()

Miscellaneous functions
-----------------------

.. function:: have_cdc()

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

.. function:: 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.

.. function:: info([dump_alloc_table])

   Print out lots of information about the board.

.. function:: mount(device, mountpoint, \*, readonly=False, mkfs=False)

   Mount a block device and make it available as part of the filesystem.
   ``device`` must be an object that provides the block protocol:

    - ``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.

   To unmount a device, pass ``None`` as the device and the mount location
   as ``mountpoint``.

.. function:: repl_uart(uart)

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

.. function:: rng()

   Return a 30-bit hardware generated random number.

.. function:: sync()

   Sync all file systems.

.. function:: unique_id()

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

Classes
-------

.. toctree::
   :maxdepth: 1

   pyb.Accel.rst
   pyb.ADC.rst
   pyb.CAN.rst
   pyb.DAC.rst
   pyb.ExtInt.rst
   pyb.I2C.rst
   pyb.LCD.rst
   pyb.LED.rst
   pyb.Pin.rst
   pyb.RTC.rst
   pyb.Servo.rst
   pyb.SPI.rst
   pyb.Switch.rst
   pyb.Timer.rst
   pyb.UART.rst
   pyb.USB_VCP.rst