Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4786

SDK • Avoiding simple mistakes with the SDK

$
0
0
Hey y'all. A thing I've noticed about using the RP2040's peripherals during my experimentation is that it is easy to forget a seemingly innocuous or redundant function call, and have bad things happen. Here's my latest example:

Code:

  irq_add_shared_handler(ADC_IRQ_FIFO, my_adc_irq_handler, 128);  irq_set_enabled(ADC_IRQ_FIFO, true);  irq_set_priority(ADC_IRQ_FIFO, 0);  adc_select_input(0);  adc_set_round_robin(0b11);  const uint32_t clock_hz = clock_get_hz(clk_adc);  const uint32_t target_hz = 500;  const float divider = 1.0f * clock_hz / target_hz;  adc_set_clkdiv(divider);  adc_fifo_setup(true, false, 2, 0, false);  adc_fifo_drain();  adc_irq_set_enabled(true);  // OOPS -- initially forgot this line.  adc_run(true);
As you can see, I did adc_fifo_setup to enable the FIFO and set its threshold. I also set-enabled the ADC_IRQ_FIFO. But I forgot to do adc_irq_set_enabled. To a newbie, it looks like I've already signalled two different ways that I want the ADC FIFO to send an IRQ, but if you forget the third one, nothing happens! Doh!

As you can see, I did eventually resolve the issue by hunting down some example code and going through it line by line. I seem to hit issues like this fairly often, and I attribute that partly to how much of a newb I am at embedded programming. Possibly over time I will just come to be faster at diagnosing issues like this. But I am curious if you have any tips on how to get there faster. If you were staring at this code and that one line was missing, how would you have gone about diagnosing the issue quickly?

Statistics: Posted by jags84 — Wed Apr 17, 2024 1:25 am



Viewing all articles
Browse latest Browse all 4786

Trending Articles