⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mt6189a1_drv.c

📁 gemini0816 fm drv new
💻 C
📖 第 1 页 / 共 5 页
字号:
#if defined MT6189_DEBUG
   MT6189_DumpCtrlWord();
#endif
   return true;
}

static int16 MT6189_HiLoInjectOptimizer(int32 CurFreq, int16 SignalLevel)
{
   uint16 LevelHigh, LevelLow;

   if (MT6189_SetFreq(CurFreq + 2 * PRESET_IF, HI_INJECTION) == false)
      return SET_FREQ_FAIL;
   LevelHigh = MT6189_GetCurRSSI();

   if (MT6189_SetFreq(CurFreq - 2 * PRESET_IF, HI_INJECTION) == false)
      return SET_FREQ_FAIL;
   LevelLow = MT6189_GetCurRSSI();

   if (LevelHigh < SignalLevel && LevelLow < SignalLevel)
      return RSSI_CHECK_FAIL;
   if (LevelHigh > LevelLow)
      return LO_INJECTION;
   else
      return HI_INJECTION;
}

static void MT6189_SetVolumeLevel(uint8 level) {
	uint8  TmpReg;
   /// Bug fix: Fix the mono/stereo gain issue
   const uint8 prev_level = _current_level;
   	TmpReg = MT6189_ReadCache(CW(3, 0)) & 0xE3;		//end Shorten locking time loop   
	MT6189_WriteByte(CW(3, 0), TmpReg);
   ASSERT((level & 0x7F) < 13);

   _current_level = level;

   if ((false == _is_fm_on) || _is_fm_mute) {
      return;
   }

   if (prev_level == 0 && level != 0) {
      MT6189_Mute(0);
   } else if (prev_level != 0 && level == 0) {
      /// This is a special case.
      /// If the level is set to zero, there is still sound leakage due to the HW design of MT6189.
      /// Thus, we use MT6189_Mute to force it keeps silent.
      /// However, the _is_fm_mute flag is not raised since it is not really a mute situation.
      MT6189_Mute(1);
      _is_fm_mute = 0;
      return;
   }

   SerialCommInit();
   {
      uint8 TmpReg,TmpReg1,TmpReg2,TmpReg3;
      MT6189_ReadByte(CW(0, 0), &TmpReg2);
      MT6189_ReadByte(CW(0, 1), &TmpReg3);
      MT6189_ReadByte(CW(18, 1), &TmpReg);
      MT6189_ReadByte(CW(14, 0), &TmpReg1);
      if ((TmpReg2 == 1)&&(TmpReg3 == 1))
      {
#if !defined( MT6205B ) && !defined( MT6208 )
      if (TmpReg & 0x80) {
         /// Stereo
         TmpReg1&=0xEF;
         MT6189_WriteByte(CW(14, 0),(uint8)(TmpReg1) | 0x90);
           MT6189_WriteByte(CW(16, 0), 66);
           MT6189_WriteByte(CW(16, 1), 66);
        } else {
           /// Mono
           TmpReg1&=0x6F;
           MT6189_WriteByte(CW(14, 0),TmpReg1);
           MT6189_WriteByte(CW(16, 0), 48);
           MT6189_WriteByte(CW(16, 1), 48);
        }
#else
        if (TmpReg & 0x80) {
           /// Stereo
           TmpReg1&=0xEF;
           MT6189_WriteByte(CW(14, 0),TmpReg1|0x90);
         MT6189_WriteByte(CW(16, 0), (uint8)(64 | (level & 0x7F)));
         MT6189_WriteByte(CW(16, 1), (uint8)(64 | (level & 0x7F)));
      } else {
         /// Mono
         TmpReg1&=0x6F;
         MT6189_WriteByte(CW(14, 0),TmpReg1);
         MT6189_WriteByte(CW(16, 0), (uint8)(48 | (level & 0x7F)));
         MT6189_WriteByte(CW(16, 1), (uint8)(48 | (level & 0x7F)));
        }

#endif
    }
    else
    {
#if !defined( MT6205B ) && !defined( MT6208 )
        if (TmpReg & 0x80) {
           /// Stereo
           TmpReg1&=0xEF;
           MT6189_WriteByte(CW(14, 0),(uint8)(TmpReg1) | 0x90);
           MT6189_WriteByte(CW(16, 0), 14);
           MT6189_WriteByte(CW(16, 1), 14);
        } else {
           /// Mono
           TmpReg1&=0x6F;
           MT6189_WriteByte(CW(14, 0),TmpReg1);
           MT6189_WriteByte(CW(16, 0), 11);
           MT6189_WriteByte(CW(16, 1), 11);
        }
#else
        if (TmpReg & 0x80) {
           /// Stereo
           TmpReg1&=0xEF;
           MT6189_WriteByte(CW(14, 0),TmpReg1|0x90);
           MT6189_WriteByte(CW(16, 0), (uint8)(level & 0x0F));
           MT6189_WriteByte(CW(16, 1), (uint8)(level & 0x0F));
        } else {
           /// Mono
           TmpReg1&=0x6F;
           MT6189_WriteByte(CW(14, 0),TmpReg1);
           MT6189_WriteByte(CW(16, 0), (uint8)(level & 0x0F));
           MT6189_WriteByte(CW(16, 1), (uint8)(level & 0x0F));
        }
#endif
      }
   }
   SerialCommRelease();
}

static void MT6189_Mute(uint8 mute) {
   uint8 TmpReg;

   _is_fm_mute = (bool)mute;
   if (false == _is_fm_on)
      return;

   if (_current_level == 0 && mute == 0)
      return;

   SerialCommInit();

   TmpReg = MT6189_ReadCache(CW(14, 1)) & 0xF8;
#ifndef USE_MT6189_AMP
   TmpReg |= 0x03;
#endif
   if (mute != 0)
      TmpReg |= 0x07;
   MT6189_WriteByte(CW(14, 1), TmpReg);

   if (mute == 0) {
      MT6189_SetVolumeLevel(_current_level);
   } else {
      MT6189_WriteByte(CW(16, 0), 0);
      MT6189_WriteByte(CW(16, 1), 0);
   }
   SerialCommRelease();
}

/// level ranges from 0 to 12
void FMDrv_SetVolumeLevel(uint8 level)
{
#ifdef MT6189_DEBUG_DUMP_LOG
   kal_sprintf((void*)_dbg_str, "\nFMDrv_SetVolumeLevel(%d);\n\0", level);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   MT6189_SetVolumeLevel(level);
}

void FMDrv_Mute(uint8 mute)
{
#ifdef MT6189_DEBUG_DUMP_LOG
   kal_sprintf((void*)_dbg_str, "\nFMDrv_Mute(%d);\n\0", mute);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
   MT6189_Mute(mute);
}

void FMDrv_PowerOnReset(void)
{
   int32 fail_count;
   int32 i;
   uint8 tmp_reg;

#ifdef MT6189_DEBUG_DUMP_LOG
   uint32 start_t, duration_t;
   _file_handle = FS_Open((const WCHAR *)_file_name, FS_CREATE);
   kal_sprintf((void*)_dbg_str, "\nFMDrv_PowerOnReset();\n\0");
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   start_t = video_get_current_time();
#endif

   SerialCommInit();
   SerialCommCryClkOn();

   /// Power On
   for (i=0; i<POWER_ON_COMMAND_COUNT; i++) {
      MT6189_WriteByte(PowerOnSetting[i].addr, PowerOnSetting[i].value);
   }
   
#if defined MT6189_DEBUG
   MT6189_DumpCtrlWord();
#endif
   
   /// Calibrate PLL
   fail_count = 0;
   for (i=0; i<CAL_PLL_COMMAND_COUNT; i++) {
      tmp_reg = MT6189_ReadCache(CalPLLProcess[i].addr);
      tmp_reg &= CalPLLProcess[i].and;
      tmp_reg |= CalPLLProcess[i].or;
      if (false == MT6189_WriteByte(CalPLLProcess[i].addr, tmp_reg)) {
         if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD){
            ASSERT(0);
         }
         i = 0;
      }
   }

#if defined MT6189_DEBUG
   MT6189_DumpCtrlWord();
#endif

   /// Calibrate demod I
   fail_count = 0;
   for (i=0; i<CAL_DEMOD_I_COMMAND_COUNT; i++) {
      tmp_reg = MT6189_ReadCache(CalDemodIProcess[i].addr);
      tmp_reg &= CalDemodIProcess[i].and;
      tmp_reg |= CalDemodIProcess[i].or;
      if (false == MT6189_WriteByte(CalDemodIProcess[i].addr, tmp_reg)) {
         if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD){
            ASSERT(0);
         }
         i = 0;
      }
   }

#if defined MT6189_DEBUG
   MT6189_DumpCtrlWord();
#endif

   /// Calibrate demod Q
   fail_count = 0;
   for (i=0; i<CAL_DEMOD_Q_COMMAND_COUNT; i++) {
      tmp_reg = MT6189_ReadCache(CalDemodQProcess[i].addr);
      tmp_reg &= CalDemodQProcess[i].and;
      tmp_reg |= CalDemodQProcess[i].or;
      if (false == MT6189_WriteByte(CalDemodQProcess[i].addr, tmp_reg)) {
         if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD){
            ASSERT(0);
         }
         i = 0;
      }
   }
   
#if defined MT6189_DEBUG
   MT6189_DumpCtrlWord();
#endif

   /// Calibrate Pilot
   {
      uint8 k;
      fail_count = 0;
      for (i=0; i<CAL_PILOT_COMMAND_COUNT; i++) {
         tmp_reg = MT6189_ReadCache(CalPilotProcess[i].addr);
         tmp_reg &= CalPilotProcess[i].and;
         tmp_reg |= CalPilotProcess[i].or;
         if (false == MT6189_WriteByte(CalPilotProcess[i].addr, tmp_reg)) {
            if (++fail_count == CALIBRATION_FAIL_COUNT_THRESHOLD){
               ASSERT(0);
            }
            i = 0;
         }
      }
      MT6189_ReadByte(CW( 97, 0), &tmp_reg);
      if (tmp_reg < 14)
         k = tmp_reg+1;
      else
         k = 15;

      MT6189_WriteByte(CW( 6, 1), (uint8)(MT6189_ReadCache(CW( 6, 1)) & 0x0F) | 0x10);
      MT6189_WriteByte(CW( 7, 1), 0);
      MT6189_WriteByte(CW( 97, 0), k);

      tmp_reg = MT6189_ReadCache(CW( 3, 0)) & 0x1F;
#if defined   REF_CLK_32K
      tmp_reg |= 0x20;
#elif defined REF_CLK_13M
      tmp_reg |= 0x40;
#elif defined REF_CLK_26M
      tmp_reg |= 0xC0;
#endif
      MT6189_WriteByte(CW( 3, 0), tmp_reg);
      MT6189_WriteByte(CW( 3, 1), (uint8)(MT6189_ReadCache(CW( 3, 1)) & 0xFB));
   }

   MT6189_WriteByte(CW(8, 1), (uint8)(MT6189_ReadCache(CW(8, 1)) & 0xFE));

   MT6189_WriteByte(CW(9, 1), (uint8)(MT6189_ReadCache(CW(9, 1)) & 0xFE));
   MT6189_ReadByte(CW( 97, 1), &tmp_reg);
   if(tmp_reg >= 13)
   	MT6189_WriteByte(CW( 4, 0), (uint8)(MT6189_ReadCache(CW( 4, 0)) & 0x8F) | 0x30);
   else if(tmp_reg == 12)
   	MT6189_WriteByte(CW( 4, 0), (uint8)(MT6189_ReadCache(CW( 4, 0)) & 0x8F) | 0x40);
   else if(tmp_reg == 11)
   	MT6189_WriteByte(CW( 4, 0), (uint8)(MT6189_ReadCache(CW( 4, 0)) & 0x8F) | 0x50);
   else if(tmp_reg == 10)
   	MT6189_WriteByte(CW( 4, 0), (uint8)(MT6189_ReadCache(CW( 4, 0)) & 0x8F) | 0x60);
   else if(tmp_reg <= 9)
   	MT6189_WriteByte(CW( 4, 0), (uint8)(MT6189_ReadCache(CW( 4, 0)) & 0x8F) | 0x70);

#if defined MT6189_DEBUG
   MT6189_DumpCtrlWord();
#endif
   SerialCommRelease();
   _is_fm_on = true;
   _current_frequency = -1;
   _rssi_threshold = FM_RADIO_INPUT_LEVEL_THRESHOLD;
   if(RSSI_offset != 0)
    _rssi_threshold = RSSI_offset;

#ifdef MT6189_DEBUG_DUMP_LOG
   duration_t = video_get_duration_ms(start_t);
   kal_sprintf((void*)_dbg_str, "   time cost: %d ms.\n\0", duration_t);
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
}

void FMDrv_PowerOffProc(void)
{
   int32 i;
   uint8 tmp_reg;

#ifdef MT6189_DEBUG_DUMP_LOG
   if (_file_handle == 0)
      ASSERT(0);
   kal_sprintf((void*)_dbg_str, "\nFMDrv_PowerOffProc();\n\0");
   FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
   FS_Close(_file_handle);
   _file_handle = 0;
#endif

#ifdef USE_MT6189_AMP
   SwitchExtFMPath(0);
#endif

   SerialCommInit();
   for (i=0; i<POWER_OFF_COMMAND_COUNT; i++) {
      tmp_reg = MT6189_ReadCache(PowerOffProc[i].addr);
      tmp_reg &= PowerOffProc[i].and;
      tmp_reg |= PowerOffProc[i].or;
      MT6189_WriteByte(PowerOffProc[i].addr, tmp_reg);
   }
   SerialCommCryClkOff();
   SerialCommRelease();
   _is_fm_on = false;
   _current_frequency = -1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -