Example #1
0
ret_code_t i2s_ws2812b_drive_xfer(rgb_led_t *led_array, uint16_t num_leds, uint8_t drive_pin)
{
	ret_code_t err_code;
	
	// define configs
	nrf_drv_i2s_config_t config = NRF_DRV_I2S_DEFAULT_CONFIG;
	config.sck_pin      = 22; // Don't set NRF_DRV_I2S_PIN_NOT_USED for I2S_CONFIG_SCK_PIN. (The program will stack.) 
	config.lrck_pin     = NRF_DRV_I2S_PIN_NOT_USED;
	config.mck_pin      = NRF_DRV_I2S_PIN_NOT_USED;
	config.sdout_pin    = drive_pin;
	config.sdin_pin     = NRF_DRV_I2S_PIN_NOT_USED;

	config.irq_priority = APP_IRQ_PRIORITY_HIGH;
	config.mode         = NRF_I2S_MODE_MASTER;
	config.format       = NRF_I2S_FORMAT_I2S;
	config.alignment    = NRF_I2S_ALIGN_LEFT;
	config.sample_width = NRF_I2S_SWIDTH_16BIT;
	config.channels     = NRF_I2S_CHANNELS_STEREO;
	config.mck_setup 		= NRF_I2S_MCK_32MDIV10;
	config.ratio     		= NRF_I2S_RATIO_32X;
	
	// initialize i2s
	err_code = nrf_drv_i2s_init(&config, i2s_ws2812b_drive_handler);
	APP_ERROR_CHECK(err_code);
	if ( err_code != NRF_SUCCESS ) 
	{
		return err_code;
	}

	// start transfer
	uint16_t tx_buffer_size = num_leds * I2S_WS2812B_DRIVE_BUF_SIZE_PER_LED / 4;
	uint32_t m_buffer_tx[tx_buffer_size];
	i2s_ws2812b_drive_tx_led_array = led_array;

	i2s_ws2812b_drive_m_blocks_transferred = 0;
	i2s_ws2812b_drive_flag_first_buffer = true;
	

	err_code = nrf_drv_i2s_start(NULL, m_buffer_tx, tx_buffer_size * 2, 0);
	APP_ERROR_CHECK(err_code);
	if ( err_code != NRF_SUCCESS ) 
	{
		return err_code;
	}

	nrf_delay_us((num_leds+20) * (24*5/4));

	// stop transfer
	nrf_drv_i2s_stop();

	// un-initialize i2s
	nrf_drv_i2s_uninit();
	
	return NRF_SUCCESS;
}
Example #2
0
int main(void)
{
    uint32_t err_code = NRF_SUCCESS;

    LEDS_CONFIGURE(LED_MASK_OK | LED_MASK_ERROR);

    init_uart();
    printf("\r\n"
           "I2S loopback example\r\n");

    nrf_drv_i2s_config_t config = NRF_DRV_I2S_DEFAULT_CONFIG;
    // In Master mode the MCK frequency and the MCK/LRCK ratio should be
    // set properly in order to achieve desired audio sample rate (which
    // is equivalent to the LRCK frequency).
    // For the following settings we'll get the LRCK frequency equal to
    // 15873 Hz (the closest one to 16 kHz that is possible to achieve).
    config.mck_setup = NRF_I2S_MCK_32MDIV21;
    config.ratio     = NRF_I2S_RATIO_96X;

    err_code = nrf_drv_i2s_init(&config, data_handler);
    APP_ERROR_CHECK(err_code);

    for (;;)
    {
        memset(m_buffer_rx, 0xCC, sizeof(m_buffer_rx));

        m_blocks_transferred = 0;

        err_code = nrf_drv_i2s_start(m_buffer_rx, m_buffer_tx,
                                     I2S_BUFFER_SIZE, 0);
        APP_ERROR_CHECK(err_code);

        while (m_blocks_transferred < BLOCKS_TO_TRANSFER)
        {}
        nrf_drv_i2s_stop();

        LEDS_OFF(LED_MASK_OK | LED_MASK_ERROR);
        nrf_delay_ms(PAUSE_TIME);
    }
}