既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新
可知 micropython 是实现了 Python3.x 以上的 Python 语法解释器,接着看到。
MicroPython implements the entire Python 3.4 syntax (including exceptions,
with, yield from, etc., and additionally async/await keywords from
Python 3.5). The following core datatypes are provided: str (including
basic Unicode support), bytes, bytearray, tuple, list, dict, set,
frozenset, array.array, collections.namedtuple, classes and instances.
Builtin modules include sys, time, and struct, etc. Select ports have
support for _thread module (multithreading). Note that only a subset of
Python 3 functionality is implemented for the data types and modules.
可以看到实现了不少关键词(keywords ),注意 Python3 最大的变化在于 str 和 byte 彻底分离,而 MicroPython 也为此做了很多修改,符合预期。
然后协程相关的语法糖(yield、async、await)都支持了,with 和 in 也有,再来基本的标准库 sys 与 time、struct 、math 等。
最最重要的是,大量现成的基础容器(str、bytes、bytearray、tuple、list、dict、set、btree),编写软件的福利鸭!
最后就是根据芯片的 RTOS 情况提供的 _thread 多线程库,功能上略有缺失,因为多线程的设计和 X86 的有些区别。
but,这些特性并非所有芯片都支持,有时候,要依据不同的芯片情况,对此做裁剪处理,类似制作 linux kernel 一样,选择性的放入一些硬件相关的关键 C 驱动代码。
现在我们接着看很关键的仓库目录说明,可能有些过时,但这并不影响我们对它的认识。
Major components in this repository:
- py/ -- the core Python implementation, including compiler, runtime, and core library.
- mpy-cross/ -- the MicroPython cross-compiler which is used to turn scripts into precompiled bytecode.
- ports/unix/ -- a version of MicroPython that runs on Unix.
- ports/stm32/ -- a version of MicroPython that runs on the PyBoard and similar STM32 boards (using ST's Cube HAL drivers).
- ports/minimal/ -- a minimal MicroPython port. Start with this if you want to port MicroPython to another microcontroller.
- tests/ -- test framework and test scripts.
- docs/ -- user documentation in Sphinx reStructuredText format. Rendered HTML documentation is available at http://docs.micropython.org.
Additional components:
- ports/bare-arm/ -- a bare minimum version of MicroPython for ARM MCUs. Used mostly to control code size.
- ports/teensy/ -- a version of MicroPython that runs on the Teensy 3.1 (preliminary but functional).
- ports/pic16bit/ -- a version of MicroPython for 16-bit PIC microcontrollers.
- ports/cc3200/ -- a version of MicroPython that runs on the CC3200 from TI.
- ports/esp8266/ -- a version of MicroPython that runs on Espressif's ESP8266 SoC.
- ports/esp32/ -- a version of MicroPython that runs on Espressif's ESP32 SoC.
- ports/nrf/ -- a version of MicroPython that runs on Nordic's nRF51 and nRF52 MCUs.
- extmod/ -- additional (non-core) modules implemented in C.
- tools/ -- various tools, including the pyboard.py module.
- examples/ -- a few example Python scripts.
可以看到这些是官方做了一些架构上的说明,虽然有点乱,但也是可以窥探一二的。
只是为了编译使用 MicroPython 的同学,可以直接看到 ports 文件夹,这里存放着官方移植的一些对应硬件的编译固件配置,进入到其中选择你想要编译的平台,即可得到一个 MicroPython 解释器,开始你的 Python 编程。
如果想知道更多,我继续往下讲。
想知道更多?
文件夹列表大致内容如下
- py Python 解释器相关的抽象实现的代码,包含运行时等等。
- mpy-cross MicroPython 编译器,处理 Python 代码回 ByteCode 机器码。
- ports 对应平台移植配置文件
- tests 框架测试脚本
- docs 配置到 Sphinx 的文档网站
- extmod 一些不需要在 Core 中的抽象 C 接口代码。
- tools 各类脚本辅助工具,例如 Pyboard.py 可以通信控制 MicroPython 。
- examples Python 示例代码。
- lib 给 port 用的各自平台的 SDK 依赖库,可能在里面,也可能在外面,并不重要。
- drivers 通过软实现的硬件驱动,基于 py 的架构使用标准 C 实现的 Python 模块(C + Python),和芯片自己提供的 SDK 略微不同,有较大的兼容性。
对于我们做移植或做深层次的开发有两个方向,假设不破坏官方的架构的基础上,如何结合到现在自己手头的硬件或者软件。
MicroPython 解释器核心的基本认识
我们需要对 MicroPython 的解释器有一个大概的认识,这就需要初略阅读一下 py 的结构了,关键的文件在 compile(python) 、 obj(alloc) 、 nlr (non-local return) 、vm(execute) 等地,总之全都很重要吧。
因此一个解释器的构成情况常有有代码执行环境(堆栈)、代码交互接口(REPL)、变量存取接口(allocator)、代码编译接口(compile)等。
就我们通常的使用方法来讲解吧。
一段 Python 代码,通过 REPL 进入解释器的缓存中,解释器程序将会对这段代码进行解释(编译成 ByteCode),接着通过解释后的 code 进行执行,执行时送到解释器的函数栈上,该执行的就执行,该报异常的报异常。
要做到这样的效果,我们必须深入往下看才能得知具体的细节,关于 py 的部分我就暂且讲到这里,之后也会单独解释一些 py 的源码解释,专门讲解它的构成和 debug 或是修改源码(bug)。
MicroPython 的测试 & 示例
如果我们没有能力去修改 MicroPython ,那就要了解它的外围代码和测试,分别在 example 和 tests,这里有许多测试示例代码供你应用和查阅。
如看 run-tests-exp.sh 和 run-tests-exp.py 文件,又或是 run-tests ,这些都是自动化的测试接口脚本,通过这样的方式可以让 micropython 解释器进行接口覆盖性测试。
自动化生成的示例如下:(均可在 Windows 或 Unix 下运行喔)
如图所见,将在 repl 执行的 Python code 结果返回存到 .exp (example)下进行测试核对。
了解到它的测试框架后,现在是不是对 MicroPython 稍微有了一些信心呢?接着我们还要看到真正开始使用的部分了。
MicroPython 中各芯片标准移植接口
这里就存放着 MicroPython 应用领域中最有价值的代码,也就是官方提出的专用的移植接口示范,对于一般的开发者来说,足够使用了,但对于专业的来说,不仅是使用它,包括如何移植它,改造它,修复它等等都是要了解的。
先看一点简单的移植示例吧 windows 、unix 、还有 minimal 文件夹。
比如我现在在 Windows 上,使用 VS2019 直接打开 windows 的 micropython.vcxproj 文件即可编译生成一个 micropython.exe 解释器了,如下图。
而 unix 则对应着 linux 或 macOS 环境下直接运行即可,我这里不想多示范了,没有什么区别。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上物联网嵌入式知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、电子书籍、讲解视频,并且后续会持续更新