Try MicroPython control MCU

1. Summary

This document mainly introduces how to develop embedded applications in the python environment, taking the STM32H43 board as an example. From the system environment construction, to compilation, to the realization of hardware control with python. Can be used as an introductory tutorial.

2. Preparation

2.1 Development environment construction

2.1.1 Win10 comes with Linux system

The development environment can use the virtual machine by itself and then install the ubuntu system, and the Win10 system comes with the linux system, which can be used only by enabling it, which is more convenient, so we take the linux subsystem that comes with the win10 as an example.

Enable the linux subsystem in the control panel, click to enable or disable the Windows function.

Check the windows subsystem for linux as shown below, and then restart the computer

Click to restart now

After restarting, enter the windows 10 application store, search for Ubuntu, you can choose 18.04 or 20.02 to install

After downloading and installing, click to start

Set the username and password,

The default path for installation is

C:Usershpp19AppDataLocalPackagesCanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgscLocalState ootfshome

The path cannot be modified, so the linux environment is just fine, you can enter some common commands to see, such as ls, cp, rm and other commands.

The Linux subsystem installed under Win10 only supports the command line and does not have comprehensive functions to install with a virtual machine, but it is sufficient for the development of micropython.

2.1.2 Virtual machine installation

If you don't want to install with windows10, you can build it yourself. You need to install a virtual machine and download the iso image file of ubunut, and install it. There are many such installations on the Internet. We will not list them in detail. The first installation is recommended.

2.1.3 Dependent software installation

Git installation, git is used to obtain packages

Use the cloned micropython software package, you can also go to the official website to download it yourself, and copy it to the home directory of the linux environment.

Use git to clone the package

If the network is not good, wait for a while, which is slower. You can also go to the official website to download it yourself. I have already downloaded it. You can ask me for the latest board micropython-1.13.

http://www.micropython.org/download/

2.1.4 Micropython directory structure

The directory structure is more important. Only when the directory structure is cleared can you migrate to different boards.

For detailed directory structure introduction, please refer to README.md

The main focus is on the ST development board currently supported by micropython. The H7 series is more relevant to us. You can see that many ST chips are currently supported.

3. Cross compilation tool chain

Enter sudo apt install gcc make in the terminal to install gcc, make

After installation, you can check the version

Enter sudo apt-get install gcc-arm-none-eabi to install the compiler for ARM

If your cross-toolchain installation and downloading is slow, please follow the steps below to modify, because the software source address that comes with linux is all foreign, the domestic update download installation will be slower, you need to update the list to domestic

Delete all the contents of the opened file and replace it with the following mirror source

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

After modifying the software source, update the software list and software:

sudo apt update

sudo apt upgrade

If you are not familiar with the vim editor commands, you can Baidu yourself, or directly in the following directory

C:Usershpp19AppDataLocalPackagesCanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgscLocalState Under ootfsetcapt, replace the sources.list file with the following Alibaba Cloud mirror source, which will be very fast.

Compile mpy-cross, mpy-cross is a micropython auxiliary tool, which will be used during the compilation process, enter the command make -C mpy-cross in the root directory of the source code of micropython

At this point, the cross-compilation tool chain is installed and the STM32 program can be compiled.

4. Compile the program

Enter the command make -C ports/stm32 in the source root directory. If BOARD is not specified, the PYBV10 board under ports/stm32 will be compiled by default, and what we need is to be able to run on H743, so we need to cut and transplant a H743 On the board, you can see that the STM32 boards currently supported by micropython are F091, F401, F429, F446, F746, H743, etc.

Copy NUCLEO_H743ZI and name it BODEBOARD

Enter the command ls to view the BODEBOARD folder and files, enter the view

There are mainly 5 files, and we need to modify the files inside

First modify the clock to 25000000, mainly modify the stm32h7xx_hal_conf.h file

Modify mpconfigboard.h

Modify the clock to the following configuration, you can copy the clock configuration under our previous IAR project.

Modify other pins. In this section, we will only modify the three LED and UART1 pins as examples. In the next version update of the document, all the hardware pins used will be updated, or continue to modify the corresponding in this file Hardware pins

Save and exit

Modify the pins.csv file

Add LED and UART1 pins

Save and exit.

Because we are referring to NUCLEO_H743ZI, the mpconfigboard.mk file does not need to be modified. Now that the modification is completed, you can compile, enter the command make -C ports/stm32 BOARD=BODEBOARD in the root directory of micropython

Wait for the compilation to complete.

You can see that the .hex and .dfu firmware are generated, choose one of them and download it to the H743 board, so there is a python parser in the board, which can recognize the python language

How to copy the compiled firmware to the windows host? Of course, you can go directly to the generated directory and copy it. The windows partition is mounted by default in the linux subsystem of win10, and we can see it in the /mnt directory

In this way, we can directly use the command cp to copy to windows. Of course, if you are not used to command line operations, you can install the midinight commander software, which can easily manage files. Enter the command sudo apt install mc to install. After the installation, enter mc. Open the software directly.


No matter which method is used, download the Hex file to the hardware board. After the download is complete, power on the board again. You can see a PYBFLASH similar to a U disk drive letter, and open boot.py inside.

Main.py and other four files, our final application can be run by directly writing in main.py.

5. Testing

Let's first demonstrate using putty to control the LED lights on the board, turn on putty, configure the baud rate and serial port number,

Import the LED library to control the LED

It can be observed that the three LED lights of the H743 board light up in sequence, and the hardware can be controlled through Putty. Then how to make the board power on and the three LED lights light up without the need to control Putty. ?

This is to edit the main.py file we mentioned earlier, open it with any text editor, write the python code, save and exit, and you can run it.

The LED lighting control is realized through the above code, and the single-chip microcomputer is controlled by python. So far, you can write a lot of fun things in python and run on the microcontroller.

6. Reference documents

Serial number

literature

1

www.micropython.org  



Guess you like

Origin blog.csdn.net/wgp2hpp/article/details/109523692
try