├── .gitignore ├── LICENSE ├── README.md ├── README_ZH.md ├── SConscript ├── docs ├── assets │ ├── 08_direct_run_files.gif │ ├── IoT_Board.png │ ├── W60x_HW_origin.png │ ├── add_main_stack.png │ ├── c-gen.png │ ├── check_memory.png │ ├── check_pandora_examples.png │ ├── config_runtime.png │ ├── en_connect_board.gif │ ├── mount_fs.png │ ├── mpy-usage-demo.png │ ├── open_filesync_option.png │ ├── powershell_mpycross_deploy.png │ ├── select_micropython.png │ ├── select_mpy_package.png │ ├── sparrow_example.png │ ├── sparrow_one_board.png │ ├── tools-mpy-download.png │ └── w601_examples.png ├── external_c_modules.md ├── firmware-develop.md ├── introduction.md ├── micropython-ide.md ├── micropython-librarys.md ├── micropython-mpy.md ├── micropython_for_pandora_iot_board.md ├── micropython_for_sparrow_one_board.md ├── micropython_for_w601_iot_board.md ├── spec-librarys │ ├── machine.md │ ├── machine │ │ ├── ADC.md │ │ ├── I2C.md │ │ ├── LCD.md │ │ ├── PWM.md │ │ ├── Pin.md │ │ ├── RTC.md │ │ ├── SPI.md │ │ ├── Timer.md │ │ ├── UART.md │ │ └── WDT.md │ ├── micropython.md │ ├── network.md │ ├── network │ │ └── wlan.md │ └── rtthread.md ├── std-librarys │ ├── _thread.md │ ├── builtins.md │ ├── cmath.md │ ├── gc.md │ ├── math.md │ ├── rtthread.md │ ├── sys.md │ ├── uarray.md │ ├── ubinascii.md │ ├── ucollections.md │ ├── uctypes.md │ ├── uerrno.md │ ├── uhashlib.md │ ├── uheapq.md │ ├── uio.md │ ├── ujson.md │ ├── uos.md │ ├── urandom.md │ ├── ure.md │ ├── uselect.md │ ├── usocket.md │ ├── ussl.md │ ├── ustruct.md │ ├── utime.md │ └── uzlib.md └── tools-mpy-cross.md ├── drivers └── bus │ ├── qspi.h │ ├── softqspi.c │ ├── softspi.c │ └── spi.h ├── extmod ├── axtls-include │ ├── config.h │ └── version.h ├── crypto-algorithms │ ├── sha256.c │ └── sha256.h ├── lwip-include │ ├── arch │ │ ├── cc.h │ │ └── perf.h │ └── lwipopts.h ├── machine_i2c.c ├── machine_i2c.h ├── machine_mem.c ├── machine_mem.h ├── machine_pinbase.c ├── machine_pinbase.h ├── machine_pulse.c ├── machine_pulse.h ├── machine_signal.c ├── machine_signal.h ├── machine_spi.c ├── machine_spi.h ├── misc.h ├── modbtree.c ├── modframebuf.c ├── modonewire.c ├── modubinascii.c ├── modubinascii.h ├── moducryptolib.c ├── moductypes.c ├── moduhashlib.c ├── moduheapq.c ├── modujson.c ├── modurandom.c ├── modure.c ├── moduselect.c ├── modussl_axtls.c ├── modussl_mbedtls.c ├── modutimeq.c ├── moduwebsocket.c ├── moduwebsocket.h ├── moduzlib.c ├── modwebrepl.c ├── modwebsocket.c ├── modwebsocket.h ├── re1.5 │ ├── charclass.c │ ├── compilecode.c │ ├── dumpcode.c │ ├── re1.5.h │ └── recursiveloop.c ├── uos_dupterm.c ├── utime_mphal.c ├── utime_mphal.h ├── uzlib │ ├── adler32.c │ ├── crc32.c │ ├── defl_static.h │ ├── tinf.h │ ├── tinf_compat.h │ ├── tinfgzip.c │ ├── tinflate.c │ ├── tinfzlib.c │ ├── uzlib.h │ └── uzlib_conf.h ├── vfs.c ├── vfs.h ├── vfs_fat.c ├── vfs_fat.h ├── vfs_fat_diskio.c ├── vfs_fat_file.c ├── vfs_posix.c ├── vfs_posix.h ├── vfs_posix_file.c ├── vfs_reader.c ├── virtpin.c ├── virtpin.h └── webrepl │ ├── manifest.py │ ├── webrepl.py │ ├── webrepl_setup.py │ └── websocket_helper.py ├── lib ├── mp-readline │ ├── readline.c │ └── readline.h ├── netutils │ ├── netutils.c │ └── netutils.h ├── oofatfs │ ├── diskio.h │ ├── ff.c │ ├── ff.h │ ├── ffconf.h │ └── option │ │ ├── ccsbcs.c │ │ └── unicode.c ├── timeutils │ ├── timeutils.c │ └── timeutils.h └── utils │ ├── gchelper.h │ ├── gchelper_m0.s │ ├── gchelper_m3.s │ ├── interrupt_char.c │ ├── interrupt_char.h │ ├── mpirq.c │ ├── mpirq.h │ ├── printf.c │ ├── pyexec.c │ ├── pyexec.h │ ├── stdout_helpers.c │ └── sys_stdio_mphal.c ├── port ├── frozen_mpy.c ├── gccollect.c ├── genhdr │ ├── gen_qstr.py │ ├── moduledefs.h │ ├── mpversion.h │ └── qstrdefs.generated.h ├── modules │ ├── machine │ │ ├── machine_adc.c │ │ ├── machine_adc.h │ │ ├── machine_hw_i2c.c │ │ ├── machine_hw_spi.c │ │ ├── machine_lcd.c │ │ ├── machine_lcd.h │ │ ├── machine_pin.c │ │ ├── machine_pwm.c │ │ ├── machine_pwm.h │ │ ├── machine_rtc.c │ │ ├── machine_rtc.h │ │ ├── machine_timer.c │ │ ├── machine_timer.h │ │ ├── machine_uart.c │ │ ├── machine_uart.h │ │ ├── machine_wdt.c │ │ ├── machine_wdt.h │ │ ├── modmachine.c │ │ └── modmachine.h │ ├── modffi.c │ ├── modfile.c │ ├── modnetwork.c │ ├── modnetwork.h │ ├── modnetwork_wlan.c │ ├── modpyb.c │ ├── modrtthread.c │ ├── moduos.c │ ├── moduos_file.c │ ├── moduos_file.h │ ├── modusocket.c │ ├── modutils.c │ ├── modutime.c │ └── user │ │ └── moduserfunc.c ├── mpconfigport.h ├── mpgetcharport.c ├── mpgetcharport.h ├── mphalport.c ├── mphalport.h ├── mpputsnport.c ├── mpputsnport.h ├── mpthreadport.c ├── mpthreadport.h ├── mpy_main.c ├── mpy_project_cfg.h ├── native │ ├── easyflash_module.c │ ├── easyflash_module.py │ ├── native_module.c │ └── native_module.py └── qstrdefsport.h ├── py ├── argcheck.c ├── asmarm.c ├── asmarm.h ├── asmbase.c ├── asmbase.h ├── asmthumb.c ├── asmthumb.h ├── asmx64.c ├── asmx64.h ├── asmx86.c ├── asmx86.h ├── asmxtensa.c ├── asmxtensa.h ├── bc.c ├── bc.h ├── bc0.h ├── binary.c ├── binary.h ├── builtin.h ├── builtinevex.c ├── builtinhelp.c ├── builtinimport.c ├── compile.c ├── compile.h ├── dynruntime.h ├── dynruntime.mk ├── emit.h ├── emitbc.c ├── emitcommon.c ├── emitglue.c ├── emitglue.h ├── emitinlinethumb.c ├── emitinlinextensa.c ├── emitnarm.c ├── emitnative.c ├── emitnthumb.c ├── emitnx64.c ├── emitnx86.c ├── emitnxtensa.c ├── emitnxtensawin.c ├── formatfloat.c ├── formatfloat.h ├── frozenmod.c ├── frozenmod.h ├── gc.c ├── gc.h ├── grammar.h ├── lexer.c ├── lexer.h ├── makecompresseddata.py ├── makemoduledefs.py ├── makeqstrdata.py ├── makeqstrdefs.py ├── makeversionhdr.py ├── malloc.c ├── map.c ├── misc.h ├── mkenv.mk ├── mkrules.mk ├── modarray.c ├── modbuiltins.c ├── modcmath.c ├── modcollections.c ├── modgc.c ├── modio.c ├── modmath.c ├── modmicropython.c ├── modstruct.c ├── modsys.c ├── modthread.c ├── moduerrno.c ├── mpconfig.h ├── mperrno.h ├── mphal.h ├── mpprint.c ├── mpprint.h ├── mpstate.c ├── mpstate.h ├── mpthread.h ├── mpz.c ├── mpz.h ├── nativeglue.c ├── nativeglue.h ├── nlr.c ├── nlr.h ├── nlrpowerpc.c ├── nlrsetjmp.c ├── nlrthumb.c ├── nlrx64.c ├── nlrx86.c ├── nlrxtensa.c ├── obj.c ├── obj.h ├── objarray.c ├── objarray.h ├── objattrtuple.c ├── objbool.c ├── objboundmeth.c ├── objcell.c ├── objclosure.c ├── objcomplex.c ├── objdeque.c ├── objdict.c ├── objenumerate.c ├── objexcept.c ├── objexcept.h ├── objfilter.c ├── objfloat.c ├── objfun.c ├── objfun.h ├── objgenerator.c ├── objgenerator.h ├── objgetitemiter.c ├── objint.c ├── objint.h ├── objint_longlong.c ├── objint_mpz.c ├── objlist.c ├── objlist.h ├── objmap.c ├── objmodule.c ├── objmodule.h ├── objnamedtuple.c ├── objnamedtuple.h ├── objnone.c ├── objobject.c ├── objpolyiter.c ├── objproperty.c ├── objrange.c ├── objreversed.c ├── objset.c ├── objsingleton.c ├── objslice.c ├── objstr.c ├── objstr.h ├── objstringio.c ├── objstringio.h ├── objstrunicode.c ├── objtuple.c ├── objtuple.h ├── objtype.c ├── objtype.h ├── objzip.c ├── opmethods.c ├── pairheap.c ├── pairheap.h ├── parse.c ├── parse.h ├── parsenum.c ├── parsenum.h ├── parsenumbase.c ├── parsenumbase.h ├── persistentcode.c ├── persistentcode.h ├── profile.c ├── profile.h ├── py.mk ├── pystack.c ├── pystack.h ├── qstr.c ├── qstr.h ├── qstrdefs.h ├── reader.c ├── reader.h ├── repl.c ├── repl.h ├── ringbuf.c ├── ringbuf.h ├── runtime.c ├── runtime.h ├── runtime0.h ├── runtime_utils.c ├── scheduler.c ├── scope.c ├── scope.h ├── sequence.c ├── showbc.c ├── smallint.c ├── smallint.h ├── stackctrl.c ├── stackctrl.h ├── stream.c ├── stream.h ├── unicode.c ├── unicode.h ├── vm.c ├── vmentrytable.h ├── vstr.c └── warning.c └── tools └── mpy-cross ├── hellortt.mpy ├── hellortt.py └── mpy-cross.exe /.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode/settings.json 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013, 2014 Damien P. George 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | from building import * 2 | import rtconfig 3 | 4 | # get current directory 5 | cwd = GetCurrentDir() 6 | # The set of source files associated with this SConscript file. 7 | src = Glob('py/*.c') 8 | src += Glob('lib/mp-readline/*.c') 9 | src += Glob('lib/utils/*.c') 10 | src += Glob('extmod/*.c') 11 | src += Glob('port/*.c') 12 | src += Glob('port/modules/*.c') 13 | src += Glob('port/modules/machine/*.c') 14 | src += Glob('port/modules/user/*.c') 15 | src += Glob('lib/netutils/*.c') 16 | src += Glob('lib/timeutils/*.c') 17 | src += Glob('drivers/bus/*.c') 18 | src += Glob('port/native/*.c') 19 | 20 | path = [cwd + '/'] 21 | path += [cwd + '/port'] 22 | path += [cwd + '/port/modules'] 23 | path += [cwd + '/port/modules/machine'] 24 | 25 | LOCAL_CCFLAGS = '' 26 | 27 | if rtconfig.PLATFORM in ['gcc', 'armclang']: 28 | LOCAL_CCFLAGS += ' -std=gnu99' 29 | elif rtconfig.PLATFORM in ['keil']: 30 | LOCAL_CCFLAGS += ' --c99 --gnu' 31 | 32 | group = DefineGroup('MicroPython', src, depend = ['PKG_USING_MICROPYTHON'], CPPPATH = path, LOCAL_CCFLAGS = LOCAL_CCFLAGS) 33 | 34 | Return('group') 35 | -------------------------------------------------------------------------------- /docs/assets/08_direct_run_files.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/08_direct_run_files.gif -------------------------------------------------------------------------------- /docs/assets/IoT_Board.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/IoT_Board.png -------------------------------------------------------------------------------- /docs/assets/W60x_HW_origin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/W60x_HW_origin.png -------------------------------------------------------------------------------- /docs/assets/add_main_stack.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/add_main_stack.png -------------------------------------------------------------------------------- /docs/assets/c-gen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/c-gen.png -------------------------------------------------------------------------------- /docs/assets/check_memory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/check_memory.png -------------------------------------------------------------------------------- /docs/assets/check_pandora_examples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/check_pandora_examples.png -------------------------------------------------------------------------------- /docs/assets/config_runtime.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/config_runtime.png -------------------------------------------------------------------------------- /docs/assets/en_connect_board.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/en_connect_board.gif -------------------------------------------------------------------------------- /docs/assets/mount_fs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/mount_fs.png -------------------------------------------------------------------------------- /docs/assets/mpy-usage-demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/mpy-usage-demo.png -------------------------------------------------------------------------------- /docs/assets/open_filesync_option.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/open_filesync_option.png -------------------------------------------------------------------------------- /docs/assets/powershell_mpycross_deploy.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/powershell_mpycross_deploy.png -------------------------------------------------------------------------------- /docs/assets/select_micropython.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/select_micropython.png -------------------------------------------------------------------------------- /docs/assets/select_mpy_package.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/select_mpy_package.png -------------------------------------------------------------------------------- /docs/assets/sparrow_example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/sparrow_example.png -------------------------------------------------------------------------------- /docs/assets/sparrow_one_board.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/sparrow_one_board.png -------------------------------------------------------------------------------- /docs/assets/tools-mpy-download.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/tools-mpy-download.png -------------------------------------------------------------------------------- /docs/assets/w601_examples.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/docs/assets/w601_examples.png -------------------------------------------------------------------------------- /docs/external_c_modules.md: -------------------------------------------------------------------------------- 1 | # 为 MicroPython 扩展 C 模块 2 | 3 | 当使用原生 MicroPython 进行开发时,你可能会遇到这样一些限制,比如官方没有实现自己想要的功能,或者你觉得这些实现不符合自己的工作需求。此时,添加自己的 C 模块到 MicroPython 中是一个不错的选择,你可以按照自己的想法,设计适合自己的 Python 函数调用。 4 | 5 | 为了帮助各位开发者快速添加 C 模块,RT-Thread 提供了相应的辅助工具 [C 绑定代码自动生成器](https://summerlife.github.io/RT-MicroPython-Generator/)。该工具可以帮助开发者自动生成 C 代码和 MicroPython 之间的接口层,开发者只需将 C 语言编写的功能代码添加到指定位置,MicroPython 即可直接调用该功能。 6 | 7 | ## Python 调用 C 函数的实现原理 8 | 9 | C 语言和 Python 是两种完全不同的语言,如何使用 MicroPython 来调用 C 语言所实现的函数是许多小伙伴非常疑惑的地方。简单来说,这个问题的关键点在于,如何用 C 语言的形式在 MicroPython 源代码中**表述函数的入参和出参**。我举一个例子来讲解这个问题, 请观察如下 Python 函数: 10 | 11 | ```python 12 | def add(a, b): 13 | return a + b 14 | ``` 15 | 16 | 该函数有两个入参,返回一个参数。此时如果能用 C 语言表示该 **Python 函数的输入输出参数**,就可以将一个实现该功能的 C 函数对接到 MicroPython 中。 17 | 18 | ### 添加用户函数到 MicroPython 19 | 20 | 假设上述函数的参数类型都为整形,通过自动生成器可以得到如下样板函数: 21 | 22 | ```c 23 | STATIC mp_obj_t add( 24 | mp_obj_t arg_1_obj, 25 | mp_obj_t arg_2_obj) { 26 | mp_int_t arg_1 = mp_obj_get_int(arg_1_obj); /* 通过 Python 获取的第一个整形参数 arg_1 */ 27 | mp_int_t arg_2 = mp_obj_get_int(arg_2_obj); /* 通过 Python 获取的第二个整形参数 arg_2 */ 28 | mp_int_t ret_val; 29 | 30 | /* Your code start! */ 31 | 32 | ret_val = arg_1 + arg_2; /* 处理入参 arg_1 和 arg_2,并将结果赋给返回值 ret_val */ 33 | 34 | /* Your code end! */ 35 | 36 | return mp_obj_new_int(ret_val); /* 向 python 返回整形参数 ret_val */ 37 | } 38 | MP_DEFINE_CONST_FUN_OBJ_2(add_obj, add); 39 | ``` 40 | 41 | 生成器会处理好需要导出到 MicroPython 的函数的入参和出参,而开发者只需要编写相应的代码来处理这些输入参数,并且把返回值赋给输出参数即可。 通过包含头文件的方式,可以调用先前编写的 C 函数来对输入参数进行处理,或者根据输入参数来执行相应的动作,添加控制硬件的驱动的原理也是一样的。 42 | 43 | 最终使用 Python 调用 C 函数的效果如下: 44 | 45 | ```python 46 | >>> import userfunc 47 | >>> userfunc.add(666,777) 48 | 1443 49 | ``` 50 | 51 | ### 添加用户模块到 MicroPython 52 | 53 | 添加用户模块到 MicroPython 中也不难,首先应当熟练掌握上述添加 C 函数的过程,然后参考 PR [add module userfunc to MicroPython](https://github.com/RT-Thread-packages/micropython/pull/144) 来添加属于自己的模块,该 PR 实现了添加 `userfunc` 模块到 MicroPython 的功能,你可以按照同样的方式将自己编写的模块注册到 MicroPython 中,要注意仔细查看这个 PR 中修改的 4 个文件,不要漏掉修改的细节。 54 | -------------------------------------------------------------------------------- /docs/introduction.md: -------------------------------------------------------------------------------- 1 | # MicroPython 入门必读 2 | 3 | 本文档将初步介绍 MicroPython 的基本概念,RT-Thread MicroPython 的特性与优势,以及可以被用在哪些领域。 4 | 5 | ## 主要特性 6 | 7 | - MicroPython 是 Python 3 编程语言的一种精简而高效的实现,它包含 Python 标准库的一个子集,并被优化为在微控制器和受限环境中运行。 8 | 9 | - RT-Thread MicroPython 可以运行在任何搭载了 RT-Thread 操作系统并且有一定资源的嵌入式平台上。 10 | 11 | - MicroPython 可以运行在有一定资源的开发板上,给你一个低层次的 Python 操作系统,可以用来控制各种电子系统。 12 | 13 | - MicroPython 富有各种高级特性,比如交互式提示、任意精度整数、闭包函数、列表解析、生成器、异常处理等等。 14 | 15 | - MicroPython 的目标是尽可能与普通 Python 兼容,使开发者能够轻松地将代码从桌面端转移到微控制器或嵌入式系统。程序可移植性很强,因为不需要考虑底层驱动,所以程序移植变得轻松和容易。 16 | 17 | ## MicroPython 的优势 18 | 19 | - Python 是一款容易上手的脚本语言,同时具有强大的功能,语法优雅简单。使用 MicroPython 编程可以降低嵌入式的开发门槛,让更多的人体验嵌入式的乐趣。 20 | - 通过 MicroPython 实现硬件底层的访问和控制,不需要了解底层寄存器、数据手册、厂家的库函数等,即可轻松控制硬件。 21 | - 外设与常用功能都有相应的模块,降低开发难度,使开发和移植变得容易和快速。 22 | 23 | ## MicroPython 的应用领域 24 | 25 | - MicroPython 在嵌入式系统上完整实现了 Python3 的核心功能,可以在产品开发的各个阶段给开发者带来便利。 26 | - 通过 MicroPython 提供的库和函数,开发者可以快速控制 LED、液晶、舵机、多种传感器、SD、UART、I2C 等,实现各种功能,而不用再去研究底层硬件模块的使用方法,翻看寄存器手册。这样不但降低了开发难度,而且减少了重复开发工作,可以加快开发速度,提高开发效率。以前需要较高水平的嵌入式工程师花费数天甚至数周才能完成的功能,现在普通的嵌入式开发者用几个小时就能实现类似的功能。 27 | - 随着半导体技术的不断发展,芯片的功能、内部的存储器容量和资源不断增加,成本不断降低,可以使用 MicroPython 来进行开发设计的应用领域也会越来越多。 28 | 29 | ### 产品原型验证 30 | 31 | - 众所周知,在开发新产品时,原型设计是一个非常重要的环节,这个环节需要以最快速的方式设计出产品的大致模型,并验证业务流程或者技术点。与传统开发方法相比,使用 MicroPython 对于原型验证非常有用,让原型验证过程变得轻松,加速原型验证过程。 32 | - 在进行一些物联网功能开发时,网络功能也是 MicroPython 的长处,可以利用现成的众多 MicroPython 网络模块,节省开发时间。而这些功能如果使用 C/C++ 来完成,会耗费几倍的时间。 33 | 34 | ### 硬件测试 35 | 36 | - 嵌入式产品在开发时,一般会分为硬件开发及软件开发。硬件工程师并不一定都擅长软件开发,所以在测试新硬件时,经常需要软件工程师参与。这就导致软件工程师可能会耗费很多时间帮助硬件工程师查找设计或者焊接问题。有了 MicroPython 后,将 MicroPython 固件烧入待测试的新硬件,在检查焊接、连线等问题时,只需使用简单的 Python 命令即可测试。这样,硬件工程师一人即可搞定,再也不用麻烦别人了。 37 | 38 | ### 创客 DIY 39 | 40 | - MicroPython 无需复杂的设置,不需要安装特别的软件环境和额外的硬件,使用任何文本编辑器就可以进行编程。大部分硬件功能,使用一个命令就能驱动,不用了解硬件底层就能快速开发。这些特性使得 MicroPython 非常适合创客使用来开发一些有创意的项目。 41 | - 下面是使用 MicroPython 开发的一些 DIY 项目: 42 | - [显示温湿度的 WIFI 时钟](https://www.bilibili.com/video/av15929152?from=search&seid=16285206333541196172) 43 | - [OpenMV 智能摄像头](https://www.bilibili.com/video/av16418889?from=search&seid=16285206333541196172) 44 | - [快速实现人脸识别](https://www.bilibili.com/video/av73853903?from=search&seid=9793819178982436353) 45 | - [搭建 MQTT 服务器](http://www.360doc.com/content/17/1218/22/8473307_714341237.shtml) 46 | 47 | ### 教育 48 | 49 | - MicroPython 使用简单、方便,非常适合于编程入门。在校学生或者业余爱好者都可以通过 MicroPython 快速的开发一些好玩的项目,在开发的过程中学习编程思想,提高自己的动手能力。 50 | -------------------------------------------------------------------------------- /docs/micropython-ide.md: -------------------------------------------------------------------------------- 1 | # MicroPython IDE 2 | 3 | RT-Thread 为广大开发者提供了 VSCode 最好用的 MicroPython 插件 来帮助大家使用 MicroPython 来开发应用程序。该插件为 MicroPython 开发提供了功能强大的开发环境,主要特性如下: 4 | 5 | - 便捷的开发板连接方式(串口、网络、USB) 6 | - 支持基于 MicroPython 的代码智能补全与语法检查 7 | - 支持 MicroPython REPL 交互环境 8 | - 提供丰富的代码示例与 demo 程序 9 | - 提供工程同步功能 10 | - 支持下载单个文件或文件夹至开发板 11 | - 支持在内存中快速运行代码文件功能 12 | - 支持运行代码片段功能 13 | - 支持多款主流 MicroPython 开发板 14 | - 支持 Windows、Ubuntu、Mac 操作系统 15 | 16 | ### 安装 IDE 开发环境 17 | 18 | 开发者可以通过 RT-Thread MicroPython IDE 来快速开发 MicroPython 应用,下图展示了 IDE 的快速调试功能: 19 | 20 | ![08_direct_run_files](assets/08_direct_run_files.gif) 21 | 22 | 可通过查看如下文档进一步了解并使用 RT-Thread MicroPython IDE: 23 | 24 | - [RT-Thread MicroPython Develop Environment](https://marketplace.visualstudio.com/items?itemName=RT-Thread.rt-thread-micropython) -------------------------------------------------------------------------------- /docs/micropython-librarys.md: -------------------------------------------------------------------------------- 1 | # MicroPython 库 2 | 3 | ### MicroPython 标准库 4 | 5 | - [`Builtin functions and exceptions`](std-librarys/builtins.md) – 内置函数与异常 6 | - [`cmath`](std-librarys/cmath.md) – 复数运算函数功能 7 | - [`gc`](std-librarys/gc.md) – 控制垃圾收集器 8 | - [`math`](std-librarys/math.md) – 数学函数功能 9 | - [`sys`](std-librarys/sys.md) – 系统特定功能 10 | - [`uarray`](std-librarys/uarray.md) – 数组存储功能 11 | - [`ubinascii`](std-librarys/ubinascii.md) – 二进制与 ASCII 码转换功能 12 | - [`ucollections`](std-librarys/ucollections.md) – 集合与容器类型 13 | - [`uerrno`](std-librarys/uerrno.md) – 系统错误码 14 | - [`uhashlib`](std-librarys/uhashlib.md) – 哈希算法 15 | - [`uheapq`](std-librarys/uheapq.md) – 堆队列算法 16 | - [`uio`](std-librarys/uio.md) – 输入输出流 17 | - [`ujson`](std-librarys/ujson.md) – JSON 编解码 18 | - [`uos`](std-librarys/uos.md) – 基本的操作系统服务 19 | - [`ure`](std-librarys/ure.md) – 正则表达式 20 | - [`uselect`](std-librarys/uselect.md) – 在一组 streams 上等待事件 21 | - [`usocket`](std-librarys/usocket.md) – socket 模块 22 | - [`ussl`](std-librarys/ussl.md) – SSL/TLS 模块 23 | - [`ustruct`](std-librarys/ustruct.md) – 原生数据类型的打包和解包 24 | - [`utime`](std-librarys/utime.md) – 时间相关功能 25 | - [`uzlib`](std-librarys/uzlib.md) – zlib 解压 26 | - [`_thread`](std-librarys/_thread.md) – 多线程支持 27 | 28 | ### MicroPython 特定库 29 | 30 | 在 RT-Thread 移植的 MicroPython 版本中,实现了如下特定功能库: 31 | 32 | - [`micropython`](spec-librarys/micropython.md) – 实现 MicroPython 内部功能访问与控制 33 | - [`rtthread`](spec-librarys/rtthread.md) – RT-Thread 系统功能模块 34 | - [`machine`](spec-librarys/machine.md) – 硬件控制模块 35 | - [Pin](spec-librarys/machine/Pin.md) 36 | - [I2C ](spec-librarys/machine/I2C.md) 37 | - [SPI](spec-librarys/machine/SPI.md) 38 | - [UART](spec-librarys/machine/UART.md) 39 | - [LCD](spec-librarys/machine/LCD.md) 40 | - [RTC](spec-librarys/machine/RTC.md) 41 | - [PWM](spec-librarys/machine/PWM.md) 42 | - [ADC](spec-librarys/machine/ADC.md) 43 | - [WDT](spec-librarys/machine/WDT.md) 44 | - [TIMER](spec-librarys/machine/Timer.md) 45 | 46 | - [`network`](spec-librarys/network.md) – 网络功能配置模块 47 | - [wlan](spec-librarys/network/wlan.md) 48 | -------------------------------------------------------------------------------- /docs/micropython-mpy.md: -------------------------------------------------------------------------------- 1 | # MicroPython .mpy 文件详解 2 | 3 | MicroPython 定义了 `.mpy` 文件的概念,该文件是一种二进制容器文件格式,在其中包含了预编译的 Python 代码,这种类型的文件可以像普通的 `.py` 模块一样被导入到 MicroPython 程序中。举个例子来说明这种类型文件的使用方法。例如,只要 `foo.mpy` 存在于指定的路径中,我们就可以通过 `import foo` 语句来导入 `foo.mpy` 文件。 4 | 5 | 这种类型文件的导入规则是这样的,首先按顺序搜索 `sys.path` 中列出的每个目录。当搜索特定目录时,首先查找 `foo.py`,如果找不到该目录,则查找 `foo.mpy`,如果没有找到,则在下一个目录中继续搜索。通过这种方式,`foo.py` 文件的优先级将高于 `foo.mpy` 文件。这些 `.mpy` 文件中的主要内容是字节码,这种类型的文件可以通过 `mpy-cross` 程序从 Python 源文件(`.py`文件)生成。 6 | 7 | -------------------------------------------------------------------------------- /docs/micropython_for_sparrow_one_board.md: -------------------------------------------------------------------------------- 1 | # MicroPython for sparrow one board 2 | 3 | ![sparrow_one](assets/sparrow_one_board.png) 4 | 5 | [**麻雀一号开发板**](https://item.taobao.com/item.htm?spm=a1z10.1-c.w4004-5210898174.2.29401ae39JyGKY&id=606684373403) 是 RT-Thread 推出的一款物联网音视频开发板,现在它已经全面支持 **MicroPython** 。在麻雀一号开发板上,你将会体验到有别于传统的,前所未有的开发方式。 6 | 7 | 借助于 MicroPython,你将有能力使用 Python 语言控制所有硬件外设,体验高级语言带来的便利特性,与此同时你还可以利用高级软件库快速实现你的 IoT 构想。 8 | 9 | ## 硬件支持 10 | 11 | 麻雀一号开发板固件硬件功能如下所示: 12 | 13 | | 外设名称 | 简介 | 14 | | --------- | ---------------------------------------------------------- | 15 | | key | 输入按键 | 16 | | uart1 | 串口1 | 17 | | lcd | lcd 显示屏 | 18 | | wifi | wifi 网络连接 | 19 | | bluetooth | 蓝牙 | 20 | | player | 扬声器,音频播放 | 21 | | recorder | 麦克风,录音功能 | 22 | | camera | 摄像头,可拍照并存入文件系统,开启 TCP Server 查看实时图像 | 23 | 24 | ## 入门必读 25 | 26 | 如果您从来没有了解过 MicroPython, 可以阅读这篇简短的文章来 [带你入门 MicroPython](https://github.com/RT-Thread-packages/micropython/blob/master/docs/introduction.md)。 27 | 28 | ## 开启 MicroPython 之旅 29 | 30 | 推荐遵循如下步骤开始进行 MicroPython 开发: 31 | 32 | - 在您的开发板上烧录合适的固件 33 | - 在 PC 机上安装 RT-Thread MicroPython 开发环境并连接上开发板 34 | 35 | 接下来就可以尽情挥洒您的创意了,更详细的内容可以点击下文中的链接来进一步了解。 36 | 37 | ### 下载合适的固件 38 | 39 | - [Sparrow One Board firmware](https://www.rt-thread.org/qa/forum.php?mod=viewthread&tid=12305&extra=page%3D1%26filter%3Dtypeid%26typeid%3D20) 40 | 41 | ### 安装 IDE 开发环境 42 | 43 | - [RT-Thread MicroPython develop environment](https://marketplace.visualstudio.com/items?itemName=RT-Thread.rt-thread-micropython) 44 | 45 | ## 开发资料 46 | 47 | ### 示例程序 48 | 49 | 以下示例程序可以在 RT-Thread MicroPython IDE 开发环境中直接添加到工程: 50 | 51 | ![sparrow_example](assets/sparrow_example.png) 52 | 53 | ### MicroPython 模块详解 54 | 55 | - [MicroPython Librarys](https://github.com/RT-Thread-packages/micropython/blob/master/docs/micropython-librarys.md) 56 | 57 | 58 | ## 联系我们 59 | 60 | 如果在使用的过程中遇到问题,您可以用如下方式联系我们: 61 | 62 | - 在 github 上提交 issue 63 | - [RT-Thread MicroPython 官方论坛](https://www.rt-thread.org/qa/forum.php?mod=forumdisplay&fid=2&filter=typeid&typeid=20) 64 | 65 | - RT-Thread MicroPython 交流 QQ 群:703840633 66 | -------------------------------------------------------------------------------- /docs/spec-librarys/machine.md: -------------------------------------------------------------------------------- 1 | ## **machine** – 与硬件相关的功能 2 | 3 | **machine** 模块包含与特定开发板上的硬件相关的特定函数。 在这个模块中的大多数功能允许实现直接和不受限制地访问和控制系统上的硬件块(如CPU,定时器,总线等)。如果使用不当,会导致故障,死机,崩溃,在极端的情况下,硬件会损坏。 4 | 5 | 需要注意的是,由于不同开发板的硬件资源不同,MicroPython 移植所能控制的硬件也是不一样的。因此对于控制硬件的例程来说,在使用前需要修改相关的配置参数来适配不同的开发板,或者直接运行已经对某一开发板适配好的 MicroPython 示例程序。本文档中的例程都是基于 RT-Thread IoT Board 潘多拉开发板而讲解的。 6 | 7 | ### 函数 8 | 9 | #### 复位相关函数 10 | 11 | ##### **machine.info**() 12 | 显示关于系统介绍和内存占用等信息。 13 | 14 | ##### **machine.reset**() 注:暂未实现 15 | 重启设备,类似于按下复位按钮。 16 | 17 | ##### **machine.reset_cause**() 注:暂未实现 18 | 获得复位的原因,查看可能的返回值的常量。 19 | 20 | #### 中断相关函数 21 | 22 | ##### **machine.disable_irq**() 23 | 禁用中断请求。返回先前的 `IRQ` 状态,该状态应该被认为是一个未知的值。这个返回值应该在 `disable_irq` 函数被调用之前被传给 `enable_irq` 函数来重置中断到初始状态。 24 | 25 | ##### **machine.enable_irq**(state) 26 | 重新使能中断请求。状态参数应该是从最近一次禁用功能的调用中返回的值。 27 | 28 | #### 功耗相关函数 29 | 30 | ##### **machine.freq**() 31 | 返回 `CPU` 的运行频率。 32 | 33 | ##### **machine.idle**() 注:暂未实现 34 | 阻断给 `CPU` 的时钟信号,在较短或者较长的周期里减少功耗。当中断发生时,外设将继续工作。 35 | 36 | ##### **machine.sleep**() 注:暂未实现 37 | 停止 `CPU` 并禁止除了 `WLAN` 之外的所有外设。系统会从睡眠请求的地方重新恢复工作。为了确保唤醒一定会发生,应当首先配置中断源。 38 | 39 | ##### **machine.deepsleep**() 注:暂未实现 40 | 停止 `CPU` 和所有外设(包括网络接口)。执行从主函数中恢复,就像被复位一样。复位的原因可以检查 `machine.DEEPSLEEP` 参数获得。为了确保唤醒一定会发生,应该首先配置中断源,比如一个引脚的变换或者 `RTC` 的超时。 41 | -------------------------------------------------------------------------------- /docs/spec-librarys/machine/ADC.md: -------------------------------------------------------------------------------- 1 | ## machine.ADC 2 | 3 | **machine.ADC** 类是 machine 模块下的一个硬件类,用于指定 ADC 设备的配置和控制,提供对 ADC 设备的操作方法。 4 | 5 | - ADC(Analog-to-Digital Converter,模数转换器),用于将连续变化的模拟信号转化为离散的数字信号。 6 | - ADC 设备两个重要参数:采样值、分辨率; 7 | - 采样值:当前时间由模拟信号转化的数值信号的数值; 8 | - 分辨率:以二进制(或十进制)数的位数来表示,一般有 8 位、10 位、12 位、16 位等,它说明模数转换器对输入信号的分辨能力,位数越多,表示分辨率越高,采样值会更精确。 9 | 10 | ### 构造函数 11 | 12 | 在 RT-Thread MicroPython 中 `ADC` 对象的构造函数如下: 13 | 14 | #### **class machine.ADC**(id, channel) 15 | 16 | - **id**:使用的 ADC 设备编号,`id = 1` 表示编号为 1 的 ADC 设备,或者表示使用的 ADC 设备名,如 `id = "adc"` 表示设备名为 `adc` 的 ADC 设备; 17 | - **channel**:使用的 ADC 设备通道号,每个 ADC 设备对应多个通道; 18 | 19 | 例如:`ADC(1,4)` 表示当前使用编号为 1 的 ADC 设备的 4 通道。 20 | 21 | ### 方法 22 | 23 | #### **ADC.init**(channel) 24 | 25 | 根据输入的层参数初始化 ADC 对象,入参为使用的 ADC 对象通道号; 26 | 27 | #### **ADC.deinit**() 28 | 29 | 用于关闭 ADC 对象,ADC 对象 deinit 之后需要重新 init 才能使用。 30 | 31 | #### **ADC.read**() 32 | 33 | 用于获取并返回当前 ADC 对象的采样值。例如当前采样值为 2048,对应设备的分辨率为 12位,当前设备参考电压为 3.3V ,则该 ADC 对象通道上实际电压值的计算公式为:**采样值 * 参考电压 / (1 << 分辨率位数)**,即 `vol = 2048 / 4096 * 3.3 V = 1.15V`。 34 | 35 | ### 示例 36 | 37 | ``` python 38 | >>> from machine import ADC # 从 machine 导入 ADC 类 39 | >>> adc = ADC(1, 13) # 创建 ADC 对象,当前使用编号为 1 的 ADC 设备的 13 通道 40 | >>> adc.read() # 获取 ADC 对象采样值 41 | 4095 42 | >>> adc.deinit() # 关闭 ADC 对象 43 | >>> adc.init(13) # 开启并重新配置 ADC 对象 44 | ``` 45 | -------------------------------------------------------------------------------- /docs/spec-librarys/machine/LCD.md: -------------------------------------------------------------------------------- 1 | ## machine.LCD 2 | 3 | **machine.LCD** 类是 machine 模块下面的一个硬件类,用于对 LCD 的配置和控制,提供对 LCD 设备的操作方法。 4 | 5 | IoT board 板载一块 1.3 寸,分辨率为 `240*240` 的 LCD 显示屏,因此对该屏幕操作时,(x, y) 坐标的范围是 `0 - 239`。 6 | 7 | ### 构造函数 8 | 9 | 在 RT-Thread MicroPython 中 `LCD` 对象的构造函数如下: 10 | 11 | #### **class machine.LCD**() 12 | 在给定总线上构造一个 `LCD` 对象,无入参,初始化的对象取决于特定硬件,初始化方式可参考 [示例](#_3)。 13 | 14 | ### 方法 15 | 16 | #### **LCD.light**(value) 17 | 18 | 控制是否开启 LCD 背光,入参为 True 则打开 LCD 背光,入参为 False 则关闭 LCD 背光。 19 | 20 | #### **LCD.fill**(color) 21 | 22 | 根据给定的颜色填充整个屏幕,支持多种颜色,可以传入的参数有: 23 | 24 | ``` 25 | WHITE BLACK BLUE BRED GRED GBLUE RED MAGENTA GREEN CYAN YELLOW BROWN BRRED GRAY GRAY175 GRAY151 GRAY187 GRAY240 26 | ``` 27 | 28 | 详细的使用方法可参考[示例](#_3)。 29 | 30 | #### **LCD.pixel**(x, y, color) 31 | 32 | 向指定的位置(x, y)画点,点的颜色为 color 指定的颜色,可指定的颜色和上一个功能相同。 33 | 34 | > 注意:(x, y) 坐标不要超过实际范围,使用下面的方法对坐标进行操作时同样需要遵循此限制。 35 | 36 | #### **LCD.text**(str, x, y, size) 37 | 38 | 在指定的位置(x,y)写入字符串,字符串由 str 指定,字体的大小由 size 指定,size 的大小可为 16,24,32。 39 | 40 | #### **LCD.line**(x1, y1, x2, y2) 41 | 42 | 在 LCD 上画一条直线,起始地址为 (x1, y1),终点为(x2, y2)。 43 | 44 | #### **LCD.rectangle**(x1, y1, x2, y2) 45 | 46 | 在 LCD 上画一个矩形,左上角的位置为(x1, y1),右下角的位置为(x2, y2)。 47 | 48 | #### **LCD.circle**(x1, y1, r) 49 | 50 | 在 LCD 上画一个圆形,圆心的位置为(x1, y1),半径长度为 r。 51 | 52 | #### **LCD.show_bmp**( x, y, pathname) 53 | 54 | 在 LCD 指定位置上显示 32-bit bmp 格式的图片信息,注意显示 bmp 图片时,(x, y) 坐标是图片的左下角。 55 | 56 | ### 示例 57 | 58 | ```python 59 | from machine import LCD # 从 machine 导入 LCD 类 60 | lcd = LCD() # 创建一个 lcd 对象 61 | lcd.light(False) # 关闭背光 62 | lcd.light(True) # 打开背光 63 | lcd.fill(lcd.BLACK) # 将整个 LCD 填充为黑色 64 | lcd.fill(lcd.RED) # 将整个 LCD 填充为红色 65 | lcd.fill(lcd.GRAY) # 将整个 LCD 填充为灰色 66 | lcd.fill(lcd.WHITE) # 将整个 LCD 填充为白色 67 | lcd.pixel(50, 50, lcd.BLUE) # 将(50,50)位置的像素填充为蓝色 68 | lcd.text("hello RT-Thread", 0, 0, 16) # 在(0, 0) 位置以 16 字号打印字符串 69 | lcd.text("hello RT-Thread", 0, 16, 24) # 在(0, 16)位置以 24 字号打印字符串 70 | lcd.text("hello RT-Thread", 0, 48, 32) # 在(0, 48)位置以 32 字号打印字符串 71 | lcd.line(0, 50, 239, 50) # 以起点(0,50),终点(239,50)画一条线 72 | lcd.line(0, 50, 239, 50) # 以起点(0,50),终点(239,50)画一条线 73 | lcd.rectangle(100, 100, 200, 200) # 以左上角为(100,100),右下角(200,200)画矩形 74 | lcd.circle(150, 150, 80) # 以圆心位置(150,150),半径为 80 画圆 75 | lcd.show_bmp(180, 50, "sun.bmp") # 以位置(180,50)为图片左下角坐标显示文件系统中的 bmp 图片 "sun.bmp" 76 | ``` 77 | -------------------------------------------------------------------------------- /docs/spec-librarys/machine/PWM.md: -------------------------------------------------------------------------------- 1 | ## machine.PWM 2 | 3 | **machine.PWM** 类是 machine 模块下的一个硬件类,用于指定 PWM 设备的配置和控制,提供对 PWM 设备的操作方法。 4 | 5 | - PWM (Pulse Width Modulation,脉冲宽度调制) 是一种对模拟信号电平进行数字编码的方式; 6 | - PWM 设备可以通过调节有效电平在一个周期信号中的比例时间来操作设备; 7 | - PWM 设备两个重要的参数:频率(freq)和占空比(duty); 8 | - 频率:从一个上升沿(下降沿)到下一个上升沿(下降沿)的时间周期,单位为 Hz; 9 | - 占空比:有效电平(通常为电平)在一个周期内的时间比例; 10 | 11 | ### 构造函数 12 | 13 | 在 RT-Thread MicroPython 中 `PWM` 对象的构造函数如下: 14 | 15 | #### **class machine.PWM**(id, channel, freq, duty) 16 | 17 | 在给定的总线上构建一个 `PWM` 对象,参数介绍如下: 18 | 19 | - **id**:使用的 PWM 设备编号,如 `id = 1` 表示编号为 1 的 PWM 设备,或者表示使用的 PWM 设备名,如 `id = "pwm"` 表示设备名为 `pwm` 的 PWM 设备; 20 | - **channel**:使用的 PWM 设备通道号,每个 PWM 设备包含多个通道,范围为 [0, 4]; 21 | - **freq**:初始化频率,范围 [1, 156250]; 22 | - **duty**:初始化占空比数值,范围 [0 255]; 23 | 24 | 例如:`PWM(1,4,100,100)` 表示当前使用 编号为 1 的 PWM 设备的 4 通道,初始化频率为 1000 Hz,初始化占空比的数值为 100。 25 | 26 | ### 方法 27 | 28 | #### **PWM.init**(channel, freq, duty) 29 | 30 | 根据输入的参数初始化 PWM 对象,参数说明同上。 31 | 32 | #### **PWM.deinit**() 33 | 34 | 用于关闭 PWM 对象,对象 deinit 之后需要重新 init 才能使用。 35 | 36 | #### **PWM.freq**(freq) 37 | 38 | 用于获取或者设置 PWM 对象的频率,频率的范围为 [1, 156250]。如果参数为空,返回当前 PWM 对象的频率;如果参数非空,则使用该参数设置当前 PWM 对象的频率。 39 | 40 | #### **PWM.duty**(duty) 41 | 42 | 用于获取或者设置 PWM 对象的占空比数值,占空比数值的范围为 [0, 255],例如 `duty = 100`,表示当前设备占空比为 `100/255 = 39.22%` 。如果参数为空,返回当前 PWM 对象的占空比数值;如果参数非空,则使用该参数设置当前 PWM 对象的占空比数值。 43 | 44 | ### 示例 45 | 46 | ``` python 47 | >>> from machine import PWM # 从 machine 导入 PWM 类 48 | >>> pwm = PWM(3, 3, 1000, 100) # 创建 PWM 对象,当前使用编号为 3 的 PWM 设备的 3 通道,初始化的频率为 1000Hz,占空比数值为 100(占空比为 100/255 = 39.22%) 49 | >>> pwm.freq(2000) # 设置 PWM 对象频率 50 | >>> pwm.freq() # 获取 PWM 对象频率 51 | 2000 52 | >>> pwm.duty(200) # 设置 PWM 对象占空比数值 53 | >>> pwm.duty() # 获取 PWM 对象占空比数值 54 | 200 55 | >>> pwm.deinit() # 关闭 PWM 对象 56 | >>> pwm.init(3, 1000, 100) # 开启并重新配置 PWM 对象 57 | ``` -------------------------------------------------------------------------------- /docs/spec-librarys/machine/RTC.md: -------------------------------------------------------------------------------- 1 | ## machine.RTC 2 | 3 | **machine.RTC** 类是 machine 模块下面的一个硬件类,用于对指定 RTC 设备的配置和控制,提供对 RTC 设备的操作方法。 4 | 5 | - RTC(Real-Time Clock )实时时钟可以提供精确的实时时间,它可以用于产生年、月、日、时、分、秒等信息。 6 | 7 | ### 构造函数 8 | 9 | 在 RT-Thread MicroPython 中 `RTC` 对象的构造函数如下: 10 | 11 | #### **class machine.RTC**() 12 | 13 | 所以在给定的总线上构造一个 `RTC` 对象,无入参对象,使用方式可参考 [示例](#_3)。 14 | 15 | ### 方法 16 | 17 | #### **RTC.init**(datetime) 18 | 19 | 根据传入的参数初始化 RTC 设备起始时间。入参 `datetime` 为一个时间元组,格式如下: 20 | 21 | ``` 22 | (year, month, day, wday, hour, minute, second, yday) 23 | ``` 24 | 参数介绍如下所示: 25 | 26 | - **year**:年份; 27 | - **month**:月份,范围 [1, 12]; 28 | - **day**:日期,范围 [1, 31]; 29 | - **wday**:星期,范围 [0, 6],0 表示星期一,以此类推; 30 | - **hour**:小时,范围 [0, 23]; 31 | - **minute**:分钟,范围[0, 59]; 32 | - **second**:秒,范围[0, 59]; 33 | - **yday**:从当前年份 1 月 1 日开始的天数,范围 [0, 365],一般置位 0 未实现。 34 | 35 | 使用的方式可参考 [示例](#_3)。 36 | 37 | #### **RTC.deinit**() 38 | 39 | 重置 RTC 设备时间到 2015 年 1 月 1日,重新运行 RTC 设备。 40 | 41 | #### **RTC.now**() 42 | 43 | 获取当前时间,返回值为上述 `datetime` 时间元组格式。 44 | 45 | ### 示例 46 | 47 | ```python 48 | >>> from machine import RTC 49 | >>> rtc = RTC() # 创建 RTC 设备对象 50 | >>> rtc.init((2019,6,5,2,10,22,30,0)) # 设置初始化时间 51 | >>> rtc.now() # 获取当前时间 52 | (2019, 6, 5, 2, 10, 22, 40, 0) 53 | >>> rtc.deinit() # 重置时间到2015年1月1日 54 | >>> rtc.now() # 获取当前时间 55 | (2015, 1, 1, 3, 0, 0, 1, 0) 56 | ``` 57 | -------------------------------------------------------------------------------- /docs/spec-librarys/machine/UART.md: -------------------------------------------------------------------------------- 1 | ## machine.UART 2 | 3 | **machine.UART** 类是 machine 模块下面的一个硬件类,用于对 UART 的配置和控制,提供对 UART 设备的操作方法。 4 | 5 | `UART` 实现了标准的 `uart/usart` 双工串行通信协议,在物理层上,他由两根数据线组成:`RX` 和 `TX`。通信单元是一个字符,它可以是 8 或 9 位宽。 6 | 7 | ### 构造函数 8 | 9 | 在 RT-Thread MicroPython 中 `UART` 对象的构造函数如下: 10 | 11 | #### **class machine.UART**(id, ...) 12 | 在给定总线上构造一个 `UART` 对象,`id` 取决于特定的移植。 13 | 初始化参数可以参考下面的 `UART.init` 方法。 14 | 15 | 使用硬件 UART 在初始化时只需传入 `UART` 设备的编号即可,如传入 `1` 表示 `uart1` 设备。 16 | 初始化方式可参考 [示例](#_3)。 17 | 18 | ### 方法 19 | 20 | #### **UART.init**(baudrate = 9600, bits=8, parity=None, stop=1) 21 | - **baudrate** :`SCK` 时钟频率。 22 | - **bits** :每次发送数据的长度。 23 | - **parity** :校验方式。 24 | - **stop** :停止位的长度。 25 | 26 | #### **UART.deinit**() 27 | 关闭串口总线。 28 | 29 | #### **UART.read**([nbytes]) 30 | 读取字符,如果指定读 n 个字节,那么最多读取 n 个字节,否则就会读取尽可能多的数据。 31 | 返回值:一个包含读入数据的字节对象。如果如果超时则返回 `None`。 32 | 33 | #### **UART.readinto**(buf[, nbytes]) 34 | 读取字符到 `buf` 中,如果指定读 n 个字节,那么最多读取 n 个字节,否则就读取尽可能多的数据。另外读取数据的长度不超过 `buf` 的长度。 35 | 返回值:读取和存储到 `buf` 中的字节数。如果超时则返回 `None`。 36 | 37 | #### **UART.readline**() 38 | 读一行数据,以换行符结尾。 39 | 返回值:读入的行数,如果超时则返回 `None`。 40 | 41 | #### **UART.write**(buf) 42 | 将 `buf` 中的数据写入总线。 43 | 返回值:写入的字节数,如果超时则返回 `None`。 44 | 45 | ### 示例 46 | 47 | 在构造函数的第一个参数传入`1`,系统就会搜索名为 `uart1` 的设备,找到之后使用这个设备来构建 `UART` 对象: 48 | 49 | ```python 50 | from machine import UART 51 | uart = UART(1, 115200) # init with given baudrate 52 | uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters 53 | uart.read(10) # read 10 characters, returns a bytes object 54 | uart.read() # read all available characters 55 | uart.readline() # read a line 56 | uart.readinto(buf) # read and store into the given buffer 57 | uart.write('abc') # write the 3 characters 58 | ``` 59 | 60 | 更多内容可参考 [machine.UART](http://docs.micropython.org/en/latest/pyboard/library/machine.UART.html) 。 -------------------------------------------------------------------------------- /docs/spec-librarys/machine/WDT.md: -------------------------------------------------------------------------------- 1 | ## machine.WDT 2 | 3 | **machine.WDT** 类是 machine 模块下的一个硬件类,用于 WDT 设备的配置和控制,提供对 WDT 设备的操作方法。 4 | 5 | 如下为 WDT 设备基本介绍: 6 | 7 | - WDT(WatchDog Timer,硬件看门狗),是一个定时器设备,用于系统程序结束或出错导致系统进入不可恢复状态时重启系统。 8 | 9 | - WDT 启动之后,计数器开始计数,在计数器溢出前没有被复位,会对 CPU 产生一个复位信号使设备重启(简称 “被狗咬”); 10 | 11 | - 系统正常运行时,需要在 WDT 设备允许的时间间隔内对看门狗计数清零(简称“喂狗”),WDT 设备一旦启动,需要定时“喂狗”以确保设备正常运行。 12 | 13 | ### 构造函数 14 | 15 | 在 RT-Thread MicroPython 中 `WDT` 对象的构造函数如下: 16 | 17 | #### **class machine.WDT**(id = "wdt", timeout=5) 18 | 19 | - **id**: 使用的 WDT 设备编号,`id = 1` 表示编号为 1 的 WDT 设备,或者表示使用的 WDT 设备名,如 `id = "wdt"` 表示设备名为 `wdt` 的 WDT 设备; 20 | 21 | - **timeout**:设置看门狗超时时间,单位:秒(s); 22 | 23 | 用于创建一个 WDT 对象并且启动 WDT 功能。一旦启动,设置的超时时间无法改动,WDT 功能无法停止。 24 | 25 | 如果该函数入参为空,则设置超时时间为 5 秒;如果入参非空,则使用该入参设置 WDT 超时时间,超时时间最小设置为 1 秒。 26 | 27 | ### 方法 28 | 29 | #### **WDT.feed**() 30 | 31 | 用于执行“喂狗”操作,清空看门狗设备计数。应用程序应该合理的周期性调用该函数,以防止系统重启。 32 | 33 | ### 示例 34 | 35 | ``` python 36 | >>> from machine import WDT # 从 machine 导入 WDT 类 37 | >>> wdt = WDT() # 创建 WDT 对象,默认超时时间为 5 秒 38 | >>> wdt = WDT(10) # 创建 WDT 对象,设置超时时间为 10 秒 39 | >>> wdt.feed() # 在 10 秒超时时间内需要执行“喂狗”操作,清空看门狗设备计数,否则系统将重启 40 | ``` 41 | 42 | 更多内容可参考 [machine.WDT](http://docs.micropython.org/en/latest/library/machine.WDT.html) 。 43 | -------------------------------------------------------------------------------- /docs/spec-librarys/network.md: -------------------------------------------------------------------------------- 1 | ## network – 网络配置 2 | 3 | 此模块提供网络驱动程序和路由配置。特定硬件的网络驱动程序在此模块中可用,用于配置硬件网络接口。然后,配置接口提供的网络服务可以通过 `usocket` 模块使用。 4 | 5 | ### 专用的网络类配置 6 | 7 | 下面具体的类实现了抽象网卡的接口,并提供了一种控制各种网络接口的方法。 8 | 9 | - [class WLAN – control built-in WiFi interfaces](network/wlan.md) 10 | 11 | -------------------------------------------------------------------------------- /docs/spec-librarys/rtthread.md: -------------------------------------------------------------------------------- 1 | ## **rtthread** – 系统相关函数 2 | 3 | **rtthread** 模块提供了与 RT-Thread 操作系统相关的功能,如查看栈使用情况等。 4 | 5 | ### 函数 6 | 7 | #### rtthread.current_tid() 8 | 返回当前线程的 id 。 9 | 10 | #### rtthread.is_preempt_thread() 11 | 返回是否是可抢占线程。 12 | 13 | ### 示例 14 | 15 | ``` 16 | >>> import rtthread 17 | >>> 18 | >>> rtthread.is_preempt_thread() # determine if it's a preemptible thread 19 | True 20 | >>> rtthread.current_tid() # current thread id 21 | 268464956 22 | >>> 23 | ``` 24 | -------------------------------------------------------------------------------- /docs/std-librarys/_thread.md: -------------------------------------------------------------------------------- 1 | ## _thread – 多线程支持 2 | 3 | `_thread` 模块提供了用于处理多线程的基本方法——多个控制线程共享它们的全局数据空间。为了实现同步,提供了简单的锁(也称为互斥锁或二进制信号量)。 4 | 5 | ### 示例 6 | 7 | ```python 8 | import _thread 9 | import time 10 | def testThread(): 11 | while True: 12 | print("Hello from thread") 13 | time.sleep(2) 14 | 15 | _thread.start_new_thread(testThread, ()) 16 | while True: 17 | pass 18 | ``` 19 | 20 | 输出结果(启动新的线程,每隔两秒打印字符): 21 | 22 | Hello from thread 23 | Hello from thread 24 | Hello from thread 25 | Hello from thread 26 | Hello from thread 27 | 28 | 更多内容可参考 [_thread](http://docs.micropython.org/en/latest/pyboard/library/_thread.html) 。 29 | -------------------------------------------------------------------------------- /docs/std-librarys/cmath.md: -------------------------------------------------------------------------------- 1 | ## **cmath** – 复数运算函数功能 2 | 3 | `cmath` 模块提供了一些用于复数运算的函数。这个模块中的函数接受整数、浮点数或复数作为参数。他们还将接受任何有复数或浮点方法的 Python 对象:这些方法分别用于将对象转换成复数或浮点数,然后将该函数应用到转换的结果中。 4 | 5 | ### 函数 6 | 7 | #### **cmath.cos**(z) 8 | 返回``z``的余弦。 9 | 10 | #### **cmath.exp**(z) 11 | 返回``z``的指数。 12 | 13 | #### **cmath.log**(z) 14 | 返回``z``的对数。 15 | 16 | #### **cmath.log10**(z) 17 | 返回``z``的常用对数。 18 | 19 | #### **cmath.phase**(z) 20 | 返回``z``的相位, 范围是(-pi, +pi],以弧度表示。 21 | 22 | #### **cmath.polar**(z) 23 | 返回``z``的极坐标。 24 | 25 | #### **cmath.rect**(r, phi) 26 | 返回`模量r`和相位``phi``的复数。 27 | 28 | #### **cmath.sin**(z) 29 | 返回``z``的正弦。 30 | 31 | #### **cmath.sqrt**(z) 32 | 返回``z``的平方根。 33 | 34 | ### 常数 35 | 36 | #### **cmath.e** 37 | 自然对数的指数。 38 | 39 | #### **cmath.pi** 40 | 圆周率。 41 | 42 | 更多内容可参考 [cmath](http://docs.micropython.org/en/latest/pyboard/library/cmath.html) 。 43 | -------------------------------------------------------------------------------- /docs/std-librarys/gc.md: -------------------------------------------------------------------------------- 1 | ## **gc** – 控制垃圾回收 2 | 3 | **gc** 模块提供了垃圾收集器的控制接口。 4 | 5 | ### 函数 6 | 7 | #### **gc.enable**() 8 | 允许自动回收内存碎片。 9 | 10 | #### **gc.disable**() 11 | 禁止自动回收,但可以通过collect()函数进行手动回收内存碎片。 12 | 13 | #### **gc.collect**() 14 | 运行一次垃圾回收。 15 | 16 | #### **gc.mem_alloc**() 17 | 返回已分配的内存数量。 18 | 19 | #### **gc.mem_free**() 20 | 返回剩余的内存数量。 21 | 22 | 更多内容可参考 [gc](http://docs.micropython.org/en/latest/pyboard/library/gc.html) 。 23 | -------------------------------------------------------------------------------- /docs/std-librarys/rtthread.md: -------------------------------------------------------------------------------- 1 | ## **rtthread** – 系统相关函数 2 | 3 | **rtthread** 模块提供了与 RT-Thread 操作系统相关的功能,如查看栈使用情况等。 4 | 5 | ### 函数 6 | 7 | #### rtthread.current_tid() 8 | 返回当前线程的 id 。 9 | 10 | #### rtthread.is_preempt_thread() 11 | 返回是否是可抢占线程。 12 | 13 | #### rtthread.stacks_analyze() 14 | 返回当前系统线程和栈使用信息。 15 | 16 | ### 示例 17 | 18 | ``` 19 | >>> import rtthread 20 | >>> 21 | >>> rtthread.is_preempt_thread() # determine if code is running in a preemptible thread 22 | True 23 | >>> rtthread.current_tid() # current thread id 24 | 268464956 25 | >>> rtthread.stacks_analyze() # show thread information 26 | thread pri status sp stack size max used left tick error 27 | ---------- --- ------- ---------- ---------- ------ ---------- --- 28 | elog_async 31 suspend 0x000000a8 0x00000400 26% 0x00000003 000 29 | tshell 20 ready 0x00000260 0x00001000 39% 0x00000003 000 30 | tidle 31 ready 0x00000070 0x00000100 51% 0x0000000f 000 31 | SysMonitor 30 suspend 0x000000a4 0x00000200 32% 0x00000005 000 32 | timer 4 suspend 0x00000080 0x00000200 25% 0x00000009 000 33 | >>> 34 | ``` 35 | -------------------------------------------------------------------------------- /docs/std-librarys/sys.md: -------------------------------------------------------------------------------- 1 | ## **sys** – 系统特有功能函数 2 | 3 | **sys** 模块提供系统特有的功能。 4 | 5 | ### 函数 6 | 7 | #### **sys.exit**(retval=0) 8 | 终止当前程序给定的退出代码。 函数会抛出 `SystemExit` 异常。 9 | #### **sys.print_exception**(exc, file=sys.stdout) 10 | 打印异常与追踪到一个类似文件的对象 file (或者缺省 `sys.stdout` ). 11 | 12 | > 提示:这是 CPython 中回溯模块的简化版本。不同于 `traceback.print_exception()`,这个函数用异常值代替了异常类型、异常参数和回溯对象。文件参数在对应位置,不支持更多参数。CPython 兼容回溯模块在 `micropython-lib`。 13 | 14 | ### 常数 15 | 16 | #### **sys.argv** 17 | 当前程序启动时参数的可变列表。 18 | 19 | #### **sys.byteorder** 20 | 系统字节顺序 (“little” or “big”). 21 | 22 | #### **sys.implementation** 23 | 关于当前 Python 实现的信息,对于 MicroPython 来说,有以下属性: 24 | - 名称 - ‘’micropython“ 25 | - 版本 - 元组(主要,次要,小),比如(1,9,3) 26 | 27 | #### **sys.modules** 28 | 已加载模块的字典。在一部分移植中,它可能不包含内置模块。 29 | 30 | #### **sys.path** 31 | 用来搜索导入模块地址的列表。 32 | 33 | #### **sys.platform** 34 | 返回当前平台的信息。 35 | 36 | #### **sys.stderr** 37 | 标准错误流。 38 | 39 | #### **sys.stdin** 40 | 标准输入流。 41 | 42 | #### **sys.stdout** 43 | 标准输出流。 44 | 45 | #### **sys.version** 46 | 符合的 Python 语言版本,如字符串。 47 | 48 | #### **sys.version_info** 49 | 本次实现使用的 Python 语言版本,用一个元组的方式表示。 50 | 51 | ### 示例 52 | 53 | ``` 54 | >>> import sys 55 | >>> sys.version 56 | '3.4.0' 57 | >>> sys.version_info 58 | (3, 4, 0) 59 | >>> sys.path 60 | ['', '/libs/mpy/'] 61 | >>> sys.__name__ 62 | 'sys' 63 | >>> sys.platform 64 | 'rt-thread' 65 | >>> sys.byteorder 66 | 'little' 67 | ``` 68 | 69 | 更多内容可参考 [sys](http://docs.micropython.org/en/latest/pyboard/library/sys.html) 。 70 | -------------------------------------------------------------------------------- /docs/std-librarys/uarray.md: -------------------------------------------------------------------------------- 1 | ## **array** – 数字数据数组 2 | 3 | **array** 模块定义了一个对象类型,它可以简洁地表示基本值的数组:字符、整数、浮点数。支持代码格式: b, B, h, H, i, I, l, L, q, Q, f, d (最后2个需要支持浮点数)。 4 | 5 | ### 构造函数 6 | 7 | #### **class array.array**(typecode[, iterable]) 8 | 用给定类型的元素创建数组。数组的初始内容由 iterable 提供,如果没有提供,则创建一个空数组。 9 | 10 | ``` 11 | typecode:数组的类型 12 | iterable:数组初始内容 13 | ``` 14 | 15 | 示例: 16 | 17 | ```python 18 | >>> import array 19 | >>> a = array.array('i', [2, 4, 1, 5]) 20 | >>> b = array.array('f') 21 | >>> print(a) 22 | array('i', [2, 4, 1, 5]) 23 | >>> print(b) 24 | array('f') 25 | ``` 26 | 27 | ### 方法 28 | 29 | #### **array.append**(val) 30 | 将一个新元素追加到数组的末尾。 31 | 32 | 示例: 33 | 34 | ```python 35 | >>> a = array.array('f', [3, 6]) 36 | >>> print(a) 37 | array('f', [3.0, 6.0]) 38 | >>> a.append(7.0) 39 | >>> print(a) 40 | array('f', [3.0, 6.0, 7.0]) 41 | ``` 42 | 43 | #### **array.extend**(iterable) 44 | 将一个新的数组追加到数组的末尾,注意追加的数组和原来数组的数据类型要保持一致。 45 | 46 | 示例: 47 | 48 | ```python 49 | >>> a = array.array('i', [1, 2, 3]) 50 | >>> b = array.array('i', [4, 5]) 51 | >>> a.extend(b) 52 | >>> print(a) 53 | array('i', [1, 2, 3, 4, 5]) 54 | ``` 55 | 56 | 更多内容可参考 [array](http://docs.micropython.org/en/latest/pyboard/library/array.html) 。 57 | -------------------------------------------------------------------------------- /docs/std-librarys/ubinascii.md: -------------------------------------------------------------------------------- 1 | ## **ubinascii** – 二进制/ ASCII转换 2 | 3 | `ubinascii` 模块包含许多在二进制和各种 ascii 编码的二进制表示之间转换的方法。 4 | 5 | ### 函数 6 | 7 | #### **ubinascii.hexlify**(data[, sep]) 8 | 将字符串转换为十六进制表示的字符串。 9 | 10 | 示例: 11 | 12 | ``` 13 | >>> ubinascii.hexlify('hello RT-Thread') 14 | b'68656c6c6f2052542d546872656164' 15 | >>> ubinascii.hexlify('summer') 16 | b'73756d6d6572' 17 | ``` 18 | 如果指定了第二个参数sep,它将用于分隔两个十六进制数。 19 | 20 | 示例: 21 | 22 | ``` 23 | 如果指定了第二个参数sep,它将用于分隔两个十六进制数。 24 | 示例: 25 | >>> ubinascii.hexlify('hello RT-Thread'," ") 26 | b'68 65 6c 6c 6f 20 52 54 2d 54 68 72 65 61 64' 27 | >>> ubinascii.hexlify('hello RT-Thread',",") 28 | b'68,65,6c,6c,6f,20,52,54,2d,54,68,72,65,61,64' 29 | ``` 30 | 31 | #### **ubinascii.unhexlify**(data) 32 | 转换十六进制字符串为二进制字符串,功能和 hexlify 相反。 33 | 34 | 示例: 35 | 36 | ``` 37 | >>> ubinascii.unhexlify('73756d6d6572') 38 | b'summer' 39 | ``` 40 | 41 | #### **ubinascii.a2b_base64**(data) 42 | Base64编码的数据转换为二进制表示。返回字节串。 43 | 44 | #### **ubinascii.b2a_base64**(data) 45 | 编码base64格式的二进制数据。返回的字符串。 46 | 47 | 更多内容可参考 [ubinascii](http://docs.micropython.org/en/latest/pyboard/library/ubinascii.html) 。 48 | -------------------------------------------------------------------------------- /docs/std-librarys/ucollections.md: -------------------------------------------------------------------------------- 1 | ## **ucollections** – 集合与容器类型 2 | 3 | **ucollections** 模块实现了专门的容器数据类型,它提供了 Python 的通用内置容器的替代方案,包括了字典、列表、集合和元组。 4 | 5 | ### 类 6 | 7 | #### **ucollections.namedtuple**(name, fields) 8 | 这是工厂函数创建一个新的 `namedtuple` 型与一个特定的字段名称和集合。`namedtuple` 是元组允许子类要访问它的字段不仅是数字索引,而且还具有属性使用符号字段名访问语法。 字段是字符串序列指定字段名称。为了兼容的实现也可以用空间分隔的字符串命名的字段(但效率较低) 。 9 | 10 | 代码示例: 11 | ```python 12 | from ucollections import namedtuple 13 | 14 | MyTuple = namedtuple("MyTuple", ("id", "name")) 15 | t1 = MyTuple(1, "foo") 16 | t2 = MyTuple(2, "bar") 17 | print(t1.name) 18 | assert t2.name == t2[1] 19 | ucollections.OrderedDict(...) 20 | ``` 21 | 22 | #### **ucollections.OrderedDict**(...) 23 | 字典类型的子类,会记住并保留键/值的追加顺序。当有序的字典被迭代输出时,键/值 会按照他们被添加的顺序返回 : 24 | 25 | ```python 26 | from ucollections import OrderedDict 27 | 28 | # To make benefit of ordered keys, OrderedDict should be initialized 29 | # from sequence of (key, value) pairs. 30 | d = OrderedDict([("z", 1), ("a", 2)]) 31 | # More items can be added as usual 32 | d["w"] = 5 33 | d["b"] = 3 34 | for k, v in d.items(): 35 | print(k, v) 36 | ``` 37 | 输出: 38 | 39 | z 1 40 | a 2 41 | w 5 42 | b 3 43 | 44 | 更多的内容可参考 [ucollections](http://docs.micropython.org/en/latest/pyboard/library/ucollections.html) 。 45 | -------------------------------------------------------------------------------- /docs/std-librarys/uctypes.md: -------------------------------------------------------------------------------- 1 | ## **uctypes** – 以结构化的方式访问二进制数据 2 | 3 | uctypes 模块用来访问二进制数据结构,它提供 C 兼容的数据类型。 4 | 5 | ### 常量 6 | - uctypes.LITTLE_ENDIAN — 小端压缩结构。 7 | - uctypes.BIG_ENDIAN — 大端压缩结构类型。 8 | - NATIVE — mricopython 本地的存储类型 9 | 10 | 11 | ### 构造函数 12 | 13 | #### class uctypes.struct(addr, descriptor, type) 14 | 将内存中以 c 形式打包的结构体或联合体转换为字典,并返回该字典。 15 | ``` 16 | addr:开始转换的地址 17 | descriptor:转换描述符 18 | 格式:"field_name":offset|uctypes.UINT32 19 | offset:偏移量, 20 | 单位:字节、VOID、UINT8、INT8、UINT16、INT16、UINT32、INT32、UINT64、INT64、BFUINT8、BFINT8、BFUINT16、BFINT16、BFUINT32、BFINT32、BF_POS、BF_LEN、FLOAT32、FLOAT64、PTR、ARRAY 21 | type:c 结构体或联合体存储类型,默认为本地存储类型 22 | ``` 23 | 24 | 示例: 25 | 26 | ```python 27 | >>> a = b"0123" 28 | >>> s = uctypes.struct(uctypes.addressof(a), {"a": uctypes.UINT8 | 0, "b": uctypes.UINT16 | 1}, uctypes.LITTLE_ENDIAN) 29 | >>> print(s) 30 | 31 | >>> print(s.a) 32 | 48 33 | >>> s.a = 49 34 | >>> print(a) 35 | b'1123' 36 | ``` 37 | 38 | ### 方法 39 | 40 | #### **uctypes.sizeof**(struct) 41 | 按字节返回数据的大小。参数可以是类或者数据对象 (或集合)。 42 | 示例: 43 | ```python 44 | >>> a = b"0123" 45 | >>>b = uctypes.struct(uctypes.addressof(a), {"a": uctypes.UINT8 | 0, "b": uctypes.UINT16 | 1}, uctypes.LITTLE_ENDIAN) 46 | >>> b.a 47 | 48 48 | >>> print(uctypes.sizeof(b)) 49 | 3 50 | ``` 51 | 52 | #### **uctypes.addressof**(obj) 53 | 返回对象地址。参数需要是 bytes, bytearray 。 54 | 示例: 55 | 56 | ```python 57 | >>> a = b"0123" 58 | >>> print(uctypes.addressof(a)) 59 | 1073504048 60 | ``` 61 | 62 | #### **uctypes.bytes_at**(addr, size) 63 | 捕捉从 addr 开始到 size 个地址偏移量结束的内存数据为 bytearray 对象并返回。 64 | 示例: 65 | 66 | ```python 67 | >>> a = b"0123" 68 | >>>print( uctypes.bytes_at(uctypes.addressof(a), 4)) 69 | b'0123' 70 | ``` 71 | 72 | #### **uctypes.bytearray_at**(addr, size) 73 | 捕捉给定大小和地址内存为 bytearray 对象。与 bytes_at() 函数不同的是,它可以被再次写入,可以访问给定地址的参数。 74 | 示例: 75 | 76 | ```python 77 | >>> a = b"0123" 78 | >>> print(uctypes.bytearray_at(uctypes.addressof(a), 2)) 79 | bytearray(b'01') 80 | ``` 81 | 82 | 更多内容可参考 [uctypes](http://docs.micropython.org/en/latest/pyboard/library/uctypes.html) 。 83 | -------------------------------------------------------------------------------- /docs/std-librarys/uerrno.md: -------------------------------------------------------------------------------- 1 | ## **uerrno** – 系统错误码模块 2 | 3 | `uerrno` 模块提供了标准的 errno 系统符号,每个符号都有对应的整数值。 4 | 5 | ### 示例 6 | 7 | ```python 8 | try: 9 | uos.mkdir("my_dir") 10 | except OSError as exc: 11 | if exc.args[0] == uerrno.EEXIST: 12 | print("Directory already exists") 13 | uerrno.errorcode 14 | Dictionary mapping numeric error codes to strings with symbolic error code (see above): 15 | 16 | >>> print(uerrno.errorcode[uerrno.EEXIST]) 17 | EEXIST 18 | ``` 19 | 20 | 更多内容可参考 [uerrno](http://docs.micropython.org/en/latest/pyboard/library/uerrno.html) 。 21 | -------------------------------------------------------------------------------- /docs/std-librarys/uhashlib.md: -------------------------------------------------------------------------------- 1 | ## **uhashlib** – 哈希算法 2 | 3 | `uhashlib` 模块实现了二进制数据哈希算法。 4 | 5 | ### 算法功能 6 | 7 | #### **SHA256** 8 | 当代的散列算法(SHA2系列),它适用于密码安全的目的。被包含在 MicroPython 内核中,除非有特定的代码大小限制,否则推荐任何开发板都支持这个功能。 9 | 10 | #### **SHA1** 11 | 上一代的算法,不推荐新的应用使用这种算法,但是 SHA1 算法是互联网标准和现有应用程序的一部分,所以针对网络连接便利的开发板会提供这种功能。 12 | 13 | #### **MD5** 14 | 一种遗留下来的算法,作为密码使用被认为是不安全的。只有特定的开发板,为了兼容老的应用才会提供这种算法。 15 | 16 | ### 函数 17 | 18 | #### **class uhashlib.sha256**([data]) 19 | 创建一个SHA256哈希对象并提供 data 赋值。 20 | 21 | #### **class uhashlib.sha1**([data]) 22 | 创建一个SHA1哈希对象并提供 data 赋值。 23 | 24 | #### **class uhashlib.md5**([data]) 25 | 创建一个MD5哈希对象并提供 data 赋值。 26 | 27 | #### **hash.update**(data) 28 | 将更多二进制数据放入哈希表中。 29 | 30 | #### **hash.digest**() 31 | 返回字节对象哈希的所有数据。调用此方法后,将无法将更多数据送入哈希。 32 | 33 | #### **hash.hexdigest**() 34 | 此方法没有实现, 使用 ubinascii.hexlify(hash.digest()) 达到类似效果。 35 | 36 | 更多内容可参考 [uhashlib](http://docs.micropython.org/en/latest/pyboard/library/uhashlib.html) 。 37 | -------------------------------------------------------------------------------- /docs/std-librarys/uheapq.md: -------------------------------------------------------------------------------- 1 | ## **uheapq** – 堆排序算法 2 | 3 | `uheapq` 模块提供了堆排序相关算法,堆队列是一个列表,它的元素以特定的方式存储。 4 | 5 | ### 函数 6 | 7 | #### **uheapq.heappush**(heap, item) 8 | 将对象压入堆中。 9 | 10 | #### **uheapq.heappop**(heap) 11 | 从 heap 弹出第一个元素并返回。 如果是堆时空的会抛出 IndexError。 12 | 13 | #### **uheapq.heapify**(x) 14 | 将列表 x 转换成堆。 15 | 16 | 更多内容可参考 [uheapq](http://docs.micropython.org/en/latest/pyboard/library/uheapq.html) 。 17 | -------------------------------------------------------------------------------- /docs/std-librarys/uio.md: -------------------------------------------------------------------------------- 1 | ## **uio** – 输入/输出流 2 | 3 | **uio** 模块包含流类型 (类似文件) 对象和帮助函数。 4 | 5 | ### 函数 6 | 7 | #### **uio.open**(name, mode='r', \*\*kwargs) 8 | 9 | 打开一个文件,关联到内建函数``open()``。所有端口 (用于访问文件系统) 需要支持模式参数,但支持其他参数不同的端口。 10 | 11 | ### 类 12 | 13 | #### **class uio.FileIO**(...) 14 | 这个文件类型用二进制方式打开文件,等于使用``open(name, “rb”)``。 不应直接使用这个实例。 15 | 16 | #### **class uio.TextIOWrapper**(...) 17 | 这个类型以文本方式打开文件,等同于使用``open(name, “rt”)``不应直接使用这个实例。 18 | 19 | #### **class uio.StringIO**([string]) 20 | 21 | #### **class uio.BytesIO**([string]) 22 | 内存文件对象。`StringIO` 用于文本模式 I/O (用 “t” 打开文件),`BytesIO` 用于二进制方式 (用 “b” 方式)。文件对象的初始内容可以用字符串参数指定(`stringio`用普通字符串,`bytesio`用`bytes`对象)。所有的文件方法,如 `read(), write(), seek(), flush(), close()` 都可以用在这些对象上,包括下面方法: 23 | 24 | #### **getvalue**() 25 | 获取缓存区内容。 26 | 27 | 更多内容可参考 [uio](http://docs.micropython.org/en/latest/pyboard/library/uio.html) 。 28 | -------------------------------------------------------------------------------- /docs/std-librarys/ujson.md: -------------------------------------------------------------------------------- 1 | ## **ujson** – JSON编码与解码 2 | 3 | `ujson` 模块提供 Python 对象到 JSON(JavaScript Object Notation) 数据格式的转换。 4 | 5 | ### 函数 6 | 7 | #### **ujson.dumps**(obj) 8 | 9 | 将 dict 类型转换成 str。 10 | 11 | ``` 12 | obj:要转换的对象 13 | ``` 14 | 15 | 示例: 16 | 17 | ``` 18 | >>> obj = {1:2, 3:4, "a":6} 19 | >>> print(type(obj), obj) #原来为dict类型 20 | {3: 4, 1: 2, 'a': 6} 21 | >>> jsObj = json.dumps(obj) #将dict类型转换成str 22 | >>> print(type(jsObj), jsObj) 23 | {3: 4, 1: 2, "a": 6} 24 | ``` 25 | 26 | #### **ujson.loads**(str) 27 | 解析 JSON 字符串并返回对象。如果字符串格式错误将引发 ValueError 异常。 28 | 示例: 29 | 30 | ``` 31 | >>> obj = {1:2, 3:4, "a":6} 32 | >>> jsDumps = json.dumps(obj) 33 | >>> jsLoads = json.loads(jsDumps) 34 | >>> print(type(obj), obj) 35 | {3: 4, 1: 2, 'a': 6} 36 | >>> print(type(jsDumps), jsDumps) 37 | {3: 4, 1: 2, "a": 6} 38 | >>> print(type(jsLoads), jsLoads) 39 | {'a': 6, 1: 2, 3: 4} 40 | ``` 41 | 42 | 更多内容可参考 [ujson](http://docs.micropython.org/en/latest/pyboard/library/ujson.html) 。 43 | -------------------------------------------------------------------------------- /docs/std-librarys/uos.md: -------------------------------------------------------------------------------- 1 | ## **uos** – 基本的操作系统服务 2 | 3 | `uos` 模块包含了对文件系统的访问操作,是对应 CPython 模块的一个子集。 4 | 5 | ### 函数 6 | 7 | #### **uos.chdir**(path) 8 | 更改当前目录。 9 | 10 | #### **uos.getcwd**() 11 | 获取当前目录。 12 | 13 | #### **uos.listdir**([dir]) 14 | 没有参数就列出当前目录,否则列出给定目录。 15 | 16 | #### **uos.mkdir**(path) 17 | 创建一个目录。 18 | 19 | #### **uos.remove**(path) 20 | 删除文件。 21 | 22 | #### **uos.rmdir**(path) 23 | 删除目录。 24 | 25 | #### **uos.rename**(old_path, new_path) 26 | 重命名文件或者文件夹。 27 | 28 | #### **uos.stat**(path) 29 | 获取文件或目录的状态。 30 | 31 | #### **uos.sync**() 32 | 同步所有的文件系统。 33 | 34 | ### 示例 35 | 36 | ``` 37 | >>> import uos 38 | >>> uos. # Tab 39 | __name__ uname chdir getcwd 40 | listdir mkdir remove rmdir 41 | stat unlink mount umount 42 | >>> uos.mkdir("rtthread") 43 | >>> uos.getcwd() 44 | '/' 45 | >>> uos.chdir("rtthread") 46 | >>> uos.getcwd() 47 | '/rtthread' 48 | >>> uos.listdir() 49 | ['web_root', 'rtthread', '11'] 50 | >>> uos.rmdir("11") 51 | >>> uos.listdir() 52 | ['web_root', 'rtthread'] 53 | >>> 54 | ``` 55 | 56 | 更多内容可参考 [uos](http://docs.micropython.org/en/latest/pyboard/library/uos.html) 。 57 | -------------------------------------------------------------------------------- /docs/std-librarys/ure.md: -------------------------------------------------------------------------------- 1 | ## **ure** – 正则表达式 2 | 3 | `ure` 模块用于测试字符串的某个模式,执行正则表达式操作。 4 | 5 | ### 匹配字符集 6 | 7 | 8 | #### 匹配任意字符 9 | ``'.'`` 10 | 11 | #### 匹配字符集合,支持单个字符和一个范围 12 | ``'[]'`` 13 | 14 | #### 支持多种匹配元字符 15 | ``'^'`` 16 | ``'$'`` 17 | ``'?'`` 18 | ``'*'`` 19 | ``'+'`` 20 | ``'??'`` 21 | ``'*?'`` 22 | ``'+?'`` 23 | ``'{m,n}'`` 24 | 25 | ### 函数 26 | 27 | #### **ure.compile**(regex) 28 | 编译正则表达式,返回 regex 对象。 29 | 30 | #### **ure.match**(regex, string) 31 | 用 string 匹配 regex,匹配总是从字符串的开始匹配。 32 | 33 | #### **ure.search**(regex, string) 34 | 在 string 中搜索 regex。不同于匹配,它搜索第一个匹配位置的正则表达式字符串 (结果可能会是0)。 35 | 36 | #### **ure.DEBUG** 37 | 标志值,显示表达式的调试信息。 38 | 39 | ### **正则表达式对象**: 40 | 编译正则表达式,使用 `ure.compile()` 创建实例。 41 | 42 | #### **regex.match**(string) 43 | #### **regex.search**(string) 44 | #### **regex.split**(string, max_split=-1) 45 | 46 | ### **匹配对象** : 47 | 匹配对象是 match() 和 search() 方法的返回值。 48 | 49 | #### **match.group**([index]) 50 | 只支持数字组。 51 | 52 | 更多内容可参考 [ure](http://docs.micropython.org/en/latest/pyboard/library/ure.html) 。 53 | -------------------------------------------------------------------------------- /docs/std-librarys/uselect.md: -------------------------------------------------------------------------------- 1 | ## **uselect** – 等待流事件 2 | 3 | `uselect` 模块提供了等待数据流的事件功能。 4 | 5 | ### 常数 6 | 7 | #### **select.POLLIN** - 读取可用数据 8 | 9 | #### **select.POLLOUT** - 写入更多数据 10 | 11 | #### **select.POLLERR** - 发生错误 12 | 13 | #### **select.POLLHUP** - 流结束/连接终止检测 14 | 15 | ### 函数 16 | 17 | #### **select.select**(rlist, wlist, xlist[, timeout]) 18 | 监控对象何时可读或可写,一旦监控的对象状态改变,返回结果(阻塞线程)。这个函数是为了兼容,效率不高,推荐用 `poll` 函数 。 19 | 20 | ``` 21 | rlist:等待读就绪的文件描述符数组 22 | wlist:等待写就绪的文件描述符数组 23 | xlist:等待异常的数组 24 | timeout:等待时间(单位:秒) 25 | ``` 26 | 示例: 27 | 28 | ```python 29 | def selectTest(): 30 | global s 31 | rs, ws, es = select.select([s,], [], []) 32 | #程序会在此等待直到对象s可读 33 | print(rs) 34 | for i in rs: 35 | if i == s: 36 | print("s can read now") 37 | data,addr=s.recvfrom(1024) 38 | print('received:',data,'from',addr) 39 | ``` 40 | 41 | ### Poll 类 42 | 43 | #### **select.poll**() 44 | 创建 poll 实例。 45 | 46 | 示例: 47 | 48 | ``` 49 | >>>poller = select.poll() 50 | >>>print(poller) 51 | 52 | ``` 53 | 54 | #### **poll.register**(obj[, eventmask]) 55 | 注册一个用以监控的对象,并设置被监控对象的监控标志位 flag。 56 | 57 | ``` 58 | obj:被监控的对象 59 | flag:被监控的标志 60 | select.POLLIN — 可读 61 | select.POLLHUP — 已挂断 62 | select.POLLERR — 出错 63 | select.POLLOUT — 可写 64 | ``` 65 | 66 | #### **poll.unregister**(obj) 67 | 解除监控的对象的注册。 68 | 69 | ``` 70 | obj:注册过的对象 71 | ``` 72 | 73 | 示例: 74 | 75 | ``` 76 | >>>READ_ONLY = select.POLLIN | select.POLLHUP | select.POLLERR 77 | >>>READ_WRITE = select.POLLOUT | READ_ONLY 78 | >>>poller.register(s, READ_WRITE) 79 | >>>poller.unregister(s) 80 | ``` 81 | 82 | #### **poll.modify**(obj, eventmask) 83 | 修改已注册的对象监控标志。 84 | 85 | ``` 86 | obj:已注册的被监控对象 87 | flag:修改为的监控标志 88 | ``` 89 | 90 | 示例: 91 | 92 | ``` 93 | >>>READ_ONLY = select.POLLIN | select.POLLHUP | select.POLLERR 94 | >>>READ_WRITE = select.POLLOUT | READ_ONLY 95 | >>>poller.register(s, READ_WRITE) 96 | >>>poller.modify(s, READ_ONLY) 97 | ``` 98 | 99 | #### **poll.poll**([timeout]) 100 | 等待至少一个已注册的对象准备就绪。返回 (obj, event, ...) 元组, event 元素指定了一个流发生的事件,是上面所描述的 `select.POLL*`常量组合。 根据平台和版本的不同,在元组中可能有其他元素,所以不要假定元组的大小是 2 。如果超时,则返回空列表。 101 | 102 | 更多内容可参考 [uselect](http://docs.micropython.org/en/latest/pyboard/library/uselect.html) 。 103 | -------------------------------------------------------------------------------- /docs/std-librarys/ussl.md: -------------------------------------------------------------------------------- 1 | # ussl – SSL/TLS 模块 2 | 3 | This module implements a subset of the corresponding* [`CPython`](http://docs.micropython.org/en/latest/reference/glossary.html#term-cpython) *module, as described below. For more information, refer to the original CPython documentation: [`ssl`](https://docs.python.org/3.5/library/ssl.html#module-ssl). 4 | 5 | This module provides access to Transport Layer Security (previously and widely known as “Secure Sockets Layer”) encryption and peer authentication facilities for network sockets, both client-side and server-side. 6 | 7 | ## 功能函数 8 | 9 | - `ussl.wrap_socket`(sock, server_side=False, key=None, cert=None) 10 | 11 | Takes a [`stream`](http://docs.micropython.org/en/latest/reference/glossary.html#term-stream) *sock* (usually usocket.socket instance of `SOCK_STREAM` type), and returns an instance of ssl.SSLSocket, which wraps the underlying stream in an SSL context. Returned object has the usual [`stream`](http://docs.micropython.org/en/latest/reference/glossary.html#term-stream) interface methods like `read()`, `write()`, etc. In MicroPython, the returned object does not expose socket interface and methods like `recv()`, `send()`. In particular, a server-side SSL socket should be created from a normal socket returned from[`accept()`](http://docs.micropython.org/en/latest/library/usocket.html#usocket.socket.accept) on a non-SSL listening server socket. Depending on the underlying module implementation in a particular [`MicroPython port`](http://docs.micropython.org/en/latest/reference/glossary.html#term-micropython-port), some or all keyword arguments above may be not supported. 12 | 13 | Warning: Some implementations of `ussl` module do NOT validate server certificates, which makes an SSL connection established prone to man-in-the-middle attacks. 14 | 15 | ## 异常类型 16 | 17 | - `ssl.SSLError` 18 | 19 | This exception does NOT exist. Instead its base class, OSError, is used. 20 | 21 | ## 常量 22 | 23 | - `ussl.CERT_NONE` 24 | 25 | - `ussl.CERT_OPTIONAL` 26 | 27 | - `ussl.CERT_REQUIRED` 28 | - Supported values for **cert_reqs** parameter. -------------------------------------------------------------------------------- /docs/std-librarys/ustruct.md: -------------------------------------------------------------------------------- 1 | ## **ustruct** – 打包和解包原始数据类型 2 | 3 | **ustruct** 模块在 Python 值和以 Python 字节对象表示的 C 结构之间执行转换。 4 | 5 | - 支持 size/byte 的前缀: @, <, >, !. 6 | - 支持的格式代码: b, B, h, H, i, I, l, L, q, Q, s, P, f, d (最后2个需要支持浮点数). 7 | 8 | ### 函数 9 | 10 | #### **ustruct.calcsize**(fmt) 11 | 返回存放某一类型数据 fmt 需要的字节数。 12 | 13 | ``` 14 | fmt:数据类型 15 | b — 字节型 16 | B — 无符号字节型 17 | h — 短整型 18 | H — 无符号短整型 19 | i — 整型 20 | I — 无符号整型 21 | l — 整型 22 | L — 无符号整型 23 | q — 长整型 24 | Q — 无符号长整型 25 | f — 浮点型 26 | d — 双精度浮点型 27 | P — 无符号型 28 | ``` 29 | 30 | 示例: 31 | 32 | ```python 33 | >>> print(struct.calcsize("i")) 34 | 4 35 | >>> print(struct.calcsize("B")) 36 | 1 37 | ``` 38 | 39 | #### **ustruct.pack**(fmt, v1, v2, ...) 40 | 按照格式字符串 fmt 打包参数 v1, v2, ... 。返回值是参数打包后的字节对象。 41 | 42 | ``` 43 | fmt:同上 44 | ``` 45 | 46 | 示例: 47 | 48 | ```python 49 | >>> struct.pack("ii", 3, 2) 50 | b'\x03\x00\x00\x00\x02\x00\x00\x00' 51 | ``` 52 | 53 | #### **ustruct.unpack**(fmt, data) 54 | 从 fmt 中解包数据。返回值是解包后参数的元组。 55 | 56 | ``` 57 | data:要解压的字节对象 58 | ``` 59 | 60 | 示例: 61 | 62 | ```python 63 | >>> buf = struct.pack("bb", 1, 2) 64 | >>> print(buf) 65 | b'\x01\x02' 66 | >>> print(struct.unpack("bb", buf)) 67 | (1, 2) 68 | ``` 69 | 70 | #### **ustruct.pack_into**(fmt, buffer, offset, v1, v2, ...) 71 | 按照格式字符串 fmt 压缩参数 v1, v2, ... 到缓冲区 buffer,开始位置是 offset。当offset 为负数时,从缓冲区末尾开始计数。 72 | 73 | #### **ustruct.unpack_from**(fmt, data, offset=0) 74 | 以 fmt 作为规则从 data 的 offset 位置开始解包数据,如果 offset 是负数就是从缓冲区末尾开始计算。返回值是解包后的参数元组。 75 | ```python 76 | >>> buf = struct.pack("bb", 1, 2) 77 | >>> print(struct.unpack("bb", buf)) 78 | (1, 2) 79 | >>> print(struct.unpack_from("b", buf, 1)) 80 | (2,) 81 | ``` 82 | 更多的内容可参考 [ustruct](http://docs.micropython.org/en/latest/pyboard/library/ustruct.html) 。 83 | -------------------------------------------------------------------------------- /docs/std-librarys/uzlib.md: -------------------------------------------------------------------------------- 1 | ## **uzlib** – zlib 解压缩 2 | 3 | `uzlib` 模块实现了使用 DEFLATE 算法解压缩二进制数据 (常用的 zlib 库和 gzip 文档)。目前不支持压缩。 4 | 5 | ### 函数 6 | 7 | #### **uzlib.decompress**(data) 8 | 返回解压后的 bytes 数据。 9 | 10 | 更多内容可参考 [uzlib](http://docs.micropython.org/en/latest/pyboard/library/uzlib.html) 。 11 | -------------------------------------------------------------------------------- /docs/tools-mpy-cross.md: -------------------------------------------------------------------------------- 1 | # 生成并运行 .mpy 文件 2 | 3 | Python 工程中,.py 文件可以被编译成 .pyc 字节码文件。使用这类预先编译好的二进制文件的优势是一定程度上能够保护源代码,同时可以提高程序的加载速度。在 micropython 中同样地提供了类似的功能,能够将 .py 文件编译成 .mpy 文件,并且能够在 MCU 内直接运行,减少资源消耗的同时提高了程序的加载速度。本篇演示使用潘多拉开发板,以 hello world 工程为例,编译并运行该 demo。 4 | 5 | ## 使用 mpy-cross 工具编译 6 | 7 | RT-Thread 为开发者提供了便利的开发环境 ,针对 windows 平台提供了可以直接运行的 mpy-cross.exe,无需拖带 Linux 环境,轻量且速度快。 8 | 9 | 本文将在 windows 环境中,使用软件包中提供的 mpy-cross.exe 软件。 10 | 11 | 在 RT-Thread/micropython 项目里找到 tools 文件夹,下载 mpy-cross.exe。 12 | 13 | 这里以 `hellortt.py` 示例: 14 | 15 | ```python 16 | class HelloRtt: 17 | def __repr__(self): 18 | self.__call__() 19 | return "" 20 | 21 | def __call__(self): 22 | print("hello world!!") 23 | print("hello RTT") 24 | 25 | hello = HelloRtt() 26 | ``` 27 | 28 | 首先将需要被编译的 `hellortt.py` 文件拖入到 mpy-cross.exe 所在文件夹中,在按住 shift 键的同时,点击鼠标右键,选择 `在此处打开 Powershell 窗口`( cmd 窗口在这里也是可以使用的)。然后在 Powershell 中,键入 `.\mpy-cross.exe` 后面接上需要编译的工程 `hellortt.py`: 29 | 30 | ![powershell_mpycross_deploy](assets/powershell_mpycross_deploy.png) 31 | 32 | 此时如果编译成功将会生成 mpy 文件,上图编译成功后,在文件夹中生成了 `hellortt.mpy` 文件。我们将 `hellortt.mpy` 拷贝到 MCU 的文件系统上,便可以像 .py 文件一样被 import 并调用。 33 | 34 | ## 拷贝至 MCU 文件系统中 35 | 36 | 将生成好的 .mpy 文件,拷贝至 MicroPython IDE 的工程中,并选中它,右键下载到开发板的文件系统中,如图: 37 | 38 | ![tools-mpy-download](assets/tools-mpy-download.png) 39 | 40 | 当下载完成之后,便可以在串口命令行中尝试 import 该 mpy 文件,执行该文件的函数,确认能够正常使用: 41 | 42 | ![mpy-usage-demo](assets/mpy-usage-demo.png) 43 | 44 | 如果运行时出现 `ValueError: invalid .mpy file` 错误,有可能是 MCU 上 的 micropython 固件与 mpy-cross 版本固件不相符所致,当前提供的 mpy-cross 工具支持 v1.12 版本的固件 。 45 | 46 | -------------------------------------------------------------------------------- /drivers/bus/qspi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017-2018 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_DRIVERS_BUS_QSPI_H 27 | #define MICROPY_INCLUDED_DRIVERS_BUS_QSPI_H 28 | 29 | #include "py/mphal.h" 30 | 31 | enum { 32 | MP_QSPI_IOCTL_INIT, 33 | MP_QSPI_IOCTL_DEINIT, 34 | MP_QSPI_IOCTL_BUS_ACQUIRE, 35 | MP_QSPI_IOCTL_BUS_RELEASE, 36 | }; 37 | 38 | typedef struct _mp_qspi_proto_t { 39 | int (*ioctl)(void *self, uint32_t cmd); 40 | void (*write_cmd_data)(void *self, uint8_t cmd, size_t len, uint32_t data); 41 | void (*write_cmd_addr_data)(void *self, uint8_t cmd, uint32_t addr, size_t len, const uint8_t *src); 42 | uint32_t (*read_cmd)(void *self, uint8_t cmd, size_t len); 43 | void (*read_cmd_qaddr_qdata)(void *self, uint8_t cmd, uint32_t addr, size_t len, uint8_t *dest); 44 | } mp_qspi_proto_t; 45 | 46 | typedef struct _mp_soft_qspi_obj_t { 47 | mp_hal_pin_obj_t cs; 48 | mp_hal_pin_obj_t clk; 49 | mp_hal_pin_obj_t io0; 50 | mp_hal_pin_obj_t io1; 51 | mp_hal_pin_obj_t io2; 52 | mp_hal_pin_obj_t io3; 53 | } mp_soft_qspi_obj_t; 54 | 55 | extern const mp_qspi_proto_t mp_soft_qspi_proto; 56 | 57 | #endif // MICROPY_INCLUDED_DRIVERS_BUS_QSPI_H 58 | -------------------------------------------------------------------------------- /drivers/bus/spi.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016-2018 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_DRIVERS_BUS_SPI_H 27 | #define MICROPY_INCLUDED_DRIVERS_BUS_SPI_H 28 | 29 | #include "py/mphal.h" 30 | 31 | enum { 32 | MP_SPI_IOCTL_INIT, 33 | MP_SPI_IOCTL_DEINIT, 34 | }; 35 | 36 | typedef struct _mp_spi_proto_t { 37 | int (*ioctl)(void *self, uint32_t cmd); 38 | void (*transfer)(void *self, size_t len, const uint8_t *src, uint8_t *dest); 39 | } mp_spi_proto_t; 40 | 41 | typedef struct _mp_soft_spi_obj_t { 42 | uint32_t delay_half; // microsecond delay for half SCK period 43 | uint8_t polarity; 44 | uint8_t phase; 45 | mp_hal_pin_obj_t sck; 46 | mp_hal_pin_obj_t mosi; 47 | mp_hal_pin_obj_t miso; 48 | } mp_soft_spi_obj_t; 49 | 50 | extern const mp_spi_proto_t mp_soft_spi_proto; 51 | 52 | int mp_soft_spi_ioctl(void *self, uint32_t cmd); 53 | void mp_soft_spi_transfer(void *self, size_t len, const uint8_t *src, uint8_t *dest); 54 | 55 | #endif // MICROPY_INCLUDED_DRIVERS_BUS_SPI_H 56 | -------------------------------------------------------------------------------- /extmod/axtls-include/version.h: -------------------------------------------------------------------------------- 1 | #define AXTLS_VERSION "(no version)" 2 | -------------------------------------------------------------------------------- /extmod/crypto-algorithms/sha256.h: -------------------------------------------------------------------------------- 1 | /********************************************************************* 2 | * Source: https://github.com/B-Con/crypto-algorithms 3 | * Filename: sha256.h 4 | * Author: Brad Conte (brad AT bradconte.com) 5 | * Copyright: This code is released into the public domain. 6 | * Disclaimer: This code is presented "as is" without any guarantees. 7 | * Details: Defines the API for the corresponding SHA1 implementation. 8 | *********************************************************************/ 9 | 10 | #ifndef SHA256_H 11 | #define SHA256_H 12 | 13 | /*************************** HEADER FILES ***************************/ 14 | #include 15 | 16 | /****************************** MACROS ******************************/ 17 | #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest 18 | 19 | /**************************** DATA TYPES ****************************/ 20 | typedef unsigned char BYTE; // 8-bit byte 21 | typedef unsigned int WORD; // 32-bit word, change to "long" for 16-bit machines 22 | 23 | typedef struct { 24 | BYTE data[64]; 25 | WORD datalen; 26 | unsigned long long bitlen; 27 | WORD state[8]; 28 | } CRYAL_SHA256_CTX; 29 | 30 | /*********************** FUNCTION DECLARATIONS **********************/ 31 | void sha256_init(CRYAL_SHA256_CTX *ctx); 32 | void sha256_update(CRYAL_SHA256_CTX *ctx, const BYTE data[], size_t len); 33 | void sha256_final(CRYAL_SHA256_CTX *ctx, BYTE hash[]); 34 | 35 | #endif // SHA256_H 36 | -------------------------------------------------------------------------------- /extmod/lwip-include/arch/cc.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_ARCH_CC_H 2 | #define MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_ARCH_CC_H 3 | 4 | #include 5 | 6 | // Generate lwip's internal types from stdint 7 | 8 | typedef uint8_t u8_t; 9 | typedef int8_t s8_t; 10 | typedef uint16_t u16_t; 11 | typedef int16_t s16_t; 12 | typedef uint32_t u32_t; 13 | typedef int32_t s32_t; 14 | 15 | typedef u32_t mem_ptr_t; 16 | 17 | #define U16_F "hu" 18 | #define S16_F "hd" 19 | #define X16_F "hx" 20 | #define U32_F "u" 21 | #define S32_F "d" 22 | #define X32_F "x" 23 | 24 | #define X8_F "02x" 25 | #define SZT_F "u" 26 | 27 | #define BYTE_ORDER LITTLE_ENDIAN 28 | 29 | #define LWIP_CHKSUM_ALGORITHM 2 30 | 31 | #include 32 | #define LWIP_PLATFORM_DIAG(x) 33 | #define LWIP_PLATFORM_ASSERT(x) { assert(1); } 34 | 35 | //#define PACK_STRUCT_FIELD(x) x __attribute__((packed)) 36 | #define PACK_STRUCT_FIELD(x) x 37 | #define PACK_STRUCT_STRUCT __attribute__((packed)) 38 | #define PACK_STRUCT_BEGIN 39 | #define PACK_STRUCT_END 40 | 41 | #endif // MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_ARCH_CC_H 42 | -------------------------------------------------------------------------------- /extmod/lwip-include/arch/perf.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_ARCH_PERF_H 2 | #define MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_ARCH_PERF_H 3 | 4 | #define PERF_START /* null definition */ 5 | #define PERF_STOP(x) /* null definition */ 6 | 7 | #endif // MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_ARCH_PERF_H 8 | -------------------------------------------------------------------------------- /extmod/lwip-include/lwipopts.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_LWIPOPTS_H 2 | #define MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_LWIPOPTS_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | // We're running without an OS for this port. We don't provide any services except light protection. 9 | #define NO_SYS 1 10 | 11 | #define SYS_LIGHTWEIGHT_PROT 1 12 | #include 13 | typedef uint32_t sys_prot_t; 14 | 15 | #define TCP_LISTEN_BACKLOG 1 16 | 17 | // We'll put these into a proper ifdef once somebody implements an ethernet driver 18 | #define LWIP_ARP 0 19 | #define LWIP_ETHERNET 0 20 | 21 | #define LWIP_DNS 1 22 | 23 | #define LWIP_NETCONN 0 24 | #define LWIP_SOCKET 0 25 | 26 | #ifdef MICROPY_PY_LWIP_SLIP 27 | #define LWIP_HAVE_SLIPIF 1 28 | #endif 29 | 30 | // For now, we can simply define this as a macro for the timer code. But this function isn't 31 | // universal and other ports will need to do something else. It may be necessary to move 32 | // things like this into a port-provided header file. 33 | #define sys_now mp_hal_ticks_ms 34 | 35 | #endif // MICROPY_INCLUDED_EXTMOD_LWIP_INCLUDE_LWIPOPTS_H 36 | -------------------------------------------------------------------------------- /extmod/machine_mem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_MEM_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_MEM_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _machine_mem_obj_t { 32 | mp_obj_base_t base; 33 | unsigned elem_size; // in bytes 34 | } machine_mem_obj_t; 35 | 36 | extern const mp_obj_type_t machine_mem_type; 37 | 38 | extern const machine_mem_obj_t machine_mem8_obj; 39 | extern const machine_mem_obj_t machine_mem16_obj; 40 | extern const machine_mem_obj_t machine_mem32_obj; 41 | 42 | #if defined(MICROPY_MACHINE_MEM_GET_READ_ADDR) 43 | uintptr_t MICROPY_MACHINE_MEM_GET_READ_ADDR(mp_obj_t addr_o, uint align); 44 | #endif 45 | #if defined(MICROPY_MACHINE_MEM_GET_WRITE_ADDR) 46 | uintptr_t MICROPY_MACHINE_MEM_GET_WRITE_ADDR(mp_obj_t addr_o, uint align); 47 | #endif 48 | 49 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_MEM_H 50 | -------------------------------------------------------------------------------- /extmod/machine_pinbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H 28 | 29 | #include "py/obj.h" 30 | 31 | extern const mp_obj_type_t machine_pinbase_type; 32 | 33 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_PINBASE_H 34 | -------------------------------------------------------------------------------- /extmod/machine_pulse.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H 28 | 29 | #include "py/obj.h" 30 | #include "py/mphal.h" 31 | 32 | mp_uint_t machine_time_pulse_us(mp_hal_pin_obj_t pin, int pulse_level, mp_uint_t timeout_us); 33 | 34 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_time_pulse_us_obj); 35 | 36 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_PULSE_H 37 | -------------------------------------------------------------------------------- /extmod/machine_signal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H 27 | #define MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H 28 | 29 | #include "py/obj.h" 30 | 31 | extern const mp_obj_type_t machine_signal_type; 32 | 33 | #endif // MICROPY_INCLUDED_EXTMOD_MACHINE_SIGNAL_H 34 | -------------------------------------------------------------------------------- /extmod/misc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014-2016 Damien P. George 7 | * Copyright (c) 2016 Paul Sokolovsky 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 | #ifndef MICROPY_INCLUDED_EXTMOD_MISC_H 28 | #define MICROPY_INCLUDED_EXTMOD_MISC_H 29 | 30 | // This file contains cumulative declarations for extmod/ . 31 | 32 | #include 33 | #include "py/runtime.h" 34 | 35 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_uos_dupterm_obj); 36 | 37 | #if MICROPY_PY_OS_DUPTERM 38 | bool mp_uos_dupterm_is_builtin_stream(mp_const_obj_t stream); 39 | uintptr_t mp_uos_dupterm_poll(uintptr_t poll_flags); 40 | int mp_uos_dupterm_rx_chr(void); 41 | void mp_uos_dupterm_tx_strn(const char *str, size_t len); 42 | void mp_uos_deactivate(size_t dupterm_idx, const char *msg, mp_obj_t exc); 43 | #else 44 | #define mp_uos_dupterm_tx_strn(s, l) 45 | #endif 46 | 47 | #endif // MICROPY_INCLUDED_EXTMOD_MISC_H 48 | -------------------------------------------------------------------------------- /extmod/modubinascii.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_MODUBINASCII_H 27 | #define MICROPY_INCLUDED_EXTMOD_MODUBINASCII_H 28 | 29 | extern mp_obj_t mod_binascii_hexlify(size_t n_args, const mp_obj_t *args); 30 | extern mp_obj_t mod_binascii_unhexlify(mp_obj_t data); 31 | extern mp_obj_t mod_binascii_a2b_base64(mp_obj_t data); 32 | extern mp_obj_t mod_binascii_b2a_base64(mp_obj_t data); 33 | extern mp_obj_t mod_binascii_crc32(size_t n_args, const mp_obj_t *args); 34 | 35 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_hexlify_obj); 36 | MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_unhexlify_obj); 37 | MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_a2b_base64_obj); 38 | MP_DECLARE_CONST_FUN_OBJ_1(mod_binascii_b2a_base64_obj); 39 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mod_binascii_crc32_obj); 40 | 41 | #endif // MICROPY_INCLUDED_EXTMOD_MODUBINASCII_H 42 | -------------------------------------------------------------------------------- /extmod/moduwebsocket.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_EXTMOD_MODUWEBSOCKET_H 2 | #define MICROPY_INCLUDED_EXTMOD_MODUWEBSOCKET_H 3 | 4 | #define FRAME_OPCODE_MASK 0x0f 5 | enum { 6 | FRAME_CONT, FRAME_TXT, FRAME_BIN, 7 | FRAME_CLOSE = 0x8, FRAME_PING, FRAME_PONG 8 | }; 9 | 10 | #endif // MICROPY_INCLUDED_EXTMOD_MODUWEBSOCKET_H 11 | -------------------------------------------------------------------------------- /extmod/modwebsocket.h: -------------------------------------------------------------------------------- 1 | #ifndef MICROPY_INCLUDED_EXTMOD_MODWEBSOCKET_H 2 | #define MICROPY_INCLUDED_EXTMOD_MODWEBSOCKET_H 3 | 4 | #define FRAME_OPCODE_MASK 0x0f 5 | enum { 6 | FRAME_CONT, FRAME_TXT, FRAME_BIN, 7 | FRAME_CLOSE = 0x8, FRAME_PING, FRAME_PONG 8 | }; 9 | 10 | #endif // MICROPY_INCLUDED_EXTMOD_MODWEBSOCKET_H 11 | -------------------------------------------------------------------------------- /extmod/re1.5/charclass.c: -------------------------------------------------------------------------------- 1 | #include "re1.5.h" 2 | 3 | int _re1_5_classmatch(const char *pc, const char *sp) 4 | { 5 | // pc points to "cnt" byte after opcode 6 | int is_positive = (pc[-1] == Class); 7 | int cnt = *pc++; 8 | while (cnt--) { 9 | if (*sp >= *pc && *sp <= pc[1]) return is_positive; 10 | pc += 2; 11 | } 12 | return !is_positive; 13 | } 14 | 15 | int _re1_5_namedclassmatch(const char *pc, const char *sp) 16 | { 17 | // pc points to name of class 18 | int off = (*pc >> 5) & 1; 19 | if ((*pc | 0x20) == 'd') { 20 | if (!(*sp >= '0' && *sp <= '9')) { 21 | off ^= 1; 22 | } 23 | } else if ((*pc | 0x20) == 's') { 24 | if (!(*sp == ' ' || (*sp >= '\t' && *sp <= '\r'))) { 25 | off ^= 1; 26 | } 27 | } else { // w 28 | if (!((*sp >= 'A' && *sp <= 'Z') || (*sp >= 'a' && *sp <= 'z') || (*sp >= '0' && *sp <= '9') || *sp == '_')) { 29 | off ^= 1; 30 | } 31 | } 32 | return off; 33 | } 34 | -------------------------------------------------------------------------------- /extmod/re1.5/recursiveloop.c: -------------------------------------------------------------------------------- 1 | // Copyright 2007-2009 Russ Cox. All Rights Reserved. 2 | // Use of this source code is governed by a BSD-style 3 | // license that can be found in the LICENSE file. 4 | 5 | #include "re1.5.h" 6 | 7 | static int 8 | recursiveloop(char *pc, const char *sp, Subject *input, const char **subp, int nsubp) 9 | { 10 | const char *old; 11 | int off; 12 | 13 | re1_5_stack_chk(); 14 | 15 | for(;;) { 16 | if(inst_is_consumer(*pc)) { 17 | // If we need to match a character, but there's none left, it's fail 18 | if(sp >= input->end) 19 | return 0; 20 | } 21 | switch(*pc++) { 22 | case Char: 23 | if(*sp != *pc++) 24 | return 0; 25 | case Any: 26 | sp++; 27 | continue; 28 | case Class: 29 | case ClassNot: 30 | if (!_re1_5_classmatch(pc, sp)) 31 | return 0; 32 | pc += *(unsigned char*)pc * 2 + 1; 33 | sp++; 34 | continue; 35 | case NamedClass: 36 | if (!_re1_5_namedclassmatch(pc, sp)) 37 | return 0; 38 | pc++; 39 | sp++; 40 | continue; 41 | case Match: 42 | return 1; 43 | case Jmp: 44 | off = (signed char)*pc++; 45 | pc = pc + off; 46 | continue; 47 | case Split: 48 | off = (signed char)*pc++; 49 | if(recursiveloop(pc, sp, input, subp, nsubp)) 50 | return 1; 51 | pc = pc + off; 52 | continue; 53 | case RSplit: 54 | off = (signed char)*pc++; 55 | if(recursiveloop(pc + off, sp, input, subp, nsubp)) 56 | return 1; 57 | continue; 58 | case Save: 59 | off = (unsigned char)*pc++; 60 | if(off >= nsubp) { 61 | continue; 62 | } 63 | old = subp[off]; 64 | subp[off] = sp; 65 | if(recursiveloop(pc, sp, input, subp, nsubp)) 66 | return 1; 67 | subp[off] = old; 68 | return 0; 69 | case Bol: 70 | if(sp != input->begin) 71 | return 0; 72 | continue; 73 | case Eol: 74 | if(sp != input->end) 75 | return 0; 76 | continue; 77 | } 78 | re1_5_fatal("recursiveloop"); 79 | } 80 | } 81 | 82 | int 83 | re1_5_recursiveloopprog(ByteProg *prog, Subject *input, const char **subp, int nsubp, int is_anchored) 84 | { 85 | return recursiveloop(HANDLE_ANCHORED(prog->insts, is_anchored), input->begin, input, subp, nsubp); 86 | } 87 | -------------------------------------------------------------------------------- /extmod/utime_mphal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * Copyright (c) 2016 Paul Sokolovsky 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 | #ifndef MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H 28 | #define MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H 29 | 30 | #include "py/obj.h" 31 | 32 | MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_obj); 33 | MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_ms_obj); 34 | MP_DECLARE_CONST_FUN_OBJ_1(mp_utime_sleep_us_obj); 35 | MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_ms_obj); 36 | MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_us_obj); 37 | MP_DECLARE_CONST_FUN_OBJ_0(mp_utime_ticks_cpu_obj); 38 | MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_diff_obj); 39 | MP_DECLARE_CONST_FUN_OBJ_2(mp_utime_ticks_add_obj); 40 | 41 | #endif // MICROPY_INCLUDED_EXTMOD_UTIME_MPHAL_H 42 | -------------------------------------------------------------------------------- /extmod/uzlib/crc32.c: -------------------------------------------------------------------------------- 1 | /* 2 | * CRC32 checksum 3 | * 4 | * Copyright (c) 1998-2003 by Joergen Ibsen / Jibz 5 | * All Rights Reserved 6 | * 7 | * http://www.ibsensoftware.com/ 8 | * 9 | * This software is provided 'as-is', without any express 10 | * or implied warranty. In no event will the authors be 11 | * held liable for any damages arising from the use of 12 | * this software. 13 | * 14 | * Permission is granted to anyone to use this software 15 | * for any purpose, including commercial applications, 16 | * and to alter it and redistribute it freely, subject to 17 | * the following restrictions: 18 | * 19 | * 1. The origin of this software must not be 20 | * misrepresented; you must not claim that you 21 | * wrote the original software. If you use this 22 | * software in a product, an acknowledgment in 23 | * the product documentation would be appreciated 24 | * but is not required. 25 | * 26 | * 2. Altered source versions must be plainly marked 27 | * as such, and must not be misrepresented as 28 | * being the original software. 29 | * 30 | * 3. This notice may not be removed or altered from 31 | * any source distribution. 32 | */ 33 | 34 | /* 35 | * CRC32 algorithm taken from the zlib source, which is 36 | * Copyright (C) 1995-1998 Jean-loup Gailly and Mark Adler 37 | */ 38 | 39 | #include "tinf.h" 40 | 41 | static const unsigned int tinf_crc32tab[16] = { 42 | 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 43 | 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, 44 | 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 45 | 0xbdbdf21c 46 | }; 47 | 48 | /* crc is previous value for incremental computation, 0xffffffff initially */ 49 | uint32_t uzlib_crc32(const void *data, unsigned int length, uint32_t crc) 50 | { 51 | const unsigned char *buf = (const unsigned char *)data; 52 | unsigned int i; 53 | 54 | for (i = 0; i < length; ++i) 55 | { 56 | crc ^= buf[i]; 57 | crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4); 58 | crc = tinf_crc32tab[crc & 0x0f] ^ (crc >> 4); 59 | } 60 | 61 | // return value suitable for passing in next time, for final value invert it 62 | return crc/* ^ 0xffffffff*/; 63 | } 64 | -------------------------------------------------------------------------------- /extmod/uzlib/defl_static.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) uzlib authors 3 | * 4 | * This software is provided 'as-is', without any express 5 | * or implied warranty. In no event will the authors be 6 | * held liable for any damages arising from the use of 7 | * this software. 8 | * 9 | * Permission is granted to anyone to use this software 10 | * for any purpose, including commercial applications, 11 | * and to alter it and redistribute it freely, subject to 12 | * the following restrictions: 13 | * 14 | * 1. The origin of this software must not be 15 | * misrepresented; you must not claim that you 16 | * wrote the original software. If you use this 17 | * software in a product, an acknowledgment in 18 | * the product documentation would be appreciated 19 | * but is not required. 20 | * 21 | * 2. Altered source versions must be plainly marked 22 | * as such, and must not be misrepresented as 23 | * being the original software. 24 | * 25 | * 3. This notice may not be removed or altered from 26 | * any source distribution. 27 | */ 28 | 29 | /* This files contains type declaration and prototypes for defl_static.c. 30 | They may be altered/distinct from the originals used in PuTTY source 31 | code. */ 32 | 33 | struct Outbuf { 34 | unsigned char *outbuf; 35 | int outlen, outsize; 36 | unsigned long outbits; 37 | int noutbits; 38 | int comp_disabled; 39 | }; 40 | 41 | void outbits(struct Outbuf *out, unsigned long bits, int nbits); 42 | void zlib_start_block(struct Outbuf *ctx); 43 | void zlib_finish_block(struct Outbuf *ctx); 44 | void zlib_literal(struct Outbuf *ectx, unsigned char c); 45 | void zlib_match(struct Outbuf *ectx, int distance, int len); 46 | -------------------------------------------------------------------------------- /extmod/uzlib/tinf.h: -------------------------------------------------------------------------------- 1 | /* Compatibility header for the original tinf lib/older versions of uzlib. 2 | Note: may be removed in the future, please migrate to uzlib.h. */ 3 | #include "uzlib.h" 4 | -------------------------------------------------------------------------------- /extmod/uzlib/tinf_compat.h: -------------------------------------------------------------------------------- 1 | /* This header contains compatibility defines for the original tinf API 2 | and uzlib 2.x and below API. These defines are deprecated and going 3 | to be removed in the future, so applications should migrate to new 4 | uzlib API. */ 5 | #define TINF_DATA struct uzlib_uncomp 6 | 7 | #define destSize dest_size 8 | #define destStart dest_start 9 | #define readSource source_read_cb 10 | -------------------------------------------------------------------------------- /extmod/uzlib/tinfzlib.c: -------------------------------------------------------------------------------- 1 | /* 2 | * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) 3 | * 4 | * Copyright (c) 2003 by Joergen Ibsen / Jibz 5 | * All Rights Reserved 6 | * 7 | * http://www.ibsensoftware.com/ 8 | * 9 | * Copyright (c) 2014-2018 by Paul Sokolovsky 10 | * 11 | * This software is provided 'as-is', without any express 12 | * or implied warranty. In no event will the authors be 13 | * held liable for any damages arising from the use of 14 | * this software. 15 | * 16 | * Permission is granted to anyone to use this software 17 | * for any purpose, including commercial applications, 18 | * and to alter it and redistribute it freely, subject to 19 | * the following restrictions: 20 | * 21 | * 1. The origin of this software must not be 22 | * misrepresented; you must not claim that you 23 | * wrote the original software. If you use this 24 | * software in a product, an acknowledgment in 25 | * the product documentation would be appreciated 26 | * but is not required. 27 | * 28 | * 2. Altered source versions must be plainly marked 29 | * as such, and must not be misrepresented as 30 | * being the original software. 31 | * 32 | * 3. This notice may not be removed or altered from 33 | * any source distribution. 34 | */ 35 | 36 | #include "tinf.h" 37 | 38 | int uzlib_zlib_parse_header(TINF_DATA *d) 39 | { 40 | unsigned char cmf, flg; 41 | 42 | /* -- get header bytes -- */ 43 | 44 | cmf = uzlib_get_byte(d); 45 | flg = uzlib_get_byte(d); 46 | 47 | /* -- check format -- */ 48 | 49 | /* check checksum */ 50 | if ((256*cmf + flg) % 31) return TINF_DATA_ERROR; 51 | 52 | /* check method is deflate */ 53 | if ((cmf & 0x0f) != 8) return TINF_DATA_ERROR; 54 | 55 | /* check window size is valid */ 56 | if ((cmf >> 4) > 7) return TINF_DATA_ERROR; 57 | 58 | /* check there is no preset dictionary */ 59 | if (flg & 0x20) return TINF_DATA_ERROR; 60 | 61 | /* initialize for adler32 checksum */ 62 | d->checksum_type = TINF_CHKSUM_ADLER; 63 | d->checksum = 1; 64 | 65 | return cmf >> 4; 66 | } 67 | -------------------------------------------------------------------------------- /extmod/uzlib/uzlib_conf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * uzlib - tiny deflate/inflate library (deflate, gzip, zlib) 3 | * 4 | * Copyright (c) 2014-2018 by Paul Sokolovsky 5 | */ 6 | 7 | #ifndef UZLIB_CONF_H_INCLUDED 8 | #define UZLIB_CONF_H_INCLUDED 9 | 10 | #ifndef UZLIB_CONF_DEBUG_LOG 11 | /* Debug logging level 0, 1, 2, etc. */ 12 | #define UZLIB_CONF_DEBUG_LOG 0 13 | #endif 14 | 15 | #ifndef UZLIB_CONF_PARANOID_CHECKS 16 | /* Perform extra checks on the input stream, even if they aren't proven 17 | to be strictly required (== lack of them wasn't proven to lead to 18 | crashes). */ 19 | #define UZLIB_CONF_PARANOID_CHECKS 0 20 | #endif 21 | 22 | #endif /* UZLIB_CONF_H_INCLUDED */ 23 | -------------------------------------------------------------------------------- /extmod/vfs_fat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_VFS_FAT_H 27 | #define MICROPY_INCLUDED_EXTMOD_VFS_FAT_H 28 | 29 | #include "py/obj.h" 30 | #include "lib/oofatfs/ff.h" 31 | #include "extmod/vfs.h" 32 | 33 | typedef struct _fs_user_mount_t { 34 | mp_obj_base_t base; 35 | mp_vfs_blockdev_t blockdev; 36 | FATFS fatfs; 37 | } fs_user_mount_t; 38 | 39 | extern const byte fresult_to_errno_table[20]; 40 | extern const mp_obj_type_t mp_fat_vfs_type; 41 | extern const mp_obj_type_t mp_type_vfs_fat_fileio; 42 | extern const mp_obj_type_t mp_type_vfs_fat_textio; 43 | 44 | MP_DECLARE_CONST_FUN_OBJ_3(fat_vfs_open_obj); 45 | 46 | #endif // MICROPY_INCLUDED_EXTMOD_VFS_FAT_H 47 | -------------------------------------------------------------------------------- /extmod/vfs_posix.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2018 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_VFS_POSIX_H 27 | #define MICROPY_INCLUDED_EXTMOD_VFS_POSIX_H 28 | 29 | #include "py/lexer.h" 30 | #include "py/obj.h" 31 | 32 | extern const mp_obj_type_t mp_type_vfs_posix; 33 | extern const mp_obj_type_t mp_type_vfs_posix_fileio; 34 | extern const mp_obj_type_t mp_type_vfs_posix_textio; 35 | 36 | mp_obj_t mp_vfs_posix_file_open(const mp_obj_type_t *type, mp_obj_t file_in, mp_obj_t mode_in); 37 | 38 | #endif // MICROPY_INCLUDED_EXTMOD_VFS_POSIX_H 39 | -------------------------------------------------------------------------------- /extmod/virtpin.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "extmod/virtpin.h" 28 | 29 | int mp_virtual_pin_read(mp_obj_t pin) { 30 | mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(pin); 31 | mp_pin_p_t *pin_p = (mp_pin_p_t*)s->type->protocol; 32 | return pin_p->ioctl(pin, MP_PIN_READ, 0, NULL); 33 | } 34 | 35 | void mp_virtual_pin_write(mp_obj_t pin, int value) { 36 | mp_obj_base_t* s = (mp_obj_base_t*)MP_OBJ_TO_PTR(pin); 37 | mp_pin_p_t *pin_p = (mp_pin_p_t*)s->type->protocol; 38 | pin_p->ioctl(pin, MP_PIN_WRITE, value, NULL); 39 | } 40 | -------------------------------------------------------------------------------- /extmod/virtpin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_EXTMOD_VIRTPIN_H 27 | #define MICROPY_INCLUDED_EXTMOD_VIRTPIN_H 28 | 29 | #include "py/obj.h" 30 | 31 | #define MP_PIN_READ (1) 32 | #define MP_PIN_WRITE (2) 33 | #define MP_PIN_INPUT (3) 34 | #define MP_PIN_OUTPUT (4) 35 | 36 | // Pin protocol 37 | typedef struct _mp_pin_p_t { 38 | mp_uint_t (*ioctl)(mp_obj_t obj, mp_uint_t request, uintptr_t arg, int *errcode); 39 | } mp_pin_p_t; 40 | 41 | int mp_virtual_pin_read(mp_obj_t pin); 42 | void mp_virtual_pin_write(mp_obj_t pin, int value); 43 | 44 | // If a port exposes a Pin object, it's constructor should be like this 45 | mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args); 46 | 47 | #endif // MICROPY_INCLUDED_EXTMOD_VIRTPIN_H 48 | -------------------------------------------------------------------------------- /extmod/webrepl/manifest.py: -------------------------------------------------------------------------------- 1 | freeze('.', ('webrepl.py', 'webrepl_setup.py', 'websocket_helper.py',)) 2 | -------------------------------------------------------------------------------- /extmod/webrepl/websocket_helper.py: -------------------------------------------------------------------------------- 1 | import sys 2 | try: 3 | import ubinascii as binascii 4 | except: 5 | import binascii 6 | try: 7 | import uhashlib as hashlib 8 | except: 9 | import hashlib 10 | 11 | DEBUG = 0 12 | 13 | def server_handshake(sock): 14 | clr = sock.makefile("rwb", 0) 15 | l = clr.readline() 16 | #sys.stdout.write(repr(l)) 17 | 18 | webkey = None 19 | 20 | while 1: 21 | l = clr.readline() 22 | if not l: 23 | raise OSError("EOF in headers") 24 | if l == b"\r\n": 25 | break 26 | # sys.stdout.write(l) 27 | h, v = [x.strip() for x in l.split(b":", 1)] 28 | if DEBUG: 29 | print((h, v)) 30 | if h == b'Sec-WebSocket-Key': 31 | webkey = v 32 | 33 | if not webkey: 34 | raise OSError("Not a websocket request") 35 | 36 | if DEBUG: 37 | print("Sec-WebSocket-Key:", webkey, len(webkey)) 38 | 39 | d = hashlib.sha1(webkey) 40 | d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") 41 | respkey = d.digest() 42 | respkey = binascii.b2a_base64(respkey)[:-1] 43 | if DEBUG: 44 | print("respkey:", respkey) 45 | 46 | sock.send(b"""\ 47 | HTTP/1.1 101 Switching Protocols\r 48 | Upgrade: websocket\r 49 | Connection: Upgrade\r 50 | Sec-WebSocket-Accept: """) 51 | sock.send(respkey) 52 | sock.send("\r\n\r\n") 53 | 54 | 55 | # Very simplified client handshake, works for MicroPython's 56 | # websocket server implementation, but probably not for other 57 | # servers. 58 | def client_handshake(sock): 59 | cl = sock.makefile("rwb", 0) 60 | cl.write(b"""\ 61 | GET / HTTP/1.1\r 62 | Host: echo.websocket.org\r 63 | Connection: Upgrade\r 64 | Upgrade: websocket\r 65 | Sec-WebSocket-Key: foo\r 66 | \r 67 | """) 68 | l = cl.readline() 69 | # print(l) 70 | while 1: 71 | l = cl.readline() 72 | if l == b"\r\n": 73 | break 74 | # sys.stdout.write(l) 75 | -------------------------------------------------------------------------------- /lib/mp-readline/readline.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H 27 | #define MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H 28 | 29 | #define CHAR_CTRL_A (1) 30 | #define CHAR_CTRL_B (2) 31 | #define CHAR_CTRL_C (3) 32 | #define CHAR_CTRL_D (4) 33 | #define CHAR_CTRL_E (5) 34 | #define CHAR_CTRL_F (6) 35 | #define CHAR_CTRL_K (11) 36 | #define CHAR_CTRL_N (14) 37 | #define CHAR_CTRL_P (16) 38 | #define CHAR_CTRL_U (21) 39 | 40 | void readline_init0(void); 41 | int readline(vstr_t *line, const char *prompt); 42 | void readline_push_history(const char *line); 43 | 44 | void readline_init(vstr_t *line, const char *prompt); 45 | void readline_note_newline(const char *prompt); 46 | int readline_process_char(int c); 47 | 48 | #endif // MICROPY_INCLUDED_LIB_MP_READLINE_READLINE_H 49 | -------------------------------------------------------------------------------- /lib/oofatfs/option/unicode.c: -------------------------------------------------------------------------------- 1 | #include "../ff.h" 2 | 3 | #if _USE_LFN != 0 4 | 5 | #if _CODE_PAGE == 932 /* Japanese Shift_JIS */ 6 | #include "cc932.c" 7 | #elif _CODE_PAGE == 936 /* Simplified Chinese GBK */ 8 | #include "cc936.c" 9 | #elif _CODE_PAGE == 949 /* Korean */ 10 | #include "cc949.c" 11 | #elif _CODE_PAGE == 950 /* Traditional Chinese Big5 */ 12 | #include "cc950.c" 13 | #else /* Single Byte Character-Set */ 14 | #include "ccsbcs.c" 15 | #endif 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /lib/utils/gchelper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_LIB_UTILS_GCHELPER_H 27 | #define MICROPY_INCLUDED_LIB_UTILS_GCHELPER_H 28 | 29 | #include 30 | 31 | #if MICROPY_GCREGS_SETJMP 32 | #include 33 | typedef jmp_buf gc_helper_regs_t; 34 | #else 35 | 36 | #if defined(__x86_64__) 37 | typedef uintptr_t gc_helper_regs_t[6]; 38 | #elif defined(__i386__) 39 | typedef uintptr_t gc_helper_regs_t[4]; 40 | #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__) 41 | typedef uintptr_t gc_helper_regs_t[10]; 42 | #endif 43 | 44 | #endif 45 | 46 | void gc_helper_collect_regs_and_stack(void); 47 | 48 | #endif // MICROPY_INCLUDED_LIB_UTILS_GCHELPER_H 49 | -------------------------------------------------------------------------------- /lib/utils/gchelper_m0.s: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2018 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | .syntax unified 28 | .cpu cortex-m0 29 | .thumb 30 | 31 | .section .text 32 | .align 2 33 | 34 | .global gc_helper_get_regs_and_sp 35 | .type gc_helper_get_regs_and_sp, %function 36 | 37 | @ uint gc_helper_get_regs_and_sp(r0=uint regs[10]) 38 | gc_helper_get_regs_and_sp: 39 | @ store registers into given array 40 | str r4, [r0, #0] 41 | str r5, [r0, #4] 42 | str r6, [r0, #8] 43 | str r7, [r0, #12] 44 | mov r1, r8 45 | str r1, [r0, #16] 46 | mov r1, r9 47 | str r1, [r0, #20] 48 | mov r1, r10 49 | str r1, [r0, #24] 50 | mov r1, r11 51 | str r1, [r0, #28] 52 | mov r1, r12 53 | str r1, [r0, #32] 54 | mov r1, r13 55 | str r1, [r0, #36] 56 | 57 | @ return the sp 58 | mov r0, sp 59 | bx lr 60 | 61 | .size gc_helper_get_regs_and_sp, .-gc_helper_get_regs_and_sp 62 | -------------------------------------------------------------------------------- /lib/utils/gchelper_m3.s: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | .syntax unified 28 | .cpu cortex-m3 29 | .thumb 30 | 31 | .section .text 32 | .align 2 33 | 34 | .global gc_helper_get_regs_and_sp 35 | .type gc_helper_get_regs_and_sp, %function 36 | 37 | @ uint gc_helper_get_regs_and_sp(r0=uint regs[10]) 38 | gc_helper_get_regs_and_sp: 39 | @ store registers into given array 40 | str r4, [r0], #4 41 | str r5, [r0], #4 42 | str r6, [r0], #4 43 | str r7, [r0], #4 44 | str r8, [r0], #4 45 | str r9, [r0], #4 46 | str r10, [r0], #4 47 | str r11, [r0], #4 48 | str r12, [r0], #4 49 | str r13, [r0], #4 50 | 51 | @ return the sp 52 | mov r0, sp 53 | bx lr 54 | 55 | .size gc_helper_get_regs_and_sp, .-gc_helper_get_regs_and_sp 56 | -------------------------------------------------------------------------------- /lib/utils/interrupt_char.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/obj.h" 28 | #include "py/mpstate.h" 29 | 30 | #if MICROPY_KBD_EXCEPTION 31 | 32 | int mp_interrupt_char = -1; 33 | 34 | void mp_hal_set_interrupt_char(int c) { 35 | mp_interrupt_char = c; 36 | } 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /lib/utils/interrupt_char.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_LIB_UTILS_INTERRUPT_CHAR_H 27 | #define MICROPY_INCLUDED_LIB_UTILS_INTERRUPT_CHAR_H 28 | 29 | extern int mp_interrupt_char; 30 | void mp_hal_set_interrupt_char(int c); 31 | 32 | #endif // MICROPY_INCLUDED_LIB_UTILS_INTERRUPT_CHAR_H 33 | -------------------------------------------------------------------------------- /lib/utils/stdout_helpers.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "py/mpconfig.h" 4 | #include "py/mphal.h" 5 | 6 | /* 7 | * Extra stdout functions 8 | * These can be either optimized for a particular port, or reference 9 | * implementation below can be used. 10 | */ 11 | 12 | // Send "cooked" string of given length, where every occurrence of 13 | // LF character is replaced with CR LF. 14 | void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) { 15 | while (len--) { 16 | if (*str == '\n') { 17 | mp_hal_stdout_tx_strn("\r", 1); 18 | } 19 | mp_hal_stdout_tx_strn(str++, 1); 20 | } 21 | } 22 | 23 | // Send zero-terminated string 24 | void mp_hal_stdout_tx_str(const char *str) { 25 | mp_hal_stdout_tx_strn(str, strlen(str)); 26 | } 27 | -------------------------------------------------------------------------------- /port/gccollect.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2018 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #include 27 | 28 | #include "py/mpstate.h" 29 | #include "py/gc.h" 30 | 31 | void gc_collect(void) { 32 | gc_collect_start(); 33 | 34 | #if MICROPY_PY_THREAD 35 | // trace root pointers from any threads 36 | mp_thread_gc_others(); 37 | #else 38 | // gc the main thread stack 39 | gc_collect_root(rt_thread_self()->stack_addr, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)rt_thread_self()->stack_addr) / 4); 40 | #endif 41 | 42 | gc_collect_end(); 43 | // gc_dump_info(); 44 | } 45 | -------------------------------------------------------------------------------- /port/genhdr/moduledefs.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/port/genhdr/moduledefs.h -------------------------------------------------------------------------------- /port/genhdr/mpversion.h: -------------------------------------------------------------------------------- 1 | // This file was generated by py/makeversionhdr.py 2 | #define MICROPY_GIT_TAG "v1.13-148-ged7ddd4" 3 | #define MICROPY_GIT_HASH "ed7ddd4" 4 | #define MICROPY_BUILD_DATE "2020-11-03" 5 | -------------------------------------------------------------------------------- /port/modules/machine/machine_adc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef MICROPY_INCLUDED_MACHINE_ADC_H 28 | #define MICROPY_INCLUDED_MACHINE_ADC_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_adc_type; 34 | 35 | #endif // MICROPY_INCLUDED_MACHINE_ADC_H 36 | -------------------------------------------------------------------------------- /port/modules/machine/machine_lcd.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 SummerGift 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_MACHINE_LCD_H 27 | #define MICROPY_INCLUDED_MACHINE_LCD_H 28 | 29 | extern const mp_obj_type_t machine_lcd_type; 30 | 31 | #endif // MICROPY_INCLUDED_MACHINE_LCD_H 32 | -------------------------------------------------------------------------------- /port/modules/machine/machine_pwm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef MICROPY_INCLUDED_MACHINE_PWM_H 28 | #define MICROPY_INCLUDED_MACHINE_PWM_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_pwm_type; 34 | 35 | #endif // MICROPY_INCLUDED_MACHINE_PWM_H 36 | -------------------------------------------------------------------------------- /port/modules/machine/machine_rtc.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef MICROPY_INCLUDED_MACHINE_RTC_H 28 | #define MICROPY_INCLUDED_MACHINE_RTC_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_rtc_type; 34 | 35 | #endif // MICROPY_INCLUDED_MACHINE_RTC_H 36 | -------------------------------------------------------------------------------- /port/modules/machine/machine_timer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef MICROPY_INCLUDED_MACHINE_TIMER_H 28 | #define MICROPY_INCLUDED_MACHINE_TIMER_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_timer_type; 34 | 35 | #endif // MICROPY_INCLUDED_MACHINE_TIMER_H 36 | 37 | -------------------------------------------------------------------------------- /port/modules/machine/machine_uart.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 SummerGift 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef MICROPY_INCLUDED_MACHINE_UART_H 28 | #define MICROPY_INCLUDED_MACHINE_UART_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_uart_type; 34 | 35 | #endif // _MACHINE_UART_H 36 | -------------------------------------------------------------------------------- /port/modules/machine/machine_wdt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef MICROPY_INCLUDED_MACHINE_WDT_H 28 | #define MICROPY_INCLUDED_MACHINE_WDT_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_wdt_type; 34 | 35 | #endif // MICROPY_INCLUDED_MACHINE_WDT_H 36 | 37 | -------------------------------------------------------------------------------- /port/modules/machine/modmachine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef _MODMACHINE_H 28 | #define _MODMACHINE_H 29 | 30 | #include "py/obj.h" 31 | #include 32 | 33 | extern const mp_obj_type_t machine_pin_type; 34 | 35 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(machine_info_obj); 36 | MP_DECLARE_CONST_FUN_OBJ_0(machine_unique_id_obj); 37 | MP_DECLARE_CONST_FUN_OBJ_0(machine_reset_obj); 38 | MP_DECLARE_CONST_FUN_OBJ_0(machine_bootloader_obj); 39 | MP_DECLARE_CONST_FUN_OBJ_0(machine_freq_obj); 40 | MP_DECLARE_CONST_FUN_OBJ_0(pyb_wfi_obj); 41 | MP_DECLARE_CONST_FUN_OBJ_0(pyb_disable_irq_obj); 42 | MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_enable_irq_obj); 43 | MP_DECLARE_CONST_FUN_OBJ_0(machine_sleep_obj); 44 | MP_DECLARE_CONST_FUN_OBJ_0(machine_deepsleep_obj); 45 | 46 | typedef struct _machine_pin_obj_t { 47 | mp_obj_base_t base; 48 | char name[RT_NAME_MAX]; 49 | uint32_t pin; 50 | mp_obj_t pin_isr_cb; 51 | } machine_pin_obj_t; 52 | 53 | #endif // _MODMACHINE_H 54 | -------------------------------------------------------------------------------- /port/modules/modnetwork.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | #include "py/objlist.h" 32 | #include "py/runtime.h" 33 | #include "py/mphal.h" 34 | #include "lib/netutils/netutils.h" 35 | #include "modnetwork.h" 36 | 37 | #if MICROPY_PY_NETWORK 38 | 39 | STATIC const mp_rom_map_elem_t mp_module_network_globals_table[] = { 40 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_network) }, 41 | 42 | #if defined(MICROPY_PY_WLAN) 43 | { MP_ROM_QSTR(MP_QSTR_WLAN), MP_ROM_PTR(&get_wlan_obj) }, 44 | { MP_ROM_QSTR(MP_QSTR_STA_IF), MP_ROM_INT(STATION_IF)}, 45 | { MP_ROM_QSTR(MP_QSTR_AP_IF), MP_ROM_INT(SOFTAP_IF)}, 46 | #endif 47 | }; 48 | 49 | STATIC MP_DEFINE_CONST_DICT(mp_module_network_globals, mp_module_network_globals_table); 50 | 51 | const mp_obj_module_t mp_module_network = { 52 | .base = { &mp_type_module }, 53 | .globals = (mp_obj_dict_t*)&mp_module_network_globals, 54 | }; 55 | 56 | #endif // MICROPY_PY_NETWORK 57 | -------------------------------------------------------------------------------- /port/modules/modnetwork.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_MODNETWORK_H 27 | #define MICROPY_INCLUDED_MODNETWORK_H 28 | 29 | #define STATION_IF 0 30 | #define SOFTAP_IF 1 31 | 32 | #define MOD_NETWORK_AF_INET (2) 33 | #define MOD_NETWORK_AF_INET6 (10) 34 | 35 | #define MOD_NETWORK_SOCK_STREAM (1) 36 | #define MOD_NETWORK_SOCK_DGRAM (2) 37 | #define MOD_NETWORK_SOCK_RAW (3) 38 | 39 | #define MODNETWORK_INCLUDE_CONSTANTS (1) 40 | 41 | MP_DECLARE_CONST_FUN_OBJ_KW(get_wlan_obj); 42 | 43 | #endif // MICROPY_INCLUDED_MODNETWORK_H 44 | -------------------------------------------------------------------------------- /port/modules/user/moduserfunc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 SummerGift 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/runtime.h" 28 | 29 | STATIC mp_obj_t add( 30 | mp_obj_t arg_1_obj, 31 | mp_obj_t arg_2_obj) { 32 | mp_int_t arg_1 = mp_obj_get_int(arg_1_obj); 33 | mp_int_t arg_2 = mp_obj_get_int(arg_2_obj); 34 | mp_int_t ret_val; 35 | 36 | /* Your code start! */ 37 | 38 | ret_val = arg_1 + arg_2; 39 | 40 | /* Your code end! */ 41 | 42 | return mp_obj_new_int(ret_val); 43 | } 44 | MP_DEFINE_CONST_FUN_OBJ_2(add_obj, add); 45 | 46 | STATIC const mp_rom_map_elem_t mp_module_userfunc_globals_table[] = { 47 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_userfunc) }, 48 | { MP_ROM_QSTR(MP_QSTR_add), MP_ROM_PTR(&add_obj) }, 49 | }; 50 | 51 | STATIC MP_DEFINE_CONST_DICT(mp_module_userfunc_globals, mp_module_userfunc_globals_table); 52 | 53 | const mp_obj_module_t mp_module_userfunc = { 54 | .base = { &mp_type_module }, 55 | .globals = (mp_obj_dict_t*)&mp_module_userfunc_globals, 56 | }; 57 | 58 | -------------------------------------------------------------------------------- /port/mpgetcharport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef _MPGETCHARPORT_H_ 28 | #define _MPGETCHARPORT_H_ 29 | 30 | void mp_getchar_init(void); 31 | void mp_getchar_deinit(void); 32 | int mp_getchar(void); 33 | 34 | #endif /* _MPGETCHARPORT_H_ */ 35 | -------------------------------------------------------------------------------- /port/mpputsnport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef _MPPUTCHARPORT_H_ 28 | #define _MPPUTCHARPORT_H_ 29 | 30 | void mp_putsn_init(void); 31 | void mp_putsn_deinit(void); 32 | void mp_putsn(const char *str, size_t len); 33 | void mp_putsn_stream(const char *str, size_t len); 34 | 35 | #endif /* _MPPUTCHARPORT_H_ */ 36 | -------------------------------------------------------------------------------- /port/mpthreadport.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2018 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #ifndef _MPTHREADPORT_H 28 | #define _MPTHREADPORT_H 29 | 30 | #include "rtthread.h" 31 | 32 | typedef struct _mp_thread_mutex_t { 33 | struct rt_mutex mutex; 34 | int is_init; 35 | } mp_thread_mutex_t; 36 | 37 | void mp_thread_init(void *stack, uint32_t stack_len); 38 | void mp_thread_gc_others(void); 39 | void mp_thread_deinit(void); 40 | 41 | #endif // _MPTHREADPORT_H 42 | -------------------------------------------------------------------------------- /port/mpy_project_cfg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | /** 28 | * MicroPython Project compile configuration 29 | */ 30 | #define NDEBUG 0 31 | #define N_X64 0 32 | #define N_X86 0 33 | #define N_THUMB 0 34 | #define N_ARM 0 35 | #define N_XTENSA 0 36 | -------------------------------------------------------------------------------- /port/native/easyflash_module.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #if defined(MICROPYTHON_USING_FFI) && defined(PKG_EASYFLASH_ENV) 31 | 32 | #include 33 | 34 | RTM_EXPORT(ef_set_env) 35 | RTM_EXPORT(ef_get_env) 36 | RTM_EXPORT(ef_del_env) 37 | RTM_EXPORT(ef_print_env) 38 | 39 | #endif /* defined(MICROPYTHON_USING_FFI) && defined(PKG_EASYFLASH_ENV) */ 40 | -------------------------------------------------------------------------------- /port/native/easyflash_module.py: -------------------------------------------------------------------------------- 1 | import ffi 2 | 3 | print = ffi.func("v", "ef_print_env", "v") 4 | set = ffi.func("i", "ef_set_env", "ss") 5 | get = ffi.func("s", "ef_get_env", "s") 6 | remove = ffi.func("i", "ef_del_env", "s") 7 | -------------------------------------------------------------------------------- /port/native/native_module.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 Armink (armink.ztl@gmail.com) 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #ifdef MICROPYTHON_USING_FFI 31 | 32 | /* 33 | * Native module implement by C function export. 34 | * The python module will using ffi to import all of functions. 35 | * You can call those functions by: 36 | * 37 | * STEP1: 38 | * generate the python module from 'native_module.c' to 'native_module.py' 39 | * run 'python native_gen.py native_module.c' command 40 | * 41 | * STEP2: 42 | * push the 'native_module.py' file to '/libs/mpy/' folder on target 43 | * 44 | * STEP3: 45 | * import native_modbule 46 | * 47 | * STEP4: 48 | * native_module.show("Hello native module") 49 | * native_module.add(1, 2) 50 | */ 51 | 52 | void native_module_show(const char *str) { 53 | mp_printf(&mp_plat_print, "Native module show: %s\n", str); 54 | } 55 | RTM_EXPORT(native_module_show) 56 | 57 | int native_module_add(int a, int b) { 58 | return a + b; 59 | } 60 | RTM_EXPORT(native_module_add) 61 | 62 | #endif /* MICROPYTHON_USING_FFI */ 63 | -------------------------------------------------------------------------------- /port/native/native_module.py: -------------------------------------------------------------------------------- 1 | import ffi 2 | 3 | show = ffi.func("v", "native_module_show", "s") 4 | add = ffi.func("i", "native_module_add", "ii") 5 | -------------------------------------------------------------------------------- /port/qstrdefsport.h: -------------------------------------------------------------------------------- 1 | // qstrs specific to this port 2 | -------------------------------------------------------------------------------- /py/compile.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_COMPILE_H 27 | #define MICROPY_INCLUDED_PY_COMPILE_H 28 | 29 | #include "py/lexer.h" 30 | #include "py/parse.h" 31 | #include "py/emitglue.h" 32 | 33 | // the compiler will raise an exception if an error occurred 34 | // the compiler will clear the parse tree before it returns 35 | mp_obj_t mp_compile(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl); 36 | 37 | #if MICROPY_PERSISTENT_CODE_SAVE 38 | // this has the same semantics as mp_compile 39 | mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_file, bool is_repl); 40 | #endif 41 | 42 | // this is implemented in runtime.c 43 | mp_obj_t mp_parse_compile_execute(mp_lexer_t *lex, mp_parse_input_kind_t parse_input_kind, mp_obj_dict_t *globals, mp_obj_dict_t *locals); 44 | 45 | #endif // MICROPY_INCLUDED_PY_COMPILE_H 46 | -------------------------------------------------------------------------------- /py/emitnarm.c: -------------------------------------------------------------------------------- 1 | // ARM specific stuff 2 | 3 | #include "py/mpconfig.h" 4 | 5 | #if MICROPY_EMIT_ARM 6 | 7 | // This is defined so that the assembler exports generic assembler API macros 8 | #define GENERIC_ASM_API (1) 9 | #include "py/asmarm.h" 10 | 11 | // Word indices of REG_LOCAL_x in nlr_buf_t 12 | #define NLR_BUF_IDX_LOCAL_1 (3) // r4 13 | #define NLR_BUF_IDX_LOCAL_2 (4) // r5 14 | #define NLR_BUF_IDX_LOCAL_3 (5) // r6 15 | 16 | #define N_ARM (1) 17 | #define EXPORT_FUN(name) emit_native_arm_##name 18 | #include "py/emitnative.c" 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /py/emitnthumb.c: -------------------------------------------------------------------------------- 1 | // thumb specific stuff 2 | 3 | #include "py/mpconfig.h" 4 | 5 | #if MICROPY_EMIT_THUMB 6 | 7 | // this is defined so that the assembler exports generic assembler API macros 8 | #define GENERIC_ASM_API (1) 9 | #include "py/asmthumb.h" 10 | 11 | // Word indices of REG_LOCAL_x in nlr_buf_t 12 | #define NLR_BUF_IDX_LOCAL_1 (3) // r4 13 | #define NLR_BUF_IDX_LOCAL_2 (4) // r5 14 | #define NLR_BUF_IDX_LOCAL_3 (5) // r6 15 | 16 | #define N_THUMB (1) 17 | #define EXPORT_FUN(name) emit_native_thumb_##name 18 | #include "py/emitnative.c" 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /py/emitnx64.c: -------------------------------------------------------------------------------- 1 | // x64 specific stuff 2 | 3 | #include "py/mpconfig.h" 4 | 5 | #if MICROPY_EMIT_X64 6 | 7 | // This is defined so that the assembler exports generic assembler API macros 8 | #define GENERIC_ASM_API (1) 9 | #include "py/asmx64.h" 10 | 11 | // Word indices of REG_LOCAL_x in nlr_buf_t 12 | #define NLR_BUF_IDX_LOCAL_1 (5) // rbx 13 | #define NLR_BUF_IDX_LOCAL_2 (6) // r12 14 | #define NLR_BUF_IDX_LOCAL_3 (7) // r13 15 | 16 | #define N_X64 (1) 17 | #define EXPORT_FUN(name) emit_native_x64_##name 18 | #include "py/emitnative.c" 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /py/emitnx86.c: -------------------------------------------------------------------------------- 1 | // x86 specific stuff 2 | 3 | #include "py/mpconfig.h" 4 | #include "py/nativeglue.h" 5 | 6 | #if MICROPY_EMIT_X86 7 | 8 | // This is defined so that the assembler exports generic assembler API macros 9 | #define GENERIC_ASM_API (1) 10 | #include "py/asmx86.h" 11 | 12 | // Word indices of REG_LOCAL_x in nlr_buf_t 13 | #define NLR_BUF_IDX_LOCAL_1 (5) // ebx 14 | #define NLR_BUF_IDX_LOCAL_2 (7) // esi 15 | #define NLR_BUF_IDX_LOCAL_3 (6) // edi 16 | 17 | // x86 needs a table to know how many args a given function has 18 | STATIC byte mp_f_n_args[MP_F_NUMBER_OF] = { 19 | [MP_F_CONVERT_OBJ_TO_NATIVE] = 2, 20 | [MP_F_CONVERT_NATIVE_TO_OBJ] = 2, 21 | [MP_F_NATIVE_SWAP_GLOBALS] = 1, 22 | [MP_F_LOAD_NAME] = 1, 23 | [MP_F_LOAD_GLOBAL] = 1, 24 | [MP_F_LOAD_BUILD_CLASS] = 0, 25 | [MP_F_LOAD_ATTR] = 2, 26 | [MP_F_LOAD_METHOD] = 3, 27 | [MP_F_LOAD_SUPER_METHOD] = 2, 28 | [MP_F_STORE_NAME] = 2, 29 | [MP_F_STORE_GLOBAL] = 2, 30 | [MP_F_STORE_ATTR] = 3, 31 | [MP_F_OBJ_SUBSCR] = 3, 32 | [MP_F_OBJ_IS_TRUE] = 1, 33 | [MP_F_UNARY_OP] = 2, 34 | [MP_F_BINARY_OP] = 3, 35 | [MP_F_BUILD_TUPLE] = 2, 36 | [MP_F_BUILD_LIST] = 2, 37 | [MP_F_BUILD_MAP] = 1, 38 | [MP_F_BUILD_SET] = 2, 39 | [MP_F_STORE_SET] = 2, 40 | [MP_F_LIST_APPEND] = 2, 41 | [MP_F_STORE_MAP] = 3, 42 | [MP_F_MAKE_FUNCTION_FROM_RAW_CODE] = 3, 43 | [MP_F_NATIVE_CALL_FUNCTION_N_KW] = 3, 44 | [MP_F_CALL_METHOD_N_KW] = 3, 45 | [MP_F_CALL_METHOD_N_KW_VAR] = 3, 46 | [MP_F_NATIVE_GETITER] = 2, 47 | [MP_F_NATIVE_ITERNEXT] = 1, 48 | [MP_F_NLR_PUSH] = 1, 49 | [MP_F_NLR_POP] = 0, 50 | [MP_F_NATIVE_RAISE] = 1, 51 | [MP_F_IMPORT_NAME] = 3, 52 | [MP_F_IMPORT_FROM] = 2, 53 | [MP_F_IMPORT_ALL] = 1, 54 | [MP_F_NEW_SLICE] = 3, 55 | [MP_F_UNPACK_SEQUENCE] = 3, 56 | [MP_F_UNPACK_EX] = 3, 57 | [MP_F_DELETE_NAME] = 1, 58 | [MP_F_DELETE_GLOBAL] = 1, 59 | [MP_F_MAKE_CLOSURE_FROM_RAW_CODE] = 3, 60 | [MP_F_ARG_CHECK_NUM_SIG] = 3, 61 | [MP_F_SETUP_CODE_STATE] = 4, 62 | [MP_F_SMALL_INT_FLOOR_DIVIDE] = 2, 63 | [MP_F_SMALL_INT_MODULO] = 2, 64 | [MP_F_NATIVE_YIELD_FROM] = 3, 65 | [MP_F_SETJMP] = 1, 66 | }; 67 | 68 | #define N_X86 (1) 69 | #define EXPORT_FUN(name) emit_native_x86_##name 70 | #include "py/emitnative.c" 71 | 72 | #endif 73 | -------------------------------------------------------------------------------- /py/emitnxtensa.c: -------------------------------------------------------------------------------- 1 | // Xtensa specific stuff 2 | 3 | #include "py/mpconfig.h" 4 | 5 | #if MICROPY_EMIT_XTENSA 6 | 7 | // this is defined so that the assembler exports generic assembler API macros 8 | #define GENERIC_ASM_API (1) 9 | #include "py/asmxtensa.h" 10 | 11 | // Word indices of REG_LOCAL_x in nlr_buf_t 12 | #define NLR_BUF_IDX_LOCAL_1 (8) // a12 13 | #define NLR_BUF_IDX_LOCAL_2 (9) // a13 14 | #define NLR_BUF_IDX_LOCAL_3 (10) // a14 15 | 16 | #define N_XTENSA (1) 17 | #define EXPORT_FUN(name) emit_native_xtensa_##name 18 | #include "py/emitnative.c" 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /py/emitnxtensawin.c: -------------------------------------------------------------------------------- 1 | // Xtensa-Windowed specific stuff 2 | 3 | #include "py/mpconfig.h" 4 | 5 | #if MICROPY_EMIT_XTENSAWIN 6 | 7 | // this is defined so that the assembler exports generic assembler API macros 8 | #define GENERIC_ASM_API (1) 9 | #define GENERIC_ASM_API_WIN (1) 10 | #include "py/asmxtensa.h" 11 | 12 | // Word indices of REG_LOCAL_x in nlr_buf_t 13 | #define NLR_BUF_IDX_LOCAL_1 (2 + 4) // a4 14 | #define NLR_BUF_IDX_LOCAL_2 (2 + 5) // a5 15 | #define NLR_BUF_IDX_LOCAL_3 (2 + 6) // a6 16 | 17 | #define N_NLR_SETJMP (1) 18 | #define N_PRELUDE_AS_BYTES_OBJ (1) 19 | #define N_XTENSAWIN (1) 20 | #define EXPORT_FUN(name) emit_native_xtensawin_##name 21 | #include "py/emitnative.c" 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /py/formatfloat.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_FORMATFLOAT_H 27 | #define MICROPY_INCLUDED_PY_FORMATFLOAT_H 28 | 29 | #include "py/mpconfig.h" 30 | 31 | #if MICROPY_PY_BUILTINS_FLOAT 32 | int mp_format_float(mp_float_t f, char *buf, size_t bufSize, char fmt, int prec, char sign); 33 | #endif 34 | 35 | #endif // MICROPY_INCLUDED_PY_FORMATFLOAT_H 36 | -------------------------------------------------------------------------------- /py/frozenmod.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Paul Sokolovsky 7 | * Copyright (c) 2016 Damien P. George 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 | #ifndef MICROPY_INCLUDED_PY_FROZENMOD_H 28 | #define MICROPY_INCLUDED_PY_FROZENMOD_H 29 | 30 | #include "py/lexer.h" 31 | 32 | enum { 33 | MP_FROZEN_NONE, 34 | MP_FROZEN_STR, 35 | MP_FROZEN_MPY, 36 | }; 37 | 38 | int mp_find_frozen_module(const char *str, size_t len, void **data); 39 | const char *mp_find_frozen_str(const char *str, size_t *len); 40 | mp_import_stat_t mp_frozen_stat(const char *str); 41 | 42 | #endif // MICROPY_INCLUDED_PY_FROZENMOD_H 43 | -------------------------------------------------------------------------------- /py/mkenv.mk: -------------------------------------------------------------------------------- 1 | ifneq ($(lastword a b),b) 2 | $(error These Makefiles require make 3.81 or newer) 3 | endif 4 | 5 | # Set TOP to be the path to get from the current directory (where make was 6 | # invoked) to the top of the tree. $(lastword $(MAKEFILE_LIST)) returns 7 | # the name of this makefile relative to where make was invoked. 8 | # 9 | # We assume that this file is in the py directory so we use $(dir ) twice 10 | # to get to the top of the tree. 11 | 12 | THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST)) 13 | TOP := $(patsubst %/py/mkenv.mk,%,$(THIS_MAKEFILE)) 14 | 15 | # Turn on increased build verbosity by defining BUILD_VERBOSE in your main 16 | # Makefile or in your environment. You can also use V=1 on the make command 17 | # line. 18 | 19 | ifeq ("$(origin V)", "command line") 20 | BUILD_VERBOSE=$(V) 21 | endif 22 | ifndef BUILD_VERBOSE 23 | BUILD_VERBOSE = 0 24 | endif 25 | ifeq ($(BUILD_VERBOSE),0) 26 | Q = @ 27 | else 28 | Q = 29 | endif 30 | # Since this is a new feature, advertise it 31 | ifeq ($(BUILD_VERBOSE),0) 32 | $(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.) 33 | endif 34 | 35 | # default settings; can be overridden in main Makefile 36 | 37 | PY_SRC ?= $(TOP)/py 38 | BUILD ?= build 39 | 40 | RM = rm 41 | ECHO = @echo 42 | CP = cp 43 | MKDIR = mkdir 44 | SED = sed 45 | CAT = cat 46 | TOUCH = touch 47 | PYTHON = python3 48 | 49 | AS = $(CROSS_COMPILE)as 50 | CC = $(CROSS_COMPILE)gcc 51 | CXX = $(CROSS_COMPILE)g++ 52 | GDB = $(CROSS_COMPILE)gdb 53 | LD = $(CROSS_COMPILE)ld 54 | OBJCOPY = $(CROSS_COMPILE)objcopy 55 | SIZE = $(CROSS_COMPILE)size 56 | STRIP = $(CROSS_COMPILE)strip 57 | AR = $(CROSS_COMPILE)ar 58 | 59 | MAKE_MANIFEST = $(PYTHON) $(TOP)/tools/makemanifest.py 60 | MAKE_FROZEN = $(PYTHON) $(TOP)/tools/make-frozen.py 61 | MPY_CROSS = $(TOP)/mpy-cross/mpy-cross 62 | MPY_TOOL = $(PYTHON) $(TOP)/tools/mpy-tool.py 63 | 64 | MPY_LIB_DIR = $(TOP)/../micropython-lib 65 | 66 | all: 67 | .PHONY: all 68 | 69 | .DELETE_ON_ERROR: 70 | 71 | MKENV_INCLUDED = 1 72 | -------------------------------------------------------------------------------- /py/modarray.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/builtin.h" 28 | 29 | #if MICROPY_PY_ARRAY 30 | 31 | STATIC const mp_rom_map_elem_t mp_module_array_globals_table[] = { 32 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uarray) }, 33 | { MP_ROM_QSTR(MP_QSTR_array), MP_ROM_PTR(&mp_type_array) }, 34 | }; 35 | 36 | STATIC MP_DEFINE_CONST_DICT(mp_module_array_globals, mp_module_array_globals_table); 37 | 38 | const mp_obj_module_t mp_module_uarray = { 39 | .base = { &mp_type_module }, 40 | .globals = (mp_obj_dict_t *)&mp_module_array_globals, 41 | }; 42 | 43 | MP_REGISTER_MODULE(MP_QSTR_uarray, mp_module_uarray, MICROPY_PY_ARRAY); 44 | 45 | #endif 46 | -------------------------------------------------------------------------------- /py/modcollections.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/builtin.h" 28 | 29 | #if MICROPY_PY_COLLECTIONS 30 | 31 | STATIC const mp_rom_map_elem_t mp_module_collections_globals_table[] = { 32 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ucollections) }, 33 | #if MICROPY_PY_COLLECTIONS_DEQUE 34 | { MP_ROM_QSTR(MP_QSTR_deque), MP_ROM_PTR(&mp_type_deque) }, 35 | #endif 36 | { MP_ROM_QSTR(MP_QSTR_namedtuple), MP_ROM_PTR(&mp_namedtuple_obj) }, 37 | #if MICROPY_PY_COLLECTIONS_ORDEREDDICT 38 | { MP_ROM_QSTR(MP_QSTR_OrderedDict), MP_ROM_PTR(&mp_type_ordereddict) }, 39 | #endif 40 | }; 41 | 42 | STATIC MP_DEFINE_CONST_DICT(mp_module_collections_globals, mp_module_collections_globals_table); 43 | 44 | const mp_obj_module_t mp_module_collections = { 45 | .base = { &mp_type_module }, 46 | .globals = (mp_obj_dict_t *)&mp_module_collections_globals, 47 | }; 48 | 49 | #endif // MICROPY_PY_COLLECTIONS 50 | -------------------------------------------------------------------------------- /py/mpstate.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpstate.h" 28 | 29 | #if MICROPY_DYNAMIC_COMPILER 30 | mp_dynamic_compiler_t mp_dynamic_compiler = {0}; 31 | #endif 32 | 33 | mp_state_ctx_t mp_state_ctx; 34 | -------------------------------------------------------------------------------- /py/nlr.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2017 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpstate.h" 28 | 29 | #if !MICROPY_NLR_SETJMP 30 | // When not using setjmp, nlr_push_tail is called from inline asm so needs special care 31 | #if MICROPY_NLR_X86 && MICROPY_NLR_OS_WINDOWS 32 | // On these 32-bit platforms make sure nlr_push_tail doesn't have a leading underscore 33 | unsigned int nlr_push_tail(nlr_buf_t *nlr) asm ("nlr_push_tail"); 34 | #else 35 | // LTO can't see inside inline asm functions so explicitly mark nlr_push_tail as used 36 | __attribute__((used)) unsigned int nlr_push_tail(nlr_buf_t *nlr); 37 | #endif 38 | #endif 39 | 40 | unsigned int nlr_push_tail(nlr_buf_t *nlr) { 41 | nlr_buf_t **top = &MP_STATE_THREAD(nlr_top); 42 | nlr->prev = *top; 43 | MP_NLR_SAVE_PYSTACK(nlr); 44 | *top = nlr; 45 | return 0; // normal return 46 | } 47 | 48 | void nlr_pop(void) { 49 | nlr_buf_t **top = &MP_STATE_THREAD(nlr_top); 50 | *top = (*top)->prev; 51 | } 52 | -------------------------------------------------------------------------------- /py/nlrsetjmp.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2017 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/mpstate.h" 28 | 29 | #if MICROPY_NLR_SETJMP 30 | 31 | void nlr_jump(void *val) { 32 | nlr_buf_t **top_ptr = &MP_STATE_THREAD(nlr_top); 33 | nlr_buf_t *top = *top_ptr; 34 | if (top == NULL) { 35 | nlr_jump_fail(val); 36 | } 37 | top->ret_val = val; 38 | MP_NLR_RESTORE_PYSTACK(top); 39 | *top_ptr = top->prev; 40 | longjmp(top->jmpbuf, 1); 41 | } 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /py/objexcept.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJEXCEPT_H 27 | #define MICROPY_INCLUDED_PY_OBJEXCEPT_H 28 | 29 | #include "py/obj.h" 30 | #include "py/objtuple.h" 31 | 32 | typedef struct _mp_obj_exception_t { 33 | mp_obj_base_t base; 34 | size_t traceback_alloc : (8 * sizeof(size_t) / 2); 35 | size_t traceback_len : (8 * sizeof(size_t) / 2); 36 | size_t *traceback_data; 37 | mp_obj_tuple_t *args; 38 | } mp_obj_exception_t; 39 | 40 | void mp_obj_exception_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind); 41 | void mp_obj_exception_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); 42 | 43 | #define MP_DEFINE_EXCEPTION(exc_name, base_name) \ 44 | const mp_obj_type_t mp_type_##exc_name = { \ 45 | { &mp_type_type }, \ 46 | .name = MP_QSTR_##exc_name, \ 47 | .print = mp_obj_exception_print, \ 48 | .make_new = mp_obj_exception_make_new, \ 49 | .attr = mp_obj_exception_attr, \ 50 | .parent = &mp_type_##base_name, \ 51 | }; 52 | 53 | #endif // MICROPY_INCLUDED_PY_OBJEXCEPT_H 54 | -------------------------------------------------------------------------------- /py/objfun.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJFUN_H 27 | #define MICROPY_INCLUDED_PY_OBJFUN_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_fun_bc_t { 32 | mp_obj_base_t base; 33 | mp_obj_dict_t *globals; // the context within which this function was defined 34 | const byte *bytecode; // bytecode for the function 35 | const mp_uint_t *const_table; // constant table 36 | #if MICROPY_PY_SYS_SETTRACE 37 | const struct _mp_raw_code_t *rc; 38 | #endif 39 | // the following extra_args array is allocated space to take (in order): 40 | // - values of positional default args (if any) 41 | // - a single slot for default kw args dict (if it has them) 42 | // - a single slot for var args tuple (if it takes them) 43 | // - a single slot for kw args dict (if it takes them) 44 | mp_obj_t extra_args[]; 45 | } mp_obj_fun_bc_t; 46 | 47 | void mp_obj_fun_bc_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest); 48 | 49 | #endif // MICROPY_INCLUDED_PY_OBJFUN_H 50 | -------------------------------------------------------------------------------- /py/objgenerator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJGENERATOR_H 27 | #define MICROPY_INCLUDED_PY_OBJGENERATOR_H 28 | 29 | #include "py/obj.h" 30 | #include "py/runtime.h" 31 | 32 | mp_vm_return_kind_t mp_obj_gen_resume(mp_obj_t self_in, mp_obj_t send_val, mp_obj_t throw_val, mp_obj_t *ret_val); 33 | 34 | #endif // MICROPY_INCLUDED_PY_OBJGENERATOR_H 35 | -------------------------------------------------------------------------------- /py/objlist.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJLIST_H 27 | #define MICROPY_INCLUDED_PY_OBJLIST_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_list_t { 32 | mp_obj_base_t base; 33 | size_t alloc; 34 | size_t len; 35 | mp_obj_t *items; 36 | } mp_obj_list_t; 37 | 38 | void mp_obj_list_init(mp_obj_list_t *o, size_t n); 39 | 40 | #endif // MICROPY_INCLUDED_PY_OBJLIST_H 41 | -------------------------------------------------------------------------------- /py/objmodule.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2019 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJMODULE_H 27 | #define MICROPY_INCLUDED_PY_OBJMODULE_H 28 | 29 | #include "py/obj.h" 30 | 31 | extern const mp_map_t mp_builtin_module_map; 32 | 33 | mp_obj_t mp_module_get(qstr module_name); 34 | void mp_module_register(qstr qstr, mp_obj_t module); 35 | 36 | mp_obj_t mp_module_search_umodule(const char *module_str); 37 | 38 | #if MICROPY_MODULE_BUILTIN_INIT 39 | void mp_module_call_init(qstr module_name, mp_obj_t module_obj); 40 | #else 41 | static inline void mp_module_call_init(qstr module_name, mp_obj_t module_obj) { 42 | (void)module_name; 43 | (void)module_obj; 44 | } 45 | #endif 46 | 47 | #endif // MICROPY_INCLUDED_PY_OBJMODULE_H 48 | -------------------------------------------------------------------------------- /py/objnamedtuple.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014-2017 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H 27 | #define MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H 28 | 29 | #include "py/objtuple.h" 30 | 31 | typedef struct _mp_obj_namedtuple_type_t { 32 | mp_obj_type_t base; 33 | size_t n_fields; 34 | qstr fields[]; 35 | } mp_obj_namedtuple_type_t; 36 | 37 | typedef struct _mp_obj_namedtuple_t { 38 | mp_obj_tuple_t tuple; 39 | } mp_obj_namedtuple_t; 40 | 41 | size_t mp_obj_namedtuple_find_field(const mp_obj_namedtuple_type_t *type, qstr name); 42 | mp_obj_namedtuple_type_t *mp_obj_new_namedtuple_base(size_t n_fields, mp_obj_t *fields); 43 | 44 | #endif // MICROPY_INCLUDED_PY_OBJNAMEDTUPLE_H 45 | -------------------------------------------------------------------------------- /py/objnone.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "py/obj.h" 30 | 31 | #if !MICROPY_OBJ_IMMEDIATE_OBJS 32 | typedef struct _mp_obj_none_t { 33 | mp_obj_base_t base; 34 | } mp_obj_none_t; 35 | #endif 36 | 37 | STATIC void none_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { 38 | (void)self_in; 39 | if (MICROPY_PY_UJSON && kind == PRINT_JSON) { 40 | mp_print_str(print, "null"); 41 | } else { 42 | mp_print_str(print, "None"); 43 | } 44 | } 45 | 46 | const mp_obj_type_t mp_type_NoneType = { 47 | { &mp_type_type }, 48 | .name = MP_QSTR_NoneType, 49 | .print = none_print, 50 | .unary_op = mp_generic_unary_op, 51 | }; 52 | 53 | #if !MICROPY_OBJ_IMMEDIATE_OBJS 54 | const mp_obj_none_t mp_const_none_obj = {{&mp_type_NoneType}}; 55 | #endif 56 | -------------------------------------------------------------------------------- /py/objpolyiter.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "py/runtime.h" 30 | 31 | // This is universal iterator type which calls "iternext" method stored in 32 | // particular object instance. (So, each instance of this time can have its 33 | // own iteration behavior.) Having this type saves to define type objects 34 | // for various internal iterator objects. 35 | 36 | // Any instance should have these 2 fields at the beginning 37 | typedef struct _mp_obj_polymorph_iter_t { 38 | mp_obj_base_t base; 39 | mp_fun_1_t iternext; 40 | } mp_obj_polymorph_iter_t; 41 | 42 | STATIC mp_obj_t polymorph_it_iternext(mp_obj_t self_in) { 43 | mp_obj_polymorph_iter_t *self = MP_OBJ_TO_PTR(self_in); 44 | // Redirect call to object instance's iternext method 45 | return self->iternext(self_in); 46 | } 47 | 48 | const mp_obj_type_t mp_type_polymorph_iter = { 49 | { &mp_type_type }, 50 | .name = MP_QSTR_iterator, 51 | .getiter = mp_identity_getiter, 52 | .iternext = polymorph_it_iternext, 53 | }; 54 | -------------------------------------------------------------------------------- /py/objsingleton.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | #include 29 | 30 | #include "py/obj.h" 31 | 32 | /******************************************************************************/ 33 | /* singleton objects defined by Python */ 34 | 35 | typedef struct _mp_obj_singleton_t { 36 | mp_obj_base_t base; 37 | qstr name; 38 | } mp_obj_singleton_t; 39 | 40 | STATIC void singleton_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { 41 | (void)kind; 42 | mp_obj_singleton_t *self = MP_OBJ_TO_PTR(self_in); 43 | mp_printf(print, "%q", self->name); 44 | } 45 | 46 | const mp_obj_type_t mp_type_singleton = { 47 | { &mp_type_type }, 48 | .name = MP_QSTR_, 49 | .print = singleton_print, 50 | .unary_op = mp_generic_unary_op, 51 | }; 52 | 53 | const mp_obj_singleton_t mp_const_ellipsis_obj = {{&mp_type_singleton}, MP_QSTR_Ellipsis}; 54 | #if MICROPY_PY_BUILTINS_NOTIMPLEMENTED 55 | const mp_obj_singleton_t mp_const_notimplemented_obj = {{&mp_type_singleton}, MP_QSTR_NotImplemented}; 56 | #endif 57 | -------------------------------------------------------------------------------- /py/objstringio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_OBJSTRINGIO_H 27 | #define MICROPY_INCLUDED_PY_OBJSTRINGIO_H 28 | 29 | #include "py/obj.h" 30 | 31 | typedef struct _mp_obj_stringio_t { 32 | mp_obj_base_t base; 33 | vstr_t *vstr; 34 | // StringIO has single pointer used for both reading and writing 35 | mp_uint_t pos; 36 | // Underlying object buffered by this StringIO 37 | mp_obj_t ref_obj; 38 | } mp_obj_stringio_t; 39 | 40 | #endif // MICROPY_INCLUDED_PY_OBJSTRINGIO_H 41 | -------------------------------------------------------------------------------- /py/opmethods.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/obj.h" 28 | #include "py/builtin.h" 29 | 30 | STATIC mp_obj_t op_getitem(mp_obj_t self_in, mp_obj_t key_in) { 31 | const mp_obj_type_t *type = mp_obj_get_type(self_in); 32 | return type->subscr(self_in, key_in, MP_OBJ_SENTINEL); 33 | } 34 | MP_DEFINE_CONST_FUN_OBJ_2(mp_op_getitem_obj, op_getitem); 35 | 36 | STATIC mp_obj_t op_setitem(mp_obj_t self_in, mp_obj_t key_in, mp_obj_t value_in) { 37 | const mp_obj_type_t *type = mp_obj_get_type(self_in); 38 | return type->subscr(self_in, key_in, value_in); 39 | } 40 | MP_DEFINE_CONST_FUN_OBJ_3(mp_op_setitem_obj, op_setitem); 41 | 42 | STATIC mp_obj_t op_delitem(mp_obj_t self_in, mp_obj_t key_in) { 43 | const mp_obj_type_t *type = mp_obj_get_type(self_in); 44 | return type->subscr(self_in, key_in, MP_OBJ_NULL); 45 | } 46 | MP_DEFINE_CONST_FUN_OBJ_2(mp_op_delitem_obj, op_delitem); 47 | 48 | STATIC mp_obj_t op_contains(mp_obj_t lhs_in, mp_obj_t rhs_in) { 49 | const mp_obj_type_t *type = mp_obj_get_type(lhs_in); 50 | return type->binary_op(MP_BINARY_OP_CONTAINS, lhs_in, rhs_in); 51 | } 52 | MP_DEFINE_CONST_FUN_OBJ_2(mp_op_contains_obj, op_contains); 53 | -------------------------------------------------------------------------------- /py/parsenum.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_PARSENUM_H 27 | #define MICROPY_INCLUDED_PY_PARSENUM_H 28 | 29 | #include "py/mpconfig.h" 30 | #include "py/lexer.h" 31 | #include "py/obj.h" 32 | 33 | // these functions raise a SyntaxError if lex!=NULL, else a ValueError 34 | mp_obj_t mp_parse_num_integer(const char *restrict str, size_t len, int base, mp_lexer_t *lex); 35 | mp_obj_t mp_parse_num_decimal(const char *str, size_t len, bool allow_imag, bool force_complex, mp_lexer_t *lex); 36 | 37 | #endif // MICROPY_INCLUDED_PY_PARSENUM_H 38 | -------------------------------------------------------------------------------- /py/parsenumbase.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_PARSENUMBASE_H 27 | #define MICROPY_INCLUDED_PY_PARSENUMBASE_H 28 | 29 | #include "py/mpconfig.h" 30 | 31 | size_t mp_parse_num_base(const char *str, size_t len, int *base); 32 | 33 | #endif // MICROPY_INCLUDED_PY_PARSENUMBASE_H 34 | -------------------------------------------------------------------------------- /py/pystack.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2017 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include 28 | 29 | #include "py/runtime.h" 30 | 31 | #if MICROPY_ENABLE_PYSTACK 32 | 33 | void mp_pystack_init(void *start, void *end) { 34 | MP_STATE_THREAD(pystack_start) = start; 35 | MP_STATE_THREAD(pystack_end) = end; 36 | MP_STATE_THREAD(pystack_cur) = start; 37 | } 38 | 39 | void *mp_pystack_alloc(size_t n_bytes) { 40 | n_bytes = (n_bytes + (MICROPY_PYSTACK_ALIGN - 1)) & ~(MICROPY_PYSTACK_ALIGN - 1); 41 | #if MP_PYSTACK_DEBUG 42 | n_bytes += MICROPY_PYSTACK_ALIGN; 43 | #endif 44 | if (MP_STATE_THREAD(pystack_cur) + n_bytes > MP_STATE_THREAD(pystack_end)) { 45 | // out of memory in the pystack 46 | nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError, 47 | MP_OBJ_NEW_QSTR(MP_QSTR_pystack_space_exhausted))); 48 | } 49 | void *ptr = MP_STATE_THREAD(pystack_cur); 50 | MP_STATE_THREAD(pystack_cur) += n_bytes; 51 | #if MP_PYSTACK_DEBUG 52 | *(size_t *)(MP_STATE_THREAD(pystack_cur) - MICROPY_PYSTACK_ALIGN) = n_bytes; 53 | #endif 54 | return ptr; 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /py/qstrdefs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | // *FORMAT-OFF* 28 | 29 | #include "py/mpconfig.h" 30 | 31 | // All the qstr definitions in this file are available as constants. 32 | // That is, they are in ROM and you can reference them simply as MP_QSTR_xxxx. 33 | 34 | // qstr configuration passed to makeqstrdata.py of the form QCFG(key, value) 35 | QCFG(BYTES_IN_LEN, MICROPY_QSTR_BYTES_IN_LEN) 36 | QCFG(BYTES_IN_HASH, MICROPY_QSTR_BYTES_IN_HASH) 37 | 38 | Q() 39 | Q(*) 40 | Q(_) 41 | Q(/) 42 | #if MICROPY_PY_BUILTINS_STR_OP_MODULO 43 | Q(%#o) 44 | Q(%#x) 45 | #else 46 | Q({:#o}) 47 | Q({:#x}) 48 | #endif 49 | Q({:#b}) 50 | Q( ) 51 | Q(\n) 52 | Q(maximum recursion depth exceeded) 53 | Q() 54 | Q() 55 | Q() 56 | Q() 57 | Q() 58 | Q() 59 | Q() 60 | Q() 61 | Q(utf-8) 62 | 63 | #if MICROPY_ENABLE_PYSTACK 64 | Q(pystack exhausted) 65 | #endif 66 | -------------------------------------------------------------------------------- /py/reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013-2016 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_READER_H 27 | #define MICROPY_INCLUDED_PY_READER_H 28 | 29 | #include "py/obj.h" 30 | 31 | // the readbyte function must return the next byte in the input stream 32 | // it must return MP_READER_EOF if end of stream 33 | // it can be called again after returning MP_READER_EOF, and in that case must return MP_READER_EOF 34 | #define MP_READER_EOF ((mp_uint_t)(-1)) 35 | 36 | typedef struct _mp_reader_t { 37 | void *data; 38 | mp_uint_t (*readbyte)(void *data); 39 | void (*close)(void *data); 40 | } mp_reader_t; 41 | 42 | void mp_reader_new_mem(mp_reader_t *reader, const byte *buf, size_t len, size_t free_len); 43 | void mp_reader_new_file(mp_reader_t *reader, const char *filename); 44 | void mp_reader_new_file_from_fd(mp_reader_t *reader, int fd, bool close_fd); 45 | 46 | #endif // MICROPY_INCLUDED_PY_READER_H 47 | -------------------------------------------------------------------------------- /py/repl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2013, 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_REPL_H 27 | #define MICROPY_INCLUDED_PY_REPL_H 28 | 29 | #include "py/mpconfig.h" 30 | #include "py/misc.h" 31 | #include "py/mpprint.h" 32 | 33 | #if MICROPY_HELPER_REPL 34 | bool mp_repl_continue_with_input(const char *input); 35 | size_t mp_repl_autocomplete(const char *str, size_t len, const mp_print_t *print, const char **compl_str); 36 | #endif 37 | 38 | #endif // MICROPY_INCLUDED_PY_REPL_H 39 | -------------------------------------------------------------------------------- /py/ringbuf.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2019 Jim Mussared 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #include "ringbuf.h" 27 | 28 | int ringbuf_get16(ringbuf_t *r) { 29 | int v = ringbuf_peek16(r); 30 | if (v == -1) { 31 | return v; 32 | } 33 | r->iget += 2; 34 | if (r->iget >= r->size) { 35 | r->iget -= r->size; 36 | } 37 | return v; 38 | } 39 | 40 | int ringbuf_peek16(ringbuf_t *r) { 41 | if (r->iget == r->iput) { 42 | return -1; 43 | } 44 | uint32_t iget_a = r->iget + 1; 45 | if (iget_a == r->size) { 46 | iget_a = 0; 47 | } 48 | if (iget_a == r->iput) { 49 | return -1; 50 | } 51 | return (r->buf[r->iget] << 8) | (r->buf[iget_a]); 52 | } 53 | 54 | int ringbuf_put16(ringbuf_t *r, uint16_t v) { 55 | uint32_t iput_a = r->iput + 1; 56 | if (iput_a == r->size) { 57 | iput_a = 0; 58 | } 59 | if (iput_a == r->iget) { 60 | return -1; 61 | } 62 | uint32_t iput_b = iput_a + 1; 63 | if (iput_b == r->size) { 64 | iput_b = 0; 65 | } 66 | if (iput_b == r->iget) { 67 | return -1; 68 | } 69 | r->buf[r->iput] = (v >> 8) & 0xff; 70 | r->buf[iput_a] = v & 0xff; 71 | r->iput = iput_b; 72 | return 0; 73 | } 74 | -------------------------------------------------------------------------------- /py/runtime_utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2015 Josef Gajdusek 7 | * Copyright (c) 2015 Paul Sokolovsky 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/runtime.h" 29 | 30 | mp_obj_t mp_call_function_1_protected(mp_obj_t fun, mp_obj_t arg) { 31 | nlr_buf_t nlr; 32 | if (nlr_push(&nlr) == 0) { 33 | mp_obj_t ret = mp_call_function_1(fun, arg); 34 | nlr_pop(); 35 | return ret; 36 | } else { 37 | mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); 38 | return MP_OBJ_NULL; 39 | } 40 | } 41 | 42 | mp_obj_t mp_call_function_2_protected(mp_obj_t fun, mp_obj_t arg1, mp_obj_t arg2) { 43 | nlr_buf_t nlr; 44 | if (nlr_push(&nlr) == 0) { 45 | mp_obj_t ret = mp_call_function_2(fun, arg1, arg2); 46 | nlr_pop(); 47 | return ret; 48 | } else { 49 | mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val)); 50 | return MP_OBJ_NULL; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /py/stackctrl.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | 27 | #include "py/runtime.h" 28 | #include "py/stackctrl.h" 29 | 30 | void mp_stack_ctrl_init(void) { 31 | volatile int stack_dummy; 32 | MP_STATE_THREAD(stack_top) = (char *)&stack_dummy; 33 | } 34 | 35 | void mp_stack_set_top(void *top) { 36 | MP_STATE_THREAD(stack_top) = top; 37 | } 38 | 39 | mp_uint_t mp_stack_usage(void) { 40 | // Assumes descending stack 41 | volatile int stack_dummy; 42 | return MP_STATE_THREAD(stack_top) - (char *)&stack_dummy; 43 | } 44 | 45 | #if MICROPY_STACK_CHECK 46 | 47 | void mp_stack_set_limit(mp_uint_t limit) { 48 | MP_STATE_THREAD(stack_limit) = limit; 49 | } 50 | 51 | void mp_stack_check(void) { 52 | if (mp_stack_usage() >= MP_STATE_THREAD(stack_limit)) { 53 | mp_raise_recursion_depth(); 54 | } 55 | } 56 | 57 | #endif // MICROPY_STACK_CHECK 58 | -------------------------------------------------------------------------------- /py/stackctrl.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Paul Sokolovsky 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_STACKCTRL_H 27 | #define MICROPY_INCLUDED_PY_STACKCTRL_H 28 | 29 | #include "py/mpconfig.h" 30 | 31 | void mp_stack_ctrl_init(void); 32 | void mp_stack_set_top(void *top); 33 | mp_uint_t mp_stack_usage(void); 34 | 35 | #if MICROPY_STACK_CHECK 36 | 37 | void mp_stack_set_limit(mp_uint_t limit); 38 | void mp_stack_check(void); 39 | #define MP_STACK_CHECK() mp_stack_check() 40 | 41 | #else 42 | 43 | #define mp_stack_set_limit(limit) 44 | #define MP_STACK_CHECK() 45 | 46 | #endif 47 | 48 | #endif // MICROPY_INCLUDED_PY_STACKCTRL_H 49 | -------------------------------------------------------------------------------- /py/unicode.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | */ 26 | #ifndef MICROPY_INCLUDED_PY_UNICODE_H 27 | #define MICROPY_INCLUDED_PY_UNICODE_H 28 | 29 | #include "py/mpconfig.h" 30 | #include "py/misc.h" 31 | 32 | mp_uint_t utf8_ptr_to_index(const byte *s, const byte *ptr); 33 | bool utf8_check(const byte *p, size_t len); 34 | 35 | #endif // MICROPY_INCLUDED_PY_UNICODE_H 36 | -------------------------------------------------------------------------------- /py/warning.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of the MicroPython project, http://micropython.org/ 3 | * 4 | * The MIT License (MIT) 5 | * 6 | * Copyright (c) 2014 Damien P. George 7 | * Copyright (c) 2015-2018 Paul Sokolovsky 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 29 | #include 30 | 31 | #include "py/emit.h" 32 | #include "py/runtime.h" 33 | 34 | #if MICROPY_WARNINGS 35 | 36 | void mp_warning(const char *category, const char *msg, ...) { 37 | if (category == NULL) { 38 | category = "Warning"; 39 | } 40 | mp_print_str(MICROPY_ERROR_PRINTER, category); 41 | mp_print_str(MICROPY_ERROR_PRINTER, ": "); 42 | 43 | va_list args; 44 | va_start(args, msg); 45 | mp_vprintf(MICROPY_ERROR_PRINTER, msg, args); 46 | mp_print_str(MICROPY_ERROR_PRINTER, "\n"); 47 | va_end(args); 48 | } 49 | 50 | void mp_emitter_warning(pass_kind_t pass, const char *msg) { 51 | if (pass == MP_PASS_CODE_SIZE) { 52 | mp_warning(NULL, msg); 53 | } 54 | } 55 | 56 | #endif // MICROPY_WARNINGS 57 | -------------------------------------------------------------------------------- /tools/mpy-cross/hellortt.mpy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/tools/mpy-cross/hellortt.mpy -------------------------------------------------------------------------------- /tools/mpy-cross/hellortt.py: -------------------------------------------------------------------------------- 1 | # import os 2 | class HelloRtt: 3 | 4 | def __repr__(self): 5 | self.__call__() 6 | return "" 7 | 8 | def __call__(self): 9 | print("hello world!!") 10 | print("hello RTT") 11 | 12 | hello = HelloRtt() 13 | -------------------------------------------------------------------------------- /tools/mpy-cross/mpy-cross.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/micropython/dc05133d476156e573179375304526170e285d83/tools/mpy-cross/mpy-cross.exe --------------------------------------------------------------------------------