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

📄 ns954lib.c

📁 NS954X tuner solution source code with RDS function
💻 C
📖 第 1 页 / 共 2 页
字号:
//=============================================================================
// NS954 Sample source code
//=============================================================================
#include "ns954lib.h"
#include "host.h"


#define val_REG_00h()  host_Read(0x00)
#define val_REG_04h()  host_Read(0x04)


#define BIT_FOSAT           0x04
#define BIT_STATION         0x02
#define STB_STATION_EN      STB_3
#define REG_DEEMP           0x0D
#define STB_DEEMP           STB_6


//-----------------------------------------------------
// AM 9KHz CLKSEL Table
//-----------------------------------------------------
XDATA StCLKSEL clkTable_am9k[CLKSEL_MAX]={
	{ 513,0x0},	{ 522,0x1},	{ 531,0xE},	{ 576,0xC}, { 621,0x2},	{ 630,0xE},	{ 639,0x3},	{ 666,0x1},
	{ 675,0xE},	{ 720,0x3},	{ 765,0x2},	{ 774,0x0},	{ 810,0x1},	{ 819,0xE},	{ 864,0x3},	{ 873,0x0},
	{ 909,0x2},	{ 918,0xF},	{ 954,0x1},	{ 963,0xE},	{1008,0x3},	{1053,0x2},	{1062,0xF},	{1098,0x1},
	{1107,0xE}, {1152,0x3},	{1197,0x2},	{1206,0xF},	{1242,0x1},	{1251,0xE},	{1278,0x3},	{1296,0x2},
	{1341,0x2},	{1350,0xF},	{1386,0x1},	{1395,0xE},	{1440,0x3},	{1485,0x2},	{1494,0xF},	{1530,0x1},
	{1539,0xE},	{1557,0x1},	{1584,0x3},	{1629,0x2},	{1638,0xF},	{1674,0x1},	{1683,0xD},	
	{0,0}
};

//-----------------------------------------------------
// AM-10KHz CLKSEL Table
//-----------------------------------------------------
XDATA StCLKSEL clkTable_am10k[CLKSEL_MAX]={
	{ 530,0xD},	{ 580,0xE},	{ 620,0x2},	{ 640,0x3}, { 670,0x2},	{ 720,0x3},	{ 760,0x2},	{ 770,0xE},
	{ 820,0xE}, { 860,0x2},	{ 910,0x2},	{ 960,0x3},	{1000,0x2},	{1010,0xE},	{1060,0xE},	{1100,0x2},
	{1150,0x2}, {1200,0x3},	{1240,0x2},	{1250,0xE},	{1300,0xE},	{1340,0x2},	{1390,0x2},	{1440,0x3},
	{1480,0x2},	{1490,0xE},	{1540,0xE},	{1580,0x2},	{1630,0x2},	{1680,0x3},	{1720,0x2},	{1730,0xE},
	{1780,0x3},
	{0,0}
};

//-----------------------------------------------------
// FM CLKSEL Table
//-----------------------------------------------------
XDATA StCLKSEL clkTable_fm[CLKSEL_MAX]={
	{ 7660,0x2}, { 7665,0x2}, { 7670,0x2}, { 7675,0x2},	{ 7680,0x2}, { 7685,0x2}, { 7690,0x2}, { 7695,0x2},
	{ 8440,0x2}, { 8445,0x2}, { 8450,0x2}, { 8455,0x2},	{ 9160,0x2}, { 9165,0x2}, { 9170,0x2}, { 9175,0x2},
	{ 9180,0x2}, { 9185,0x2}, { 9190,0x2}, { 9195,0x2},	{ 9200,0xE}, { 9205,0xE}, { 9210,0xE}, { 9215,0xE},
	{ 9220,0xE}, { 9225,0xE}, { 9230,0xE}, { 9235,0xE},	{ 9240,0xE}, { 9245,0xE}, { 9250,0xE}, { 9255,0xE},
	{ 9260,0xE}, { 9265,0xE}, { 9270,0xE}, { 9275,0xE},	{ 9940,0xE}, { 9945,0xE}, { 9950,0xE}, { 9955,0xE},
	{ 9960,0x2}, { 9965,0x2}, { 9970,0x2}, { 9975,0x2},	{ 9980,0x2}, { 9985,0x2}, { 9990,0x2}, { 9995,0x2},
	{10000,0xF}, {10005,0xF}, {10010,0xF}, {10015,0xF},	{10240,0x2}, {10245,0x2}, {10250,0x2}, {10255,0x2},
	{10260,0x2}, {10265,0x2}, {10270,0x2}, {10275,0x2},	{10490,0x1}, {10495,0x1}, {10720,0x2}, {10725,0x2},
	{10730,0x2}, {10735,0x2}, {10740,0x2}, {10745,0x2},	{10750,0x2}, {10755,0x2}, {10760,0x2}, {10765,0x2},
	{10770,0x2}, {10775,0x2},
	{0,0}
};


//=============================================================================
// Find CLKSEL value
//-----------------------------------------------------------------------------
// Parameter(s)
//  band : BAND_FM, BAND_AM9k, BAND_AM10k, BAND_OIRT, ...
//  freq : select frequency
//-----------------------------------------------------------------------------
// Return Value
//  value of Register (address 0x10)
//=============================================================================
u8 ns_find_clksel(eBAND band, u16 freq)
{
	int i,num;
	StCLKSEL* pSel=0;

	switch(band){
	case BAND_FM:      pSel=clkTable_fm;     num=numof(clkTable_fm);    break;
	case BAND_AM10k:   pSel=clkTable_am10k;  num=numof(clkTable_am10k); break;
	case BAND_AM9k:    pSel=clkTable_am9k;   num=numof(clkTable_am9k);  break;
	}

	if(pSel){
		for(i=0;i<num;i++){
			if(pSel[i].freq==0) {
				return 0x10;
			}
			if(pSel[i].freq==freq){
				return pSel[i].clksel;
			}
		}
	}
	return 0x10;
}

//=============================================================================
// Register calc function
//-----------------------------------------------------------------------------
// Parameter(s)
//  adr  : Register Address
//  sb   : Start bit 
//  msk  : mask value
//  dat  : data value
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void RegChange(u8 adr,STARTBIT sb,u8 msk,u8 dat)
{
    u8 reg = host_Read(adr);

	reg &= ~(msk<<sb);
	reg |= (dat&msk)<<sb;
	host_Write(adr,reg);
}

//=============================================================================
// Setup De-Emphasis Register
//-----------------------------------------------------------------------------
// Parameter(s)
//  b50us : 0x01 (50us), 0x00 (75us)
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_reg_deemp(u8 b50us)
{
	RegChange(REG_DEEMP,STB_DEEMP,1,(b50us)?1:0);
}

//=============================================================================
// Setup STATION_EN Register
//-----------------------------------------------------------------------------
// Parameter(s)
//  en = 0x01 (STATION_EN bit is 1), 0x00 (STATION_EN bit is 0)
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_reg_station_en(u8 en)
{
	RegChange(0x01,STB_STATION_EN,1,en?1:0);
}

//=============================================================================
// Set Band Register
//-----------------------------------------------------------------------------
// Parameter(s)
//  band = BAND_FM, BAND_AM, BAND_OIRT, ...
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
static void ns_reg_band(u8 band)
{
	RegChange(0x00,STB_4,7,(band&7));
}

//=============================================================================
// Setup Force Mono Register
//-----------------------------------------------------------------------------
// Parameter(s)
//  bMono = 0x01(Force Mono), 0x00(depend on Broadcasting)
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_reg_mono(u8 bMono)
{
	RegChange(0x00,STB_2,1,(bMono)?1:0);
}

//=============================================================================
// Set Mute Control Register
//-----------------------------------------------------------------------------
// Parameter(s)
//  bMute = 0x01(MUTE ON), 0x00(MUTE OFF)
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_reg_mute(u8 bMute)
{
	RegChange(0x00,STB_1,1,(bMute)?1:0);
}


//=============================================================================
// Power ON Register initialize
// This function is setup to default register value.
// Some register setup won't be complete.
// So, please setup some setup functions.
// For example. 
//   ns_reg_mono, ns_reg_deemp, ... and more
//-----------------------------------------------------------------------------
// Parameter(s)
//  none
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_power_on(void)
{
	host_Write(REG_SRST,0xAA);


	host_Write(0x01,0x30);
	host_Write(0x0C,0x80);
	host_Write(0x0E,0x34);
	host_Write(0x15,0xC4);
	host_Write(0x20,0x3C);
	host_Write(0x21,0x03);
	host_Write(0x22,0x0A);
	host_Write(0x23,0x0A);
	host_Write(0x30,0xFF); // PESY=1
	host_Write(0x3D,0x07);
	host_Write(0x40,0x1A);
	host_Write(0x41,0x9A);
	host_Write(0x50,0xE1);
	host_Write(0x55,0x36);
	host_Write(0x5C,0xE4);
	host_Write(0x5D,0x61);
	host_Write(0x5E,0x88);
	host_Write(0x5F,0xA5);
	host_Write(0x71,0x2C);
	host_Write(0x72,0x06);
	host_Write(0x00,0x03); // PE=1,MUTE=1
}

//=============================================================================
// Power OFF
//-----------------------------------------------------------------------------
// Parameter(s)
//  none
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_power_off(void)
{
	ns_reg_mute(1);
	host_wait_ms(20);
	host_Write(0x30,0xEF); // PESY=0
	RegChange(0x00,STB_0,1,0);
}

//=============================================================================
// Find CLKSEL and Setup for ns_freq
//-----------------------------------------------------------------------------
// Parameter(s)
//  band : BAND_FM, BAND_OIRT, BAND_AM, BAND_AM10k, BAND_AM9k, ...    
//  freq : frequency value (see 'ns_freq' function prototype)
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
static void ns_clksel_set(eBAND band,u16 freq)
{
	u8 r10h;
	r10h = ns_find_clksel(band,freq);
	host_Write(0x10,r10h);
}

//=============================================================================
// Frequency change function
// This function uses the expression converted into 16 bits operation. 
// 
//  FM      : PSY = freq         x4 / 200(KHz)
//  OIRT    : PSY =(freq+10(KHz))x4 / 120(KHz)
//  AM      : PSY = freq
//-----------------------------------------------------------------------------
// Parameter(s)
//  freq : FM or OIRT : 76.1MHz -> freq=7610
//         AM         : 999KHz  -> freq=999
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_freq(u16 freq)
{
	eBAND band = (val_REG_00h()>>4);
	u16 fr_syn=0;
	u16 psy,ofs=0;
	u8  reg_psy[2];

	// check AM9k or AM10k
	if(band==BAND_AM){
		if(freq%10) { 
			band = BAND_AM9k; 
		} 
		else if(val_REG_04h()&0x80){ 
			band = BAND_AM9k; 
		}
	}

	switch(band){
	case BAND_OIRT:    fr_syn=3; ofs=1; break;
	case BAND_FM  :    fr_syn=5; break;
	default:           psy=freq; break;
	}


	if(fr_syn) psy=(freq+ofs)/fr_syn; /* FM or OIRT */
	else       psy=freq;              /* AM or ...  */

	// Search and Set CLKSEL
	ns_clksel_set(band,freq);

	reg_psy[0] = (u8)psy;
	reg_psy[1] = (u8)(psy>>8);
	host_Write2(0x02,reg_psy,2);
}

//=============================================================================
// Frequency change function with MUTE control
//-----------------------------------------------------------------------------
// Parameter(s)
//  freq : FM or OIRT : 76.1MHz -> freq=7610
//         AM         : 999KHz  -> freq=999
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_freq_with_mute(u16 freq)
{
	ns_reg_mute(1);
	host_wait_ms(20);
	ns_freq(freq);
	ns_reg_mute(0);
}


//=============================================================================
// Change BAND
//-----------------------------------------------------------------------------
// Parameter(s)
//  band : BAND_FM, BAND_OIRT, BAND_AM, BAND_AM10k, BAND_AM9k, ... 
//-----------------------------------------------------------------------------
// Return Value
//  none
//=============================================================================
void ns_band(eBAND band)
{
	u8 reg_band = BAND_MASK(band);
	u8 reg_c=0x00; //AAUTO=0,AMFSEL=0

	// Set BAND Register
	ns_reg_band(reg_band);

	// Setup Another Register
	switch(band){
	case BAND_AM10k:
		host_Write(0x04,0x00);     //K9=0,AMCSEL=0
		reg_c=0x80;                //AAUTO=1,AMFSEL=0
		break;

	case BAND_AM9k:
		host_Write(0x04,0x80);     //K9=1,AMCSEL=0
		reg_c=0x80;                //AAUTO=1,AMFSEL=0
		break;
	}

	// Setup AAUTO & AMFSEL
	RegChange(0x0C,STB_0,0x8F,reg_c);

	// DSP Reset
	host_Write(REG_SRST,0x0A); 

}

//=============================================================================
// Seek Procedure
// (1) Setup Frequency Register
// (2) DSP Reset and waiting for Register stable.
// (3) Check Seeking Parameters
//-----------------------------------------------------------------------------
// Parameter(s)
//  freq : frequency value (see 'ns_freq' function prototype)
//-----------------------------------------------------------------------------
// Return Value
//  1 : seek checking state is COMPLETE!
//  0 : seek checking state is not complete.
//      Please frq changed and retry this function.
//=============================================================================
u8 ns_seek_check(u8 band,u16 frq)
{
	u8  i,nRssi,bFail=0;
	u8  state;
	ns_freq(frq);

	// Soft Reset
	host_Write(REG_SRST,0x0A); 
	host_wait_ms(DEF_WAIT_AFTER_SRST);

	//------------------------------------------------------------------
	// FM & OIRT (FILT_RSSI threshold & FOSAT)
	//------------------------------------------------------------------
	if(band<=BAND_OIRT){
		for(i=0;i<DEF_SEEK_CHECK_COUNT;i++){
			state = host_Read(0x05);
			if(state&BIT_FOSAT){
				bFail=1;
				break;
			}

			nRssi = host_Read(0x6c);
			if(nRssi&0x80 || nRssi<DEF_RSSI_THREASHOLD_FM){
				bFail=1; // Threshold Error
				break;
			}

			if((i+1)==DEF_SEEK_CHECK_COUNT){
				break;
			}
			host_wait_ms(DEF_WAIT_FM_INTERVAL);

⌨️ 快捷键说明

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