tpa6130.h File Reference


Detailed Description

TPA6130 amplifier driver header file.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file tpa6130.h.

#include <stddef.h>
#include "compiler.h"

Go to the source code of this file.

Defines

#define TPA6130_MODE_MONO   1
#define TPA6130_MODE_STEREO   0
#define TPA6130_SIG_DIFF   0
#define TPA6130_SIG_POS   1
#define TPA6130_TWI_ADDRESS   0x60
Volume Control
#define TPA6130_VOL_MAX   ((S8)( 63))
#define TPA6130_VOL_MIN   ((S8)( 0))

Functions

void tpa6130_dac_decrease_volume (void)
 Decreases the output volume of the DACs.
void tpa6130_dac_flush (void)
 Flushes the sample buffer being output to the DACs.
U8 tpa6130_dac_get_volume (void)
 Returns the current volume of the DAC.
void tpa6130_dac_increase_volume (void)
 Increases the output volume of the DACs.
Bool tpa6130_dac_is_volume_boosted (void)
 Returns the state of the boost mode of the DAC.
Bool tpa6130_dac_is_volume_muted (void)
 Returns the state of the mute mode of the DAC.
void tpa6130_dac_mute (Bool mute)
 No more audio if input is true.
Bool tpa6130_dac_output (void *sample_buffer, size_t sample_length)
 Outputs a sample buffer to the DACs.
void tpa6130_dac_set_volume (U8 volume)
 Set the volume of the DAC.
void tpa6130_dac_setup (U32 sample_rate_hz, U8 num_channels, U8 bits_per_sample, Bool swap_channels, void(*callback)(U32 arg), U32 callback_opt, U32 pba_hz)
 Sets the DACs up with new settings.
void tpa6130_dac_start (U32 sample_rate_hz, U8 num_channels, U8 bits_per_sample, Bool swap_channels, void(*callback)(U32 arg), U32 callback_opt, U32 pba_hz)
 Starts the ABDAC.
void tpa6130_dac_stop (void)
 Stops the DACs.
S8 tpa6130_get_volume (void)
 Gets the current volume settings.
S8 tpa6130_init (void)
 Probes and initializes the amplifier. Probes the TWI bus for the amplifier by using the slave address specified in the configuration (TPA6130_TWI_ADDRESS). If the device responds with an ACK the version register is read out and compared to the valid versions (TPA6130_VERSION). Last step is to set volume to 0, unmute and set the configuration specified in the conf_tpa6130.h file (stereo, mono ..).
void tpa6130_powerup (void)
 Powers up the amplifier from low power mode. This is the software low power mode described in the datasheet.
void tpa6130_set_volume (S8 volume)
 Sets the volume of the amplifier. Valid values are between 0 (min -59dB) and 63 (max 4dB) altough the function takes care of any values higher than that by setting it to max.
void tpa6130_shutdown (void)
 Shuts down the amplifier and sets it into low power mode. This is the software low power mode described in the datasheet.


Define Documentation

#define TPA6130_MODE_MONO   1

Definition at line 60 of file tpa6130.h.

#define TPA6130_MODE_STEREO   0

Definition at line 59 of file tpa6130.h.

#define TPA6130_SIG_DIFF   0

Definition at line 70 of file tpa6130.h.

#define TPA6130_SIG_POS   1

Definition at line 71 of file tpa6130.h.

#define TPA6130_TWI_ADDRESS   0x60

Definition at line 80 of file tpa6130.h.

Referenced by tpa6130_init(), tpa6130_read_data(), tpa6130_write_data(), and twi_init().

#define TPA6130_VOL_MAX   ((S8)( 63))

Definition at line 88 of file tpa6130.h.

Referenced by tpa6130_dac_get_volume(), and tpa6130_set_volume().

#define TPA6130_VOL_MIN   ((S8)( 0))


Function Documentation

void tpa6130_dac_decrease_volume ( void   ) 

Decreases the output volume of the DACs.

Note:
The DACs must have been started beforehand.
Decreases the output volume of the DACs.

Stops at the lowest possible volume.

Definition at line 621 of file tpa6130.c.

References MUTE_L, MUTE_R, tpa6130_get_volume(), tpa6130_set_volume(), and TPA6130_VOL_MIN.

00622 {
00623   S8 volume = tpa6130_get_volume()& (~(MUTE_L | MUTE_R));;
00624   if( volume > TPA6130_VOL_MIN )
00625     --volume;
00626   tpa6130_set_volume( volume );
00627 }

void tpa6130_dac_flush ( void   ) 

Flushes the sample buffer being output to the DACs.

Definition at line 631 of file tpa6130.c.

00632 {
00633   pdca_disable_interrupt_transfer_complete(TPA6130_ABDAC_PDCA_CHANNEL);
00634   pdca_disable_interrupt_reload_counter_zero(TPA6130_ABDAC_PDCA_CHANNEL);
00635 
00636   /*TODO Do we really want to wait here? Or do we just don't care when
00637    * the buffer is empty/flushed */
00638   //while(!pdca_get_transfer_status(TPA6130_ABDAC_PDCA_CHANNEL) &
00639   //  PDCA_TRANSFER_COMPLETE);
00640   pdca_disable       (TPA6130_ABDAC_PDCA_CHANNEL       );
00641   pdca_load_channel  (TPA6130_ABDAC_PDCA_CHANNEL,0x0, 0);
00642   pdca_reload_channel(TPA6130_ABDAC_PDCA_CHANNEL,0x0, 0);
00643   pdca_enable        (TPA6130_ABDAC_PDCA_CHANNEL       );
00644 }

U8 tpa6130_dac_get_volume ( void   ) 

Returns the current volume of the DAC.

Definition at line 587 of file tpa6130.c.

References MUTE_L, MUTE_R, tpa6130_get_volume(), and TPA6130_VOL_MAX.

00588 {
00589  // return volume is num display step for LCD
00590  //  volume scale is between 10 and 245
00591  // 0 is -100db
00592  // 245 is max volume
00593   U16 raw_volume;
00594   raw_volume = (tpa6130_get_volume() & (~(MUTE_L | MUTE_R)));
00595   return (U8) ((raw_volume * 255) / TPA6130_VOL_MAX);
00596 }

void tpa6130_dac_increase_volume ( void   ) 

Increases the output volume of the DACs.

Note:
The DACs must have been started beforehand.

Definition at line 609 of file tpa6130.c.

References MUTE_L, MUTE_R, tpa6130_get_volume(), tpa6130_set_volume(), and TPA6130_VOL_MIN.

00610 {
00611   S8 volume = tpa6130_get_volume()& (~(MUTE_L | MUTE_R));
00612   if( volume < TPA6130_VOL_MIN )
00613     volume = TPA6130_VOL_MIN;
00614   tpa6130_set_volume(volume+1);
00615 }

Bool tpa6130_dac_is_volume_boosted ( void   ) 

Returns the state of the boost mode of the DAC.

Note:
The DAC must have been started beforehand.

Definition at line 579 of file tpa6130.c.

00580 {
00581   return FALSE;
00582 }

Bool tpa6130_dac_is_volume_muted ( void   ) 

Returns the state of the mute mode of the DAC.

Note:
The DAC must have been started beforehand.

Definition at line 522 of file tpa6130.c.

00523 {
00524   return FALSE;
00525 }

void tpa6130_dac_mute ( Bool  mute  ) 

No more audio if input is true.

Definition at line 527 of file tpa6130.c.

References MUTE_L, MUTE_R, tpa6130_get_volume(), tpa6130_output_param, TPA6130_VOLUME_AND_MUTE, and tpa6130_write_data().

00528 {
00529   // //1st Version Mute Audio for Play/Pause
00530 /*  int volume=tpa6130_get_volume();
00531   if(mute==TRUE) {
00532      //Mute volume
00533      volume= volume|MUTE_L|MUTE_R;
00534   }
00535   else {
00536      //Unmute volume
00537      volume= volume&(~(MUTE_L|MUTE_R));
00538  
00539   }
00540  tpa6130_write_data(TPA6130_VOLUME_AND_MUTE,volume);
00541 */
00542   //2n Version Stop PDCA >> No lost of audio when pause
00543 /*  if(mute==TRUE) {
00544     pdca_disable(TPA6130_ABDAC_PDCA_CHANNEL);
00545   }
00546   else {
00547     pdca_enable(TPA6130_ABDAC_PDCA_CHANNEL);
00548   }
00549 */
00550 
00551   // 3rd Version wait until the current buffers are empty and disable the interrutps
00552   S8 volume = tpa6130_get_volume();
00553   if (mute)
00554   {
00555     U32 save_dac_reload_callback_opt;
00556 
00557     // Mute the audio stream
00558     volume = volume | MUTE_L | MUTE_R;
00559     tpa6130_write_data(TPA6130_VOLUME_AND_MUTE, volume);
00560     // Disable the reload channel of the interrupt
00561     save_dac_reload_callback_opt = tpa6130_output_param.callback_opt;
00562     tpa6130_output_param.callback_opt = 0;
00563     // Disable the reload interruption and wait until the transfer is complete
00564     pdca_disable_interrupt_reload_counter_zero(TPA6130_ABDAC_PDCA_CHANNEL);
00565     while (!(pdca_get_transfer_status(TPA6130_ABDAC_PDCA_CHANNEL) & PDCA_TRANSFER_COMPLETE));
00566     // Restore the reload callback function
00567     tpa6130_output_param.callback_opt = save_dac_reload_callback_opt;
00568   }
00569   else
00570   {
00571     // Re-enable the interrupts
00572     pdca_enable_interrupt_reload_counter_zero(TPA6130_ABDAC_PDCA_CHANNEL);
00573     // Un-mute the audio stream
00574     volume = volume & (~(MUTE_L | MUTE_R));
00575     tpa6130_write_data(TPA6130_VOLUME_AND_MUTE, volume);
00576   }
00577 }

Bool tpa6130_dac_output ( void *  sample_buffer,
size_t  sample_length 
)

Outputs a sample buffer to the DACs.

Note:
The DACs must have been started beforehand.
Outputs a sample buffer to the DACs.

Note:
The DACs must have been started beforehand.

Definition at line 481 of file tpa6130.c.

References tpa6130_output_param.

Referenced by main().

00482 {
00483   //int global_interrupt_enabled;
00484 
00485   /*Wait until the PDCA loads the reload value to its transfer
00486    * counter register(TCRR=0). Then we are ready to set up a new
00487    * transfer */
00488   if(!(pdca_get_transfer_status(TPA6130_ABDAC_PDCA_CHANNEL) &
00489     PDCA_TRANSFER_COUNTER_RELOAD_IS_ZERO))
00490   {
00491     return FALSE;
00492   }
00493 
00494   /* Nothing to do if we get no data. */
00495   if(sample_length)
00496   {
00497 
00498     /*TODO Do we really need to adjust the buffer for mono*/
00499 
00500     /* While reloading the PDC we do not need any active interrupt*/
00501     //if((global_interrupt_enabled = Is_global_interrupt_enabled()))
00502     //  Disable_global_interrupt();
00503 
00504     /*FIXME This assumes a stereo 16-bit sample size */
00505     // one sample here consists of 2x16-bit (16-bit stereo)
00506     pdca_reload_channel(TPA6130_ABDAC_PDCA_CHANNEL,
00507       sample_buffer, sample_length);
00508 
00509     //if(global_interrupt_enabled)
00510     //  Enable_global_interrupt();
00511 
00512     /*TODO enable transfer complete interrupt
00513      * Is it possible to move this to setup or other places?*/
00514     if(tpa6130_output_param.callback_opt & AUDIO_DAC_OUT_OF_SAMPLE_CB)
00515       pdca_enable_interrupt_transfer_complete(TPA6130_ABDAC_PDCA_CHANNEL);
00516     if (tpa6130_output_param.callback_opt & AUDIO_DAC_RELOAD_CB)
00517       pdca_enable_interrupt_reload_counter_zero(TPA6130_ABDAC_PDCA_CHANNEL);
00518   }
00519   return TRUE;
00520 }

void tpa6130_dac_set_volume ( U8  volume  ) 

Set the volume of the DAC.

Definition at line 600 of file tpa6130.c.

References tpa6130_set_volume().

00601 {
00602    tpa6130_set_volume(volume);
00603 }

void tpa6130_dac_setup ( U32  sample_rate_hz,
U8  num_channels,
U8  bits_per_sample,
Bool  swap_channels,
void(*)(U32 arg)  callback,
U32  callback_opt,
U32  pba_hz 
)

Sets the DACs up with new settings.

Note:
The DACs must have been started beforehand.

Definition at line 407 of file tpa6130.c.

References callback, FPBA_HZ, pm, TPA6130_ABDAC_GCLK_INPUT_HZ, tpa6130_init(), tpa6130_output_param, and usb_stream_resync_frequency.

Referenced by tpa6130_dac_start().

00414 {
00415   // save input parameters to local driver data
00416   tpa6130_output_param.num_channels = num_channels;
00417   tpa6130_output_param.callback     = callback;
00418   tpa6130_output_param.callback_opt = callback_opt;
00419 
00420   /* Probe for amplifier and initialize it */
00421   tpa6130_init();
00422 
00423 #if defined(TPA6130_DAC_CLOCK_SET_CALLBACK)
00424   TPA6130_DAC_CLOCK_SET_CALLBACK(sample_rate_hz);
00425 #else
00426   /* ABDAC configuration
00427    * The ABDAC needs the input frequency of its generic clock (bus_hz)
00428    * Here we use the configuration value from the conf_tpa6130.h file
00429    * (TPA6130_ABDAC_GCLK_INPUT_HZ).
00430    * The sample rate specifies the desired sample rate for the ABDAC.
00431    * The generic clock input must be greater than 256*sample_rate_hz
00432    * or the setup of the ABDAC will fail silently here.
00433    * TODO we could add asserts here to detect wrong settings during
00434    * compile time.
00435    */
00436   if(!abdac_set_dac_sample_rate(sample_rate_hz)) {
00437     // if it is not possible to set correctly the sample rate
00438     // Use default set function
00439     abdac_set_dac_hz(TPA6130_ABDAC, TPA6130_ABDAC_GCLK_INPUT_HZ,sample_rate_hz);
00440   }
00441 #endif
00442 
00443   if(swap_channels)
00444   {
00445     abdac_swap_channels(TPA6130_ABDAC);
00446   }
00447   abdac_enable(TPA6130_ABDAC);
00448 
00449   /* PDCA setup */
00450   /*FIXME we use only word as transfer size for now.
00451    * half-word transfer size will only write to channel0
00452    * of the ABDAC, this can be used to implement mono */
00453   pdca_channel_options_t tpa6130_abdac_pdca_options =
00454   {
00455     .addr   = NULL,
00456     .size   = 0,
00457     .r_addr   = 0,
00458     .r_size   = 0,
00459     .pid    = TPA6130_ABDAC_PDCA_PID,
00460     .transfer_size  = PDCA_TRANSFER_SIZE_WORD
00461   };
00462 
00463   /* Initialize the PCDA for the ABDAC
00464    * The channel number can be set in the configuration file
00465    * with the define TPA6130_ABDAC_PDCA_CHANNEL.
00466    */
00467   pdca_init_channel(TPA6130_ABDAC_PDCA_CHANNEL,
00468     &tpa6130_abdac_pdca_options);
00469   /* Enable the PDCA channel. Since we did not provide any data
00470    * yet the channel is in idle mode */
00471   pdca_enable(TPA6130_ABDAC_PDCA_CHANNEL);
00472 
00473 }

void tpa6130_dac_start ( U32  sample_rate_hz,
U8  num_channels,
U8  bits_per_sample,
Bool  swap_channels,
void(*)(U32 arg)  callback,
U32  callback_opt,
U32  pba_hz 
)

Starts the ABDAC.

Parameters:
sample_rate_hz Sample rate for the ABDAC.
num_channels number of channel
bits_per_sample number of bit per sample
swap_channels TRUE -> swap the channels
callback Provide a functions that handles callback.
callback_opt Callback flag
pba_hz Speed of the peripheral bus A.

Definition at line 367 of file tpa6130.c.

References callback, TPA6130_ABDAC_GPIO_MAP, tpa6130_abdac_tx_pdca_int_handler(), tpa6130_dac_setup(), tpa6130_dac_stop(), and tpa6130_powerup().

Referenced by main().

00374 {
00375   /* stop ABDAC if running*/
00376   tpa6130_dac_stop();
00377 
00378   /* configure used pins for ABDAC */
00379   gpio_enable_module(TPA6130_ABDAC_GPIO_MAP,
00380     sizeof(TPA6130_ABDAC_GPIO_MAP) /
00381     sizeof(TPA6130_ABDAC_GPIO_MAP[0]));
00382 
00383   /* configure and start PDC and ABDAC*/
00384   tpa6130_dac_setup(sample_rate_hz,
00385     num_channels,
00386     bits_per_sample,
00387     swap_channels,
00388     callback,
00389     callback_opt,
00390     pba_hz);
00391 
00392   /* Register a interrupt service routine for the ABDAC channel of
00393    * the PDCA
00394    */
00395   INTC_register_interrupt(&tpa6130_abdac_tx_pdca_int_handler,
00396     TPA6130_ABDAC_PDCA_IRQ,
00397     TPA6130_ABDAC_PDCA_INT_LEVEL);
00398 
00399   tpa6130_powerup();
00400 
00401 }

void tpa6130_dac_stop ( void   ) 

Stops the DACs.

Definition at line 650 of file tpa6130.c.

References TPA6130_ABDAC_GPIO_MAP, tpa6130_output_param, and tpa6130_shutdown().

Referenced by tpa6130_dac_start().

00651 {
00652   /* Disable amplifier 1st */
00653   tpa6130_shutdown();
00654 
00655   /* Flush the dac */
00656   // Don't flush the DAC when stop
00657   //tpa6130_dac_flush();
00658 
00659   /* Disable ABDAC */
00660   abdac_disable(TPA6130_ABDAC);
00661 
00662   /* Stop  PDCA */
00663   pdca_disable(TPA6130_ABDAC_PDCA_CHANNEL);
00664 
00665   /* Set used GPIO pins to GPIO state */
00666   gpio_enable_gpio(TPA6130_ABDAC_GPIO_MAP,
00667     sizeof(TPA6130_ABDAC_GPIO_MAP)
00668     / sizeof(TPA6130_ABDAC_GPIO_MAP[0]));
00669 
00670   tpa6130_output_param.num_channels = 0;
00671   tpa6130_output_param.callback     = NULL;
00672   tpa6130_output_param.callback_opt = 0;
00673 }

S8 tpa6130_get_volume ( void   ) 

Gets the current volume settings.

Returns:
Current volume settings. Value is between 0 (-59dB) and 63 (4dB).

Definition at line 353 of file tpa6130.c.

References tpa6130_read_data(), TPA6130_VOLUME_AND_MUTE, and TWI_READ_SR.

Referenced by main(), tpa6130_dac_decrease_volume(), tpa6130_dac_get_volume(), tpa6130_dac_increase_volume(), and tpa6130_dac_mute().

00354 {
00355   return tpa6130_read_data(TPA6130_VOLUME_AND_MUTE, TWI_READ_SR);
00356 }

S8 tpa6130_init ( void   ) 

Probes and initializes the amplifier. Probes the TWI bus for the amplifier by using the slave address specified in the configuration (TPA6130_TWI_ADDRESS). If the device responds with an ACK the version register is read out and compared to the valid versions (TPA6130_VERSION). Last step is to set volume to 0, unmute and set the configuration specified in the conf_tpa6130.h file (stereo, mono ..).

Note:
This function is called by tpa6130_dac_setup()
Returns:
A positive value upon success and a negative value upon failure.

A positive value upon success and a negative value upon failure.

Definition at line 289 of file tpa6130.c.

References HP_EN_L, HP_EN_R, TPA6130_CONTROL, TPA6130_I2C_ADDRESS_VERSION, TPA6130_MODE, tpa6130_read_data(), tpa6130_shadow_regs, TPA6130_TWI_ADDRESS, TPA6130_VOLUME_AND_MUTE, tpa6130_write_data(), TWI_READ_HW, and VERSION.

Referenced by main(), and tpa6130_dac_setup().

00290 {
00291   /* Check if the device responds on the TWI bus*/
00292   if(twi_probe(TPA6130_TWI, TPA6130_TWI_ADDRESS) != TWI_SUCCESS)
00293   return TWI_NO_CHIP_FOUND;
00294   /* If the device has no valid version we can not use it */
00295   if(tpa6130_read_data(TPA6130_I2C_ADDRESS_VERSION, TWI_READ_HW)!= VERSION)
00296   {
00297     return -8;
00298   }
00299   /* un-mute the output channels, the volume is still 0 and
00300    * should be increased by an application (fade-in/fade-out) */
00301   tpa6130_write_data(TPA6130_VOLUME_AND_MUTE, tpa6130_shadow_regs.volume_and_mute);
00302   /* set stereo/mono mode and enable both amplifiers (left/right) */
00303   tpa6130_write_data(TPA6130_CONTROL,(TPA6130_MODE << 4) | HP_EN_L | HP_EN_R);
00304 
00305   return TWI_SUCCESS;
00306 }

void tpa6130_powerup ( void   ) 

Powers up the amplifier from low power mode. This is the software low power mode described in the datasheet.

Definition at line 319 of file tpa6130.c.

References SW_SHUTDOWN, TPA6130_CONTROL, tpa6130_read_data(), tpa6130_write_data(), and TWI_READ_HW.

Referenced by tpa6130_dac_start().

00320 {
00321   U8 data;
00322   data = tpa6130_read_data(TPA6130_CONTROL, TWI_READ_HW);
00323   tpa6130_write_data(TPA6130_CONTROL, data & (~SW_SHUTDOWN));
00324 }

void tpa6130_set_volume ( S8  volume  ) 

Sets the volume of the amplifier. Valid values are between 0 (min -59dB) and 63 (max 4dB) altough the function takes care of any values higher than that by setting it to max.

Definition at line 332 of file tpa6130.c.

References MUTE_L, MUTE_R, TPA6130_VOL_MAX, TPA6130_VOL_MIN, TPA6130_VOLUME_AND_MUTE, and tpa6130_write_data().

Referenced by main(), tpa6130_dac_decrease_volume(), tpa6130_dac_increase_volume(), and tpa6130_dac_set_volume().

00333 {
00334   S8 new_volume = volume;
00335 
00336   if(volume > TPA6130_VOL_MAX)
00337   {
00338     new_volume = TPA6130_VOL_MAX;
00339   }
00340   else if(volume <= TPA6130_VOL_MIN )
00341   {
00342     // MUTE Left and Right;
00343     new_volume = MUTE_L|MUTE_R;
00344   }
00345   
00346   tpa6130_write_data(TPA6130_VOLUME_AND_MUTE, new_volume );
00347 }

void tpa6130_shutdown ( void   ) 

Shuts down the amplifier and sets it into low power mode. This is the software low power mode described in the datasheet.

Definition at line 311 of file tpa6130.c.

References SW_SHUTDOWN, TPA6130_CONTROL, tpa6130_read_data(), tpa6130_write_data(), and TWI_READ_HW.

Referenced by tpa6130_dac_stop().

00312 {
00313   U8 data;
00314   data = tpa6130_read_data(TPA6130_CONTROL, TWI_READ_HW);
00315   tpa6130_write_data(TPA6130_CONTROL, data | SW_SHUTDOWN);
00316 }


Generated on Fri Feb 19 02:23:28 2010 for AVR32 UC3 - Example of using the TPA6130A2 by  doxygen 1.5.5