hsnhsyn1
Junior Member level 1
- Joined
- Mar 19, 2013
- Messages
- 16
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,400
Hi, i'm working for a SPI slave module on fpga with verilog. I have not used SPI before, so i don't know it well. I just read and looked some examples about it.
-> on this website it's very simple i think. but there are some points i couldn't understand.
in this part, they used a shift register for SCK. i know that SCK is produced by the master device, but the thing i don't understand is why SCK needs to be a register, and is not used like a clock signal?
and the other thing is, what do they mean with "first byte sent in a message is the message count"? why is it different from the data receiving counter?
if anyone can help, i would be grateful.
HTML:
http://www.fpga4fun.com/SPI2.html
in this part, they used a shift register for SCK. i know that SCK is produced by the master device, but the thing i don't understand is why SCK needs to be a register, and is not used like a clock signal?
Code:
// sync SCK to the FPGA clock using a 3-bits shift register
reg [2:0] SCKr; always @(posedge clk) SCKr <= {SCKr[1:0], SCK};
wire SCK_risingedge = (SCKr[2:1]==2'b01); // now we can detect SCK rising edges
wire SCK_fallingedge = (SCKr[2:1]==2'b10); // and falling edges
and the other thing is, what do they mean with "first byte sent in a message is the message count"? why is it different from the data receiving counter?
Code:
reg [7:0] byte_data_sent;
reg [7:0] cnt;
always @(posedge clk) if(SSEL_startmessage) cnt<=cnt+8'h1; // count the messages
always @(posedge clk)
if(SSEL_active)
begin
if(SSEL_startmessage)
byte_data_sent <= cnt; // first byte sent in a message is the message count
else
if(SCK_fallingedge)
begin
if(bitcnt==3'b000)
byte_data_sent <= 8'h00; // after that, we send 0s
else
byte_data_sent <= {byte_data_sent[6:0], 1'b0};
end
end
assign MISO = byte_data_sent[7]; // send MSB first
// we assume that there is only one slave on the SPI bus
// so we don't bother with a tri-state buffer for MISO
// otherwise we would need to tri-state MISO when SSEL is inactive
endmodule
if anyone can help, i would be grateful.