def stm8flash(tooldir='../../../Tools/', tool='stm8flash', device='stm8s105c6', hardware='stlink', outdir='output', target='main.ihx'): # separate output a bit sys.stdout.write('\n') # on Windows attach .exe to tool if platform.system() == 'Windows': tool = tool + ".exe" # assemble shell command cmd = tooldir + tool + ' -c ' + hardware + ' -w ' + outdir + '/' + target + ' -p ' + device # on Windows replace path delimiter if platform.system() == 'Windows': cmd = cmd.replace('/','\\') cmd = cmd + ".exe" #print cmd # upload hexfile exitcode = os.system(cmd) # check for success if (exitcode != 0): sys.stderr.write('error '+str(exitcode)+'\n\n') misc.Exit(exitcode)
def STVP(tooldir='C:/Programme/STMicroelectronics/st_toolset/stvp/', tool='STVP_CmdLine.exe', device='STM8S105x6', hardware='ST-LINK', outdir='output', target='main.ihx'): # assemble shell command cmd = tooldir + tool + ' -BoardName=' + hardware + ' -Port=USB -ProgMode=SWIM -Device=' + device + \ ' -readData -readOption -no_progData -no_progOption -no_loop -no_log -FileProg=' + outdir + '/' + target # on Windows replace path delimiter if platform.system() == 'Windows': cmd = cmd.replace('/','\\') #print cmd # upload hexfile exitcode = os.system(cmd) # check for success if (exitcode != 0): sys.stderr.write('error '+str(exitcode)+'\n\n') misc.Exit(exitcode)
def stm8gal(tooldir='../../../Tools/', tool='stm8gal', port='/dev/ttyUSB0', outdir='output', target='main.ihx', reset=0): # on Windows attach .exe to tool if platform.system() == 'Windows': tool = tool + ".exe" # assemble shell command cmd = tooldir + tool + ' -p ' + port + ' -w ' + outdir + '/' + target + ' -v -R ' + str(reset) # on Windows replace path delimiter if platform.system() == 'Windows': cmd = cmd.replace('/','\\') #print cmd # upload hexfile exitcode = os.system(cmd) # check for success if (exitcode != 0): sys.stderr.write('error '+str(exitcode)+'\n\n') misc.Exit(exitcode)
def buildProject(workdir='.', make='make', numCPU=0): # change to specified working directory os.chdir(workdir) # print message to console sys.stdout.write('building target ... ') sys.stdout.flush() # for non-positive CPU number use all available cores if (numCPU <= 0): numCPU = multiprocessing.cpu_count() # compile project exitcode, out, err = misc.executeCmd(make+' -j'+str(numCPU), verbose=1) # on error terminate script if (exitcode != 0): misc.Exit(exitcode) else: sys.stdout.write('done\n') sys.stdout.flush()
STVP(tooldir=SWIM_PATH, device=SWIM_NAME, hardware=SWIM_TOOL, outdir=OBJDIR, target=TARGET) else: stm8flash(tooldir=TOOL_DIR, device=SWIM_NAME, hardware=SWIM_TOOL, outdir=OBJDIR, target=TARGET) # if specified open serial console after upload if args.skipterminal == False: if TERMINAL == True: cmd = 'python ' + TOOL_DIR + 'terminal.py -p ' + PORT exitcode = os.system(cmd) if (exitcode != 0): sys.stderr.write('error ' + str(exitcode) + '\n\n') misc.Exit(exitcode) # wait for return, then close window if args.skippause == False: if (sys.version_info.major == 3): input("\npress return to exit ... ") else: raw_input("\npress return to exit ... ") sys.stdout.write('\n\n') # END OF MODULE
def createMakefile(workdir='.', libroot='../../../Library/', outdir='output', target='main.ihx', options=''): # print message to console sys.stdout.write('creating Makefile ... ') sys.stdout.flush() # change to specified working directory os.chdir(workdir) # set project paths LIB_ROOT = libroot PRJ_ROOT = workdir OBJDIR = outdir TARGET = target # set command for creating dependencies and set search paths CC = 'sdcc ' CFLAGS = '-mstm8 --std-sdcc99 --opt-code-speed '+options+' ' #CFLAGS = '-mstm8 --std-sdcc99 --debug -DDEBUG '+options+' ' LFLAGS = '-mstm8 -lstm8 --out-fmt-ihx ' DEPEND = '-MM ' INCLUDE = '-I. ' for dir in misc.listSubdirs(PRJ_ROOT): dir = dir.replace('\\','/') INCLUDE += '-I' + dir + ' ' for dir in misc.listSubdirs(LIB_ROOT): dir = dir.replace('\\', '/') # convert Windows path to POSIX for Makefile if os.path.basename(dir) == 'inc': # include all 'inc' folders INCLUDE += '-I' + dir + ' ' # get set of .c files in project folder incl. subdirectories source_todo = misc.listFiles(PRJ_ROOT,".c") source_done = set() header_done = set() object_done = set() # generate generic Makefile header Makefile = open('Makefile', 'wb') Makefile.write(('OBJDIR = '+OBJDIR+'\n').encode()) # Python 3.x requires bytes, not string Makefile.write(('TARGET = '+TARGET+'\n\n').encode()) Makefile.write(('.PHONY: clean all default objects\n\n').encode()) Makefile.write(('.PRECIOUS: $(TARGET)\n\n').encode()) Makefile.write(('default: $(OBJDIR) $(OBJDIR)/$(TARGET)\n\n').encode()) Makefile.write(('all: default\n\n').encode()) Makefile.write(('# create output folder\n').encode()) Makefile.write(('$(OBJDIR):\n').encode()) if platform.system() == 'Windows': Makefile.write((' mkdir $(OBJDIR)\n').encode()) else: Makefile.write((' mkdir -p $(OBJDIR)\n').encode()) Makefile.write(('\n').encode()) # iteratively add project sources to Makefile Makefile.write(('# compile sources\n').encode()) while (len(source_todo) > 0): # get next pending source and mark as done source = source_todo.pop() source_done.add(source) # convert Windows path to POSIX for Makefile source = source.replace('\\','/') # use compiler generate dependency list cmd = CC+DEPEND+CFLAGS+INCLUDE+source #print cmd exitcode, out, err = misc.executeCmd(cmd, verbose=1) if (exitcode != 0): misc.Exit(exitcode) # for Python 3.x need to explicitely convert bytes to str if (sys.version_info.major == 3): out = str(out, sys.stdout.encoding) # append .c file with dependency and compile instruction to Makefile Makefile.write(('$(OBJDIR)/'+out).encode()) #print(out) Makefile.write(('\t'+CC+CFLAGS+INCLUDE+'-c $< -o $@\n\n').encode()) # extract file list including object[0], source[1] and headers[2..N] out = out.replace(':', '') out = out.replace('\\', '') out = out.replace('\n', '') out = out.split() #print out # for all files returned by compiler... for next in out: # append object files for linker if next.endswith('.rel'): object_done.add(next) # if corresponding source to header exists, add to pending sources if next.endswith('.h'): if next not in header_done: # not yet in list header_done.add(next) # add to treated headers next = (next[::-1].replace("/inc/"[::-1], "/src/"[::-1], 1))[::-1] # replace last /inc/ by /src/ (see https://stackoverflow.com/questions/2556108/rreplace-how-to-replace-the-last-occurrence-of-an-expression-in-a-string) if (os.path.isfile(next[:-1]+'c')): # if corresponding .c exists, add to todo list source_todo.add(next[:-1]+'c') # link project object files Makefile.write(('# link sources\n').encode()) Makefile.write(('$(OBJDIR)/$(TARGET): ').encode()) for next in object_done: Makefile.write(('$(OBJDIR)/'+next+' ').encode()) Makefile.write(('\n').encode()) Makefile.write(('\t'+CC+LFLAGS).encode()) for next in object_done: Makefile.write(('$(OBJDIR)/'+next+' ').encode()) Makefile.write((' -o $@\n').encode()) # close Makefile.dep Makefile.close() # print message to console sys.stdout.write('done\n') sys.stdout.flush()
# show progress sys.stdout.write('compile ' + os.path.dirname(file) + ' ... ') sys.stdout.flush() # build w/o upload if platform.system() == 'Windows': err = os.system( 'python build_upload.py --skipupload --skipterminal --skippause > NUL' ) else: err = os.system( 'python build_upload.py --skipupload --skipterminal --skippause > /dev/null' ) if err != 0: misc.Exit(err) # print message sys.stdout.write('done\n') sys.stdout.flush() # restore working directory os.chdir(oldDir) # optional prompt for return, then close window if True: if (sys.version_info.major == 3): input("\npress return to exit ... ") else: raw_input("\npress return to exit ... ") sys.stdout.write('\n\n')