[FPGA] adc1118 FPGA控制源码公布

[复制链接]
490|0
 楼主| feihufuture 发表于 2020-8-26 12:56 | 显示全部楼层 |阅读模式
本帖最后由 feihufuture 于 2020-8-26 13:00 编辑

[payamount]1.00[/payamount]
[pay]



  1.         //------------------------------------------------------------------------------
  2.         
  3.         //------------------------------------------------------------------------------
  4.         module adc1118
  5.         #
  6.         (
  7.         parameter SPI_SCK_CYCLE = 50,
  8.         parameter SPI_BIT_NUM        = 16
  9.         )
  10.         (
  11.         input                                        i_clk,
  12.         input                                 i_rsto_power_on,
  13.         //
  14.         input        [1        :        0]                i_fpga_spi_cs_0_s,
  15.         output                                o_fpga_spi_sck,
  16.         output                                 o_fpga_spi_cs,
  17.         input                                        i_fpga_spi_miso,
  18.         output                                o_fpga_spi_mosi,
  19.         //
  20.         output                [15        :        0]         o_adc_c1_data,
  21.         output                [15        :        0]         o_adc_c2_data
  22.         );
  23.         
  24.         
  25.         reg         [7  : 0]         cs_cnt;
  26.         reg   [5 : 0]          ws;//work state
  27.         reg                                        fpga_spi_miso_d;
  28.         
  29.         
  30.         reg     [6:0]         basic_clk_cnt ;
  31.         reg     [4:0]         fpga_spi_clk_cnt;
  32.         reg     [3:0]         spi_byte_cnt ;
  33.         wire    [2:0]         spi_bit_index;

  34.         reg                                                fpga_spi_sck         =         0;
  35.         reg                                                fpga_spi_cs                =        1;
  36.         reg                                                fpga_spi_mosi        =        0;
  37.         
  38.         wire                                                sample_point;
  39.         reg                                                 sample_point_d;
  40.         reg         [15        :        0]         adc_c1_data;
  41.         reg         [15        :        0]         adc_c2_data;
  42.         reg         [15        :        0]         adc_c1_data_r;
  43.         reg         [15        :        0]         adc_c2_data_r;
  44.         
  45.         
  46.         wire                                         clk                                        =        i_clk;
  47.         wire                                         rsto_power_on                 =        i_rsto_power_on;
  48.         wire        [1        :        0]                fpga_spi_cs_0_s         =         i_fpga_spi_cs_0_s;
  49.         wire                                        fpga_spi_miso                 =        i_fpga_spi_miso;
  50.         //------------------------------------------------------------------------------
  51.         //datasheet: "The next time CS is taken low, data transmission starts with the currently buffered conversion result
  52.         //on the first SCLK rising edge. If DOUT/DRDY is low when data retrieval starts, the conversion buffer is
  53.         //already updated with a new result. Otherwise, if DOUT/DRDY is high, the same result from the previous data
  54.         //transmission cycle is read."
  55.         //
  56.         parameter         S_IDLE                                 =         6'b00_0001;
  57.         parameter        S_IN_RD                                =        6'b00_0010;
  58.         parameter        S_ADC_WR_FRONT                =        6'b00_0100;
  59.         parameter        S_ADC_WR_WAIT                =        6'b00_1000;
  60.         parameter        S_ADC_WR                                =         6'b01_0000;
  61.         parameter        S_ADC_WR_BACK                 =        6'b10_0000;
  62.         //
  63.         //
  64.         //8ms is 128sps of ADC.
  65.         //"fpga_spi_cs_0_s_cnt" is used to count the number of 0.6ms.
  66.         reg [9:0] fpga_spi_cs_0_s_cnt;
  67.         //
  68.         always @(posedge clk)
  69.         begin
  70.         if (~rsto_power_on)        fpga_spi_cs_0_s_cnt <= 10'd0;
  71.         else if(fpga_spi_cs_0_s == 2'b01) fpga_spi_cs_0_s_cnt <= fpga_spi_cs_0_s_cnt + 1;
  72.         else fpga_spi_cs_0_s_cnt <= fpga_spi_cs_0_s_cnt;
  73.         end
  74.         //
  75.         //channel_change_flag "0": send AN2-3 command; "1" send AN0-1 command.
  76.         //
  77.         reg channel_change_flag;
  78.         always @(posedge clk)
  79.         begin
  80.         if (~rsto_power_on) channel_change_flag <= 1'b0;
  81.         else if(fpga_spi_cs_0_s == 2'b01)
  82.                 begin
  83.                 if(&fpga_spi_cs_0_s_cnt) channel_change_flag <= ~channel_change_flag;
  84.                 end
  85.         end
  86.         //
  87.         //
  88.         always @(posedge clk)
  89.         begin
  90.         if (~rsto_power_on) ws <= S_IDLE;
  91.         else
  92.                 case(ws)
  93.                 S_IDLE                        :         ws <= S_IN_RD;
  94.                 //
  95.                 S_IN_RD                         :         if(fpga_spi_cs_0_s == 2'b01)
  96.                                                                         begin
  97.                                                                         if(&fpga_spi_cs_0_s_cnt[3:0]) ws <= S_ADC_WR_FRONT;
  98.                                                                         end
  99.                 //
  100.                 S_ADC_WR_FRONT        :         if(&cs_cnt) ws <= S_ADC_WR;
  101.                 //S_ADC_WR_WAIT        :        if(~fpga_spi_miso) ws <= S_ADC_WR;
  102.                 S_ADC_WR                        :         if((basic_clk_cnt == SPI_SCK_CYCLE - 1)&(fpga_spi_clk_cnt == SPI_BIT_NUM - 1)) ws <= S_ADC_WR_BACK;
  103.                 S_ADC_WR_BACK         :         if(&cs_cnt) ws <= S_IDLE;
  104.                 default: ;
  105.                 endcase
  106.         end
  107.         //cs of spi changes in the midle of cs_cnt, that is 256/2 * 10ns, about 1.28us keeping high or low.
  108.         always @(posedge clk)
  109.         begin
  110.         if (~rsto_power_on) cs_cnt <= 8'b0;
  111.         else
  112.                 case(ws)
  113.                 S_ADC_WR_FRONT,S_ADC_WR_BACK: cs_cnt <= cs_cnt + 1;
  114.                 default:  cs_cnt <= 8'b0;
  115.                 endcase
  116.         end
  117.         //
  118.         always @(posedge clk)
  119.         begin
  120.         if (~rsto_power_on) fpga_spi_miso_d <= 1'b1;
  121.         else fpga_spi_miso_d <= fpga_spi_miso;
  122.         end
  123.         //------------------------------------------------------------------------------
  124.         
  125.         
  126.         
  127.         
  128.         //------------------------------------------------------------------------------
  129.         //The datasheet of ADS1118 says: " When CS is taken high, the serial interface is reset, SCLK is ignored, and DOUT/DRDY
  130.         //enters a high-impedance state; as such, DOUT/DRDY cannot provide indication of data ready. "
  131.         //
  132.         parameter CS_FALL_LIMIT_CNT = 128;
  133.         parameter CS_RISE_LIMIT_CNT = 128;
  134.         //
  135.         assign o_fpga_spi_cs = fpga_spi_cs;
  136.         always @(posedge clk)
  137.         begin
  138.         if (~rsto_power_on) fpga_spi_cs <= 1'b1;
  139.         else
  140.                 case(ws)
  141.                         S_ADC_WR_BACK        :        if( cs_cnt == CS_FALL_LIMIT_CNT ) fpga_spi_cs <= 1'b1;
  142.                         S_ADC_WR_FRONT        :  if( cs_cnt == CS_RISE_LIMIT_CNT ) fpga_spi_cs <= 1'b0;
  143.                         default: ;
  144.                 endcase
  145.         end
  146.         //
  147.         //------------------------------------------------------------------------------
  148.         
  149.         
  150.         
  151.         
  152.         //------------------------------------------------------------------------------
  153.         //basic_clk_cnt: count the number of system clock within one spi clock.
  154.         //fpga_spi_clk_cnt: count the number of the spi clock; every spi clock is consisted of "fpga_spi_sck_cycle" cycles of the system clock "clk".
  155.         always @(posedge clk)
  156.         begin
  157.         if (~rsto_power_on)
  158.                 begin
  159.                 basic_clk_cnt <= 7'd0;
  160.                 fpga_spi_clk_cnt <= 5'd0;
  161.                 end
  162.         else
  163.                 case(ws)
  164.                 S_ADC_WR:
  165.                         begin
  166.                         if (basic_clk_cnt == SPI_SCK_CYCLE - 1)
  167.                                 begin
  168.                                 basic_clk_cnt <= 7'd0;
  169.                                 fpga_spi_clk_cnt <= fpga_spi_clk_cnt + 1;
  170.                                 end
  171.                         else basic_clk_cnt <= basic_clk_cnt + 1;
  172.                         end
  173.                 default:
  174.                         begin
  175.                         basic_clk_cnt <= 7'd0;
  176.                         fpga_spi_clk_cnt <= 5'd0;
  177.                         end
  178.                 endcase
  179.         end
  180.         //
  181.         assign spi_bit_index = fpga_spi_clk_cnt[2:0];
  182.         //
  183.         //spi_byte_cnt counts the byte number of spi data sending, while fpga_spi_clk_cnt counts the bit number.
  184.         always @(posedge clk)
  185.         begin
  186.         if (~rsto_power_on) spi_byte_cnt <= 4'd0;
  187.         else
  188.                 case(ws)
  189.                 S_ADC_WR:
  190.                         begin
  191.                         if ((basic_clk_cnt == SPI_SCK_CYCLE - 1)&(fpga_spi_clk_cnt[2:0] == 3'd7)) spi_byte_cnt <= spi_byte_cnt + 1;
  192.                         end
  193.                 default: spi_byte_cnt <= 4'd0;
  194.                 endcase
  195.         end
  196.         //
  197.         //------------------------------------------------------------------------------
  198.         
  199.         
  200.         
  201.         //------------------------------------------------------------------------------
  202.         assign o_fpga_spi_sck = fpga_spi_sck;
  203.         //
  204.         always @(posedge clk)
  205.         begin
  206.         if (~rsto_power_on) fpga_spi_sck <= 1'b0;
  207.         else
  208.                 case(ws)
  209.                         S_ADC_WR:
  210.                         //From the datasheet of ADC1118, says : " The device latches data on DIN on the falling edge of SCLK."
  211.                                 begin
  212.                                 if (basic_clk_cnt == 6'd1) fpga_spi_sck <= 1'b1;
  213.                                 else if (basic_clk_cnt == SPI_SCK_CYCLE/2) fpga_spi_sck <= 1'b0;
  214.                                 end
  215.                         default: fpga_spi_sck <= 1'b0;
  216.                 endcase
  217.         end
  218.         //------------------------------------------------------------------------------
  219.         
  220.         
  221.         
  222.         //------------------------------------------------------------------------------
  223.         //AN0 acquire voltage.
  224.         //Bits[14:12]        :        100         :         AINP = AIN0 and AINN = GND
  225.         //Bits[11:9]         :         001         :         FS = ±4.096V , (if Vin ≥ FS (2^15 – 1)/2^15  output code is 7FFFh )
  226.         //Bits[7:5]                :        111         :         860SPS
  227.         //Bit 4                        :        0                 :         ADC mode
  228.         //Bit 3                        :        1                 :         Pull-up resistor enabled on DOUT pin
  229.         //Bits[2:1]                :        01         :        Valid data, update the Config Register
  230.         //
  231.         parameter C1_CONFIG_BYTE0 = 8'b0011_0000;
  232.         parameter C1_CONFIG_BYTE1 = 8'b1000_1011;
  233.         //
  234.         parameter C2_CONFIG_BYTE0 = 8'b0000_0000;
  235.         parameter C2_CONFIG_BYTE1 = 8'b1000_1011;
  236.         //
  237.         assign o_fpga_spi_mosi = fpga_spi_mosi;
  238.         //
  239.         //
  240.         //
  241.         always @(posedge clk)
  242.         begin
  243.         if (~rsto_power_on) fpga_spi_mosi <= 1'b0 ;
  244.         else if(~channel_change_flag)//send AN2-3 command, and get AN2-3 data.
  245.                 case(ws)
  246.                 S_ADC_WR:
  247.                         begin
  248.                         if ((basic_clk_cnt == 6'd1)&(spi_byte_cnt == 4'd0))
  249.                                 begin
  250.                                 case (spi_bit_index)
  251.                                 3'd0 : fpga_spi_mosi <= C1_CONFIG_BYTE0[7];
  252.                                 3'd1 : fpga_spi_mosi <= C1_CONFIG_BYTE0[6];
  253.                                 3'd2 : fpga_spi_mosi <= C1_CONFIG_BYTE0[5];
  254.                                 3'd3 : fpga_spi_mosi <= C1_CONFIG_BYTE0[4];
  255.                                 3'd4 : fpga_spi_mosi <= C1_CONFIG_BYTE0[3];
  256.                                 3'd5 : fpga_spi_mosi <= C1_CONFIG_BYTE0[2];
  257.                                 3'd6 : fpga_spi_mosi <= C1_CONFIG_BYTE0[1];
  258.                                 3'd7 : fpga_spi_mosi <= C1_CONFIG_BYTE0[0];
  259.                                 default :;
  260.                                 endcase
  261.                                 end        
  262.                         else if ((basic_clk_cnt == 6'd1)&(spi_byte_cnt == 4'd1))
  263.                                 begin
  264.                                 case (spi_bit_index)
  265.                                 3'd0 : fpga_spi_mosi <= C1_CONFIG_BYTE1[7];
  266.                                 3'd1 : fpga_spi_mosi <= C1_CONFIG_BYTE1[6];
  267.                                 3'd2 : fpga_spi_mosi <= C1_CONFIG_BYTE1[5];
  268.                                 3'd3 : fpga_spi_mosi <= C1_CONFIG_BYTE1[4];
  269.                                 3'd4 : fpga_spi_mosi <= C1_CONFIG_BYTE1[3];
  270.                                 3'd5 : fpga_spi_mosi <= C1_CONFIG_BYTE1[2];
  271.                                 3'd6 : fpga_spi_mosi <= C1_CONFIG_BYTE1[1];
  272.                                 3'd7 : fpga_spi_mosi <= C1_CONFIG_BYTE1[0];
  273.                                 default :;                       
  274.                                 endcase
  275.                                 end
  276.                         end
  277.                 endcase
  278.         else
  279.                 case(ws)
  280.                 S_ADC_WR:
  281.                         begin
  282.                                 if ((basic_clk_cnt == 6'd1)&(spi_byte_cnt == 4'd0))
  283.                                 begin
  284.                                 case (spi_bit_index)
  285.                                 3'd0 : fpga_spi_mosi <= C2_CONFIG_BYTE0[7];
  286.                                 3'd1 : fpga_spi_mosi <= C2_CONFIG_BYTE0[6];
  287.                                 3'd2 : fpga_spi_mosi <= C2_CONFIG_BYTE0[5];
  288.                                 3'd3 : fpga_spi_mosi <= C2_CONFIG_BYTE0[4];
  289.                                 3'd4 : fpga_spi_mosi <= C2_CONFIG_BYTE0[3];
  290.                                 3'd5 : fpga_spi_mosi <= C2_CONFIG_BYTE0[2];
  291.                                 3'd6 : fpga_spi_mosi <= C2_CONFIG_BYTE0[1];
  292.                                 3'd7 : fpga_spi_mosi <= C2_CONFIG_BYTE0[0];
  293.                                 default :;
  294.                                 endcase
  295.                                 end        
  296.                         else if ((basic_clk_cnt == 6'd1)&(spi_byte_cnt == 4'd1))
  297.                                 begin
  298.                                 case (spi_bit_index)
  299.                                 3'd0 : fpga_spi_mosi <= C2_CONFIG_BYTE1[7];
  300.                                 3'd1 : fpga_spi_mosi <= C2_CONFIG_BYTE1[6];
  301.                                 3'd2 : fpga_spi_mosi <= C2_CONFIG_BYTE1[5];
  302.                                 3'd3 : fpga_spi_mosi <= C2_CONFIG_BYTE1[4];
  303.                                 3'd4 : fpga_spi_mosi <= C2_CONFIG_BYTE1[3];
  304.                                 3'd5 : fpga_spi_mosi <= C2_CONFIG_BYTE1[2];
  305.                                 3'd6 : fpga_spi_mosi <= C2_CONFIG_BYTE1[1];
  306.                                 3'd7 : fpga_spi_mosi <= C2_CONFIG_BYTE1[0];
  307.                                 default :;                       
  308.                                 endcase                          
  309.                                 end
  310.                         end
  311.                 endcase
  312.         end
  313.         //------------------------------------------------------------------------------
  314.         
  315.         
  316.         
  317.         //------------------------------------------------------------------------------
  318.         assign sample_point = (basic_clk_cnt == (SPI_SCK_CYCLE - 2)) ;
  319.         //
  320.         always @(posedge clk)
  321.         begin
  322.                 sample_point_d <= sample_point ;
  323.         end
  324.         //
  325.         assign o_adc_c1_data = adc_c1_data_r;
  326.         assign o_adc_c2_data = adc_c2_data_r;
  327.         //
  328.         //"adc_c1_data_r" is avoid of sending the changing data to the upper application layer.
  329.         always @(posedge clk)
  330.         begin
  331.         if (~rsto_power_on)
  332.                 begin
  333.                 adc_c1_data_r <= 16'b0;
  334.                 adc_c2_data_r <= 16'b0;
  335.                 end
  336.         else if(ws == S_ADC_WR_BACK)
  337.                 begin
  338.                 if(fpga_spi_cs_0_s_cnt > 10'd32)//128sps, the data will be ok after about 32 0.6s's period.
  339.                         begin
  340.                         adc_c1_data_r <= adc_c1_data;
  341.                         adc_c2_data_r <= adc_c2_data;
  342.                         end
  343.                 end
  344.         end
  345.         //
  346.         //From the datasheet of ADC1118, says : " Data on DOUT/DRDY are shifted out on the rising edge of SCLK.
  347.         //The MSB of the data (bit 15) on DOUT/DRDY is clocked out on the first rising edge of SCLK."
  348.         //
  349.         //the FPGA should acquire data using the falling edge of SCK.
  350.         //
  351.         //Below, FPGA acquire data a little time before the rising edge of SCK, that is a large time after the falling edge of SCK.
  352.         //
  353.         //Must pay attention: when configure channel 1, and read data of channel 2; configure channel 2, read channel 1.(this chip feature
  354.         //is data is valid in next cycle.)
  355.         //
  356.         //
  357.         always @(posedge clk)
  358.         begin
  359.         if (~rsto_power_on)
  360.                 begin
  361.                 adc_c1_data <= 16'b0;
  362.                 adc_c2_data <= 16'b0;
  363.                 end
  364.         else if(~channel_change_flag)
  365.                 case(ws)
  366.                 S_ADC_WR:
  367.                         begin
  368.                         case(spi_byte_cnt)
  369.                                 4'd0:
  370.                                         begin
  371.                                         if (sample_point)
  372.                                                 case (spi_bit_index)
  373.                                                 3'd0 : adc_c1_data[15]        <=        fpga_spi_miso_d;
  374.                                                 3'd1 : adc_c1_data[14]        <=        fpga_spi_miso_d;
  375.                                                 3'd2 : adc_c1_data[13]        <=        fpga_spi_miso_d;
  376.                                                 3'd3 : adc_c1_data[12]        <=        fpga_spi_miso_d;
  377.                                                 3'd4 : adc_c1_data[11]        <=        fpga_spi_miso_d;
  378.                                                 3'd5 : adc_c1_data[10]        <=        fpga_spi_miso_d;
  379.                                                 3'd6 : adc_c1_data[9]        <=        fpga_spi_miso_d;
  380.                                                 3'd7 : adc_c1_data[8]        <=        fpga_spi_miso_d;
  381.                                                 endcase
  382.                                         end
  383.                                 4'd1:        
  384.                                         begin
  385.                                         if (sample_point)
  386.                                                 case (spi_bit_index)
  387.                                                 3'd0 : adc_c1_data[7]                <=        fpga_spi_miso_d;
  388.                                                 3'd1 : adc_c1_data[6]                <=        fpga_spi_miso_d;
  389.                                                 3'd2 : adc_c1_data[5]                <=        fpga_spi_miso_d;
  390.                                                 3'd3 : adc_c1_data[4]                <=        fpga_spi_miso_d;
  391.                                                 3'd4 : adc_c1_data[3]                <=        fpga_spi_miso_d;
  392.                                                 3'd5 : adc_c1_data[2]                <=        fpga_spi_miso_d;
  393.                                                 3'd6 : adc_c1_data[1]                <=        fpga_spi_miso_d;
  394.                                                 3'd7 : adc_c1_data[0]                <=        fpga_spi_miso_d;
  395.                                                 endcase
  396.                                         end
  397.                                 default: ;
  398.                         endcase
  399.                         end
  400.                 endcase
  401.         else
  402.                 case(ws)
  403.                 S_ADC_WR:
  404.                         begin
  405.                         case(spi_byte_cnt)
  406.                                 4'd0:
  407.                                         begin
  408.                                         if (sample_point)
  409.                                                 case (spi_bit_index)
  410.                                                 3'd0 : adc_c2_data[15]        <=        fpga_spi_miso_d;
  411.                                                 3'd1 : adc_c2_data[14]        <=        fpga_spi_miso_d;
  412.                                                 3'd2 : adc_c2_data[13]        <=        fpga_spi_miso_d;
  413.                                                 3'd3 : adc_c2_data[12]        <=        fpga_spi_miso_d;
  414.                                                 3'd4 : adc_c2_data[11]        <=        fpga_spi_miso_d;
  415.                                                 3'd5 : adc_c2_data[10]        <=        fpga_spi_miso_d;
  416.                                                 3'd6 : adc_c2_data[9]                <=        fpga_spi_miso_d;
  417.                                                 3'd7 : adc_c2_data[8]                <=        fpga_spi_miso_d;
  418.                                                 endcase
  419.                                         end
  420.                                 4'd1:        
  421.                                         begin
  422.                                         if (sample_point)
  423.                                                 case (spi_bit_index)
  424.                                                 3'd0 : adc_c2_data[7]                <=        fpga_spi_miso_d;
  425.                                                 3'd1 : adc_c2_data[6]                <=        fpga_spi_miso_d;
  426.                                                 3'd2 : adc_c2_data[5]                <=        fpga_spi_miso_d;
  427.                                                 3'd3 : adc_c2_data[4]                <=        fpga_spi_miso_d;
  428.                                                 3'd4 : adc_c2_data[3]                <=        fpga_spi_miso_d;
  429.                                                 3'd5 : adc_c2_data[2]                <=        fpga_spi_miso_d;
  430.                                                 3'd6 : adc_c2_data[1]                <=        fpga_spi_miso_d;
  431.                                                 3'd7 : adc_c2_data[0]                <=        fpga_spi_miso_d;
  432.                                                 endcase
  433.                                         end
  434.                                 default: ;
  435.                         endcase
  436.                         end  
  437.                 default: ;
  438.                 endcase
  439.         end
  440.         //------------------------------------------------------------------------------
  441.         
  442.         
  443.         
  444.         endmodule
  445.         
  446.         
  447.         
  448.         
  449.         
  450.         
  451.         
  452.         
  453.         
  454.         

[/pay]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

171

主题

1029

帖子

101

粉丝
快速回复 在线客服 返回列表 返回顶部