看到 @dcexpert 最近一直在研究micropython , 我也决定凑凑热闹 。
首先声明, 此非我原创,我是根据micropython的github 源码移植的。
1.先做好准备工作:
(1)装一个linux系统的笔记本, 我用的是ubuntu14.04 32位, 64位有些不同
(2) 下载 micropython 源码
https://github.com/micropython/micropython 点 Download Zip 即可
(3)下载 arm-none-eabi
https://launchpad.net/gcc-arm-embedded/5.0/5-2015-q4-major/+download/gcc-arm-none-eabi-5_2-2015q4-20151219-linux.tar.bz2
解压
tar xvjf gcc-arm-none-eabi-5_2-2015q4-20151219-linux.tar.bz2
输入以下命令:
vim ~/.bashrc
在文件末尾添加 PATH环境变量
$PATH = xxxx/gcc-arm-none-eabi-5_2-2015q4/bin:$PATH;
xxx为安装包解压后的文件路径,这里是想让系统直接能找到arm-none-eabi-gcc compiler
然后重新source 一下
source ./
注意这里如果是64位系统, 必须要先安装64位下可以执行32位的工具
sudo apt-get install ia32-libs
如果在shell输入 arm 双击tab可以出来 arm-none-eabi-gcc 等一系列工具,说明ok了
(4) 下载 st-link 源码
https://github.com/texane/stlink
解压之后, 先要安装依赖的包
sudo apt-get install libusb-1.0-0-dev pkg-config autotools-dev autoconf
然后进入解压文件,依次执行:
./autogen.sh
./configure
make
然后会出现一个st-flash文件,可以烧写bin到开发板,另外,它可以用于gdb调试,这个自己去探索吧
然后还是要添加PATH路径,不过这次要在所有用户都添加,因此选择 /etc/profile 文件
vim /etc/profile
在末尾添加 :
export PATH=xxxx/stlink:$PATH; 注: xxxx为文件路径,每个人都不同
然后重新source一下,验证方法和前面一样,如果在任何目录可以直接出来 st-flash
(4) 串口工具 minicom
sudo apt-get install minicom
至此,准备工作完成了
2. 编译
进入micropython 的stmhal目录(留心观察的网友可能会发现 总目录下有 cc3200 和 esp8266等目录,这些都是已经移植成功的板子,有这些板子的可以自己尝试一下)
cd stmhal
执行:
make BOARD=STM32F7DISC
如果会看 Makefile的可以看一下Makefile文件,里面开头源码如下:
- # Select the board to build for: if not given on the command line,
- # then default to PYBV10.
- BOARD ?= PYBV10
- ifeq ($(wildcard boards/$(BOARD)/.),)
- $(error Invalid BOARD specified)
- endif
复制代码 显然 BOARD指名的是 board子目录下的目录,
然后进入 board目录,发现有 NUCLE_F411RE . NUCLEO_F401RE, STM32F439, STMF429DISC 等,其中就有 STM32F7DISC
因此指定不同的BOARD就可以编译不同的板子,有这些板子的也都可以 嗨起来了
如果之前的步骤没有错误的话,这次编译不会报错,出现 build-STM32F7DISC 目录
里面有我们要烧写的文件 : firmware0.bin 和 firmware1.bin
(3)烧写
先将stm32f7和电脑连接
执行如下命令:
lsusb
目的是找到 stm32f7的usb bus和device
bruno@bruno-linux:~/git/micropython/stmhal$ lsusb
Bus 004 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 004 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 003 Device 003: ID 1bcf:2883 Sunplus Innovation Technology Inc.
Bus 003 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c07e Logitech, Inc.
Bus 001 Device 012: ID 0483:374b STMicroelectronics ST-LINK/V2.1 (Nucleo-F103RB)
Bus 001 Device 011: ID 04d9:0024 Holtek Semiconductor, Inc.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
然后转到管理员 sudo -s
设置环境变量 : export STLINK_DEVICE="001:012" 001是Bus , 012是Device
烧写: make BOARD=STM32F7DISC deploy-stlink
如果一切顺利的话,其实非常不顺利,我第二次烧写出现了问题
出现如下错误:
- root@bruno-linux:~/git/micropython/stmhal# make BOARD=STM32F7DISC deploy-stlink
- Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
- Writing build-STM32F7DISC/firmware0.bin to the board via ST-LINK
- 2016-03-29T18:37:17 INFO src/stlink-usb.c: bus 001 dev 021
- 2016-03-29T18:37:17 INFO src/stlink-common.c: Loading device parameters....
- 2016-03-29T18:37:17 WARN src/stlink-common.c: unknown chip id! 0x10000
复制代码然后去github stlink上找解决办法,找了一会,终于找到了, 有人说长按 reset 键4-5秒,然后再次执行,就ok了,我试了一下,很靠谱
如果不能解决你们的问题的话,建议自己去github 上找一找
完美的话,提示如下:
- root@bruno-linux:~/git/micropython/stmhal# make BOARD=STM32F7DISC deploy-stlink
- Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
- Writing build-STM32F7DISC/firmware0.bin to the board via ST-LINK
- 2016-03-29T18:37:31 INFO src/stlink-usb.c: bus 001 dev 021
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Loading device parameters....
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Device connected is: F7 device, id 0x10016449
- 2016-03-29T18:37:31 INFO src/stlink-common.c: SRAM size: 0x50000 bytes (320 KiB), Flash: 0x100000 bytes (1024 KiB) in pages of 2048 bytes
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Attempting to write 10084 (0x2764) bytes to stm32 address: 134217728 (0x8000000)
- EraseFlash - Sector:0x0 Size:0x8000
- Flash page at addr: 0x08000000 erased
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Finished erasing 1 pages of 32768 (0x8000) bytes
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Starting Flash write for F2/F4/L4
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Successfully loaded flash loader in sram
- enabling 32-bit flash writes
- size: 10084
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Starting verification of write complete
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Flash written and verified! jolly good!
- Writing build-STM32F7DISC/firmware1.bin to the board via ST-LINK
- 2016-03-29T18:37:31 INFO src/stlink-usb.c: bus 001 dev 021
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Loading device parameters....
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Device connected is: F7 device, id 0x10016449
- 2016-03-29T18:37:31 INFO src/stlink-common.c: SRAM size: 0x50000 bytes (320 KiB), Flash: 0x100000 bytes (1024 KiB) in pages of 2048 bytes
- 2016-03-29T18:37:31 INFO src/stlink-common.c: Attempting to write 274300 (0x42f7c) bytes to stm32 address: 134348800 (0x8020000)
- EraseFlash - Sector:0x4 Size:0x20000
- Flash page at addr: 0x08020000 erasedEraseFlash - Sector:0x5 Size:0x40000
- Flash page at addr: 0x08040000 erased
- 2016-03-29T18:37:35 INFO src/stlink-common.c: Finished erasing 2 pages of 262144 (0x40000) bytes
- 2016-03-29T18:37:35 INFO src/stlink-common.c: Starting Flash write for F2/F4/L4
- 2016-03-29T18:37:35 INFO src/stlink-common.c: Successfully loaded flash loader in sram
- enabling 32-bit flash writes
- size: 32768
- size: 32768
- size: 32768
- size: 32768
- size: 32768
- size: 32768
- size: 32768
- size: 32768
- size: 12156
- 2016-03-29T18:37:38 INFO src/stlink-common.c: Starting verification of write complete
- 2016-03-29T18:37:39 INFO src/stlink-common.c: Flash written and verified! jolly good!
复制代码
然后现在就可以 玩 micropython啦, 先将 板子的usb fs和电脑连接起来,之前的st-link usb连接不能断,要供电,
连接后,会出来一个新的文件夹,放的就是我们熟悉的文件啦
boot.py main.py pybcdc.inf README.txt
然后 lsusb | grep ttyACM
选择 ttyACM0
打开串口 ,先配置
sudo minicom -s
进入 Serial port setup
配置为 /dev/ttyACM0
115200 8N1
其他不变,回车
选择 Save setup as dfl
然后选择 Exit
ok ,一些就绪
进入熟悉的python环境了
尝试了一下:
- myled = pyb.LED(2)
- myled.on()
- myled.off()
- myled.toggle()
复制代码
不出意外,小灯果然点亮和熄灭了