always @(posedge clk) begin if(~spi_cs) case(state) 0 : if(spi_clk_pos) begin cmd_reg <= {cmd_reg[6:0],spi_di_tmp2}; //也可以将spi_di寄存 cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 1; end end 1 : begin case(cmd_reg) ARM_FPGA_REG1_ADDR : if(spi_clk_pos) //写FPGA寄存器是上升沿 begin
ARM_FPGA_REG1 <= {ARM_FPGA_REG1[6:0],spi_di_tmp2}; cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 0; end end ARM_FPGA_REG2_ADDR : if(spi_clk_pos) begin ARM_FPGA_REG2 <= {ARM_FPGA_REG2[6:0],spi_di_tmp2}; cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 0; end end ARM_FPGA_REG3_ADDR : if(spi_clk_pos) begin ARM_FPGA_REG3 <= {ARM_FPGA_REG3[6:0],spi_di_tmp2}; cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 0; end end ARM_FPGA_REG4_ADDR : if(spi_clk_pos) begin ARM_FPGA_REG4 <= {ARM_FPGA_REG4[6:0],spi_di_tmp2}; cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 0; end end ARM_FPGA_BUF_READ_ADDR : if(spi_clk_pos) begin ARM_FPGA_BUF_NUM <= {ARM_FPGA_BUF_NUM[6:0],spi_di_tmp2}; cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 3; end end /*以下为寄存器读*/ ARM_FPGA_REG1_ADDR + 1'b1 : if(spi_clk_neg) //读FPGA寄存器是下降沿 begin out_data <= ARM_FPGA_REG1; state <= 2; end ARM_FPGA_REG2_ADDR + 1'b1 : if(spi_clk_neg) //读FPGA寄存器是下降沿 begin out_data <= ARM_FPGA_REG2; state <= 2; end ARM_FPGA_REG3_ADDR + 1'b1 : if(spi_clk_neg) //读FPGA寄存器是下降沿 begin out_data <= ARM_FPGA_REG3; state <= 2; end ARM_FPGA_REG4_ADDR + 1'b1 : if(spi_clk_neg) //读FPGA寄存器是下降沿 begin out_data <= ARM_FPGA_REG4; state <= 2; end default : state<= 0;
endcase end 2 : begin //移位发送 if(spi_clk_neg) begin out_data <= {out_data[6:0],1'b0}; cnt <= cnt + 1; if(cnt == 7) begin cnt <= 0; state <= 0; end end end 3 : begin //连续读装载第一个初值 if(spi_clk_neg) begin out_data <= read_cnt;//data_in; if(read_cnt == ARM_FPGA_BUF_NUM) begin read_cnt <= 0; state <= 0; end else state <= 4; end end 4 : begin //移位发送 if(spi_clk_neg) begin out_data <= {out_data[6:0],1'b0}; cnt <= cnt + 1; if(cnt == 6) //6就可以了 begin cnt <= 0; read_cnt <= read_cnt + 1; state <= 3; end end end default : state <= 0; endcase end assign spi_do = out_data[7]; //SPI发送