I am designing a board with STM32F107 microcontroller and AD7680 ADC. What I need is to read data from ADC, increment sequence number after reading each sample, and send ADC values via UART or USB OTG (USB CDC device) to a computer. Also I need to catch rising edge on a single GPIO trigger pin and remember the sequence number in the time of triggering.

The main constraint is the speed - I need to make it as fast as possible. Since I don't have to much experience with Cortex MCUs, I have two questions:

  • Is it possible to achieve this scenario using DMA (i.e. without some loop which will consume all CPU time)?

  • I know, it's difficult, but can somebody estimate the maximal achievable sampling rate (5 kHz, 20 kHz, 50 kHz...)?

Thank you in advance, any suggestions are welcomed.



DMA can be used, but it makes sense only if you gather the samples in "blocks". For example, DMA grabs 100 ADC samples and puts it in some area in memory. Once it's done, you send the whole block through the UART, using another DMA channel and simultaneously, the first DMA channel grabs another 100 ADC samples to another area in memory. Repeat.

However, it doesn't work the same with the USB interface on STM32F107. DMA does not directly interact with USB, because USB has a dedicated packet buffer. However, you could use DMA to transfer data to this packet buffer.

However, you still must have a main loop, obviously. Only it will be called every 100 samples instead of for each sample, which will free some CPU power. But the compromise is that there is additional latency to get the samples.

Now, as to estimate the maximum data rate, there are three limiting factors: the CPU, the ADC, and the output communication interface. I think the CPU, unless you run it at a very low frequency, will not be the bottlneck. The bottleneck will be the peripherals. The ADC does the conversion in about 1µS, so it's ok for the rates you mention in your question. Now, the output peripheral: if it is the UART, it all depends on the baud rate you intend to use, and the math is quickly done: at 4.5 Mbit/s (maximum speed for USART1), with 16 bits per sample (ADC is 12 bit, but you have to round the sample size to a whole byte), it makes about 280 ksamples/seconds. Maybe you will have some overhead due to the protocol, so make it a little less. For USB, the math is the same, except that the communication speed is 12MBit/sec, but the protocol overhead will be greater. In the end, you will be able to go at least as fast as with the USART.

Edit: Sorry, I just realized the ADC is external. But it doesn't change what I said, except the samples come from SPI (and DMA should interact with SPI, then). So the limiting factor here will actually be the ADC itslef, at 100ksps.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.