前言
當(dāng)你器件的引腳賊少的時(shí)候,需要主機(jī)和從機(jī)通信,spi就派上了用場(chǎng),它可以一對(duì)多,但只是片選到的從機(jī)能和主機(jī)通信,其他的掛機(jī)。
spi:serial peripheral interface 串行外圍接口
大致了解:
spi是個(gè)同步協(xié)議,數(shù)據(jù)在master和slaver間交換通過(guò)時(shí)鐘sck,由于它是同步協(xié)議,時(shí)鐘速率就可以各種變換。
sck:主機(jī)提供,從機(jī)不能操控,從器件由主機(jī)產(chǎn)生的時(shí)鐘控制。數(shù)據(jù)只有在sck來(lái)了的上升沿或者下降沿才傳輸。
高級(jí)一點(diǎn)的spi芯片有配置寄存器,高級(jí)一點(diǎn)的工作有四種模式,采樣相位和sck空閑電平可配置。
當(dāng)然在這里我們主要實(shí)現(xiàn)簡(jiǎn)單的spi協(xié)議:sck是系統(tǒng)時(shí)鐘的四分頻,wr請(qǐng)求信號(hào)有效時(shí),主機(jī)開(kāi)始工作,數(shù)據(jù)位8bit,sck空閑時(shí)低電平,工作時(shí)第一個(gè)沿?cái)?shù)據(jù)傳輸。只有一個(gè)從機(jī),cs低電平片選。
看下結(jié)構(gòu):
接口定義:
編碼實(shí)現(xiàn):(版權(quán)所有,請(qǐng)勿用于商業(yè)用途,僅供學(xué)習(xí)使用)
1 //************************************************ 2 // Filename : spi_ms_test1.v 3 // Author : Kingstacker 4 // Company : School 5 // Email : kingstacker_work@163.com 6 // Device : Altera cyclone4 ep4ce6f17c8 7 // Description : spi master module;data 8bit;sck is 4 div of the clk; 8 //************************************************ 9 module spi_ms #(parameter WIDTH = 8)( 10 //input; 11 input wire clk, 12 input wire rst_n, 13 input wire wr, //send request; 14 input wire [WIDTH-1:0] master_din, //the data you want send; 15 input wire miso, //the data form slave; 16 //output; 17 output reg cs, //slave select; 18 output reg sck, //data exchange clock; 19 output reg mosi, //master out; 20 output reg [WIDTH-1:0] master_dout //the data you received; 21 ); 22 localparam CLK_HZ = 50_000_000; //clk frequency; 23 localparam SCK_HZ = 12_500_000; //sck frequency; 24 localparam DIV_NUMBER = CLK_HZ / SCK_HZ; 25 localparam CNT_MAX = (DIV_NUMBER >>1) - 1'b1; 26 localparam DATA_CNT_MAX = 5'd31; 27 localparam MOSI_CNT_MAX = 3'd7; 28 localparam IDEL = 2'b00; 29 localparam SEND = 2'b01; 30 localparam FINISH = 2'b10; 31 reg cnt; //sck cnt; 32 reg sck_en; //enable sck; 33 reg data_cnt_en; 34 reg sck_reg1; 35 reg sck_reg2; 36 wire sck_p; //posedge sck; 37 wire sck_n; //negedge sck; 38 wire send_over; 39 reg [1:0] cstate; 40 reg [4:0] data_cnt; //cnt the send data; 41 reg [2:0] mosi_cnt; 42 reg [WIDTH-1:0] master_din_reg; 43 reg [WIDTH-1:0] master_dout_reg; 44 //produce sck; 45 always @(posedge clk or negedge rst_n) begin 46 if (~rst_n) begin 47 cnt <= 0; 48 sck <= 1'b0; 49 end //if 50 else begin 51 if (sck_en == 1'b1) begin 52 if (cnt == CNT_MAX) begin 53 cnt <= 0; 54 sck <= ~sck; 55 end 56 else begin 57 cnt <= cnt + 1'b1; 58 sck <= sck; 59 end 60 end 61 else begin 62 cnt <= 0; 63 sck <= 1'b0; 64 end 65 end //else 66 end //always 67 //produce sck_p and sck_n; 68 always @(posedge clk or negedge rst_n) begin 69 if (~rst_n) begin 70 sck_reg1 <= 1'b0; 71 sck_reg2 <= 1'b0; 72 end //if 73 else begin 74 sck_reg1 <= sck; 75 sck_reg2 <= sck_reg1; 76 end //else 77 end //always 78 assign sck_p = (sck_reg1 & (~sck_reg2)); //sck posedge; 79 assign sck_n = ((~sck_reg1) & sck_reg2); //sck negedge; 80 //fsm;hot code; 81 always @(posedge clk or negedge rst_n) begin 82 if (~rst_n) begin 83 cstate <= IDEL; 84 end 85 else begin 86 case (cstate) 87 IDEL: cstate <= (wr)? SEND : IDEL; 88 SEND: cstate <= (send_over) ? FINISH : SEND; 89 FINISH: cstate <= IDEL; 90 default: cstate <= IDEL; 91 endcase //case 92 end 93 end 94 always @(posedge clk or negedge rst_n) begin 95 if (~rst_n) begin 96 cs <= 1'b1; 97 data_cnt_en <= 1'b0; 98 sck_en <= 1'b0; 99 master_din_reg <= 0; 100 master_dout <= 0; 101 end 102 else begin 103 case (cstate) 104 IDEL: begin 105 data_cnt_en <= 1'b0; 106 master_din_reg <= (wr) ? master_din : master_din_reg; //load the data you want send to slaver; 107 end 108 SEND: begin 109 data_cnt_en <= 1'b1; 110 cs <= 1'b0; 111 sck_en <= 1'b1; 112 master_dout <= (send_over) ? master_dout_reg : master_dout; //master receiverd data; 113 end 114 FINISH: begin //send and load ok; 115 sck_en <= 1'b0; 116 cs <= 1'b1; 117 data_cnt_en <= 1'b0; 118 end 119 default: begin 120 cs <= 1'b1; 121 sck_en <= 1'b0; 122 data_cnt_en <= 1'b0; 123 end 124 endcase //case 125 end 126 end 127 always @(posedge clk or negedge rst_n) begin 128 if (~rst_n) begin 129 data_cnt <= 0; 130 end 131 else begin 132 data_cnt <= (data_cnt_en) ? (data_cnt + 1'b1) : 5'd0; //4 div * 8bit = 32 cnt; 133 end 134 end 135 assign send_over = (data_cnt == DATA_CNT_MAX) ? 1'b1 : 1'b0; 136 //rising edge miso; 137 always @(posedge clk or negedge rst_n) begin 138 if (~rst_n) begin 139 master_dout_reg <= 0; 140 end 141 else begin 142 master_dout_reg <= (sck_p) ? {master_dout_reg[6:0],miso} : master_dout_reg; 143 end 144 end 145 //mosi; 146 always @(posedge clk or negedge rst_n) begin 147 if (~rst_n) begin 148 mosi_cnt <= 0; 149 end 150 else begin 151 if (sck_n) begin 152 if (mosi_cnt == MOSI_CNT_MAX) begin 153 mosi_cnt <= 0; 154 end 155 else begin 156 mosi_cnt <= mosi_cnt + 1'b1; 157 end 158 end 159 else begin 160 mosi_cnt <= mosi_cnt; 161 end 162 end 163 end 164 always @(posedge clk or negedge rst_n) begin 165 if (~rst_n) begin 166 mosi <= 1'b0; 167 end 168 else begin 169 mosi <= (sck_n) ? master_din_reg[MOSI_CNT_MAX-mosi_cnt] : mosi; 170 end 171 end 172 endmodule
仿真:
綜合資源使用:
Fmax:
以上。
-
FPGA
+關(guān)注
關(guān)注
1629文章
21736瀏覽量
603384 -
接口
+關(guān)注
關(guān)注
33文章
8598瀏覽量
151156 -
SPI
+關(guān)注
關(guān)注
17文章
1706瀏覽量
91581
原文標(biāo)題:spi master接口的fpga實(shí)現(xiàn)
文章出處:【微信號(hào):zhuyandz,微信公眾號(hào):FPGA之家】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論