@@ -426,8 +426,8 @@ void ap3_pwm_wait_for_pulse(uint32_t timer, uint32_t segment, uint32_t output, u
426426
427427// **********************************************
428428// ap3_pwm_output
429- // - This function allows you to specify an arbitrary pwm output signal with a given frame width (fw) and time high (th).
430- // - Due to contraints of the hardware th must be lesser than fw by at least 2.
429+ // - This function allows you to specify an arbitrary pwm output signal with a given frame width (fw) and time high (th).
430+ // - Due to contraints of the hardware th must be lesser than fw by at least 2.
431431// - Furthermore fw must be at least 3 to see any high pulses
432432//
433433// This causes the most significant deviations for small values of fw. For example:
@@ -449,7 +449,7 @@ void ap3_pwm_wait_for_pulse(uint32_t timer, uint32_t segment, uint32_t output, u
449449//
450450// ...
451451//
452- // Then we conclude that for the case th == (fw - 1) the duty cycle will be 100% and
452+ // Then we conclude that for the case th == (fw - 1) the duty cycle will be 100% and
453453// the percent error from the expected duty cycle will be 100/fw
454454// **********************************************
455455
@@ -458,7 +458,8 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
458458 // handle configuration, if necessary
459459 ap3_err_t retval = AP3_OK;
460460
461- if ( fw > 0 ){ // reduce fw so that the user's desired value is the period
461+ if (fw > 0 )
462+ { // reduce fw so that the user's desired value is the period
462463 fw--;
463464 }
464465
@@ -506,7 +507,7 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
506507 }
507508 }
508509 else
509- { // Use the 0th index of the outcfg_tbl to select the functions
510+ { // Use the 0th index of the outcfg_tbl to select the functions
510511 timer = OUTCTIMN (ctx, 0 );
511512 if (OUTCTIMB (ctx, 0 ))
512513 {
@@ -519,16 +520,17 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
519520 }
520521
521522 // Ensure that th is not greater than the fw
522- if (th > fw){
523+ if (th > fw)
524+ {
523525 th = fw;
524526 }
525527
526528 // Test for AM_HAL_CTIMER_OUTPUT_FORCE0 or AM_HAL_CTIMER_OUTPUT_FORCE1
527- if (( th == 0 ) || ( fw == 0 ))
529+ if (( th == 0 ) || (fw == 0 ))
528530 {
529531 output = AM_HAL_CTIMER_OUTPUT_FORCE0;
530532 }
531- else if ( th == fw )
533+ else if ( th == fw)
532534 {
533535 output = AM_HAL_CTIMER_OUTPUT_FORCE1;
534536 }
@@ -592,14 +594,17 @@ ap3_err_t analogWriteResolution(uint8_t res)
592594ap3_err_t analogWrite (uint8_t pin, uint32_t val)
593595{
594596 // Determine the high time based on input value and the current resolution setting
595- uint32_t fw = 0xFFFF ; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
596- if ( val == ((0x01 << _analogWriteBits ) - 1 ) ){
597- val = fw; // Enable FORCE1
598- }else {
599- val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
600- }
601- uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
602-
597+ uint32_t fw = 0xFFFF ; // Choose the frame width in clock periods (32767 -> ~ 180 Hz)
598+ if (val == ((0x01 << _analogWriteBits) - 1 ))
599+ {
600+ val = fw; // Enable FORCE1
601+ }
602+ else
603+ {
604+ val <<= (16 - _analogWriteBits); // Shift over the value to fill available resolution
605+ }
606+ uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ; // Use an Ambiq HAL provided value to select which clock
607+
603608 return ap3_pwm_output (pin, val, fw, clk);
604609}
605610
@@ -627,3 +632,40 @@ ap3_err_t servoWrite(uint8_t pin, uint32_t val)
627632
628633 return ap3_pwm_output (pin, th, fw, clk);
629634}
635+
636+ ap3_err_t tone (uint8_t pin, uint32_t freq)
637+ {
638+ uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ;
639+
640+ uint32_t fw = 0 ;
641+ if (freq > 0 )
642+ {
643+ // Determine the frame width based on input freq
644+ fw = 12000000 / freq;
645+ }
646+ uint32_t th = fw / 2 ; // 50% time high
647+
648+ return ap3_pwm_output (pin, th, fw, clk);
649+ }
650+ ap3_err_t tone (uint8_t pin, uint32_t freq, uint32_t duration)
651+ {
652+ ap3_err_t status = AP3_OK;
653+ status = tone (pin, freq);
654+ if (status != AP3_OK)
655+ {
656+ return (status);
657+ }
658+
659+ uint32_t startTime = millis ();
660+
661+ while (millis () - startTime < duration)
662+ ;
663+
664+ status = noTone (pin);
665+ return (status);
666+ }
667+
668+ ap3_err_t noTone (uint8_t pin)
669+ {
670+ return tone (pin, 0 );
671+ }
0 commit comments