#include "ThomsonT2000Tuner.h" #include "IicComm.h" #if NIMTN_TYPE==NIMTN_T2000 BOOLEAN TunerThomsonT2000BeforeSendBytes(void); BOOLEAN TunerThomsonT2000AfterSendBytes(void); int TunerThomsonT2000SendCenterFreq(unsigned long ulCenterFrequency,unsigned int uiAgcSelection,unsigned int uiInput,unsigned long ulRefDividerRatio,unsigned int uiMode) { unsigned char ucaSendBytes[4]; unsigned short usN; unsigned long ulOscilatorFrequency,ulN; long lWait; ulN = ((ulCenterFrequency + THOMSON_T2000_F_IF) *1000)/ulRefDividerRatio; if(ulN > 32768){ //max n in 14 bits usN = (unsigned short)(ulN/2 +(double)0.5); } else{ //round N to the closest integer usN = (unsigned short)(ulN + (double)0.5); } ucaSendBytes[1] = usN & 0x00FF; ucaSendBytes[0] = (usN & 0x07f00) >> 8; if(166600 == ulRefDividerRatio) ucaSendBytes[2] = 0x13; else if(62500 == ulRefDividerRatio) ucaSendBytes[2] = 0x5; else if(50000 == ulRefDividerRatio) ucaSendBytes[2] = 0xd; else return THOMSON_T2000_UNDEFINED_REFERENCE_DIVISION_RATIO; //set c0,c1 ulOscilatorFrequency = ulCenterFrequency + THOMSON_T2000_F_IF; if((ulOscilatorFrequency >=97000)&&(ulOscilatorFrequency < 180000)) ucaSendBytes[2] |= 0; else if((ulOscilatorFrequency >=180000)&&(ulOscilatorFrequency < 195000)) ucaSendBytes[2] |= 0x20; else if((ulOscilatorFrequency >=195000)&&(ulOscilatorFrequency < 201000)) ucaSendBytes[2] |= 0x40; else if((ulOscilatorFrequency >=201000)&&(ulOscilatorFrequency < 345000)) ucaSendBytes[2] |= 0; else if((ulOscilatorFrequency >=345000)&&(ulOscilatorFrequency < 420000)) ucaSendBytes[2] |= 0x20; else if((ulOscilatorFrequency >=420000)&&(ulOscilatorFrequency < 460000)) ucaSendBytes[2] |= 0x40; else if((ulOscilatorFrequency >=460000)&&(ulOscilatorFrequency < 472000)) ucaSendBytes[2] |= 0x60; else if((ulOscilatorFrequency >=472000)&&(ulOscilatorFrequency < 745000)) ucaSendBytes[2] |= 0x20; else if((ulOscilatorFrequency >=745000)&&(ulOscilatorFrequency < 820000)) ucaSendBytes[2] |= 0x40; else if((ulOscilatorFrequency >=820000)&&(ulOscilatorFrequency < 902000)) ucaSendBytes[2] |= 0x60; ucaSendBytes[2] |= 0x80; //set control byte 2 if(0 == uiInput)//terrestrial ucaSendBytes[3] = 0; else if(1 == uiInput)//cable ucaSendBytes[3] = 1; else return THOMSON_T2000_UNDEFINED_INPUT; if(1 == uiAgcSelection)//external agc ucaSendBytes[3] |= 0x2; else if(0 != uiAgcSelection)//internal agc return THOMSON_T2000_UNDEFINED_AGC_SELECTION; if((ulCenterFrequency >= THOMSON_T2000_LOW_BAND_MIN)&&( ulCenterFrequency<= THOMSON_T2000_LOW_BAND_MAX))//VHF low ucaSendBytes[3] |= 0xc; else if((ulCenterFrequency >= THOMSON_T2000_MID_BAND_MIN)&&(ulCenterFrequency <= THOMSON_T2000_MID_BAND_MAX))//VHF high ucaSendBytes[3] |= 0x28; else if((ulCenterFrequency >= THOMSON_T2000_HIGH_BAND_MIN)&&(ulCenterFrequency <= THOMSON_T2000_HIGH_BAND_MAX))///UHF ucaSendBytes[3] |= 0x44; if(FALSE == TunerThomsonT2000BeforeSendBytes()) return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 4)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } //make a small delay lWait = 100000; while(lWait) lWait--; //Send data byte if internal analog AGC mode. if(1 == uiMode){ ucaSendBytes[0] = 0x00; ucaSendBytes[1] = 0xD4; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } ucaSendBytes[0] = 0x01; ucaSendBytes[1] = 0x30; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } ucaSendBytes[0] = 0x02; ucaSendBytes[1] = 0x04; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } } //Send data byte if internal digital AGC mode. else if(0 == uiMode){ ucaSendBytes[0] = 0x00; ucaSendBytes[1] = 0xD4; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } ucaSendBytes[0] = 0x01; ucaSendBytes[1] = 0x30; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } ucaSendBytes[0] = 0x02; ucaSendBytes[1] = 0x24; if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) { return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; } } else{ return THOMSON_T2000_UNDEFINED_MODE; } if(FALSE == TunerThomsonT2000AfterSendBytes()) return THOMSON_T2000_SEND_CONTROL_DATA_FAILED; return THOMSON_T2000_SEND_CONTROL_DATA_SUCCESS; } unsigned long TunerThomsonT2000GetFrequencyFromChannel(unsigned int uiChannelNumber,unsigned int uiInput) { unsigned long ulCenterFrequency = 0; if(0 == uiInput){//terrestrial if ((uiChannelNumber >= 2) && (uiChannelNumber <= 6)) { switch (uiChannelNumber) { case 2: ulCenterFrequency = 57; break; case 3: ulCenterFrequency = 63; break; case 4: ulCenterFrequency = 69; break; case 5: ulCenterFrequency = 79; break; case 6: ulCenterFrequency = 85; break; } } else if ((uiChannelNumber >= 7) && (uiChannelNumber <= 13)) { ulCenterFrequency = 174 + ((uiChannelNumber - 7) * 6) + 3; } else if ((uiChannelNumber >= 14) && (uiChannelNumber <= 69)) { ulCenterFrequency = 470 + ((uiChannelNumber - 14) * 6) + 3; } } else if(1 == uiInput){//CATV if ((uiChannelNumber >= 1) && (uiChannelNumber <= 6)) { switch (uiChannelNumber) { case 1: ulCenterFrequency = 75; break; case 2: ulCenterFrequency = 57; break; case 3: ulCenterFrequency = 63; break; case 4: ulCenterFrequency = 69; break; case 5: ulCenterFrequency = 79; break; case 6: ulCenterFrequency = 85; break; } } else if ((uiChannelNumber >= 95) && (uiChannelNumber <= 99)) { ulCenterFrequency = 90 + ((uiChannelNumber - 95) * 6) + 3; } else if ((uiChannelNumber >= 7) && (uiChannelNumber <= 13)) { ulCenterFrequency = 174 + ((uiChannelNumber - 7) * 6) + 3; } else if ((uiChannelNumber >= 14) && (uiChannelNumber <= 22)) { ulCenterFrequency = 120 + ((uiChannelNumber - 14) * 6) + 3; } else if ((uiChannelNumber >= 23) && (uiChannelNumber <= 94)) { ulCenterFrequency = 216 + ((uiChannelNumber - 23) * 6) + 3; } else if ((uiChannelNumber >= 100) && (uiChannelNumber <= 135)) { ulCenterFrequency = 648 + ((uiChannelNumber - 100) * 6) + 3; } } return ulCenterFrequency*1000; } int TunerThomsonT2000SendChannel(unsigned int uiChannelNumber,unsigned int uiAgcSelection,unsigned int uiInput,unsigned long ulRefDividerRatio,unsigned int uiMode) { unsigned long ulCenterFrequency = TunerThomsonT2000GetFrequencyFromChannel(uiChannelNumber,uiInput); if(0 == ulCenterFrequency) return THOMSON_T2000_CHANNEL_DOES_NOT_FIT_ANY_FREQUENCY; return TunerThomsonT2000SendCenterFreq( ulCenterFrequency,uiAgcSelection,uiInput,ulRefDividerRatio,uiMode); } int TunerThomsonT2000SendPicCarrier(unsigned long ulPicCarrier,unsigned int uiAgcSelection,unsigned int uiInput,unsigned long ulRefDividerRatio,unsigned int uiMode) { return TunerThomsonT2000SendCenterFreq( ulPicCarrier + 1750,uiAgcSelection,uiInput,ulRefDividerRatio,uiMode); } BOOLEAN TunerThomsonT2000BeforeSendBytes(void) { if(IIC_COM_SUCCESS != SendHostControl(0, 0)) return FALSE; else return TRUE; } BOOLEAN TunerThomsonT2000AfterSendBytes(void) { if(IIC_COM_SUCCESS != SendHostControl(0x80, 0)) return FALSE; else return TRUE; } #endif//#if NIMTN_TYPE==NIMTN_T2000