Provide a configuration symbol to allow the sound command play a sine wave
instead of a square wave.

Signed-off-by: Heinrich Schuchardt <heinrich.schucha...@canonical.com>
---
v2:
        update documentation
---
 doc/usage/cmd/sound.rst      |  3 +++
 drivers/sound/Kconfig        |  6 ++++++
 drivers/sound/sound-uclass.c | 10 ++++++++--
 test/dm/sound.c              | 20 ++++++++++++--------
 4 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/doc/usage/cmd/sound.rst b/doc/usage/cmd/sound.rst
index d3fac243b1..aa988fa261 100644
--- a/doc/usage/cmd/sound.rst
+++ b/doc/usage/cmd/sound.rst
@@ -35,6 +35,9 @@ Configuration
 
 The sound command is enabled by CONFIG_CMD_SOUND=y.
 
+By default a square wave is generated. With CONFIG_SOUND_SINE=y a sine wave is
+used instead.
+
 Return value
 ------------
 
diff --git a/drivers/sound/Kconfig b/drivers/sound/Kconfig
index 0948d8caab..1005192fd4 100644
--- a/drivers/sound/Kconfig
+++ b/drivers/sound/Kconfig
@@ -12,6 +12,12 @@ config SOUND
          audio codecs are called from the sound-i2s code. This could be
          converted to driver model.
 
+config SOUND_SINE
+       bool "Generate sine wave"
+       help
+         When this setting is enabled playing a sound produces a sine
+         wave. If the settings is not enabled, a square wave is produced.
+
 config I2S
        bool "Enable I2S support"
        depends on SOUND
diff --git a/drivers/sound/sound-uclass.c b/drivers/sound/sound-uclass.c
index 2ffc4fc7c1..637f6b11ab 100644
--- a/drivers/sound/sound-uclass.c
+++ b/drivers/sound/sound-uclass.c
@@ -99,8 +99,14 @@ int sound_beep(struct udevice *dev, int msecs, int 
frequency_hz)
                return -ENOMEM;
        }
 
-       sound_create_square_wave(i2s_uc_priv->samplingrate, data, data_size,
-                                frequency_hz, i2s_uc_priv->channels);
+       if (CONFIG_IS_ENABLED(SOUND_SINE))
+               sound_create_sine_wave(i2s_uc_priv->samplingrate, data,
+                                      data_size, frequency_hz,
+                                      i2s_uc_priv->channels);
+       else
+               sound_create_square_wave(i2s_uc_priv->samplingrate, data,
+                                        data_size, frequency_hz,
+                                        i2s_uc_priv->channels);
 
        ret = 0;
        while (msecs >= 1000) {
diff --git a/test/dm/sound.c b/test/dm/sound.c
index 15d545ab5a..f16ea80157 100644
--- a/test/dm/sound.c
+++ b/test/dm/sound.c
@@ -17,6 +17,12 @@ static int dm_test_sound(struct unit_test_state *uts)
 {
        struct sound_uc_priv *uc_priv;
        struct udevice *dev;
+       int expected;
+
+       if (CONFIG_IS_ENABLED(SOUND_SINE))
+               expected = 3494;
+       else
+               expected = 4560;
 
        /* check probe success */
        ut_assertok(uclass_first_device_err(UCLASS_SOUND, &dev));
@@ -24,24 +30,22 @@ static int dm_test_sound(struct unit_test_state *uts)
        ut_asserteq_str("audio-codec", uc_priv->codec->name);
        ut_asserteq_str("i2s", uc_priv->i2s->name);
        ut_asserteq(0, sandbox_get_setup_called(dev));
-
        ut_assertok(sound_beep(dev, 1, 100));
        ut_asserteq(48, sandbox_get_sound_count(dev));
-       ut_asserteq(4560, sandbox_get_sound_sum(dev));
+       ut_asserteq(expected, sandbox_get_sound_sum(dev));
        ut_assertok(sound_beep(dev, 1, 100));
        ut_asserteq(96, sandbox_get_sound_count(dev));
-       ut_asserteq(9120, sandbox_get_sound_sum(dev));
+       expected *= 2;
+       ut_asserteq(expected, sandbox_get_sound_sum(dev));
        ut_assertok(sound_beep(dev, 1, -100));
        ut_asserteq(144, sandbox_get_sound_count(dev));
-       ut_asserteq(9120, sandbox_get_sound_sum(dev));
+       ut_asserteq(expected, sandbox_get_sound_sum(dev));
        ut_assertok(sound_beep(dev, 1, 0));
        ut_asserteq(192, sandbox_get_sound_count(dev));
-       ut_asserteq(9120, sandbox_get_sound_sum(dev));
+       ut_asserteq(expected, sandbox_get_sound_sum(dev));
        ut_assertok(sound_beep(dev, 1, INT_MAX));
        ut_asserteq(240, sandbox_get_sound_count(dev));
-       ut_asserteq(9120, sandbox_get_sound_sum(dev));
-       ut_asserteq(false, sandbox_get_sound_active(dev));
-
+       ut_asserteq(expected, sandbox_get_sound_sum(dev));
        return 0;
 }
 DM_TEST(dm_test_sound, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
-- 
2.37.2

Reply via email to