index.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. ***********
  2. Get Started
  3. ***********
  4. :link_to_translation:`zh_CN:[中文]`
  5. This document is intended to help you set up the software development environment for the hardware based on the ESP32 chip by Espressif.
  6. After that, a simple example will show you how to use ESP-IDF (Espressif IoT Development Framework) for menu configuration, then building, and flashing firmware onto an ESP32 board.
  7. .. include:: /_build/inc/version-note.inc
  8. Introduction
  9. ============
  10. ESP32 is a system on a chip that integrates the following features:
  11. * Wi-Fi (2.4 GHz band)
  12. * Bluetooth 4.2
  13. * Dual high performance cores
  14. * Ultra Low Power co-processor
  15. * Several peripherals
  16. Powered by 40 nm technology, ESP32 provides a robust, highly integrated platform, which helps meet the continuous demands for efficient power usage, compact design, security, high performance, and reliability.
  17. Espressif provides basic hardware and software resources to help application developers realize their ideas using the ESP32 series hardware. The software development framework by Espressif is intended for development of Internet-of-Things (IoT) applications with Wi-Fi, Bluetooth, power management and several other system features.
  18. What You Need
  19. =============
  20. Hardware:
  21. * An **ESP32** board
  22. * **USB cable** - USB A / micro USB B
  23. * **Computer** running Windows, Linux, or macOS
  24. Software:
  25. * **Toolchain** to compile code for ESP32
  26. * **Build tools** - CMake and Ninja to build a full **Application** for ESP32
  27. * **ESP-IDF** that essentially contains API (software libraries and source code) for ESP32 and scripts to operate the **Toolchain**
  28. * **Text editor** to write programs (**Projects**) in C, e.g., `Eclipse <https://www.eclipse.org/>`_
  29. .. figure:: ../../_static/what-you-need.png
  30. :align: center
  31. :alt: Development of applications for ESP32
  32. :figclass: align-center
  33. Development of applications for ESP32
  34. Development Board Overviews
  35. ===========================
  36. If you have one of ESP32 development boards listed below, you can click on the link to learn more about its hardware.
  37. .. toctree::
  38. :maxdepth: 1
  39. ESP32-DevKitC <../hw-reference/get-started-devkitc>
  40. ESP-WROVER-KIT <../hw-reference/get-started-wrover-kit>
  41. ESP32-PICO-KIT <../hw-reference/get-started-pico-kit>
  42. ESP32-Ethernet-Kit <../hw-reference/get-started-ethernet-kit>
  43. .. _get-started-step-by-step:
  44. Installation Step by Step
  45. =========================
  46. This is a detailed roadmap to walk you through the installation process.
  47. Setting up Development Environment
  48. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  49. * :ref:`get-started-get-prerequisites` for :doc:`Windows <windows-setup>`, :doc:`Linux <linux-setup>` or :doc:`macOS <macos-setup>`
  50. * :ref:`get-started-get-esp-idf`
  51. * :ref:`get-started-set-up-tools`
  52. * :ref:`get-started-set-up-env`
  53. Creating Your First Project
  54. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. * :ref:`get-started-start-project`
  56. * :ref:`get-started-connect`
  57. * :ref:`get-started-configure`
  58. * :ref:`get-started-build`
  59. * :ref:`get-started-flash`
  60. * :ref:`get-started-build-monitor`
  61. .. _get-started-get-prerequisites:
  62. Step 1. Install prerequisites
  63. =============================
  64. Some tools need to be installed on the computer before proceeding to the next steps. Follow the links below for the instructions for your OS:
  65. .. toctree::
  66. :hidden:
  67. Windows <windows-setup>
  68. Linux <linux-setup>
  69. macOS <macos-setup>
  70. +-------------------+-------------------+-------------------+
  71. | |windows-logo| | |linux-logo| | |macos-logo| |
  72. +-------------------+-------------------+-------------------+
  73. | `Windows`_ | `Linux`_ | `Mac OS`_ |
  74. +-------------------+-------------------+-------------------+
  75. .. |windows-logo| image:: ../../_static/windows-logo.png
  76. :target: ../get-started/windows-setup.html
  77. .. |linux-logo| image:: ../../_static/linux-logo.png
  78. :target: ../get-started/linux-setup.html
  79. .. |macos-logo| image:: ../../_static/macos-logo.png
  80. :target: ../get-started/macos-setup.html
  81. .. _Windows: ../get-started/windows-setup.html
  82. .. _Linux: ../get-started/linux-setup.html
  83. .. _Mac OS: ../get-started/macos-setup.html
  84. .. _get-started-get-esp-idf:
  85. Step 2. Get ESP-IDF
  86. ===================
  87. To build applications for the ESP32, you need the software libraries provided by Espressif in `ESP-IDF repository <https://github.com/espressif/esp-idf>`_.
  88. To get ESP-IDF, navigate to your installation directory and clone the repository with ``git clone``, following instructions below specific to your operating system.
  89. .. note::
  90. This guide uses the directory ``~/esp`` on Linux and macOS or ``%userprofile%\esp`` on Windows as an installation folder for ESP-IDF. You can use any directory, but you will need to adjust paths for the commands respectively. Keep in mind that ESP-IDF does not support spaces in paths.
  91. Linux and macOS
  92. ~~~~~~~~~~~~~~~
  93. Open Terminal, and run the following commands:
  94. .. include:: /_build/inc/git-clone-bash.inc
  95. ESP-IDF will be downloaded into ``~/esp/esp-idf``.
  96. Consult :doc:`/versions` for information about which ESP-IDF version to use in a given situation.
  97. Windows
  98. ~~~~~~~
  99. In addition to installing the tools, :ref:`get-started-windows-tools-installer` for Windows introduced in Step 1 can also download a copy of ESP-IDF.
  100. Consult :doc:`/versions` for information about which ESP-IDF version to use in a given situation.
  101. If you wish to download ESP-IDF without the help of ESP-IDF Tools Installer, refer to these :ref:`instructions <get-esp-idf-windows-command-line>`.
  102. .. _get-started-set-up-tools:
  103. Step 3. Set up the tools
  104. ========================
  105. Aside from the ESP-IDF, you also need to install the tools used by ESP-IDF, such as the compiler, debugger, Python packages, etc.
  106. Windows
  107. ~~~~~~~
  108. :ref:`get-started-windows-tools-installer` for Windows introduced in Step 1 installs all the required tools.
  109. If you want to install the tools without the help of ESP-IDF Tools Installer, open the Command Prompt and follow these steps:
  110. .. code-block:: batch
  111. cd %userprofile%\esp\esp-idf
  112. install.bat
  113. Linux and macOS
  114. ~~~~~~~~~~~~~~~
  115. .. code-block:: bash
  116. cd ~/esp/esp-idf
  117. ./install.sh
  118. Customizing the tools installation path
  119. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  120. The scripts introduced in this step install compilation tools required by ESP-IDF inside the user home directory: ``$HOME/.espressif`` on Linux and macOS, ``%USERPROFILE%\.espressif`` on Windows. If you wish to install the tools into a different directory, set the environment variable ``IDF_TOOLS_PATH`` before running the installation scripts. Make sure that your user has sufficient permissions to read and write this path.
  121. If changing the ``IDF_TOOLS_PATH``, make sure it is set to the same value every time the ``install.bat``/``install.sh`` and ``export.bat``/``export.sh`` scripts are executed.
  122. .. _get-started-set-up-env:
  123. Step 4. Set up the environment variables
  124. ========================================
  125. The installed tools are not yet added to the PATH environment variable. To make the tools usable from the command line, some environment variables must be set. ESP-IDF provides another script which does that.
  126. Windows
  127. ~~~~~~~
  128. :ref:`get-started-windows-tools-installer` for Windows creates an "ESP-IDF Command Prompt" shortcut in the Start Menu. This shortcut opens the Command Prompt and sets up all the required environment variables. You can open this shortcut and proceed to the next step.
  129. Alternatively, if you want to use ESP-IDF in an existing Command Prompt window, you can run:
  130. .. code-block:: batch
  131. %userprofile%\esp\esp-idf\export.bat
  132. Linux and macOS
  133. ~~~~~~~~~~~~~~~
  134. In the terminal where you are going to use ESP-IDF, run:
  135. .. code-block:: bash
  136. . $HOME/esp/esp-idf/export.sh
  137. Note the space between the leading dot and the path!
  138. You can also automate this step, making ESP-IDF tools available in every terminal, by adding this line to your ``.profile`` or ``.bash_profile`` script.
  139. .. _get-started-start-project:
  140. Step 5. Start a Project
  141. =======================
  142. Now you are ready to prepare your application for ESP32. You can start with :example:`get-started/hello_world` project from :idf:`examples` directory in IDF.
  143. Copy :example:`get-started/hello_world` to ``~/esp`` directory:
  144. Linux and macOS
  145. ~~~~~~~~~~~~~~~
  146. .. code-block:: bash
  147. cd ~/esp
  148. cp -r $IDF_PATH/examples/get-started/hello_world .
  149. Windows
  150. ~~~~~~~
  151. .. code-block:: batch
  152. cd %userprofile%\esp
  153. xcopy /e /i %IDF_PATH%\examples\get-started\hello_world hello_world
  154. There is a range of example projects in the :idf:`examples` directory in ESP-IDF. You can copy any project in the same way as presented above and run it.
  155. It is also possible to build examples in-place, without copying them first.
  156. .. important::
  157. The ESP-IDF build system does not support spaces in the paths to either ESP-IDF or to projects.
  158. .. _get-started-connect:
  159. Step 6. Connect Your Device
  160. ===========================
  161. Now connect your ESP32 board to the computer and check under what serial port the board is visible.
  162. Serial ports have the following patterns in their names:
  163. - **Windows**: names like ``COM1``
  164. - **Linux**: starting with ``/dev/tty``
  165. - **macOS**: starting with ``/dev/cu.``
  166. If you are not sure how to check the serial port name, please refer to :doc:`establish-serial-connection` for full details.
  167. .. note::
  168. Keep the port name handy as you will need it in the next steps.
  169. .. _get-started-configure:
  170. Step 7. Configure
  171. =================
  172. Navigate to your ``hello_world`` directory from :ref:`get-started-start-project` and run the project configuration utility ``menuconfig``.
  173. Linux and macOS
  174. ~~~~~~~~~~~~~~~
  175. .. code-block:: bash
  176. cd ~/esp/hello_world
  177. idf.py menuconfig
  178. Windows
  179. ~~~~~~~
  180. .. code-block:: batch
  181. cd %userprofile%\esp\hello_world
  182. idf.py menuconfig
  183. If the previous steps have been done correctly, the following menu appears:
  184. .. figure:: ../../_static/project-configuration.png
  185. :align: center
  186. :alt: Project configuration - Home window
  187. :figclass: align-center
  188. Project configuration - Home window
  189. To navigate and use ``menuconfig``, press the following keys:
  190. * Arrow keys for navigation
  191. * ``Enter`` to go into a submenu
  192. * ``Esc`` to go up one level or exit
  193. * ``?`` to see a help screen. Enter key exits the help screen
  194. * ``Space``, or ``Y`` and ``N`` keys to enable (Yes) and disable (No) configuration items with checkboxes "``[*]``"
  195. * ``?`` while highlighting a configuration item to display help about that item
  196. * ``/`` to find configuration items
  197. .. attention::
  198. If you use ESP32-DevKitC board with the **ESP32-SOLO-1** module, enable single core mode (:ref:`CONFIG_FREERTOS_UNICORE`) in menuconfig before flashing examples.
  199. .. _get-started-build:
  200. Step 8. Build the Project
  201. =========================
  202. Build the project by running::
  203. idf.py build
  204. This command will compile the application and all ESP-IDF components, then it will generate the bootloader, partition table, and application binaries.
  205. .. code-block:: none
  206. $ idf.py build
  207. Running cmake in directory /path/to/hello_world/build
  208. Executing "cmake -G Ninja --warn-uninitialized /path/to/hello_world"...
  209. Warn about uninitialized values.
  210. -- Found Git: /usr/bin/git (found version "2.17.0")
  211. -- Building empty aws_iot component due to configuration
  212. -- Component names: ...
  213. -- Component paths: ...
  214. ... (more lines of build system output)
  215. [527/527] Generating hello-world.bin
  216. esptool.py v2.3.1
  217. Project build complete. To flash, run this command:
  218. ../../../components/esptool_py/esptool/esptool.py -p (PORT) -b 921600 write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x10000 build/hello-world.bin build 0x1000 build/bootloader/bootloader.bin 0x8000 build/partition_table/partition-table.bin
  219. or run 'idf.py -p PORT flash'
  220. If there are no errors, the build will finish by generating the firmware binary .bin file.
  221. .. _get-started-flash:
  222. Step 9. Flash onto the Device
  223. =============================
  224. Flash the binaries that you just built onto your ESP32 board by running::
  225. idf.py -p PORT [-b BAUD] flash
  226. Replace PORT with your ESP32 board's serial port name from :ref:`get-started-connect`.
  227. You can also change the flasher baud rate by replacing BAUD with the baud rate you need. The default baud rate is ``460800``.
  228. For more information on idf.py arguments, see :ref:`idf.py`.
  229. .. note::
  230. The option ``flash`` automatically builds and flashes the project, so running ``idf.py build`` is not necessary.
  231. .. code-block:: none
  232. Running esptool.py in directory [...]/esp/hello_world
  233. Executing "python [...]/esp-idf/components/esptool_py/esptool/esptool.py -b 460800 write_flash @flash_project_args"...
  234. esptool.py -b 460800 write_flash --flash_mode dio --flash_size detect --flash_freq 40m 0x1000 bootloader/bootloader.bin 0x8000 partition_table/partition-table.bin 0x10000 hello-world.bin
  235. esptool.py v2.3.1
  236. Connecting....
  237. Detecting chip type... ESP32
  238. Chip is ESP32D0WDQ6 (revision 1)
  239. Features: WiFi, BT, Dual Core
  240. Uploading stub...
  241. Running stub...
  242. Stub running...
  243. Changing baud rate to 460800
  244. Changed.
  245. Configuring flash size...
  246. Auto-detected Flash size: 4MB
  247. Flash params set to 0x0220
  248. Compressed 22992 bytes to 13019...
  249. Wrote 22992 bytes (13019 compressed) at 0x00001000 in 0.3 seconds (effective 558.9 kbit/s)...
  250. Hash of data verified.
  251. Compressed 3072 bytes to 82...
  252. Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 5789.3 kbit/s)...
  253. Hash of data verified.
  254. Compressed 136672 bytes to 67544...
  255. Wrote 136672 bytes (67544 compressed) at 0x00010000 in 1.9 seconds (effective 567.5 kbit/s)...
  256. Hash of data verified.
  257. Leaving...
  258. Hard resetting via RTS pin...
  259. If there are no issues by the end of the flash process, the module will be reset and the “hello_world” application will be running.
  260. .. (Not currently supported) If you'd like to use the Eclipse IDE instead of running ``idf.py``, check out the :doc:`Eclipse guide <eclipse-setup>`.
  261. .. _get-started-build-monitor:
  262. Step 10. Monitor
  263. ================
  264. To check if "hello_world" is indeed running, type ``idf.py -p PORT monitor`` (Do not forget to replace PORT with your serial port name).
  265. This command launches the :doc:`IDF Monitor <../api-guides/tools/idf-monitor>` application::
  266. $ idf.py -p /dev/ttyUSB0 monitor
  267. Running idf_monitor in directory [...]/esp/hello_world/build
  268. Executing "python [...]/esp-idf/tools/idf_monitor.py -b 115200 [...]/esp/hello_world/build/hello-world.elf"...
  269. --- idf_monitor on /dev/ttyUSB0 115200 ---
  270. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  271. ets Jun 8 2016 00:22:57
  272. rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  273. ets Jun 8 2016 00:22:57
  274. ...
  275. After startup and diagnostic logs scroll up, you should see "Hello world!" printed out by the application.
  276. .. code-block:: none
  277. ...
  278. Hello world!
  279. Restarting in 10 seconds...
  280. I (211) cpu_start: Starting scheduler on APP CPU.
  281. Restarting in 9 seconds...
  282. Restarting in 8 seconds...
  283. Restarting in 7 seconds...
  284. To exit IDF monitor use the shortcut ``Ctrl+]``.
  285. If IDF monitor fails shortly after the upload, or, if instead of the messages above, you see random garbage similar to what is given below, your board is likely using a 26MHz crystal. Most development board designs use 40MHz, so ESP-IDF uses this frequency as a default value.
  286. .. figure:: ../../_static/get-started-garbled-output.png
  287. :align: center
  288. :alt: Garbled output
  289. :figclass: align-center
  290. If you have such a problem, do the following:
  291. 1. Exit the monitor.
  292. 2. Go back to :ref:`menuconfig <get-started-configure>`.
  293. 3. Go to Component config --> ESP32-specific --> Main XTAL frequency, then change :ref:`CONFIG_ESP32_XTAL_FREQ_SEL` to 26MHz.
  294. 4. After that, :ref:`build and flash <get-started-flash>` the application again.
  295. .. note::
  296. You can combine building, flashing and monitoring into one step by running::
  297. idf.py -p PORT flash monitor
  298. See also:
  299. - :doc:`IDF Monitor <../api-guides/tools/idf-monitor>` for handy shortcuts and more details on using IDF monitor.
  300. - :ref:`idf.py` for a full reference of ``idf.py`` commands and options.
  301. **That's all that you need to get started with ESP32!**
  302. Now you are ready to try some other :idf:`examples`, or go straight to developing your own applications.
  303. Updating ESP-IDF
  304. ================
  305. You should update ESP-IDF from time to time, as newer versions fix bugs and provide new features. The simplest way to do the update is to delete the existing ``esp-idf`` folder and clone it again, as if performing the initial installation described in :ref:`get-started-get-esp-idf`.
  306. Another solution is to update only what has changed. :ref:`The update procedure depends on the version of ESP-IDF you are using <updating>`.
  307. After updating ESP-IDF, execute ``install.sh`` (``install.bat`` on Windows) again, in case the new ESP-IDF version requires different versions of tools. See instructions at :ref:`get-started-set-up-tools`.
  308. Once the new tools are installed, update the environment using ``export.sh`` (``export.bat`` on Windows). See instructions at :ref:`get-started-set-up-env`.
  309. Related Documents
  310. =================
  311. .. toctree::
  312. :maxdepth: 1
  313. establish-serial-connection
  314. eclipse-setup
  315. ../api-guides/tools/idf-monitor
  316. toolchain-setup-scratch
  317. ../get-started-legacy/index
  318. .. _Stable version: https://docs.espressif.com/projects/esp-idf/en/stable/
  319. .. _Releases page: https://github.com/espressif/esp-idf/releases