This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Getting i2s to work...

Hi,

I'd like to output some audio data through i2s. It's quite easy to understand your examples provided on infocenter, however, I can't get my code to work.

Here is what I do:

#define TX_BUFFER_SIZE    8                /* Length of transmit buffer, specified in 32-bit words */
                                                  
static uint32_t   mTxBuffer[TX_BUFFER_SIZE];

//-------------------------------------------------------------------

void  soundInitialise (ASoundCompleteCallback pCB)
{
    nrf_drv_i2s_config_t  config = NRF_DRV_I2S_DEFAULT_CONFIG;

    nrf_drv_i2s_init    (&config, onTxPrepare);
    nrf_gpio_pin_clear  (SDMODE_PIN);
    nrf_gpio_cfg_output (SDMODE_PIN);
}


void  soundPlay ()
{
    nrf_gpio_pin_set  (SDMODE_PIN);

    nrf_drv_i2s_start (NULL, mTxBuffer, TX_BUFFER_SIZE, 0);
}


static void  onTxPrepare (uint32_t const *p_data_received,
                          uint32_t       *p_data_to_send,
                          uint16_t        number_of_words)
{
    if (p_data_received != NULL)
        ;

    if (p_data_to_send  != NULL)
    {
        if (mTuneTx >= mTuneStop)
        {
            nrf_drv_i2s_stop   ();
            nrf_gpio_pin_clear (SDMODE_PIN);

            return;
        }

        while (number_of_words)
        {
            if (mTuneTx < mTuneStop)
               *p_data_to_send = *mTuneTx;
            else
               *p_data_to_send = 0;

            number_of_words--;
            mTuneTx++;
        }
    }
}

As soon as I call soundPlay () the code jumps into onTxPrepare () where a HardFault exception occurs. Why?

Thanks for your assistance.

Parents
  • It's not easy to say what is going on here as it is not easy to reproduce on my end. I have tried to the best of my abilities to merge your code into the I2S example from the SDK. I'm able to get some code that compiles and runs fine, but I don't get any hard faults. My best guess is that your code is trying to access some "bad" memory in the while(number_of_words). And are you trying to copy the content of the mTuneTx buffer to p_data_to_send? Won't this code just overwrite the value in p_data_to_send[0] 'number_of_words' number of times since you are not incrementing p_data_to_send?

Reply
  • It's not easy to say what is going on here as it is not easy to reproduce on my end. I have tried to the best of my abilities to merge your code into the I2S example from the SDK. I'm able to get some code that compiles and runs fine, but I don't get any hard faults. My best guess is that your code is trying to access some "bad" memory in the while(number_of_words). And are you trying to copy the content of the mTuneTx buffer to p_data_to_send? Won't this code just overwrite the value in p_data_to_send[0] 'number_of_words' number of times since you are not incrementing p_data_to_send?

Children
No Data
Related