diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/README.md b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/README.md deleted file mode 100644 index a4fed7df..00000000 --- a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/README.md +++ /dev/null @@ -1,379 +0,0 @@ -# 如何使用 TencentOS-tiny 上移植的 MicroPython 组件 - -## 1 基础Keil工程 - -### 1.1 源代码 - -* micropython/py 组中添加 - * components/language/micropython/py 目录下的 *.c 文件 - -* micropython/shared 组中添加: - * components/language/micropython/shared/readline 目录下的 readline.c 文件 - * components/language/micropython/shared/runtime 目录下的 pyexec.c, stdout_helpers.c, sys_stdio_mphal.c, interrupt_char.c 文件 - -* micropython/extmod 组中添加: - * components/language/micropython/extmod 目录下的 utime_mphal.c, machine_signal.c, virtpin.c, machine_mem.c 文件 - -* micropython/port 组中添加: - * components/language/micropython/port 目录下的 *.c 文件 - * components/language/micropython/port/modules 目录下的 *.c 文件 - * 工程目录中 board/ 目录下的 mphalboard.c 文件 - -* hal 组中添加: - * 工程目录下 board/hal/ 目录下的 *.c 文件 - -* examples 组中添加: - * examples/micropython_demo/micropython_demo.c文件 - -### 1.2 头文件 - -在工程配置界面 C/C++ 页面中的 Include Paths 中添加下列目录: - -![include_paths](./imgs/include_paths.png) - -* 添加 examples/micropython_demo -* 添加 components/language/micropython -* 添加 components/language/micropython/port -* 添加 components/language/micropython/port/modules -* 添加工程目录 -* 添加工程目录下的 board 目录 -* 添加工程目录下的 board/hal 目录 - -### 1.3 其他选项 - -在工程配置界面的 C/C++ 页面下勾选 GNU extensions 选项,并在 Define 中添加 `USE_MICROPYTHON`。 - -![other_configuration](./imgs/other_configuration.png) - -### 1.4 编译脚本 - -MicroPython 采用了字符串驻留(String interning)技术节省 ROM 和 RAM 空间,在工程编译前需要先生成内部字符串 QSTR(uniQue STRing)。本工程中采用 Makefile 搜索代码中所有需要预先生成的 QSTR,然后在工程目录下的 genhdr 目录中生成 qstrdefs.generated.h 等文件。关于QSTR的更详细信息,可以在[官方文档](https://docs.micropython.org/en/latest/develop/qstr.html)中获取。 - -在工程配置界面 User 页面下的 Before Build/Rebuild 中添加以下两个命令: - -```shell -cmd.exe /C cd $P..\..\..\..\components\language\micropython\port & make clean-build V=1 -``` - -```shell -cmd.exe /C cd $P..\..\..\..\components\language\micropython\port & make genhdr V=1 PYTHON=python PORT_BOARD_DIR=$Pboard TARGET_GENHDR_DIR=$Pgenhdr -``` - -![before_build_command](./imgs/before_build_command.png) - -该命令的作用是生成工程目录下的 genhdr 目录中的头文件,这些头文件通常只有在修改配置之后才需要重新生成,因此**在不修改配置的情况下,不需要勾选这两个命令**。 - -> 使用前请确保安装了make等unix工具,gcc工具链以及Python3,并将它们加入到系统环境变量中。如果没有安装,gcc工具链和unix命令行工具可以在MinGW中获取。访问[MinGW官方网站](https://www.mingw-w64.org/)和[Python官方网站](https://www.python.org/)分别可以获取MinGW和Python的相关信息。 - -第二条命令中指定了多个变量的值,这些变量的含义分别是: - -* **PYTHON**:表示命令行可以使用的python命令,根据当前环境中的python命令修改 -* PORT_BOARD_DIR:表示与开发板相关的代码目录,该目录下至少需要包含mpconfigboard.h 文件和 mphalboard.h 文件 -* TARGET_GENHDR_DIR:表示生成的头文件所在的代码目录 - -### 1.5 外部Flash扩展 - -> 该功能目前只能在小熊派开发板上使用 - -MicroPython对ROM的需求量较大,上述基础Keil工程编译后需要占用239.70kB的ROM空间,对于包括ROM空间较少的开发板(比如:小熊派开发板)不太友好,为了能够在这样的开发板上运行更加完整的MicroPython功能,可以利用开发板的外部Flash对ROM空间进行扩展。 - -1. micropython_extra_demo 工程目录下包含了小熊派开发板的Keil外部Flash下载算法文件 BearPi_W25Q64JV.FLM,将该文件拷贝到 **/ARM/Flash** 目录下。 - -2. 在Keil工程中配置外部 ROM,并选择 Flash 下载算法 - - * 在工程配置的 Target 页面中,添加一个 ROM 区域,起始地址为 **0x90000000**,区域大小为 **0x800000**(ROM区域大小取决于外部 Flash 芯片容量) - - ![external_flash_1](./imgs/external_flash_1.png) - - * 在 Debug 页面中,点击右上角的 Settings,选择 Flash Download 选项卡,进入下图界面 - - ![external_flash_2](./imgs/external_flash_2.png) - - * 添加描述为**“BearPi W25Q84JV 8MB Flash”**的 Flash 下载算法,并将右上角的 Size 调整到**0x8000**。 - -3. 将特定文件或者组的存储位置改到外部 ROM 中。 - - ![external_flash_3](./imgs/external_flash_3.png) - -4. 添加 W25Q64JV 的驱动代码,启用内存映射模式。(在 MicroPython 的工程中,只需要启用第二节 “[功能配置和扩展](#2-功能配置和扩展)” 中的`MP_USING_QSPI_FLASH`宏并添加相应文件即可) - -> 中断相关的代码以及内存映射模式启动前就需要执行的代码,不能放在外部 Flash 中。推荐将 MicroPython 相关的代码(除了移植的代码)都放在外部 Flash 中。 - -## 2 功能配置和扩展 - -与开发板相关的配置文件主要包括 mpconfigboard.h 和 mphalboard.h/c,其中 mpconfigboard.h 包含对 MicroPython 的配置,mphalboard.h/c 包含开发板特有的 MicroPython 函数的部分功能实现。 - -mpconfigboard.h 主要由宏定义构成,可以使用的定义以及对应的含义如下表: - -| 名称 | 含义 | 取值范围 | 说明 | -| ------------------------ | ------------------------------------------- | ----------------------------------------------------- | ------------------------------------------------------------ | -| MICROPY_HW_BOARD_NAME | 开发板名称 | 字符串 | | -| MICROPY_HW_MCU_NAME | 微控制器名称 | 字符串 | | -| MICROPY_HW_UART_NUM | UART数量 | 整数 | 需要与 tos_hal 的实现一致 | -| MICROPY_HW_UART_REPL | 用于 MicroPython 交互式命令行的 UART 的编号 | 整数 | 需要与 tos_hal 的实现一致 | -| MICROPY_HW_SPI_NUM | SPI 数量 | 整数 | 需要与 tos_hal 的实现一致 | -| MICROPY_CONFIG_ROM_LEVEL | ROM 水平 | MICROPY_CONFIG_ROM_LEVEL
\_\_FEATURES | 代表 MicroPython 的功能裁剪程度,这里只列出两个典型值,其他取值需要参考 py/mpconfig.h 文件 | - -除了以上配置之外,还有一些可选的功能配置,采用定义`MP_USING_xxx`宏的形式进行功能扩展: - -| 宏定义 | 描述 | 依赖的文件/组件 | -| ------------------------------------------ | --------------------------------- | ------------------------------------------------------------ | -| MP_USING_QSPI_FLASH | 使用外部QSPI-Flash扩展内部ROM空间 | 开发板目录/BSP/Hardware/W25Qxx-QSPI/w25qxx.c,详见第一节的外部Flash扩展部分 | -| MP_USING_VFS | 启用uos模块,启用VFS功能 | tos_vfs组件,
extmod/vfs.c,
extmod/vfs_reader.c,
extmod/moduos.c | -| MP_USING_MACHINE_SPI | 在umachine库中启用SPI功能 | extmod/machine_spi.c,
drivers/bus/softspi.c, | -| MP_USING_MACHINE_I2C | 在umachine库中启用I2C功能 | extmod/machine_i2c.c | -| MP_USING_NETWORK | 启用network模块 | socket_wrapper组件,
sal_wrapper组件,
extmod/modnetwork.c,
extmod/modusocket.c,
shared/netutils/netutils.c | -| MP_USING_USELECT | 启用uselect模块 | extmod/moduselect.c | -| 以下模块在ROM_LEVEL为EXTRA的时候会默认启用 | ----- | ----- | -| MP_USING_CMATH | 启用cmath模块 | - | -| MP_USING_UASYNCIO | 启用uasyncio模块 | extmod/moduasyncio.c | -| MP_USING_UCTYPES | 启用uctypes模块 | extmod/moductypes.c | -| MP_USING_UZLIBS | 启用uzlibs模块 | extmod/moduzlibs.c | -| MP_USING_UJSON | 启用ujson模块 | extmod/modujson.c | -| MP_USING_URE | 启用ure模块 | extmod/modure.c | -| MP_USING_HEAPQ | 启用uheapq模块 | extmod/moduheapq.c | -| MP_USING_UHASHLIB | 启用uhashlib模块 | extmod/moduhashlib.c | -| MP_USING_UBINASCII | 启用ubinascii模块 | extmod/modubinascii.c | -| MP_USING_URANDOM | 启用urandom模块 | extmod/modurandom.c | -| MP_USING_FRAMEBUF | 启用framebuf模块 | extmod/modframebuf.c | - -## 3 使用MicroPython - -下载 MicroPython 固件后,可以使用支持串口功能的 shell 连接到 MicroPython 的交互式解释器(REPL)上,也可以使用 MicroPython 提供的 mpremote 工具连接到 REPL。REPL 所关联的串口由第二节 “[功能配置和扩展](#2-功能配置和扩展)” 中的 `MICROPY_HW_UART_REPL` 宏设定。 - -### 3.1 mpremote - -#### 3.1.1 使用 pip 安装 mpremote - -```shell -pip install mpremote -``` - -#### 3.1.2 使用 mpremote 可以在常规的 shell 上连接到串口 REPL - -```shell -mpremote connect -``` - -例如:连接COM14端口 - -```shell -mpremote connect COM14 -``` - -按下 ”Ctrl-]“ 可以退出REPL - -#### 3.1.3 使用 mpremote 还可以在开发板上执行本地的 python 脚本 - - ```shell -mpremote connect run - ``` - -例如:使用mpremote运行示例脚本led.py文件 - - ```shell -mpremote connect COM14 run ./led.py - ``` - -mpremote还包含其他功能,参考[官方文档]( https://docs.micropython.org/en/latest/reference/mpremote.html)了解详情。 - -### 3.2 REPL - -#### 3.2.1 help() 命令 - -进入 REPL 后,可以使用 help() 命令,查看与 MicroPython 控制命令相关的提示信息: - -help() - -其中 Ctrl-C 可以用来中断正在运行的程序,Ctrl-D 可以用来软复位,Ctrl-E 可以进入粘贴模式。 - -#### 3.2.2 help('modules') 命令 - -在交互式解释器中可以使用 help('modules') 命令查看当前可以引入的模块: - -help('modules') - -#### 3.2.3 粘贴模式 - -在正常交互模式下,输入 Ctrl-E 可以进入粘贴模式。 - -在粘贴模式下可以方便地将 python 脚本粘贴到 REPL 中。 - -> 由于输入缓冲区大小只有512B,所以比较长的脚本需要分段粘贴。 - -粘贴完成后,输入 Ctrl-D 可以退出粘贴模式,并开始执行脚本。 - -下图展示了使用粘贴模式粘贴 pin.py 脚本的情况: - -paste_mode_pin_py - -#### 3.2.4 其他功能 - -REPL还有自动缩进,自动补全以及特殊变量"_"等功能,详细内容可以参考[官方文档](https://docs.micropython.org/en/latest/reference/repl.html)。 - -### 3.3 自动执行脚本 - -对于启用文件系统的 MicroPython 工程,可以在文件系统根目录中存放 boot.py 和 main.py 脚本,这两个脚本会在 MicroPython 启动时依次执行。 - -## 4 示例工程和示例脚本说明 - -### 4.1 示例工程 - -项目针对小熊派开发板提供了两个不同的示例工程: - -* micropython_basic_demo 是一个基础版的 MicroPython 工程,在第一节 ”[基础Keil工程](#1-基础Keil工程)“ 中给出了比较详细的该工程的创建流程。该工程包含了 MicroPython 的REPL功能、_thread线程模块功能以及基本的 machine 模块功能。 -* micropython_extra_demo 是在 micropython_basic_demo 工程的基础上,参考第二节 “[功能配置和扩展](#2-功能配置和扩展)” 中的内容进行配置得到的扩展版的 MicroPython 工程。由于小熊派开发板的 ROM 不足以支持所有的 MicroPython 功能,该工程内使用了外部 Flash 扩展 ROM 容量。该工程在 micropython_basic_demo 工程的基础上,添加了 MicroPython 的一系列扩展模块,还添加了uos 模块以及 network 模块等移植模块。 - -### 4.2 示例脚本 - -在工程目录下的 scripts 目录中给出了三个示例脚本文件: - -* pin.py:脚本展示了 umachine 模块和 utime 模块的使用方式 - - ```python - import umachine as machine - import utime as time - - running = True - - def key1_irq_callback(pin): - if pin.value() == 0: - print('Key1 is pressed') - else: - print('Key1 is released') - - def key2_irq_callback(pin): - global running - running = False - - if __name__ == '__main__': - - led = machine.Pin("LED", mode=machine.Pin.OUT) - key1 = machine.Pin("KEY1", mode=machine.Pin.IN_PULLUP) - key1.irq(trigger=machine.Pin.IRQ_RISING|machine.Pin.IRQ_FALLING, handler=key1_irq_callback) - key2 = machine.Pin("KEY2", mode=machine.Pin.IN_PULLUP) - key2.irq(trigger=machine.Pin.IRQ_FALLING, handler=key2_irq_callback) - - while running: - led.on() - time.sleep(0.5) - led.off() - time.sleep(0.5) - ``` - - 脚本运行现象是:LED每过0.5s亮灭变化一次;按下按键1,打印一行文本 ”key1 is pressed“,释放按键1,打印一行文本 ”key1 is released“;按下按键2,LED不再闪烁,脚本退出。 - -* thread.py:脚本展示了 _thread 模块的使用方法 - - ```python - import _thread - - lock = _thread.allocate_lock() - n_thread = 4 - n_finished = 0 - - def thread_entry(no): - print(no) - with lock: - global n_finished - n_finished += 1 - - if __name__ == '__main__': - for i in range(n_thread): - _thread.start_new_thread(thread_entry, (i,)) - - while n_finished < n_thread: - pass - print("done") - ``` - - 脚本运行现象是:创建四个线程,序号分别为0~3,每个线程打印自己的序号后退出。 - -* os.py:该脚本展示了 uos 模块中的文件操作和目录操作 - - > 该脚本只有在启用 VFS 的前提下才能运行 - - ```python - import uos as os - - # test mkdir() - print('{:-^40}'.format('test mkdir')) - print(os.listdir()) - os.mkdir('test_dir') - print(os.listdir()) - - # test file open() and write() - print('{:-^40}'.format('test write')) - f = open('test_dir/test_file', 'w') - f.write("Hello TencentOS-tiny\n") - print("Hello MicroPython", file=f) - f.close() - print(os.listdir('test_dir')) - - # test file open() and read() - print('{:-^40}'.format('test read')) - f = open('test_dir/test_file', 'r') - print(f.readlines()) - f.close() - - # test 'with' statement and iterator - print('{:-^40}'.format('test with statement')) - with open('test_dir/test_file', 'r') as file: - for line in file: - print(line) - - # test rename() - print('{:-^40}'.format('test rename')) - os.rename('test_dir', 'test_dir2') - print(os.listdir()) - print(os.listdir('test_dir2')) - - # test unlink() - print('{:-^40}'.format('test unlink')) - os.unlink('test_dir2/test_file') - print(os.listdir('test_dir2')) - os.unlink('test_dir2') - print(os.listdir()) - ``` - - 脚本运行现象是:输出以下内容 - - ``` - ---------------test mkdir--------------- - ['System Volume Information'] - ['test_dir', 'System Volume Information'] - ---------------test write--------------- - ['test_file'] - ---------------test read---------------- - ['Hello TencentOS-tiny\n', 'Hello MicroPython\n'] - ----------test with statement----------- - Hello TencentOS-tiny - - Hello MicroPython - - --------------test rename--------------- - ['System Volume Information', 'test_dir2'] - ['test_file'] - --------------test unlink--------------- - [] -['System Volume Information'] - ``` - - - - - - - - - - - - - - - - - - - diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/before_build_command.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/before_build_command.png deleted file mode 100644 index c7b4e34b..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/before_build_command.png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_1.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_1.png deleted file mode 100644 index 20e68411..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_1.png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_2.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_2.png deleted file mode 100644 index 1ce85a0c..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_2.png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_3.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_3.png deleted file mode 100644 index deb9c402..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/external_flash_3.png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/help('modules').png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/help('modules').png deleted file mode 100644 index 75e292ff..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/help('modules').png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/help().png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/help().png deleted file mode 100644 index 12aeadf7..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/help().png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/include_paths.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/include_paths.png deleted file mode 100644 index 9d045c37..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/include_paths.png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/other_configuration.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/other_configuration.png deleted file mode 100644 index ce69806e..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/other_configuration.png and /dev/null differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/paste_mode_pin_py.png b/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/paste_mode_pin_py.png deleted file mode 100644 index 6f4d1db1..00000000 Binary files a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/imgs/paste_mode_pin_py.png and /dev/null differ diff --git a/doc/32.TencentOS_Tiny_BearPi_MicroPython_QuickStart.md b/doc/32.TencentOS_Tiny_BearPi_MicroPython_QuickStart.md new file mode 100644 index 00000000..73fc7231 --- /dev/null +++ b/doc/32.TencentOS_Tiny_BearPi_MicroPython_QuickStart.md @@ -0,0 +1,449 @@ +# TencentOS Tiny小熊派MicroPython快速入门指南 + +| Revision | Date | Description | +| -------- | ---------- | ----------- | +| 1.0 | 2022-10-12 | 文档初版 | + +## 一. MicroPython 简介 + +MicroPython 是 Python 3 语言的精简实现 ,包括 Python 标准库的一个子集,能够在微控制器和受限环境中运行。MicroPython 可在不同平台上运行,为开发者提供快捷的嵌入式开发方式。 + +## 二. MicroPython 开发环境准备 + +### 2.1 MDK软件介绍 + +MDK 即 RealView MDK 或 MDK-ARM(Microcontroller Development kit),是 ARM 公司收购 Keil 公司以后,基于 uVision 界面推出的针对 ARM7、ARM9、Cortex-M0、Cortex-M1、Cortex-M2、Cortex-M3、Cortex-R4 等 ARM 处理器的嵌入式软件开发工具。MDK-ARM 集成了业内最领先的技术,包括 uVision4 集成开发环境与 RealView 编译器 RVCT。支持 ARM7、ARM9 和最新的 Cortex-M3/M1/M0 核处理器,自动配置启动代码,集成 Flash 烧写模块,强大的 Simulation 设备模拟,性能分析等功能,与 ARM 之前的工具包 ADS 等相比,RealView 编译器的最新版本可将性能改善超过 20%。 +Keil 公司开发的 ARM 开发工具 MDK,是用来开发基于 ARM 核的系列微控制器的嵌入式应用程序。它适合不同层次的开发者使用,包括专业的应用程序开发工程师和嵌入式软件开发的入门者。MDK 包含了工业标准的 Keil C 编译器、宏汇编器、调试器、实时内核等组件,支持所有基于 ARM 的设备,能帮助工程师按照计划完成项目。 + +### 2.2 MDK安装 + +登录 MDK 官网,下载 MDK5 软件,下载地址: +http://www2.keil.com/mdk5 或者 https://www.keil.com/download/product/ +下载的版本最好在5.24以上,本开发教程以5.24版本为例,双击 MDK524 应用程序文件,点击 next>>。 + +![mdk01](./image/BearPi_MicroPython/mdk01.png) + +勾选“I agree to ...”,即是同意一些安装协议。点击 Next>>。 + +![mdk02](./image/BearPi_MicroPython/mdk02.png) + +选择安装路径,可以默认也可以安装在我们自己建立的文件夹下。点击 Next>>。 + +![mdk03](./image/BearPi_MicroPython/mdk03.png) + +这里填写的是一些用户信息,填写完整后,继续点击 Next>>。 + +![mdk04](./image/BearPi_MicroPython/mdk04.png) + +然后等待安装完成即可。 + +![mdk05](./image/BearPi_MicroPython/mdk05.png) + +安装完成,点击 Finish。 + +![mdk06](./image/BearPi_MicroPython/mdk06.png) + +然后会跳出来这个界面,这个我们后面再讲,先点 OK,把弹框都叉掉。 + +![mdk07](./image/BearPi_MicroPython/mdk07.png) + +激活 MDK,导入 License,激活 MDK 后便可使用了。 + +![mdk08](./image/BearPi_MicroPython/mdk08.png) + +![mdk09](./image/BearPi_MicroPython/mdk09.png) + +特别提示:一定要输入 License 激活 MDK 软件,建议购买正版 License。 + +### 2.3 MDK 安装 Pack + +安装完 MDK 后,我们需要安装开发套件中单片机型号对应的 Pack。 + +**安装方式一** 登录官网:http://www.keil.com/dd2/pack/ +下载 Keil.STM32L4xx_DFP.2.0.0.pack 后安装,如下图 + +![mdk_pack01](./image/BearPi_MicroPython/mdk_pack01.png) + +**安装方式二** MDK 软件上在线安装 + +打开软件,在导航栏打开 Pack 安装界面,然后选择 ok 选项。 + +![mdk_pack02](./image/BearPi_MicroPython/mdk_pack02.png) + +![mdk_pack03](./image/BearPi_MicroPython/mdk_pack03.png) + +进入在线安装界面,选择 STM32L4XX Pack,点击 Install 进行安装。 + +![mdk_pack04](./image/BearPi_MicroPython/mdk_pack04.png) + +至此,我们开发板的单片机程序开发环境已经搭建完毕,重启 MDK 软件就可以使用了。 + +### 2.4 ST-Link 驱动安装 + +前面讲了开发板单片机程序的开发环境的搭建,但是为了将程序烧录到开发板中我们还需要使用仿真器。我们这套开发板选用 ST 公司的 ST-Link V2 仿真器进行开发板程序的烧写和仿真,下面介绍 ST-Link 驱动的安装及环境搭建。 +在 ST 官网下载 ST-Link 驱动, +https://www.st.com/content/st_com/zh/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-utilities/stsw-link009.html + +(驱动有2种: 32位电脑系统安装“dpinst_x86”、64位电脑系统安装“dpinst_amd64”)。 + +![development_env_stlink_driver_file](./image/BearPi_MicroPython/development_env_stlink_driver_file.png) + +运行对应的驱动,安装 ST-Link V2 驱动程序。安装路径尽量保持默认路径。 + +![development_env_stlink_driver_install](./image/BearPi_MicroPython/development_env_stlink_driver_install.png) + +安装完成后, 将小熊派开发板通过 USB 接口连入电脑(开发板上自带 ST-Link)。打开“设备管理器”。若看到如下图所示,表示驱动安装成功。 + +![development_env_stlink_driver_ok](./image/BearPi_MicroPython/development_env_stlink_driver_ok.png) + +这里提醒 2 点: +1, 各种 Windows 版本设备名称和所在设备管理器栏目可能不一样,例如 Win10 插上 STLINK 后显示的是 STM32 STLINK。 +2, 如果设备名称旁边显示的是黄色的叹号,请直接点击设备名称,然后在弹出的界面点击更新设备驱动 +至此, ST-Link 驱动已经安装完成。接下来大家只需要在 MDK工程里面配置一下 ST-Link 即可。 + +### 2.5 CH340 串口驱动安装 + +互联网搜索下载 CH340 串口芯片的驱动 + +安装方法:打开驱动安装程序点击安装即可。 + +![ch340_file](./image/BearPi_MicroPython/ch340_file.png) + +![ch340_driver_install](./image/BearPi_MicroPython/ch340_driver_install.png) + +注:若安装失败,请先点击卸载,后点击安装。 + +### 2.6 安装串行终端软件 + +#### 2.6.1 安装 mpremote [可选] + +MicroPython 官方提供了使用 Python 实现的命令行工具 mpremote。在安装了 Python 的情况下,mpremote 可以通过以下命令安装: + +``` +pip install mpremote +``` + +USB-TTL 模块连接到 PC 上后,可以在”设备管理器-端口(COM 和 LPT)“中看到对应的端口名: + +![basic_demo_device](./image/BearPi_MicroPython/basic_demo_device.png) + +图中显示的端口名为 COM14,在命令行中输入下列命令可以快速连接到 COM14 的串口设备上。 + +``` +mpremote connect COM14 +``` + +连接完成后,就可以在串行终端上进行操作了。 + +mpremote 还针对 MicroPython 提供了其他便捷的功能,详情可以参考 MicroPython 官方文档:https://docs.micropython.org/en/latest/reference/mpremote.html。 + +#### 2.6.2 安装 Tabby 或其他串行终端软件 [可选] + +Tabby 是一个开源的、高度可配置的现代化终端,集成了 SSH 和串行终端等功能。 +在 Tabby 的 Github 仓库中可以下载最新版本的安装包,仓库地址为:https://github.com/Eugeny/tabby/releases/latest + +安装完成后,打开 Tabby Terminal,点击“Profiles&connections”按钮: + +![tabby1](./image/BearPi_MicroPython/tabby1.png) + +在弹出的菜单中,筛选“Serial:”开头的设备,找到需要的设备进行连接: + +![tabby2](./image/BearPi_MicroPython/tabby2.png) + +选择所需的波特率,常见波特率有115200和9600: + +![tabby3](./image/BearPi_MicroPython/tabby3.png) + +连接完成后就可以在串行终端上进行操作了。 + +## 三. MicroPython 基础 demo + +### 3.1 基础 demo 工程 + +#### 3.1.1 打开 TencentOS Tiny 提供的 micropython_basic_demo 工程 + +TencentOS Tiny 官方开源仓库下载源码,地址为: +https://github.com/Tencent/TencentOS-tiny + +进入 目录,打开 BearPi_STM32L31RC.uvprojx 工程 + +![basic_demo_open](./image/BearPi_MicroPython/basic_demo_open.png) + +#### 3.1.2 编译 micropython_basic_demo 工程 + +打开工程后,在左侧的工程文件导航页面打开 examples 目录,可以看到 micropython_demo.c 源码,这里主要创建了一个 MicroPython 的任务,任务中通过调用 mp_main 函数启动 MicroPython 的主要功能。开发者可以按照下图指示,点击编译按钮编译整个工程,如图: + +![basic_demo_compile](./image/BearPi_MicroPython/basic_demo_compile.png) + +#### 3.1.3 下载运行 + +使用 USB 线将开发板连接到 PC,配置下载环境,点击下图按钮,进入工程配置界面: + +![basic_demo_config1](./image/BearPi_MicroPython/basic_demo_config1.png) + +按照下图提示配置下载器类型和 Flash 下载算法: + +![basic_demo_config2](./image/BearPi_MicroPython/basic_demo_config2.png) + +![basic_demo_config3](./image/BearPi_MicroPython/basic_demo_config3.png) + +编译完成后,点击如图所示“LOAD”按钮下载程序即可。 + +![basic_demo_download](./image/BearPi_MicroPython/basic_demo_download.png) + +#### 3.1.4 通过终端串口软件连接到 MicroPython REPL + +MicroPython REPL 是 MicroPython 的交互式解释器,可以实现 MicroPython 语句的输入执行和输出,关于 REPL 的详细用法可以参考**官方文档**了解。官方文档链接为:https://docs.micropython.org/en/latest/reference/repl.html + +首先将开发板与 USB-TTL 模块相连,micropython_basic_demo 工程默认将 UART2 用于 REPL 的输入输出,波特率为115200。 + +![basic_demo_attach_uart](./image/BearPi_MicroPython/basic_demo_attach_uart.png) + +然后将 USB-TTL 模块连接到 PC 上,查看设备管理器找到正确的串行端口。然后用终端串口软件连接到对应的端口,就可以使用 MicroPython 了。关于串口终端软件的安装和使用可以参考第2.6节“[安装串行终端软件](#2.6-安装串行终端软件)”。 + +在终端串口软件中输入回车,如果能看到 Python 的 REPL 提示符“>>>”,证明已经成功连接到 MicroPython REPL 上了。 + +下图展示了在 PowerShell 中使用 mpremote 连接 MicroPython REPL 的现象: + +![basic_demo_repl](./image/BearPi_MicroPython/basic_demo_repl.png) + +### 3.2 基础 demo 模块介绍 + +#### 3.2.1 help('modules') 命令 + +在 REPL 中执行 help('modules') 命令,可以看到当前 MicroPython 固件中包含哪些模块。基础 demo 中包含的模块较少,如下图所示。 + +![basic_demo_modules](./image/BearPi_MicroPython/basic_demo_modules.png) + +如果需要特定的内置模块,可以在第3.4节“[在基础demo工程上进行扩展](#3.4-在基础demo工程上进行扩展)”中了解如何将需要的 MicroPython 内置模块加入到基础 demo 工程中。 + +#### 3.2.2 模块介绍 + +| 模块名称 | 模块简介 | +| ----------- | ------------------------------------------------------------ | +| uarray | 数组,可以针对特定类型创建数组,并对数组进行添加或扩展等操作 | +| builtins | 内置的函数和异常 | +| gc | 控制垃圾回收器,可以启动或禁用自动垃圾回收,也可以进行手动的垃圾回收 | +| math | 数学函数(暂时不提供 expm1, log2, log10, cosh, sinh, tanh, acosh, asinh, atanh, erf, erfc, gamma 和 lgamma 等特殊函数) | +| ustruct | 打包和解包,可以将 Python 中的 Bytes 对象作为C语言的结构体进行处理 | +| usys | 系统相关函数 | +| utime | 与时间相关的函数(目前不提供和日历时间相关的功能) | +| \_thread | 提供多线程支持 | +| umachine | 与硬件相关的函数 | +| micropython | 访问和控制 MicroPython 的内部实现 | + +上述模块中,部分模块名以字母‘u’开头,这表明他们是 MicroPython 可以被替换的内部模块。这些模块在使用时,开头的‘u’可以被省略,比如:`import machine` 等同于 `import umachine`。 + +以下划线‘\_’开头的模块表示该模块还在实验中,在使用时开头的下划线不能被省略。 + +以上模块都可以在**官方文档**中找到详细使用方式,官方文档链接:https://docs.micropython.org/en/latest/library/index.html + +### 3.3 运行示例脚本 + +#### 3.3.1 通过REPL粘贴模式快捷运行脚本 + +连接到 MicroPython 的 REPL 后,可以输入 \ 进入粘贴模式。在粘贴模式下,REPL会关闭自动缩进功能,也允许输入空行,方便粘贴写好的脚本。 + +脚本粘贴完成后,输入 \ 可以退出粘贴模式,并开始执行脚本。 + +例如:将 pin.py 脚本的内容通过粘贴模式输入到 REPL 中如下图所示。 + +![basic_demo_repl_paste_mode](./image/BearPi_MicroPython/basic_demo_repl_paste_mode.png) + +#### 3.3.2 通过 mpremote 快捷运行脚本 + +mpremote 是 MicroPython 官方提供的命令行工具,除了连接串口 REPL 以外,还可以实现快捷运行脚本的功能。 + +在 PC 上运行以下命令可以快速运行脚本: + +``` +mpremote connect run +``` + +\ 是 PC 上连接到 REPL 的串行端口名称,\ 是待执行的脚本文件的路径。 + +例如:使用 mpremote 在 COM14 端口的 REPL 运行 pin.py 脚本的命令如下。 + +``` +mpremote connect COM14 run ./pin.py +``` + +#### 3.3.3 示例脚本运行现象 + +进入 目录可以看到 TencentOS Tiny 提供的四个示例脚本: + +![basic_demo_scripts](./image/BearPi_MicroPython/basic_demo_scripts.png) + +MicroPython 基础 demo 可以运行两个示例脚本,分别是 pin.py 和 thread.py: + +* **pin.py 脚本**展示了 umachine 模块和 utime 模块的部分用法。运行脚本后,LED 每过 0.5s 亮灭状态变化一次;按下按键1,打印一行文本 “key1 is pressed”,释放按键1,打印一行文本 “key1 is released”;按下按键2,LED 不再闪烁,脚本退出。 +* **thread.py 脚本**展示了 _thread 模块的使用方法。脚本会创建四个线程,序号分别为0~3,每个线程会在打印自己的序号后退出。 + +### 3.4 在基础 demo 工程上进行扩展 + +基础 demo 工程只包含了有限的 MicroPython 的功能,可以通过修改工程目录下的 board/mpconfigboard.h 文件对 MicroPython 的功能进行扩展,文件中用于功能扩展的宏定义及其描述如下表。表格中还给出了该功能依赖的文件/TOS组件,可以根据需要在工程中添加。 + +| 宏定义 | 描述 | 依赖的文件/组件 | +| -------------------------------- | ------------------------------------- | ------------------------------------------------------------ | +| MP_USING_QSPI_FLASH | 使用外部 QSPI-Flash 扩展内部 ROM 空间 | 开发板目录/BSP/Hardware/W25Qxx-QSPI/w25qxx.c | +| MP_USING_VFS | 启用 uos 模块,启用 VFS 功能 | tos_vfs 组件,
extmod/vfs.c,
extmod/vfs_reader.c,
extmod/moduos.c | +| MP_USING_MACHINE_SPI | 在 umachine 模块中启用 SPI 功能 | extmod/machine_spi.c,
drivers/bus/softspi.c, | +| MP_USING_MACHINE_I2C | 在 umachine 模块中启用 I2C 功能 | extmod/machine_i2c.c | +| MP_USING_NETWORK | 启用 network 模块 | socket_wrapper 组件,
sal_wrapper 组件,
extmod/modnetwork.c,
extmod/modusocket.c,
shared/netutils/netutils.c | +| MP_USING_USELECT | 启用 uselect 模块 | extmod/moduselect.c | +| 以下模块在扩展 demo 中会自动启用 | ----- | ----- | +| MP_USING_CMATH | 启用 cmath 模块 | - | +| MP_USING_UASYNCIO | 启用 uasyncio 模块 | extmod/moduasyncio.c | +| MP_USING_UCTYPES | 启用 uctypes 模块 | extmod/moductypes.c | +| MP_USING_UZLIBS | 启用 uzlibs 模块 | extmod/moduzlibs.c | +| MP_USING_UJSON | 启用 ujson 模块 | extmod/modujson.c | +| MP_USING_URE | 启用 ure 模块 | extmod/modure.c | +| MP_USING_HEAPQ | 启用 uheapq 模块 | extmod/moduheapq.c | +| MP_USING_UHASHLIB | 启用 uhashlib 模块 | extmod/moduhashlib.c | +| MP_USING_UBINASCII | 启用ubinascii模块 | extmod/modubinascii.c | +| MP_USING_URANDOM | 启用 urandom 模块 | extmod/modurandom.c | +| MP_USING_FRAMEBUF | 启用 framebuf 模块 | extmod/modframebuf.c | + +## 四. MicroPython 扩展 demo + +### 4.1 扩展 demo 工程 + +#### 4.1.1 打开工程前的准备 + +由于 MicroPython 对 ROM 的需求量较大,为了能够在 ROM 容量有限的小熊派开发板上运行更加完整的 MicroPython 功能,需要利用 Flash 下载算法对 ROM 空间进行扩展。 + +工程目录下包含了小熊派开发板的外部 Flash 下载算法 BearPi_W25Q64JV.FLM 文件: + +![extra_demo_flm](./image/BearPi_MicroPython/extra_demo_flm.png) + +将 BearPi_W25Q64JV 文件拷贝到 **/ARM/Flash** 目录下: + +![extra_demo_flm_copy](./image/BearPi_MicroPython/extra_demo_flm_copy.png) + +进入 目录,打开 BearPi_STM32L31RC.uvprojx 工程,按照下图指示进入下载选项配置界面: + +![extra_demo_config1](./image/BearPi_MicroPython/extra_demo_config1.png) + +![basic_demo_config2](./image/BearPi_MicroPython/basic_demo_config2.png) + +进入“Flash Download”页面,检查“Programming Algorithm”区域和“RAM for Algorithm”区域中的选项是否和下图相同: + +![extra_demo_config3](./image/BearPi_MicroPython/extra_demo_config3.png) + +如果不同,需要检查是否已经将 BearPi_W25Q64JV.FLM 文件移动到指定目录,重启 MDK。也可以手动进行修改。 + +#### 4.1.2 编译,下载和运行 + +扩展 demo 工程是在基础 demo 工程的基础上扩展部分功能实现的,其编译、下载以及运行步骤基本与基础 demo 工程相同,可以参考第3.1节“[基础 demo 工程](#3.1-基础-demo-工程)”中的相关步骤进行操作。 + +注意1:扩展 demo 涉及对外部 Flash 的写入,需要的时间较长,请耐心等待。 + +注意2:扩展 demo 中由于使用了文件系统,因此需要先按照下图,将一张 TF 卡插入到开发板的卡槽中,否则不能正常运行。 + +![extra_demo_tf](./image/BearPi_MicroPython/extra_demo_tf.png) + +### 4.2 扩展 demo 模块介绍 + +#### 4.2.1 help('modules') 命令 + +在 REPL 中执行 help('modules') 命令,可以看到当前 MicroPython 固件中包含哪些模块。扩展 demo 中包含的模块较多,如下图所示。 + +![extra_demo_modules](./image/BearPi_MicroPython/extra_demo_modules.png) + +#### 4.2.2 模块介绍 + +相比于基础 demo,扩展 demo 增加了以下模块,基础 demo 中已经包含的模块可以在第3.2节“[基础 demo 模块介绍](#3.2-基础-demo-模块介绍)”找到介绍: + +| 模块名称 | 模块简介 | +| ------------ | ------------------------------------------------------------ | +| ubinascii | 实现二进制和 ASCII 格式编码的转换,目前可以完成二进制和十六进制字符串的转换以及二进制和 Base64 编码数据的转换 | +| cmath | 提供复数运算,三角函数运算以及常数 e 和 pi | +| ucollections | 容器,包含双端队列、命名元组和有序字典 | +| uerrno | 系统错误代码,提供从 OSError 中获取详细错误代码的功能 | +| uhashlib | 哈希算法,提供 sha256 算法 | +| uheapq | 堆队列算法,提供最小堆队列算法 | +| uio | 输入/输出流,提供流对象和辅助函数的支持 | +| ujson | JSON 编码和解码功能 | +| uos | 基本“操作系统”服务(目前基本只包含了操作文件系统的功能) | +| urandom | 产生随机数 | +| ure | 简单的正则表达式 | +| uselect | 在一组流中等待事件 | +| usocket | socket 功能(目前不提供服务端的函数,如:bind、listen、accept 等) | +| \_uasyncio | 异步 I/O 调度功能 | +| uzlib | 提供 zlib 解压功能,可以将使用 DEFLATE 算法压缩过的二进制数据解压出来 | +| framebuf | 帧缓冲区操作 | +| network | 网络配置,提供网络接口的配置(目前只支持把 ESP8266 配置为网络接口) | +| uctypes | 按照C语言的方式构造结构化的二进制数据 | + +以上模块都可以在**官方文档**中找到详细使用方式,官方文档链接:https://docs.micropython.org/en/latest/library/index.html + + +### 4.3 运行示例脚本 + +#### 4.3.1 通过文件系统自动运行脚本 + +除了第3.3节“[运行示例脚本](#3.3-运行示例脚本)”中提到的两种快捷运行脚本的方式,扩展 demo 还有第三种运行脚本的方式。由于扩展 demo 中引入了文件系统,因此可以在文件系统中存储启动时自动运行的脚本。 + +MicroPython 在启动时会依次检查文件系统根目录下是否存在名为“boot.py”和“main.py”的脚本,并依次运行。 + +例如:通过将 pin.py 脚本移动到 TF 卡根目录中,并改名为 main.py,可以实现启动时自动闪灯的效果。 + +![extra_demo_main_py](./image/BearPi_MicroPython/extra_demo_main_py.png) + +#### 4.3.2 示例脚本运行现象 + +MicroPython 扩展 demo 可以运行四个示例脚本,除了第3.3节“[运行示例脚本](#3.3-运行示例脚本)”中的 pin.py 和 thread.py 脚本外,还可以运行 os.py 和 tcp.py 脚本: + +* **os.py 脚本**测试了 uos 模块中操作文件和目录的主要功能,脚本的运行后会输出以下内容: + + ``` + ---------------test mkdir--------------- + ['System Volume Information'] + ['test_dir', 'System Volume Information'] + ---------------test write--------------- + ['test_file'] + ---------------test read---------------- + ['Hello TencentOS-tiny\n', 'Hello MicroPython\n'] + ----------test with statement----------- + Hello TencentOS-tiny + + Hello MicroPython + + --------------test rename--------------- + ['System Volume Information', 'test_dir2'] + ['test_file'] + --------------test unlink--------------- + [] + ['System Volume Information'] + ``` + +* **tcp.py 脚本**展示了 network 模块和 usocket 模块的基本用法 + + 注意1:该脚本需要使用 esp8266 模块进行网络通信,因此在运行脚本前,需按照下图所示,安装上 esp8266 模块。 + + ![extra_demo_esp8266](./image/BearPi_MicroPython/extra_demo_esp8266.png) + + 注意2:脚本中的 WIFI_SSID、WIFI_PASSWORD 以及 SERVER_HOST、SERVER_PORT 需要根据实际 WiFi 和服务器情况进行修改。 + + 注意3:esp8266 连接 WiFi 时间可能较长,请耐心等待。 + + 脚本运行的结果与连接到的 TCP 服务器有关,如果连接到 TCP 回声服务器,会输出以下内容: + + ``` + esp8266 init done + esp8266 connect done + socket init done + socket connect done + send len:18 + recv data:b'This is TCP test!\n' + send len:18 + recv data:b'This is TCP test!\n' + send len:18 + recv data:b'This is TCP test!\n' + send len:18 + recv data:b'This is TCP test!\n' + send len:18 + recv data:b'This is TCP test!\n' + ``` + + \ No newline at end of file diff --git a/doc/image/BearPi_MicroPython/basic_demo_attach_uart.png b/doc/image/BearPi_MicroPython/basic_demo_attach_uart.png new file mode 100644 index 00000000..dc602e07 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_attach_uart.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_compile.png b/doc/image/BearPi_MicroPython/basic_demo_compile.png new file mode 100644 index 00000000..c216b7d3 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_compile.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_config1.png b/doc/image/BearPi_MicroPython/basic_demo_config1.png new file mode 100644 index 00000000..10eeed19 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_config1.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_config2.png b/doc/image/BearPi_MicroPython/basic_demo_config2.png new file mode 100644 index 00000000..41c42a3d Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_config2.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_config3.png b/doc/image/BearPi_MicroPython/basic_demo_config3.png new file mode 100644 index 00000000..15cea9a5 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_config3.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_device.png b/doc/image/BearPi_MicroPython/basic_demo_device.png new file mode 100644 index 00000000..32c04600 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_device.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_download.png b/doc/image/BearPi_MicroPython/basic_demo_download.png new file mode 100644 index 00000000..1173dfce Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_download.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_modules.png b/doc/image/BearPi_MicroPython/basic_demo_modules.png new file mode 100644 index 00000000..5ce60da6 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_modules.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_open.png b/doc/image/BearPi_MicroPython/basic_demo_open.png new file mode 100644 index 00000000..2a7f5cd0 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_open.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_repl.png b/doc/image/BearPi_MicroPython/basic_demo_repl.png new file mode 100644 index 00000000..d80babb3 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_repl.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_repl_paste_mode.png b/doc/image/BearPi_MicroPython/basic_demo_repl_paste_mode.png new file mode 100644 index 00000000..5ab941a0 Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_repl_paste_mode.png differ diff --git a/doc/image/BearPi_MicroPython/basic_demo_scripts.png b/doc/image/BearPi_MicroPython/basic_demo_scripts.png new file mode 100644 index 00000000..fa89751b Binary files /dev/null and b/doc/image/BearPi_MicroPython/basic_demo_scripts.png differ diff --git a/doc/image/BearPi_MicroPython/ch340_driver_install.png b/doc/image/BearPi_MicroPython/ch340_driver_install.png new file mode 100644 index 00000000..0fb65393 Binary files /dev/null and b/doc/image/BearPi_MicroPython/ch340_driver_install.png differ diff --git a/doc/image/BearPi_MicroPython/ch340_file.png b/doc/image/BearPi_MicroPython/ch340_file.png new file mode 100644 index 00000000..4dc1443f Binary files /dev/null and b/doc/image/BearPi_MicroPython/ch340_file.png differ diff --git a/doc/image/BearPi_MicroPython/development_env_stlink_driver_file.png b/doc/image/BearPi_MicroPython/development_env_stlink_driver_file.png new file mode 100644 index 00000000..0fb035b0 Binary files /dev/null and b/doc/image/BearPi_MicroPython/development_env_stlink_driver_file.png differ diff --git a/doc/image/BearPi_MicroPython/development_env_stlink_driver_install.png b/doc/image/BearPi_MicroPython/development_env_stlink_driver_install.png new file mode 100644 index 00000000..ba638ba9 Binary files /dev/null and b/doc/image/BearPi_MicroPython/development_env_stlink_driver_install.png differ diff --git a/doc/image/BearPi_MicroPython/development_env_stlink_driver_ok.png b/doc/image/BearPi_MicroPython/development_env_stlink_driver_ok.png new file mode 100644 index 00000000..f8a2ad14 Binary files /dev/null and b/doc/image/BearPi_MicroPython/development_env_stlink_driver_ok.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_config1.png b/doc/image/BearPi_MicroPython/extra_demo_config1.png new file mode 100644 index 00000000..d49de422 Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_config1.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_config3.png b/doc/image/BearPi_MicroPython/extra_demo_config3.png new file mode 100644 index 00000000..b8fc7e5d Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_config3.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_esp8266.png b/doc/image/BearPi_MicroPython/extra_demo_esp8266.png new file mode 100644 index 00000000..35e3ee49 Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_esp8266.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_flm.png b/doc/image/BearPi_MicroPython/extra_demo_flm.png new file mode 100644 index 00000000..c4d2cc92 Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_flm.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_flm_copy.png b/doc/image/BearPi_MicroPython/extra_demo_flm_copy.png new file mode 100644 index 00000000..f165944a Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_flm_copy.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_main_py.png b/doc/image/BearPi_MicroPython/extra_demo_main_py.png new file mode 100644 index 00000000..a3dd55f3 Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_main_py.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_modules.png b/doc/image/BearPi_MicroPython/extra_demo_modules.png new file mode 100644 index 00000000..663a448d Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_modules.png differ diff --git a/doc/image/BearPi_MicroPython/extra_demo_tf.png b/doc/image/BearPi_MicroPython/extra_demo_tf.png new file mode 100644 index 00000000..34757c31 Binary files /dev/null and b/doc/image/BearPi_MicroPython/extra_demo_tf.png differ diff --git a/doc/image/BearPi_MicroPython/mdk01.png b/doc/image/BearPi_MicroPython/mdk01.png new file mode 100644 index 00000000..d9f2a959 Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk01.png differ diff --git a/doc/image/BearPi_MicroPython/mdk02.png b/doc/image/BearPi_MicroPython/mdk02.png new file mode 100644 index 00000000..d0f95bac Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk02.png differ diff --git a/doc/image/BearPi_MicroPython/mdk03.png b/doc/image/BearPi_MicroPython/mdk03.png new file mode 100644 index 00000000..9f47d98d Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk03.png differ diff --git a/doc/image/BearPi_MicroPython/mdk04.png b/doc/image/BearPi_MicroPython/mdk04.png new file mode 100644 index 00000000..ed684e5c Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk04.png differ diff --git a/doc/image/BearPi_MicroPython/mdk05.png b/doc/image/BearPi_MicroPython/mdk05.png new file mode 100644 index 00000000..f232166e Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk05.png differ diff --git a/doc/image/BearPi_MicroPython/mdk06.png b/doc/image/BearPi_MicroPython/mdk06.png new file mode 100644 index 00000000..2f5dd24e Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk06.png differ diff --git a/doc/image/BearPi_MicroPython/mdk07.png b/doc/image/BearPi_MicroPython/mdk07.png new file mode 100644 index 00000000..f438fc98 Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk07.png differ diff --git a/doc/image/BearPi_MicroPython/mdk08.png b/doc/image/BearPi_MicroPython/mdk08.png new file mode 100644 index 00000000..0c7ef34b Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk08.png differ diff --git a/doc/image/BearPi_MicroPython/mdk09.png b/doc/image/BearPi_MicroPython/mdk09.png new file mode 100644 index 00000000..2a313808 Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk09.png differ diff --git a/doc/image/BearPi_MicroPython/mdk_pack01.png b/doc/image/BearPi_MicroPython/mdk_pack01.png new file mode 100644 index 00000000..b22ceffb Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk_pack01.png differ diff --git a/doc/image/BearPi_MicroPython/mdk_pack02.png b/doc/image/BearPi_MicroPython/mdk_pack02.png new file mode 100644 index 00000000..8718d5dd Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk_pack02.png differ diff --git a/doc/image/BearPi_MicroPython/mdk_pack03.png b/doc/image/BearPi_MicroPython/mdk_pack03.png new file mode 100644 index 00000000..85b63e90 Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk_pack03.png differ diff --git a/doc/image/BearPi_MicroPython/mdk_pack04.png b/doc/image/BearPi_MicroPython/mdk_pack04.png new file mode 100644 index 00000000..6fae4bb0 Binary files /dev/null and b/doc/image/BearPi_MicroPython/mdk_pack04.png differ diff --git a/doc/image/BearPi_MicroPython/tabby1.png b/doc/image/BearPi_MicroPython/tabby1.png new file mode 100644 index 00000000..9cc93ec4 Binary files /dev/null and b/doc/image/BearPi_MicroPython/tabby1.png differ diff --git a/doc/image/BearPi_MicroPython/tabby2.png b/doc/image/BearPi_MicroPython/tabby2.png new file mode 100644 index 00000000..faa104c9 Binary files /dev/null and b/doc/image/BearPi_MicroPython/tabby2.png differ diff --git a/doc/image/BearPi_MicroPython/tabby3.png b/doc/image/BearPi_MicroPython/tabby3.png new file mode 100644 index 00000000..8ab5a18f Binary files /dev/null and b/doc/image/BearPi_MicroPython/tabby3.png differ diff --git a/doc/image/BearPi_MicroPython/tabby4.png b/doc/image/BearPi_MicroPython/tabby4.png new file mode 100644 index 00000000..773a2c59 Binary files /dev/null and b/doc/image/BearPi_MicroPython/tabby4.png differ diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/scripts/os.py b/examples/micropython_demo/scripts/os.py similarity index 100% rename from board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/scripts/os.py rename to examples/micropython_demo/scripts/os.py diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/scripts/pin.py b/examples/micropython_demo/scripts/pin.py similarity index 100% rename from board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/scripts/pin.py rename to examples/micropython_demo/scripts/pin.py diff --git a/examples/micropython_demo/scripts/tcp.py b/examples/micropython_demo/scripts/tcp.py new file mode 100644 index 00000000..b336007a --- /dev/null +++ b/examples/micropython_demo/scripts/tcp.py @@ -0,0 +1,36 @@ +import network +import umachine as machine +import usocket as socket +import utime as time + +WIFI_SSID='KYzhang' +WIFI_PASSWORD='kyzhangx' +SERVER_HOST='117.50.111.72' +SERVER_PORT=8001 + +if __name__ == '__main__': + + # register esp8266 as NIC + uart0 = machine.UART(0) # BearPi LPUART1 + esp8266 = network.ESP8266(uart0) + print("esp8266 init done") + esp8266.connect(ssid=WIFI_SSID, pwd=WIFI_PASSWORD) + print("esp8266 connect done") + + # get socket and establish tcp connection + tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + print("socket init done") + tcp_socket.connect((SERVER_HOST, SERVER_PORT)) + print("socket connect done") + + # tcp send and receive + cnt = 0 + while cnt < 5: + send_len = tcp_socket.send("This is TCP test!\n") + print(f"send len:{send_len}") + data = tcp_socket.recv(1024) + if not data: + break + print(f"recv data:{data}") + cnt += 1 + time.sleep(0.5) diff --git a/board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/scripts/thread.py b/examples/micropython_demo/scripts/thread.py similarity index 100% rename from board/BearPi_STM32L431RC/KEIL/micropython_basic_demo/scripts/thread.py rename to examples/micropython_demo/scripts/thread.py