代码拉取完成,页面将自动刷新
import os
import shutil
class CodeGen():
def __init__(self,targetPath):
self.PROJ_PATH = targetPath
def IncludeCMSIS(self):
lines =[]
with open(self.PROJ_PATH+'\\Core\\Inc\\main.h', 'r', encoding='utf-8') as f:
lines = f.readlines()
incbegin = -1
incend =-1
inclosbegin =-1
inclosend =-1
inclos =-1
for linenumber in range(1,len(lines)):
if '/* USER CODE BEGIN Includes */' in lines[linenumber-1]:
incbegin = linenumber
if '/* USER CODE END Includes */' in lines[linenumber-1]:
incend = linenumber
if '/* LITEOS BEGIN Includes */' in lines[linenumber-1]:
inclosbegin = linenumber
if '/* LITEOS END Includes */' in lines[linenumber-1]:
inclosend = linenumber
if '#include \"cmsis_os.h\"' in lines[linenumber-1]:
inclos = linenumber
if inclos < 0:
if incbegin >0 and inclosbegin <0:
lines.insert(incbegin,'/* LITEOS BEGIN Includes */\n'+'#include \"cmsis_os.h\"\n' + '/* LITEOS END Includes */\n')
if incbegin >0 and inclosbegin >0:
lines.insert(inclosbegin,'#include \"cmsis_os.h\"\n' )
if inclos <0 and incbegin > 0:
with open(self.PROJ_PATH+'\\Core\\Inc\\main.h', 'w', encoding='utf-8') as wf:
wf.write(''.join(lines))
def LiteOScGen(self):
if not os.path.exists(self.PROJ_PATH+'\\Core\\Src\\liteos.c'):
shutil.copy2('liteos.c', self.PROJ_PATH+'\\Core\\Src\\liteos.c')
lines =[]
with open(self.PROJ_PATH+'\\Core\\Src\\main.c', 'r', encoding='utf-8') as f:
lines = f.readlines()
pfpbegin = -1
pfpend =-1
pfplosbegin = -1
pfplosend =-1
lospfp =-1
for linenumber in range(1,len(lines)):
if '/* USER CODE BEGIN PFP */' in lines[linenumber-1]:
pfpbegin = linenumber
if '/* USER CODE END PFP */' in lines[linenumber-1]:
pfpend = linenumber
if 'void LiteOS_Init(void);' in lines[linenumber-1]:
lospfp = linenumber
if '/* LITEOS BEGIN PFP */' in lines[linenumber-1]:
pfplosbegin = linenumber
if '/* LITEOS END PFP */' in lines[linenumber-1]:
pfplosend = linenumber
if lospfp < 0:
if pfpbegin >0 and pfplosbegin <0:
lines.insert(pfpbegin,'/* LITEOS BEGIN PFP */\n'+'void LiteOS_Init(void);\n' + '/* LITEOS END PFP */\n')
if pfpbegin >0 and pfplosbegin >0:
lines.insert(pfplosbegin,'void LiteOS_Init(void);\n' )
usrbegin2 =-1
usrend2 =-1
initcall =-1
osstart =-1
for linenumber in range(1,len(lines)):
if '/* USER CODE BEGIN 2 */' in lines[linenumber-1]:
usrbegin2 = linenumber
if '/* USER CODE END 2 */' in lines[linenumber-1]:
usrend2 = linenumber
if 'LiteOS_Init();' in lines[linenumber-1] :
initcall = linenumber
if 'osKernelStart();' in lines[linenumber-1]:
osstart = linenumber
if initcall < 0 and usrbegin2 >0:
lines.insert(usrbegin2,'\tLiteOS_Init();\n' )
if osstart <0 and usrend2 >0:
lines.insert(usrend2-1,'\tosKernelStart();\n' )
if osstart < 0 or initcall < 0 or lospfp <0:
with open(self.PROJ_PATH+'\\Core\\Src\\main.c', 'w', encoding='utf-8') as wf:
wf.write(''.join(lines))
def adjConfRamSize(self,ramsize):
lines =[]
with open(self.PROJ_PATH+'\\LiteOS\\Config\\target_config.h', 'r', encoding='utf-8') as f:
lines = f.readlines()
sizeline = -1
for linenumber in range(1,len(lines)):
if '#define BOARD_SRAM_SIZE_KB' in lines[linenumber]:
lines[linenumber] = '#define BOARD_SRAM_SIZE_KB ' + ramsize + '\n'
with open(self.PROJ_PATH+'\\LiteOS\\Config\\target_config.h', 'w', encoding='utf-8') as wf:
wf.write(''.join(lines))
def CancelIRQHandler(self):
for root, _, filenames in os.walk(self.PROJ_PATH + '\\Core\\Src'):
for filename in filenames:
if filename.endswith('_it.c') and filename.startswith('stm32'):
source_file_path = os.path.join(root, filename)
lines =[]
with open (source_file_path,'r',encoding='utf-8') as f:
lines = f.readlines()
pendirq = 0
pendstart =0
pendend = 0
for linenumber in range (1,len(lines)):
if lines[linenumber-1].startswith('void PendSV_Handler(void)' ):
pendirq = linenumber
if pendirq > 0 and '{' in lines[linenumber-1]:
pendstart = pendstart +1
if pendstart > 0 and '}' in lines[linenumber -1]:
pendend = pendend +1
if pendstart > 0 and pendstart == pendend:
pendend = linenumber
break
if pendirq >0 and pendend > pendirq:
lines[pendirq -1] = '#if 0\n'+ lines[pendirq-1]
lines[pendend -1] = lines[pendend-1]+'\n#endif'
sysirq =0
sysstart =0
sysend =0
for linenumber in range (1,len(lines)):
if lines[linenumber-1].startswith('void SysTick_Handler(void)'):
sysirq = linenumber
if sysirq > 0 and '{' in lines[linenumber-1]:
sysstart = sysstart +1
if sysstart > 0 and '}' in lines[linenumber -1]:
sysend = sysend +1
if sysstart > 0 and pendstart == sysend:
sysend = linenumber
break
if sysirq >0 and sysend > sysirq:
lines[sysirq -1]= '#if 0\n' + lines[sysirq-1]
lines[sysend -1]= lines[sysend-1]+'\n#endif'
with open(source_file_path, 'w', encoding='utf-8') as wf:
wf.write(''.join(lines))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。