index.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. ***********
  2. Get Started
  3. ***********
  4. :link_to_translation:`zh_CN:[中文]`
  5. This document is intended to help users set up the software environment for development of applications using hardware based on the Espressif ESP32. Through a simple example we would like to illustrate how to use ESP-IDF (Espressif IoT Development Framework), including the menu based configuration, compiling the ESP-IDF and firmware download to ESP32 boards.
  6. .. include:: /_build/inc/version-note.inc
  7. Introduction
  8. ============
  9. ESP32 integrates Wi-Fi (2.4 GHz band) and Bluetooth 4.2 solutions on a single chip, along with dual high performance cores, Ultra Low Power co-processor and several peripherals. Powered by 40 nm technology, ESP32 provides a robust, highly integrated platform to meet the continuous demands for efficient power usage, compact design, security, high performance, and reliability.
  10. Espressif provides the basic hardware and software resources that help application developers to build their ideas around the ESP32 series hardware. The software development framework by Espressif is intended for rapidly developing Internet-of-Things (IoT) applications, with Wi-Fi, Bluetooth, power management and several other system features.
  11. What You Need
  12. =============
  13. To develop applications for ESP32 you need:
  14. * **PC** loaded with either Windows, Linux or Mac operating system
  15. * **Toolchain** to build the **Application** for ESP32
  16. * **ESP-IDF** that essentially contains API for ESP32 and scripts to operate the **Toolchain**
  17. * A text editor to write programs (**Projects**) in C, e.g. `Eclipse <https://www.eclipse.org/>`_
  18. * The **ESP32** board itself and a **USB cable** to connect it to the **PC**
  19. .. figure:: ../../_static/what-you-need.png
  20. :align: center
  21. :alt: Development of applications for ESP32
  22. :figclass: align-center
  23. Development of applications for ESP32
  24. Preparation of development environment consists of three steps:
  25. 1. Setup of **Toolchain**
  26. 2. Getting of **ESP-IDF** from GitHub
  27. 3. Installation and configuration of **Eclipse**
  28. You may skip the last step, if you prefer to use different editor.
  29. Having environment set up, you are ready to start the most interesting part - the application development. This process may be summarized in four steps:
  30. 1. Configuration of a **Project** and writing the code
  31. 2. Compilation of the **Project** and linking it to build an **Application**
  32. 3. Flashing (uploading) of the **Application** to **ESP32**
  33. 4. Monitoring / debugging of the **Application**
  34. See instructions below that will walk you through these steps.
  35. Guides
  36. ======
  37. If you have one of ESP32 development boards listed below, click on provided links to get you up and running.
  38. .. toctree::
  39. :maxdepth: 1
  40. ESP32 DevKitC <get-started-devkitc>
  41. ESP-WROVER-KIT <get-started-wrover-kit>
  42. ESP32-PICO-KIT <get-started-pico-kit>
  43. If you have different board, move to sections below.
  44. .. _get-started-setup-toolchain:
  45. Setup Toolchain
  46. ===============
  47. The quickest way to start development with ESP32 is by installing a prebuilt toolchain. Pick up your OS below and follow provided instructions.
  48. .. toctree::
  49. :hidden:
  50. Windows <windows-setup>
  51. Linux <linux-setup>
  52. MacOS <macos-setup>
  53. +-------------------+-------------------+-------------------+
  54. | |windows-logo| | |linux-logo| | |macos-logo| |
  55. +-------------------+-------------------+-------------------+
  56. | `Windows`_ | `Linux`_ | `Mac OS`_ |
  57. +-------------------+-------------------+-------------------+
  58. .. |windows-logo| image:: ../../_static/windows-logo.png
  59. :target: ../get-started/windows-setup.html
  60. .. |linux-logo| image:: ../../_static/linux-logo.png
  61. :target: ../get-started/linux-setup.html
  62. .. |macos-logo| image:: ../../_static/macos-logo.png
  63. :target: ../get-started/macos-setup.html
  64. .. _Windows: ../get-started/windows-setup.html
  65. .. _Linux: ../get-started/linux-setup.html
  66. .. _Mac OS: ../get-started/macos-setup.html
  67. .. note::
  68. We are using ``~/esp`` directory to install the prebuilt toolchain, ESP-IDF and sample applications. You can use different directory, but need to adjust respective commands.
  69. Depending on your experience and preferences, instead of using a prebuilt toolchain, you may want to customize your environment. To set up the system your own way go to section :ref:`get-started-customized-setup`.
  70. Once you are done with setting up the toolchain then go to section :ref:`get-started-get-esp-idf`.
  71. .. _get-started-get-esp-idf:
  72. Get ESP-IDF
  73. ===========
  74. .. highlight:: bash
  75. Besides the toolchain (that contains programs to compile and build the application), you also need ESP32 specific API / libraries. They are provided by Espressif in `ESP-IDF repository <https://github.com/espressif/esp-idf>`_.
  76. To obtain a local copy: open terminal, navigate to the directory you want to put ESP-IDF, and clone the repository using ``git clone`` command:
  77. .. include:: /_build/inc/git-clone-bash.inc
  78. ESP-IDF will be downloaded into ``~/esp/esp-idf``.
  79. Consult :doc:`/versions` for information about which version of ESP-IDF to use in a given situation.
  80. .. include:: /_build/inc/git-clone-notes.inc
  81. .. note::
  82. Do not miss the ``--recursive`` option. If you have already cloned ESP-IDF without this option, run another command to get all the submodules::
  83. cd esp-idf
  84. git submodule update --init --recursive
  85. .. _get-started-setup-path:
  86. Setup Path to ESP-IDF
  87. =====================
  88. The toolchain programs access ESP-IDF using ``IDF_PATH`` environment variable. This variable should be set up on your PC, otherwise projects will not build. Setting may be done manually, each time PC is restarted. Another option is to set up it permanently by defining ``IDF_PATH`` in user profile. To do so, follow instructions specific to :ref:`Windows <add-idf_path-to-profile-windows>` , :ref:`Linux and MacOS <add-idf_path-to-profile-linux-macos>` in section :doc:`add-idf_path-to-profile`.
  89. .. _get-started-get-packages:
  90. Install the Required Python Packages
  91. ====================================
  92. Python packages required by ESP-IDF are located in the ``$IDF_PATH/requirements.txt`` file. You can install them by running::
  93. python -m pip install --user -r $IDF_PATH/requirements.txt
  94. .. note::
  95. Please invoke that version of the Python interpreter which you will be using with ESP-IDF. The version of the
  96. interpreter can be checked by running command ``python --version`` and depending on the result, you might want to
  97. use ``python2``, ``python2.7`` or similar instead of ``python``, e.g.::
  98. python2.7 -m pip install --user -r $IDF_PATH/requirements.txt
  99. .. _get-started-start-project:
  100. Start a Project
  101. ===============
  102. Now you are ready to prepare your application for ESP32. To start off quickly, we will use :example:`get-started/hello_world` project from :idf:`examples` directory in IDF.
  103. Copy :example:`get-started/hello_world` to ``~/esp`` directory::
  104. cd ~/esp
  105. cp -r $IDF_PATH/examples/get-started/hello_world .
  106. You can also find a range of example projects under the :idf:`examples` directory in ESP-IDF. These example project directories can be copied in the same way as presented above, to begin your own projects.
  107. .. important::
  108. The esp-idf build system does not support spaces in paths to esp-idf or to projects.
  109. .. _get-started-connect:
  110. Connect
  111. =======
  112. You are almost there. To be able to proceed further, connect ESP32 board to PC, check under what serial port the board is visible and verify if serial communication works. If you are not sure how to do it, check instructions in section :doc:`establish-serial-connection`. Note the port number, as it will be required in the next step.
  113. .. _get-started-configure:
  114. Configure
  115. =========
  116. Being in terminal window, go to directory of ``hello_world`` application by typing ``cd ~/esp/hello_world``. Then start project configuration utility ``menuconfig``::
  117. cd ~/esp/hello_world
  118. make menuconfig
  119. If previous steps have been done correctly, the following menu will be displayed:
  120. .. figure:: ../../_static/project-configuration.png
  121. :align: center
  122. :alt: Project configuration - Home window
  123. :figclass: align-center
  124. Project configuration - Home window
  125. In the menu, navigate to ``Serial flasher config`` > ``Default serial port`` to configure the serial port, where project will be loaded to. Confirm selection by pressing enter, save configuration by selecting ``< Save >`` and then exit application by selecting ``< Exit >``.
  126. .. note::
  127. On Windows, serial ports have names like COM1. On MacOS, they start with ``/dev/cu.``. On Linux, they start with ``/dev/tty``.
  128. (See :doc:`establish-serial-connection` for full details.)
  129. Here are couple of tips on navigation and use of ``menuconfig``:
  130. * Use up & down arrow keys to navigate the menu.
  131. * Use Enter key to go into a submenu, Escape key to go out or to exit.
  132. * Type ``?`` to see a help screen. Enter key exits the help screen.
  133. * Use Space key, or ``Y`` and ``N`` keys to enable (Yes) and disable (No) configuration items with checkboxes "``[*]``"
  134. * Pressing ``?`` while highlighting a configuration item displays help about that item.
  135. * Type ``/`` to search the configuration items.
  136. .. note::
  137. If you are **Arch Linux** user, navigate to ``SDK tool configuration`` and change the name of ``Python 2 interpreter`` from ``python`` to ``python2``.
  138. .. attention::
  139. When using ESP32-DevKitC board with ESP32-SOLO-1 module, enable single core mode (:ref:`CONFIG_FREERTOS_UNICORE`) in menuconfig before flashing example applications.
  140. .. _get-started-build-flash:
  141. Build and Flash
  142. ===============
  143. Now you can build and flash the application. Run::
  144. make flash
  145. This will compile the application and all the ESP-IDF components, generate bootloader, partition table, and application binaries, and flash these binaries to your ESP32 board.
  146. .. highlight:: none
  147. ::
  148. esptool.py v2.0-beta2
  149. Flashing binaries to serial port /dev/ttyUSB0 (app at offset 0x10000)...
  150. esptool.py v2.0-beta2
  151. Connecting........___
  152. Uploading stub...
  153. Running stub...
  154. Stub running...
  155. Changing baud rate to 921600
  156. Changed.
  157. Attaching SPI flash...
  158. Configuring flash size...
  159. Auto-detected Flash size: 4MB
  160. Flash params set to 0x0220
  161. Compressed 11616 bytes to 6695...
  162. Wrote 11616 bytes (6695 compressed) at 0x00001000 in 0.1 seconds (effective 920.5 kbit/s)...
  163. Hash of data verified.
  164. Compressed 408096 bytes to 171625...
  165. Wrote 408096 bytes (171625 compressed) at 0x00010000 in 3.9 seconds (effective 847.3 kbit/s)...
  166. Hash of data verified.
  167. Compressed 3072 bytes to 82...
  168. Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 8297.4 kbit/s)...
  169. Hash of data verified.
  170. Leaving...
  171. Hard resetting...
  172. If there are no issues, at the end of build process, you should see messages describing progress of loading process. Finally, the end module will be reset and "hello_world" application will start.
  173. If you'd like to use the Eclipse IDE instead of running ``make``, check out the :doc:`Eclipse guide <eclipse-setup>`.
  174. .. _get-started-build-monitor:
  175. Monitor
  176. =======
  177. To see if "hello_world" application is indeed running, type ``make monitor``. This command is launching :doc:`IDF Monitor <idf-monitor>` application::
  178. $ make monitor
  179. MONITOR
  180. --- idf_monitor on /dev/ttyUSB0 115200 ---
  181. --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
  182. ets Jun 8 2016 00:22:57
  183. rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
  184. ets Jun 8 2016 00:22:57
  185. ...
  186. Several lines below, after start up and diagnostic log, you should see "Hello world!" printed out by the application. ::
  187. ...
  188. Hello world!
  189. Restarting in 10 seconds...
  190. I (211) cpu_start: Starting scheduler on APP CPU.
  191. Restarting in 9 seconds...
  192. Restarting in 8 seconds...
  193. Restarting in 7 seconds...
  194. To exit the monitor use shortcut ``Ctrl+]``.
  195. .. note::
  196. If instead of the messages above, you see a random garbage similar to::
  197. e���)(Xn@�y.!��(�PW+)��Hn9a؅/9�!�t5��P�~�k��e�ea�5�jA
  198. ~zY��Y(1�,1�� e���)(Xn@�y.!Dr�zY(�jpi�|�+z5Ymvp
  199. or monitor fails shortly after upload, your board is likely using 26MHz crystal, while the ESP-IDF assumes default of 40MHz. Exit the monitor, go back to the :ref:`menuconfig <get-started-configure>`, change :ref:`CONFIG_ESP32_XTAL_FREQ_SEL` to 26MHz, then :ref:`build and flash <get-started-build-flash>` the application again. This is found under ``make menuconfig`` under Component config --> ESP32-specific --> Main XTAL frequency.
  200. To execute ``make flash`` and ``make monitor`` in one go, type ``make flash monitor``. Check section :doc:`IDF Monitor <idf-monitor>` for handy shortcuts and more details on using this application.
  201. That's all what you need to get started with ESP32!
  202. Now you are ready to try some other :idf:`examples`, or go right to developing your own applications.
  203. Environment Variables
  204. =====================
  205. Some environment variables can be specified whilst calling ``make`` allowing users to **override arguments without needing to reconfigure them using** ``make menuconfig``.
  206. +-----------------+--------------------------------------------------------------+
  207. | Variables | Description & Usage |
  208. +=================+==============================================================+
  209. | ``ESPPORT`` | Overrides the serial port used in ``flash`` and ``monitor``. |
  210. | | |
  211. | | Examples: ``make flash ESPPORT=/dev/ttyUSB1``, |
  212. | | ``make monitor ESPPORT=COM1`` |
  213. +-----------------+--------------------------------------------------------------+
  214. | ``ESPBAUD`` | Overrides the serial baud rate when flashing the ESP32. |
  215. | | |
  216. | | Example: ``make flash ESPBAUD=9600`` |
  217. +-----------------+--------------------------------------------------------------+
  218. | ``MONITORBAUD`` | Overrides the serial baud rate used when monitoring. |
  219. | | |
  220. | | Example: ``make monitor MONITORBAUD=9600`` |
  221. +-----------------+--------------------------------------------------------------+
  222. .. note::
  223. Users can export environment variables (e.g. ``export ESPPORT=/dev/ttyUSB1``).
  224. All subsequent calls of ``make`` within the same terminal session will use
  225. the exported value given that the variable is not simultaneously overridden.
  226. Updating ESP-IDF
  227. ================
  228. After some time of using ESP-IDF, you may want to update it to take advantage of new features or bug fixes. The simplest way to do so is by deleting existing ``esp-idf`` folder and cloning it again, exactly as when doing initial installation described in sections :ref:`get-started-get-esp-idf`.
  229. If downloading to a new path, remember to :doc:`add-idf_path-to-profile` so that the toolchain scripts know where to find the ESP-IDF in its release specific location.
  230. Another solution is to update only what has changed. :ref:`The update procedure depends on the version of ESP-IDF you are using <updating>`.
  231. Related Documents
  232. =================
  233. .. toctree::
  234. :maxdepth: 1
  235. add-idf_path-to-profile
  236. establish-serial-connection
  237. make-project
  238. eclipse-setup
  239. idf-monitor
  240. toolchain-setup-scratch
  241. .. _Stable version: https://docs.espressif.com/projects/esp-idf/en/stable/
  242. .. _Releases page: https://github.com/espressif/esp-idf/releases