为什么我不能为 UART 模拟我的接收器代码?
Why can't I simulate my receiver code for UART?
我正在尝试使用 Verilog 在测试台中模拟 uart 接收器。我强制所有输入位,时钟和复位,我强制RsRx
,接收器的串行输入,为了得到输出rx_data
,但rx_data
总是
发送器运行良好,但接收器运行不佳。我认为问题出在测试台上,因为接收器的代码是由 ARM 提供的。
有人可以帮帮我吗?
module TEST_Uart() ;
reg HCLK=0 ;
reg HRESETn=0 ;
wire b_tick ;
wire rx_done ;
reg RsRx=1 ;
wire [7:0]rx_data ;
initial
forever #1 HCLK = ~HCLK;
initial
begin
@(posedge HCLK);
HRESETn=1 ;
end
BAUDGEN uBAUDGEN(
.clk(HCLK),
.resetn(HRESETn),
.baudtick(b_tick)
);
//UART receiver
UART_RX uUART_RX(
.clk(HCLK),
.resetn(HRESETn),
.b_tick(b_tick),
.rx(RsRx),
.rx_done(rx_done),
.dout(rx_data[7:0])
);
task UART_WRITE_BYTE;
input [7:0] i_Data;
integer ii;
begin
// Send Start Bit
RsRx <= 1'b0;
// Send Data Byte
for (ii=0; ii<8; ii=ii+1)
begin
RsRx <= i_Data[ii];
end
// Send Stop Bit
RsRx <= 1'b1;
end
endtask // UART_WRITE_BYTE
initial
begin
// Send a command to the UART (exercise Rx)
@(posedge HCLK);
UART_WRITE_BYTE(8'h3F);
@(posedge HCLK);
// Check that the correct command was received
if (rx_data == 8'h3F)
$display("Test Passed - Correct Byte Received");
else
$display("Test Failed - Incorrect Byte Received");
end
endendmodule
您需要在 UART_WRITE_BYTE
task
内添加延迟。当前,当您 运行 进行模拟并查看波形时,RsRx
似乎始终为 1。这是因为您的所有分配都发生在同一模拟时间。
您需要在每次分配之间添加延迟。例如,在下面的代码中,我在每次分配后添加了 5 个延迟:
task UART_WRITE_BYTE;
input [7:0] i_Data;
integer ii;
begin
// Send Start Bit
RsRx <= 1'b0;
#5;
// Send Data Byte
for (ii=0; ii<8; ii=ii+1)
begin
RsRx <= i_Data[ii];
#5;
end
// Send Stop Bit
RsRx <= 1'b1;
#5;
end
endtask // UART_WRITE_BYTE
我用5只是为了演示原理。延迟确实应该与波特率有关。您可能想用与 b_tick
信号边缘相关的代码替换该固定延迟,例如:
@(posedge b_tick);
我正在尝试使用 Verilog 在测试台中模拟 uart 接收器。我强制所有输入位,时钟和复位,我强制RsRx
,接收器的串行输入,为了得到输出rx_data
,但rx_data
总是
发送器运行良好,但接收器运行不佳。我认为问题出在测试台上,因为接收器的代码是由 ARM 提供的。 有人可以帮帮我吗?
module TEST_Uart() ;
reg HCLK=0 ;
reg HRESETn=0 ;
wire b_tick ;
wire rx_done ;
reg RsRx=1 ;
wire [7:0]rx_data ;
initial
forever #1 HCLK = ~HCLK;
initial
begin
@(posedge HCLK);
HRESETn=1 ;
end
BAUDGEN uBAUDGEN(
.clk(HCLK),
.resetn(HRESETn),
.baudtick(b_tick)
);
//UART receiver
UART_RX uUART_RX(
.clk(HCLK),
.resetn(HRESETn),
.b_tick(b_tick),
.rx(RsRx),
.rx_done(rx_done),
.dout(rx_data[7:0])
);
task UART_WRITE_BYTE;
input [7:0] i_Data;
integer ii;
begin
// Send Start Bit
RsRx <= 1'b0;
// Send Data Byte
for (ii=0; ii<8; ii=ii+1)
begin
RsRx <= i_Data[ii];
end
// Send Stop Bit
RsRx <= 1'b1;
end
endtask // UART_WRITE_BYTE
initial
begin
// Send a command to the UART (exercise Rx)
@(posedge HCLK);
UART_WRITE_BYTE(8'h3F);
@(posedge HCLK);
// Check that the correct command was received
if (rx_data == 8'h3F)
$display("Test Passed - Correct Byte Received");
else
$display("Test Failed - Incorrect Byte Received");
end
endendmodule
您需要在 UART_WRITE_BYTE
task
内添加延迟。当前,当您 运行 进行模拟并查看波形时,RsRx
似乎始终为 1。这是因为您的所有分配都发生在同一模拟时间。
您需要在每次分配之间添加延迟。例如,在下面的代码中,我在每次分配后添加了 5 个延迟:
task UART_WRITE_BYTE;
input [7:0] i_Data;
integer ii;
begin
// Send Start Bit
RsRx <= 1'b0;
#5;
// Send Data Byte
for (ii=0; ii<8; ii=ii+1)
begin
RsRx <= i_Data[ii];
#5;
end
// Send Stop Bit
RsRx <= 1'b1;
#5;
end
endtask // UART_WRITE_BYTE
我用5只是为了演示原理。延迟确实应该与波特率有关。您可能想用与 b_tick
信号边缘相关的代码替换该固定延迟,例如:
@(posedge b_tick);