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
    • Yes
    • The buffers should be 32 bits wide, so e.g. a buffer of size 3 is actually 12 bytes. So no need to make buffersizes multiples of 4.
    • I'm not sure if I understand. If you only want to send p_data_to_send should never be NULL.
    • Check out the nrf_drv_i2s_data_handler_t typedef in nrf_drv_i2s.h.
    • Preferably not inside an interrupt. Look at how it is done in the SDK.

    I can't think of anything else than that there must be something wrong with how you handle and/or point to your buffers.

Reply
    • Yes
    • The buffers should be 32 bits wide, so e.g. a buffer of size 3 is actually 12 bytes. So no need to make buffersizes multiples of 4.
    • I'm not sure if I understand. If you only want to send p_data_to_send should never be NULL.
    • Check out the nrf_drv_i2s_data_handler_t typedef in nrf_drv_i2s.h.
    • Preferably not inside an interrupt. Look at how it is done in the SDK.

    I can't think of anything else than that there must be something wrong with how you handle and/or point to your buffers.

Children
No Data
Related