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 624 of file tpa6130.c.

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

00625 {
00626   S8 volume = tpa6130_get_volume()& (~(MUTE_L | MUTE_R));;
00627   if( volume > TPA6130_VOL_MIN )
00628     --volume;
00629   tpa6130_set_volume( volume );
00630 }

void tpa6130_dac_flush ( void   ) 

Flushes the sample buffer being output to the DACs.

Definition at line 634 of file tpa6130.c.

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

U8 tpa6130_dac_get_volume ( void   ) 

Returns the current volume of the DAC.

Definition at line 590 of file tpa6130.c.

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

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

void tpa6130_dac_increase_volume ( void   ) 

Increases the output volume of the DACs.

Note:
The DACs must have been started beforehand.

Definition at line 612 of file tpa6130.c.

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

00613 {
00614   S8 volume = tpa6130_get_volume()& (~(MUTE_L | MUTE_R));
00615   if( volume < TPA6130_VOL_MIN )
00616     volume = TPA6130_VOL_MIN;
00617   tpa6130_set_volume(volume+1);
00618 }

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 582 of file tpa6130.c.

00583 {
00584   return FALSE;
00585 }

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 525 of file tpa6130.c.

00526 {
00527   return FALSE;
00528 }

void tpa6130_dac_mute ( Bool  mute  ) 

No more audio if input is true.

Definition at line 530 of file tpa6130.c.

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

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

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 484 of file tpa6130.c.

References tpa6130_output_param.

Referenced by main().

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

void tpa6130_dac_set_volume ( U8  volume  ) 

Set the volume of the DAC.

Definition at line 603 of file tpa6130.c.

References tpa6130_set_volume().

00604 {
00605    tpa6130_set_volume(volume);
00606 }

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 410 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().

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

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 370 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().

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

void tpa6130_dac_stop ( void   ) 

Stops the DACs.

Definition at line 653 of file tpa6130.c.

References TPA6130_ABDAC_GPIO_MAP, tpa6130_output_param, and tpa6130_shutdown().

Referenced by tpa6130_dac_start().

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

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 356 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().

00357 {
00358   return tpa6130_read_data(TPA6130_VOLUME_AND_MUTE, TWI_READ_SR);
00359 }

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 292 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().

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

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 322 of file tpa6130.c.

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

Referenced by tpa6130_dac_start().

00323 {
00324   U8 data;
00325   data = tpa6130_read_data(TPA6130_CONTROL, TWI_READ_HW);
00326   tpa6130_write_data(TPA6130_CONTROL, data & (~SW_SHUTDOWN));
00327 }

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 335 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().

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

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 314 of file tpa6130.c.

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

Referenced by tpa6130_dac_stop().

00315 {
00316   U8 data;
00317   data = tpa6130_read_data(TPA6130_CONTROL, TWI_READ_HW);
00318   tpa6130_write_data(TPA6130_CONTROL, data | SW_SHUTDOWN);
00319 }


Generated on Thu Dec 17 19:57:29 2009 for AVR32 UC3 - Example of using the TPA6130A2 by  doxygen 1.5.5