📄 sound.c
字号:
//*****************************************************************************
//
// sound.c - Sound driver for the DK-LM3S9B92 board.
//
// Copyright (c) 2008-2009 Luminary Micro, Inc. All rights reserved.
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws. All rights are reserved. You may not combine
// this software with "viral" open-source software in order to form a larger
// program. Any use in violation of the foregoing restrictions may subject
// the user to criminal sanctions under applicable laws, as well as to civil
// liability for the breach of the terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 5228 of the DK-LM3S9B92 Firmware Package.
//
//*****************************************************************************
//*****************************************************************************
//
//! \addtogroup sound_api
//! @{
//
//*****************************************************************************
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_timer.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "inc/hw_udma.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_i2s.h"
#include "driverlib/i2s.h"
#include "driverlib/udma.h"
#include "drivers/tlv320aic23b.h"
#include "drivers/sound.h"
//*****************************************************************************
//
// The current volume of the music/sound effects.
//
//*****************************************************************************
static unsigned char g_ucVolume = 100;
//*****************************************************************************
//
// A pointer to the song currently being played, if any. The value of this
// variable is used to determine whether or not a song is being played. Since
// each entry is a short, the maximum length of the song is 65536 / 200
// seconds, which is around 327 seconds.
//
//*****************************************************************************
static const unsigned short *g_pusMusic = 0;
//*****************************************************************************
//
// Interrupt values for tone generation.
//
//*****************************************************************************
static unsigned long g_ulFrequency;
static unsigned long g_ulDACStep;
static unsigned long g_ulSize;
static unsigned long g_ulTicks;
static unsigned short g_ulMusicCount;
static unsigned short g_ulMusicSize;
#define SAMPLE_RATE 48000
#define SAMPLE_LEFT_UP 0x00000001
#define SAMPLE_RIGHT_UP 0x00000002
//*****************************************************************************
//
// Sawtooth state information, this allows for a phase difference between left
// and right waveforms.
//
//*****************************************************************************
volatile struct
{
int iSample;
unsigned long ulFlags;
} g_sSample;
#define NUM_SAMPLES 512
static unsigned long g_pulTxBuf[NUM_SAMPLES];
//*****************************************************************************
//
// I2S Pin definitions.
//
//*****************************************************************************
#define I2S0_LRCTX_PERIPH (SYSCTL_PERIPH_GPIOE)
#define I2S0_LRCTX_PORT (GPIO_PORTE_BASE)
#define I2S0_LRCTX_PIN (GPIO_PIN_4)
#define I2S0_SDATX_PERIPH (SYSCTL_PERIPH_GPIOE)
#define I2S0_SDATX_PORT (GPIO_PORTE_BASE)
#define I2S0_SDATX_PIN (GPIO_PIN_5)
#define I2S0_SCLKTX_PERIPH (SYSCTL_PERIPH_GPIOB)
#define I2S0_SCLKTX_PORT (GPIO_PORTB_BASE)
#define I2S0_SCLKTX_PIN (GPIO_PIN_6)
#define I2S0_SDARX_PERIPH (SYSCTL_PERIPH_GPIOD)
#define I2S0_SDARX_PORT (GPIO_PORTD_BASE)
#define I2S0_SDARX_PIN (GPIO_PIN_4)
#define I2S0_MCLKTX_PERIPH (SYSCTL_PERIPH_GPIOF)
#define I2S0_MCLKTX_PORT (GPIO_PORTF_BASE)
#define I2S0_MCLKTX_PIN (GPIO_PIN_1)
//*****************************************************************************
//
// Buffer management structures and defines.
//
//*****************************************************************************
#define NUM_BUFFERS 2
static struct
{
//
// Pointer to the buffer.
//
unsigned long *pulData;
//
// Size of the buffer.
//
unsigned long ulSize;
//
// Callback function for this buffer.
//
tBufferCallback pfnBufferCallback;
}
g_sOutBuffers[NUM_BUFFERS],
g_sInBuffers[NUM_BUFFERS];
//*****************************************************************************
//
// A set of flags. The flag bits are defined as follows:
//
// 0 -> A RX DMA transfer is pending.
// 1 -> A TX DMA transfer is pending.
//
//*****************************************************************************
#define FLAG_RX_PENDING 0
#define FLAG_TX_PENDING 1
static volatile unsigned long g_ulDMAFlags;
//*****************************************************************************
//
// The buffer index that is currently playing.
//
//*****************************************************************************
static unsigned long g_ulPlaying;
static unsigned long g_ulRecording;
static unsigned long g_ulSampleRate;
static unsigned short g_usChannels;
static unsigned short g_usBitsPerSample;
//*****************************************************************************
//
// This function is used to generate a pattern to fill the TX buffer.
//
//*****************************************************************************
static unsigned long
PatternNext(void)
{
int iSample;
if(g_sSample.ulFlags & SAMPLE_LEFT_UP)
{
g_sSample.iSample += g_ulDACStep;
if(g_sSample.iSample >= 32767)
{
g_sSample.ulFlags &= ~SAMPLE_LEFT_UP;
g_sSample.iSample = 32768 - g_ulDACStep;
}
}
else
{
g_sSample.iSample -= g_ulDACStep;
if(g_sSample.iSample <= -32768)
{
g_sSample.ulFlags |= SAMPLE_LEFT_UP;
g_sSample.iSample = g_ulDACStep - 32768;
}
}
//
// Copy the sample to prevent a compiler warning on the return line.
//
iSample = g_sSample.iSample;
return((iSample & 0xffff) | (iSample << 16));
}
//*****************************************************************************
//
// Generate the next tone.
//
//*****************************************************************************
static unsigned long
SoundNextTone(void)
{
int iIdx;
g_sSample.iSample = 0;
g_sSample.ulFlags = SAMPLE_LEFT_UP;
//
// Set the frequency.
//
g_ulFrequency = g_pusMusic[g_ulMusicCount + 1];
//
// Calculate the step size for each sample.
//
g_ulDACStep = ((65536 * 2 * g_ulFrequency) / SAMPLE_RATE);
//
// How big is the buffer that needs to be restarted.
//
g_ulSize = (SAMPLE_RATE/g_ulFrequency);
//
// Cap the size in a somewhat noisy way. This will affect frequencies below
// 93.75 Hz or 48000/NUM_SAMPLES.
//
if(g_ulSize > NUM_SAMPLES)
{
g_ulSize = NUM_SAMPLES;
}
//
// Move on to the next value.
//
g_ulMusicCount += 2;
//
// Stop if there are no more entries in the list.
//
if(g_ulMusicCount < g_ulMusicSize)
{
g_ulTicks = (g_pusMusic[g_ulMusicCount] * g_ulFrequency) / 1000;
}
else
{
g_ulTicks = 0;
}
//
// Fill the buffer with the new tone.
//
for(iIdx = 0; iIdx < g_ulSize; iIdx++)
{
g_pulTxBuf[iIdx] = PatternNext();
}
//
// This should be the size in bytes and not words.
//
g_ulSize <<= 2;
return(g_ulTicks);
}
//*****************************************************************************
//
// Handles playback of the single buffer when playing tones.
//
//*****************************************************************************
static void
BufferCallback(void *pvBuffer, unsigned long ulEvent)
{
if((ulEvent & BUFFER_EVENT_FREE) && (g_ulTicks != 0))
{
//
// Kick off another request for a buffer playback.
//
SoundBufferPlay(pvBuffer, g_ulSize, BufferCallback);
//
// Count down before stopping.
//
g_ulTicks--;
}
else
{
//
// Stop requesting transfers.
//
I2STxDisable(I2S0_BASE);
}
}
//*****************************************************************************
//
//! Initializes the sound output.
//!
//! \param ulEnableReceive is set to 1 to enable the receive portion of the I2S
//! controller and 0 to leave the I2S controller not configured.
//!
//! This function prepares the sound driver to play songs or sound effects. It
//! must be called before any other sound function. The sound driver uses
//! uDMA with the I2S controller so the caller must ensure that the uDMA
//! peripheral is enabled and its control table configured prior to making this
//! call. The GPIO peripheral and pins used by the I2S interface are
//! controlled by the I2S0_*_PERIPH, I2S0_*_PORT and I2S0_*_PIN definitions.
//!
//! \return None
//
//*****************************************************************************
void
SoundInit(unsigned long ulEnableReceive)
{
//
// Set the current active buffer to zero.
//
g_ulPlaying = 0;
g_ulRecording = 0;
//
// Enable and reset the peripheral.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_I2S0);
//
// Select alternate functions for all of the I2S pins.
//
SysCtlPeripheralEnable(I2S0_SCLKTX_PERIPH);
GPIOPinTypeI2S(I2S0_SCLKTX_PORT, I2S0_SCLKTX_PIN);
SysCtlPeripheralEnable(I2S0_MCLKTX_PERIPH);
GPIOPinTypeI2S(I2S0_MCLKTX_PORT, I2S0_MCLKTX_PIN);
SysCtlPeripheralEnable(I2S0_LRCTX_PERIPH);
GPIOPinTypeI2S(I2S0_LRCTX_PORT, I2S0_LRCTX_PIN);
SysCtlPeripheralEnable(I2S0_SDATX_PERIPH);
GPIOPinTypeI2S(I2S0_SDATX_PORT, I2S0_SDATX_PIN);
//
// Initialize the DAC.
//
TLV320AIC23BInit();
//
// Set the FIFO trigger limit
//
I2STxFIFOLimitSet(I2S0_BASE, 4);
//
// Clear out all pending interrupts.
//
I2SIntClear(I2S0_BASE, I2S_INT_TXERR | I2S_INT_TXREQ );
//
// Disable all uDMA attributes.
//
uDMAChannelAttributeDisable(UDMA_CHANNEL_I2S0TX, UDMA_ATTR_ALL);
//
// Enable the I2S Tx controller.
//
I2STxEnable(I2S0_BASE);
//
// Only enable the RX channel if requested.
//
if(ulEnableReceive)
{
//
// Enable the I2S RX Data pin.
//
SysCtlPeripheralEnable(I2S0_SDARX_PERIPH);
GPIOPinTypeI2S(I2S0_SDARX_PORT, I2S0_SDARX_PIN);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -