Skip to content

Commit d532f6c

Browse files
kwagyemanclaude
andcommitted
docs: scrub upstream-derived reference/ and develop/ pages for OpenMV
Make upstream-MicroPython-derived documentation OpenMV-accurate, comparing each page against current upstream so nothing new is missing. reference/: scrubbed asm_thumb2 (added ARM Thumb-2 Quick Reference Card PDF; replaced the CRC integer-restriction example with a hardware-free one since the debug protocol uses the CRC peripheral), filesystem, glossary (+ _glossary anchor), mpremote, manifest, repl, reset_boot, isr_rules, speed_python, constrained, mpyfiles, packages, micropython2_migration; pyb->machine, external->internal links, grammar/structure fixes; pyb.Flash cleanup; conf.py excludes the superseded pyboard.py.rst. develop/ Internals: compared every page to upstream and merged missing upstream sections (memorymgt 'MicroPython Memory from C code'; cmodules 'C Dynamic Memory Allocation' / 'C++ Modules'); grammar/link/typo fixes; redirected actionable examples to the boards OpenMV users run (PYBV11->OPENMV4, ESP32->OpenMV Cam/rp2) while keeping comprehensive cross-platform reference matrices intact. openmvcam/: OpenMV-branded the Boards/Shields/Sensors page titles without changing the sidebar captions. .gitattributes: mark *.pdf binary. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 95c5660 commit d532f6c

41 files changed

Lines changed: 850 additions & 726 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*.jpg binary
1414
*.dxf binary
1515
*.mpy binary
16+
*.pdf binary
1617
*.der binary
1718
*.mp4 binary
1819
*.webm binary

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ def _render_landing_code(src):
372372
"library/pyb.LCD.rst",
373373
"library/pyb.Switch.rst",
374374
"library/wipy.rst",
375+
"reference/pyboard.py.rst",
375376
"mimxrt/general.rst",
376377
"mimxrt/pinout.rst",
377378
"mimxrt/tutorial/intro.rst",

docs/develop/cmodules.rst

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ limitations with the Python environment, often due to an inability to access
88
certain hardware resources or Python speed limitations.
99

1010
If your limitations can't be resolved with suggestions in :ref:`speed_python`,
11-
writing some or all of your module in C (and/or C++ if implemented for your port)
12-
is a viable option.
11+
writing some or all of your module in C (and/or
12+
:ref:`C++ if implemented for your port<cxx_support>`) is a viable option.
1313

1414
If your module is designed to access or work with commonly available
1515
hardware or libraries please consider implementing it inside the MicroPython
@@ -25,7 +25,7 @@ ports. But when compiling a particular port you will only need to use one
2525
method of building, either Make or CMake.
2626

2727
An alternative approach is to use :ref:`natmod` which allows writing custom C
28-
code that is placed in a .mpy file, which can be imported dynamically in to
28+
code that is placed in a .mpy file, which can be imported dynamically into
2929
a running MicroPython system without the need to recompile the main firmware.
3030

3131

@@ -45,7 +45,7 @@ A MicroPython user C module is a directory with the following files:
4545
* ``micropython.mk`` contains the Makefile fragment for this module.
4646

4747
``$(USERMOD_DIR)`` is available in ``micropython.mk`` as the path to your
48-
module directory. As it's redefined for each c module, is should be expanded
48+
module directory. As it's redefined for each C module, it should be expanded
4949
in your ``micropython.mk`` to a local make variable,
5050
eg ``EXAMPLE_MOD_DIR := $(USERMOD_DIR)``
5151

@@ -140,7 +140,7 @@ For building the example modules which come with MicroPython,
140140
set ``USER_C_MODULES`` to the ``examples/usercmodule`` directory for Make,
141141
or to ``examples/usercmodule/micropython.cmake`` for CMake.
142142

143-
For example, here's how the to build the unix port with the example modules:
143+
For example, here's how to build the unix port with the example modules:
144144

145145
.. code-block:: bash
146146
@@ -215,12 +215,10 @@ Then build with:
215215

216216
.. code-block:: bash
217217
218-
cd my_project/micropython/ports/esp32
219-
make USER_C_MODULES=../../../../modules/micropython.cmake
218+
cd my_project/micropython/ports/rp2
219+
make USER_C_MODULES=../../../modules/micropython.cmake
220220
221-
Note that the esp32 port needs the extra ``..`` for relative paths due to the
222-
location of its main ``CMakeLists.txt`` file. You can also specify absolute
223-
paths to ``USER_C_MODULES``.
221+
You can also specify absolute paths to ``USER_C_MODULES``.
224222

225223
All modules specified by the ``USER_C_MODULES`` variable (either found in this
226224
directory when using Make, or added via ``include`` when using CMake) will be
@@ -285,3 +283,72 @@ can now be accessed in Python just like any other builtin module, e.g.
285283
sleep_ms(1000)
286284
print(watch.time())
287285
# should display approximately 1000
286+
287+
288+
.. _c_heap:
289+
290+
C Dynamic Memory Allocation
291+
---------------------------
292+
293+
MicroPython uses its own "Python heap" for `memorymanagement`,
294+
which is not the same as the "C heap" used by C library functions ``malloc()``,
295+
``free()``, etc. Not every MicroPython port comes with a "C heap" at all.
296+
297+
Tier 1 & 2 ports have varying support for C dynamic memory allocation via a "C
298+
heap":
299+
300+
- unix, windows, esp32 and webassembly ports support C dynamic memory
301+
allocation.
302+
- rp2 port will fail to allocate any memory at runtime unless the firmware is
303+
built with ``MICROPY_C_HEAP_SIZE=n`` to reserve ``n`` bytes of memory for a C
304+
heap. This memory will not be available for Python code to use.
305+
- alif, mimxrt, nrf, renesas-ra, samd, and stm32 port builds that include
306+
dynamic C allocation will fail at link-time with errors such as ``undefined
307+
reference to `malloc'``. MicroPython has no built-in support for dynamic C
308+
allocation on these ports. Any solution requires manually adding a C heap
309+
implementation to the custom build.
310+
- zephyr port currently does not support building with user modules.
311+
312+
Python heap as C heap
313+
~~~~~~~~~~~~~~~~~~~~~
314+
315+
It may be practical for C code to call "Python heap" dynamic allocation
316+
functions such as ``m_malloc()``, ``m_malloc0()`` and ``m_free()`` instead.
317+
318+
See `python_memory_from_c` for more information about this approach.
319+
320+
.. _cxx_support:
321+
322+
C++ Modules
323+
-----------
324+
325+
Most Tier 1 & 2 MicroPython ports (and some Tier 3) support building C++ user
326+
modules, using the C++-specific environment variables described above.
327+
328+
Integrating C++ and MicroPython successfully involves some additional
329+
considerations:
330+
331+
C++ Dynamic Memory Allocation
332+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
333+
334+
C++ programs (as well as C++ Standard Library features) typically use dynamic
335+
memory allocation. The C++ default memory allocator (i.e. operators ``new`` and
336+
``delete``) is typically implemented as a layer on top of `c_heap`.
337+
338+
For MicroPython ports which don't include C dynamic memory allocation support,
339+
C++ dynamic memory allocation can be supported in one of two ways:
340+
341+
- Implement C dynamic memory allocation in your custom build.
342+
- Implement a custom C++ allocator in your custom build.
343+
344+
Linkage Considerations
345+
~~~~~~~~~~~~~~~~~~~~~~
346+
347+
Because MicroPython is a C-based project, any symbols which link to or from
348+
MicroPython need to be qualified ``extern "C"`` in C++ code.
349+
350+
It's strongly recommended to follow the pattern demonstrated in
351+
`examples/usercmodule/cppexample
352+
<https://github.com/micropython/micropython/blob/master/examples/usercmodule/cppexample>`_,
353+
where the Python module is implemented in a minimal C file wrapper around the
354+
C++ code.

docs/develop/compiler.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The compilation process in MicroPython involves the following steps:
1010
* Then bytecode or native code is emitted based on the parse tree.
1111

1212
For purposes of this discussion we are going to add a simple language feature ``add1``
13-
that can be use in Python as:
13+
that can be used in Python as:
1414

1515
.. code-block:: bash
1616
@@ -44,7 +44,7 @@ in ``py/compile.c`` to turn this rule into executable code.
4444
The third required argument can be ``or`` or ``and``. This specifies the number of nodes associated
4545
with a statement. For example, in this case, our ``add1`` statement is similar to ADD1 in assembly
4646
language. It takes one numeric argument. Therefore, the ``add1_stmt`` has two nodes associated with it.
47-
One node is for the statement itself, i.e the literal ``add1`` corresponding to ``KW_ADD1``,
47+
One node is for the statement itself, i.e. the literal ``add1`` corresponding to ``KW_ADD1``,
4848
and the other for its argument, a ``testlist`` rule which is the top-level expression rule.
4949

5050
.. note::
@@ -250,7 +250,7 @@ There is also support for *inline assembly code*, where assembly instructions ar
250250
written as Python function calls but are emitted directly as the corresponding
251251
machine code. This assembler has only three passes (scope, code size, emit)
252252
and uses a different implementation, not the ``compile_scope`` function.
253-
See the `inline assembler tutorial <https://docs.micropython.org/en/latest/pyboard/tutorial/assembler.html#pyboard-tutorial-assembler>`_
253+
See the :ref:`inline assembler reference <asm_thumb2_index>`
254254
for more details.
255255

256256
Fourth pass

docs/develop/gettingstarted.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ below but other unixes ought to work with little modification:
168168
gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
169169
Copyright (C) 2019 Free Software Foundation, Inc.
170170
This is free software; see the source for copying conditions. There is NO
171-
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.then build:
171+
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
172+
173+
then build:
172174

173175
.. code-block:: bash
174176
@@ -232,7 +234,7 @@ You can also specify which board to use:
232234
$ make BOARD=<board>
233235
234236
See `ports/stm32/boards <https://github.com/micropython/micropython/tree/master/ports/stm32/boards>`_
235-
for the available boards. e.g. "PYBV11" or "NUCLEO_WB55".
237+
for the available boards. e.g. "OPENMV4" or "OPENMV4P".
236238

237239
Building the documentation
238240
--------------------------
@@ -399,7 +401,7 @@ tests
399401
tools
400402

401403
Contains scripts used by the build and CI process, as well as user tools such
402-
as ``pyboard.py`` and ``mpremote``.
404+
as ``mpremote``.
403405

404406
examples
405407

docs/develop/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ to MicroPython. It acts as a comprehensive resource on the implementation detail
66
for both novice and expert contributors.
77

88
Development around MicroPython usually involves modifying the core runtime, porting or
9-
maintaining a new library. This guide describes at great depth, the implementation
9+
maintaining a new library. This guide describes in great depth the implementation
1010
details of MicroPython including a getting started guide, compiler internals, porting
1111
MicroPython to a new platform and implementing a core MicroPython library.
1212

docs/develop/library.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Implementing a Module
66
This chapter details how to implement a core module in MicroPython.
77
MicroPython modules can be one of the following:
88

9-
- Built-in module: A general module that is be part of the MicroPython repository.
9+
- Built-in module: A general module that is part of the MicroPython repository.
1010
- User module: A module that is useful for your specific project that you maintain
1111
in your own repository or private codebase.
1212
- Dynamic module: A module that can be deployed and imported at runtime to your device.
@@ -26,15 +26,15 @@ Implementing a core module
2626
--------------------------
2727

2828
Like CPython, MicroPython has core builtin modules that can be accessed through import statements.
29-
An example is the ``gc`` module discussed in :ref:`memorymanagement`.
29+
An example is the :mod:`gc` module discussed in :ref:`memorymanagement`.
3030

3131
.. code-block:: bash
3232
3333
>>> import gc
3434
>>> gc.enable()
3535
>>>
3636
37-
MicroPython has several other builtin standard/core modules like ``io``, ``array`` etc.
37+
MicroPython has several other builtin standard/core modules like :mod:`io`, :mod:`array`, etc.
3838
Adding a new core module involves several modifications.
3939

4040
First, create the ``C`` file in the ``py/`` directory. In this example we are adding a

docs/develop/maps.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ placing the item in the next free slot which is slot ``4`` in our example:
3030

3131
.. image:: img/linprob.png
3232

33-
The same methods i.e open addressing and linear probing are used to search for an item in a dictionary.
33+
The same methods, i.e. open addressing and linear probing, are used to search for an item in a dictionary.
3434
Assume we want to search for the data item ``33``. The computed hash value will be 2. Looking at slot 2
35-
reveals ``33``, at this point, we return ``True``. Searching for ``70`` is quite different as there was a
36-
collision at the time of insertion. Therefore computing the hash value is ``0`` which is currently
35+
reveals ``33``. At this point, we return ``True``. Searching for ``70`` is quite different as there was a
36+
collision at the time of insertion. Therefore the computed hash value is ``0``, which is currently
3737
holding ``44``. Instead of simply returning ``False``, we perform a sequential search starting at point
3838
``1`` until the item ``70`` is found or we encounter a free slot. This is the general way of performing
3939
look-ups in hashes:

docs/develop/memorymgt.rst

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ There are many GC algorithms but MicroPython uses the
2121
policy for managing memory. This algorithm has a mark phase that traverses the heap marking all
2222
live objects while the sweep phase goes through the heap reclaiming all unmarked objects.
2323

24-
Garbage collection functionality in MicroPython is available through the ``gc`` built-in
24+
Garbage collection functionality in MicroPython is available through the :mod:`gc` built-in
2525
module:
2626

2727
.. code-block:: bash
@@ -42,12 +42,73 @@ module:
4242
4343
Even when ``gc.disable()`` is invoked, collection can be triggered with ``gc.collect()``.
4444

45+
.. _python_memory_from_c:
46+
47+
MicroPython Memory from C code
48+
------------------------------
49+
50+
Awareness of the garbage collector is needed when writing C code that allocates
51+
memory from the "Python heap" (i.e. functions ``m_malloc()``, ``m_malloc0()``,
52+
``m_free()``, etc).
53+
54+
The mark phase of the garbage collector scans for live pointers to heap memory
55+
starting from the following roots:
56+
57+
- The stack of the main Python runtime (or REPL).
58+
- The stacks of each "Python thread", for ports which implement Python threads
59+
on top of native operating system threads or tasks.
60+
- The "root pointers" defined in C code using the macro
61+
``MP_REGISTER_ROOT_POINTER``. These are the recommended way to have statically
62+
scoped pointers to the Python heap.
63+
- Tracked allocations made with the ``m_tracked_calloc()``, ``m_tracked_realloc``
64+
and ``m_tracked_free()`` functions. These special functions allow allocating a
65+
block of memory which is always considered live by the garbage collector.
66+
Similar to memory allocation in C, this memory is only freed by calling
67+
``m_tracked_free()`` or by soft reset. There is a small memory usage and
68+
runtime overhead to each tracked allocation. This feature is not enabled by
69+
default on all ports.
70+
71+
The garbage collector then recursively scans and marks all the memory pointed to
72+
by the root pointers, until all addresses are exhausted. This is sufficient to
73+
find all Python objects that are still in use by the MicroPython runtime.
74+
75+
However, the following memory will **not** be scanned by the garbage collector
76+
and could be freed prematurely:
77+
78+
- Static or global C variables which contain pointers to heap memory.
79+
- Pointers which don't point to the "head" of an allocated buffer (i.e. to the
80+
exact address returned by ``m_malloc()``), but instead to an address inside
81+
the allocated buffer (for example, a pointer to a nested struct). For
82+
performance reasons, the garbage collector doesn't mark the enclosing buffer
83+
in these cases.
84+
- The stack of any thread or RTOS task which isn't running Python code or
85+
manually registered as a "Python thread" (for ports which support native
86+
threads or tasks).
87+
88+
Ways to avoid use-after-free in these scenarios:
89+
90+
- Use the tracked allocation API ``m_tracked_calloc()``, ``m_tracked_realloc()``
91+
and ``m_tracked_free()``.
92+
- Register a root pointer (see above), instead of storing a pointer in a static
93+
variable.
94+
- Restructure the code, for example by having an API where Python code
95+
initialises a singleton Python object (implemented in C) which holds all of the
96+
relevant pointers instead of having them in static variables.
97+
98+
.. note:: :ref:`soft_reset` always clears the Python heap and frees all memory.
99+
It's important not to hold any pointers to the heap after a soft
100+
reset, as they will become dangling pointers to freed memory.
101+
102+
Some ports support a "C heap" as well (see `c_heap`), in which case
103+
you can allocate memory that will stay valid over soft reset by
104+
calling standard C functions ``malloc``, etc.
105+
45106
The object model
46107
----------------
47108

48109
All MicroPython objects are referred to by the ``mp_obj_t`` data type.
49110
This is usually word-sized (i.e. the same size as a pointer on the target architecture),
50-
and can be typically 32-bit (STM32, nRF, ESP32, Unix x86) or 64-bit (Unix x64).
111+
and can be typically 32-bit (STM32, RP2, nRF, Unix x86) or 64-bit (Unix x64).
51112
It can also be greater than a word-size for certain object representations, for
52113
example ``OBJ_REPR_D`` has a 64-bit sized ``mp_obj_t`` on a 32-bit architecture.
53114

docs/develop/natmod.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ language which can be compiled to stand-alone machine code can be put into a
2121

2222
A native .mpy module is built using the ``mpy_ld.py`` tool, which is found in the
2323
``tools/`` directory of the project. This tool takes a set of object files
24-
(.o files) and links them together to create a native .mpy files. It requires
24+
(.o files) and links them together to create a native .mpy file. It requires
2525
CPython 3 and the library pyelftools v0.25 or greater.
2626

2727
Supported features and limitations
@@ -53,7 +53,7 @@ architecture flags are present, only if they match the target's capabilities).
5353
For more details about .mpy files see :ref:`mpy_files`.
5454

5555
Native code must be compiled as position independent code (PIC) and use a global
56-
offset table (GOT), although the details of this varies from architecture to
56+
offset table (GOT), although the details of this vary from architecture to
5757
architecture. When importing .mpy files with native code the import machinery
5858
is able to do some basic relocation of the native code. This includes
5959
relocating text, rodata and BSS sections.
@@ -124,7 +124,7 @@ The filesystem layout consists of two main parts, the source files and the Makef
124124
both C source files as well as any Python files which will be included in the
125125
resulting .mpy file.
126126

127-
* The ``Makefile`` contains the build configuration for the module and list the
127+
* The ``Makefile`` contains the build configuration for the module and lists the
128128
source files used to build the .mpy module. It should define ``MPY_DIR`` as the
129129
location of the MicroPython repository (to find header files, the relevant Makefile
130130
fragment, and the ``mpy_ld.py`` tool), ``MOD`` as the name of the module, ``SRC``

0 commit comments

Comments
 (0)