Programmable Devices
CPLDs, FPGAs, SoC FPGAs, Configuration, and Transceivers
20704 Discussions

How to destuff can frame?

Altera_Forum
Honored Contributor II
1,170 Views

Hi, 

 

I am trying to destuff can frame without using can controller. All i know about a can frame that 

 

1) the normal size of can frame is 108 bits. If i have a max number of stuffed bits will be 130. 

2) the stuffed bits can be in the area between SOF and CRC Sequence. 

 

I wrote some code but till now, it is not working. 

 

entity validation is  

port(  

clk : in std_logic;  

message_in : in std_logic_vector (129 downto 0);  

message_out : out std_logic_vector (129 downto 0);  

confirmation : out std_logic);  

end validation;  

architecture archi of validation is  

signal tmp2: std_logic_vector(129 downto 0); 

signal tmp: std_logic_vector(107 downto 0); 

begin  

 

process(message_in, tmp, clk) 

variable j : integer :=0 ; 

variable i : integer :=13 ; 

begin 

if (clk'event and clk='1') then 

tmp(12 downto 0) <= message_in(12 downto 0); 

if (j <= 107) then 

if ((message_in(i) /= message_in(i+1)) and (message_in(i) /= message_in(i+2)) and (message_in(i) /= message_in(i+3)) and (message_in(i) /= message_in(i+4)) and (message_in(i)/= message_in(i+5))) then 

tmp(j+16 downto j+13) <= message_in(i+4) & message_in(i+3) & message_in(i+2) & message_in(i+1); 

j:=j+4; 

i:=i+5; 

elsif ((message_in(i) = message_in(i+1)) or (message_in(i) = message_in(i+2)) or (message_in(i) /= message_in(i+3)) or (message_in(i) /= message_in(i+4)) or (message_in(i)/= message_in(i+5))) then 

tmp(j+13) <= message_in(i); 

j:=j+1; 

i:=i+1; 

end if; 

elsif (j > 107) then 

if (tmp(98 downto 96) = "101") then 

confirmation <='1'; 

tmp2<= message_in; 

end if; 

end if; 

end if; 

end process; 

message_out <= tmp2; 

end archi;  

 

I just want with this code check any bit, and then if it is like what i want just me pass the real message with the stuffes bits and send a confirmation signal. 

 

So is there anyone have an ideas or find smth wrong in my code.
0 Kudos
2 Replies
Altera_Forum
Honored Contributor II
444 Views

I did not check your vhdl code, but your assumption is wrong. The normal size of a can frame is not 108 bits. There is no normal frame size. The frame size depends on the data length and the can identifier (11 bit or 29 bit identifier) of the transmitted frame. The shortest message can be 47 bits (without any stuff bits, 11 bit identifier, 0 data bytes), the longest message is 129 bits (29 bit identifier, 8 data bytes) + stuffbits. 

 

I think an easier way to detect a stuff bit should be something like that. 

 

process(clk, reset_n) 

begin 

if(reset_n = '0') then 

stuffbit <= '0'; 

elsif(clk'event and clk = '1') then 

-- is next bit a stuff bit ? 

if(message_in(4 downto 0) = "11111" or message_in(4 downto 0) = "00000") then 

stuffbit <= '1'; 

else 

stuffbit <= '0'; 

end if; 

end if; 

end process;
0 Kudos
Altera_Forum
Honored Contributor II
444 Views

Thank you for you help... 

 

in my case, the identifier will be 11 bits all the time, but the new problem that you mentioned is "if there is no data".... 

 

but maybe in the beginning, i am going to ignore this....and i will assume that i have 8 bytes all the time...so in this case, i will have 130 bits as maximum number of bits. 

 

But still I dont how long is exactly the message.  

 

So I thought that it is good to assume the maximum and try to remove the stuffed bits and then check the message. 

 

I am sending a can frame with CANoe to the FPGA...so I can somehow write what i exactly want.... but the problem in my code that the bits are not the correct place... 

 

SO i was trying the last days to found a typical COMPLETE can frame... so I can test with modelsim....easier to debug
0 Kudos
Reply