@@ -426,8 +426,8 @@ void ap3_pwm_wait_for_pulse(uint32_t timer, uint32_t segment, uint32_t output, u
426
426
427
427
// **********************************************
428
428
// 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.
431
431
// - Furthermore fw must be at least 3 to see any high pulses
432
432
//
433
433
// 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
449
449
//
450
450
// ...
451
451
//
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
453
453
// the percent error from the expected duty cycle will be 100/fw
454
454
// **********************************************
455
455
@@ -458,7 +458,8 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
458
458
// handle configuration, if necessary
459
459
ap3_err_t retval = AP3_OK;
460
460
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
462
463
fw--;
463
464
}
464
465
@@ -506,7 +507,7 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
506
507
}
507
508
}
508
509
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
510
511
timer = OUTCTIMN (ctx, 0 );
511
512
if (OUTCTIMB (ctx, 0 ))
512
513
{
@@ -519,16 +520,17 @@ ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk)
519
520
}
520
521
521
522
// Ensure that th is not greater than the fw
522
- if (th > fw){
523
+ if (th > fw)
524
+ {
523
525
th = fw;
524
526
}
525
527
526
528
// 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 ))
528
530
{
529
531
output = AM_HAL_CTIMER_OUTPUT_FORCE0;
530
532
}
531
- else if ( th == fw )
533
+ else if ( th == fw)
532
534
{
533
535
output = AM_HAL_CTIMER_OUTPUT_FORCE1;
534
536
}
@@ -592,14 +594,17 @@ ap3_err_t analogWriteResolution(uint8_t res)
592
594
ap3_err_t analogWrite (uint8_t pin, uint32_t val)
593
595
{
594
596
// 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
+
603
608
return ap3_pwm_output (pin, val, fw, clk);
604
609
}
605
610
@@ -627,3 +632,40 @@ ap3_err_t servoWrite(uint8_t pin, uint32_t val)
627
632
628
633
return ap3_pwm_output (pin, th, fw, clk);
629
634
}
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