Skip to content

Commit

Permalink
Lib/MicroPython: Initial PolyMCU MicroPython wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviermartin committed Feb 27, 2017
1 parent 15c0747 commit 24c5017
Show file tree
Hide file tree
Showing 11 changed files with 402 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Application/LabAPart/MicroPython/Application.cmake
Expand Up @@ -24,5 +24,8 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #


# Heap for MicroPython Garbage collector
set(FIRMWARE_HEAP 0x2000)

# List of modules needed by the application # List of modules needed by the application
set(LIST_MODULES CMSIS Lib/PolyMCU Lib/MicroPython) set(LIST_MODULES CMSIS Lib/PolyMCU Lib/MicroPython)
4 changes: 2 additions & 2 deletions Application/LabAPart/MicroPython/main.c
Expand Up @@ -35,12 +35,12 @@


#include <stdio.h> #include <stdio.h>


static char heap[2048]; extern uint8_t __HeapBase, __HeapLimit;


// The processor clock is initialized by CMSIS startup + system file // The processor clock is initialized by CMSIS startup + system file
int main (void) { int main (void) {
#if MICROPY_ENABLE_GC #if MICROPY_ENABLE_GC
gc_init(heap, heap + sizeof(heap)); gc_init(&__HeapBase, &__HeapLimit);
#endif #endif


mp_init(); mp_init();
Expand Down
94 changes: 94 additions & 0 deletions Lib/MicroPython/CMakeLists.txt
@@ -0,0 +1,94 @@
#
# Copyright (c) 2017, Lab A Part
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

cmake_minimum_required(VERSION 2.8)

if(EXTERNAL_PROJECT_IN_BINARY_DIR)
set(MICROPYTHON_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/src)
set(MICROPYTHON_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR})
else()
set(MICROPYTHON_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(MICROPYTHON_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif()

find_package(Board)
find_package(MicroPython)

set(MICROPYTHON_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build)
set(MICROPYTHON_BUILD_HEADER_DIR ${MICROPYTHON_BUILD_DIR}/genhdr)

# List of sources
set(micropython_lib_SRCS micropython.c
uart_core.c)

if(NOT IS_DIRECTORY ${MICROPYTHON_SRC_DIR})
include(ExternalProject)

ExternalProject_Add(micropython_src
SOURCE_DIR ${MICROPYTHON_SRC_DIR}
#BINARY_DIR ${MICROPYTHON_INSTALL_DIR}
GIT_REPOSITORY /home/olivier/labapart/micropython
# mbedtls-2.0.0 cannot install file properly
#GIT_TAG 6f42417b
#CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${MBEDTLS_INSTALL_DIR} -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID} -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} -DCMAKE_SYSTEM_NAME=Generic -DCMAKE_C_FLAGS=${MBEDTLS_CFLAGS}
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
)
endif()

# Generate flags
set(LIBMICROPYTHON_CFLAGS "${CMAKE_C_FLAGS} -Isrc -I. -I${MICROPYTHON_BUILD_DIR} -I${CMAKE_BINARY_DIR}")
get_directory_property(flags COMPILE_DEFINITIONS)
foreach(flag ${flags})
set(LIBMICROPYTHON_CFLAGS "${LIBMICROPYTHON_CFLAGS} -D${flag}")
endforeach()
get_directory_property(dirs INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
set(LIBMICROPYTHON_CFLAGS "${LIBMICROPYTHON_CFLAGS} -I${dir}")
endforeach()

add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/libmicropython.a
COMMAND make V=1 CC=${CMAKE_C_COMPILER} CFLAGS=${LIBMICROPYTHON_CFLAGS} libmicropython.a
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${micropython_lib_SRCS} mpconfigport.h
COMMENT "Build MicroPython main library"
)

add_custom_command(OUTPUT ${MICROPYTHON_BUILD_DIR}/_frozen_mpy.c
COMMAND ${CMAKE_COMMAND} -E env "PYTHONPATH=${MICROPYTHON_SRC_DIR}/py:$ENV{PYTHONPATH}" ${MICROPYTHON_SRC_DIR}/tools/mpy-tool.py -f -q ${MICROPYTHON_BUILD_HEADER_DIR}/qstrdefs.preprocessed.h -mlongint-impl=none ${MICROPYTHON_SRC_DIR}/minimal/frozentest.mpy > ${MICROPYTHON_BUILD_DIR}/_frozen_mpy.c
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/libmicropython.a
COMMENT "Generate '_frozen_mpy.c'"
)

# Add generated source files
list(APPEND micropython_lib_SRCS ${MICROPYTHON_SRC_DIR}/lib/utils/pyexec.c
${MICROPYTHON_SRC_DIR}/lib/libc/string0.c
${MICROPYTHON_SRC_DIR}/lib/mp-readline/readline.c
${MICROPYTHON_BUILD_DIR}/_frozen_mpy.c)

add_library(polymcu_micropython STATIC ${micropython_lib_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/libmicropython.a)
32 changes: 32 additions & 0 deletions Lib/MicroPython/FindMicroPython.cmake
@@ -0,0 +1,32 @@
#
# Copyright (c) 2017, Lab A Part
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

find_package(Board)

include_directories(${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}/src ${CMAKE_CURRENT_LIST_DIR}/build)

set(MicroPython_LIBRARIES polymcu_micropython ${CMAKE_CURRENT_LIST_DIR}/libmicropython.a)
#set(MicroPython_LIBRARIES polymcu_micropython)
15 changes: 15 additions & 0 deletions Lib/MicroPython/Makefile
@@ -0,0 +1,15 @@
include src/py/mkenv.mk

# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h

# include py core make definitions
include src/py/py.mk

OBJ = $(PY_O)

$(BUILD)/_frozen_mpy.c: frozentest.mpy $(BUILD)/genhdr/qstrdefs.generated.h
$(ECHO) "MISC freezing bytecode"
$(Q)../tools/mpy-tool.py -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h -mlongint-impl=none $< > $@

include src/py/mkrules.mk
39 changes: 39 additions & 0 deletions Lib/MicroPython/micropython.c
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2017, Lab A Part
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this
* o list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "py/gc.h"

void gc_collect(void) {
#if 0
// WARNING: This gc_collect implementation doesn't try to get root
// pointers from CPU registers, and thus may function incorrectly.
void *dummy;
gc_collect_start();
gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
gc_collect_end();
gc_dump_info();
#endif
}
38 changes: 38 additions & 0 deletions Lib/MicroPython/micropython.h
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2017, Lab A Part
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this
* o list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef __MICROPYTHON_H__
#define __MICROPYTHON_H__

#include "mpconfigport.h"

// Receive single character
int mp_hal_stdin_rx_chr(void);

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len);

#endif
116 changes: 116 additions & 0 deletions Lib/MicroPython/mpconfigport.h
@@ -0,0 +1,116 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2013-2017 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <stdint.h>
#include "polymcu_config.h"

// options to control how Micro Python is built

#define MICROPY_QSTR_BYTES_IN_HASH (1)
#define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool
#define MICROPY_ALLOC_PATH_MAX (256)
#define MICROPY_ALLOC_PARSE_CHUNK_INIT (16)
#define MICROPY_EMIT_X64 (0)
#define MICROPY_EMIT_THUMB (0)
#define MICROPY_EMIT_INLINE_THUMB (0)
#define MICROPY_COMP_MODULE_CONST (0)
#define MICROPY_COMP_CONST (0)
#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0)
#define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0)
#define MICROPY_MEM_STATS (0)
#define MICROPY_DEBUG_PRINTERS (0)
#define MICROPY_ENABLE_GC (1)
#define MICROPY_GC_ALLOC_THRESHOLD (0)
#define MICROPY_REPL_EVENT_DRIVEN (0)
#define MICROPY_HELPER_REPL (1)
#define MICROPY_HELPER_LEXER_UNIX (0)
#define MICROPY_ENABLE_SOURCE_LINE (0)
#define MICROPY_ENABLE_DOC_STRING (0)
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
#define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0)
#define MICROPY_PY_ASYNC_AWAIT (0)
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
#define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
#define MICROPY_PY_BUILTINS_ENUMERATE (0)
#define MICROPY_PY_BUILTINS_FILTER (0)
#define MICROPY_PY_BUILTINS_FROZENSET (0)
#define MICROPY_PY_BUILTINS_REVERSED (0)
#define MICROPY_PY_BUILTINS_SET (0)
#define MICROPY_PY_BUILTINS_SLICE (0)
#define MICROPY_PY_BUILTINS_PROPERTY (0)
#define MICROPY_PY_BUILTINS_MIN_MAX (0)
#define MICROPY_PY___FILE__ (0)
#define MICROPY_PY_GC (0)
#define MICROPY_PY_ARRAY (0)
#define MICROPY_PY_ATTRTUPLE (0)
#define MICROPY_PY_COLLECTIONS (0)
#define MICROPY_PY_MATH (0)
#define MICROPY_PY_CMATH (0)
#define MICROPY_PY_IO (0)
#define MICROPY_PY_STRUCT (0)
#define MICROPY_PY_SYS (0)
#define MICROPY_MODULE_FROZEN_MPY (1)
#define MICROPY_CPYTHON_COMPAT (0)
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE)
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_NONE)

// type definitions for the specific machine

#define BYTES_PER_WORD (4)

#define MICROPY_MAKE_POINTER_CALLABLE(p) ((void*)((mp_uint_t)(p) | 1))

// This port is intended to be 32-bit, but unfortunately, int32_t for
// different targets may be defined in different ways - either as int
// or as long. This requires different printf formatting specifiers
// to print such value. So, we avoid int32_t and use int directly.
#define UINT_FMT "%u"
#define INT_FMT "%d"
typedef int mp_int_t; // must be pointer size
typedef unsigned mp_uint_t; // must be pointer size

typedef long mp_off_t;

#define MP_PLAT_PRINT_STRN(str, len) mp_hal_stdout_tx_strn_cooked(str, len)

// extra built in names to add to the global namespace
#define MICROPY_PORT_BUILTINS \
{ MP_OBJ_NEW_QSTR(MP_QSTR_open), (mp_obj_t)&mp_builtin_open_obj },

// We need to provide a declaration/definition of alloca()
#include <alloca.h>

#define MICROPY_HW_BOARD_NAME BOARD
#define MICROPY_HW_MCU_NAME CPU

#ifdef __thumb__
#define MICROPY_MIN_USE_CORTEX_CPU (1)
#endif

#define MP_STATE_PORT MP_STATE_VM

#define MICROPY_PORT_ROOT_POINTERS \
const char *readline_hist[8];
2 changes: 2 additions & 0 deletions Lib/MicroPython/mphalport.h
@@ -0,0 +1,2 @@
static inline mp_uint_t mp_hal_ticks_ms(void) { return 0; }
static inline void mp_hal_set_interrupt_char(char c) {}
1 change: 1 addition & 0 deletions Lib/MicroPython/qstrdefsport.h
@@ -0,0 +1 @@
// qstrs specific to this port

0 comments on commit 24c5017

Please sign in to comment.