📄 drv_vs1002.c
字号:
}
#else
Int32U Tmp;
Int32S iSize = Size;
MP3_ChipSelect(0);
while(iSize > 0)
{
if(StreamMode)
while(!MP3_DReq());
for(int i = 8; i; --i)
{
Tmp = *pData;
MP3_TranserByte(Tmp );
MP3_TranserByte(Tmp>> 8);
MP3_TranserByte(Tmp>>16);
MP3_TranserByte(Tmp>>24);
iSize -= 4;
if(!iSize)
{
return;
}
if(SrcAddInc)
{
++pData;
}
}
}
#endif // MP3_STREAM_MODE_ENA > 0
}
#if MP3_STREAM_MODE_ENA > 0
/*************************************************************************
* Function Name: MP3_DmaHandler
* Parameters: Int32U IntrType - Error / Terminal Count
* Return: none
*
* Description: Interrupt handler of the MP3 DMA.
*
*************************************************************************/
void MP3_DmaHandler(Int32U IntrType)
{
if (IntrType)
{
// Terminal Count
DMA_ClearIT(Channel6,DMA_TCC);
Mp3TransferStatus = Mp3DataTransferComplete;
}
else
{
// Error
DMA_ClearIT(Channel6,DMA_TCC);
DMA_ClearIT(Channel6,DMA_EC);
Mp3TransferStatus = Mp3DataTransferCompleteWithError;
DMA_ChannelCmd(DMA_Channel6,DISABLE);
}
}
/*************************************************************************
* Function Name: Tim0Handler
* Parameters: none
*
* Return: none
*
* Description: Timer 0 interrupt handler
*
*************************************************************************/
void Tim0Handler (void)
{
if (Mp3TransferStatus == Mp3DataTransferProgress)
{
for(Int32U i = 0; i < MP3_STREAM_SIZE; ++i)
{
SSP1->DR = *(pMp3Data+i);
}
if(AddInc)
{
pMp3Data += MP3_STREAM_SIZE;
}
if((Mp3Size -= MP3_STREAM_SIZE) <= 0)
{
Mp3TransferStatus = Mp3DataTransferComplete;
if(!PlayFile)
{
// Disable TIM0 OC1 interrupt
TIM0->CR2 &= ~TIM_IT_OC1;
// Disable TIM0 counter
TIM0->CR1 &= 0x7FFF;
}
}
}
// Update OC1R
TIM0->OC1R += DmaPeriodHold;
// Clear TIM0 flag OC1
TIM0->SR &= ~TIM_FLAG_OC1;
}
#endif // MP3_STREAM_MODE_ENA > 0
/*************************************************************************
* Function Name: Mp3SendCmd
* Parameters: MP3_Cmd_t Cmd, pInt32U pData
* Return: none
*
* Description: Send command to the MP3 module (VS1002)
*
*************************************************************************/
void Mp3SendCmd (MP3_Cmd_t Cmd, pInt32U pData)
{
Int32U Tmp,Size;
pMp3Stream_t pMp3Stream;
#if MP3_STREAM_MODE_ENA > 0
static Int32U Period, MinPeriod, MaxPeriod, PeriodDelta;
Int32U StreamID;
TIM_InitTypeDef TIM_InitStructure;
#endif // MP3_STREAM_MODE_ENA > 0
switch (Cmd)
{
case Mp3CmdPowerUp:
*pData = Mp3ModuleInit(*pData,*(pData+1));
break;
case Mp3CmdPowerDown:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_RxFifoDrain();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_ReadCmd);
MP3_TranserByte(MP3_STATUS);
Tmp = (MP3_TranserByte(0) << 8) & 0xFF00;
Tmp|= MP3_TranserByte(0) & 0x00FF;
MP3_ChipSelect(0);
// Set SS APDOWN2 bit (3)
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_STATUS);
MP3_TranserByte(Tmp >> 8);
MP3_TranserByte(Tmp | (1UL<<3));
MP3_ChipSelect(0);
// Wait 10ms
Dly100us((void*)100);
// Reset VS1002
MP3_Reset(1);
break;
case Mp3CmdGetRevision:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_RxFifoDrain();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_ReadCmd);
MP3_TranserByte(MP3_STATUS);
MP3_TranserByte(0);
Tmp = MP3_TranserByte(0);
*pData = (Tmp>>4) & 0x7;
MP3_ChipSelect(0);
break;
case Mp3CmdSetClkReg:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_WaitTxFifoEmptying();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_CLOCKF);
MP3_TranserByte(*pData>>8);
MP3_TranserByte(*pData );
MP3_ChipSelect(0);
break;
case Mp3CmdSetModeReg:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_WaitTxFifoEmptying();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_MODE);
MP3_TranserByte(*pData>>8);
MP3_TranserByte(*pData );
MP3_ChipSelect(0);
break;
case Mp3CmdSetVol:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_WaitTxFifoEmptying();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_VOL);
MP3_TranserByte(*pData>>8);
MP3_TranserByte(*pData );
MP3_ChipSelect(0);
break;
case Mp3CmdTstSin:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_RxFifoDrain();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_ReadCmd);
MP3_TranserByte(MP3_MODE);
Tmp = (MP3_TranserByte(0) << 8) & 0xFF00;
Tmp|= MP3_TranserByte(0) & 0x00FF;
MP3_ChipSelect(0);
// Set test bit
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_MODE);
MP3_TranserByte(Tmp >> 8);
MP3_TranserByte(Tmp | (1UL << 5));
MP3_ChipSelect(0);
if(*pData)
{
// Send test command sequence
MP3_TranserByte(0x53);
MP3_TranserByte(0xEF);
MP3_TranserByte(0x6E);
MP3_TranserByte(*pData);
MP3_TranserByte(0x00);
MP3_TranserByte(0x00);
MP3_TranserByte(0x00);
MP3_TranserByte(0x00);
}
else
{
// Send exit form the test command sequence
MP3_TranserByte(0x45);
MP3_TranserByte(0x78);
MP3_TranserByte(0x69);
MP3_TranserByte(0x74);
MP3_TranserByte(0x00);
MP3_TranserByte(0x00);
MP3_TranserByte(0x00);
MP3_TranserByte(0x00);
}
Dly100us((void*)1000);
// Clear test bit
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_MODE);
MP3_TranserByte(Tmp>>8);
MP3_TranserByte(Tmp );
MP3_ChipSelect(0);
break;
case Mp3CmdPlayStatus:
*pData = Mp3TransferStatus;
break;
case Mp3CmdPlay:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
pMp3Stream = (pMp3Stream_t)pData;
if (pMp3Stream->Size)
{
MP3_WaitTxFifoEmptying();
// for synchronization
MP3_ChipSelect(1);
if(!PlayFile)
{
Size = VS1002_BUFFER_SIZE/2;
Size = MIN(pMp3Stream->Size,Size);
Mp3InitDmaTransmit(pMp3Stream->pStream,Size,FALSE,TRUE,NULL);
// Wait until DMA transfer is active and player recognize MP3 file type
while(Mp3TransferStatus == Mp3DataTransferProgress);
#if MP3_STREAM_MODE_ENA > 0
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_RxFifoDrain();
// Init transfer timer depending of the file bitrate
MP3_ChipSelect(1);
MP3_TranserByte(MP3_ReadCmd);
MP3_TranserByte(MP3_HDAT1);
Tmp = (MP3_TranserByte(0) << 8) & 0xFF00;
Tmp|= MP3_TranserByte(0) & 0x00FF;
MP3_ChipSelect(0);
if((Tmp & 0xFFE0) != 0xFFE0)
{
// invalid stream
pMp3Stream->pStream = NULL;
return;
}
StreamID = (Tmp>>3) & 0x3;
MP3_ChipSelect(1);
MP3_TranserByte(MP3_ReadCmd);
MP3_TranserByte(MP3_HDAT0);
Tmp = (MP3_TranserByte(0) << 8) & 0xFF00;
Tmp|= MP3_TranserByte(0) & 0x00FF;
MP3_ChipSelect(0);
Tmp >>= 12;
Tmp &= 0xF;
if(StreamID == ISO_11172_3_1_0)
{
Period = ISO11172_Bitrate[Tmp];
}
else
{
Period = MPG2_Bitrate[Tmp];
}
if(!Period)
{
// invalid stream bitrate
pMp3Stream->pStream = NULL;
return;
}
Tmp = Period/20;
MinPeriod = Period - Tmp; // 95%
MaxPeriod = Period + Tmp; // 105%
PeriodDelta = Tmp/32;
DmaPeriodHold = Period;
// Timer 0
// TIM Configuration in Output Compare Timing Mode
TIM_InitStructure.TIM_Mode = TIM_OCM_CHANNEL_1; // OUTPUT COMPARE CHANNEL 1 Mode
TIM_InitStructure.TIM_OC1_Modes = TIM_TIMING; // OCMP1 pin is disabled
TIM_InitStructure.TIM_Clock_Source = TIM_CLK_APB; // assign PCLK to TIM_Clk
TIM_InitStructure.TIM_Prescaler = (48/8) - 1;
TIM_InitStructure.TIM_Pulse_Length_1 = Period;
TIM_Init(TIM0, &TIM_InitStructure);
TIM_CounterCmd(TIM0, TIM_CLEAR); // clear the timer 0 counter
// Clear TIM0 flag OC1
TIM_ClearFlag(TIM0,TIM_FLAG_OC1);
// Enable TIM0 OC1 interrupt
TIM_ITConfig(TIM0, TIM_IT_OC1, ENABLE);
TIM_DMACmd(TIM0,ENABLE);
// Enable TIM0 counter
TIM_CounterCmd(TIM0, TIM_START);
#endif // MP3_STREAM_MODE_ENA > 0
PlayFile = 1;
if((pMp3Stream->Size -= Size) == 0)
{
break;
}
}
#if MP3_STREAM_MODE_ENA > 0
// Correct stream sample rate
switch(pMp3Stream->PlaySpeed)
{
case Mp3PlayIncreaseSpeed:
if(DmaPeriodHold < MaxPeriod)
{
DmaPeriodHold += PeriodDelta;
}
break;
case Mp3PlayDecreaseSpeed:
if(DmaPeriodHold > MinPeriod)
{
DmaPeriodHold -= PeriodDelta;
}
break;
default:
if(DmaPeriodHold < Period)
{
DmaPeriodHold += PeriodDelta;
}
else if (DmaPeriodHold > Period)
{
DmaPeriodHold -= PeriodDelta;
}
}
#endif // STREAM_MODE_ENA > 0
Mp3InitDmaTransmit(pMp3Stream->pStream,pMp3Stream->Size,TRUE,TRUE,NULL);
break;
}
case Mp3CmdPlayStop:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable TIM0 OC1 interrupt
TIM_ITConfig(TIM0, TIM_IT_OC1, DISABLE);
// Disable TIM0 counter
TIM_CounterCmd(TIM0, TIM_STOP);
MP3_WaitTxFifoEmptying();
MP3_ChipSelect(1);
// Init Transfer of 2048 zero (clear on-chip buffer)
Mp2DmaTmp = 0;
Mp3InitDmaTransmit(&Mp2DmaTmp,2048,FALSE,FALSE,NULL);
PlayFile = 0;
break;
case Mp3CmdWideStereo:
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_RxFifoDrain();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_ReadCmd);
MP3_TranserByte(MP3_MODE);
Tmp = (MP3_TranserByte(0) << 8) & 0xFF00;
Tmp|= MP3_TranserByte(0) & 0x00FF;
MP3_ChipSelect(0);
if(*pData)
{
Tmp |= 1;
}
else
{
Tmp &= ~1;
}
// Set test bit
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_MODE);
MP3_TranserByte(Tmp >> 8);
MP3_TranserByte(Tmp | (1UL << 5));
MP3_ChipSelect(0);
break;
case Mp3CmdLoudness:
Tmp = *pData?0x8A:0;
// Wait until data transfer is active
while(Mp3TransferStatus == Mp3DataTransferProgress);
// Disable SPI1 DMA
SSP_DMACmd(SSP1,SSP_DMA_Transmit | SSP_DMA_Receive,DISABLE);
MP3_WaitTxFifoEmptying();
MP3_ChipSelect(1);
MP3_TranserByte(MP3_WriteCmd);
MP3_TranserByte(MP3_BASS);
MP3_TranserByte(Tmp>>8);
MP3_TranserByte(Tmp );
MP3_ChipSelect(0);
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -