1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

VerilogでSPI受信

Posted at

概要

ESP32マイコンをSPIマスター,FPGAをSPIスレーブとする構成で,SPIデータの受信を行うVerilogコードを紹介します.

SPI_受信ブロック図.dio.png

詳細はブログ 「FPGAでシンプルSPI受信」 にて紹介しておりますので,参考になれば幸いです!

Verilogソースコード

SPI_RECEIVER_32BIT
module SPI_RECEIVER_32BIT
  (input	wire      i_clk       
  ,input	wire      i_rst_n     
  ,input	wire      i_SPI_CLK   
  ,input	wire      i_SPI_CS_n  
  ,input	wire      i_SPI_MOSI  
  ,output	reg[31:0] o_data      
  ,output	reg       o_valid     
  );
    
  reg [2:0]	buf_clk;
  reg [2:0]	buf_ena;
  reg [2:0]	buf_dat;
  
  reg [31:0]rsv_data;
  wire      clk_rise;
  wire      ena_rise;
  wire      shift	;
    
  always@(posedge i_clk or negedge i_rst_n)begin
    if(!i_rst_n)begin
        buf_clk	<=3'b111;
        buf_ena	<=3'b111;
        buf_dat	<=3'b000;
    end else begin
        buf_clk	<={buf_clk[1:0] , i_SPI_CLK};
        buf_ena	<={buf_ena[1:0] , i_SPI_CS_n};
        buf_dat	<={buf_dat[1:0] , i_SPI_MOSI};
    end
  end
    
  assign clk_rise=( (buf_clk[2]==1'b0) && (buf_clk[1]==1'b1) ) ? 1'b1 : 1'b0;
  assign ena_rise=( (buf_ena[2]==1'b0) && (buf_ena[1]==1'b1) ) ? 1'b1 : 1'b0;
  assign shift   =( (clk_rise == 1'b1) && (buf_ena[2]==1'b0) ) ? 1'b1 : 1'b0;

  always@(posedge i_clk)begin
    if(shift)
      rsv_data <= {rsv_data[30:0],buf_dat[2]};
  end

  always@(posedge i_clk)begin
    if(!i_rst_n)begin
      o_data	<=32'd0;
      o_valid <=1'b0;
    end else if(ena_rise==1'b1)begin
      o_data	<=rsv_data;
      o_valid <=1'b1;
    end else begin
      o_data	<=o_data;
      o_valid <=1'b0;
    end
  end

endmodule
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?