📄 mt6189a1_drv.c
字号:
break;
default:
ASSERT(0);
}
break;
case RSSI_threshold:
if (item_value == 1)
RSSI_offset = 1;
else if (item_value == 2)
RSSI_offset = 2;
else if (item_value == 3)
RSSI_offset = 3;
else if (item_value == 4)
RSSI_offset = 4;
else if (item_value == 5)
RSSI_offset = 5;
else if (item_value == 6)
RSSI_offset = 6;
else
RSSI_offset = FM_RADIO_INPUT_LEVEL_THRESHOLD;
break;
case IF_count_delta:
if(item_value == 10)
{
#if !defined( MT6205B ) && !defined( MT6208 )
PRESET_IF_DELTA = 20;
#else
PRESET_IF_DELTA = 20;
#endif
}
else if(item_value == 15)
{
#if !defined( MT6205B ) && !defined( MT6208 )
PRESET_IF_DELTA = 30;
#else
PRESET_IF_DELTA = 30;
#endif
}
else if(item_value == 20)
{
#if !defined( MT6205B ) && !defined( MT6208 )
PRESET_IF_DELTA = 40;
#else
PRESET_IF_DELTA = 40;
#endif
}
else if(item_value == 25)
{
#if !defined( MT6205B ) && !defined( MT6208 )
PRESET_IF_DELTA = 50;
#else
PRESET_IF_DELTA = 50;
#endif
}
else if(item_value == 30)
{
#if !defined( MT6205B ) && !defined( MT6208 )
PRESET_IF_DELTA = 60;
#else
PRESET_IF_DELTA = 60;
#endif
}
else
{
#if !defined( MT6205B ) && !defined( MT6208 )
PRESET_IF_DELTA = 30;
#else
PRESET_IF_DELTA = 30;
#endif
}
break;
}
SerialCommRelease();
}
static uint16 MT6189_GetCurRSSI(void)
{
uint8 TmpReg;
MT6189_WriteByte(CW(12, 1), (uint8)(MT6189_ReadCache(CW(12, 1)) & 0xFD));
MT6189_WriteByte(CW(12, 0), (uint8)(MT6189_ReadCache(CW(12, 0)) & 0xFE));
MT6189_WriteByte(CW(14, 1), (uint8)(MT6189_ReadCache(CW(14, 1)) & 0xEF));
MT6189_ReadByte(CW(4, 1), &TmpReg);
return (TmpReg & 0x0F);
}
static uint8 MT6189_ReadIntFlags(bool ReadClear)
{
uint8 TmpReg;
MT6189_ReadByte(CW(1, 0), &TmpReg);
if (ReadClear)
MT6189_WriteByte(CW(1, 0), 0x00);
return TmpReg;
}
/// Timing window for 32K clock: 64/32768 = 0.00195 second
/// Timing window for 26M clock: 49152/26000000 = 0.00189 second
static int32 MT6189_GetCurIFCnt(uint8 count, uint8 early_check)
{
int32 CurIF = 0;
uint32 WaitingCounter = 0;
uint32 if_cnt;
uint16 rssi;
int32 i = (int32)count;
int32 rssi_consecutive_fail = 0;
int32 if_consecutive_fail = 0;
_valid_rssi = 0;
#ifdef MT6189_DEBUG_DUMP_LOG
kal_sprintf((void*)_dbg_str, " MT6189_GetCurIFCnt(%d, %d);\n \0", count, early_check);
FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
/// Disable counter
MT6189_WriteByte(CW(6, 1), 0x14);
/// Clear interrupt flags
MT6189_WriteByte(CW(1, 0), 0x00);
MT6189_WriteByte(CW(7, 1), (uint8)(MT6189_ReadCache(CW(7, 1)) & 0xFC));
MT6189_WriteByte(CW(6, 0), 0x01);
while (--i >= 0) {
uint8 TmpReg;
/// Clear IF counter
MT6189_WriteByte(CW(20, 0), 0x00);
MT6189_WriteByte(CW(20, 1), 0x00);
/// Enable counter
MT6189_WriteByte(CW(6, 1), 0x15);
/// Make sure that the counting is done
do {
if (++WaitingCounter == 4096) {
ASSERT(0);
return 0;
}
MT6189_ReadByte(CW(1, 0), &TmpReg);
} while ((TmpReg & 0x08) == 0);
/// Disable counter
MT6189_WriteByte(CW(6, 1), 0x14);
/// Clear interrupt flags
MT6189_WriteByte(CW(1, 0), 0x00);
MT6189_ReadByte(CW(20, 1), &TmpReg);
if_cnt = (TmpReg & 0x03);
if_cnt <<= 8;
MT6189_ReadByte(CW(20, 0), &TmpReg);
if_cnt += TmpReg;
CurIF += if_cnt;
#ifdef MT6189_DEBUG_DUMP_LOG
kal_sprintf((void*)_dbg_str, "%d,\0", if_cnt);
FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
rssi = (uint16)MT6189_GetCurRSSI();
_valid_rssi += rssi;
if (early_check) {
#ifdef MT6189_DEBUG_DUMP_LOG
kal_sprintf((void*)_dbg_str, "%d \0", rssi);
FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
if (rssi < _rssi_threshold) {
if (++rssi_consecutive_fail == 1) {
CurIF /= (count-i);
_valid_rssi /= (count-i);
#ifdef MT6189_DEBUG_DUMP_LOG
kal_sprintf((void*)_dbg_str, "\n RSSI Early Check Fail!\n\0");
FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
return -CurIF;
}
} else {
rssi_consecutive_fail = 0;
}
if (if_cnt > (PRESET_IF_CNT_PLUS + 120) ||
if_cnt < (PRESET_IF_CNT_PLUS - 120)) {
if (++if_consecutive_fail == 1) {
CurIF /= (count-i);
_valid_rssi /= (count-i);
#ifdef MT6189_DEBUG_DUMP_LOG
kal_sprintf((void*)_dbg_str, "\n IF Early Check Fail!\n\0");
FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
return -CurIF;
}
} else {
if_consecutive_fail = 0;
}
}
}
_valid_rssi = (_valid_rssi/count);
CurIF = (CurIF/count);
#ifdef MT6189_DEBUG_DUMP_LOG
kal_sprintf((void*)_dbg_str, "\n IF count returned: %d\n\0", CurIF);
FS_Write(_file_handle, _dbg_str, strlen((void*)_dbg_str), &_data_written);
#endif
return CurIF;
}
/// Timing window for 32K clock: 64/32768 = 0.00195 second
/// Timing window for 26M clock: 49152/26000000 = 0.00189 second
static uint16 MT6189_GetCurIF(void)
{
uint32 CurIF = MT6189_GetCurIFCnt(IF_MEASURE_COUNT, 0);
CurIF *= IF_CONVERT_FACTOR;
CurIF /= 1000;
return (uint16) CurIF;
}
static bool MT6189_SetFreq(int32 CurFreq, uint8 HiLoInj)
{
uint16 Divider, N2, Ns, Target;
uint8 TmpReg;
uint32 WaitingCounter = 0,freq = 0,CurFreq_PLUS = 0;
_current_frequency = CurFreq/FM_TUNER_GRID;
freq = CurFreq;
PRESET_IF_CNT_PLUS = PRESET_IF_CNT;
/// DividerNumber = (CurFreq +- PresetIF) * 4 / RefClock
if (HiLoInj == LO_INJECTION) {
CurFreq -= PRESET_IF;
} else {
CurFreq += PRESET_IF;
}
CurFreq *= 4000;
#if defined REF_CLK_32K
CurFreq += 16384; /// rounding
CurFreq >>= 15;
CurFreq_PLUS = (uint32)(CurFreq*32768/4000);
if(CurFreq_PLUS>freq)
PRESET_IF_CNT_PLUS = (uint16)((CurFreq_PLUS-freq)*1000*64/32768);
else
PRESET_IF_CNT_PLUS = (uint16)((freq-CurFreq_PLUS)*1000*64/32768);
#else
CurFreq += 16927; /// rounding
CurFreq /= 33854;
CurFreq_PLUS = (uint32)(CurFreq*33854/4000);
if(CurFreq_PLUS>freq)
PRESET_IF_CNT_PLUS = (uint16)((CurFreq_PLUS-freq)*1.891);//1000*49152/26000000
else
PRESET_IF_CNT_PLUS = (uint16)((freq-CurFreq_PLUS)*1.891);//1000*49152/26000000
#endif
Divider = CurFreq;
N2 = (Divider >> 6);
Ns = Divider - (N2 << 6);
Target = (Divider >> 4);
/// CW12 [0:0] 0
MT6189_WriteByte(CW(12, 0), (uint8)(MT6189_ReadCache(CW(12, 0)) & 0xFE));
/// CW12 [9:9] 0
MT6189_WriteByte(CW(12, 1), (uint8)(MT6189_ReadCache(CW(12, 1)) & 0xFD));
/// CW7 [4:0] 7
MT6189_WriteByte(CW(7, 0), (uint8)((MT6189_ReadCache(CW(7, 0)) & 0xE0) | 0x07));
/// Set Hi/Lo injection
TmpReg = MT6189_ReadCache(CW(4, 0)) & 0x7F;
if (HiLoInj == HI_INJECTION)
TmpReg |= 0x80;
MT6189_WriteByte(CW(4, 0), TmpReg);
/// Set IF as 140 khz
MT6189_WriteByte(CW(4, 1), (uint8)((MT6189_ReadCache(CW(4, 1)) & 0xBF) | 0x40));
/// CW2 [13:8] 000000
MT6189_WriteByte(CW(2, 1), (uint8)(MT6189_ReadCache(CW(2, 1)) & 0xC0));
/// CW2 [7:0] N2
MT6189_WriteByte(CW(2, 0), (uint8)N2);
/// CW3 [10:10] 1, [13:13] 1
MT6189_WriteByte(CW(3, 1), (uint8)((MT6189_ReadCache(CW(3, 1)) & 0xDB) | 0x24));
TmpReg = MT6189_ReadCache(CW(3, 0)) & 0x1C;
#if defined REF_CLK_32K
TmpReg |= 0x22;
#elif defined REF_CLK_13M
TmpReg |= 0x42;
#elif defined REF_CLK_26M
TmpReg |= 0xC2;
#endif
MT6189_WriteByte(CW(3, 0), TmpReg); /// CW3
/// CW7 [9:8] 3
MT6189_WriteByte(CW(7, 1), (uint8)((MT6189_ReadCache(CW(7, 1)) & 0xFC) | 0x03));
/// CW25 [11:0] Target
MT6189_WriteByte(CW(25, 0), (uint8)(Target&0xFF));
MT6189_WriteByte(CW(25, 1), (uint8)(Target>>8));
/// CW6 [1:0] 00
MT6189_WriteByte(CW(6, 0), (uint8)(MT6189_ReadCache(CW(6, 0)) & 0xFC));
/// CW1 [7:0] 00
MT6189_WriteByte(CW(1, 0), 0x00);
TmpReg = MT6189_ReadCache(CW(3, 0)) & 0xE3; //start Shorten locking time loop
TmpReg |= 0x1C;
MT6189_WriteByte(CW(3, 0), TmpReg);
/// CW5 [4:4]
TmpReg = MT6189_ReadCache(CW(5, 0)) & 0xEF;
#if defined REF_CLK_32K
TmpReg |= 0x10;
#endif
MT6189_WriteByte(CW(5, 0), TmpReg); /// CW5 [4:4]
/// CW6 [15:8] 1
MT6189_WriteByte(CW(6, 1), 0x01);
/// Make sure that the counting is done
do {
if ((WaitingCounter >> 8) == 10) {
return false;
}
if (((WaitingCounter++) & 0x00FF) == 0) {
MT6189_ReadByte(CW(1, 0), &TmpReg);
}
} while ((TmpReg & 0x01) == 0);
/// Reset CW1_0
MT6189_WriteByte(CW(1, 0), 0x00);
/// CW2 [13:8] Ns
MT6189_WriteByte(CW(2, 1), (uint8)((MT6189_ReadCache(CW(2, 1)) & 0xC0) | (uint8)Ns));
/// CW3 [10:10] 0
MT6189_WriteByte(CW(3, 1), (uint8)(MT6189_ReadCache(CW(3, 1)) & 0xFB));
MT6189_WriteByte(CW( 6, 1), (uint8)((MT6189_ReadCache(CW( 6, 1)) & 0x0F) | 0x10));
MT6189_WriteByte(CW( 7, 1), 0);
MT6189_ReadByte(CW(96, 0), &TmpReg);
if (TmpReg < 64)
MT6189_WriteByte(CW(96, 0), (uint8)(TmpReg+1));
MT6189_ReadByte(CW(14, 0), &TmpReg); //Open S_auto in anymode
TmpReg&=0x6F;
MT6189_WriteByte(CW(14, 0),(uint8)(TmpReg) | 0x80);
/// delay until VCO being stablized
kal_sleep_task( VCO_LOCKING_TIME );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -