S3C2416裸机开发系列十八_音频驱动实现(2)
扫描二维码
随时随地手机看文章
标签:WM8960IIS音频驱动s3c2416裸机开发
2014-06-10 09:50853人阅读评论(0)收藏举报
分类:
s3c2416裸机开发(24)
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
象棋小子 1048272975
IIS模块实现IIS.c如下:
#include"s3c2416.h"
#include "IIS.h"
#include"Exception.h"
#include"UART0.h"
#defineDEBUG_IIS
#ifdef DEBUG_IIS
#defineDebug(x...) Uart0_Printf(x)
#else
#defineDebug(x...)
#endif
staticunsigned char TX_Channel; // 发送音频的声道数
staticunsigned char TX_BitLen; // 发送位长
staticunsigned char RX_Channel; // 接收音频的声道数
staticunsigned char RX_BitLen; // 接收位长
staticvolatile unsigned char TxBufferFlag;
staticvolatile unsigned char RxBufferFlag;
staticunsigned int TxCount; // 播放时记录缓存中的写位置
staticunsigned int RxCount; // 录音时记录缓存中的读位置
// 插放与录音均采用双缓存,DMA传输的主存与cache会有数据一致性问题
// 音频DMA缓存分配到不开启cache的内存区域
staticunsigned int TxBuffer0[4*1024] __attribute__((section("No_Cache"),zero_init));
staticunsigned int TxBuffer1[4*1024] __attribute__((section("No_Cache"),zero_init));
staticunsigned int RxBuffer0[4*1024] __attribute__((section("No_Cache"),zero_init));
staticunsigned int RxBuffer1[4*1024] __attribute__((section("No_Cache"),zero_init));
static voidDMA_IRQ(void)
{
static unsigned char TxBufferChannel = 0;
static unsigned char RxBufferChannel = 0;
unsigned int DMA_Channel;
DMA_Channel = rSUBSRCPND;
if (DMA_Channel &(1< if (TxBufferChannel == 0) { rDISRC0 = ((unsigned int)TxBuffer1);// 开始使用Buffer1缓存 TxBufferFlag &= ~(1<<0);// 发送标志0位清空,说明Buffer0数据需填充 TxBufferChannel = 1; // 正在发送Buffer1缓存 } else { rDISRC0 = ((unsignedint)TxBuffer0);// 开始使用Buffer0缓存 TxBufferFlag &= ~(1<<1);// 发送标志1位清空,说明Buffer1数据需填充 TxBufferChannel = 0; // 正在发送Buffer0缓存 } rDCON0 = (rDCON0&(~0xfffff)) |(sizeof(TxBuffer0)/4); rDMASKTRIG0 = (1<<1); // IIS TX打开DMA0通道 rSUBSRCPND |= (1< } if (DMA_Channel &(1< if (RxBufferChannel == 0) { rDIDST1 = ((unsigned int)RxBuffer1);// DMA1目的地址 RxBufferFlag |= (1<<0); // 接收缓存0位置位,说明Buffer0数据准备好 RxBufferChannel = 1; // 下一次使用Buffer1 } else { rDIDST1 = ((unsigned int)RxBuffer0);// DMA1目的地址 RxBufferFlag |= (1<<1); // 接收缓存1位置位,说明Buffer1数据准备好 RxBufferChannel = 0; // 下一次使用Buffer0 } rDCON1 = (rDCON1&(~0xfffff)) |(sizeof(RxBuffer0)/4); rDMASKTRIG1 = (1<<1); // IIS RX打开DMA1通道 rSUBSRCPND |= (1< } rSRCPND1 |= (1 << INT_DMA); rINTPND1 |= (1 << INT_DMA); } unsignedint IIS_WriteBuffer(unsigned char *pData, unsigned int MaxLen) { unsigned int i; unsigned int nCount; // 能写入buffer中数据长度(以字计,fifo 32位长) unsigned int *pBuffer; unsigned char *pTemp = pData; if (pTemp==0 || MaxLen==0) { return 0; // 参数错误,数据未写入缓存 } if ((TxBufferFlag&0x3) == 0x3) { return 0; // Buffer0,Buffer1均已写满 } if (!(TxBufferFlag & (1<<0))) { //Buffer0需填充 pBuffer = &TxBuffer0[TxCount]; } else { // Buffer1需填充 pBuffer = &TxBuffer1[TxCount]; } nCount = (sizeof(TxBuffer0)/4) - TxCount; switch (TX_BitLen) { case 8: if (TX_Channel != 1) { // 双声道 if (MaxLen/2 == 0) {// 左右声道fifo中有两个8位有效音频数据 pTemp += MaxLen; // 不足一个采样2字节数据,丢弃写入buffer中 } MaxLen = MaxLen/2; // 32位的fifo中有两个8位有效音频数据 if (MaxLen < nCount) { nCount = MaxLen; } for (i=0; i *pBuffer++ = (((unsignedint)pTemp[1]<<16)+ ((unsignedint)pTemp[0]<<0)); pTemp += 2; // 2个8位的声道数据已写入buffer中 } } else { // 单声道 if (MaxLen < nCount) {// 32位的fifo中有一个8位有效音频数据 nCount = MaxLen; } for (i=0; i *pBuffer++ = (unsignedint)pTemp[0]<<0; pTemp += 1; // 1个8位的声道数据已写入FIFO中 } } break; case 16: if (TX_Channel != 1) { // 双声道 if (MaxLen/4 == 0) {// 左右声道fifo中有四个8位有效音频数据 pTemp += MaxLen; // 不足一个采样4字节数据,丢弃写入buffer中 } MaxLen = MaxLen/4; // 32位的fifo中有四个8位有效音频数据 if (MaxLen < nCount) { nCount = MaxLen; } for (i=0; i *pBuffer++ = (((unsignedint)pTemp[0]<<0) + ((unsignedint)pTemp[1]<<8)) +