Skip to content

Commit 239e0e0

Browse files
author
Nathan Seidle
committed
Add tone functions
1 parent e8f2671 commit 239e0e0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

cores/arduino/ard_sup/analog/ap3_analog.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,3 +603,40 @@ ap3_err_t servoWrite(uint8_t pin, uint32_t val)
603603

604604
return ap3_pwm_output(pin, th, fw, clk);
605605
}
606+
607+
ap3_err_t tone(uint8_t pin, uint32_t freq)
608+
{
609+
uint32_t clk = AM_HAL_CTIMER_HFRC_12MHZ;
610+
611+
uint32_t fw = 0;
612+
if (freq > 0)
613+
{
614+
// Determine the frame width based on input freq
615+
fw = 12000000 / freq;
616+
}
617+
uint32_t th = fw / 2; // 50% time high
618+
619+
return ap3_pwm_output(pin, th, fw, clk);
620+
}
621+
ap3_err_t tone(uint8_t pin, uint32_t freq, uint32_t duration)
622+
{
623+
ap3_err_t status = AP3_OK;
624+
status = tone(pin, freq);
625+
if (status != AP3_OK)
626+
{
627+
return (status);
628+
}
629+
630+
uint32_t startTime = millis();
631+
632+
while (millis() - startTime < duration)
633+
;
634+
635+
status = noTone(pin);
636+
return (status);
637+
}
638+
639+
ap3_err_t noTone(uint8_t pin)
640+
{
641+
return tone(pin, 0);
642+
}

cores/arduino/ard_sup/ap3_analog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ ap3_err_t analogWrite(uint8_t pin, uint32_t val);
5151
ap3_err_t servoWriteResolution(uint8_t res);
5252
ap3_err_t servoWrite(uint8_t pin, uint32_t val);
5353

54+
ap3_err_t tone(uint8_t pin, uint32_t freq);
55+
ap3_err_t tone(uint8_t pin, uint32_t freq, uint32_t duration);
56+
ap3_err_t noTone(uint8_t pin);
57+
5458
#endif // _AP3_ANALOG_H_

0 commit comments

Comments
 (0)