I'm writing firmware for an ATSAM4S (runs at 120 MHz)and I noticed a strange issue with ADC interrupt handler. I set up ADC at 20 MHz, 5 enabled channels, and DMA transfer complete interrupt. According to calculations interrupt timing is fine. It triggers when 5 conversations are completed.

I don't have any code to post at this moment but my question is quite clear. I have a print function which prints out to a UART port the result of ADC (for debugging), in an infinite while loop. I noticed that main function (print out function) freezes completely or slows down when I put too many lines of code into the interrupt handler of ADC. By too many lines, I mean further processing of ADC results like calculations of mean value, calculations of mean value above and/or below of a specific threshold, in all channels...

Does that mean that ADC takes too much machine cycles that once it's finished, another interrupt is waiting to be carried out (due to next conversion) and it fires immediately the interrupt handler before any piece of code executed in main function?

  • 2
    \$\begingroup\$ It sounds as though that's the case, you're spending too much time in the interrupt context. You could prove that by toggling a pin at the start and end of the irq handler and watching it on an oscilloscope. \$\endgroup\$ – Colin Mar 26 at 9:37
  • \$\begingroup\$ If your UART is working on the interrupt , you may be blocking the UART interrupt by staying long in ADC interrupt? \$\endgroup\$ – MaNyYaCk Mar 26 at 9:43
  • \$\begingroup\$ Very good idea but I think there's a limitation of the frequency of GPIO. \$\endgroup\$ – MrBit Mar 26 at 9:44
  • 2
    \$\begingroup\$ Interrupt routines should never do any more than they absolutely have to do to service the external device. Once the data is in, process it in the main loop. It may be that you want to do too much processing for your MCU, in which case you're doomed anyway. \$\endgroup\$ – Neil_UK Mar 26 at 10:10
  • 2
    \$\begingroup\$ If that's the case, you're doomed anyway as Neil says, it sounds like you need a quicker processor. Your ADC interrupt should just put the data into a buffer, or ideally use a DMA for that and get rid of the ADC interrupt. \$\endgroup\$ – Colin Mar 26 at 10:53

Your Answer

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

Browse other questions tagged or ask your own question.