You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2022/09/20 06:28:41 UTC

[mynewt-core] branch master updated: i2s_nrfx: Add Audio clock support

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 60d700731 i2s_nrfx: Add Audio clock support
60d700731 is described below

commit 60d700731cbc9afedc1a2b3a9fb7a273d381fdc6
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Mon Sep 19 09:16:31 2022 +0200

    i2s_nrfx: Add Audio clock support
    
    NRF5340 has dedicated audio clock that allows for more precise audio
    frequencies then 32MHz oscillator only.
    
    NRF5340 also supports 32 bits sample slots.
    
    This add support ACLK.
    For this to work NRFX_CLOCK_ENABLED must be enabled to allows usage of
    clock related nrfx api.
---
 hw/drivers/i2s/i2s_nrfx/src/i2s_nrfx.c      | 30 +++++++++++++++++++++++++++++
 hw/mcu/nordic/nrf5340/include/nrfx_config.h |  2 +-
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/hw/drivers/i2s/i2s_nrfx/src/i2s_nrfx.c b/hw/drivers/i2s/i2s_nrfx/src/i2s_nrfx.c
index 751d93c0e..3136aaa9f 100644
--- a/hw/drivers/i2s/i2s_nrfx/src/i2s_nrfx.c
+++ b/hw/drivers/i2s/i2s_nrfx/src/i2s_nrfx.c
@@ -23,6 +23,7 @@
 #include <i2s/i2s_driver.h>
 #include <i2s_nrfx/i2s_nrfx.h>
 #include <nrfx/drivers/include/nrfx_i2s.h>
+#include <nrfx_clock.h>
 
 struct i2s_nrfx {
     nrfx_i2s_config_t nrfx_i2s_cfg;
@@ -218,6 +219,35 @@ i2s_nrfx_select_clock_cfg(nrfx_i2s_config_t *cfg, uint32_t sample_rate)
         /* User provided custom clock setup, no need to use stock values */
         return;
     }
+#if NRF_I2S_HAS_CLKCONFIG
+    float src_frq;
+    uint32_t ratio;
+    uint32_t mck;
+    if (cfg->clksrc == I2S_CONFIG_CLKCONFIG_CLKSRC_ACLK) {
+        NRF_CLOCK->TASKS_HFCLKAUDIOSTOP = 1;
+        if (88200 / sample_rate * sample_rate == 88200) {
+            nrfx_clock_hfclkaudio_config_set(15298);
+        } else {
+            nrfx_clock_hfclkaudio_config_set(39854);
+        }
+        NRF_CLOCK->TASKS_HFCLKAUDIOSTART = 1;
+        src_frq = (32000000 * (4 + nrfx_clock_hfclkaudio_config_get() * 0.000015259f) / 12);
+        if (cfg->sample_width == NRF_I2S_SWIDTH_24BIT) {
+            cfg->ratio = NRF_I2S_RATIO_48X;
+            ratio = 48;
+        } else if (cfg->sample_width == NRF_I2S_SWIDTH_32BIT || cfg->sample_width == NRF_I2S_SWIDTH_16BIT_IN32BIT ||
+            cfg->sample_width == NRF_I2S_SWIDTH_24BIT_IN32BIT || cfg->sample_width == NRF_I2S_SWIDTH_8BIT_IN32BIT) {
+            cfg->ratio = NRF_I2S_RATIO_64X;
+            ratio = 64;
+        } else {
+            cfg->ratio = NRF_I2S_RATIO_32X;
+            ratio = 32;
+        }
+        mck = sample_rate * ratio;
+        cfg->mck_setup = 4096 * (mck * 1048576ull / (src_frq + mck / 2));
+        return;
+    }
+#endif
     for (i = 0; i < ARRAY_SIZE(sample_rates); ++i) {
         if (sample_rates[i] == sample_rate) {
             if (cfg->sample_width == NRF_I2S_SWIDTH_24BIT) {
diff --git a/hw/mcu/nordic/nrf5340/include/nrfx_config.h b/hw/mcu/nordic/nrf5340/include/nrfx_config.h
index 141085eb5..59badaffc 100644
--- a/hw/mcu/nordic/nrf5340/include/nrfx_config.h
+++ b/hw/mcu/nordic/nrf5340/include/nrfx_config.h
@@ -234,7 +234,7 @@
 // <e> NRFX_CLOCK_ENABLED - nrfx_clock - CLOCK peripheral driver.
 //==========================================================
 #ifndef NRFX_CLOCK_ENABLED
-#define NRFX_CLOCK_ENABLED 0
+#define NRFX_CLOCK_ENABLED 1
 #endif
 // <o> NRFX_CLOCK_CONFIG_LF_SRC  - LF clock source.