Skip to content

Commit 607548f

Browse files
jimmodpgeorge
authored andcommitted
examples/natmod: Add features4 as a class definition example.
Also provide a basic README.md for dynamic native modules. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent f52a2cd commit 607548f

4 files changed

Lines changed: 162 additions & 0 deletions

File tree

examples/natmod/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Dynamic Native Modules
2+
3+
Dynamic Native Modules are .mpy files that contain native machine code from a
4+
language other than Python. For more info see [the documentation]
5+
(https://docs.micropython.org/en/latest/develop/natmod.html).
6+
7+
This should not be confused with [User C Modules]
8+
(https://docs.micropython.org/en/latest/develop/cmodules.html) which are a
9+
mechanism to add additional out-of-tree modules into the firmware build.
10+
11+
## Examples
12+
13+
This directory contains several examples of writing dynamic native modules, in
14+
two main categories:
15+
16+
1. Feature examples.
17+
18+
* `features0` - A module containing a single "factorial" function which
19+
demonstrates working with integers.
20+
21+
* `features1` - A module that demonstrates some common tasks:
22+
- defining simple functions exposed to Python
23+
- defining local, helper C functions
24+
- defining constant integers and strings exposed to Python
25+
- getting and creating integer objects
26+
- creating Python lists
27+
- raising exceptions
28+
- allocating memory
29+
- BSS and constant data (rodata)
30+
- relocated pointers in rodata
31+
32+
* `features2` - This is a hybrid module containing both Python and C code,
33+
and additionally the C code is spread over multiple files. It also
34+
demonstrates using floating point (only when the target supports
35+
hardware floating point).
36+
37+
* `features3` - A module that shows how to use types, constant objects,
38+
and creating dictionary instances.
39+
40+
* `features4` - A module that demonstrates how to define a class.
41+
42+
2. Dynamic version of existing built-ins.
43+
44+
This provides a way to add missing functionality to firmware that doesn't
45+
include certain built-in modules. See the `heapq`, `random`, `re`,
46+
`deflate`, `btree`, and `framebuf` directories.
47+
48+
So for example, if your firmware was compiled with `MICROPY_PY_FRAMEBUF`
49+
disabled (e.g. to save flash space), then it would not include the
50+
`framebuf` module. The `framebuf` native module provides a way to add the
51+
`framebuf` module dynamically.
52+
53+
The way these work is they define a dynamic native module which
54+
`#include`'s the original module and then does the necessary
55+
initialisation of the module's globals dict.
56+
57+
## Build instructions
58+
59+
To compile an example, you need to have the same toolchain available as
60+
required for your target port. e.g. `arm-none-eabi-gcc` for any ARM Cortex M
61+
target. See the port instructions for details.
62+
63+
You also need to have the `pyelftools` Python package available, either via
64+
your system package manager or installed from PyPI in a virtual environment
65+
with `pip`.
66+
67+
Each example provides a Makefile. You should specify the `ARCH` argument to
68+
make (one of x86, x64, armv6m, armv7m, xtensa, xtensawin):
69+
70+
```
71+
$ cd features0
72+
$ make ARCH=armv7m
73+
$ mpremote cp features0.mpy :
74+
```

examples/natmod/features4/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Location of top-level MicroPython directory
2+
MPY_DIR = ../../..
3+
4+
# Name of module
5+
MOD = features4
6+
7+
# Source files (.c or .py)
8+
SRC = features4.c
9+
10+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11+
ARCH = x64
12+
13+
# Include to get the rules for compiling and linking the module
14+
include $(MPY_DIR)/py/dynruntime.mk
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
This example extends on features0 but demonstrates how to define a class.
3+
4+
The Factorial class constructor takes an integer, and then the calculate
5+
method can be called to get the factorial.
6+
7+
>>> import features4
8+
>>> f = features4.Factorial(4)
9+
>>> f.calculate()
10+
24
11+
*/
12+
13+
// Include the header file to get access to the MicroPython API
14+
#include "py/dynruntime.h"
15+
16+
// This is type(Factorial)
17+
mp_obj_full_type_t mp_type_factorial;
18+
19+
// This is the internal state of a Factorial instance.
20+
typedef struct {
21+
mp_obj_base_t base;
22+
mp_int_t n;
23+
} mp_obj_factorial_t;
24+
25+
// Essentially Factorial.__new__ (but also kind of __init__).
26+
// Takes a single argument (the number to find the factorial of)
27+
STATIC mp_obj_t factorial_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args_in) {
28+
mp_arg_check_num(n_args, n_kw, 1, 1, false);
29+
30+
mp_obj_factorial_t *o = mp_obj_malloc(mp_obj_factorial_t, type);
31+
o->n = mp_obj_get_int(args_in[0]);
32+
33+
return MP_OBJ_FROM_PTR(o);
34+
}
35+
36+
STATIC mp_int_t factorial_helper(mp_int_t x) {
37+
if (x == 0) {
38+
return 1;
39+
}
40+
return x * factorial_helper(x - 1);
41+
}
42+
43+
// Implements Factorial.calculate()
44+
STATIC mp_obj_t factorial_calculate(mp_obj_t self_in) {
45+
mp_obj_factorial_t *self = MP_OBJ_TO_PTR(self_in);
46+
return mp_obj_new_int(factorial_helper(self->n));
47+
}
48+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(factorial_calculate_obj, factorial_calculate);
49+
50+
// Locals dict for the Factorial type (will have a single method, calculate,
51+
// added in mpy_init).
52+
mp_map_elem_t factorial_locals_dict_table[1];
53+
STATIC MP_DEFINE_CONST_DICT(factorial_locals_dict, factorial_locals_dict_table);
54+
55+
// This is the entry point and is called when the module is imported
56+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
57+
// This must be first, it sets up the globals dict and other things
58+
MP_DYNRUNTIME_INIT_ENTRY
59+
60+
// Initialise the type.
61+
mp_type_factorial.base.type = (void*)&mp_type_type;
62+
mp_type_factorial.flags = MP_TYPE_FLAG_NONE;
63+
mp_type_factorial.name = MP_QSTR_Factorial;
64+
MP_OBJ_TYPE_SET_SLOT(&mp_type_factorial, make_new, factorial_make_new, 0);
65+
factorial_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_calculate), MP_OBJ_FROM_PTR(&factorial_calculate_obj) };
66+
MP_OBJ_TYPE_SET_SLOT(&mp_type_factorial, locals_dict, (void*)&factorial_locals_dict, 1);
67+
68+
// Make the Factorial type available on the module.
69+
mp_store_global(MP_QSTR_Factorial, MP_OBJ_FROM_PTR(&mp_type_factorial));
70+
71+
// This must be last, it restores the globals dict
72+
MP_DYNRUNTIME_INIT_EXIT
73+
}

tools/ci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ function ci_native_mpy_modules_build {
425425
make -C examples/natmod/features1 ARCH=$arch
426426
make -C examples/natmod/features2 ARCH=$arch
427427
make -C examples/natmod/features3 ARCH=$arch
428+
make -C examples/natmod/features4 ARCH=$arch
428429
make -C examples/natmod/btree ARCH=$arch
429430
make -C examples/natmod/deflate ARCH=$arch
430431
make -C examples/natmod/framebuf ARCH=$arch

0 commit comments

Comments
 (0)