ENع
→MicroPython
الدرس S3 · Special topics

MicroPython on the ESP32

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

MicroPython puts a Python 3 compiler and runtime on the microcontroller itself. Flash it once onto an ESP32 board, open the >>> prompt over USB, and type Python that runs straight away: two lines light an LED on pin 13. Then save main.py to the board's flash so it blinks the LED at every reset, take control back with Ctrl-C, and see what the convenience costs in speed and memory. The lessons “From code to chip: compile, link and flash” and “setup() and loop(): the superloop” show the C++ way this replaces.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل S3 · تفاعلي
01 / 10

Python on the chip

MicroPython is Python 3 made small enough for a microcontroller: it fits in 256 KB of code and 16 KB of RAM. Its compiler and runtime run on the ESP32 itself, so the chip can read Python text and run it.

MicroPython  a lean Python 3 for microcontrollers
  needs      256 KB of code space, 16 KB of RAM
  compiler   Python text  -> bytecode   (on the chip)
  runtime    a virtual machine runs the bytecode
ESP32        up to 240 MHz, 520 KB SRAM, 448 KB ROM
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Python on the chip

    MicroPython is Python 3 made small enough for a microcontroller: it fits in 256 KB of code and 16 KB of RAM. Its compiler and runtime run on the ESP32 itself, so the chip can read Python text and run it.

    MicroPython  a lean Python 3 for microcontrollers
      needs      256 KB of code space, 16 KB of RAM
      compiler   Python text  -> bytecode   (on the chip)
      runtime    a virtual machine runs the bytecode
    ESP32        up to 240 MHz, 520 KB SRAM, 448 KB ROM
  2. Flash the interpreter once

    First, a laptop tool called esptool copies MicroPython onto the board's 4 MiB flash chip, starting at address 0x1000. That is the only firmware you flash. From then on, your Python is just text the interpreter reads.

    on the laptop, once:
      $ esptool.py erase_flash
      $ esptool.py --baud 460800 write_flash 0x1000 ESP32_GENERIC.bin
    flash  4 MiB chip on the board
      0x1000   MicroPython firmware
      after it  a small filesystem for your files
  3. Open the REPL

    Plug in USB and open a serial terminal at 115,200 baud. The chip answers with >>>, the REPL prompt. REPL means read, evaluate, print, loop: it reads a line you type, runs it, prints any result and waits again.

    $ mpremote            (serial terminal, 115200 baud)
    >>>                       the chip is waiting
    R  read      a line of Python text
    E  evaluate  compile it and run it
    P  print     show the result
    L  loop      wait for the next line
  4. Type one line

    Type led = Pin(13, Pin.OUT) and press Enter. The text travels down the cable. On the chip the compiler turns it into bytecode, a compact list of steps, and the virtual machine runs them. Now led lives in the heap, in SRAM.

    >>> from machine import Pin
    >>> led = Pin(13, Pin.OUT)
    laptop   sends the text over USB
    ESP32    compile  -> bytecode
             run      -> GPIO 13 becomes an output
    heap     led  (a Pin object, in SRAM)
  5. The LED lights at once

    Type led.value(1). The chip runs it and sets pin 13 high, close to 3.3 volts, and the LED lights. Nothing was compiled on the laptop or flashed: you try an idea and see the answer straight away.

    >>> led.value(1)
    pin 13   high: at least 2.64 V (0.8 x 3.3 V)
    LED      on
    no compile on the laptop, no flash, no restart
  6. Save main.py

    Typed lines are gone after a reset. To keep a program, save it as a file: mpremote cp main.py : copies main.py into a small filesystem on the flash chip, beside MicroPython. Files survive power off.

    $ mpremote cp main.py :
      ":" means the board's side of the copy
    flash chip
      0x1000   MicroPython
      files    main.py  (kept with the power off)
    
    from machine import Pin
    import time
    led = Pin(13, Pin.OUT)
    while True:
        led.on()
        time.sleep(0.5)
        led.off()
        time.sleep(0.5)
  7. A reset runs main.py

    At every reset MicroPython runs boot.py if there is one, then main.py. The reset starts with empty memory, so lines 1 to 3 build led again: get Pin, import the time module, make pin 13 an output.

    reset  ->  boot.py (if found)  ->  main.py
               (no main.py, or it ends: the >>> prompt)
    line 1  from machine import Pin
    line 2  import time
    line 3  led = Pin(13, Pin.OUT)
  8. Blink, forever

    Line 4 starts a loop that never ends. led.on() sets the pin high, time.sleep(0.5) waits half a second, led.off() sets it low, and it waits again: one blink per second, with no laptop needed.

    line 4    while True:     forever
    line 5-6  on,  wait 0.5 s
    line 7-8  off, wait 0.5 s
    period    0.5 + 0.5 = 1 s  ->  1 blink per second
  9. Ctrl-C takes it back

    With the cable in, press Ctrl-C in the terminal. MicroPython raises KeyboardInterrupt inside the loop, main.py stops where it was, and the >>> prompt returns. Edit, copy, reset, and try again in seconds.

    Ctrl-C   ->  KeyboardInterrupt in main.py
    >>>          the REPL is back; the LED keeps its last level
    Ctrl-D   ->  soft reset: memory cleared, main.py runs again
  10. The price: speed and memory

    Convenience costs time. The virtual machine does many machine steps per bytecode; MicroPython's own native mode runs roughly 2 times faster. Every new object takes heap, and the garbage collector that frees it can pause for several milliseconds.

    bytecode    each step is run by the VM (slow-ish)
    native      machine code: roughly 2x faster, bigger
    heap        every new object takes SRAM
    garbage collector  frees it, pausing several ms
    for tight timing: C, or native code, or hardware timers

اختبار يتاح بعد الخطوة الأخيرة.