This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

used MCLK with GPIO for external compenant clock

Hi,

I would like to use MCK as a clock for adc CLK 4Mhz.

Could i enable & configure all registers without I2S or PDM peripheral activation?

Is there a sample code for MCLK configuration?

Parents
  • Thank you for your feedback.

    i  did it with I2S activation, without nominate LRCK,SCLK,DATA, juste MCLK.

    Here is the code

    #include <nrf.h>
    #include <zephyr.h>
    #include <device.h>
    #include <sys/printk.h>
    #include <stdbool.h>
    #include <string.h>
    #include <nrfx_i2s.h>
    
    
    
    
    static void i2s_clk_init(void);
    // a modifier 
    #define I2S_MCK_PIN 15 // MCLK pin is GPIO2
    
    #define I2S_DATA_BLOCK_WORDS 512 //How many numbers do we want. time reorded = DATA_BLOCK_WORDS / freq
    
    static uint32_t m_buffer_rx32u[I2S_DATA_BLOCK_WORDS];
    static nrfx_i2s_buffers_t i2s_initial_buffers;
    
    
    
    
    
    // I2S ISR handler
    ISR_DIRECT_DECLARE(i2s_isr_handler)
    {
      nrfx_i2s_irq_handler();
     ISR_DIRECT_PM();  // PM done after servicing interrupt for best latency
      return 1;         // We should check if scheduling decision should be made
    }
    
    // I2S event handler
    static void i2s_data_handler(nrfx_i2s_buffers_t const *p_released, uint32_t status)
    {
      if (NRFX_I2S_STATUS_NEXT_BUFFERS_NEEDED == status)
      {
        nrfx_i2s_next_buffers_set(&i2s_initial_buffers);
      }
    }
    
    /*****************************************************************************
    * Function: i2s_clk_init
    *
    * Description: provide 2.1333333 MHz. reference clock to audio ADC
    *
    * Parameters: none
    *
    * Returns: nothing
    *****************************************************************************/
    static void i2s_clk_init(void)
    {
      // the audio clock defaults to 12.288MHz, so we don't need to adjust it
    
      // configure I2S
      IRQ_DIRECT_CONNECT(I2S0_IRQn, 0, i2s_isr_handler, 0);
      i2s_initial_buffers.p_rx_buffer = m_buffer_rx32u;
    
      nrfx_i2s_config_t i2s_cfg = NRFX_I2S_DEFAULT_CONFIG(NRFX_I2S_PIN_NOT_USED, NRFX_I2S_PIN_NOT_USED, I2S_MCK_PIN, NRFX_I2S_PIN_NOT_USED, NRFX_I2S_PIN_NOT_USED);
      i2s_cfg.clksrc = NRF_I2S_CLKSRC_PCLK32M ; // audio clock
      i2s_cfg.mck_setup = NRF_I2S_MCK_32MDIV15;
      i2s_cfg.enable_bypass = false;         // bypass division, so we get the raw audio clock
      nrfx_err_t err_code = nrfx_i2s_init(&i2s_cfg, i2s_data_handler);
    
      if(err_code != NRFX_SUCCESS) 
      {
        printk("Error initializing I2S\n");
        return;
      }
    }
    
    
    void MCLK_Start(void)
    {
      nrfx_err_t err_code;
      i2s_clk_init();
        // start i2s
      err_code = nrfx_i2s_start(&i2s_initial_buffers, I2S_DATA_BLOCK_WORDS, 0);
      if (err_code != NRFX_SUCCESS)
      {
        printk("I2S start error\n");
      }
    }
    
    void MCLK_stop(void)
    {
        // stop i2s
      nrfx_i2s_stop();
    }

    juste be carful to add the necessary in CMakeLists file

  • Thank you Kanan for providing a solution for others to use.

Reply Children
No Data
Related