spi/spi_slave.v

157 lines
2.9 KiB
Coq
Raw Permalink Normal View History

2024-01-23 10:26:49 -05:00
/* (c) Peter McGoron 2022 v0.4
2024-01-23 14:05:26 -05:00
*
* This code is disjunctively dual-licensed under the MPL v2.0, or the
* CERN-OHL-W v2.
2022-07-21 02:37:22 -04:00
*/
2022-07-21 13:51:55 -04:00
2024-01-23 10:26:49 -05:00
module spi_slave #(
parameter ENABLE_MISO = 1, // Enable MISO and to_master port
parameter ENABLE_MOSI = 1, // Enable MOSI and from_master port
parameter WID = 24, // Width of bits per transaction.
parameter WID_LEN = 5, // Length in bits required to store WID
parameter POLARITY = 0,
parameter PHASE = 0 // 0 = rising-read falling-write, 1 = rising-write falling-read.
2024-01-23 10:26:49 -05:00
) (
input clk,
2023-04-20 15:10:04 -04:00
input rst_L,
input sck,
input ss_L,
2024-01-23 10:26:49 -05:00
output reg [WID-1:0] from_master,
2022-10-23 14:03:19 -04:00
input mosi,
2024-01-23 10:26:49 -05:00
input [WID-1:0] to_master,
2022-07-21 13:51:55 -04:00
output reg miso,
2024-01-23 10:26:49 -05:00
2022-07-21 13:51:55 -04:00
output reg finished,
2022-07-21 01:53:38 -04:00
input rdy,
2022-07-21 13:51:55 -04:00
output reg err
);
2022-10-23 14:03:19 -04:00
/* MOSI is almost always an external wire, so buffer it. */
reg mosi_hot = 0;
reg read_mosi = 0;
2024-01-23 14:05:26 -05:00
always @ (posedge clk) if (ENABLE_MOSI == 1) begin
2022-10-23 14:03:19 -04:00
read_mosi <= mosi_hot;
mosi_hot <= mosi;
end
wire ss = !ss_L;
reg sck_delay = 0;
reg [WID_LEN-1:0] bit_counter = 0;
reg ss_delay = 0;
2022-07-21 01:53:38 -04:00
reg ready_at_start = 0;
`ifndef SPI_SLAVE_NO_WRITE
reg [WID-1:0] send_buf = 0;
`endif
task read_data();
2024-01-23 14:05:26 -05:00
if (ENABLE_MOSI == 1) begin
2024-01-23 10:26:49 -05:00
from_master <= from_master << 1;
from_master[0] <= read_mosi;
end
endtask
task write_data();
2024-01-23 14:05:26 -05:00
if (ENABLE_MISO == 1) begin
2024-01-23 10:26:49 -05:00
send_buf <= send_buf << 1;
miso <= send_buf[WID-1];
end
endtask
task setup_bits();
2024-01-23 14:05:26 -05:00
if (ENABLE_MISO == 1) begin
2024-01-23 10:26:49 -05:00
/* at Mode 00, the transmission starts with
* a rising edge, and at mode 11, it starts with a falling
* edge. For both modes, these are READs.
*
* For mode 01 and mode 10, the first action is a WRITE.
*/
if (POLARITY == PHASE) begin
miso <= to_master[WID-1];
send_buf <= to_master << 1;
end else begin
send_buf <= to_master;
end
end
endtask
2022-07-21 01:53:38 -04:00
task check_counter();
2022-10-22 18:34:54 -04:00
if (bit_counter == WID[WID_LEN-1:0]) begin
2022-07-21 01:53:38 -04:00
err <= ready_at_start;
end else begin
bit_counter <= bit_counter + 1;
end
endtask
always @ (posedge clk) begin
2023-04-20 15:10:04 -04:00
if (!rst_L) begin
sck_delay <= 0;
bit_counter <= 0;
2023-04-20 15:10:04 -04:00
ss_delay <= 0;
ready_at_start <= 0;
2024-01-23 10:26:49 -05:00
2024-01-23 14:05:26 -05:00
if (ENABLE_MOSI == 1) from_master <= 0;
2024-01-23 10:26:49 -05:00
2024-01-23 14:05:26 -05:00
if (ENABLE_MISO == 1) begin
2024-01-23 10:26:49 -05:00
miso <= 0;
send_buf <= 0;
end
finished <= 0;
err <= 0;
2023-04-20 15:10:04 -04:00
end else begin
sck_delay <= sck;
ss_delay <= ss;
2023-04-20 15:10:04 -04:00
case ({ss_delay, ss})
2'b01: begin // rising edge of SS
bit_counter <= 0;
finished <= 0;
err <= 0;
ready_at_start <= rdy;
2023-04-20 15:10:04 -04:00
setup_bits();
end
2'b10: begin // falling edge
2023-04-20 15:10:04 -04:00
finished <= ready_at_start;
end
2'b11: begin
case ({sck_delay, sck})
2'b01: begin // rising edge
if (PHASE == 1) begin
write_data();
end else begin
read_data();
end
if (POLARITY == 0) begin
check_counter();
end
end
2023-04-20 15:10:04 -04:00
2'b10: begin // falling edge
if (PHASE == 1) begin
read_data();
end else begin
write_data();
end
2023-04-20 15:10:04 -04:00
if (POLARITY == 1) begin
check_counter();
end
end
2023-04-20 15:10:04 -04:00
default: ;
endcase
end
2'b00: if (!rdy) begin
finished <= 0;
err <= 0;
end
endcase
end
end
endmodule