Python automated code generation and validation code summary

Keywords: Python

0 thread

First, explain the whole plan, which is divided into two parts to automatically generate the required code, compile and download, and verify the correctness of the code

It is hoped that through such an automatic way, the repeated labor can be reduced in the chip composite pin test, and the automatic code generation IDE can be improved.

1. Automatically open, compile and download:

1.1 automatically open the project and open the software

The following code opens the Keil project through an absolute path

import subprocess
import os
main = "C:/Users/siyu/.PyCharmCE2019.1/config/scratches/two/main.uvproj"
if os.path.exists(main):
    rc,out= subprocess.getstatusoutput(main)
    print (rc)
    print ('*'*10)
    print (out)

reference resources:
Three methods of calling executable file. exe in python

1.2. Automatically click the compile and download buttons,

Use python to open the project file, and use opencv in python to locate the keys

Package installation

pip install pyautogui
import pyautogui
#Both methods can be used to determine the position of the target screenshot on the system
#location=pyautogui.locateOnScreen(image='autotest.png')
location=pyautogui.locateOnScreen(image='C:/Users/siyu/Pictures/autotest.png')
#Output coordinates
print(location)
#The center coordinate position of the target image in the system is obtained by using the center() function
x,y=pyautogui.center(location)
print('center()',x,y)

#Click on the recognized target image
#The parameters X and Y represent the coordinate position, clicks represent the number of clicks, and button can be set as left or right button
pyautogui.click(x=x,y=y,clicks=1,button='left')

This has nearly realized the operation based on GUI. The difference between clicking on different cases requires dynamic addition of buttons.

Reference code
Used
[pyautogui] image recognition and clicking on windows system using Python (Mac OS system can also)

other
[Python] match the position in the original image through the screenshot (opencv)
Pyppeter (VI) – find the location of an image in another image through opencv

2. Automatic code writing

This is divided into three parts: a) add C files in the implementation project b) change the code in C files C) implement the optimized GUI

2.1 changes to the project

Implementation steps:
Open project file in text mode > > > Add File

Read the project file and store it in the str array

f = open("main.uvproj","r")   #Set file object

str = f.read()     #Read all the contents of the txt file into the string str
print(str)
f.close()   #Close file

Realize the search of main string and output line number

findMusic="main";
MusicLine=0;
f = open('main.c','r')

for lines in f.readlines():
    MusicLine=MusicLine+1;
    if lines.find(findMusic)!=-1:
        print(lines)
        print("find it .........................")
        print(MusicLine)
    #print(lines,end='')
f.close()

python implements changes to the specified line
The code is not verified. It should be python2 code

#coding=utf-8
lines=[]
f=open("d:\\1script\\1.txt",'r')  #your path!
for line in f:
    lines.append(line)
f.close()
print lines
lines.insert(3,"666\n")           #Insert 666 in the fourth line and enter
s=''.join(lines)
f=open("d:\\1script\\1.txt",'w+') #Rewrite file
f.write(s)
f.close()
del lines[:]                      #clear list 
print lines

Reference code:
How python opens a txt file It realizes two functions of reading and writing.
python writes to the specified line of the file

2.2 changes to C document

Implementation steps: open the C file > > > locate the location to be changed > > > compare the modification library, modify the code > > > save and exit
The library function is required to locate the code, identify all the meaning of the code, and it is difficult to realize. Only the code area can be generated, and editing is prohibited,

Read the c file and store it in str

f = open("main.c","r")   #Set file object

str = f.read()     #Read all the contents of the txt file into the string str
print(str)
f.close()   #Close file

Part of verification

FPGA is used as the verification input of the digital part to upload the high and low level and information to the upper computer. The upper computer makes judgment to detect whether the information is correct.

USB speed:

Foreign language nametimetransmission speed Supply voltageSupply current
USB1.019961.5 Mbps(0.12 MB/s) (Low-Speed)5V500mA
USB1.1199812 Mbps(1.5 MB/s) (Full-Speed)5V500mA
USB2.02000480Mbps(60 MB/s)(High-speed)5V500mA
USB3.020085.0Gbps(500MB/s )(Super-speed)5V900mA
USB3.110.0Gbps(1000MB/s )(Super-speed)

USB transfer to computer
Xiaomeige: USB2.0 CY7C68013 is based on xiaomeige ACX720 FPGA development board and adopts the driver recommended by the chip manufacturer.
Punctual atom Da Vinci FPGA development board

Operation logic

  1. Select the required function (e.g. IIC)
  2. Select the desired GPIO
  3. Generate code, generate project file
  4. Open project file
  5. Compile and download project files
  6. Use the logic analysis instrument to grab the information and upload it to the upper computer
  7. The host computer analyzes the implementation logic to judge whether it is correct

Posted by infyportalgroup on Sun, 05 Dec 2021 13:20:06 -0800