source: svn/trunk/zas_dstar/devices/Tuner/Oren_Cascade2B/ThomsonT2000Tuner.c @ 2

Last change on this file since 2 was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
File size: 7.4 KB
Line 
1#include "ThomsonT2000Tuner.h"
2#include "IicComm.h"
3#if NIMTN_TYPE==NIMTN_T2000
4BOOLEAN TunerThomsonT2000BeforeSendBytes(void);
5BOOLEAN TunerThomsonT2000AfterSendBytes(void);
6
7int TunerThomsonT2000SendCenterFreq(unsigned long ulCenterFrequency,unsigned int uiAgcSelection,unsigned int uiInput,unsigned long ulRefDividerRatio,unsigned int uiMode)
8{
9        unsigned char ucaSendBytes[4];
10        unsigned short usN;
11        unsigned long ulOscilatorFrequency,ulN;
12    long lWait;
13        ulN = ((ulCenterFrequency + THOMSON_T2000_F_IF) *1000)/ulRefDividerRatio;
14        if(ulN > 32768){ 
15                //max n in 14 bits
16                usN = (unsigned short)(ulN/2 +(double)0.5);
17        }
18        else{
19                //round N to the closest integer
20                usN = (unsigned short)(ulN + (double)0.5);
21        }
22       
23       
24        ucaSendBytes[1] = usN & 0x00FF;
25    ucaSendBytes[0] = (usN & 0x07f00) >> 8;
26
27        if(166600 == ulRefDividerRatio)
28                ucaSendBytes[2] = 0x13;
29        else if(62500 == ulRefDividerRatio)
30                ucaSendBytes[2] = 0x5;
31        else if(50000 == ulRefDividerRatio)
32                ucaSendBytes[2] = 0xd;
33        else
34                return THOMSON_T2000_UNDEFINED_REFERENCE_DIVISION_RATIO;
35
36
37        //set c0,c1
38        ulOscilatorFrequency = ulCenterFrequency + THOMSON_T2000_F_IF;
39        if((ulOscilatorFrequency >=97000)&&(ulOscilatorFrequency < 180000))
40                ucaSendBytes[2] |= 0;
41        else if((ulOscilatorFrequency >=180000)&&(ulOscilatorFrequency < 195000))
42                ucaSendBytes[2] |= 0x20;
43        else if((ulOscilatorFrequency >=195000)&&(ulOscilatorFrequency < 201000))
44                ucaSendBytes[2] |= 0x40;
45        else if((ulOscilatorFrequency >=201000)&&(ulOscilatorFrequency < 345000))
46                ucaSendBytes[2] |= 0;
47        else if((ulOscilatorFrequency >=345000)&&(ulOscilatorFrequency < 420000))
48                ucaSendBytes[2] |= 0x20;
49        else if((ulOscilatorFrequency >=420000)&&(ulOscilatorFrequency < 460000))
50                ucaSendBytes[2] |= 0x40;
51        else if((ulOscilatorFrequency >=460000)&&(ulOscilatorFrequency < 472000))
52                ucaSendBytes[2] |= 0x60;
53        else if((ulOscilatorFrequency >=472000)&&(ulOscilatorFrequency < 745000))
54                ucaSendBytes[2] |= 0x20;
55        else if((ulOscilatorFrequency >=745000)&&(ulOscilatorFrequency < 820000))
56                ucaSendBytes[2] |= 0x40;
57        else if((ulOscilatorFrequency >=820000)&&(ulOscilatorFrequency < 902000))
58                ucaSendBytes[2] |= 0x60;
59
60        ucaSendBytes[2] |= 0x80;
61
62
63        //set control byte 2
64        if(0 == uiInput)//terrestrial
65                ucaSendBytes[3] = 0;
66        else if(1 == uiInput)//cable
67                ucaSendBytes[3] = 1;
68        else
69                return THOMSON_T2000_UNDEFINED_INPUT;
70
71        if(1 == uiAgcSelection)//external agc
72                ucaSendBytes[3] |= 0x2;
73        else if(0 != uiAgcSelection)//internal agc
74                return THOMSON_T2000_UNDEFINED_AGC_SELECTION;
75
76        if((ulCenterFrequency >= THOMSON_T2000_LOW_BAND_MIN)&&( ulCenterFrequency<= THOMSON_T2000_LOW_BAND_MAX))//VHF low
77                ucaSendBytes[3] |= 0xc;
78        else if((ulCenterFrequency >= THOMSON_T2000_MID_BAND_MIN)&&(ulCenterFrequency <= THOMSON_T2000_MID_BAND_MAX))//VHF high
79                ucaSendBytes[3] |= 0x28;
80        else if((ulCenterFrequency >= THOMSON_T2000_HIGH_BAND_MIN)&&(ulCenterFrequency <= THOMSON_T2000_HIGH_BAND_MAX))///UHF
81                ucaSendBytes[3] |= 0x44;
82
83        if(FALSE == TunerThomsonT2000BeforeSendBytes())
84                return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
85
86        if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 4)) {
87                return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
88        }
89
90        //make a small delay
91        lWait = 100000;
92        while(lWait)
93                lWait--;
94       
95        //Send data byte if internal analog AGC mode.
96        if(1 == uiMode){
97                ucaSendBytes[0] = 0x00;
98                ucaSendBytes[1] = 0xD4; 
99                if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) {
100                        return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
101                }
102
103                ucaSendBytes[0] = 0x01;
104                ucaSendBytes[1] = 0x30; 
105                if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) {
106                        return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
107                }
108
109                ucaSendBytes[0] = 0x02;
110                ucaSendBytes[1] = 0x04; 
111                if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) {
112                        return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
113                }
114        }
115
116        //Send data byte if internal digital AGC mode.
117        else if(0 == uiMode){
118                ucaSendBytes[0] = 0x00;
119                ucaSendBytes[1] = 0xD4; 
120                if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) {
121                        return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
122                }
123
124                ucaSendBytes[0] = 0x01;
125                ucaSendBytes[1] = 0x30; 
126
127                if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) {
128                        return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
129                }
130
131                ucaSendBytes[0] = 0x02;
132                ucaSendBytes[1] = 0x24; 
133                if (IicTransmit(T2000_DEVICE_ADDRESS_WRITE, ucaSendBytes, 2)) {
134                        return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
135                }
136        }
137        else{
138                return THOMSON_T2000_UNDEFINED_MODE;
139        }
140
141        if(FALSE == TunerThomsonT2000AfterSendBytes())
142                return THOMSON_T2000_SEND_CONTROL_DATA_FAILED;
143
144        return THOMSON_T2000_SEND_CONTROL_DATA_SUCCESS;
145
146}
147
148unsigned long TunerThomsonT2000GetFrequencyFromChannel(unsigned int uiChannelNumber,unsigned int uiInput)
149{
150        unsigned long ulCenterFrequency = 0;
151        if(0 ==  uiInput){//terrestrial
152                if ((uiChannelNumber >= 2) && (uiChannelNumber <= 6)) {
153                        switch (uiChannelNumber) {
154                                case 2:
155                                        ulCenterFrequency = 57;
156                                        break;
157                                case 3:
158                                        ulCenterFrequency = 63;
159                                        break;
160                                case 4:
161                                        ulCenterFrequency = 69;
162                                        break;
163                                case 5:
164                                        ulCenterFrequency = 79;
165                                        break;
166                                case 6:
167                                        ulCenterFrequency = 85;
168                                        break;
169                        }
170                }
171                else if ((uiChannelNumber >= 7) && (uiChannelNumber <= 13)) {
172                        ulCenterFrequency = 174 + ((uiChannelNumber - 7) * 6) + 3; 
173                }
174                else if ((uiChannelNumber >= 14) && (uiChannelNumber <= 69)) {
175                        ulCenterFrequency = 470 + ((uiChannelNumber - 14) * 6) + 3; 
176                }
177        }       
178        else if(1 ==  uiInput){//CATV
179                if ((uiChannelNumber >= 1) && (uiChannelNumber <= 6)) {
180                        switch (uiChannelNumber) {
181                                case 1:
182                                        ulCenterFrequency = 75;
183                                        break;
184                                case 2:
185                                        ulCenterFrequency = 57;
186                                        break;
187                                case 3:
188                                        ulCenterFrequency = 63;
189                                        break;
190                                case 4:
191                                        ulCenterFrequency = 69;
192                                        break;
193                                case 5:
194                                        ulCenterFrequency = 79;
195                                        break;
196                                case 6:
197                                        ulCenterFrequency = 85;
198                                        break;
199                        }
200                }
201                else if ((uiChannelNumber >= 95) && (uiChannelNumber <= 99)) {
202                        ulCenterFrequency = 90 + ((uiChannelNumber - 95) * 6) + 3; 
203                }
204                else if ((uiChannelNumber >= 7) && (uiChannelNumber <= 13)) {
205                        ulCenterFrequency = 174 + ((uiChannelNumber - 7) * 6) + 3; 
206                }
207                else if ((uiChannelNumber >= 14) && (uiChannelNumber <= 22)) {
208                        ulCenterFrequency = 120 + ((uiChannelNumber - 14) * 6) + 3; 
209                }
210                else if ((uiChannelNumber >= 23) && (uiChannelNumber <= 94)) {
211                        ulCenterFrequency = 216 + ((uiChannelNumber - 23) * 6) + 3; 
212                }
213                else if ((uiChannelNumber >= 100) && (uiChannelNumber <= 135)) {
214                        ulCenterFrequency = 648 + ((uiChannelNumber - 100) * 6) + 3; 
215                }
216        }
217
218        return ulCenterFrequency*1000;
219
220}
221
222int TunerThomsonT2000SendChannel(unsigned int uiChannelNumber,unsigned int uiAgcSelection,unsigned int uiInput,unsigned long ulRefDividerRatio,unsigned int uiMode)
223{
224        unsigned long ulCenterFrequency = TunerThomsonT2000GetFrequencyFromChannel(uiChannelNumber,uiInput);
225        if(0 == ulCenterFrequency)
226                return THOMSON_T2000_CHANNEL_DOES_NOT_FIT_ANY_FREQUENCY;
227       
228        return TunerThomsonT2000SendCenterFreq( ulCenterFrequency,uiAgcSelection,uiInput,ulRefDividerRatio,uiMode);
229}
230
231int TunerThomsonT2000SendPicCarrier(unsigned long ulPicCarrier,unsigned int uiAgcSelection,unsigned int uiInput,unsigned long ulRefDividerRatio,unsigned int uiMode)
232{
233        return TunerThomsonT2000SendCenterFreq( ulPicCarrier + 1750,uiAgcSelection,uiInput,ulRefDividerRatio,uiMode);
234
235}
236
237BOOLEAN TunerThomsonT2000BeforeSendBytes(void)
238{
239
240        if(IIC_COM_SUCCESS != SendHostControl(0, 0))
241                return FALSE;
242        else
243                return TRUE;
244}
245
246BOOLEAN TunerThomsonT2000AfterSendBytes(void)
247{
248        if(IIC_COM_SUCCESS != SendHostControl(0x80, 0))
249                return FALSE;
250        else
251                return TRUE;
252}
253
254#endif//#if NIMTN_TYPE==NIMTN_T2000
Note: See TracBrowser for help on using the repository browser.