-
Notifications
You must be signed in to change notification settings - Fork 25
Preprocessor for ULP/RTC macros #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
79db90f
add units test for the .set directive
wnienhaus 84d734d
add support for left aligned assembler directives (e.g. .set)
wnienhaus ec81ecc
fix a crash bug where BSS size calculation was attempted on the value…
wnienhaus c184924
raise error when attempting to store values in .bss section
wnienhaus 25d34b0
fix reference to non-existing variable
wnienhaus 76a81ac
fix typo in comment of instruction definition
wnienhaus 56f4530
add support for the .global directive. only symbols flagged as global…
wnienhaus 9907b10
let SymbolTable.export() optionally export non-global symbols too
wnienhaus 27ab850
support ULP opcodes in upper case
wnienhaus 54b117e
add a compatibility test for the recent fixes and improvements
wnienhaus feb42dc
add support for evaluating expressions
wnienhaus 87507c9
add a compatibility test for evaluating expressions
wnienhaus 99352a3
docs: add that expressions are now supported
wnienhaus d76fd26
add preprocessor that can replace simple #define values in code
wnienhaus 4dded94
allow assembler to skip comment removal to avoid removing comments twice
wnienhaus 219f939
fix evaluation of expressions during first assembler pass
wnienhaus 5c3eeb8
remove no-longer-needed pass dependent code from SymbolTable
wnienhaus 3e8c0d5
add support for macros such as WRITE_RTC_REG
wnienhaus ac1de99
add simple include file processing
wnienhaus 8d88fd1
add support for using a btree database (DefinesDB) to store defines f…
wnienhaus 46f1442
add special handling for the BIT macro used in the esp-idf framework
wnienhaus 2f6ee78
add include processor tool for populating a defines.db from include f…
wnienhaus 69ae946
add compatibility tests using good example code off the net
wnienhaus 4f90f76
add documentation for the preprocessor
wnienhaus d44384f
fix use of treg field in i_move instruction to match binutils-esp32 o…
wnienhaus 254adf9
allow specifying the address for reg_rd and reg_wr in 32-bit words
wnienhaus c3bd101
support .int data type
wnienhaus 2a0a39a
refactor: small improvements based on PR comments.
wnienhaus 47d5e8a
Updated LICENSE file and added AUTHORS file
wnienhaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
E-mail addresses listed here are not intended for support. | ||
|
||
py-esp32-ulp authors | ||
-------------------- | ||
py-esp32-ulp is written and maintained by Thomas Waldmann and various contributors: | ||
|
||
- Thomas Waldmann <[email protected]> | ||
- Wilko Nienhaus <[email protected]> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
Preprocessor | ||
--------------------- | ||
|
||
py-esp32-ulp contains a small preprocessor, which aims to fulfill one goal: | ||
facilitate assembling of ULP code from Espressif and other open-source | ||
projects to loadable/executable machine code without modification. | ||
|
||
Such code uses convenience macros (``READ_RTC_*`` and ``WRITE_RTC_*``) | ||
provided by the ESP-IDF framework, along with constants defined in the | ||
framework's include files (such as ``RTC_GPIO_IN_REG``), to make reading | ||
and writing from/to peripheral registers much easier. | ||
|
||
In order to do this the preprocessor has two capabilities: | ||
|
||
1. Parse and replace identifiers defined with ``#define`` | ||
2. Recognise the ``WRITE_RTC_*`` and ``READ_RTC_*`` macros and expand | ||
them in a way that mirrors what the real ESP-IDF macros do. | ||
|
||
|
||
Usage | ||
------------------------ | ||
|
||
Normally the assembler is called as follows | ||
|
||
.. code-block:: python | ||
|
||
src = "..full assembler file contents" | ||
assembler = Assembler() | ||
assembler.assemble(src) | ||
... | ||
|
||
With the preprocessor, simply pass the source code via the preprocessor first: | ||
|
||
.. code-block:: python | ||
|
||
from preprocess import preprocess | ||
|
||
src = "..full assembler file contents" | ||
src = preprocess(src) | ||
assembler = Assembler() | ||
assembler.assemble(src) | ||
... | ||
|
||
|
||
Using a "Defines Database" | ||
-------------------------- | ||
|
||
Because the py-esp32-ulp assembler was built for running on the ESP32 | ||
microcontroller with limited RAM, the preprocessor aims to work there too. | ||
|
||
To handle large number of defined constants (such as the ``RTC_*`` constants from | ||
the ESP-IDF) the preprocessor can use a database (based on BerkleyDB) stored on the | ||
device's filesystem for looking up defines. | ||
|
||
The database needs to be populated before preprocessing. (Usually, when only using | ||
constants from the ESP-IDF, this is a one-time step, because the include files | ||
don't change.) The database can be reused for all subsequent preprocessor runs. | ||
|
||
(The database can also be generated on a PC and then deployed to the ESP32, to | ||
save processing effort on the device. In that case the include files themselves | ||
are not needed on the device either.) | ||
|
||
1. Build the defines database | ||
|
||
The ``esp32_ulp.parse_to_db`` tool can be used to generate the defines | ||
database from include files. The resulting file will be called | ||
``defines.db``. | ||
|
||
(The following assume running on a PC. To do this on device, refer to the | ||
`esp32_ulp/parse_to_db.py <../esp32_ulp/parse_to_db.py>`_ file.) | ||
|
||
.. code-block:: bash | ||
|
||
# general command | ||
micropython -m esp32_ulp.parse_to_db path/to/include.h | ||
|
||
# loading specific ESP-IDF include files | ||
micropython -m esp32_ulp.parse_to_db esp-idf/components/soc/esp32/include/soc/soc_ulp.h | ||
|
||
# loading multiple files at once | ||
micropython -m esp32_ulp.parse_to_db esp-idf/components/soc/esp32/include/soc/*.h | ||
|
||
# if file system space is not a concern, the following can be convenient | ||
# by including all relevant include files from the ESP-IDF framework. | ||
# This results in an approximately 2MB large database. | ||
micropython -m esp32_ulp.parse_to_db \ | ||
esp-idf/components/soc/esp32/include/soc/*.h \ | ||
esp-idf/components/esp_common/include/*.h | ||
|
||
# most ULP code uses only 5 include files. Parsing only those into the | ||
# database should thus allow assembling virtually all ULP code one would | ||
# find or want to write. | ||
# This results in an approximately 250kB large database. | ||
micropython -m esp32_ulp.parse_to_db \ | ||
esp-idf/components/soc/esp32/include/soc/{soc,soc_ulp,rtc_cntl_reg,rtc_io_reg,sens_reg}.h | ||
|
||
2. Using the defines database during preprocessing | ||
|
||
The preprocessor will automatically use a defines database, when using the | ||
``preprocess.preprocess`` convenience function, even when the database does | ||
not exist (an absent database is treated like an empty database, and care | ||
is taken not to create an empty database file, cluttering up the filesystem, | ||
when not needed). | ||
|
||
If you do not want the preprocessor use use a DefinesDB, pass ``False`` to | ||
the ``use_defines_db`` argument of the ``preprocess`` convenience function, | ||
or instantiate the ``Preprocessor`` class directly, without passing it a | ||
DefinesDB instance via ``use_db``. | ||
|
||
Design choices | ||
-------------- | ||
|
||
The preprocessor does not support: | ||
|
||
1. Function style macros such as :code:`#define f(a,b) (a+b)` | ||
|
||
This is not important, because there are only few RTC macros that need | ||
to be supported and they are simply implemented as Python functions. | ||
|
||
Since the preprocessor will understand ``#define`` directives directly in the | ||
assembler source file, include mechanisms are not needed in some cases | ||
(simply copying the needed ``#define`` statements from include files into the | ||
assembler source will work). | ||
|
||
2. ``#include`` directives | ||
|
||
The preprocessor does not currently follow ``#include`` directives. To | ||
limit space requirements (both in memory and on the filesystem), the | ||
preprocessor relies on a database of defines (key/value pairs). This | ||
database should be populated before using the preprocessor, by using the | ||
``esp32_ulp.parse_to_db`` tool (see section above), which parses include | ||
files for identifiers defined therein. | ||
|
||
3. Preserving comments | ||
|
||
The assumption is that the output will almost always go into the | ||
assembler directly, so preserving comments is not very useful and | ||
would add a lot of complexity. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.