最近在玩一个开源的AD9361项目,AD9361采用纯逻辑配置,不需要ARM或者MicroBlaze。其中,先是用AD9361 Evaluation Software生成配置脚本,再转换成ad9361_lut.v。
在网上查了一圈,有个转换工具叫bit_converter,也有人用python写了转换脚本,但一点下载,都是要收费的。
好在皇天不负有心人,哈哈,我给《AD9361-FM-Radio-Verilog-CMOS-main》的作者发了封邮件,然后在GITHUB上留言,大佬速度回复,并提供了转换的python脚本。
extract.py代码如下:
import re# with open('default.txt', 'r') as f:
with open('my_lut.txt', 'r') as f:for line in f.readlines():scan_line = re.search('ad9361_cmd_data[\s]=[\s]{(.*)};', line)if scan_line:print(scan_line.group(1).strip().replace('\t', ' '))
main.py代码如下:
import re
from art import *lut_str = "\t\t13'd{:<4d}:\tad9361_cmd_data\t= {{1'b{}, 10'h{}, 8'h{}}};"tmp = '=' * 50 + '\n'
# tmp += text2art("SITLINV")
# tmp += '=' * 50 + '\n'
tmp += text2art("BRIANSUNE")
tmp += '=' * 50 + '\n'
tmp += 'File Name: ad9361_lut.v' + '\n'
tmp += '=' * 50 + '\n'
tmp += 'Programed By: BrianSune\n'
tmp += 'Contact: briansune@gmail.com\n'output_str = ''.join('// {}\n'.format(tps) for tps in tmp.split('\n'))
output_str += '\nfunction [18 : 0] ad9361_cmd_data;\n'
output_str += 'input [12 : 0] index;\n'
output_str += '''
begincase(index)13'd0 :\tad9361_cmd_data\t= {1'b1, 10'h000, 8'h00};
'''# print(output_str)lut_idx = 1
output_str2 = ''check_list = []
wait_list = []path = r'E:\资料\AD9361 FM RADIO\AD9361-FM-Radio-Verilog-CMOS-main\adi_tool'
# path = r'C:\Users\briansuneZ\Desktop\golden_ad9361_bist_lvds_rx'
# path = r'C:\Users\briansuneZ\Desktop\golden_ad9361_bist_loop_lvds'
path += r'\ad9361_ini'with open(path, 'r') as f:for line in f.readlines():wr_re = re.search(r'SPIWrite[\s]+([0-9A-F]+),([0-9A-F]+)[\s]*[/ ]*(.*)', line)rd_re = re.search(r'SPIRead[\s]+([0-9A-F]+)[\s]*[/ ]*(.*)', line)cal_re = re.search(r'WAIT_CALDONE[\s]+.*[/]+ (.*0x([0-9A-F]+).*)', line)if wr_re:# print(wr_re.groups())tmp_str = lut_str.format(lut_idx, 1, wr_re.group(1), wr_re.group(2))if wr_re.group(3):tmp_str += '\t// {}'.format(wr_re.group(3))tmp_str += '\n'output_str2 += tmp_strlut_idx += 1if rd_re:# print(rd_re.groups())tmp_str = lut_str.format(lut_idx, 0, rd_re.group(1), '00')if rd_re.group(2):tmp_str += '\t// {}'.format(rd_re.group(2))tmp_str += '\n'output_str2 += tmp_strcheck_list.append('{} {}'.format(lut_idx, rd_re.group(2)))lut_idx += 1if cal_re:# print(cal_re.groups())tmp_str = lut_str.format(lut_idx, 0, cal_re.group(2), '00')tmp_str += '\t// {}\n'.format(cal_re.group(1))output_str2 += tmp_strcheck_list.append('{} {}'.format(lut_idx, cal_re.group(1)))lut_idx += 1if line.strip() == 'ReadPartNumber':output_str2 += lut_str.format(lut_idx, 0, '037', '00') + '\t// ReadPartNumber\n'check_list.append('{} {}'.format(lut_idx, 'part num'))lut_idx += 1if line[0:4] == 'WAIT':wait_list.append('{} {}'.format(lut_idx - 1, line.strip()))output_str += output_str2
output_str += lut_str.format(lut_idx, 1, '014', '68')
output_str += '''endcase
end
endfunction
'''# print(output_str)
with open('ad9361_lut.v', 'w') as wf:wf.write(output_str)[print(ck) for ck in check_list]
print('\n\n')
[print(wi) for wi in wait_list]
如果运行报错,要安装一下art,命令如下:
pip install art
最后,向briansune大佬致敬,还是国际友人有开源精神!