module clocks(
clkpin, // 50MHz
+ clk50,
rst,
locked,
clk, // 300MHz
input rst;
output locked;
output clk;
+output clk50;
output spiclk;
output tftclk;
OR2 U2_OR2_INST(.I0(U2_LOCKED_INV_RST), .I1(U2_OR3_O_OUT), .O(U2_RST_IN));
OR3 U2_OR3_INST(.I0(U2_FD3_Q_OUT), .I1(U2_FD2_Q_OUT), .I2(U2_FD1_Q_OUT), .O(U2_OR3_O_OUT));
+assign clk50 = U1_CLKIN_IBUFG;
+
endmodule
ssel,
din,
dout,
- ready,
- enable
+ done,
+ r,
+ s
);
function integer log2;
output reg ssel = 1;
input [WIDTH-1:0] din;
output [WIDTH-1:0] dout;
-output reg ready;
-input enable;
-
-initial ready = 1;
+output reg done;
+input r;
+input s;
reg started = 0;
reg [log2(WIDTH)-1:0] counter;
assign dout = sp_out;
-always @(posedge clk) if (enable) begin
- if (!started) begin
- sp_in <= din;
- sp_out <= 0;
- started <= 1;
+always @(posedge clk) begin
+ if (r) begin
+ started <= 0;
+ done <= 0;
ssel <= 1;
- ready <= 0;
- counter <= WIDTH;
- end else begin
+ end else if (started) begin
if (counter == 0) begin
- started <= 0;
ssel <= 1;
- ready <= 1;
+ done <= 1;
end else begin
ssel <= 0;
- ready <= 0;
mosi <= sp_in[WIDTH-1];
sp_in <= sp_in << 1;
sp_out <= (sp_out << 1) | miso;
counter <= counter - 1;
end
+ end else if (s) begin
+ sp_in <= din;
+ sp_out <= 0;
+ started <= 1;
+ counter <= WIDTH;
end
-end else begin
- ssel <= 1;
- ready <= 1;
end
-endmodule
\ No newline at end of file
+endmodule
wire clk; // 300MHz
wire spiclk; // 20MHz
wire tftclk; // 6.667MHz
+wire clk50;
clocks clocks_instance(
.clkpin(clkpin),
.rst(1'b0),
+ .clk50(clk50),
.clk(clk),
.spiclk(spiclk),
.tftclk(tftclk)
reg [31:0] counter = 0;
wire [23:0] spi_in;
wire [23:0] spi_out;
-wire spi_ready;
-reg spi_enable = 0;
wire mosi;
wire ssel;
+reg spi_r = 0;
+reg spi_s = 0;
+wire spi_done;
-assign led[0] = slowclk;
-assign led[1] = ssel;
-assign led[2] = mosi;
-assign led[3] = spi_ready;
+assign led[0] = ~slowclk;
+assign led[1] = ~ssel;
+assign led[2] = ~mosi;
+assign led[3] = ~spi_done;
master_spi #(
.WIDTH(24)
)
-master_spi_instance (
+master_spi_instance(
.clk(slowclk),
.mosi(mosi),
.miso(1'b0),
.ssel(ssel),
.din(spi_in),
.dout(spi_out),
- .ready(spi_ready),
- .enable(spi_enable)
+ .done(spi_done),
+ .r(spi_r),
+ .s(spi_s)
);
reg [1:0] state = 0;
assign spi_in = init_sequence[index];
reg [1:0] button_state = 0;
-always @(posedge clk) begin
- if (button_state == 2'b10) state <= 1;
- button_state <= {button_state[0], button};
-end
always @(posedge slowclk) begin
case(state)
- 1: begin state <= 2; index <= 0; end
- 2: if (spi_ready) spi_enable <= 1;
- 3: if (spi_ready) begin
- spi_enable <= 0;
- state <= index == 45 ? 0 : 2;
- index <= index + 1;
+ 0: if (button_state == 2'b10) begin state <= 1; index <= 0; end
+ 1: begin state <= 2; spi_r <= 1; spi_s <= 0; end
+ 2: begin state <= 3; spi_r <= 0; spi_s <= 1; end
+ 3: begin
+ spi_s <= 0;
+ if (spi_done) begin
+ state <= index == 45 ? 0 : 1;
+ index <= index + 1;
+ end
end
endcase
+ button_state <= { button_state[0], button };
end
-always @(posedge clk) begin
- if (counter == 150000000) begin
+always @(posedge clk50) begin
+ if (counter == 25000000) begin
counter <= 0;
- slowclk <= ~slowclk;
+ slowclk <= ~slowclk;
end else begin
- counter <= counter + 1;
+ counter <= counter + 1;
end
end