sparkfun / Arduino_Apollo3

Arduino core to support the Apollo3 microcontroller from Ambiq Micro
83 stars 39 forks source link

tone() does not work correctly on V2.x.x #471

Open paulvha opened 2 years ago

paulvha commented 2 years ago

Error description The command tone() is not generating a PWM signal on the pins.

Reproduce try tone(A16,200) in a sketch and it will fail (as with any other pin)

Root cause error in the library file : SparkFun/hardware/apollo3/2.1.1/cores/arduino/sdk/core-implement/CommonAnalog.cpp, function : void indexTone(pin_size_t index, unsigned int frequency, unsigned long duration) at the end of the function (line 184) the duration handling check is incorrect. If duration = 0 (which means NO duration check needed) the tone should continue until the user stops with notone(). Now it will stop tone immediately.

if(duration){
        uint32_t stop_time = millis() + duration;
        while(millis() < stop_time){};
    }

   ap3_pwm_output(pad, 0, 0, clk);

Change this to :

    if(duration){
        uint32_t stop_time = millis() + duration;
        while(millis() < stop_time){};
        ap3_pwm_output(pad, 0, 0, clk);
    }

It will then work as it should. The error is on all the V2 library versions. V1x library has 2 different tone() calls one with and one without duration and thus is works correctly there.

regards, Paulvha