可编程逻辑
always @(posedge clk_100m or negedge rst_n )beginif(rst_n==1'b0) begincan_clk<= 'b0;end else if (can_div==249) begincan_clk<= can_clk + 1'b1;endend //时钟经过BUFG缓冲wirecan_clk_o; BUFG can_clk_obuf (.I(can_clk), .O(can_clk_o));
数据的收发需要根据实际的项目情况进行组包控制,这里就不进行细致描述了。CAN实现数据的收发两个过程中对应FPGA来说由于接收相对复杂,就以接收模块进行描述。数据的接收过程就按照一般的状态机进行设计就行,需要注意的是不同类型帧的跳转是在控制段进行了,因此在控制段会发生状态的跳转。CAN接收状态的状态机实现如下图: 这个状态机的实现并不复杂,主要是对帧类型进行判断。然后根据数据长度把数据解析出来。下面是实现CAN数据接收代码: `timescale 1ns / 1ps//// Company: // Engineer: // // Create Date: // Design Name: // Module Name: can_rx // Project Name: // Target Devices: // Tool versions: // Description: //// Dependencies: //// Revision: // Revision 0.01 - File Created// Additional Comments: ////module can_rx( input wire can_clk ,input wire rst_n ,
inputwirecan_rx,
outputregcan_ack_out_low,outputregcan_id_out_en,outputreg[10:0]can_id_out,outputregcan_data_out_en,outputreg[63:0]can_data_out
);
reg[8:0]state;regcan_rx_t;regerror_state;reg[9:0]error_data;reg[4:0]one_bit_cont;reg[10:0]can_id;reg[6:0]bit_cont;regid_en_flag;regcontral_flag;regdata_en_flag;regcic_en_flag;regcan_rx_en_flag;regcan_rx_unen_flag;reg[4:0]can_continuity_data;regcan_continuity_data_flag;reg[4:0]can_id_data_cont;reg[3:0]can_contral_data_cont;reg[6:0]can_data_data_cont;reg[4:0]can_cic_data_cont; always @(posedge can_clk or negedge rst_n )beginif(rst_n==1'b0) begincan_rx_t<= 'b0;end else begincan_rx_t<= can_rx;endend parameterstate_idle = 9'b000000000;//状态机初始化parameterstate_start = 9'b000000001;//监测到开始标志parameterstate_sof = 9'b000000010; //开始帧头第一位SOFparameterstate_id = 9'b000000100; //包IDparameterstate_control = 9'b000001000; //标准帧控制段parameterstate_data = 9'b000010000; //数据段parameterstate_crc = 9'b000100000; //CRC段parameterstate_ack = 9'b001000000; //ACK段parameterstate_eof = 9'b010000000; //帧结束段parameterstate_end = 9'b100000000; //状态机结束状态 parameterbit_flag_no = 5'b10011; always @(posedge can_clk or negedge rst_n )beginif(rst_n==1'b0) beginstate<= 'b0;one_bit_cont<= 'b0;bit_cont<= 'b0;id_en_flag<= 'b0;contral_flag<= 'b0;data_en_flag <= 'b0;cic_en_flag<= 'b0;can_rx_en_flag <= 'b0;can_ack_out_low<= 'b1;end else case(state) state_idle:begin if ((can_rx_t==1'b1)&&(can_rx==1'b0))beginstate<= state_sof;one_bit_cont<= 'b0;can_rx_en_flag <= 'b1;end else beginstate<= state_idle;one_bit_cont<= 'b0;bit_cont<= 'b0;id_en_flag<= 'b0;contral_flag <= 'b0;data_en_flag<= 'b0;cic_en_flag <= 'b0;can_rx_en_flag <= 'b0;can_ack_out_low<= 'b1;end end state_sof:begin if ((one_bit_cont==bit_flag_no)&&(can_rx==1'b0))beginstate<= state_id;id_en_flag<= 'b1;one_bit_cont<= 'b0;end else if ((one_bit_cont
全部0条评论
快来发表一下你的评论吧 !