I'm working on a distributed real-time application, where 4 MCUs (PIC18F45k22) are connected to a network. The real-time kernel FreeRTOS is running on each of the four PICs, and all MCUs exchange data in real-time through the network (bus), in order to accomplish their functions (sensing, regulation, control, display etc...).

The PICs that I use do not include a CAN module, and I cannot get an external one (or any other real-time communication protocol module). The only solution is to use I2C bus, which is not designed for real-time communication.

In order to improve my communication protocol, I'm trying to implement (in software) some algorithms that can manage message scheduling through the bus using I2C arbitration, and also implement error detection using CRC, and I really don't know where to start. I'm not even sure if this solution is valid for a real-time application.

Does anyone have an idea or advice? Is this solution valid? If so, how can I implement CRC (how to choose the appropriate polynomial)?

  • \$\begingroup\$ Your title does not match your question. Focus please and note that any serial comms is as good as any other (barring speed restrictions) for real time stuff. \$\endgroup\$
    – Andy aka
    Jun 10, 2020 at 11:09
  • 2
    \$\begingroup\$ What do you mean it's not meant for real-time comms? What makes you say so? \$\endgroup\$
    – Justme
    Jun 10, 2020 at 11:22
  • \$\begingroup\$ @Justme I'm saying this only because while reading about real-time communication, I2C protocol is never mentioned. And also if it's compaired to frequently used protocols like CAN or LIN or even Time Trigger Protocol, it is more simple and does not manage error detection. That's why I'm not really sure if using this protocol is valid \$\endgroup\$
    – Dot_Pixis
    Jun 10, 2020 at 11:37
  • \$\begingroup\$ @Justme For example, any slave is allowed to extend a transaction by an arbitrary amount of time. That's not a feature I would include if I were designing for real time systems. \$\endgroup\$
    – The Photon
    Jun 10, 2020 at 13:24

2 Answers 2


I think I would assign one as a master. Not to be changed.

The master can message the other 3 on its initiative, and ask the others per x (m)s if they want to say something.


How about implementing external interrupts on master? Imagine you have a slave device that wants to say something. It pulls a master-slave interrupt line low, a master gets an interrupt and initiates a read from that slave. You need one additional pin per slave device, of course, but it's literally the first idea that comes to mind.

Edit: nothing stops you from sending the data in such a way that you can check its integrity. You can send stuff twice or make up some control bit or something.

  • \$\begingroup\$ The communication is actually deterministic: it is periodic, so what I'm doing is to always set the sender as master and the receiver as slave, if the master needs to receive data it is set to slave, and waits for i2c reception interrupt. Also, if the slave needs to send something, it's set to master. \$\endgroup\$
    – Dot_Pixis
    Jun 10, 2020 at 11:49
  • \$\begingroup\$ Not a problem: you can make that interrupt cause master and slave switch roles. But I really see it as a massive overkill in terms of algorithm - reconfiguring I2C every time. I think interrupt signalling as "I have data, read me please" would be simpler. Besides, you'll have no polling, so Master can do whatever in between the reads \$\endgroup\$
    – Ilya
    Jun 10, 2020 at 11:54
  • \$\begingroup\$ There is actually no polling, since I'm using a real-time kernel (RTOS), the code is devided into tasks, so when the a node is waiting for a message, it is set to slave, enables i2c interrupts, end then the communication task is blocked, allowing processor to execute other tasks. when a message is received, an interrupt is generated wich notifies communication task to continue it's execution. \$\endgroup\$
    – Dot_Pixis
    Jun 10, 2020 at 12:14
  • 1
    \$\begingroup\$ As you said, using external interrupt on master from slave is actually a better solution. Thank you for the idea, i'll work on it. \$\endgroup\$
    – Dot_Pixis
    Jun 10, 2020 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.