时间:2024-08-09 来源:网络搜集 关于我们 0
大侠好,欢迎来到FPGA技术江湖,江湖偌大,相见即是缘分。大侠可以关注FPGA技术江湖,在“闯荡江湖”、"行侠仗义"栏里获取其他感兴趣的资源,或者一起煮酒言欢。“煮酒言欢”进入IC技术圈,这里有近50个IC技术公众号。
大侠好,阿Q来也,今天是第二次和各位见面,请各位大侠多多关照。今天给各位大侠带来一篇项目开发经验分享“基于JESD204B的LMK04821芯片项目开发”第二篇,这是本人实打实的项目开发经验,希望可以给有需要的大侠提供一些参考学习作用。以后机会多多,慢慢分享一些项目开发以及学习方面的内容,欢迎各位大侠一起切磋交流。也欢迎进群交流,文章末尾有进群方式。话不多说,上货。204B实战应用-LMK04821代码详解(二)
通过阅读LMK04821数据手册,我们可以从中知道,可以通过SPI协议对LMK04821进行寄存器的配置工作,进而实现我们设计所需要的功能。
SPI协议部分,咱们可以用3线,或者4线,在本次设计中,使用3线。关于SPI的时序部分,这儿就不再赘述,手册里面都有详细的描述。
图1图2
如图2所示,就是配置LMK04821存器的单元,信号定义如下:
1、cfg_clk:系统时钟;
2、cfg_rst:系统复位;
3、通过VIO控制的信号,这组信号存在的目的在于方便检测自己配置寄存器的正确性。
vio_cfg_en:配置寄存器使能信号;
vio_cfg_wr:配置寄存器读写使能,0写1读;
vio_cfg_addr:配置的寄存器地址;
vio_cfg_wdata:寄存器中配置的值;
addr_118_data:预留信号,模块中没有用;
我们在配置LMK04821寄存器时,要验证配置寄存器操作是否正确,就要有写有读,在对应的寄存器内写入对应的数值,然后进行读操作,观察正确性。本次设计是在vivado环境下进行设计,通过添加VIO的IP核,来控制读写操作。同时,添加ILA配合VIO来进行读写数据操作的观测。别的开发环境下思路一样。
该组信号仅在回读寄存器时使用,目的是为了验证寄存器读写正确性。
图3
4、lmk_rst:LMK04821复位信号,用于复位LMK04821,直接和LMK04821芯片相连;
5、3线制SPI信号:
lmk_spi_csn:片选;
lmk_spi_sdio:数据;
lmk_spi_clk:时钟;
6、可编程管教:主要和LMK04821内部的PLL相关,本次设计中默认为0;
lmk_clk_sel0 :sel0;
lmk_clk_sel1 :sel1;
在本次设计中,SPI配置数据buffer,data_reg为24bit,r_w占1bit,箭头1所指包含W1、W2以及地址位占13bit,具体见SPI时序图;箭头2所指数据位8bit。
图4
根据图5我们可以知道,要配置LMK04821我们需要配置126个寄存器,这126个寄存器来源参见第一章实战记录。
其中,126个寄存器包含必须要配的寄存器、一些无关紧要的寄存器、以及功能实现所需要的寄存器等,有些寄存器需要配置多次。
图5
设计中,我们需要按照顺序配置126个寄存器,也就是说SPI要执行126次。因此,在代码实现过程中,注意寄存器配置的顺序,并且保证每个寄存器都准确无误的配置完成,才能进行下一个寄存器的配置。如果在设计中,要求LMK004821实现不同的功能,当配置的寄存器个数不一致时,在v文件中更改图6所示的参数即可。
图6
如下:是LMK04821配置的模块,读者可以作为参考。
代码区(参考代码):
//###########################################################################//// Copyright (C) 2017, JSZX, Co. Ltd. All Rights Reserved.//###########################################################################////-- Project Name ://-- File Name : lmk04821_spi//-- Description ://###########################################################################////---------------------------Modification History----------------------------////-- Date By Ver Comment//-- 12/04/2017 hhh 1.0 Create new//===================================================================//-- End Revision//===================================================================`timescale 1ns / 1psmodule lmk04821_spi( input cfg_clk , //<=10MHz input cfg_rst , input vio_cfg_en , input vio_cfg_wr ,//0,write;1,read; input [12:0] vio_cfg_addr , input [07:0] vio_cfg_wdata , input [07:0] addr_118_data , input r_w , input lmk_cfgen , output lmk_rst , output lmk_spi_csn , inout tri lmk_spi_sdio , output lmk_spi_clk , output lmk_clk_sel0 , output lmk_clk_sel1 , output reg regdatareadvalid , output reg [7:0] regdataread , output reg lmk_cfgdone = 1b0 ); //parameter defination parameter NUM_REG = 8d126 ;//需要配置的寄存器个数 parameter CFG_DONE_DLY = 32hF4240 ;//100ms@10Mhz; //====================================================================// //----------------------internal signals------------------------------// //====================================================================// reg [00:0] lmk_cfgen_d0 ; reg [00:0] lmk_cfgen_d1 ; reg [00:0] lmk_cfgen_d2 ; reg [00:0] vio_cfg_en_d0 ; reg [00:0] vio_cfg_en_d1 ; reg [00:0] vio_cfg_en_d2 ; reg [07:0] cnt_clk ;// 每个寄存器需要的时钟数计数器 reg [07:0] cnt_reg ;// 需要配置的寄存器计数器,最多255个! reg [23:0] data_reg ; reg [00:0] load_p ; reg [00:0] load_p_d0 ; reg [35:0] mid_data_o ; reg [35:0] mid_csn_o ; reg [00:0] spi_sdo ; reg [00:0] spi_cs_n ; wire[00:0] spi_sdi ; reg [05:0] sdo_cnt ;// //====================================================================//// //-----------------------------ila debug------------------------------//// //====================================================================//// //ila_spi// ila_spi ila_spi(// .clk ( cfg_clk ),//// .probe0 ( cnt_clk ),//8// .probe1 ( cnt_reg ),//8// .probe2 ( data_reg ),//24// .probe3 ( load_p ),//1// .probe4 ( sdo_cnt ),//6// .probe5 ( spi_cs_n ),//1// .probe6 ( spi_sdi ),//1// .probe7 ( spi_sdo ),//1// .probe8 ( lmk_cfgen_d1 ) //1// ); //====================================================================// //--------------------------main process------------------------------// //====================================================================// //lmk_clk_sel assign lmk_clk_sel0= 1b0 ; assign lmk_clk_sel1= 1b0 ; //spi signals; assign lmk_rst = cfg_rst ; assign lmk_spi_clk = (spi_cs_n) ? 1b0 : ~cfg_clk ; assign lmk_spi_csn = spi_cs_n ; assign spi_sdi = lmk_spi_sdio; assign lmk_spi_sdio= (data_reg[23]==1b1 && sdo_cnt>6h18)? 1bz : spi_sdo ; //lmk_cfgen_d0/lmk_cfgen_d1/lmk_cfgen_d2/load_p_d0 always @(posedge cfg_clk or posedge cfg_rst) begin if(cfg_rst==1b1) begin lmk_cfgen_d0 <= 1b0 ; lmk_cfgen_d1 <= 1b0 ; lmk_cfgen_d2 <= 1b0 ; load_p_d0 <= 1b0 ; vio_cfg_en_d0 <= 1b0 ; vio_cfg_en_d1 <= 1b0 ; vio_cfg_en_d2 <= 1b0 ; end else begin lmk_cfgen_d0 <= lmk_cfgen ; lmk_cfgen_d1 <= lmk_cfgen_d0 ; lmk_cfgen_d2 <= lmk_cfgen_d1 ; load_p_d0 <= load_p ; vio_cfg_en_d0 <= vio_cfg_en ; vio_cfg_en_d1 <= vio_cfg_en_d0 ; vio_cfg_en_d2 <= vio_cfg_en_d1 ; end end //load_p/cnt_reg/cnt_clk always @(posedge cfg_clk or posedge cfg_rst) begin if(cfg_rst==1b1) begin cnt_reg <= 8d0 ; cnt_clk <= 8d36 ; load_p <= 1b0 ; end else begin if(lmk_cfgen_d1==1b1 && lmk_cfgen_d2==1b0) begin cnt_clk <= 8d0 ; cnt_reg <= 8d0 ; load_p <= 1b0 ; end else if((cnt_clk==8d36)&&(cnt_reg<NUM_REG)) begin cnt_clk <= 8d0 ; cnt_reg <= cnt_reg + 8h1 ; load_p <= 1b1 ; end else begin load_p <= 1b0 ; if(cnt_clk==8d36)//cnt_reg==NUM_REG begin cnt_clk <= 8d0 ; cnt_reg <= cnt_reg ; end else begin cnt_clk <= cnt_clk + 8h1 ; cnt_reg <= cnt_reg ; end end end end //data_reg:VCO0,1930~2075;VCO1,2920~3080; always @(posedge cfg_clk or posedge cfg_rst) begin if(cfg_rst==1b1) begin data_reg <= 24h80_0000; end else begin case(cnt_reg)//VCO_2Ghz; // Serial Port Configuration 8d1 : data_reg <= {r_w,23h0000_80} ;//soft reset 8d2 : data_reg <= {r_w,23h0000_00} ;// 8d3 : data_reg <= {r_w,23h0100_04} ;//500Mhz;DCLKout0: input and output drive level;device clock out divider values 8d4 : data_reg <= {r_w,23h0101_55} ;//controls the digital delay high and low count values for the device clock outputs 8d5 : data_reg <= {r_w,23h0103_00} ;//registers control the analog delay properties for the device clocks 8d6 : data_reg <= {r_w,23h0104_22} ;//set the half step for the device clock, the SYSREF output MUX, the SYSREF clock digital delay,and half step 8d7 : data_reg <= {r_w,23h0105_00} ;//set the analog delay parameters for the SYSREF outputs 8d8 : data_reg <= {r_w,23h0106_70} ;//controls the power down functions for the digital delay, glitchless half step 8d9 : data_reg <= {r_w,23h0107_15} ;//configure the output polarity, and format:11:LVDS;15:LVPECL16; 8d10 : data_reg <= {r_w,23h0108_10} ;//125Mhz;DCLKout2;V7_IO_CLK2; 8d11 : data_reg <= {r_w,23h0109_55} ; 8d12 : data_reg <= {r_w,23h010B_00} ; 8d13 : data_reg <= {r_w,23h010C_22} ;//bit[5]:SDCLKoutY_MUX;0, Device clock output; 8d14 : data_reg <= {r_w,23h010D_00} ; 8d15 : data_reg <= {r_w,23h010E_70} ;//bit[3]:0,enable;bit[4]:powerdown; 8d16 : data_reg <= {r_w,23h010F_11} ;//11:LVDS;15:LVPECL16 8d17 : data_reg <= {r_w,23h0110_10} ;//125Mhz;DCLKout4; 8d18 : data_reg <= {r_w,23h0111_55} ; 8d19 : data_reg <= {r_w,23h0113_00} ; 8d20 : data_reg <= {r_w,23h0114_22} ;//bit[5]:SDCLKoutY_MUX;0, Device clock output; 8d21 : data_reg <= {r_w,23h0115_00} ; 8d22 : data_reg <= {r_w,23h0116_70} ;//bit[3]:0,enable;bit[4]:powerdown; 8d23 : data_reg <= {r_w,23h0117_11} ;//11:LVDS;15:LVPECL16 8d24 : data_reg <= {r_w,23h0118_04} ;//500Mhz;DCLKout6,FPGA;V7_IO_CLK0; 8d25 : data_reg <= {r_w,23h0119_55} ; 8d26 : data_reg <= {r_w,23h011B_00} ; 8d27 : data_reg <= {r_w,23h011C_22} ;//bit[5]:SDCLKoutY_MUX;0, Device clock output; 8d28 : data_reg <= {r_w,23h011D_00} ; 8d29 : data_reg <= {r_w,23h011E_70} ; 8d30 : data_reg <= {r_w,23h011F_11} ;//11:LVDS;15:LVPECL16 8d31 : data_reg <= {r_w,23h0120_10} ;//125Mhz;DCLKout8,FPGA MGT114 CLOCK; 8d32 : data_reg <= {r_w,23h0121_55} ; 8d33 : data_reg <= {r_w,23h0123_00} ; 8d34 : data_reg <= {r_w,23h0124_02} ;//bit[5]:SDCLKoutY_MUX;0, Device clock output; 8d35 : data_reg <= {r_w,23h0125_00} ; 8d36 : data_reg <= {r_w,23h0126_70} ; 8d37 : data_reg <= {r_w,23h0127_11} ;//11:LVDS;15:LVPECL16 8d38 : data_reg <= {r_w,23h0128_10} ;//125Mhz;DCLKout10,FPGA MGT116 CLOCK; 8d39 : data_reg <= {r_w,23h0129_55} ; 8d40 : data_reg <= {r_w,23h012B_00} ; 8d41 : data_reg <= {r_w,23h012C_02} ;//bit[5]:SDCLKoutY_MUX;0, Device clock output; 8d42 : data_reg <= {r_w,23h012D_00} ; 8d43 : data_reg <= {r_w,23h012E_70} ; 8d44 : data_reg <= {r_w,23h012F_11} ;//11:LVDS;15:LVPECL16 8d45 : data_reg <= {r_w,23h0130_04} ;//500Mhz;DCLKout12; 8d46 : data_reg <= {r_w,23h0131_55} ; 8d47 : data_reg <= {r_w,23h0133_00} ; 8d48 : data_reg <= {r_w,23h0134_22} ; 8d49 : data_reg <= {r_w,23h0135_00} ; 8d50 : data_reg <= {r_w,23h0136_70} ; 8d51 : data_reg <= {r_w,23h0137_15} ;//11:LVDS;15:LVPECL16 8d52 : data_reg <= {r_w,23h0138_00} ;//selects the clock distribution source, and OSCout parameters;VCO0; 8d53 : data_reg <= {r_w,23h0139_03} ;//sets the source for the SYSREF outputs 8d54 : data_reg <= {r_w,23h013A_00} ;//SYSREF_DIV[12:8] DIV register 1;sysref 2000M/160=12.5Mhz; 8d55 : data_reg <= {r_w,23h013B_A0} ;//SYSREF_DIV[7:0] DIV register 0; 8d56 : data_reg <= {r_w,23h013C_08} ;//set the delay of the SYSREF digital delay value[12:8] 8d57 : data_reg <= {r_w,23h013D_00} ;//set the delay of the SYSREF digital delay value[7:0] 8d58 : data_reg <= {r_w,23h013E_03} ;//sets the number of SYSREF pulses if SYSREF is not in continuous mode; 8d59 : data_reg <= {r_w,23h013F_04} ;//controls the feedback feature 8d60 : data_reg <= {r_w,23h0140_01} ;//13-OSCin PD; powerdown controls for OSCin and SYSREF functions;bit[0]:Powerdown SYSREF pulse generator; 8d61 : data_reg <= {r_w,23h0141_FF} ;//enables dynamic digital delay for enabled device clocks 8d62 : data_reg <= {r_w,23h0142_00} ;//sets the number of dynamic digital delay adjustments occur 8d63 : data_reg <= {r_w,23h0143_91} ;//sets general SYNC parameters such as polarization, and mode 8d64 : data_reg <= {r_w,23h0144_00} ;//prevent a clock output from being synchronized or interrupted by a SYNC event or when outputting SYSREF 8d65 : data_reg <= {r_w,23h0145_7F} ;//Always program this register to value 127 8d66 : data_reg <= {r_w,23h0171_AA} ;// 8d67 : data_reg <= {r_w,23h0172_02} ;// 8d68 : data_reg <= {r_w,23h0173_00} ;//bit[6]PLL2_PRE_PD; bit[5]PLL2_PD; 8d70 : data_reg <= {r_w,23h017C_15} ;//OPT_REG_1:21; 8d71 : data_reg <= {r_w,23h017D_33} ;//OPT_REG_2:51; 8d72 : data_reg <= {r_w,23h0182_00} ; 8d73 : data_reg <= {r_w,23h0183_00} ; 8d74 : data_reg <= {r_w,23h0184_00} ; 8d75 : data_reg <= {r_w,23h0185_00} ; 8d76 : data_reg <= {r_w,23h0188_00} ; 8d77 : data_reg <= {r_w,23h0146_38} ;//CLKin enable and type controls. 8d78 : data_reg <= {r_w,23h0147_02} ;//CLKin_SEL_MODE. pin select mode; 8d79 : data_reg <= {r_w,23h0148_02} ;//CLKin_SEL0 controls 8d80 : data_reg <= {r_w,23h0149_42} ;//CLKin_SEL1 controls and register readback SDIO pin type 8d81 : data_reg <= {r_w,23h014A_02} ;//contains control of the RESET pin 8d82 : data_reg <= {r_w,23h014B_16} ;//contains the holdover functions:start; 8d83 : data_reg <= {r_w,23h014C_00} ;// 8d84 : data_reg <= {r_w,23h014D_00} ;// 8d85 : data_reg <= {r_w,23h014E_C0} ;// 8d86 : data_reg <= {r_w,23h014F_7F} ;// 8d87 : data_reg <= {r_w,23h0150_03} ;// 8d88 : data_reg <= {r_w,23h0151_02} ;// 8d89 : data_reg <= {r_w,23h0152_00} ;//contains the holdover functions:end; 8d90 : data_reg <= {r_w,23h0153_00} ;//CLKin0_R[13:8] 8d91 : data_reg <= {r_w,23h0154_01} ;//CLKin0_R[7:0] 8d92 : data_reg <= {r_w,23h0155_00} ;//CLKin1_R[13:8] 8d93 : data_reg <= {r_w,23h0156_40} ;//CLKin1_R[7:0] 8d94 : data_reg <= {r_w,23h0157_00} ;//CLKin2_R[13:8] 8d95 : data_reg <= {r_w,23h0158_40} ;//CLKin2_R[7:0] 8d96 : data_reg <= {r_w,23h0159_00} ;//PLL1_N[13:8] 8d97 : data_reg <= {r_w,23h015A_01} ;//PLL1_N[7:0] 8d98 : data_reg <= {r_w,23h015B_D4} ;//PLL1 phase detector 8d99 : data_reg <= {r_w,23h015C_20} ;//PLL1_DLD_CNT[13:8] 8d100 : data_reg <= {r_w,23h015D_00} ;//PLL1_DLD_CNT[7:0] 8d101 : data_reg <= {r_w,23h015E_00} ;//contains the delay value for PLL1 N and R delays. 8d102 : data_reg <= {r_w,23h015F_0B} ;//configures the PLL1 LD pin 8d103 : data_reg <= {r_w,23h0160_00} ;//PLL2_R[11:8] 8d104 : data_reg <= {r_w,23h0161_01} ;//PLL2_R[7:0] 8d105 : data_reg <= {r_w,23h0162_44} ;//sets other PLL2 functions:[7:5]:PLL2_P;[4:2]:OSCin_FREQ;[1]:PLL2_XTAL_EN;[0]:PLL2_REF_2X_EN; 8d106 : data_reg <= {r_w,23h0163_00} ;//PLL2_N_CAL[17:16] 8d107 : data_reg <= {r_w,23h0164_00} ;//PLL2_N_CAL[15:8] 8d108 : data_reg <= {r_w,23h0165_0C} ;//PLL2_N_CAL[7:0] 8d109 : data_reg <= {r_w,23h0166_00} ;//PLL2_N[17:16],MSB; 8d110 : data_reg <= {r_w,23h0167_00} ;//PLL2_N[15:8],---; 8d111 : data_reg <= {r_w,23h0168_0A} ;//PLL2_N[7:0],LSB; 8d112 : data_reg <= {r_w,23h0169_59} ;//controls the PLL2 phase detector 8d113 : data_reg <= {r_w,23h016A_60} ;// 8d114 : data_reg <= {r_w,23h016B_00} ;// 8d115 : data_reg <= {r_w,23h016C_00} ;// 8d116 : data_reg <= {r_w,23h016D_00} ;// 8d117 : data_reg <= {r_w,23h016E_13} ;// 8d118 : data_reg <= {r_w,23h0143_90} ;// 8d119 : data_reg <= {r_w,23h0139_00} ;// 8d120 : data_reg <= {r_w,23h0143_B0} ;// 8d121 : data_reg <= {r_w,23h0143_90} ;// 8d122 : data_reg <= {r_w,23h0144_FF} ;// 8d123 : data_reg <= {r_w,23h0143_10} ;// 8d124 : data_reg <= {r_w,23h0143_11} ;// 8d125 : data_reg <= {r_w,23h0139_03} ;// 8d126 : data_reg <= {1b1,23h0002_00} ;// default : data_reg <= 24h80_0000 ; endcase end end //spi_sdo/spi_cs_n/mid_data_o/mid_csn_o/mid_data_o/vio_cfg_cnt always @(posedge cfg_clk or posedge cfg_rst) begin if(cfg_rst==1b1) begin spi_sdo <= 1b0; spi_cs_n <= 1b1; mid_data_o <= 36h0; mid_csn_o <= 36hFFFFFFFFF; sdo_cnt <= 6b0 ; end else begin if(load_p_d0==1b1) begin spi_sdo <= 1b0; mid_data_o <= {data_reg[23:0],12hfff};//r_w:0 write;1 read; spi_cs_n <= 1b1; mid_csn_o <= {24h0,12hFFF}; sdo_cnt <= 6h1 ; end else if(vio_cfg_en_d1==1b1 && vio_cfg_en_d2==1b0) begin spi_sdo <= 1b0; mid_data_o <= {vio_cfg_wr,2b00,vio_cfg_addr,vio_cfg_wdata,12hfff};//r_w:0 write;1 read; spi_cs_n <= 1b1; mid_csn_o <= {24h0,12hFFF}; sdo_cnt <= 6h1 ; end else begin spi_sdo <= mid_data_o[35]; mid_data_o <= {mid_data_o[34:0],1b0}; spi_cs_n <= mid_csn_o[35]; mid_csn_o <= {mid_csn_o[34:0],1b1}; if(sdo_cnt<6h3f) begin sdo_cnt <= sdo_cnt + 6h1 ; end else begin sdo_cnt <= sdo_cnt ; end end end end //regdatareadvalid/regdataread always @(posedge cfg_clk or posedge cfg_rst) begin if(cfg_rst==1b1) begin regdatareadvalid <= 1b0 ; regdataread <= 8b0 ; end else begin if(spi_cs_n==1b0) begin if(data_reg[23]==1b1) begin if(sdo_cnt>6d18 && sdo_cnt<6d25)//2-17;18-25; begin regdatareadvalid <= 1b0 ; regdataread <= {regdataread[6:0],spi_sdi}; end else if(sdo_cnt==6d25) begin regdatareadvalid <= 1b1 ; regdataread <= {regdataread[6:0],spi_sdi}; end else begin regdatareadvalid <= 1b0 ; regdataread <= regdataread ; end end else begin regdatareadvalid <= 1b0 ; regdataread <= regdataread ; end end else begin regdatareadvalid <= 1b0 ; regdataread <= regdataread ; end end end //lmk_cfgdone always @(posedge cfg_clk or posedge cfg_rst) begin if(cfg_rst) begin lmk_cfgdone <= 1b0 ; end else begin if(cnt_reg>=NUM_REG) begin lmk_cfgdone <= 1b1 ; end else begin lmk_cfgdone <= 1b0 ; end end end //====================================================================// //------------------------------- end ------------------------------// //====================================================================//endmodule下一篇,将详细介绍jesd_204B IP核应用的相关知识,各位大侠,尽请关注。
end
往期精选
FPGA时序分析及约束专题课,Quartus、ISE、Vivado全涉及,线上可随时报名参加!
FPGA图像处理专题课,Quartus、ISE、Vivado全涉及,线上可随时报名参加
FPGA就业班,2023.02.08开班,课程内容丰富,系统性学习FPGA,高薪就业,线上线下同步!
FPGA技术江湖广发江湖帖
无广告纯净模式,给技术交流一片净土,从初学小白到行业精英业界大佬等,从军工领域到民用企业等,从通信、图像处理到人工智能等各个方向应有尽有,QQ微信双选,FPGA技术江湖打造最纯净最专业的技术交流学习平台。
FPGA技术江湖微信交流群
加群主微信,备注姓名+公司/学校+岗位/专业进群
FPGA技术江湖QQ交流群
备注姓名+公司/学校+岗位/专业进群