There is a bug in the ATMEL ASF freertos peripheral control demo using freertos_usart_serial_read_packet()

Hello, There is a bug in the freertos peripheral control demo available on ASF. http://asf.atmel.com/docs/latest/sam3x/html/groupfreertosservice__group.html In particular there is a software architecture issue with management of the circular buffer for the USART demo. The function, freertosusartserialreadpacket(), under certain circumstances mishandles the read and write pointers and concludes the circular buffer is empty when it is actually full. The bug involves the function, freertosusartserialreadpacket() (line 491), and the ISR Handler (line 658) in file freertosusartserial.c.
freertos_usart_serial_read_packet()  calls:
     freertos_copy_bytes_from_pdc_circular_buffer() in file
freertos_peripheral_control.c line 181 which manipulates
p_rx_buffer_details->next_byte_to_read (the read ptr) @ lines 243
and 252
     configure_rx_dma(usart_index, data_removed)    which
manipulates rx_pdc_parameters.ul_size (the size for dma transfer) @
lines  609, 611, 618, and 623

and the ISR Handler which calls:
     configure_rx_dma(usart_index, data_added); which manipulates
rx_pdc_parameters.ul_addr (the write ptr) @ lines 704 and 711
The function, freertoscopybytesfrompdccircularbuffer(), determines how much data is available in the dma buffer. If data is available, it copies it from the dma buffer to the user buffer and then updates prxbufferdetails->nextbytetoread. Then, if data was copied from the buffer and the DMA controller is stopped, it calls configurerxdma(usartindex, dataremoved) to restart the dma controller to fill in the area in the dma buffer that was opened up by the copy from the dma buffer (the read). When the DMA transfer (started by the read above) is complete, the Handler is called @ line 694. The Handler then manipulates rxpdcparameters.uladdr (the write ptr) to set up the next write into the dma buffer and calls configurerxdma(usartindex, data_added) line 717 to restart the dma engine if there is space available to fill in the dma buffer. This works unless a call to freertosusartserialreadpacket() is interrupted between the update of prxbufferdetails->nextbytetoread and the subsequent call to configurerxdma(usartindex, *dataremoved) by a dma transfer completion interrupt which updates rx_buffer_definition->rx_pdc_parameters.ul_addr and calls configure_rx_dma(usart_index, *data_added). The logic in
configurerxdma to decide whether the buffer is empty or full line 601 becomes confused and makes the wrong choice.
  1. By example if we start out with the buffer full (i.e. both the read and write pointers are equal and the DMA controller is stopped).
  2. Then a call to freertosusartserialreadpacket reads one byte which starts the dma engine with a size of 1 to fill in the byte we read. The read pointer is 1 byte ahead of the write pointer and the dma controller is running.
  3. Then another call to freertosusartserialreadpacket reads one byte. Because the dma controller is still running configurerxdma(usartindex, dataremoved) is not called. The read pointer is 2 bytes ahead of the write pointer.
  4. Then another call to freertosusartserialreadpacket read one byte occurs. The read occurs but after line 256 in freertoscopybytesfrompdccircularbuffer() and before**executing line 561 the dma controller receives the byte started in step 2 and triggers an interrupt.
  5. The interrupt runs and immediately restarts the dma controller via a call to configurerxdma(usartindex, dataadded) to fill in the two bytes from steps 3 and step 4.
  6. Before execution can resume at line 561 (other interrupt processing and such) the dma controller completes the transfer of the two bytes in step 5 and generates an interrupt.
  7. The interrupt handler advances the write ptr by 2 and the read and write ptr are now equal and calls configurerxdma(usartindex, dataadded). This call correctly identifies the buffer as full and stops the dma controller by setting size to 0 line 609. The read and write pointer are equal and the dma controller is stopped because the buffer is full.
  8. Finally execution resumes at line 561 (from step 4) and subsequent call to configurerxdma(usartindex, dataremoved). Seeing that the read and write pointers are equal and assuming the cause was the read in step 4 incorrectly determines the buffer was empty and at line 612 starts the action to refill the entire buffer destroying the valid data in the dma buffer.
The bug in the freertosreadpacket() function occurs because between updating the prxbufferdetails->nextbytetoread and the call to configurerxdma(usartindex, dataremoved) a previous dma transfer
completes. After the interrupt service routine completes the subsequent call to configurerxdma(usartindex, dataremoved) resumes execution it can misinterpret the state of the full buffer as empty. The update of prxbufferdetails->nextbytetoread and the call to configurerxdma(usartindex, dataremoved) cannot be interrupted and hence need to execute in the *same *critical section. Additionally line 627 of configurerxdma() incorrectly performs a sanity check to make sure the dma controller is not programmed to write beyond the dma buffer length. configASSERT((rxbufferdefinition->rxpdcparameters.ulsize + rxbufferdefinition->rxpdcparameters.ulsize) <= rxbufferdefinition->pastrxbufferendaddress); should read configASSERT((rxbufferdefinition->rxpdcparameters.uladdr + rxbufferdefinition->rxpdcparameters.ulsize) <= rxbufferdefinition->pastrxbufferendaddress);

There is a bug in the ATMEL ASF freertos peripheral control demo using freertos_usart_serial_read_packet()

[I deleted the duplicate post] Thank for taking the time to provide such detailed information. It will take me a while to digest what you have written – and will report report back to Atmel if appropriate. Regards.

There is a bug in the ATMEL ASF freertos peripheral control demo using freertos_usart_serial_read_packet()

I want to ensure my report to Atmel is correct, so see if you agree with the following. The existing code in freertosuartserialreadpacket() is like this: ~~~~ /* Copy as much data as is available, up to however much a maximum of the total number of requested bytes. */ bytes_read += freertos_copy_bytes_from_pdc_circular_buffer( &(rx_buffer_definitions[usart_index]), all_usart_definitions[usart_index].pdc_base_address->PERIPH_RPR, &(data[bytes_read]), (len – bytes_read)); /* The Rx DMA will have stopped if the Rx buffer had become full before this read operation. If bytes were removed by this read then there is guaranteed to be space in the Rx buffer and the Rx DMA can be restarted. */ if (bytes_read > 0) { taskENTER_CRITICAL(); { if(rx_buffer_definitions[usart_index].rx_pdc_parameters.ul_size == 0UL) { configure_rx_dma(usart_index, data_removed); } } taskEXIT_CRITICAL(); } ~~~~
and the fix would change it to be: ~~~~ taskENTERCRITICAL(); { /* Copy as much data as is available, up to however much a maximum of the total number of requested bytes. */ bytesread += freertoscopybytesfrompdccircularbuffer( &(rxbufferdefinitions[usartindex]), allusartdefinitions[usartindex].pdcbaseaddress->PERIPHRPR, &(data[bytesread]), (len – bytes_read)); /* The Rx DMA will have stopped if the Rx buffer had become full before this read operation. If bytes were removed by this read then there is guaranteed to be space in the Rx buffer and the Rx DMA can be restarted. */ if (bytes_read > 0) { if(rx_buffer_definitions[usart_index].rx_pdc_parameters.ul_size == 0UL) { configure_rx_dma(usart_index, data_removed); } } } taskEXIT_CRITICAL(); ~~~~ So the critical section has been removed from inside the if() to around the entire code section. Do you agree? Regards.

There is a bug in the ATMEL ASF freertos peripheral control demo using freertos_usart_serial_read_packet()

Yes! I think that would work. But it seems a like a lot of code to put into a critical section. I rewrote freertoscopybytesfrompdccircularbuffer and moved the critical section out of there and joined it with if (bytesread > 0) { if(rxbufferdefinitions[usartindex].rxpdcparameters.ulsize == 0UL) { configurerxdma(usartindex, data_removed); } }