This document mainly introduces , stay python In the environment , How to develop embedded applications , With STM32H43 Board as an example to introduce . Build from the system environment , To compile , To use python Realize hardware control . It can be used as an introductory course .
2.1 Development environment construction
2.1.1 Win10 Bring their own linux System
The development environment can use the virtual machine itself and install ubuntu System , and Win10 The system comes with linux System , Just enable to use , It's more convenient , So we take win10 Self contained linux Subsystem as an example .
Enable... In the control panel linux Subsystem , Click enable or close Windows function .
As shown in the figure below, check for linux Of windows Subsystem , Then restart the computer
Click restart now
Restart and enter windows 10 The app store , Search for Ubuntu, You can choose 18.04 perhaps 20.02 Installation
Download and install , Click start
Set user name and password ,
The default path for installation is
The path cannot be modified , thus linux The environment is good , You can enter some common commands to see , Such as ls,cp, rm Wait for the order .
Win10 Underground installation linux The subsystem only supports the command line , No self installed virtual machine features are comprehensive , But for development micropython enough .
2.1.2 Virtual machine installation
If you don't want to use windows10 Installed , If you want to build it yourself , Need to install virtual machine and download ubunut Of iso Image file , Installation , There are a lot of these installations on the Internet , We will not list in detail , The first installation is recommended .
2.1.3 Rely on software installation
Git install ,git To get software packages
Using clones micropython Software package , You can also download it from the official website , copy to linux Environmental home Under the table of contents .
Use git Clone packages
If the network is not good , It's going to take a while , It's slow . You can also download it from the official website , I've done it , You can ask me for the latest board micropython-1.13
2.1.4 Micropython Directory structure
The directory structure is more important , Only clear the directory structure , You can transplant to different boards .
For a detailed introduction to the directory structure, please refer to README.md
Focus on the present micropython Supported by ST Development board , What has more to do with us is H7 series , A lot of support can be seen right now ST chip .
3. Cross compile toolchain
Input... At the terminal sudo apt install gcc make Command to install gcc, make
After installation, you can check the next version
Input sudo apt-get install gcc-arm-none-eabi Install for ARM The compiler
If your cross toolchain is slow to install and download , Please follow the steps below to modify , because linux The software source address is foreign , The download and installation of domestic updates will be slower , The list needs to be updated to domestic
Delete all the contents of the open file , Replace with the following image 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 software list and software ：
sudo apt update
sudo apt upgrade
If the vim Editor commands are unfamiliar , Can own Baidu , Or directly in the following directory
C:Usershpp19AppDataLocalPackagesCanonicalGroupLimited.Ubuntu20.04onWindows_79rhkp1fndgscLocalState ootfsetcapt Lower bound sources.list Replace the file with the following alicloud image source , It's going to be fast .
compile mpy-cross, mpy-cross It's a micropython The auxiliary tools of , In the compilation process, we will use , stay micropython Enter the command in the root directory of the source code of make -C mpy-cross
At this point, the cross compilation tool chain is installed , You can compile STM32 The program .
Enter the command in the root directory of the source code make -C ports/stm32 If you don't specify BOARD, The default compiler is ports/stm32 Under the PYBV10 The board , And what we need is to be able to H743 Run up , So we need to cut and transplant a pair of H743 The board of , You can see micropython Currently supported STM32 The board has F091, F401,F429,F446,F746,H743 etc. ,
Copy NUCLEO_H743ZI Name it BODEBOARD
Enter the command ls Check it out. There's already BODEBOARD Folders and files , Check it out
There are mainly 5 File , And we need to modify the files in this
First change the clock to 25000000, The main thing is to modify stm32h7xx_hal_conf.h file
Change the clock to the following configuration , It can be copied from before IAR Clock configuration under the project .
Modify other pins , In this section, we only revise three LED and UART1 Wait for a pin, for example , In the next version update of the document , All hardware pins used will be updated , Continue to modify the corresponding hardware pins in this file
Save and exit
modify pins.csv file
add to LED and UART1 The pins of
Save and exit .
Because we are referring to NUCLEO_H743ZI, therefore mpconfigboard.mk There is no need to modify the document . This is the end of the modification , You can compile , stay micropython Enter the command in the root directory make -C ports/stm32 BOARD=BODEBOARD
Wait for the compilation to complete .
You can see that there is .hex and .dfu The firmware , Download any one into H743 The board , In this way, the board will have python Parser , Can identify python Language
How to copy the compiled firmware to windows What about the mainframe , Of course, you can copy directly under the generated directory , stay win10 Of linux By default, the subsystem has already set windows The partition is mounted , We can do it in /mnt See under directory
So we can use the command directly cp copy to windows 了 , Of course, if you're not used to command line operations , Can install midinight commander Software , It's easy to manage files , Enter the command sudo apt install mc Installation , After the installation , Input mc You can open the software directly .
Either way , take Hex Download the file to the hardware board , After downloading , Power up the board again , You can see a PYBFLASH similar U The Rune of a disk , Open it and there's boot.py
main.py Wait for four documents , Our final application is written directly in main.py It's ready to run .
Let's first demonstrate the use of putty To control the board LED The lamp , open putty, Configure baud rate and serial port number ,
Import LED library , You can control LED 了
You can observe H743 Three of them LED The lights turn on in turn , adopt Putty You can control the hardware , So how to power up the board is just three LED The light is on , You don't have to pass it Putty Control .？
This is to edit what we mentioned earlier main.py file , Use any text editor to open , write in python Code , Save and exit , It's ready to run .
Through the above code to achieve LED The lighting control of , To achieve python Control MCU . Now you can use python Write a lot of fun things in the SCM run .
6. Reference documents