📄 btm_timer_ctr.v
字号:
//************************************************************************//
//***** CopyRight(c)2007, HollySys All right reserved. *****//
//***** 模块名称: btm_timer_ctr *****//
//***** 功能描述: 监视两组数据写入的时间来控制定时器计数 *****//
//***** 作者: 刘志凯 *****//
//***** 日期: 2007-03-03 *****//
//************************************************************************//
module btm_timer_ctr(clk, //寄存器时钟
rst_n, //异步复位
rst, //同步复位
data1_ok, //cpu1数据准备好信号
data1_id, //cpu1数据的id值
data2_ok, //cpu2数据准备好信号
data2_id, //cpu2数据的id值
timerout, //两cpu写入数据时间差超时标志
rst_buf_crc1, //当cpu2数据超时未到来复位cpu1的SR和CRC
rst_buf_crc2, //当cpu1数据超时未到来复位cpu2的SR和CRC
timer_ctr, //使能或禁止定时器控制端
timerok, //两组数据在10ms之内到来标志
timer_fault); //超时故障标志
input clk,rst_n,rst,data1_ok,data2_ok,timerout;
input [7:0] data1_id,data2_id; //变量声明
output timer_ctr,rst_buf_crc1,rst_buf_crc2,timerok,timer_fault;
reg timer_ctr,rst_buf_crc1,rst_buf_crc2,timerok,timer_fault;
reg [3:0] state;
parameter Idle = 4'b0000, //空闲状态
Data1in = 4'b0001, //当cpu1传送完成后判断cpu2是否完成状态
Judge_cpu2lose = 4'b0011, //超时后判断cpu2是否丢失一帧数据
Wait1_timerrst1 = 4'b0010, //等待定时器复位完成状态1
Wait1_timerrst2 = 4'b0110, //等待定时器复位完成状态2
Wait1_timerrst3 = 4'b0111, //等待定时器复位完成状态3
Data2in = 4'b0101, //当cpu2传送完成后判断cpu1是否完成状态
Judge_cpu1lose = 4'b0100, //超时后判断cpu1是否丢失一帧数据
Wait2_timerrst1 = 4'b1100, //等待定时器复位完成状态1
Wait2_timerrst2 = 4'b1101, //等待定时器复位完成状态2
Wait2_timerrst3 = 4'b1001, //等待定时器复位完成状态3
Wait_crcrst = 4'b1011; //复位SR和CRC校验电路状态
always@(posedge clk or negedge rst_n)
if(!rst_n) //异步复位
begin
timer_ctr <= 0;
rst_buf_crc1 <= 0;
rst_buf_crc2 <= 0;
timerok <= 0;
timer_fault <= 0;
state <= Idle;
end
else if(rst)
begin
timer_ctr <= 0;
rst_buf_crc1 <= 0;
rst_buf_crc2 <= 0;
timerok <= 0;
timer_fault <= 0;
state <= Idle;
end
else
case(state)
Idle:
begin
if(data1_ok && (!data2_ok)) //cpu1数据准备好,cpu2未准备好
begin
timer_ctr <= 1; //使能定时器
state <= Wait1_timerrst1; //等待cpu2数据准备好
end
else if(data2_ok && (!data1_ok)) //cpu2数据准备好,cpu1未准备好
begin
timer_ctr <= 1; //使能定时器
state <= Wait2_timerrst1; //等待cpu1数据准备好
end
else if(data2_ok && data1_ok) //两组数据同时准备好
begin
timerok <= 1; //两组数据满足定时要求
state <= Idle; //继续判断
end
else
begin
timer_ctr <= 0; //禁止定时器
state <= Idle; //继续判断
end
end
Data1in:
begin
if(data2_ok && (!timerout)) //未超时情况下cpu2数据准备好
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else if((!data2_ok) && timerout) //超时时cpu2数据还未准备好
begin
timer_ctr <= 0; //禁止定时器
rst_buf_crc1 <= 1; //复位cpu1的SR和CRC校验器
timer_fault <= 1; //超时故障信号
state <= Judge_cpu2lose; //去判断cpu2是否一帧丢失
end
else if((data2_ok) && (timerout)) //cpu2数据到来瞬间,超时发生认为超时
begin
timer_ctr <= 0; //禁止定时器
rst_buf_crc1 <= 1; //复位cpu1的SR和CRC校验器
rst_buf_crc2 <= 1; //复位cpu2的SR和CRC校验器
timer_fault <= 1; //超时故障信号
state <= Wait_crcrst; //完成复位状态
end
else
state <= Data1in; //否则继续等待cpu2数据写入完成或超时标志到来
end
Judge_cpu2lose:
begin
rst_buf_crc1 <= 0; //复位cpu1的SR和CRC校验器
if(data1_ok && (!data2_ok)) //等到的是新的cpu1数据准备好,认为cpu2丢失一包数据
begin
timer_ctr <= 1; //使能定时器
state <= Wait1_timerrst1; //等待定时器复位完成
end
else if(data2_ok && (!data1_ok)) //等到的是cpu2的数据
if(data1_id == data2_id) //判断该流水号与刚才写入的流水号是否一致,一致时报废
begin
rst_buf_crc2 <= 1; //复位cpu2的SR和CRC校验器
state <= Wait_crcrst; //完成复位状态
end
else //不一致时认为cpu2丢失一包数据,写入的是新一包数据
begin
timer_ctr <= 1; //使能定时器
state <= Wait2_timerrst1; //等待定时器复位完成
end
else if(data1_ok && data2_ok) //两组数据同时写入
begin
timerok <= 1; //两组数据满足定时要求
timer_fault <= 0; //清除超时故障信号
state <= Idle; //继续判断
end
else
state <= Judge_cpu2lose; //否则等待新的一帧数据准备好
end
Wait1_timerrst1:
if(data2_ok)
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else
state <= Wait1_timerrst2; //继续等待
Wait1_timerrst2:
if(data2_ok)
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else
state <= Wait1_timerrst3; //继续等待
Wait1_timerrst3:
if(data2_ok)
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else
state <= Data1in; //定时器复位完成,去判断CPU2数据的到来和超时
Data2in:
begin
if(data1_ok && (!timerout)) //cpu1数据准备好且不超时
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else if((!data1_ok) && timerout) //超时且cpu1数据未准备好
begin
timer_ctr <= 0; //禁止定时器
rst_buf_crc2 <= 1; //复位cpu2的SR和CRC校验器
timer_fault <= 1; //超时故障信号
state <= Judge_cpu1lose; //去判断cpu1是否一帧丢失
end
else if((data1_ok) && (timerout)) //cpu1数据到来瞬间,超时发生认为超时
begin
timer_ctr <= 0; //禁止定时器
rst_buf_crc1 <= 1; //复位cpu1的SR和CRC校验器
rst_buf_crc2 <= 1; //复位cpu2的SR和CRC校验器
timer_fault <= 1; //超时故障信号
state <= Wait_crcrst; //完成复位状态
end
else
state <= Data2in; //否则继续等待cpu1数据写入完成或超时发生
end
Judge_cpu1lose:
begin
rst_buf_crc2 <= 0; //复位cpu2的SR和CRC校验器
if(data2_ok && (!data1_ok)) //等到的是新的cpu2数据准备好,认为cpu1丢失一包数据
begin
timer_ctr <= 1; //使能定时器
state <= Wait2_timerrst1; //等待定时器复位完成
end
else if(data1_ok && (!data2_ok)) //等到的是cpu1的数据
if(data1_id == data2_id) //判断该流水号与刚才写入的流水号是否一致,一致时报废
begin
rst_buf_crc1 <= 1; //复位CPU1的SR和CRC校验器
state <= Wait_crcrst; //完成复位状态
end
else
begin //不一致时认为cpu1丢失一包数据,写入的是新一包数据
timer_ctr <= 1; //使能定时器
state <= Wait1_timerrst1; //去判断cpu2数据是否准备好
end
else if(data2_ok && data1_ok) //两组数据同时准备好
begin
timerok <= 1; //两组数据满足定时要求
timer_fault <= 0; //清除超时故障信号
state <= Idle; //继续判断
end
else
state <= Judge_cpu1lose; //否则等待新的一帧数据准备好
end
Wait2_timerrst1:
if(data1_ok)
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else
state <= Wait2_timerrst2; //继续等待
Wait2_timerrst2:
if(data1_ok)
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else
state <= Wait2_timerrst3; //继续等待
Wait2_timerrst3:
if(data1_ok)
begin
timerok <= 1; //两组数据满足定时要求
timer_ctr <= 0; //禁止定时器
timer_fault <= 0; //清除超时故障信号
state <= Idle; //进入下一周期的判断
end
else
state <= Data2in; //定时器复位完成,去判断CPU1数据的到来和超时
Wait_crcrst:
begin
rst_buf_crc1 <= 0; //复位cpu1的SR和CRC校验器完成
rst_buf_crc2 <= 0; //复位cpu2的SR和CRC校验器完成
if(data1_ok) //若在复位cpu2复位期间cpu1数据到来
begin
timer_ctr <= 1; //启动定时器
state <= Data1in;
end
else if(data2_ok) //若在复位cpu1复位期间cpu2数据到来
begin
timer_ctr <= 1; //启动定时器
state <= Data2in;
end
else
state <= Idle; //继续等待
end
default:
begin
timer_ctr <= 0;
rst_buf_crc1 <= 0;
rst_buf_crc2 <= 0;
timerok <= 0;
timer_fault <= 0;
state <= Idle;
end
endcase //状态机结束
endmodule //模块结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -