6285 views|11 replies

1366

Posts

6

Resources
The OP
 

About bus conflicts (Part 1) [Copy link]

 
This is also a series of posts, about the topic that I always want to know: about the bus. I always want to know how to learn CAN, but I want to write two posts . If the author uses inappropriate words in the post, I hope you can communicate and correct them. In this post, I will use half-duplex bus as the theme of the bus, one RS485 bus and one CAN bus. Because of half-duplex, in most cases, we need to consider a problem: in the case of multiple slaves, how to avoid "simultaneous" sending to the host, how to send in order without causing bus chaos, this is a problem that needs to be considered when there is only one bus. Especially when you need to broadcast to read a certain information from the slave, if you don't have an anti-collision mechanism, the messages sent on the bus will probably be a bunch of garbled codes. This post mainly describes the RS485 bus, and the MCU uses STM32. The RS485 bus does not have a hardware bus arbitration priority like CAN. To be precise, all nodes can be either the master or the slave. How to prevent the bus from causing conflicts? For RS485, the host also has a solution, which is to use each node with a node address (dip switch, information hard-coded binding, etc. are all possible), and use software delays to detect whether the bus is idle + DMA sends to quickly seize the bus to prevent conflicts. The following host only explains its principle process. The code screenshot process may not be very clear, but I will try my best to make the idea clear. This content is originally created by RCSN, a netizen of the EEWORLD forum. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source





This post is from stm32/stm8

Latest reply

, good abuse  Details Published on 2018-11-25 01:53

赞赏

1

查看全部赞赏

Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 

1366

Posts

6

Resources
2
 
Here, the author still uses a broadcast to read the software version number, and each node slave returns the version number as an example to explain. The RS485 bus needs a conversion chip to communicate with the microcontroller, and must convert TTL, that is, the corresponding serial port. Common chips include Maxim's MAX485, etc. There will be a transmit and receive enable pin. The two enable pins should not be enabled at the same time. Once powered on, the default is to enable reception. When there is data to be sent, the transmission is enabled again. After the transmission is completed, the reception is enabled again. In this way, polling is performed for communication. In the end, it is still a serial port problem, but compared with the serial port, it is more important to note that the transmission and reception cannot be simultaneous. In bus communication, the author is accustomed to using FIFO queues to send and receive data for asynchronous processing, that is, no matter whether there is data received or to be sent, all are put into the FIFO queue. Of course, if you use RTOS, you can also use message queues to send and receive data. So we need to do an initialization step: initialization of some related variables, initialization of FIFO queues, initialization of related IO, initialization of related interrupts. Then in terms of interrupts, we enabled three interrupts, one for the receive completion interrupt, the idle interrupt, and the DMA send completion interrupt (the HAL library used by the OP, the DMA send completion of the HAL library will enable the serial port send completion interrupt). So in the global interrupt we need to judge these three interrupt events and do some corresponding event logic processing.
This post is from stm32/stm8
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 

1366

Posts

6

Resources
3
 
The analysis of the receiving part will not be explained, because the things involved seem to deviate from the topic. So what we need to consider is how to avoid conflicts between our own nodes and other nodes when sending. We don't need an idea yet, that is, to judge whether the receiving end level on the bus is pulled down as whether the hardware bus is idle. The previous idle interrupt only plays the role of not sending and receiving data on the node for the first time. To really be able to send in an orderly manner, the host still uses different nodes with different delays to block to judge whether the receiving end level is pulled down as the sending priority. The lower the address of the node, the easier it is to seize the bus and send first. Then on the sending interface, we need to judge whether the DMA sending completion flag is established on the DMA sending. If not, an error is returned and the sending continues after one polling. Here we store the data to be sent in a buffer and wait until the bus is seized before sending, so here we need to change the sending status variable. Then poll and detect the status of this variable in the task.
This post is from stm32/stm8
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 

1366

Posts

6

Resources
4
 
Then we can create a thread or task, once every 2ms, to detect whether the anti-collision flag is valid, and then determine whether it is idle to decide whether to send.

TIM截图20181124000932.png (5.53 KB, downloads: 0)

TIM截图20181124000932.png

TIM截图20181124001638.png (54.22 KB, downloads: 0)

TIM截图20181124001638.png
This post is from stm32/stm8
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 
 

6040

Posts

204

Resources
5
 
Thanks for sharing! Like it!
This post is from stm32/stm8
 
 
 

578

Posts

0

Resources
6
 
This mechanism will only work well if all nodes in the entire network comply with it, otherwise conflicts will still occur.
This post is from stm32/stm8

Comments

All nodes comply with this mechanism, which means they share a common mechanism. However, this mechanism cannot guarantee 100% non-conflict. In my actual project, 96 nodes can achieve a 95% non-conflict probability.  Details Published on 2018-11-24 14:00
 
Personal signature刻苦学习,共同进步
 
 

1366

Posts

6

Resources
7
 
lcofjp posted on 2018-11-24 11:11 Thank you for sharing! Like it!
Thank you for the support of the captain: kiss:
This post is from stm32/stm8
 
Personal signature

1084534438 欢迎交流  [加油,一切皆有可能]

 
 

9157

Posts

6

Resources
8
 
Thanks for sharing your experience
This post is from stm32/stm8
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
 
 
 

1366

Posts

6

Resources
9
 
Mayer Fengsui published on 2018-11-24 11:35 This mechanism will only work well if all nodes in the entire network comply with it, otherwise conflicts will still occur
All nodes comply with it, that is, they share a set of such mechanisms. However, this mechanism cannot guarantee 100% non-conflict. In my actual project, 96 nodes can achieve a 95% non-conflict probability
This post is from stm32/stm8
 
 
 

3414

Posts

0

Resources
10
 
, what a rip-off
This post is from stm32/stm8
 
Personal signature

So TM what......?

 

 

2700

Posts

0

Resources
11
 
, it’s really bad
This post is from stm32/stm8
 
Personal signature作为一个水军,就是尽量的多回帖,因为懂的技术少,所以回帖水分大,见谅!
EEWORLD开发板置换群:309018200,——电工们免费装β的天堂,商家勿入!加群暗号:喵
 
 

2700

Posts

0

Resources
12
 
, good abuse
This post is from stm32/stm8
 
Personal signature作为一个水军,就是尽量的多回帖,因为懂的技术少,所以回帖水分大,见谅!
EEWORLD开发板置换群:309018200,——电工们免费装β的天堂,商家勿入!加群暗号:喵
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list