Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

No state transition in verilog code

Status
Not open for further replies.

masterpb.mail

Newbie
Joined
Sep 14, 2022
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
15
Hi I am writing SPI master for an ADC communication. I wrote the verilog code as shown but when I simulated using Vivado I found no state transition is taking place. Would anybody help me in it?

Code:
module spi_master #
(
    parameter CLK_DIV_FACTOR = 5'd4
)
(
    input adc_clk,
    input clk_in,
    input rst_in,
   
    input spi_start_signal_in,        //control signal for start
    input [23:0] spi_wr_data_in,      //write data to IC
   
    input spi_wr_en,
    input spi_rd_en,
   
    input spi_miso_in,                //read from IC
    input [1:0] no_of_bits,
   
    output reg [23:0] spi_rd_data_out,  //read data from IC
    output spi_rd_end_out, 
    output spi_wr_end_out,
    output spi_mosi_out,            //write to IC
    output spi_sclk_out                 //spi_Sclk   
   
    );
   
    reg [23:0] wr_data_buff_reg;
    reg [23:0] rd_data_buff_reg;
    reg [4:0] clk_cnt_reg;
    reg r_spi_mosi_out;
    wire clk_valid_reg;
    reg r_spi_clk;
    integer r_spi_clk_div_cntr = 0;
    integer r_no_of_wr_bits_cntr = 0;
    integer r_no_of_rd_bits_cntr = 0;
    reg r_spi_wr_end_out;
    reg r_spi_rd_end_out;
    integer r_no_of_bits = 0;
   
    parameter     ST_IDLE      = 3'b000,
                ST_SCLK_HIGH = 3'b001,
                ST_SCLK_LOW  = 3'b010,
                ST_DATA_TX   = 3'b011;
    reg [1:0] spi_wr_state, spi_wr_nxt_state, spi_rd_state;
   
//    assign r_no_of_bits = {(no_of_bits == 2'b00) ? 24 : (no_of_bits == 2'b01) ? 16 : 8};
    assign spi_wr_end_out     = r_spi_wr_end_out;
    assign spi_Sclk            = r_spi_clk;
    assign spi_mosi_out        = r_spi_mosi_out;
    assign spi_rd_end_out     = r_spi_rd_end_out;
   
    always @(posedge clk_in or posedge rst_in)
    begin
        if(rst_in == 1'b1) begin
            r_no_of_bits <= 0;
            spi_wr_state <= ST_IDLE; end
        else begin
            spi_wr_state <= spi_wr_nxt_state;
            r_no_of_bits <= {(no_of_bits == 2'b00) ? 24 : (no_of_bits == 2'b01) ? 16 : 8}; end
    end 
           
    always @(posedge clk_in or posedge rst_in)
    begin
        if(rst_in == 1'b1) begin
            r_spi_mosi_out          <= 1'b0;
            spi_wr_nxt_state        <= ST_IDLE;
            r_spi_clk                <= 1'b0;
            wr_data_buff_reg[23:0]     <= 24'd0;
            r_spi_wr_end_out        <= 1'b0;
            r_no_of_wr_bits_cntr    <= 0;
            r_spi_clk_div_cntr        <= 0; end
        else begin
//            if(spi_wr_en == 1'b1) begin
            case (spi_wr_state)
            ST_IDLE:    begin
                            r_spi_wr_end_out        <= 1'b0;
                            r_no_of_wr_bits_cntr    <= 0;
                            r_spi_mosi_out          <= 1'b0;
                            if(spi_wr_en == 1'b1)
                              spi_wr_nxt_state        <= ST_DATA_TX;
                            else
                              spi_wr_nxt_state        <= ST_IDLE;
//                            r_spi_clk                <= 1'b0;
                            wr_data_buff_reg[23:0]     <= spi_wr_data_in[23:0]; end
            ST_DATA_TX:    begin
                        if(r_no_of_wr_bits_cntr < r_no_of_bits) begin
                            r_spi_mosi_out          <= wr_data_buff_reg[23];
                            spi_wr_nxt_state        <= ST_SCLK_HIGH;
                            wr_data_buff_reg[23:0]     <= {wr_data_buff_reg[22:1], 1'b0}; end
                        else begin
                            r_spi_wr_end_out        <= 1'b1;
                            r_spi_mosi_out          <= 1'b0;
                            spi_wr_nxt_state        <= ST_IDLE;
                            wr_data_buff_reg[23:0]     <= 24'b0; end end   
            ST_SCLK_HIGH:begin
                            r_spi_clk                <= 1'b1;
                            r_no_of_wr_bits_cntr    <= r_no_of_wr_bits_cntr + 1;
                            if(r_spi_clk_div_cntr == (CLK_DIV_FACTOR >> 1) - 1)
                            begin
                                r_spi_clk_div_cntr    <= 0;
                                spi_wr_nxt_state    <= ST_SCLK_LOW; end
                            else begin
                                r_spi_clk_div_cntr    <= r_spi_clk_div_cntr + 1;
                                spi_wr_nxt_state    <= ST_SCLK_HIGH; end end   
            ST_SCLK_LOW:begin
                            r_spi_clk                <= 1'b0;
                            if(r_spi_clk_div_cntr == (CLK_DIV_FACTOR >> 1) - 2)
                            begin
                                r_spi_clk_div_cntr    <= 0;
                                spi_wr_nxt_state    <= ST_DATA_TX; end
                            else begin
                                r_spi_clk_div_cntr    <= r_spi_clk_div_cntr + 1;
                                spi_wr_nxt_state    <= ST_SCLK_LOW; end end
            default  : spi_wr_nxt_state    <= ST_IDLE;                   
            endcase end
//        end
    end
 
Last edited by a moderator:

Nested usage of spi_wr_en == 1'b1 doesn't look right, second is useless.

We'd need to see the test bench or a simulation waveform to understand where you are expecting a state transition and why it doesn't happen.
--- Updated ---

In addition, your state machine does all assignments in a clocked always block, spi_wr_nxt_state isn't needed.
 

Nested usage of spi_wr_en == 1'b1 doesn't look right, second is useless.

We'd need to see the test bench or a simulation waveform to understand where you are expecting a state transition and why it doesn't happen.
--- Updated ---

In addition, your state machine does all assignments in a clocked always block, spi_wr_nxt_state isn't needed.
Thanks for the reply.
I had simulated using Vivado simulator by forcing the inputs. So the test bench is not used. Is it possible resolve without test bench?
I need the state transition to happen from ST_IDLE to next state ST_DATA_TX. If spi_wr_en is enabled then it should proceed to next state to generate the SPI clock (r_spi_clk). The frequency of it is decided by CLK_DIV_FACTOR.
Do you find any issue in it?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top