{"id":9118,"date":"2024-05-12T20:01:02","date_gmt":"2024-05-12T12:01:02","guid":{"rendered":""},"modified":"2024-05-12T20:01:02","modified_gmt":"2024-05-12T12:01:02","slug":"\u4e09\u3001MicroPython\u79fb\u690d\u5230\u4efb\u610f\u5e73\u53f0\uff08\u7f16\u8bd1\u7bc7\uff09","status":"publish","type":"post","link":"https:\/\/mushiming.com\/9118.html","title":{"rendered":"\u4e09\u3001MicroPython\u79fb\u690d\u5230\u4efb\u610f\u5e73\u53f0\uff08\u7f16\u8bd1\u7bc7\uff09"},"content":{"rendered":"
\u76ee\u5f55<\/strong><\/p>\n note:<\/p>\n \u4e00\u3001\u5de5\u7a0b\u6587\u4ef6<\/p>\n 1\u3001\u6e90\u7801\u6587\u4ef6\u5939<\/p>\n 2\u3001\u4e2d\u95f4\u6e90\u6587\u4ef6\u62f7\u8d1d<\/p>\n 3\u3001\u6587\u4ef6\u91cd\u547d\u540d<\/p>\n \u4e8c\u3001\u7f16\u8bd1\u5de5\u7a0b\u914d\u7f6e\uff08Keil\uff09<\/p>\n 1\u3001\u65b0\u5efa\u5206\u7ec4<\/p>\n 2\u3001\u6dfb\u52a0\u6587\u4ef6\u5230\u7f16\u8bd1<\/p>\n \u4e09\u3001\u7f16\u8bd1<\/p>\n 1\u3001\u4fee\u6539\u7f16\u8bd1\u62a5<\/p>\n \u56db\u3001\u7f16\u8bd1\u901a\u8fc7<\/p>\n 1\u3001python\u89e3\u91ca\u5668\u521d\u59cb\u5316\u5165\u53e3<\/p>\n 2\u3001\u542f\u52a8\u7528\u6237\u4efb\u52a1\u6267\u884cpython\u89e3\u91ca\u5668<\/p>\n \u4e94\u3001\u4e0a\u673a\u8fd0\u884c<\/p>\n 1\u3001\u4e0a\u673a\u8fd0\u884c\u7ed3\u679c<\/p>\n \u516d\u3001\u603b\u7ed3<\/p>\n \u672c\u7ae0\u5185\u5bb9\u4f9d\u8d56\u672c\u4e13\u680f\u524d\u51e0\u7bc7\u6587\u7ae0\uff0c\u82e5\u6709\u7591\u95ee\u8bf7\u5148\u9605\u8bfb\u672c\u4e13\u680f\u524d\u51e0\u7bc7\u6587\u7ae0<\/span>\u3002<\/p>\n \u5982\u4e0b\uff1a<\/p>\n \u4e00\u3001MicroPython\u7b80\u4ecb\u4ee5\u53ca\u6e90\u7801\u79fb\u690d\u88c1\u526a_BIN-XYB\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n \u4e8c\u3001MicroPython\u5728PC\u4e0a\u7f16\u8bd1\uff0c\u8fd0\u884c\u811a\u672c_BIN-XYB\u7684\u535a\u5ba2-CSDN\u535a\u5ba2<\/p>\n \u79fb\u690d\u76ee\u6807\u5e73\u53f0\u5de5\u7a0b\u6587\u4ef6\u4e0b\u65b0\u5efa genhdr\u6587\u4ef6\u5939\u3002<\/p>\n \u5728PC\u7aef\u7f16\u8bd1\u540e\u7684\u6587\u4ef6\u4e2d\u627e\u5230\u5982\u4e0b\u51e0\u4e2a\u6587\u4ef6\uff0c\u62f7\u8d1d\u5230\u76ee\u6807\u5e73\u53f0\u5de5\u7a0b\u4e0b genhdr\u6587\u4ef6\u4e2d\u3002<\/p>\n <\/p>\n \u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n <\/p>\n ports\/minimal\u4e0b main.c\u6587\u4ef6 \u91cd\u547d\u540d\u4e3a py_init.c,\u5220\u9664 \u539f\u6709main.c\u3002<\/span><\/p>\n \u5c06\u6587\u4ef6\u4e2dmain\u51fd\u6570\u91cd\u547d\u540d\u4e3amicor_python_init();\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/span><\/p>\n <\/p>\n <\/p>\n \u5c06ports\/minimal\u4e0b\u6240\u6709.c\/.h\u6dfb\u52a0\u5230\u5de5\u7a0b\u3002<\/p>\n \u5c06py\u6587\u4ef6\u4e0b\u6240\u6709.c\/.h\u6587\u4ef6\u6dfb\u52a0\u5230\u5de5\u7a0b\u3002<\/p>\n \u540e\u7eed\u8bf7\u6839\u636e\u7f16\u8bd1\u62a5\u9519\uff0c\u9009\u62e9\u5411\u5de5\u7a0b\u4e2d\u6dfb\u52a0.c\/h\u6587\u4ef6\u3002<\/p>\n <\/p>\n<\/p>\n \u8bf7\u5728ports\\minimal\\mpconfigport.h \u4e2d\u6dfb\u52a0\u5982\u4e0b\u5b8f<\/p>\n #define MP_ENDIANNESS_LITTLE (1) \u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n <\/p>\n \u5c4f\u853d\u6216\u5220\u9664\u4e0b\u56fe\u6240\u793a\u5b8f\uff0c\u56e0\u4e3a\u6211\u4eec\u7684\u76ee\u6807\u662f\u79fb\u690d\u5230\u4efb\u610f\u5e73\u53f0\uff0c\u6240\u4ee5\u6392\u9664\u5e72\u6270<\/p>\n <\/p>\n \u7f16\u8bd1\u62a5\u9519\u8f83\u591a\uff0c\u8bf7\u6309\u7167\u63d0\u793a\u4fee\u6539\u3002\u4ee5\u4e0b\u662f\u51e0\u4e2a\u5178\u578b<\/p>\n 1\uff09\u3001\u6dfb\u52a0\u5230\u5de5\u7a0b\u7684.c\u548c.h\u6587\u4ef6\u7684 \u7c7b\u4f3cinclude \"py\/XXXX\"\uff0c\u6539\u4e3a include \"XXXX\"\u3002<\/p>\n <\/p>\n \u4e0b\u540c\uff0c\u7c7b\u4f3c\u9519\u8bef\u3002<\/p>\n<\/p>\n 2\uff09\u3001\u9047\u5230\u5982\u4e0b\u95ee\u9898<\/p>\n <\/p>\n \u5728Misc Controls \u9009\u9879\u4e2d\u52a0\u5165--gnu<\/p>\n <\/p>\n 3\uff09\u3001\u9047\u5230ssize_t\u672a\u5b9a\u4e49\u95ee\u9898<\/p>\n \u5728mpconfigport.h\u91cc\u5b9a\u4e49\uff1atypedef int ssize_t; \u4e14\u5c4f\u853d\u62a5\u9519\u6e90\u6587\u4ef6\u4e2d \u5305\u542b\u7684\u5934\u6587\u4ef6 #include <unistd.h>\u3002<\/p>\n <\/p>\n 4\uff09\u3001\u63a5\u53e3\u672a\u5b9a\u4e49<\/p>\n \u6839\u636e\u62a5\u9519\uff0c\u5c06shared\\readline\u3001shared\\runtime\u7b49\u6587\u4ef6\u5939\u4e0b\u6587\u4ef6\u52a0\u5165\u7f16\u8bd1\u7cfb\u7edf\u3002<\/p>\n 5\uff09\u3001\u63a7\u5236\u53f0\u4e32\u53e3\u6536\u53d1<\/p>\n \u63a7\u5236\u53f0\u4e32\u53e3\u6536\u53d1\u51fd\u6570\u5b9a\u4e49\u5728uart_core.c\u4e2d\uff0c\u672c\u4f8b\u4f7f\u7528STM32\u7684\u4e32\u53e33\uff0c\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b<\/p>\n python\u89e3\u91ca\u5668\u521d\u59cb\u5316\u6e90\u6587\u4ef6py_init.c(\u9700\u8981\u81ea\u5df1\u589e\u52a0\uff0c\u6839\u636eports\/minimal.main.c\u6539\u9020\u800c\u6765)\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n \u65b0\u5efa\u4e00\u4e2a\u7528\u6237\u4efb\u52a1\uff0c\u4efb\u52a1\u51fd\u6570\u4e3aphase2_user_task\uff0c\u5728\u6b64\u4efb\u52a1\u4e2d\u8c03\u7528micro_python_init\uff0c\u542f\u52a8python\u89e3\u91ca\u5668\uff0c\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n \u7f16\u8bd1\uff0c\u7f16\u8bd1\u901a\u8fc7\uff0c\u5982\u4e0b\u56fe\uff0c\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u4e0a\u673a\u8c03\u8bd5\u5566\u3002<\/p>\n <\/p>\n<\/p>\n \u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n <\/p>\n \u9047\u5230\u89e3\u51b3\u4e0d\u4e86\u7684\u95ee\u9898\u8bf7\u8bc4\u8bba\u533a\u89c1<\/span>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4e09\u3001MicroPython\u79fb\u690d\u5230\u4efb\u610f\u5e73\u53f0\uff08\u7f16\u8bd1\u7bc7\uff09micropython\u79fb\u690d\u5230\u4efb\u610f\u5e73\u53f0\uff0c\u7f16\u8bd1\u7bc7_micropythonkeil\u6587\u4ef6","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9118"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=9118"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/9118\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=9118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=9118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=9118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\nnote:<\/h3>\n
\u4e00\u3001\u5de5\u7a0b\u6587\u4ef6<\/h3>\n
1\u3001\u6e90\u7801\u6587\u4ef6\u5939<\/h4>\n
2\u3001\u4e2d\u95f4\u6e90\u6587\u4ef6\u62f7\u8d1d<\/h4>\n
3\u3001\u6587\u4ef6\u91cd\u547d\u540d<\/h4>\n
\u4e8c\u3001\u7f16\u8bd1\u5de5\u7a0b\u914d\u7f6e\uff08Keil\uff09<\/h3>\n
1\u3001\u65b0\u5efa\u5206\u7ec4<\/h4>\n
2\u3001\u6dfb\u52a0\u6587\u4ef6\u5230\u7f16\u8bd1<\/h4>\n
\u4e09\u3001\u7f16\u8bd1<\/h3>\n
#define MICROPY_NLR_SETJMP (1)
#define MP_UNREACHABLE for (;;);
#define MICROPY_USE_INTERNAL_ERRNO (1)<\/p>\n1\u3001\u4fee\u6539\u7f16\u8bd1\u62a5<\/h4>\n
\/*\n * Core UART functions to implement for a port\n *\/\n#include \"main.h\"\ntypedef struct {\n volatile uint32_t SR;\n volatile uint32_t DR;\n} periph_uart_t;\n#define USART_IO ((periph_uart_t *)USART3)\n\n\/\/ Receive single character\nint mp_hal_stdin_rx_chr(void) {\n unsigned char c = 0;\n while ((USART_IO->SR & (1 << 5)) == 0) {\n }\n c = USART_IO->DR;\n return c;\n}\n\n\/\/ Send string of given length\nvoid mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {\n while (len--) {\n \/\/ wait for TXE\n while ((USART_IO->SR & (1 << 7)) == 0) {\n }\n USART_IO->DR = *str++;\n }\n}<\/code><\/pre>\n
\u56db\u3001\u7f16\u8bd1\u901a\u8fc7<\/h3>\n
1\u3001python\u89e3\u91ca\u5668\u521d\u59cb\u5316\u5165\u53e3<\/h4>\n
#include <stdint.h>\n#include <stdio.h>\n#include <string.h>\n\n#include \"builtin.h\"\n#include \"compile.h\"\n#include \"runtime.h\"\n#include \"repl.h\"\n#include \"gc.h\"\n#include \"mperrno.h\"\n#include \"pyexec.h\"\n\n#if MICROPY_ENABLE_COMPILER\nvoid do_str(const char *src, mp_parse_input_kind_t input_kind) {\n nlr_buf_t nlr;\n if (nlr_push(&nlr) == 0) {\n mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);\n qstr source_name = lex->source_name;\n mp_parse_tree_t parse_tree = mp_parse(lex, input_kind);\n mp_obj_t module_fun = mp_compile(&parse_tree, source_name, true);\n mp_call_function_0(module_fun);\n nlr_pop();\n } else {\n \/\/ uncaught exception\n mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);\n }\n}\n#endif\n\nstatic char *stack_top;\n#if MICROPY_ENABLE_GC\nstatic char heap[1024 * 10];\n#endif\n\nint micro_python_init(void) {\n int stack_dummy;\n stack_top = (char *)&stack_dummy;\n\n #if MICROPY_ENABLE_GC\n gc_init(heap, heap + sizeof(heap));\n #endif\n mp_init();\n #if MICROPY_ENABLE_COMPILER\n #if MICROPY_REPL_EVENT_DRIVEN\n pyexec_event_repl_init();\n for (;;) {\n int c = mp_hal_stdin_rx_chr();\n if (pyexec_event_repl_process_char(c)) {\n break;\n }\n }\n #else\n pyexec_friendly_repl();\n #endif\n \/\/ do_str(\"print('hello world!', list(x+1 for x in range(10)), end='eol\\\\n')\", MP_PARSE_SINGLE_INPUT);\n \/\/ do_str(\"for i in range(10):\\r\\n print(i)\", MP_PARSE_FILE_INPUT);\n #else\n pyexec_frozen_module(\"frozentest.py\", false);\n #endif\n mp_deinit();\n return 0;\n}\n\n#if MICROPY_ENABLE_GC\nvoid gc_collect(void) {\n \/\/ WARNING: This gc_collect implementation doesn't try to get root\n \/\/ pointers from CPU registers, and thus may function incorrectly.\n void *dummy;\n gc_collect_start();\n gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) \/ sizeof(mp_uint_t));\n gc_collect_end();\n gc_dump_info(&mp_plat_print);\n}\n#endif\n\nmp_lexer_t *mp_lexer_new_from_file(const char *filename) {\n mp_raise_OSError(MP_ENOENT);\n}\n\nmp_import_stat_t mp_import_stat(const char *path) {\n return MP_IMPORT_STAT_NO_EXIST;\n}\n\nvoid nlr_jump_fail(void *val) {\n while (1) {\n ;\n }\n}\n\nvoid NORETURN __fatal_error(const char *msg) {\n while (1) {\n ;\n }\n}\n\n#ifndef NDEBUG\nvoid MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {\n printf(\"Assertion '%s' failed, at file %s:%d\\n\", expr, file, line);\n __fatal_error(\"Assertion failed\");\n}\n#endif<\/code><\/pre>\n
2\u3001\u542f\u52a8\u7528\u6237\u4efb\u52a1\u6267\u884cpython\u89e3\u91ca\u5668<\/h4>\n
static void phase2_user_task( void *pvParameters )\n{\n while(1)\n {\n micro_python_init();\n vTaskDelay(50000);\n break;\n }\n}<\/code><\/pre>\n
\u4e94\u3001\u4e0a\u673a\u8fd0\u884c<\/h3>\n
1\u3001\u4e0a\u673a\u8fd0\u884c\u7ed3\u679c<\/h4>\n
\u516d\u3001\u603b\u7ed3<\/h3>\n