2023-05-10 14:35:57 -04:00
|
|
|
/* (c) Peter McGoron 2022 v0.3
|
2022-10-17 00:44:30 -04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v.2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
module
|
|
|
|
`ifdef SPI_SLAVE_NO_READ
|
|
|
|
spi_slave_no_read
|
|
|
|
`elsif SPI_SLAVE_NO_WRITE
|
|
|
|
spi_slave_no_write
|
|
|
|
`else
|
|
|
|
spi_slave
|
|
|
|
`endif
|
|
|
|
#(
|
|
|
|
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.
|
|
|
|
)
|
|
|
|
(
|
|
|
|
input clk,
|
2023-05-10 14:35:57 -04:00
|
|
|
input rst_L,
|
2022-10-17 00:44:30 -04:00
|
|
|
input sck,
|
|
|
|
input ss_L,
|
|
|
|
`ifndef SPI_SLAVE_NO_READ
|
|
|
|
output reg [WID-1:0] from_master,
|
2022-11-14 08:43:16 -05:00
|
|
|
input mosi,
|
2022-10-17 00:44:30 -04:00
|
|
|
`endif
|
|
|
|
`ifndef SPI_SLAVE_NO_WRITE
|
|
|
|
input [WID-1:0] to_master,
|
|
|
|
output reg miso,
|
|
|
|
`endif
|
|
|
|
output reg finished,
|
|
|
|
input rdy,
|
|
|
|
output reg err
|
|
|
|
);
|
|
|
|
|
2022-11-14 08:43:16 -05:00
|
|
|
`ifndef SPI_SLAVE_NO_READ
|
|
|
|
/* MOSI is almost always an external wire, so buffer it. */
|
|
|
|
reg mosi_hot = 0;
|
|
|
|
reg read_mosi = 0;
|
|
|
|
|
|
|
|
always @ (posedge clk) begin
|
|
|
|
read_mosi <= mosi_hot;
|
|
|
|
mosi_hot <= mosi;
|
|
|
|
end
|
|
|
|
`endif
|
|
|
|
|
2022-10-17 00:44:30 -04:00
|
|
|
wire ss = !ss_L;
|
|
|
|
reg sck_delay = 0;
|
|
|
|
reg [WID_LEN-1:0] bit_counter = 0;
|
|
|
|
reg ss_delay = 0;
|
|
|
|
reg ready_at_start = 0;
|
|
|
|
|
|
|
|
`ifndef SPI_SLAVE_NO_WRITE
|
|
|
|
reg [WID-1:0] send_buf = 0;
|
|
|
|
`endif
|
|
|
|
|
|
|
|
task read_data();
|
|
|
|
`ifndef SPI_SLAVE_NO_READ
|
|
|
|
from_master <= from_master << 1;
|
2022-11-14 08:43:16 -05:00
|
|
|
from_master[0] <= read_mosi;
|
2022-10-17 00:44:30 -04:00
|
|
|
`endif
|
|
|
|
endtask
|
|
|
|
|
|
|
|
task write_data();
|
|
|
|
`ifndef SPI_SLAVE_NO_WRITE
|
|
|
|
send_buf <= send_buf << 1;
|
|
|
|
miso <= send_buf[WID-1];
|
|
|
|
`endif
|
|
|
|
endtask
|
|
|
|
|
|
|
|
task setup_bits();
|
|
|
|
`ifndef SPI_SLAVE_NO_WRITE
|
|
|
|
/* 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
|
|
|
|
`endif
|
|
|
|
endtask
|
|
|
|
|
|
|
|
task check_counter();
|
2022-11-14 08:43:16 -05:00
|
|
|
if (bit_counter == WID[WID_LEN-1:0]) begin
|
2022-10-17 00:44:30 -04:00
|
|
|
err <= ready_at_start;
|
|
|
|
end else begin
|
|
|
|
bit_counter <= bit_counter + 1;
|
|
|
|
end
|
|
|
|
endtask
|
|
|
|
|
|
|
|
always @ (posedge clk) begin
|
2023-05-10 14:35:57 -04:00
|
|
|
if (!rst_L) begin
|
|
|
|
sck_delay <= 0;
|
2022-10-17 00:44:30 -04:00
|
|
|
bit_counter <= 0;
|
2023-05-10 14:35:57 -04:00
|
|
|
ss_delay <= 0;
|
|
|
|
ready_at_start <= 0;
|
|
|
|
`ifndef SPI_SLAVE_NO_READ
|
|
|
|
from_master <= 0;
|
|
|
|
`endif
|
|
|
|
`ifndef SPI_SLAVE_NO_WRITE
|
|
|
|
miso <= 0;
|
|
|
|
send_buf <= 0;
|
|
|
|
`endif
|
2022-10-17 00:44:30 -04:00
|
|
|
finished <= 0;
|
|
|
|
err <= 0;
|
2023-05-10 14:35:57 -04:00
|
|
|
end else begin
|
|
|
|
sck_delay <= sck;
|
|
|
|
ss_delay <= ss;
|
2022-10-17 00:44:30 -04:00
|
|
|
|
2023-05-10 14:35:57 -04:00
|
|
|
case ({ss_delay, ss})
|
|
|
|
2'b01: begin // rising edge of SS
|
|
|
|
bit_counter <= 0;
|
|
|
|
finished <= 0;
|
|
|
|
err <= 0;
|
|
|
|
ready_at_start <= rdy;
|
2022-10-17 00:44:30 -04:00
|
|
|
|
2023-05-10 14:35:57 -04:00
|
|
|
setup_bits();
|
2022-10-17 00:44:30 -04:00
|
|
|
end
|
|
|
|
2'b10: begin // falling edge
|
2023-05-10 14:35:57 -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
|
2022-10-17 00:44:30 -04:00
|
|
|
end
|
2023-05-10 14:35:57 -04:00
|
|
|
2'b10: begin // falling edge
|
|
|
|
if (PHASE == 1) begin
|
|
|
|
read_data();
|
|
|
|
end else begin
|
|
|
|
write_data();
|
|
|
|
end
|
2022-10-17 00:44:30 -04:00
|
|
|
|
2023-05-10 14:35:57 -04:00
|
|
|
if (POLARITY == 1) begin
|
|
|
|
check_counter();
|
|
|
|
end
|
2022-10-17 00:44:30 -04:00
|
|
|
end
|
2023-05-10 14:35:57 -04:00
|
|
|
default: ;
|
|
|
|
endcase
|
2022-10-17 00:44:30 -04:00
|
|
|
end
|
2023-05-10 14:35:57 -04:00
|
|
|
2'b00: if (!rdy) begin
|
2022-11-24 09:48:04 -05:00
|
|
|
finished <= 0;
|
|
|
|
err <= 0;
|
|
|
|
end
|
2023-05-10 14:35:57 -04:00
|
|
|
endcase
|
2022-10-17 00:44:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|
2022-11-21 21:41:50 -05:00
|
|
|
`undefineall
|