//****输入数据同步****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_rx_pin_t0 <= 1'b0; r_rx_pin_t1 <= 1'b0; end elsebegin r_rx_pin_t0 <= in_rx_pin; r_rx_pin_t1 <= r_rx_pin_t0; end end //********//
//****接收状态逻辑****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_rx_state <= 1'b0; end elsebegin if (w_rx_negedge && ~r_rx_state) begin// 下降沿到来 r_rx_state <= 1'b1; end elseif (w_rx_done) begin// 接收完成 r_rx_state <= 1'b0; end elseif (r_baud_rate_clk_cnt=='d1 && r_rx_data_frame_byte[0]==1'b1) begin// 起始位采样后高电平居多 r_rx_state <= 1'b0; end elsebegin r_rx_state <= r_rx_state; // 在一次接收中rx_state保持高电平 end end end //********//
//****波特率计数器****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_baud_rate_cnt <= 'd0; end elsebegin if (r_rx_state && ~w_rx_done) begin// 接收状态 if (w_bit_rev_done) begin r_baud_rate_cnt <= 'd0; end elsebegin r_baud_rate_cnt <= r_baud_rate_cnt + 1'd1; end end elsebegin r_baud_rate_cnt <= 'd0; end end end //********//
//****波特率时钟计数****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_baud_rate_clk_cnt <= 'd0; end elsebegin if (r_rx_state && ~w_rx_done) begin if (w_bit_rev_done) begin// 计数加一 r_baud_rate_clk_cnt <= r_baud_rate_clk_cnt + 1'd1; end elsebegin r_baud_rate_clk_cnt <= r_baud_rate_clk_cnt; end end elsebegin r_baud_rate_clk_cnt <= 'd0; end end end //********//
//****串行数据存入****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_rx_data_frame_byte <= 'd0; end elsebegin if (r_rx_state) begin if (w_sample_signal) begin// 采样并移位 r_rx_data_frame_byte <= {r_rx_pin_t0, r_rx_data_frame_byte[BAUD_CLK_CNT_THRESHOLD-1:1]}; end elsebegin r_rx_data_frame_byte <= r_rx_data_frame_byte; end end elsebegin r_rx_data_frame_byte <= 'd0; end end end //********//
//****发送数据帧生成****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_tx_data_frame <= 'd0; end elsebegin if (in_tx_data_valid && out_tx_ready) begin r_tx_data_frame <= {1'b1, in_tx_data, 1'b0}; end elsebegin if (w_shift_signal) begin// 低位发送后移位 r_tx_data_frame <= {1'b0, r_tx_data_frame[BAUD_CLK_CNT_THRESHOLD-1:1]}; // 数据帧移位 end elsebegin r_tx_data_frame <= r_tx_data_frame; end end end end //********//
//****发送状态逻辑****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_tx_state <= 1'b0; end elsebegin if (in_tx_data_valid && out_tx_ready) begin r_tx_state <= 1'b1; end elseif (w_tx_done) begin r_tx_state <= 1'b0; end elsebegin r_tx_state <= r_tx_state; end end end //********//
//****波特率计数器****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_baud_rate_cnt <= 'd0; end elsebegin if (r_tx_state && ~w_tx_done) begin// 接收状态 if (w_bit_send_done) begin r_baud_rate_cnt <= 'd0; end elsebegin r_baud_rate_cnt <= r_baud_rate_cnt + 1'd1; end end elsebegin r_baud_rate_cnt <= 'd0; end end end //********//
//****波特率时钟计数****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_baud_rate_clk_cnt <= 'd0; end elsebegin if (r_tx_state && ~w_tx_done) begin if (w_bit_send_done) begin r_baud_rate_clk_cnt <= r_baud_rate_clk_cnt + 1'd1; end elsebegin r_baud_rate_clk_cnt <= r_baud_rate_clk_cnt; end end elsebegin r_baud_rate_clk_cnt <= 'd0; end end end //********//
//****发送数据部分****// always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_tx_pin <= 1'b1; end elsebegin if (r_tx_state && ~w_tx_done) begin if (w_shift_signal) begin// 数据帧移位前送出 r_tx_pin <= r_tx_data_frame[0]; // 数据低位送出 end elsebegin r_tx_pin <= r_tx_pin; end end elsebegin r_tx_pin <= 1'b1; end end end //********//
always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_tx_ready_t0 <= 1'b0; r_tx_ready_t1 <= 1'b0; end elsebegin r_tx_ready_t0 <= w_tx_ready; r_tx_ready_t1 <= r_tx_ready_t0; end end
always @(posedge in_sys_clk ornegedge in_rst_n) begin if (~in_rst_n) begin r_rx_data_valid <= 1'b0; r_rx_data <= 8'd0; end elsebegin if (w_rx_data_valid) begin r_rx_data_valid <= 1'b1; end elseif (~r_tx_ready_t0 && r_tx_ready_t1) begin r_rx_data_valid <= 1'b0; end elsebegin r_rx_data_valid <= r_rx_data_valid; end
if (w_rx_data_valid) begin r_rx_data <= w_rx_data; end elsebegin r_rx_data <= r_rx_data; end end end