machine.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. """The ``machine`` module contains specific functions related to the hardware
  2. on a particular board. Most functions in this module allow to achieve
  3. direct and unrestricted access to and control of hardware blocks on a
  4. system (like CPU, timers, buses, etc.). Used incorrectly, this can
  5. lead to malfunction, lockups, crashes of your board, and in extreme
  6. cases, hardware damage.
  7. A note of callbacks used by functions and class methods of machine
  8. module: all these callbacks should be considered as executing in an
  9. interrupt context. This is true for both physical devices with
  10. IDs >= 0 and “virtual” devices with negative IDs like -1 (these
  11. “virtual” devices are still thin shims on top of real hardware
  12. and real hardware interrupts).
  13. """
  14. from typing import Callable, Optional, Collection, Union, Any
  15. IDLE = ... # type: int
  16. SLEEP = ... # type: int
  17. DEEPSLEEP = ... # type: int
  18. PWRON_RESET = ... # type: int
  19. HARD_RESET = ... # type: int
  20. WDT_RESET = ... # type: int
  21. DEEPSLEEP_RESET = ... # type: int
  22. PIN_WAKE = ... # type: int
  23. RTC_WAKE = ... # type: int
  24. class Pin(object):
  25. """A pin object is used to control I/O pins (also known as GPIO - general-purpose
  26. input/output). Pin objects are commonly associated with a physical pin that can
  27. drive an output voltage and read input voltages. The pin class has methods to set the mode of
  28. the pin (IN, OUT, etc) and methods to get and set the digital logic level.
  29. For analog control of a pin, see the :class:`ADC` class.
  30. A pin object is constructed by using an identifier which unambiguously
  31. specifies a certain I/O pin. The allowed forms of the identifier and the
  32. physical pin that the identifier maps to are port-specific. Possibilities
  33. for the identifier are an integer, a string or a tuple with port and pin
  34. number.
  35. Usage Model::
  36. from machine import Pin
  37. # create an output pin on pin #0
  38. p0 = Pin(0, Pin.OUT)
  39. # set the value low then high
  40. p0.value(0)
  41. p0.value(1)
  42. # create an input pin on pin #2, with a pull up resistor
  43. p2 = Pin(2, Pin.IN, Pin.PULL_UP)
  44. # read and print the pin value
  45. print(p2.value())
  46. # reconfigure pin #0 in input mode
  47. p0.mode(p0.IN)
  48. # configure an irq callback
  49. p0.irq(lambda p:print(p))
  50. """
  51. IRQ_FALLING = ... # type: int
  52. IRQ_RISING = ... # type: int
  53. IRQ_LOWLEVEL = ... # type: int
  54. IRQ_HIGHLEVEL = ... # type: int
  55. IN = ... # type: int
  56. OUT = ... # type: int
  57. OPEN_DRAIN = ... # type: int
  58. PULL_UP = ... # type: int
  59. PULL_DOWN = ... # type: int
  60. LOW_POWER = ... # type: int
  61. MED_POWER = ... # type: int
  62. HIGH_POWER = ... # type: int
  63. def __init__(self, id: Any, mode: int = -1, pull: int = -1, *,
  64. value: Optional[int] = None,
  65. drive: Optional[int] = None,
  66. alt: Optional[int] = None) -> None:
  67. """Access the pin peripheral (GPIO pin) associated with the given ``id``. If
  68. additional arguments are given in the constructor then they are used to initialise
  69. the pin. Any settings that are not specified will remain in their previous state.
  70. The arguments are:
  71. - ``id`` is mandatory and can be an arbitrary object. Among possible value
  72. types are: int (an internal Pin identifier), str (a Pin name), and tuple
  73. (pair of [port, pin]).
  74. - ``mode`` specifies the pin mode, which can be one of:
  75. - ``Pin.IN`` - Pin is configured for input. If viewed as an output the pin
  76. is in high-impedance state.
  77. - ``Pin.OUT`` - Pin is configured for (normal) output.
  78. - ``Pin.OPEN_DRAIN`` - Pin is configured for open-drain output. Open-drain
  79. output works in the following way: if the output value is set to 0 the pin
  80. is active at a low level; if the output value is 1 the pin is in a high-impedance
  81. state. Not all ports implement this mode, or some might only on certain pins.
  82. - ``Pin.ALT`` - Pin is configured to perform an alternative function, which is
  83. port specific. For a pin configured in such a way any other Pin methods
  84. (except :meth:`Pin.init`) are not applicable (calling them will lead to undefined,
  85. or a hardware-specific, result). Not all ports implement this mode.
  86. - ``Pin.ALT_OPEN_DRAIN`` - The Same as ``Pin.ALT``, but the pin is configured as
  87. open-drain. Not all ports implement this mode.
  88. - ``pull`` specifies if the pin has a (weak) pull resistor attached, and can be
  89. one of:
  90. - ``None`` - No pull up or down resistor.
  91. - ``Pin.PULL_UP`` - Pull up resistor enabled.
  92. - ``Pin.PULL_DOWN`` - Pull down resistor enabled.
  93. - ``value`` is valid only for Pin.OUT and Pin.OPEN_DRAIN modes and specifies initial
  94. output pin value if given, otherwise the state of the pin peripheral remains
  95. unchanged.
  96. - ``drive`` specifies the output power of the pin and can be one of: ``Pin.LOW_POWER``,
  97. ``Pin.MED_POWER`` or ``Pin.HIGH_POWER``. The actual current driving capabilities
  98. are port dependent. Not all ports implement this argument.
  99. - ``alt`` specifies an alternate function for the pin and the values it can take are
  100. port dependent. This argument is valid only for ``Pin.ALT`` and ``Pin.ALT_OPEN_DRAIN``
  101. modes. It may be used when a pin supports more than one alternate function. If only
  102. one pin alternate function is supported the this argument is not required. Not all
  103. ports implement this argument.
  104. As specified above, the Pin class allows to set an alternate function for a particular
  105. pin, but it does not specify any further operations on such a pin. Pins configured in
  106. alternate-function mode are usually not used as GPIO but are instead driven by other
  107. hardware peripherals. The only operation supported on such a pin is re-initialising,
  108. by calling the constructor or :meth:`Pin.init` method. If a pin that is configured in
  109. alternate-function mode is re-initialised with ``Pin.IN``, ``Pin.OUT``, or
  110. ``Pin.OPEN_DRAIN``, the alternate function will be removed from the pin. """
  111. ...
  112. def init(self, value: int, drive: int, alt: int, mode: int = -1, pull: int = -1) -> None:
  113. """Re-initialise the pin using the given parameters. Only those arguments
  114. that are specified will be set. The rest of the pin peripheral state
  115. will remain unchanged. See the constructor documentation for details
  116. of the arguments.
  117. :param value: Initial output pin value.
  118. :param drive: Output power of the pin.
  119. :param alt: Alternate function for the pin.
  120. :param mode: Pin mode.
  121. :param pull: Flag that specifies if the pin has a (weak) pull resistor attached.
  122. """
  123. ...
  124. def value(self, x: Optional[int]) -> Optional[int]:
  125. """This method allows to set and get the value of the pin, depending on
  126. whether the argument **x** is supplied or not.
  127. If the argument is omitted then this method gets the digital logic
  128. level of the pin, returning 0 or 1 corresponding to low and high
  129. voltage signals respectively. The behaviour of this method depends
  130. on the mode of the pin:
  131. * ``Pin.IN`` - The method returns the actual input value currently present on the pin.
  132. * ``Pin.OUT`` - The behaviour and return value of the method is undefined.
  133. * ``Pin.OPEN_DRAIN`` - If the pin is in state ‘0’ then the behaviour and return value of the method is undefined. Otherwise, if the pin is in state ‘1’, the method returns the actual input value currently present on the pin.
  134. :param x: Value to set on a pin.
  135. :return: Current value of a pin.
  136. """
  137. ...
  138. def __call__(self, x: Optional[int]) -> Optional[int]:
  139. """Pin objects are callable. The call method provides a (fast) shortcut
  140. to set and get the value of the pin. It is equivalent to
  141. Pin.value([x]). See ``Pin.value()`` for more details.
  142. :param x: Value to set on a pin.
  143. :return: Current value of a pin.
  144. """
  145. ...
  146. def on(self) -> None:
  147. """Set pin to “1” output level."""
  148. ...
  149. def off(self) -> None:
  150. """Set pin to “0” output level."""
  151. ...
  152. def mode(self, mode: Optional[int]) -> Optional[int]:
  153. """Get or set the pin mode.
  154. **mode** can be one of following values:
  155. * ``Pin.IN`` - Pin is configured for input. If viewed as an output the pin is in high-impedance state.
  156. * ``Pin.OUT`` - Pin is configured for (normal) output.
  157. * ``Pin.OPEN_DRAIN`` - Pin is configured for open-drain output. Open-drain output works in the following way: if the output value is set to 0 the pin is active at a low level; if the output value is 1 the pin is in a high-impedance state. Not all ports implement this mode, or some might only on certain pins.
  158. * ``Pin.ALT`` - Pin is configured to perform an alternative function, which is port specific. For a pin configured in such a way any other Pin methods (except Pin.init()) are not applicable (calling them will lead to undefined, or a hardware-specific, result). Not all ports implement this mode.
  159. * ``Pin.ALT_OPEN_DRAIN`` - The Same as Pin.ALT, but the pin is configured as open-drain. Not all ports implement this mode.
  160. :param mode: Mode to be set on a pin.
  161. :return: Current mode on a pin.
  162. """
  163. ...
  164. def pull(self, pull: Optional[int]) -> Optional[int]:
  165. """Get or set the pin pull state.
  166. *pull* can be one of following values:
  167. * ``None`` - No pull up or down resistor.
  168. * ``Pin.PULL_UP`` - Pull up resistor enabled.
  169. * ``Pin.PULL_DOWN`` - Pull down resistor enabled.
  170. :param pull: Pull state.
  171. :return: Current pull state.
  172. """
  173. ...
  174. def irq(self, handler: Callable[[Pin], Any] = None, trigger: int = (IRQ_FALLING | IRQ_RISING),
  175. priority: int = 1, wake: int = None) -> Callable[[Pin], Any]:
  176. """
  177. Configure an interrupt handler to be called when the trigger source
  178. of the pin is active.
  179. If the pin mode is ``Pin.IN`` then the trigger
  180. source is the external value on the pin.
  181. If the pin mode is ``Pin.OUT`` then the trigger source is the output
  182. buffer of the pin.
  183. if the pin mode is ``Pin.OPEN_DRAIN`` then the trigger source is the
  184. output buffer for state ‘0’ and the external pin value for state ‘1’.
  185. Possible values for ``wake``:
  186. * ``machine.IDLE``
  187. * ``machine.SLEEP``
  188. * ``machine.DEEPSLEEP``
  189. Possible values for ``trigger``:
  190. * ``Pin.IRQ_FALLING`` - interrupt on falling edge.
  191. * ``Pin.IRQ_RISING`` - interrupt on rising edge.
  192. * ``Pin.IRQ_LOW_LEVEL`` - interrupt on low level.
  193. * ``Pin.IRQ_HIGH_LEVEL`` - interrupt on high level.
  194. These values can be OR’ed together to trigger on multiple events.
  195. :param handler: Interrupt handler.
  196. :param trigger: Event which can generate an interrupt
  197. :param priority: Priority level of the interrupt
  198. :param wake: Power mode in which this interrupt can wake up the system
  199. :return: Callback object.
  200. """
  201. ...
  202. class Signal(object):
  203. def __init__(self, pin_obj: Pin, invert: bool = False) -> None:
  204. """Create a Signal object.
  205. :param pin_obj: Existing Pin object.
  206. :param invert: If True, the signal will be inverted (active low).
  207. """
  208. ...
  209. def value(self, x: Optional[bool]) -> None:
  210. """This method allows to set and get the value of the signal, depending
  211. on whether the argument x is supplied or not.
  212. If the argument is omitted then this method gets the signal level, 1
  213. meaning signal is asserted (active) and 0 - signal inactive.
  214. If the argument is supplied then this method sets the signal level.
  215. The argument x can be anything that converts to a boolean. If it
  216. converts to True, the signal is active, otherwise it is inactive.
  217. Correspondence between signal being active and actual logic level
  218. on the underlying pin depends on whether signal is inverted
  219. (active-low) or not. For non-inverted signal, active status
  220. corresponds to logical 1, inactive - to logical 0. For
  221. inverted/active-low signal, active status corresponds to
  222. logical 0, while inactive - to logical 1.
  223. :param x: Signal level (active or not).
  224. :return: Signal level.
  225. :rtype: int
  226. """
  227. ...
  228. def on(self) -> None:
  229. """Activate signal."""
  230. ...
  231. def off(self) -> None:
  232. """Deactivate signal."""
  233. ...
  234. class UART(object):
  235. def __init__(self, id: int, baudrate: int = 115200) -> None:
  236. """Init UART object with a given baudrate.
  237. :param id: ID of UART "object" (either 0 or 1).
  238. :param baudrate: Rate of data transfer.
  239. """
  240. def init(self, baudrate: int, bits: int = 8, parity: Optional[int] = 0, stop: int = 1,
  241. timeout: int = 0, timeout_char: int = 0) -> None:
  242. """Init with a given parameters.
  243. :param baudrate: Baud rate, that specifies how fast data is sent over serial line.
  244. :param bits: Bit length of data packet (can be 7, 8 or 9 depending on parity).
  245. :param parity: Number of parity bits (can be 0 or 1).
  246. :param stop: Length of stop bit (can be 1 or 2).
  247. :param timeout: Timeout waiting for first char (in ms).
  248. :param timeout_char: Timeout waiting between chars (in ms).
  249. """
  250. ...
  251. def deinit(self) -> None:
  252. """Turn off the UART bus."""
  253. ...
  254. def any(self) -> int:
  255. """Returns an integer counting the number of characters that can be read
  256. without blocking. It will return 0 if there are no characters
  257. available and a positive number if there are characters. The method
  258. may return 1 even if there is more than one character available for reading.
  259. :return: Number of characters that can be read without blocking.
  260. """
  261. ...
  262. def read(self, nbytes: Optional[int]) -> bytes:
  263. """Read characters. If ``nbytes`` is specified then read at most that many
  264. bytes, otherwise read as much data as possible.
  265. :param nbytes: Upper limit on number of read characters.
  266. :return: Bytes read in.
  267. """
  268. ...
  269. def readinto(self, buf: bytearray, nbytes: Optional[int]) -> Optional[int]:
  270. """Read bytes into the ``buf``. If ``nbytes`` is specified then read at most
  271. that many bytes. Otherwise, read at most ``len(buf)`` bytes.
  272. :param buf: Buffer for holding read data.
  273. :param nbytes: Upper limit on number of read characters.
  274. :return: Number of bytes read in.
  275. """
  276. ...
  277. def readline(self) -> Optional[bytes]:
  278. """Read a line, ending in a newline character.
  279. :return: The line read or ``None`` on timeout.
  280. """
  281. ...
  282. def write(self, buf: bytearray) -> Optional[int]:
  283. """
  284. Write the buffer of bytes to the bus.
  285. :param buf: Data that needs to be written.
  286. :return: Number of bytes written or ``None`` on timeout.
  287. """
  288. ...
  289. def sendbreak(self) -> None:
  290. """
  291. Send a break condition on the bus. This drives the bus low for a
  292. duration longer than required for a normal transmission of a character.
  293. """
  294. class SPI(object):
  295. LSB = ... # type: int
  296. MSB = ... # type: int
  297. def __init__(self, id: int) -> None:
  298. """
  299. Construct an SPI object on the given bus, ``id``. Values of id depend
  300. on a particular port and its hardware. Values 0, 1, etc. are commonly
  301. used to select hardware SPI block #0, #1, etc. Value -1 can be used
  302. for bitbanging (software) implementation of SPI (if supported by a port).
  303. With no additional parameters, the SPI object is created but not
  304. initialised (it has the settings from the last initialisation of
  305. the bus, if any). If extra arguments are given, the bus is
  306. initialised. See init for parameters of initialisation.
  307. :param id: Bus ID.
  308. """
  309. ...
  310. def init(self, baudrate: int = 1000000, polarity: int = 0, phase: int = 0,
  311. bits: int = 8, firstbit: int = MSB, sck: Optional[Pin] = None,
  312. mosi: Optional[Pin] = None, miso: Optional[Pin] = None) -> None:
  313. """
  314. Initialise the SPI bus with the given parameters.
  315. :param baudrate: SCK clock rate.
  316. :param polarity: Level the idle clock line sits at (0 or 1).
  317. :param phase: Sample data on the first or second clock edge respectively (0 or 1).
  318. :param bits: Width in bits of each transfer.
  319. :param firstbit: Can be ``SPI.MSB`` or ``SPI.LSB``.
  320. :param sck: SCK pin.
  321. :param mosi: MOSI pin.
  322. :param miso: MISO pin.
  323. """
  324. ...
  325. def deinit(self) -> None:
  326. """Turn off the SPI bus."""
  327. ...
  328. def read(self, nbytes: int, write: int = 0x00) -> bytes:
  329. """Read a number of bytes specified by ``nbytes`` while continuously
  330. writing the single byte given by ``write``. Returns a ``bytes``
  331. object with the data that was read.
  332. :param nbytes: Number of characters to read.
  333. :param write: Value to continiously write while reading data.
  334. :return: Bytes read in.
  335. """
  336. ...
  337. def readinto(self, buf: bytearray, write: int = 0x00) -> None:
  338. """Read into the buffer specified by ``buf`` while continuously writing
  339. the single byte given by ``write``.
  340. """
  341. ...
  342. def write(self, buf: bytes) -> None:
  343. """Write the bytes contained in ``buf``.
  344. :param buf: Bytes to write.
  345. """
  346. ...
  347. def write_readinto(self, write_buf: bytearray, read_buf: bytearray) -> None:
  348. """Write the bytes from ``write_buf`` while reading into ``read_buf``. The
  349. buffers can be the same or different, but both buffers must have
  350. the same length.
  351. :param write_buf: Buffer to read data into.
  352. :param read_buf: Buffer to write data from.
  353. """
  354. ...
  355. class I2C(object):
  356. def __init__(self, id: int, *, scl: Pin, sda: Pin, freq: int = 400000) -> None:
  357. """Construct and return a new I2C object.
  358. :param id: Particular I2C peripheral (-1 for software implementation).
  359. :param scl: Pin object specifying the pin to use for SCL.
  360. :param sda: Pin object specifying the pin to use for SDA.
  361. :param freq: Maximum frequency for SCL.
  362. """
  363. ...
  364. def init(self, scl: Pin, sda: Pin, *, freq: int = 400000) -> None:
  365. """
  366. Initialise the I2C bus with the given arguments.
  367. :param scl: Pin object specifying the pin to use for SCL.
  368. :param sda: Pin object specifying the pin to use for SDA.
  369. :param freq: Maximum frequency for SCL.
  370. """
  371. ...
  372. def scan(self) -> Collection[int]:
  373. """Scan all I2C addresses between *0x08* and *0x77* inclusive and return a
  374. list of those that respond. A device responds if it pulls the SDA
  375. line low after its address (including a write bit) is sent on the bus.
  376. """
  377. ...
  378. def start(self) -> None:
  379. """Generate a START condition on the bus (SDA transitions to low while SCL is high)."""
  380. ...
  381. def stop(self) -> None:
  382. """Generate a STOP condition on the bus (SDA transitions to high while SCL is high)."""
  383. ...
  384. def readinto(self, buf: bytearray, nack: bool = True) -> None:
  385. """Reads bytes from the bus and stores them into ``buf``. The number of bytes
  386. read is the length of ``buf``. An **ACK** will be sent on the bus after
  387. receiving all but the last byte. After the last byte is received,
  388. if ``nack`` is true then a **NACK** will be sent, otherwise an **ACK** will be
  389. sent (and in this case the slave assumes more bytes are going to be
  390. read in a later call).
  391. :param buf: Buffer to read bytes into.
  392. :param nack: If true, then NACK will be sent after reading last bytes.
  393. """
  394. ...
  395. def write(self, buf: bytearray) -> None:
  396. """Write the bytes from ``buf`` to the bus. Checks that an **ACK** is received
  397. after each byte and stops transmitting the remaining bytes if a
  398. **NACK** is received. The function returns the number of ACKs that
  399. were received.
  400. :param buf: Buffer to write bytes from.
  401. """
  402. def readfrom(self, addr: int, nbytes: int, stop: bool = True) -> bytes:
  403. """Read ``nbytes`` from the slave specified by ``addr``.
  404. :param addr: Address of slave device.
  405. :param nbytes: Maximum amount of bytes to be read.
  406. :param stop: If true, then STOP condition is generated at the end of the transfer.
  407. :return: Data read.
  408. """
  409. ...
  410. def readfrom_into(self, addr: int, buf: bytearray, stop: bool = True) -> None:
  411. """Read into ``buf`` from the slave specified by ``addr``. The number of
  412. bytes read will be the length of buf. If ``stop`` is true then a **STOP**
  413. condition is generated at the end of the transfer.
  414. :param addr: Address of slave device.
  415. :param buf: Buffer for storing read data.
  416. :param stop: If true, then STOP condition is generated at the end of the transfer.
  417. """
  418. ...
  419. def writeto(self, addr: int, buf: bytearray, stop: bool = True) -> None:
  420. """Write the bytes from ``buf`` to the slave specified by ``addr``. If a **NACK** is
  421. received following the write of a byte from buf then the remaining
  422. bytes are not sent. If stop is true then a **STOP** condition is generated
  423. at the end of the transfer, even if a **NACK** is received.
  424. :param addr: Address of slave device.
  425. :param buf: Buffer to write data from.
  426. :param stop: If true, then STOP condition is generated at the end of the transfer.
  427. :return: Number of ACKs that were received.
  428. """
  429. ...
  430. def readfrom_mem(self, addr: int, memaddr: int, addrsize: int = 8) -> bytes:
  431. """Read ``nbytes`` from the slave specified by ``addr`` starting from the memory
  432. address specified by ``memaddr``. The argument ``addrsize`` specifies the
  433. address size in bits. Returns a bytes object with the data read.
  434. :param addr: Address of slave device.
  435. :param memaddr: Memory address location on a slave device to read from.
  436. :param addrsize: Address size in bits.
  437. :return: Data that has been read.
  438. """
  439. ...
  440. def readfrom_mem_into(self, addr: int, memaddr: int, buf, *, addrsize=8) -> None:
  441. """Read into ``buf`` from the slave specified by addr starting from the memory
  442. address specified by ``memaddr``. The number of bytes read is the length
  443. of ``buf``. The argument ``addrsize`` specifies the address size in bits
  444. (on ESP8266 this argument is not recognised and the address size is
  445. always 8 bits).
  446. :param addr: Address of slave device.
  447. :param memaddr: Memory address location on a slave device to write into.
  448. :param buf: Buffer to store read data.
  449. :param addrsize: Address size in bits.
  450. """
  451. ...
  452. def writeto_mem(self, addr: int, memaddr: int, *, addrsize=8) -> None:
  453. """Write ``buf`` to the slave specified by ``addr`` starting from the
  454. memory address specified by ``memaddr``. The argument ``addrsize`` specifies
  455. the address size in bits (on ESP8266 this argument is not recognised
  456. and the address size is always 8 bits).
  457. :param addr: Address of slave device.
  458. :param memaddr: Memory address location on a slave device to write into.
  459. :param addrsize: Address size in bits.
  460. """
  461. ...
  462. class RTC(object):
  463. def __init__(self, id: int = 0) -> None:
  464. """Create an RTC object.
  465. :param id: ID of RTC device.
  466. """
  467. ...
  468. def init(self, datetime: tuple) -> None:
  469. """Initialise the RTC. Datetime is a tuple of the form:
  470. ``(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])``
  471. :param datetime: Tuple with information regarding desired initial date.
  472. """
  473. ...
  474. def now(self) -> tuple:
  475. """Get get the current datetime tuple.
  476. :return: Current datetime tuple.
  477. """
  478. ...
  479. def deinit(self) -> None:
  480. """Resets the RTC to the time of January 1, 2015 and starts running it again."""
  481. ...
  482. def alarm(self, id: int, time: Union[int, tuple], *, repeat: bool = False) -> None:
  483. """Set the RTC alarm. Time might be either a millisecond value to program the
  484. alarm to current ``time + time_in_ms`` in the future, or a ``datetimetuple``.
  485. If the ``time`` passed is in milliseconds, repeat can be set to True to
  486. make the alarm periodic.
  487. :param id: Alarm ID.
  488. :param time: Either timestamp in milliseconds or datetime tuple, describing desired moment in the future.
  489. :param repeat: Make alarm periodic, if time passed as milliseconds.
  490. """
  491. ...
  492. def alarm_left(self, alarm_id: int = 0) -> int:
  493. """
  494. Get the number of milliseconds left before the alarm expires.
  495. :param alarm_id: Alarm ID.
  496. :return: Tumber of milliseconds left before the alarm expires.
  497. :rtype: int
  498. """
  499. ...
  500. def cancel(self, alarm_id: int = 0) -> None:
  501. """
  502. Cancel a running alarm.
  503. :param alarm_id: Alarm ID.
  504. """
  505. ...
  506. def irq(self, *, trigger: int, handler: Callable = None, wake: int = IDLE) -> None:
  507. """
  508. Create an irq object triggered by a real time clock alarm.
  509. :param trigger: Must be ``RTC.ALARM0``.
  510. :param handler: Function to be called when the callback is triggered.
  511. :param wake: Sleep mode from where this interrupt can wake up the system.
  512. """
  513. ...
  514. ALARM0 = ... # type: int
  515. class Timer(object):
  516. ONE_SHOT = ... # type: int
  517. PERIODIC = ... # type: int
  518. def __init__(self, id: int) -> None:
  519. """
  520. Construct a new timer object of the given id. Id of -1 constructs a
  521. virtual timer (if supported by a board).
  522. :param id: Timer ID.
  523. """
  524. def deinit(self) -> None:
  525. """
  526. Deinitialises the timer. Stops the timer, and disables the timer peripheral.
  527. """
  528. ...
  529. def reset() -> None:
  530. """Resets the device in a manner similar to pushing the external RESET button."""
  531. ...
  532. def reset_cause() -> int:
  533. """Get the reset cause. Below are possible return values:
  534. * ``machine.PWRON_RESET``
  535. * ``machine.HARD_RESET``
  536. * ``machine.WDT_RESET``
  537. * ``machine.DEEPSLEEP_RESET``
  538. * ``machine.SOFT_RESET``
  539. :return: Reset cause.
  540. :rtype: int
  541. """
  542. ...
  543. def disable_irq() -> int:
  544. """Disable interrupt requests. Returns the previous IRQ state which should
  545. be considered an opaque value. This return value should be passed to
  546. the ``enable_irq`` function to restore interrupts to their original state,
  547. before ``disable_irq`` was called.
  548. :return: Previous IRQ state.
  549. :rtype: int
  550. """
  551. ...
  552. def enable_irq(state: int) -> None:
  553. """Re-enable interrupt requests. The state parameter should be the value
  554. that was returned from the most recent call to the ``disable_irq`` function.
  555. :param state: IRQ state, previously returned from ``disable_irq`` function.
  556. """
  557. ...
  558. def freq() -> int:
  559. """
  560. Returns CPU frequency in hertz.
  561. :return: CPU frequency in hertz.
  562. """
  563. def idle() -> None:
  564. """Gates the clock to the CPU, useful to reduce power consumption at any time
  565. during short or long periods. Peripherals continue working and execution
  566. resumes as soon as any interrupt is triggered (on many ports this includes
  567. system timer interrupt occurring at regular intervals on the order of millisecond).
  568. """
  569. def sleep() -> None:
  570. """Stops the CPU and disables all peripherals except for WLAN. Execution is
  571. resumed from the point where the sleep was requested. For wake up to
  572. actually happen, wake sources should be configured first.
  573. """
  574. def deepsleep() -> None:
  575. """Stops the CPU and all peripherals (including networking interfaces, if
  576. any). Execution is resumed from the main script, just as with a reset.
  577. The reset cause can be checked to know that we are coming from
  578. ``machine.DEEPSLEEP``. For wake up to actually happen, wake
  579. sources should be configured first, like Pin change or RTC timeout.
  580. """
  581. def wake_reason() -> int:
  582. """Get the wake reason. Possible values are:
  583. * ``machine.WLAN_WAKE``
  584. * ``machine.PIN_WAKE``
  585. * ``machine.RTC_WAKE``
  586. :return: Wake reason.
  587. """
  588. ...
  589. def unique_id() -> bytearray:
  590. """
  591. Returns a byte string with a unique identifier of a board/SoC. It will
  592. vary from a board/SoC instance to another, if underlying hardware allows.
  593. Length varies by hardware (so use substring of a full value if you expect
  594. a short ID). In some MicroPython ports, ID corresponds to the network MAC address.
  595. :return: Unique identifier of a board/SoC.
  596. """
  597. ...
  598. def time_pulse_us(pin: Pin, pulse_level: int, timeout_us: int = 1000000) -> int:
  599. """
  600. Time a pulse on the given pin, and return the duration of the pulse in
  601. microseconds. The pulse_level argument should be 0 to time a low pulse
  602. or 1 to time a high pulse.
  603. If the current input value of the pin is different to pulse_level, the
  604. function first (*) waits until the pin input becomes equal to pulse_level,
  605. then (**) times the duration that the pin is equal to pulse_level. If the
  606. pin is already equal to pulse_level then timing starts straight away.
  607. The function will return **-2** if there was timeout waiting for condition marked
  608. (*) above, and **-1** if there was timeout during the main measurement, marked (**)
  609. above. The timeout is the same for both cases and given by timeout_us
  610. (which is in microseconds).
  611. :param pin: Pin for timing a pulse on.
  612. :param pulse_level: Level of pulse (*1* for high, *0* for low)
  613. :param timeout_us: Duration of wait for pin change conditions, in microsecond.
  614. :return: Result code (-1 or -2)
  615. """
  616. ...