STM32 debugging can bus frame loss problem

Publisher:delta14Latest update time:2019-03-11 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Background of the problem

I need to write a new motor driver, using CAN communication. The driver is relatively simple and was written quickly. When I tested a motor by myself, there was no problem, and the forward and reverse rotation tests were all normal. I thought things would go smoothly, but when I tested two motors, I found serious problems, and the left motor often did not install or the right motor did not rotate.


Steps to solve the problem


Option One: 


1. When encountering this kind of problem, we usually add a delay, thinking that it may be sent too quickly, and there may be a problem with the timing, but our system is a real-time system and the delay cannot be too long. 


2. After testing, it was found that adding a delay may reduce the probability of this problem, but it cannot solve the problem. In the end, the car still cannot operate normally. There is no way this solution will work.


analyze: 

Since adding a delay doesn't work, we can only find out why the motor is not installed. After using CAN to USB to capture the data, we found that the motor does not rotate because of frame loss. Each motor needs 2 frames of data to send data, and two motors have 4 frames of data. But only 3 frames of data can be captured. One frame of data is lost. Then this may be because the CAN sending mailbox is not enough, resulting in frame loss.


background knowledge: 

send


3 email addresses

The priority of sending messages can be configured by software

Record the timestamp of when SOF is sent

take over


2 receive FIFOs with 3 levels of depth

14 variable width filter banks – shared by the entire CAN

Identifier List

FIFO overflow handling is configurable

Record the timestamp of receiving SOF


Write the picture description here


Therefore, my next analysis is to check the number of free registers in the CAN mailbox to see if it is because when 4 frames of data are sent continuously, the CAN free mailbox is not enough. 


Write the picture description here

Write the picture description here


Therefore, before each transmission, I check bits 26, 27, and 28 of this register to count the number of available mailboxes.


Option II: 

Before sending, we count the number of available mailboxes. We will only send new data if there are mailboxes. 

result: 

The result was unexpected. Every time I sent a message, the number of mailboxes displayed was always 3, which means there was no shortage of mailboxes. Solution 2 was not feasible.


Continue analysis: 


 Write the picture description here 


It was found that when running, this bit would often be set to 1, not because of insufficient mailboxes to lose data, but because of arbitration to lose data. 

Further verification: 


 Write the picture description here 


It is found that this register always replies to mailbox 0, so it is not a problem of insufficient mailboxes. It is completely sufficient to send 4 frames of data every 50ms.


third solution: 

Because the ID of the sent data is larger than the ID replied by the slave, the data frame arbitration may be lost when the CAN is preempted because of the larger ID. 

Therefore, the automatic resend function of CAN is enabled. 

Using the hal library is just one sentence:


g_sCAN_Handler[dwDevice].Init.NART = DISABLE; // Enable automatic retransmission


Results Problem, Perfect Solution


Keywords:stm32 Reference address:STM32 debugging can bus frame loss problem

Previous article:STM32 hal library analysis uart
Next article:LIN bus development: STM32F0 TJA1020

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号