1:mod:`esp` --- functions related to the ESP8266 and ESP32
2=========================================================
3
4.. module:: esp
5    :synopsis: functions related to the ESP8266 and ESP32
6
7The ``esp`` module contains specific functions related to both the ESP8266 and
8ESP32 modules.  Some functions are only available on one or the other of these
9ports.
10
11
12Functions
13---------
14
15.. function:: sleep_type([sleep_type])
16
17    **Note**: ESP8266 only
18
19    Get or set the sleep type.
20
21    If the *sleep_type* parameter is provided, sets the sleep type to its
22    value. If the function is called without parameters, returns the current
23    sleep type.
24
25    The possible sleep types are defined as constants:
26
27        * ``SLEEP_NONE`` -- all functions enabled,
28        * ``SLEEP_MODEM`` -- modem sleep, shuts down the WiFi Modem circuit.
29        * ``SLEEP_LIGHT`` -- light sleep, shuts down the WiFi Modem circuit
30          and suspends the processor periodically.
31
32    The system enters the set sleep mode automatically when possible.
33
34.. function:: deepsleep(time_us=0, /)
35
36    **Note**: ESP8266 only - use `machine.deepsleep()` on ESP32
37
38    Enter deep sleep.
39
40    The whole module powers down, except for the RTC clock circuit, which can
41    be used to restart the module after the specified time if the pin 16 is
42    connected to the reset pin. Otherwise the module will sleep until manually
43    reset.
44
45.. function:: flash_id()
46
47    **Note**: ESP8266 only
48
49    Read the device ID of the flash memory.
50
51.. function:: flash_size()
52
53    Read the total size of the flash memory.
54
55.. function:: flash_user_start()
56
57    Read the memory offset at which the user flash space begins.
58
59.. function:: flash_read(byte_offset, length_or_buffer)
60
61.. function:: flash_write(byte_offset, bytes)
62
63.. function:: flash_erase(sector_no)
64
65.. function:: set_native_code_location(start, length)
66
67    **Note**: ESP8266 only
68
69    Set the location that native code will be placed for execution after it is
70    compiled.  Native code is emitted when the ``@micropython.native``,
71    ``@micropython.viper`` and ``@micropython.asm_xtensa`` decorators are applied
72    to a function.  The ESP8266 must execute code from either iRAM or the lower
73    1MByte of flash (which is memory mapped), and this function controls the
74    location.
75
76    If *start* and *length* are both ``None`` then the native code location is
77    set to the unused portion of memory at the end of the iRAM1 region.  The
78    size of this unused portion depends on the firmware and is typically quite
79    small (around 500 bytes), and is enough to store a few very small
80    functions.  The advantage of using this iRAM1 region is that it does not
81    get worn out by writing to it.
82
83    If neither *start* nor *length* are ``None`` then they should be integers.
84    *start* should specify the byte offset from the beginning of the flash at
85    which native code should be stored.  *length* specifies how many bytes of
86    flash from *start* can be used to store native code.  *start* and *length*
87    should be multiples of the sector size (being 4096 bytes).  The flash will
88    be automatically erased before writing to it so be sure to use a region of
89    flash that is not otherwise used, for example by the firmware or the
90    filesystem.
91
92    When using the flash to store native code *start+length* must be less
93    than or equal to 1MByte.  Note that the flash can be worn out if repeated
94    erasures (and writes) are made so use this feature sparingly.
95    In particular, native code needs to be recompiled and rewritten to flash
96    on each boot (including wake from deepsleep).
97
98    In both cases above, using iRAM1 or flash, if there is no more room left
99    in the specified region then the use of a native decorator on a function
100    will lead to `MemoryError` exception being raised during compilation of
101    that function.
102