Unsure, how to use interrupt UART API #68006
Replies: 4 comments 2 replies
-
@TorstenRobitzki, To get a better understanding of the UART API, I suggest searching the code base for instances of the functions Additionally, for a practical demonstration, take a look at the CDC ACM sample in the Zephyr Project repository. |
Beta Was this translation helpful? Give feedback.
-
Hi @ndrs-pst The CDC ACM sample, just copies the received data via a ring buffer to the output, and just logs the case, where So, I'm going to read the Thank you! Torsten |
Beta Was this translation helpful? Give feedback.
-
I ran into the same issue right now. I used to use ring buffers (which usually should be safe but technically are not). Adding a semaphore makes it even more obvious: If anything prevents me from putting my read data to it's buffer (or the other way around, prevents me from taking data from the buffer to send it) the API gets kind of stuck as all it's functions require by API definition to be called from the UART ISR (otherwise behaviour is undefined). So in case of RX the data will be processed when the next data arrives, causing a new interrupt. In case of TX it is even stuck as there won't be a new even at all (the IRQ must be disabled while the UART is still in transmit, this is especially bad if the TX Done is needed for some reason). Overall it seems unsave. The sample for UART and USB CDC ACM both just loop inside their ISR (which means the TX handler has to disable the TX IRQ or the ISR will stay in the loop). I can't get another TX interrupt if I can't fill it immediately and a fill without interrupt is not supported. |
Beta Was this translation helpful? Give feedback.
-
I finally solve my issue with the API, by writing a thread safe queue (ring buffer), that gives me access to the size of put and get areas. For example, on the reading end of the UART:
As long, as there is only one producer, putting data into the buffer, it is safe to assume, that the area, the data can be put into, only grows if there are concurrent access from the consumer. The loop in this example is just to cover the case, where the ring is split and a second call to From a user perspective, having the choise between 3 different APIs sounds very promising. But when I have to take into account, that a vendor could always skip one of the implementation, I end up with implementing all 3 APIs if I want to be platform neutral. Given the implementation above, one could argue that implementing that directly on the peripherals would probably end up in way less code, that would be easier to understand. |
Beta Was this translation helpful? Give feedback.
-
Hello,
for the nrf53 the asynchronous UART API is not supported for USB ACM. Now I try to figure out, how to pass data received from the UART to the main thread of the application and vise versa.
I think I should use a k_pipe to pass data from the interrupt service routine to the main thread (feedback on that is welcome).
As there is no API to get access to a put area of the pipe, I request the available room in the pipe used to received data (k_pipe_write_avail()), copy data from the UART to a local buffer (uart_fifo_read()) and then write that local buffer to the pipe (k_pipe_put()). This extra copy through the local data feels already clumsy and I wonder, if it could be avoided.
On the other side, where I receive data from the main thread, to be send in the ISR, I can request a minimum size of data in the pipe (k_pipe_read_avail()), but when feeding that data to the uart, I could end up in a situation, where uart_fifo_fill() will return a size less than what has been read from the pipe. I would have to store that data, that was read from the pipe but was not filled into the fifo somewhere, and feed that second buffer to the fifo, once there is room again in the uart. This feels super clumsy and unnecessary complicated.
Am I'm missing something here? I'm super new to Zephyr and maybe I have picked the wrong primitives, or even using the totally wrong approach here?
thanks in advance,
Torsten
Beta Was this translation helpful? Give feedback.
All reactions