0

Macropad tone() function broken in Arduino
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Macropad tone() function broken in Arduino

by RufusVS2020 on Sat Jul 31, 2021 5:07 pm

I got my Adabox today and wanted to drive the speaker. I found two different arduino sources for the demo. One that was shipped had the self-test code in it and a digikey splash screen included, but no speaker code. The other I found had what looked like the start of speaker code that was commented out. I tried to uncomment the code and it would lockup my macropad after the digikey splash came up. I bitbanged the PIN_SPEAKER and PIN_SPEAKER_ENABLE keys and got a sound out of it. I can only conclude that the tone() function is broken in the arduino macropad code. I am going to see if the CircuitPython version fares better.

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Macropad tone() function broken in Arduino

by RufusVS2020 on Sat Aug 21, 2021 8:52 pm

I was able to do some more testing, and found out that the tone function is not broken, but there is a conflict with
the tone generator and the neopixel code. I essentially #if'd out all the pixel calls, but left in the encoder, display, i2c
calls in the demo code, and the tones worked fine (along with the keys displayed, the i2c scanning, and the encoder
position all displayed). The only conclusion I can reach is the tone function is kiboshing some interrupt or register
that the neopixel code is using "in the background.".

Here's my modified example program. It generates quick tones at startup, then pressing the bottom left key it will
play ascending/descending tones.
Attachments
macropad_arduino_example.ino
Example modified demo program to show neopixel/speaker code conflict. press key 12 to play tones after loading.
(6.15 KiB) Not downloaded yet

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Macropad tone() function broken in Arduino

by RufusVS2020 on Sun Aug 22, 2021 4:52 pm

Following up: It appears both the NeoPixel code and the Tone generator code DO use the PIO of the RP2040 (which I was happy to find out!).
I was able to make it work by modifying the NeoPixel library file rp2040.c to have it use PIO1 instead of PIO0. I don't know what that will break
in the rest of the library, as I don't know what uses PIO1.

It seems to me they should be able to coexist, as they are both blocking functions (tone and show), if more analysis is done.

If anyone wants the changed rp2040.c code, I have attached the file here. Replace your library version (after backing it up somewhere) and try it out.

People more familiar with the rp2040 Macropad Arduino project, please pipe in with pio1 conflicts.
Attachments
rp2040.c
Modified file to use pio1 for pixel updates
(1.2 KiB) Downloaded 4 times

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Macropad tone() function broken in Arduino

by RufusVS2020 on Sun Aug 22, 2021 5:21 pm

I may be mistaken about blocking, as they probably don't wait for the PIO FIFOs to empty. More research required...

RufusVS2020
 
Posts: 35
Joined: Thu Nov 19, 2020 6:31 pm

Re: Macropad tone() function broken in Arduino

by fraxiinus on Wed Sep 08, 2021 9:45 pm

I'm running into the same thing and I believe it has to do with the fact that tone() depends on interrupts but NeoPixels disables them.

I found that information from this GitHub issue: https://github.com/adafruit/Adafruit_NeoPixel/issues/10

I going to look into alternatives to tone() and try to do it deterministically. Adafruit does have a guide on the same issue, but with Servos: https://learn.adafruit.com/neopixels-and-servos

fraxiinus
 
Posts: 1
Joined: Wed Sep 08, 2021 5:13 pm

Please be positive and constructive with your questions and comments.