MicroPython移植(1) : 构建MicroPython的开发环境

文章目录

    • 1.1 使用msys2构建Windows下开发环境
    • 1.2 构建VMWare/Ubuntu 18.04下开发环境
    • 1.3 获取MicroPython主干代码
    • 1.4 使用nordic工程测试编译和烧写功能
    • 1.5 REPL 命令行工具

1.1 使用msys2构建Windows下开发环境

  1. 下载安装 MSYS2 + mingw-w64, 以及其他工具, 用于构建类shell环境

    # 安装msys2后,打开msys2 shell
    
    pacman -Syu		# 更新
    pacman -Su
    
    # 安装mingw-w64环境,默认全装
    pacman -S  mingw-w64-x86_64-toolchain       
    
    # 安装python3
    pacman -S  python3
    
    # 安装git & vim
    pacman -S  git vim unzip
    
  2. 下载安装arm-none-eabi-gcc

    • 下载win版(由于在win环境下,不能使用Linux版本),解压后复制到目录 C:\msys64\home\ {user}\ide\gcc-arm-none-eabi-win, 如图

      MicroPython移植(1) : 构建MicroPython的开发环境_第1张图片

    • 将arm-none-eabi-gcc的bin 目录加入环境变量

      export PATH=/home/{user}/ide/gcc-arm-none-eabi-win/bin:$PATH
      
    • 执行source ~/.bashrc 后使用arm-none-eabi-gcc -v检查安装情况

      arm-none-eabi-gcc -v
      
      Using built-in specs.
      COLLECT_GCC=C:\msys64\home\{user}\ide\gcc-arm-none-eabi-win\bin\arm-none-eabi-gcc.exe
      .....
      Thread model: single
      gcc version 8.3.1 20190703 (release) [gcc-8-branch revision 273027] (GNU Tools for Arm Embedded Processors 8-2019-q3-update)
      
  3. 安装GnuMake

    • 下载GnuMake, 放置到目录 C:\msys64\home{user}\ide\GnuMake

    • 在~/.bashrc中设置GnuMake环境变量

      export PATH=/home/{user}/ide/GnuMake:$PATH
      
  4. 安装Segger JLink for win, 并添加到环境变量

  5. 安装nRF Command Line Tools(可选,测试nRF开发板使用, 点击下载 ), 并添加到环境变量

  6. 说明

    如果不单独为msys2安装工具, 比如make或arm-none-eabi-gcc, 使用win系统本身已安装的相关工具, 可启用Windows的Path环境变量,

    有三种方法修改
    ①msys2_shell.cmd 中取消一行的注释:set MSYS2_PATH_TYPE=inherit
    ②调用msys2_shell.cmd时使用-use-full-path参数

    如: C:\msys64\msys2_shell.cmd -mingw64 -use-full-path

    ③将MSYS2_PATH_TYPE=inherit设置为windows系统的环境变量

1.2 构建VMWare/Ubuntu 18.04下开发环境

  1. VMWare下安装Ubuntu 18.04, 一般已默认安装GCC 和make

  2. 下载安装arm-none-eabi-gcc for linux

    # 使用ubuntu自带的源安装编译器 arm-none-eabi-gcc
    sudo apt-get install gcc-arm-none-eabi
    
    # 安装后查验
    gcc-arm-none-eabi -v
    
  3. 安装python3

  4. 安装Segger JLink for linux, segger官网下载deb 包即可

  5. 安装nRF command line Tools for linux (可选,测试nRF开发板使用, 点击下载 )

1.3 获取MicroPython主干代码

win系统下, 代码需要配置到msys2环境内,如路径 : C:\msys64\home{user}}\workplace\micropython

linux下,设置好各工具的全局环境变量,可以较为自由的设置源码目录

git clone ...
cd micropython
git submodule update --init     # 获取子模块依赖

1.4 使用nordic工程测试编译和烧写功能

  • 根目录编译mpy-cross
make -C mpy-cross
  • cd {SRC_ROOT}/ports/nrf/
 ./drivers/bluetooth/download_ble_stack.sh    	# 下载nrf ble协议栈
 make BOARD=pca10056 SD=s140 flash         		# pca10056 and s140 for nRF52840
  • 编译结果
make BOARD=pca10056 SD=s140
Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.
mkdir -p build-pca10056-s140/genhdr
Create build-pca10056-s140/genhdr/pins.h

........

CC modules/random/modrandom.c
CC ../../lib/nrfx/mdk/system_nrf52840.c
CC build-pca10056-s140/pins_gen.c
LINK build-pca10056-s140/firmware.elf
   text    data     bss     dec     hex filename
 155816      16    1800  157632   267c0 build-pca10056-s140/firmware.elf
arm-none-eabi-objcopy -O binary build-pca10056-s140/firmware.elf build-pca10056-s140/firmware.bin
arm-none-eabi-objcopy -O ihex build-pca10056-s140/firmware.elf build-pca10056-s140/firmware.hex
  • 烧写结果
Parsing hex file.
Erasing page at address 0x0.
Erasing page at address 0x1000.
.........
Erasing page at address 0x22000.
Erasing page at address 0x23000.
Applying system reset.
Checking that the area to write is not protected.
Programming device.
nrfjprog --reset -f nrf52
Applying system reset.
Run.

1.5 REPL 命令行工具

  1. 可以使用串口助手类软件作为 REPL 命令行交互工具, 但发送消息不是很友好,不方便生成CRTL+ * 等命令

  2. ampy工具: 略

  3. mpy-miniterm工具:

    1. 地址: https://github.com/jeffmakes/mpy-miniterm
    2. 依赖库: pip install pyserial
    3. 用法: python mpy-miniterm.py --sync-dir ./   
       要求指定 --sync-dir 参数,否则会运行时候出错
       
    4. 测试nRF效果:
    
    python mpy-miniterm.py --sync-dir ./
    args: Namespace(ask=False, baudrate=115200, delete=False, develop=False, dtr=None, echo=False, eol='CRLF', exclusive=True, exit_char=29, filter=[], menu_char=20, parity='N', port=None, quiet=False, raw=False, rts=None, rtscts=False, serial_port_encoding='UTF-8', sync_dir='./', xonxoff=False)
    syncdir: ./
    
    --- Available ports:
    ---  1: COM4                 'JLink CDC UART Port (COM4)'
    --- Enter port index or full name: 1
    --- Miniterm on COM4  115200,8,N,1 ---
    --- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
    
    >>> help()
    Welcome to MicroPython!
    
    For online help please visit http://micropython.org/help/.
    
    Quick overview of commands for the board:
      board.LED(n)    -- create an LED object for LED n (n=1,2,3,4)
    
    Control commands:
      CTRL-A        -- on a blank line, enter raw REPL mode
      CTRL-B        -- on a blank line, enter normal REPL mode
      CTRL-D        -- on a blank line, do a soft reset of the board
      CTRL-E        -- on a blank line, enter paste mode
    
    For further help on a specific object, type help(obj)
    >>>
    >>> board.LED(3).on()
    >>>
    

你可能感兴趣的:(Python,嵌入式,Iot)