Hi Marius,
Thanks for your answer and sorry for the delay in my reply. I agree with you that implementation of a communication protocol is what will eventually be needed. However, I was struggling with the basics.
To have a full functioning UART for my application, I need the following capabilities:
1. Receive all incoming bytes, independent of how many are sent at a time and make them available to the application without losing any bytes, and without undue delay.
2. Be able to send a variable number of bytes.
I was struggling with part 1 which is what prompted this ticket. The UART echo example provided by NXP as part of the model based toolbox does not work properly, as it can only echo a byte at a time. If one tries to send 2 or more bytes at a time, only the first byte is echoed. The rest go missing. I suspect that the issue is the the interrupt is triggered after the whole packet is read, but as the example only reads one byte, the rest are discarded. To try to resolve this, I had bumped up the rx buffer size to 8 bytes, and I would receive everything, but of course only if the interrupt was triggered, i.e. if a full 8 byte packet was transferred. If I transferred a 3 byte packet, followed by a 5 byte packet, all of those 8 bytes were lost. I suspected this to be due to the UART rx block being triggered by the time step, vs being fully interrupt driven.
I was able to get around this restriction by moving the UART rx block to the rx ISR, reading 1 byte at a time, and ensuring all bytes are captured in a circular buffer. I created a block that then outputs a fixed size rx buffer containing the new bytes at index 0-num_bytes where num_bytes is an output variable which lets the application time step know how many bytes were received. See attached model. This works well to satisfy condition 1.
Now for part 2 - sending or echoing variable size packets. I applied the same principle to send a single byte at a time by loading an interrupt driven tx block one byte at a time from a tx circular buffer. The idea is to send the next byte whenever the previous transfer is done so as not to overwhelm the uart module. This also works well.
So problem solved. We have a fully functioning UART echo that also makes the received bytes available to the timestep. Hope the attached model can help others in the community.
One last note to help others, the NXP blockset does not seem to respect execution order. If a Simulink block feeds into a UART Tx block for example, there is no guarantee that the Simulink block will execute first. That is dangerous IMO, leading to unexpected behavior and frustration. The work around is to set priorities in the blocks, but that is not ideal and should ideally be addressed by NXP in future releases.
Thanks,
Nick