三、MicroPython移植到任意平台(编译篇)

(79) 2024-05-12 20:01:02

目录

note:

一、工程文件

1、源码文件夹

2、中间源文件拷贝

3、文件重命名

二、编译工程配置(Keil)

1、新建分组

2、添加文件到编译

三、编译

1、修改编译报

四、编译通过

1、python解释器初始化入口

2、启动用户任务执行python解释器

 五、上机运行

1、上机运行结果

六、总结


note:

本章内容依赖本专栏前几篇文章,若有疑问请先阅读本专栏前几篇文章

如下:

一、MicroPython简介以及源码移植裁剪_BIN-XYB的博客-CSDN博客

二、MicroPython在PC上编译,运行脚本_BIN-XYB的博客-CSDN博客

一、工程文件

1、源码文件夹

移植目标平台工程文件下新建 genhdr文件夹。

2、中间源文件拷贝

在PC端编译后的文件中找到如下几个文件,拷贝到目标平台工程下 genhdr文件中。

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第1张

 结果如下:

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第2张

3、文件重命名

ports/minimal下 main.c文件 重命名为 py_init.c,删除 原有main.c。

将文件中main函数重命名为micor_python_init();,如下图所示:

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第3张

二、编译工程配置(Keil)

1、新建分组

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第4张

2、添加文件到编译

将ports/minimal下所有.c/.h添加到工程。

将py文件下所有.c/.h文件添加到工程。

后续请根据编译报错,选择向工程中添加.c/h文件。

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第5张

三、编译

请在ports\minimal\mpconfigport.h 中添加如下宏

#define MP_ENDIANNESS_LITTLE (1) 
#define MICROPY_NLR_SETJMP (1)
#define MP_UNREACHABLE for (;;);
#define MICROPY_USE_INTERNAL_ERRNO (1)

 如下图所示:

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第6张

屏蔽或删除下图所示宏,因为我们的目标是移植到任意平台,所以排除干扰

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第7张

1、修改编译报

编译报错较多,请按照提示修改。以下是几个典型

1)、添加到工程的.c和.h文件的 类似include "py/XXXX",改为 include "XXXX"。

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第8张

 下同,类似错误。三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第9张

 2)、遇到如下问题

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第10张

 在Misc Controls 选项中加入--gnu

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第11张

3)、遇到ssize_t未定义问题

在mpconfigport.h里定义:typedef int ssize_t; 且屏蔽报错源文件中 包含的头文件 #include <unistd.h>。

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第12张

4)、接口未定义

根据报错,将shared\readline、shared\runtime等文件夹下文件加入编译系统。

5)、控制台串口收发

控制台串口收发函数定义在uart_core.c中,本例使用STM32的串口3,代码示例如下

/*
 * Core UART functions to implement for a port
 */
#include "main.h"
typedef struct {
    volatile uint32_t SR;
    volatile uint32_t DR;
} periph_uart_t;
#define USART_IO ((periph_uart_t *)USART3)

// Receive single character
int mp_hal_stdin_rx_chr(void) {
    unsigned char c = 0;
    while ((USART_IO->SR & (1 << 5)) == 0) {
    }
    c = USART_IO->DR;
    return c;
}

// Send string of given length
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
    while (len--) {
        // wait for TXE
        while ((USART_IO->SR & (1 << 7)) == 0) {
        }
        USART_IO->DR = *str++;
    }
}

四、编译通过

1、python解释器初始化入口

python解释器初始化源文件py_init.c(需要自己增加,根据ports/minimal.main.c改造而来),示例如下:

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "builtin.h"
#include "compile.h"
#include "runtime.h"
#include "repl.h"
#include "gc.h"
#include "mperrno.h"
#include "pyexec.h"

#if MICROPY_ENABLE_COMPILER
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
    nlr_buf_t nlr;
    if (nlr_push(&nlr) == 0) {
        mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
        qstr source_name = lex->source_name;
        mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);
        mp_obj_t module_fun = mp_compile(&parse_tree, source_name, true);
        mp_call_function_0(module_fun);
        nlr_pop();
    } else {
        // uncaught exception
        mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
    }
}
#endif

static char *stack_top;
#if MICROPY_ENABLE_GC
static char heap[1024 * 10];
#endif

int micro_python_init(void) {
    int stack_dummy;
    stack_top = (char *)&stack_dummy;

    #if MICROPY_ENABLE_GC
    gc_init(heap, heap + sizeof(heap));
    #endif
    mp_init();
    #if MICROPY_ENABLE_COMPILER
    #if MICROPY_REPL_EVENT_DRIVEN
    pyexec_event_repl_init();
    for (;;) {
        int c = mp_hal_stdin_rx_chr();
        if (pyexec_event_repl_process_char(c)) {
            break;
        }
    }
    #else
    pyexec_friendly_repl();
    #endif
    // do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
    // do_str("for i in range(10):\r\n  print(i)", MP_PARSE_FILE_INPUT);
    #else
    pyexec_frozen_module("frozentest.py", false);
    #endif
    mp_deinit();
    return 0;
}

#if MICROPY_ENABLE_GC
void gc_collect(void) {
    // 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(&mp_plat_print);
}
#endif

mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
    mp_raise_OSError(MP_ENOENT);
}

mp_import_stat_t mp_import_stat(const char *path) {
    return MP_IMPORT_STAT_NO_EXIST;
}

void nlr_jump_fail(void *val) {
    while (1) {
        ;
    }
}

void NORETURN __fatal_error(const char *msg) {
    while (1) {
        ;
    }
}

#ifndef NDEBUG
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
    printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line);
    __fatal_error("Assertion failed");
}
#endif

2、启动用户任务执行python解释器

新建一个用户任务,任务函数为phase2_user_task,在此任务中调用micro_python_init,启动python解释器,示例代码如下:

static void phase2_user_task( void *pvParameters )
{
    while(1)
    {
        micro_python_init();
        vTaskDelay(50000);
        break;
    }
}

编译,编译通过,如下图,接下来就可以上机调试啦。

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第13张

 五、上机运行

1、上机运行结果

如下图所示:

三、MicroPython移植到任意平台(编译篇) (https://mushiming.com/)  第14张

六、总结

遇到解决不了的问题请评论区见

THE END

发表回复