|
| 1 | +MicroPython port to the ESP32 |
| 2 | +============================= |
| 3 | + |
| 4 | +This is an experimental port of MicroPython to the Espressif ESP32 |
| 5 | +microcontroller. It uses the ESP-IDF framework and MicroPython runs as |
| 6 | +a task under FreeRTOS. |
| 7 | + |
| 8 | +Supported features include: |
| 9 | +- REPL (Python prompt) over UART0. |
| 10 | +- 16k stack for the MicroPython task and 64k Python heap. |
| 11 | +- Many of MicroPython's features are enabled: unicode, arbitrary-precision |
| 12 | + integers, single-precision floats, complex numbers, frozen bytecode, as |
| 13 | + well as many of the internal modules. |
| 14 | +- Internal filesystem using the flash (currently 256k in size). |
| 15 | +- The machine module with basic GPIO and bit-banging I2C, SPI support. |
| 16 | + |
| 17 | +Setting up the toolchain and ESP-IDF |
| 18 | +------------------------------------ |
| 19 | + |
| 20 | +There are two main components that are needed to build the firmware: |
| 21 | +- the Xtensa cross-compiler that targets the CPU in the ESP32 (this is |
| 22 | + different to the compiler used by the ESP8266) |
| 23 | +- the Espressif IDF (IoT development framework, aka SDK) |
| 24 | + |
| 25 | +Instructions for setting up both of these components are provided by the |
| 26 | +ESP-IDF itself, which is found at https://github.com/espressif/esp-idf . |
| 27 | +Follow the guide "Setting Up ESP-IDF", for Windows, Mac or Linux. You |
| 28 | +only need to perform up to "Step 2" of the guide, by which stage you |
| 29 | +should have installed the cross-compile and cloned the ESP-IDF repository. |
| 30 | + |
| 31 | +The instructions provided by the ESP-IDF give a link to download the |
| 32 | +cross-compiler in binary form. At the time of writing this binary had |
| 33 | +a bug in it for some floating-point operations. To fix this you will |
| 34 | +need to compile the cross-compiler from source, using crosstool-NG, and |
| 35 | +instructions to do that are also provided in the ESP-IDF set-up guide. |
| 36 | + |
| 37 | +Once everything is set up you should have a functioning toolchain with |
| 38 | +prefix xtensa-esp32-elf- (or otherwise if you configured it differently) |
| 39 | +as well as a copy of the ESP-IDF repository. |
| 40 | + |
| 41 | +You then need to set the `ESPIDF` environment/makefile variable to point to |
| 42 | +the root of the ESP-IDF repository. You can set the variable in your PATH, |
| 43 | +or at the command line when calling make, or in your own custom `makefile`. |
| 44 | +The last option is recommended as it allows you to easily configure other |
| 45 | +variables for the build. In that case, create a new file in the esp32 |
| 46 | +directory called `makefile` and add the following lines to that file: |
| 47 | +``` |
| 48 | +ESPIDF = <path to root of esp-idf repository> |
| 49 | +#PORT = /dev/ttyUSB0 |
| 50 | +#FLASH_MODE = qio |
| 51 | +#FLASH_SIZE = 4MB |
| 52 | +#CROSS_COMPILE = xtensa-esp32-elf- |
| 53 | +
|
| 54 | +include Makefile |
| 55 | +``` |
| 56 | +Be sure to enter the correct path to your local copy of the IDF repository. |
| 57 | +If the Xtensa cross-compiler is not in your path you can use the |
| 58 | +`CROSS_COMPILE` variable to set its location. Other options of interest |
| 59 | +are `PORT` for the serial port of your esp32 module, and `FLASH_MODE` |
| 60 | +and `FLASH_SIZE`. See the Makefile for further information. |
| 61 | + |
| 62 | +Building the firmware |
| 63 | +--------------------- |
| 64 | + |
| 65 | +The MicroPython cross-compiler must be built to pre-compile some of the |
| 66 | +built-in scripts to bytecode. This can be done by (from the root of |
| 67 | +this repository): |
| 68 | +```bash |
| 69 | +$ make -C mpy-cross |
| 70 | +``` |
| 71 | + |
| 72 | +Then to build MicroPython for the ESP32 run: |
| 73 | +```bash |
| 74 | +$ cd esp32 |
| 75 | +$ make |
| 76 | +``` |
| 77 | +This will produce binary firmware images in the `build/` subdirectory |
| 78 | +(three of them: bootloader.bin, partitions.bin and application.bin). |
| 79 | + |
| 80 | +To flash the firmware you must have your ESP32 module in the bootloader |
| 81 | +mode and connected to a serial port on your PC. Refer to the documentation |
| 82 | +for your particular ESP32 module for how to do this. The serial port and |
| 83 | +flash settings are set in the `Makefile`, and can be overridden in your |
| 84 | +local `makefile`; see above for more details. |
| 85 | + |
| 86 | +If you are installing MicroPython to your module for the first time, or |
| 87 | +after installing any other firmware, you should first erase the flash |
| 88 | +completely: |
| 89 | +```bash |
| 90 | +$ make erase |
| 91 | +``` |
| 92 | + |
| 93 | +To flash the MicroPython firmware to your ESP32 use: |
| 94 | +```bash |
| 95 | +$ make deploy |
| 96 | +``` |
| 97 | +This will use the `esptool.py` script (provided by ESP-IDF) to download the |
| 98 | +binary images. |
| 99 | + |
| 100 | +Getting a Python prompt |
| 101 | +----------------------- |
| 102 | + |
| 103 | +You can get a prompt via the serial port, via UART0, which is the same UART |
| 104 | +that is used for programming the firmware. The baudrate for the REPL is |
| 105 | +115200 and you can use a command such as: |
| 106 | +```bash |
| 107 | +$ picocom /dev/ttyUSB0 |
| 108 | +``` |
0 commit comments