📄 radiodrv.c
字号:
HAL_WriteGPIO(GRP_RADIOSCK, PIN_RADIOSCK, 1); //sck as 1
HAL_WriteGPIO(GRP_RADIOSEN, PIN_RADIOSEN, 1); //sen as 1
HAL_WriteGPIO(GRP_RADIOGPIO3, PIN_RADIOGPIO3, 0); //gpio3 as 1
HAL_WriteGPIO(GRP_RADIORST, PIN_RADIORST, 0);
Delay_mS(1);
HAL_WriteGPIO(GRP_RADIOSDA, PIN_RADIOSDA, 0);//sda as 0
//reset pin is active high, so needs to control reset pin
Delay_mS(1);
HAL_WriteGPIO(GRP_RADIORST, PIN_RADIORST, 1);
Delay_mS(1);
//return the default condition
HAL_WriteGPIO(GRP_RADIOSDA, PIN_RADIOSDA, 1);
HAL_WriteGPIO(GRP_RADIOGPIO3, PIN_RADIOGPIO3, 1); //gpio3 as 1
HAL_WriteGPIO(GRP_RADIOGPIO3, PIN_RADIOGPIO2, 1); //gpio3 as 1
HAL_WriteGPIO(GRP_RADIOSEN, PIN_RADIOSEN, 1); //sen as 1
HAL_WriteGPIO(GRP_RADIOSCK, PIN_RADIOSCK, 1); //sck as 1
Delay_mS(50);
// printf("reset exit");
}
//*********************************************************************************************************
// @summary This function be used to power up radio tuner of si4703
// @param None
// @return None
// @retval None
// @description
// This function be used to power-up for radio tuner of si4703
// @bug None
// @history
//*********************************************************************************************************
void RADIODRV_PowerUp(void)
{
//printf("\n @@ power up!!");
//recover volume setting
bRADIODRVTmp=RADIODRV_GetVol();
bRADIODRVTmp&=0x0F; //radio volume max step as 0x0F
//power up need set 0x02
wRADIODRVTunerReg[2]=0x4801; //dis-able mute and powe up si4703
wRADIODRVTunerReg[3]=0x0000;
wRADIODRVTunerReg[4]=0x4004;
wRADIODRVTunerReg[5]=0x1910|bRADIODRVTmp;
wRADIODRVTunerReg[6]=0x0200;
_RADIODRV_WriteReg(0x06,wRADIODRVTunerReg);
Delay_mS(85); //si4703 needs power up time
//-------------------------------------
}
//*********************************************************************************************************
// @summary This function be used to power down radio tuner of si4703
// @param None
// @return None
// @retval None
// @description
// This function be used to power-down for radio tuner of si4703
// @bug None
// @history
//*********************************************************************************************************
void RADIODRV_PowerDown(void)
{
wRADIODRVTunerReg[2]=0x0041; //enable mute and power down mode and enable mute
_RADIODRV_WriteReg(0x02,wRADIODRVTunerReg);
HAL_WriteGPIO(GRP_RADIOSDA, PIN_RADIOSDA, 1);
HAL_WriteGPIO(GRP_RADIOGPIO3, PIN_RADIOGPIO3, 1); //gpio3 as 1
HAL_WriteGPIO(GRP_RADIOGPIO3, PIN_RADIOGPIO2, 1); //gpio3 as 1
HAL_WriteGPIO(GRP_RADIOSEN, PIN_RADIOSEN, 1); //sen as 1
HAL_WriteGPIO(GRP_RADIOSCK, PIN_RADIOSCK, 1); //sck as 1
}
//*********************************************************************************************************
// @summary This function be used to convert frequency to channel
// @param wFreq Desired frequency which needs to convert to channel
// @return number of channel
// @retval number of channel which user desires to convert frequency
// @description
// This function be used to convert frequency to channel
// @bug None
// @history
//*********************************************************************************************************
WORD _RADIODRV_FreqToChan(WORD wFreq)
{
WORD wChannelSpace;
WORD wBottomBand;
WORD wChannel;
//step 1: Setting BottmBand from band select Reg[0x05].bit[7:6]
if((wRADIODRVTunerReg[0x05]&0x00C0)==0x0000)
wBottomBand=8750;//87.5Mhz
else
wBottomBand=7600;//76Mhz
if(wFreq<wBottomBand)
wFreq=wBottomBand;
//step2:Setting Channel space from channel space
if((wRADIODRVTunerReg[0x05]&0x0030)==0x0000)
{
wChannelSpace=20;//200khz
}
else if((wRADIODRVTunerReg[0x05]&0x0030)==0x0010)
{
wChannelSpace=10;//100Khz
}
else
{
wChannelSpace=5;//50Khz
}
//step3 :get channel value
wChannel=(wFreq-wBottomBand)/wChannelSpace;
//only for debug
//printf("\n @wFreq=%x",wFreq);
return (wChannel);
}
//*********************************************************************************************************
// @summary This function be used to convert channel to frequency
// @param wChan Desired channel which needs to convert to frequency
// @return value of frequency
// @retval value of frequency which user desires to convert channel
// @description
// This function be used to convert channel to frequency
// @bug None
// @history
//*********************************************************************************************************
WORD _RADIODRV_ChanToFreq(WORD wChan)
{
WORD wChannelSpace;
WORD wBottomBand;
WORD wFreq;
//step 1: Setting BottmBand from band select Reg[0x05].bit[7:6]
if((wRADIODRVTunerReg[0x05]&0x00C0)==0x0000)
wBottomBand=8750;//87.5Mhz
else
wBottomBand=7600;//76Mhz
//step2:Setting Channel space from channel space
if((wRADIODRVTunerReg[0x05]&0x0030)==0x0000)
wChannelSpace=20;//200khz
else if((wRADIODRVTunerReg[0x05]&0x0030)==0x0010)
wChannelSpace=10;//100Khz
else
wChannelSpace=5;//50Khz
//step3: get the frequency
wFreq=wBottomBand+wChannelSpace*wChan;
return (wFreq);
}
//*********************************************************************************************************
// @summary This function be used to tune radio station
// @param wRADIODRVfreq desired tune frequency
// @return status which be tuned frequency
// @retval TRUE Tune frequency is sucessful
// @retval FALSE Tune frequency is un-sucessful
// @description
// This function be used to tune frequency
// @bug None
// @history
//*********************************************************************************************************
BYTE RADIODRV_tune(WORD wRADIODRVfreq)
{
WORD wRADIODRVTemp;
//[step 0 ]: check the boundary value for band select
if(RADIODRV_GetBand()==RADIODRV_BS_USA)
{
if(wRADIODRVfreq>=10800) wRADIODRVTemp=10800;
if(wRADIODRVfreq<=8750)wRADIODRVTemp=8750;
}// japan wide band
else if(RADIODRV_GetBand()==RADIODRV_BS_JapWide)
{
if(wRADIODRVfreq>=10800) wRADIODRVTemp=10800;
if(wRADIODRVfreq<=7600)wRADIODRVTemp=7600;
}
else // japan
{
if(wRADIODRVfreq>=9000) wRADIODRVTemp=9000;
if(wRADIODRVfreq<=7600)wRADIODRVTemp=7600;
}
//[step 1 ]: get channel number from current frequency and enable tune bit
wRADIODRVTunerReg[3]=_RADIODRV_FreqToChan(wRADIODRVTemp)|0x8000;
_RADIODRV_WriteReg(0x03,wRADIODRVTunerReg);
//[step2]: wait the tune complete bit set
//added time-out method to avoid radio system hang
dwRadioDrvTime=OS_GetSysTimer();
do{
_RADIODRV_ReadReg(0x0A, wRADIODRVTunerReg);
if((OS_GetSysTimer()-dwRadioDrvTime)>COUNT_2_SEC)
{
//printf("\n @@ Tuner_F1");
return FALSE;
}
} while ((wRADIODRVTunerReg[10] & 0x4000) == 0x00);
//[step3]: clean tune bit at register 0x03
wRADIODRVTunerReg[ 3] &= ~0x8000;
_RADIODRV_WriteReg(0x03,wRADIODRVTunerReg);
//[step4]: wait STC bit be cleared
//added time-out method to avoid radio system hang
dwRadioDrvTime=OS_GetSysTimer();
do {
_RADIODRV_ReadReg(0x0A, wRADIODRVTunerReg);
if((OS_GetSysTimer()-dwRadioDrvTime)>COUNT_2_SEC)
{
// printf("\n @@ Tuner_F2");
return FALSE;
}
} while ((wRADIODRVTunerReg[10] & 0x4000) != 0x00);
//read the channel and store channel number
_RADIODRV_ReadReg(0x0B, wRADIODRVTunerReg);
return TRUE;
// printf("\n @ tune end");
}
//*********************************************************************************************************
// @summary This function be used to control volume of radio tuner
// @param bVol Desire to setting volume steps
// @return None
// @retval None
// @description
// This function be used to set volume of radio tuner
// @bug None
// @history
//*********************************************************************************************************
void RADIODRV_SetVol (BYTE bVol)
{
wRADIODRVTunerReg[5] &= (~0x000F);
wRADIODRVTunerReg[5] |= bVol & 0x000F;
_RADIODRV_WriteReg(0x05,wRADIODRVTunerReg);
// printf("@@se volume %x",wRADIODRVTunerReg[5] );
}
//*********************************************************************************************************
// @summary This function be used to set mute for volume
// @param bmute bmute as TRUE which disable mute,otherwise enalbe mute
// @return None
// @retval None
// @description
// This function be used to mute volume of radio
// @bug None
// @history
//*********************************************************************************************************
void RADIODRV_SetMute (BYTE bMute)
{
//Si4703 mute control must use hardware mute
if (bMute)
{
wRADIODRVTunerReg[2] |= 0x4000; //disable mute
}
else
{
wRADIODRVTunerReg[2]&= (~0x4000); //enable mute
}
_RADIODRV_WriteReg(0x02,wRADIODRVTunerReg);
// printf("@@volume %x",wRADIODRVTunerReg[2] );
}
//*********************************************************************************************************
// @summary This function be used to initiate radio tuner
// @param None
// @return Initial status of radio tuner
// @retval None
// @description
// This function be used to initiate radio tuner
// @bug None
// @history
//*********************************************************************************************************
BYTE RADIODRV_Init(void)
{
//BYTE bIndex;
// printf("@@\n RADIODRV tuner int!!");
//[step0]: reset Radio ic
RADIODRV_Reset();
//[step1]: initial array value
_RADIODRV_ReadReg(0x09, wRADIODRVTunerReg); //read register from 0x0A~0x0F, 0x00~0x09
// for(bIndex=0;bIndex<16;bIndex++)
// printf("\n @1_%hx=%x",bIndex,wRADIODRVTunerReg[bIndex]);
//[step2]: initial default register value and power up
//wRADIODRVTunerReg[2]=0x4001; //dis-able mute and powe up si4703
wRADIODRVTunerReg[2]=0x4801; //dis-able mute and powe up si4703, enable Verbose mode
wRADIODRVTunerReg[3]=0x0000;
#ifdef SUPPORT_RADIO_RDS
wRADIODRVTunerReg[4]=0xD004;
#else
wRADIODRVTunerReg[4]=0x4004;
#endif
//wRADIODRVTunerReg[5]=0x1910;
wRADIODRVTunerReg[5]=0x0C10;
// wRADIODRVTunerReg[6]|=0x0048;
wRADIODRVTunerReg[6]=0x0200;
_RADIODRV_WriteReg(0x06,wRADIODRVTunerReg);
Delay_mS(85);
_RADIODRV_ReadReg(0x9, wRADIODRVTunerReg); //read register from 0x0A~0x0F, 0x00~0x09
// for(bIndex=0;bIndex<16;bIndex++)
// printf("\n @%hx=%x",bIndex,wRADIODRVTunerReg[bIndex]);
//----------------------------------------------------
//[Step3]: read ID to check Si47xx has been active
return _RADIODRV_GetChipID();
}
//*********************************************************************************************************
// @summary This function be used to get chip id of si4703
// @param None
// @return Chip ID for si4703
// @retval 0x04 SI4700
// @retval 0x01 SI4701
// @retval 0x02 SI4702
// @retval 0x03 SI4703
// @description
// This function be used to get chip-id of the si-4703
// @bug None
// @history
//*********************************************************************************************************
BYTE _RADIODRV_GetChipID(void)
{
//get current chip ID for si4703
_RADIODRV_ReadReg(0x01, wRADIODRVTunerReg);
if((wRADIODRVTunerReg[1] & 0x03C0)==0x0000)
return RADIODRV_SI4700;
else if ((wRADIODRVTunerReg[1] & 0x03C0)==0x0040)
return RADIODRV_SI4702;
else if ((wRADIODRVTunerReg[1] & 0x03C0)==0x0200)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -