| #!/usr/bin/env python |
| #-*- coding: UTF-8 -*- |
| |
| import os.path |
| import sys |
| import argparse |
| import re |
| |
| |
| |
| inputfile = 'V:\gpio\MT6297_EVB_MTK1353_Golden_Setting.csv' |
| outpath = 'V:\gpio\\' |
| final_setting = [] |
| label = {'number':0, 'name':1, 'usage':2, 'mode':3, 'drv':4, 'pullen':5, 'pullsel':6, 'dir':7, 'out':8, 'smt':9, 'ies':10, 'user':11, 'app':12} |
| |
| def setting_entry_process(setting): |
| |
| entry = re.split(',', setting.upper()) |
| #gpio number |
| temp_list = ['GPIO', entry[label['number']]] |
| entry[label['number']] = ''.join(temp_list) |
| |
| #Use as EINT: 1 Use as GPIO: 0 |
| if entry[label['usage']] == 'N': |
| entry[label['usage']] = 'USE_AS_GPIO' |
| else: |
| entry[label['usage']] = 'USE_AS_EINT' |
| |
| #mode |
| #if isinstance(entry[label['mode']], int) is False: |
| temp_list = ['GPIO_MODE_0', entry[label['mode']]] |
| entry[label['mode']] = ''.join(temp_list) |
| |
| #drive strength, default as 0 |
| if entry[label['drv']] == 'DEFAULT': |
| entry[label['drv']] = 'DRV_DEFAULT_SETTING' |
| else: |
| temp_list = ['GPIO_DRV', entry[label['drv']]] |
| entry[label['drv']] = ''.join(temp_list) |
| |
| #pull enable |
| if entry[label['pullen']] == 'Y': |
| entry[label['pullen']] = 'GPIO_PULL_ENABLE' |
| else: |
| entry[label['pullen']] = 'GPIO_PULL_DISABLE' |
| #pull high:1 pull low:0 |
| if entry[label['pullsel']] == 'Y': |
| entry[label['pullsel']] = 'GPIO_PULL_UP' |
| else: |
| entry[label['pullsel']] = 'GPIO_PULL_DOWN' |
| |
| #IO dir out:1 in:0 |
| if entry[label['dir']] == 'OUT': |
| entry[label['dir']] = 'GPIO_DIR_OUT' |
| elif entry[label['dir']] == 'IN': |
| entry[label['dir']] = 'GPIO_DIR_IN' |
| |
| #output high:1 low:0 |
| if entry[label['out']] == 'NC': |
| entry[label['out']] = 'OUT_DEFAULT_SETTING' |
| elif entry[label['out']] == 'HIGH': |
| entry[label['out']] = 'GPIO_OUT_ONE' |
| elif entry[label['out']] == 'LOW': |
| entry[label['out']] = 'GPIO_OUT_ZERO' |
| |
| #smt |
| if entry[label['smt']] == 'Y': |
| entry[label['smt']] = 'SMT_DEFAULT_SETTING' |
| else: |
| entry[label['smt']] = 'GPIO_SMT_DISABLE' |
| |
| #ies |
| if entry[label['ies']] == 'Y': |
| entry[label['ies']] = 'IES_DEFAULT_SETTING' |
| else: |
| entry[label['ies']] = 'GPIO_IES_DISABLE' |
| |
| #print entry |
| #print '' |
| #discard GPIO name, user, Application info |
| entry = entry[0:len(label)-2] |
| entry.pop(label['name']) |
| final_setting.append(entry) |
| |
| def gen_gpio_code(setting, outfile): |
| with open(os.path.abspath(outfile), 'w') as fw: |
| |
| fw.write("#ifndef __GPIO_SETTING_H_\n") |
| fw.write("#define __GPIO_SETTING_H_\n\n") |
| |
| fw.write("#define USE_AS_GPIO\t\t\t\t(1)\n") |
| fw.write("#define USE_AS_EINT\t\t\t\t(0)\n\n") |
| |
| fw.write("#define DEFAULT_SETTING\t\t\t\t\t(-1)\n") |
| fw.write("#define DRV_DEFAULT_SETTING\t\t\t\t(DEFAULT_SETTING)\n") |
| fw.write("#define PULL_EN_DEFAULT_SETTING\t\t\t(DEFAULT_SETTING)\n") |
| fw.write("#define PULL_SEL_DEFAULT_SETTING\t\t(DEFAULT_SETTING)\n") |
| fw.write("#define OUT_DEFAULT_SETTING\t\t\t\t(DEFAULT_SETTING)\n") |
| fw.write("#define SMT_DEFAULT_SETTING\t\t\t\t(DEFAULT_SETTING)\n") |
| fw.write("#define IES_DEFAULT_SETTING\t\t\t\t(DEFAULT_SETTING)\n\n") |
| |
| fw.write("struct gpio_setting {\n") |
| fw.write("\tuint32_t number;\n") |
| fw.write("\tuint32_t usage;\n") |
| fw.write("\tuint32_t mode;\n") |
| fw.write("\tuint32_t drv;\n") |
| fw.write("\tuint32_t pull_en;\n") |
| fw.write("\tuint32_t pull_sel;\n") |
| fw.write("\tuint32_t dir;\n") |
| fw.write("\tuint32_t out;\n") |
| fw.write("\tuint32_t smt;\n") |
| fw.write("\tuint32_t ies;\n") |
| fw.write("};\n\n") |
| |
| fw.write("struct gpio_setting gpio_golden_table[] = {\n") |
| for entry in setting: |
| fw.write("\t{") |
| for val in entry: |
| fw.write("{0}, ".format(val)) |
| |
| fw.write("},\n") |
| fw.write("};\n") |
| |
| fw.write("#endif") |
| |
| def main(): |
| print("Apollo GPIO code generator:v0.1") |
| parser = argparse.ArgumentParser(description='Apollo GPIO generator') |
| |
| parser.add_argument('--input', '-i', required=True, |
| help='Provide the input file') |
| parser.add_argument('--output', '-o', required=False, |
| help='Provide the output path') |
| |
| args = parser.parse_args() |
| |
| if not os.path.isfile(args.input): |
| #args.input = 'V:\gpio\MT6297_EVB_MTK1353_Golden_Setting.csv' |
| print ("Input file not exist.") |
| sys.exit(0) |
| |
| if args.output is None or not os.path.isdir(args.output): |
| outpath = os.path.dirname(os.path.abspath(__file__)) |
| # print "Output path set as:" ,outpath |
| else: |
| outpath = os.path.abspath(args.output) |
| # print "args.output:",args.output |
| # print "outpath:", outpath |
| |
| with open(os.path.abspath(args.input), 'r') as fr: |
| print ("input file:",args.input) |
| for line in fr: |
| MatchObj = re.match('(.*):[ ]*(\w+)', line) |
| if MatchObj is not None: |
| filename = MatchObj.group(2) |
| print ("Golden setting table:", filename) |
| filename = 'gpio_golden' |
| temp = [os.path.join(outpath, filename), '.h'] |
| outfile = ''.join(temp) |
| print ("output file:", outfile) |
| MatchObject = re.search('(?<=\d,)\w+', line) |
| if MatchObject is not None: |
| line = line.strip() |
| setting_entry_process(line) |
| else: |
| continue |
| |
| gen_gpio_code(final_setting, outfile) |
| |
| if __name__ == '__main__': |
| main() |