Atmel Software Framework

usb_stream_player_with_cs2200.c File Reference
#include <stdio.h>
#include "conf_usb.h"
#include "conf_audio_player.h"
#include "audio_mixer.h"
#include "usb_drv.h"
#include "usb_stream_player.h"
#include "conf_cs2200.h"
#include "cs2200.h"
#include "cycle_counter.h"
#include "gpio.h"
#include "twi.h"

Macros

#define BROKEN_STREAM_TIMER   4000
 
#define CS2200_FREF   FOSC0
 
#define TIMER_USB_RESYNC_CORRECTION   320
 

Functions

void dac_sample_sent_cb (void)
 This callback function is called when the DAC interrupt has sent the buffer 'n-1' and switches to buffer 'n'. More...
 
void dac_underflow_cb (void)
 This callback function is called when the PWM DAC interrupt does not have any more audio samples (i.e. More...
 
void player_init (void)
 
void player_shutdown (void)
 
uint32_t usb_stream_get_buffer_level ()
 This function returns the number of full buffers. More...
 
uint16_t usb_stream_get_counter ()
 This function returns the internal audio counter. More...
 
uint16_t usb_stream_get_stream_status ()
 This function returns the audio status. More...
 
void usb_stream_init (uint32_t sample_rate_hz, uint8_t num_channels, uint8_t bits_per_sample, bool swap_channels)
 This function initializes the USB Stream driver. More...
 
int usb_stream_input (usb_stream_side_t side, uint8_t pipe_in, uint32_t *pFifoCount)
 This function takes the stream coming from the selected USB pipe and sends it to the DAC driver. More...
 
bool usb_stream_new_sample_rate (uint32_t sample_rate)
 Sync the sample rate of the audio output stream with the USB stream. More...
 
int32_t usb_stream_ppm_get (void)
 This function returns the PPM differences between the input audio stream frequency and the DAC output frequency. More...
 
void usb_stream_resync (void)
 This function ensures that no underflow/underflow will never occur by adjusting the SSC/ABDAC frequencies. More...
 

Variables

static t_cpu_time broken_stream_timer
 
uint32_t buf_trace []
 
t_cpu_time usb_resync_timer
 
volatile uint16_t usb_stream_cnt =0
 
usb_stream_context_tusb_stream_context = NULL
 
static uint32_t usb_stream_resync_freq_ofst =0
 
uint32_t usb_stream_resync_frequency =0
 CS2200 out frequency. More...
 
static uint16_t usb_stream_resync_last_room =0
 
static int16_t usb_stream_resync_ppm_ofst =0
 
static uint32_t usb_stream_resync_step =0
 

#define BROKEN_STREAM_TIMER   4000
#define CS2200_FREF   FOSC0
#define TIMER_USB_RESYNC_CORRECTION   320

void dac_sample_sent_cb ( void  )

This callback function is called when the DAC interrupt has sent the buffer 'n-1' and switches to buffer 'n'.

The aim of this function is thus to prepare the buffer 'n+1'; so that there is always a pending buffer for the interrupt.

References audio_mixer_dacs_output_direct(), usb_stream_context_t::bits_per_sample, buffer, usb_stream_context_t::channel_count, usb_stream_context_t::status, usb_stream_context_t::synchronized, USB_STREAM_ERROR_NOT_SYNCHRONIZED, usb_stream_fifo_get(), usb_stream_fifo_get_used_room(), and usb_stream_fifo_pull().

void dac_underflow_cb ( void  )

This callback function is called when the PWM DAC interrupt does not have any more audio samples (i.e.

"famine").

References usb_stream_context_t::status, usb_stream_context_t::synchronized, and USB_STREAM_ERROR_NOT_SYNCHRONIZED.

void player_init ( void  )
void player_shutdown ( void  )
uint32_t usb_stream_get_buffer_level ( void  )

This function returns the number of full buffers.

References usb_stream_fifo_get_used_room().

uint16_t usb_stream_get_counter ( void  )

This function returns the internal audio counter.

References usb_stream_cnt.

uint16_t usb_stream_get_stream_status ( void  )

This function returns the audio status.

References usb_stream_context_t::status.

int usb_stream_input ( usb_stream_side_t  side,
uint8_t  pipe_in,
uint32_t *  pFifoCount 
)

This function takes the stream coming from the selected USB pipe and sends it to the DAC driver.

Moreover, it ensures that both input and output stream keep synchronized by adding or deleting samples.

Parameters
sideUSB_STREAM_HOST for USB host, USB_STREAM_DEVICE for device.
pipe_inNumber of the addressed pipe/endpoint
pFifoCount(return parameter) NULL or pointer to the number of used buffers at this time
Returns
status: (USB_STREAM_STATUS_OK, USB_STREAM_STATUS_NOT_SYNCHRONIZED, USB_STREAM_STATUS_SPEED_UP, USB_STREAM_STATUS_SLOW_DOWN, USB_STREAM_STATUS_BUFFER_OVERFLOW)

References _32_BITS_RATIO, audio_mixer_dacs_output_direct(), usb_stream_context_t::bits_per_sample, BROKEN_STREAM_TIMER, buffer, usb_stream_context_t::channel_count, cpu_is_timeout(), cpu_ms_2_cy(), cpu_set_timeout(), CS2200_FREF, cs2200_freq_clk_out(), FCPU_HZ, Host_byte_count, host_read_p_rxpacket(), Host_reset_pipe_fifo_access, i, NULL, PPM, UnionPtr::s16ptr, UnionPtr::s32ptr, UnionPtr::s8ptr, usb_stream_context_t::status, swap16, swap32, usb_stream_context_t::synchronized, TIMER_USB_RESYNC_CORRECTION, Usb_byte_count, usb_read_ep_rxpacket(), Usb_reset_endpoint_fifo_access, USB_STREAM_BUFFER_NUMBER, usb_stream_cnt, USB_STREAM_DEVICE, USB_STREAM_ERROR_BROKEN_STREAM, USB_STREAM_ERROR_NO_DATA, USB_STREAM_ERROR_NONE, USB_STREAM_ERROR_NOT_SYNCHRONIZED, USB_STREAM_ERROR_OVERFLOW, usb_stream_fifo_get(), usb_stream_fifo_get_buffer(), usb_stream_fifo_get_used_room(), usb_stream_fifo_pull(), usb_stream_fifo_push(), USB_STREAM_HOST, USB_STREAM_REAL_BUFFER_SIZE, usb_stream_resync_freq_ofst, usb_stream_resync_frequency, usb_stream_resync_last_room, usb_stream_resync_ppm_ofst, USB_STREAM_RESYNC_PPM_STEPS, usb_stream_resync_step, and usb_stream_context_t::wr_id.

bool usb_stream_new_sample_rate ( uint32_t  sample_rate)

Sync the sample rate of the audio output stream with the USB stream.

Return values
falseThe new sample rate already matches the current settings.
trueThe output sample rate was changed.

References audio_mixer_dacs_flush_direct(), and usb_stream_init().

int32_t usb_stream_ppm_get ( void  )

This function returns the PPM differences between the input audio stream frequency and the DAC output frequency.

Note that this information should only be considered as an indication, for debug or visual effect for example.

References usb_stream_resync_ppm_ofst.

void usb_stream_resync ( void  )

This function ensures that no underflow/underflow will never occur by adjusting the SSC/ABDAC frequencies.

This function monitors the buffering level and set a timer for the resynchronization.

References _32_BITS_RATIO, cpu_is_timeout(), cpu_ms_2_cy(), cpu_set_timeout(), CS2200_FREF, cs2200_freq_clk_adjust(), FCPU_HZ, usb_stream_context_t::synchronized, TIMER_USB_RESYNC_CORRECTION, twi_is_busy(), USB_STREAM_BUFFER_NUMBER, usb_stream_fifo_get_used_room(), usb_stream_resync_freq_ofst, usb_stream_resync_last_room, usb_stream_resync_ppm_ofst, USB_STREAM_RESYNC_PPM_STEPS, and usb_stream_resync_step.

t_cpu_time broken_stream_timer
static
uint32_t buf_trace[]
t_cpu_time usb_resync_timer
volatile uint16_t usb_stream_cnt =0
usb_stream_context_t* usb_stream_context = NULL
uint32_t usb_stream_resync_freq_ofst =0
static
uint32_t usb_stream_resync_frequency =0

CS2200 out frequency.

Referenced by aic23b_dac_setup(), ms3_dac_setup(), usb_stream_init(), and usb_stream_input().

uint16_t usb_stream_resync_last_room =0
static
int16_t usb_stream_resync_ppm_ofst =0
static
uint32_t usb_stream_resync_step =0
static