Chapter 5 – RGB LED with PWM + strange hang on delay until with embedded_rp2040 (help wanted)

Hello Ada community,

I have just published Chapter 5 of my Raspberry Pi Pico Ada tutorial series.

Chapter 5: Controlling an RGB LED with PWM

This chapter introduces a small reusable package Pico.Analog.RGB_LED and two example sketches:

  • random colour changes
  • smooth colour-wheel gradient

While developing the first sketch I ran into a puzzling issue that I would like to ask the community about.

I have added a full catch-all exception handler and then the program simply hangs or crashes when execution reaches the line:

    Next := @ + Hold_Period;
    delay until Next;

The same handler works correctly in the second sketch. I added debug output with Pico.UART_IO.Put_Line after every step and still see no exception at all.

Has anyone observed similar behaviour with the embedded_rp2040 runtime and delay until statements? Any hints or known issues would be greatly appreciated before I set up the full SWD debug bridge for deeper investigation.

You can read the complete chapter (including the commented-out handler) here:

The corresponding Alire crate is currently waiting in the moderation queue.

Thank you in advance for any help or suggestions!

Best regards,
Martin Krischik

4 Likes

I loaded it up in the debugger and it looks like the environment task’s stack is overflowing, causing stuff to get corrupted and a HardFault to eventually occur. The default stack size of the environment task is 2 KB with this runtime, but you can increase its size by using the setting the value of __stack_size in your linker arguments via the --defsym switch.

I tried increasing the stack size to 8 KB by changing your linker arguments to this and it seems to work:

   package Linker is
      for Switches ("Ada") use Runtime_Build.Linker_Switches & 
         ("-Wl,--gc-sections", 
          "-Wl,--defsym=__stack_size=8192");
   end Linker;

Note that for other Ada tasks (declared with the task keyword), you can set their stack size with the Storage_Size pragma. For example:

task Example_Task with Storage_Size => 8192;
1 Like