AnsweredAssumed Answered

UART-RS485 introduce unnecessary delays

Question asked by vines on Jun 14, 2018
Latest reply on Jul 11, 2018 by Daniel Chen



I hope somebody can help me with my current issue. First, I will give a brief introduction to what I am using.


MCU: K60


Compiler: cw10 ( I cannot convert to KDS or Expresso with gcc as I time won't allow me to port over)

Serial Used: UART4 with a medium priority interrupt


Master and Multiple Slave (Polling method - half duplex)

adjustable Baud (19200 to 115200)



Unnecessary delays in between Master query and Slave response


Scenario 1:

Baud 19200

Master continuously polling (round robin) to all slaves.


As you can see from the figure below, there was a delay of approximately 13ms between the master query and the slave's response. In my code, I did some processing in between but not too drastic that will cause 13ms. I tried to debug the code on the slave's side, I found out that the OnBlockReceived (Processor Expert generated) didn't fire until 9ms later after the last byte of the master query was received. Since I am on interrupt, I should be able to get the trigger from the moment I receive the first byte. However, the OnBlockReceived triggered me not only on the last byte but with an addition of 9ms delay. Hence, my code was able to process the received data only then. The figure below was captured when the master is continuously polling the slaves (I am not on debug or I didn't halt the system). The next figure and scenario below will give out further evidence on the delay.  


Scenario 2:

Baud 19200

Master is on debug allowing 1 step through to the target slave. 

Same setup and code as Scenario 1. But instead of running the code I put a halt allowing 1 poll at a time.


As you can see from the figure below, the delay between the master query and the slave's response was shortened considerably to 4ms. This is acceptable to me. So this somehow coincides with my reading of additional 9ms delay by the late trigger of the interrupt. If you add the 2 it will end up 13ms delay.  


I did a similar test using a 115200 baud rate. It becomes worst. 


Scenario 3: 

Baud 115200

Same setup and code as Scenario 1. 


As you can see the delay was even worse than 19200. The delay now is roughly 35ms. But again, when I did a step through on the code the delay is minimized. See Scenario 4. 


Scenario 4: 

Baud 115200

Same setup and code as Scenario 2. 


As you can see, the delay was also around 4ms same as at 19200 baud when doing a halt on the debugging.  




Additional info:

The data is complete and correct. I don't have any issue with the integrity of the data. It is only the additional delay that I am having issues. 


Code snippet:

I have a circular buffer in my code where I store the data I obtained from the UART. I retrieve the data 1 byte at a time. Again, as mentioned data integrity is not an issue. I can receive and send the correct data without a problem. 


void Hal_Mcu_Rs485_OnBlockReceived(LDD_TUserData *UserDataPtr)
   if (m_Rs485BufCtrHead > RS485_BUF_SZ)
      m_Rs485BufCtrHead = 0;


I know that this is probably longer to your liking but hopefully somebody can help me or at least point me to a correct direction on how to fix this problem. I will appreciate any help that is given.


Thank you very much and have a good day!