10000 cc3200: Add support for loading dynamic native modules. · russbot/circuitpython@7ece905 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7ece905

Browse files
danicamporadpgeorge
authored andcommitted
cc3200: Add support for loading dynamic native modules.
Add modx.mk which allows to build common external modules and also port specific modules with a single Makefile.
1 parent b9f574b commit 7ece905

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed

cc3200/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,13 @@ else
3333
ifeq ($(BTARGET), bootloader)
3434
include bootmgr/bootloader.mk
3535
else
36+
ifeq ($(BTARGET), modx)
37+
include mods/modx/modx.mk
38+
else
3639
$(error Invalid BTARGET specified)
3740
endif
3841
endif
42+
endif
3943

4044
# always include MicroPython make rules
4145
include ../py/mkrules.mk

cc3200/application.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ APP_MISC_SRC_C = $(addprefix misc/,\
8282
mperror.c \
8383
mpexception.c \
8484
mpsystick.c \
85+
mploadbinary.c \
8586
)
8687

8788
APP_MODS_SRC_C = $(addprefix mods/,\

cc3200/misc/mploadbinary.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2015 Damien P. George
7+
* Copyright (c) 2015 Daniel Campora
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/mpextern.h"
29+
#include "py/misc.h"
30+
#include "ff.h"
31+
32+
33+
const byte *mp_extern_load_binary(const char *ext_name) {
34+
FILINFO fno;
35+
FRESULT res = f_stat(ext_name, &fno);
36+
if (res != FR_OK) {
37+
return NULL;
38+
}
39+
FIL fp;
40+
res = f_open(&fp, ext_name, FA_READ);
41+
if (res != FR_OK) {
42+
return NULL;
43+
}
44+
byte *buf = m_new(byte, fno.fsize);
45+
UINT n;
46+
res = f_read(&fp, buf, fno.fsize, &n);
47+
if (res != FR_OK) {
48+
m_del(byte, buf, fno.fsize);
49+
return NULL;
50+
}
51+
f_close(&fp);
52+
return buf;
53+
}
54+

cc3200/mods/modx/dummy.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "py/mpextern.h"
2+
3+
static unsigned char dummy_data[4] = {1, 2, 3, 4};
4+
5+
STATIC mp_obj_t dummy_compute(const mp_ext_table_t *et, mp_obj_t val1, mp_obj_t val2) {
6+
mp_uint_t _val1 = et->mp_obj_get_int(val1);
7+
mp_uint_t _val2 = et->mp_obj_get_int(val2);
8+
9+
return MP_OBJ_NEW_SMALL_INT((mp_int_t)((_val1 * 5) + (_val2 / 3)));
10+
}
11+
12+
STATIC mp_obj_t dummy_getdata(const mp_ext_table_t *et, mp_obj_t index) {
13+
mp_uint_t idx = et->mp_obj_get_int(index);
14+
15+
if (idx < 4) {
16+
return MP_OBJ_NEW_SMALL_INT(dummy_data[idx]);
17+
}
18+
else {
19+
return MP_OBJ_NEW_SMALL_INT(-1);
20+
}
21+
}
22+
23+
MP_EXT_HEADER
24+
25+
MP_EXT_INIT
26+
void init(const mp_ext_table_t *et) {
27+
mp_obj_t f_compute = et->mp_obj_new_fun_extern(false, 2, 2, dummy_compute);
28+
mp_obj_t f_getdata = et->mp_obj_new_fun_extern(false, 1, 1, dummy_getdata);
29+
et->mp_store_global(et->qstr_from_str("compute"), f_compute);
30+
et->mp_store_global(et->qstr_from_str("getdata"), f_getdata);
31+
}

cc3200/mods/modx/modx.mk

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Make the build directory reflect the board name
2+
BUILD = build/$(BOARD)/modx
3+
4+
MODX_INC = -I.
5+
MODX_INC += -I..
6+
MODX_INC += -Ihal
7+
MODX_INC += -Ihal/inc
8+
MODX_INC += -I$(PY_SRC)
9+
MODX_INC += -I$(BUILD)
10+
11+
MODX_CPPDEFINES = -Dgcc -DMODX -DTARGET_IS_CC3200
12+
13+
MODX_PORT_SRC_C = $(addprefix mods/modx/,\
14+
dummy.c \
15+
)
16+
17+
MODX_COMM_SRC_C = $(addprefix extmod/modx/,\
18+
modx.c \
19+
)
20+
21+
OBJ = $(addprefix $(BUILD)/, $(MODX_PORT_SRC_C:.c=.o) $(MODX_COMM_SRC_C:.c=.o))
22+
23+
# Define the linker script
24+
LINKER_SCRIPT = ../extmod/modx/mpextern.ld
25+
LDFLAGS += -T $(LINKER_SCRIPT)
26+
27+
# Add the modx specific CFLAGS
28+
CFLAGS += $(MODX_CPPDEFINES) $(MODX_INC)
29+
CFLAGS += -Os -DNDEBUG -fPIC
30+
31+
# Define the header build location
32+
HEADER_BUILD = $(BUILD)/genhdr
33+
34+
# Create a list of all dynamic modules to be built
35+
MODX = $(addprefix $(BUILD)/, modx.mpy dummy.mpy)
36+
37+
all: $(MODX)
38+
39+
# Link the common external modules
40+
$(BUILD)/%.axf: $(BUILD)/extmod/modx/%.o
41+
$(ECHO) "LINK $@"
42+
$(Q)$(CC) -o $@ $(LDFLAGS) $<
43+
$(Q)$(SIZE) $@
44+
45+
# Link the port's external modules
46+
$(BUILD)/%.axf: $(BUILD)/mods/modx/%.o
47+
$(ECHO) "LINK $@"
48+
$(Q)$(CC) -o $@ $(LDFLAGS) $<
49+
$(Q)$(SIZE) $@
50+
51+
$(BUILD)/%.mpy: $(BUILD)/%.axf
52+
$(ECHO) "Create $@"
53+
$(Q)$(OBJCOPY) -O binary -j .all $< $@
54+
55+
# Create an empty "qstrdefs.generated.h" needed by py/mkrules.mk
56+
$(HEADER_BUILD)/qstrdefs.generated.h: | $(HEADER_BUILD)
57+
touch $@
58+
59+
# Create an empty "py-version.h" needed by py/mkrules.mk
60+
$(HEADER_BUILD)/py-version.h: | $(HEADER_BUILD)
61+
touch $@
62+

cc3200/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
#define MICROPY_PY_BUILTINS_TIMEOUTERROR (1)
7878
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
7979
#ifndef DEBUG
80+
#define MICROPY_MODULE_EXTERN (1)
8081
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
8182
#define MICROPY_PY_BUILTINS_STR_SPLITLINES (1)
8283
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)

0 commit comments

Comments
 (0)
0