I'm looking to use the DMA to receive UART packages of variable length, but it seems that the DMA needs to know in advance how many bytes I need plan to receive. Is there any way around this?
A solution to this is if your packet doesn't already have a header, add one to it.
In the header you will need to specify the size of the data you expect to receive.
Once we have this, the ADuCM360 UART RX DMA can be configured in ping pong mode.
The primary structure will be configured to receive the header which in this example is 6 bytes.
The alternate structure will be configured to receive the maximum size of data that you expect to receive.
Now when a variable number of bytes are send to the aducm360, the first 6 bytes are stored in the primary buffer and the DMA hardware automatically switches over to the alternate DMA. At this point a DMA interrupt will also be generated. In this interrupt we can handle the header and we can extract the number of data bytes we expect to receive.
Now that we have this information we can periodically poll the n_minus_1 field in alternate UART RX dma structure until we have received the full packet.
After we've received the entire packet, we just need to reconfigure the primary and alternate structures and we need to tell the DMA to switch back to the primary DMA for the UART RX. This is done by
In the example attached the UART RX DMA interrupt is only taking care of interrupts from the primary channel so we need to ensure that the alternate channel doesn't generate interrupts. This is simply done by configuring the alternate channel to expect 1 or more bytes that the maximum number expected to be received.
To test the code just send the following bytes over UART at 115200 to the ADuCM360 and they will be returned back.
0xb xa 0xcf 0x4 0xc 0xc 0xc 0xc 0xc 0xc
In the example code I'm using 6 bytes for the header (arbitrary value), and the 4th byte secifies how many data bytes are expected.
So you can modify the bytes sent to the ADuCM360, but if you modify the 4th byte please ensure that you send a matching number of data bytes.
The library functions are not attached in this zip file. Just unzip the file in C:\ADuCMxxxV1.3\code\ADuCM360\examples or whereever you installed the ADuCM360 DVD and the project should compile without any issues. If you don't already have the DVD installed you can download it first from ftp://ftp.analog.com/pub/MicroConverter/ADuCMxxxV1.3/
Retrieving data ...