|
| 1 | +py-esp32-ulp Documentation |
| 2 | +========================== |
| 3 | + |
| 4 | +.. contents:: Table of Contents |
| 5 | + |
| 6 | + |
| 7 | +Overview |
| 8 | +-------- |
| 9 | + |
| 10 | +`README.rst </README.rst>`_ gives a general overview of this project. |
| 11 | + |
| 12 | + |
| 13 | +Installation |
| 14 | +------------ |
| 15 | + |
| 16 | +On the ESP32, install using upip: |
| 17 | + |
| 18 | +.. code-block:: python |
| 19 | +
|
| 20 | + # ensure the ESP32 is connected to a network with internet connectivity |
| 21 | + import upip |
| 22 | + upip.install('micropython-py-esp32-ulp') |
| 23 | +
|
| 24 | +On a PC, simply ``git clone`` this repo. |
| 25 | + |
| 26 | + |
| 27 | +Getting started |
| 28 | +--------------- |
| 29 | + |
| 30 | +On the ESP32 |
| 31 | +++++++++++++ |
| 32 | + |
| 33 | +The simplest example to try on the ESP32 is `counter.py </examples/counter.py>`_. |
| 34 | +It shows how to assemble code, load and run the resulting binary and exchange |
| 35 | +data between the ULP and the main CPU. |
| 36 | + |
| 37 | +Run the ``counter.py`` example: |
| 38 | + |
| 39 | +1. Install py-esp32-ulp onto the ESP32 as shown above |
| 40 | +2. Upload the `examples/counter.py </examples/counter.py>`_ file to the ESP32 |
| 41 | +3. Run with ``import counter`` |
| 42 | + |
| 43 | +You can also try the `blink.py </examples/blink.py>`_ example, which shows how to |
| 44 | +let the ULP blink an LED. |
| 45 | + |
| 46 | +Look inside each example for a more detailed description. |
| 47 | + |
| 48 | + |
| 49 | +On a PC |
| 50 | ++++++++ |
| 51 | + |
| 52 | +On a PC with the unix port of MicroPython, you can assemble source code as |
| 53 | +follows: |
| 54 | + |
| 55 | +.. code-block:: shell |
| 56 | +
|
| 57 | + git clone https://github.com/ThomasWaldmann/py-esp32-ulp.git |
| 58 | + cd py-esp32-ulp |
| 59 | + micropython -m esp32_ulp path/to/code.S # this results in path/to/code.ulp |
| 60 | +
|
| 61 | +
|
| 62 | +More examples |
| 63 | ++++++++++++++ |
| 64 | + |
| 65 | +Other ULP examples from around the web: |
| 66 | + |
| 67 | +* https://github.com/espressif/esp-iot-solution/tree/master/examples/ulp_examples |
| 68 | +* https://github.com/duff2013/ulptool |
| 69 | +* https://github.com/joba-1/Blink-ULP/blob/master/main/ulp/ |
| 70 | + |
| 71 | + |
| 72 | +Advanced usage |
| 73 | +-------------- |
| 74 | + |
| 75 | +In some applications you might want to separate the assembly stage from the |
| 76 | +loading/running stage, to avoid having to assemble the code on every startup. |
| 77 | +This can be useful in battery-powered applications where every second of sleep |
| 78 | +time matters. |
| 79 | + |
| 80 | +Splitting the assembly and load stage can be combined with other techniques, |
| 81 | +for example to implement a caching mechansim for the ULP binary that |
| 82 | +automatically updates the binary every time the assembly source code changes. |
| 83 | + |
| 84 | +The ``esp32_ulp.assemble_file`` function can be used to assemble and link an |
| 85 | +assembly source file into a machine code binary file with a ``.ulp`` extension. |
| 86 | +That file can then be loaded directly without assembling the source again. |
| 87 | + |
| 88 | +1. Create/upload an assembly source file and run the following to get a |
| 89 | + loadable ULP binary as a ``.ulp`` file: |
| 90 | + |
| 91 | + .. code-block:: python |
| 92 | +
|
| 93 | + import esp32_ulp |
| 94 | + esp32_ulp.assemble_file('code.S') # this results in code.ulp |
| 95 | +
|
| 96 | +2. The above prints out the offsets of all global symbols/labels. For the next |
| 97 | + step, you will need to note down the offset of the label, which represents |
| 98 | + the entry point to your code. |
| 99 | + |
| 100 | +3. Now load and run the resulting binary as follows: |
| 101 | + |
| 102 | + .. code-block:: python |
| 103 | +
|
| 104 | + from esp32 import ULP |
| 105 | +
|
| 106 | + ulp = ULP() |
| 107 | + with open('test.ulp', 'r') as f: |
| 108 | + # load the binary into RTC memory |
| 109 | + ulp.load_binary(0, f.read()) |
| 110 | +
|
| 111 | + # configure how often the ULP should wake up |
| 112 | + ulp.set_wakeup_period(0, 500000) # 500k usec == 0.5 sec |
| 113 | +
|
| 114 | + # start the ULP |
| 115 | + # assemble_file printed offsets in number of 32-bit words. |
| 116 | + # ulp.run() expects an offset in number of bytes. |
| 117 | + # Thus, multiply the offset to our entry point by 4. |
| 118 | + # e.g. for an offset of 2: |
| 119 | + # 2 words * 4 = 8 bytes |
| 120 | + ulp.run(2*4) # specify the offset of the entry point label |
| 121 | +
|
| 122 | +To update the binary every time the source code changes, you would need a |
| 123 | +mechanism to detect that the source code changed. This could trigger a re-run |
| 124 | +of the ``assemble_file`` function to update the binary. Manually re-running |
| 125 | +this function as needed would also work. |
| 126 | + |
| 127 | + |
| 128 | +Preprocessor |
| 129 | +------------ |
| 130 | + |
| 131 | +There is a simple preprocessor that understands just enough to allow assembling |
| 132 | +ULP source files containing convenience macros such as WRITE_RTC_REG. This is |
| 133 | +especially useful for assembling ULP examples from Espressif or other ULP code |
| 134 | +found as part of Arduino/ESP-IDF projects. |
| 135 | + |
| 136 | +The preprocessor and how to use it is documented here: `Preprocessor support </docs/preprocess.rst>`_. |
| 137 | + |
| 138 | + |
| 139 | +Limitations |
| 140 | +----------- |
| 141 | + |
| 142 | +Currently the following are not supported: |
| 143 | + |
| 144 | +* assembler macros using ``.macro`` |
| 145 | +* preprocessor macros using ``#define A(x,y) ...`` |
| 146 | +* including files using ``#include`` |
| 147 | +* ESP32-S2 (not binary compatible with the ESP32) |
| 148 | + |
| 149 | + |
| 150 | +Testing |
| 151 | +------- |
| 152 | + |
| 153 | +There are unit tests and also compatibility tests that check whether the binary |
| 154 | +output is identical with what binutils-esp32ulp produces. |
| 155 | + |
| 156 | +py-esp32-ulp has been tested on the Unix port of MicroPython and on real ESP32 |
| 157 | +devices with the chip type ESP32D0WDQ6 (revision 1) without SPIRAM. |
| 158 | + |
| 159 | +Consult the Github Actions `workflow definition file </.github/workflows/run_tests.yaml>`_ |
| 160 | +for how to run the different tests. |
| 161 | + |
| 162 | + |
| 163 | +Links |
| 164 | +----- |
| 165 | + |
| 166 | +Espressif documentation: |
| 167 | + |
| 168 | +* `ESP32 ULP coprocessor instruction set <https://esp-idf.readthedocs.io/en/latest/api-guides/ulp_instruction_set.html>`_ |
| 169 | +* `ESP32 Technical Reference Manual <https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf>`_ |
| 170 | + |
| 171 | +GNU Assembler "as" documentation (we try to be compatible for all features that are implemented) |
| 172 | + |
| 173 | +* `GNU Assembler manual <https://sourceware.org/binutils/docs/as/index.html>`_ |
0 commit comments