2024-01-23 14:05:26 -05:00
|
|
|
/* (c) Peter McGoron 2022-2024 v0.4
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
2022-10-23 14:03:19 -04:00
|
|
|
/* CYCLE_HALF_WAIT should take into account the setup time of the slave
|
|
|
|
* device, and also master buffering (MISO is one cycle off to stabilize
|
|
|
|
* the input).
|
|
|
|
*/
|
|
|
|
|
2024-01-23 10:26:49 -05:00
|
|
|
module spi_master #(
|
|
|
|
parameter ENABLE_MISO = 1, // Enable MISO and from_slave port
|
|
|
|
parameter ENABLE_MOSI = 1, // Enable MOSI and to_slave port
|
2022-07-20 19:41:54 -04:00
|
|
|
parameter WID = 24, // Width of bits per transaction.
|
|
|
|
parameter WID_LEN = 5, // Length in bits required to store WID
|
2022-10-23 04:56:56 -04:00
|
|
|
parameter CYCLE_HALF_WAIT = 1, // Half of the wait time of a cycle minus 1.
|
|
|
|
// One SCK cycle is 2*(CYCLE_HALF_WAIT + 1) clock cycles.
|
2022-07-20 19:41:54 -04:00
|
|
|
parameter TIMER_LEN = 3, // Length in bits required to store CYCLE_HALF_WAIT
|
|
|
|
parameter POLARITY = 0, // 0 = sck idle low, 1 = sck idle high
|
|
|
|
parameter PHASE = 0 // 0 = rising-read falling-write, 1 = rising-write falling-read.
|
2024-01-23 10:26:49 -05:00
|
|
|
) (
|
2022-07-20 19:41:54 -04:00
|
|
|
input clk,
|
2023-04-20 15:10:04 -04:00
|
|
|
input rst_L,
|
2024-01-23 10:26:49 -05:00
|
|
|
|
2022-07-20 19:41:54 -04:00
|
|
|
output reg [WID-1:0] from_slave,
|
|
|
|
input miso,
|
2024-01-23 10:26:49 -05:00
|
|
|
|
2022-07-20 19:41:54 -04:00
|
|
|
input [WID-1:0] to_slave,
|
2022-10-22 18:34:54 -04:00
|
|
|
output reg mosi,
|
2024-01-23 10:26:49 -05:00
|
|
|
|
2022-10-22 18:34:54 -04:00
|
|
|
output reg sck_wire,
|
|
|
|
output reg finished,
|
2023-04-20 12:23:23 -04:00
|
|
|
output reg ready_to_arm,
|
2022-07-20 19:41:54 -04:00
|
|
|
input arm
|
|
|
|
);
|
|
|
|
|
2022-10-23 14:06:38 -04:00
|
|
|
/* MISO is almost always an external wire, so buffer it.
|
|
|
|
* This might not be necessary, since the master and slave do not respond
|
|
|
|
* immediately to changes in the wires, but this is just to be safe.
|
|
|
|
* It is trivial to change, just do
|
|
|
|
* wire read_miso = miso;
|
|
|
|
*/
|
|
|
|
|
2022-10-23 14:03:19 -04:00
|
|
|
reg miso_hot = 0;
|
|
|
|
reg read_miso = 0;
|
|
|
|
|
2024-01-23 14:05:26 -05:00
|
|
|
always @ (posedge clk) if (ENABLE_MISO == 1) begin
|
2022-10-23 14:03:19 -04:00
|
|
|
read_miso <= miso_hot;
|
|
|
|
miso_hot <= miso;
|
|
|
|
end
|
|
|
|
|
2022-07-20 19:41:54 -04:00
|
|
|
parameter WAIT_ON_ARM = 0;
|
|
|
|
parameter ON_CYCLE = 1;
|
|
|
|
parameter CYCLE_WAIT = 2;
|
|
|
|
parameter WAIT_FINISHED = 3;
|
|
|
|
|
|
|
|
reg [1:0] state = WAIT_ON_ARM;
|
|
|
|
reg [WID_LEN-1:0] bit_counter = 0;
|
|
|
|
reg [TIMER_LEN-1:0] timer = 0;
|
|
|
|
|
|
|
|
reg [WID-1:0] send_buf = 0;
|
|
|
|
|
|
|
|
reg sck = 0;
|
|
|
|
assign sck_wire = sck;
|
|
|
|
|
|
|
|
task idle_state();
|
|
|
|
if (POLARITY == 0) begin
|
|
|
|
sck <= 0;
|
|
|
|
end else begin
|
|
|
|
sck <= 1;
|
|
|
|
end
|
2024-01-23 14:05:26 -05:00
|
|
|
if (ENABLE_MOSI == 1) mosi <= 0;
|
2022-07-20 19:41:54 -04:00
|
|
|
timer <= 0;
|
|
|
|
bit_counter <= 0;
|
|
|
|
endtask
|
|
|
|
|
|
|
|
task read_data();
|
2024-01-23 14:05:26 -05:00
|
|
|
if (ENABLE_MISO == 1) begin
|
2024-01-23 10:26:49 -05:00
|
|
|
from_slave <= from_slave << 1;
|
|
|
|
from_slave[0] <= read_miso;
|
|
|
|
end
|
2022-07-20 19:41:54 -04:00
|
|
|
endtask
|
|
|
|
|
|
|
|
task write_data();
|
2024-01-23 14:05:26 -05:00
|
|
|
if (ENABLE_MOSI == 1) begin
|
2024-01-23 10:26:49 -05:00
|
|
|
mosi <= send_buf[WID-1];
|
|
|
|
send_buf <= send_buf << 1;
|
|
|
|
end
|
2022-07-20 19:41:54 -04:00
|
|
|
endtask
|
|
|
|
|
2022-07-21 01:29:39 -04:00
|
|
|
task setup_bits();
|
|
|
|
/* 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
|
2024-01-23 14:05:26 -05:00
|
|
|
if (ENABLE_MOSI == 1) begin
|
2024-01-23 10:26:49 -05:00
|
|
|
mosi <= to_slave[WID-1];
|
|
|
|
send_buf <= to_slave << 1;
|
|
|
|
end
|
2022-07-21 01:29:39 -04:00
|
|
|
state <= CYCLE_WAIT;
|
|
|
|
end else begin
|
2024-01-23 14:05:26 -05:00
|
|
|
if (ENABLE_MISO == 1) begin
|
2024-01-23 10:26:49 -05:00
|
|
|
send_buf <= to_slave;
|
|
|
|
end
|
2022-07-21 01:29:39 -04:00
|
|
|
state <= ON_CYCLE;
|
|
|
|
end
|
|
|
|
endtask
|
|
|
|
|
2022-10-23 04:56:56 -04:00
|
|
|
task cycle_change();
|
|
|
|
// Stop transfer when the clock returns to its original polarity.
|
|
|
|
if (bit_counter == WID[WID_LEN-1:0] && sck == POLARITY[0]) begin
|
|
|
|
state <= WAIT_FINISHED;
|
|
|
|
end else begin
|
|
|
|
sck <= !sck;
|
|
|
|
state <= ON_CYCLE;
|
|
|
|
end
|
|
|
|
endtask
|
|
|
|
|
2023-04-20 12:23:23 -04:00
|
|
|
initial ready_to_arm = 1;
|
|
|
|
|
2022-07-20 19:41:54 -04:00
|
|
|
always @ (posedge clk) begin
|
2023-04-20 15:10:04 -04:00
|
|
|
if (!rst_L) begin
|
|
|
|
idle_state();
|
|
|
|
finished <= 0;
|
|
|
|
state <= WAIT_ON_ARM;
|
|
|
|
ready_to_arm <= 1;
|
2024-01-23 14:05:26 -05:00
|
|
|
if (ENABLE_MISO == 1) from_slave <= 0;
|
|
|
|
if (ENABLE_MOSI == 1) send_buf <= 0;
|
2023-04-20 15:10:04 -04:00
|
|
|
end else case (state)
|
2022-07-20 19:41:54 -04:00
|
|
|
WAIT_ON_ARM: begin
|
2023-04-20 12:23:23 -04:00
|
|
|
`ifdef SIMULATION
|
|
|
|
if (!ready_to_arm)
|
|
|
|
$error("not ready to arm in wait_on_arm");
|
|
|
|
`endif
|
2022-07-20 19:41:54 -04:00
|
|
|
if (!arm) begin
|
|
|
|
idle_state();
|
|
|
|
finished <= 0;
|
|
|
|
end else begin
|
2022-07-21 01:29:39 -04:00
|
|
|
setup_bits();
|
2023-04-20 12:23:23 -04:00
|
|
|
ready_to_arm <= 0;
|
2022-07-20 19:41:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
ON_CYCLE: begin
|
2023-04-20 12:23:23 -04:00
|
|
|
`ifdef SIMULATION
|
|
|
|
if (ready_to_arm)
|
|
|
|
$error("ready_to_arm while on cycle");
|
|
|
|
`endif
|
2022-07-20 19:41:54 -04:00
|
|
|
if (sck) begin // rising edge
|
|
|
|
if (PHASE == 1) begin
|
|
|
|
write_data();
|
|
|
|
end else begin
|
|
|
|
read_data();
|
|
|
|
end
|
|
|
|
|
2022-07-21 01:09:45 -04:00
|
|
|
if (POLARITY == 0) begin
|
2022-07-20 19:41:54 -04:00
|
|
|
bit_counter <= bit_counter + 1;
|
|
|
|
end
|
|
|
|
end else begin // falling edge
|
|
|
|
if (PHASE == 1) begin
|
|
|
|
read_data();
|
|
|
|
end else begin
|
|
|
|
write_data();
|
|
|
|
end
|
|
|
|
|
2022-07-21 01:09:45 -04:00
|
|
|
if (POLARITY == 1) begin
|
2022-07-20 19:41:54 -04:00
|
|
|
bit_counter <= bit_counter + 1;
|
|
|
|
end
|
|
|
|
end
|
2022-10-23 04:27:28 -04:00
|
|
|
|
2022-10-23 04:56:56 -04:00
|
|
|
if (CYCLE_HALF_WAIT == 0) begin
|
|
|
|
cycle_change();
|
|
|
|
end else begin
|
|
|
|
state <= CYCLE_WAIT;
|
|
|
|
end
|
2022-07-20 19:41:54 -04:00
|
|
|
end
|
|
|
|
CYCLE_WAIT: begin
|
2023-04-20 12:23:23 -04:00
|
|
|
`ifdef SIMULATION
|
|
|
|
if (ready_to_arm)
|
|
|
|
$error("ready_to_arm while in cycle wait");
|
|
|
|
`endif
|
2022-07-20 19:41:54 -04:00
|
|
|
if (timer == CYCLE_HALF_WAIT) begin
|
2022-10-23 04:56:56 -04:00
|
|
|
timer <= 1;
|
|
|
|
cycle_change();
|
2022-07-20 19:41:54 -04:00
|
|
|
end else begin
|
|
|
|
timer <= timer + 1;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
WAIT_FINISHED: begin
|
2023-04-20 12:23:23 -04:00
|
|
|
`ifdef SIMULATION
|
|
|
|
if (ready_to_arm)
|
|
|
|
$error("ready_to_arm while in wait finished");
|
|
|
|
`endif
|
2022-07-20 19:41:54 -04:00
|
|
|
finished <= 1;
|
|
|
|
idle_state();
|
|
|
|
if (!arm) begin
|
|
|
|
state <= WAIT_ON_ARM;
|
2023-04-20 12:23:23 -04:00
|
|
|
ready_to_arm <= 1;
|
2022-07-20 19:41:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
endcase
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule
|