summaryrefslogtreecommitdiffstats
path: root/api/Xceive_XC4000.c
blob: db77f25e0b26eb2ff886e9fc2fda56110eec0b2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * @(#)Xceive_XC4000.cpp
 *
 * ==========================================================
 * Version: 2.1
 * Date:    2009.01.20
 * ==========================================================
 *
 * ==========================================================
 * History:
 *
 * Date         Author      Description
 * ----------------------------------------------------------
 *
 * 2008.07.02   Tom Lin     created
 * 2008.09.18   Tom Lin     improve performance
 * 2009.01.20   Tom Lin     fix clock stretch issue
 * ==========================================================
 *
 * Copyright 2008 Afatech, Inc. All rights reserved.
 *
 */

//#include <stdio.h> //for Linux
#include "type.h"
#include "error.h"
#include "user.h"
#include "register.h"
#include "standard.h"
#include "xc4000_control.h"
#include "i2c_driver.h"
#include "xc4000_firmwares.h"
#include "xc4000_channelmaps.h"
#include "xc4000_standards.h"
#include "xc4000_scodes.h"
#include "Xceive_XC4000_Script.h"


extern Demodulator* XC4000Handle;
extern Byte         XC4000Chip;


Dword XC4000_open (
	IN  Demodulator*	demodulator,
	IN  Byte			chip
) {
    Dword error = Error_NO_ERROR;

    XC4000Handle = demodulator;
    XC4000Chip = chip;

    /** Enable I2C clock dynamic speed */
    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83ED, 1);
    if (error) goto exit;

    /** xc4000 links TV standard to its configuration file */
    XC4000_Loading_TVStandards ();

    /** xc4000 hardware resets */
    error = xc_reset();
    if (error) goto exit;

    User_delay (demodulator, 100);

    /** xc4000 downloads firmware */
    error = xc_load_i2c_sequence (XC4000_firmware_SEQUENCE);
    if (error) goto exit;

    /** xc4000 initiates */
    error = xc_load_i2c_sequence (XC4000_INIT_SEQUENCE);
    if (error) goto exit;

    /** Disable I2C clock dynamic speed */
    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83ED, 0);
    if (error) goto exit;

exit:
	return (error);
}


Dword XC4000_set (
	IN  Demodulator*	demodulator,
	IN  Byte			chip,
    IN  Word			bandwidth,
    IN  Dword			frequency
) {
    Dword error = Error_NO_ERROR;
    Byte amplitude[4];

    amplitude[0] = 0, amplitude[1] = 0x10, amplitude[2] = 0, amplitude[3] = 0x87;

	XC4000Handle = demodulator;
	XC4000Chip = chip;

    /** Enable I2C clock dynamic speed */
    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83ED, 1);
    if (error) goto exit;

    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83E6, 0x28);
    if (error) goto exit;

    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83E7, 0x07);
    if (error) goto exit;

    if (bandwidth == 6000)
    {
        /** Tell f/w the stretched packet */
        error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83E3, 23);
        if (error) goto exit;

        /** xc4000 sets TV standard to DVBT-6 */
        error = xc_load_i2c_sequence (XC4000_Standard[23].ConfFile);
        if (error) goto exit;
    }
    else if (bandwidth == 7000)
    {
        /** Tell f/w the stretched packet */
        error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83E3, 22);
        if (error) goto exit;

        /** xc4000 sets TV standard to DVBT-7 */
        error = xc_load_i2c_sequence(XC4000_Standard[26].ConfFile);
        if (error) goto exit;
    }
    else if (bandwidth == 8000)
    {
        /** Tell f/w the stretched packet */
        error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83E3, 22);
        if (error) goto exit;

        /** xc4000 sets TV standard to DVBT-8 */
        error = xc_load_i2c_sequence(XC4000_Standard[24].ConfFile);
        if (error) goto exit;
    }
    else
    {
        error = Error_INVALID_BW;
        goto exit;
    }

    /** xc4000 enters direct-setting-mode */
    error = xc_enter_directsitting_mode ();
    if (error) goto exit;

    /** xc4000 downloads scode */
    error = xc_send_i2c_data (&XC4000_scode_table_4560000[0][0], 13);
    if (error) goto exit;

    /** xc4000 exits direct-setting-mode */
    error = xc_exit_directsitting_mode ();
    if (error) goto exit;

    /** xc4000 sets RF mode to Air(0) */
    error = xc_rf_mode(0);
    if (error) goto exit;

    /** xc4000 sets dcode */
    error = xc_set_dcode (0);
    if (error) goto exit;

    /** xc4000 sets RF frequency of Hz */
    if (bandwidth == 6000)
        frequency -= 1750;
    else if (bandwidth == 7000)
        frequency -= 2250;
    else if (bandwidth == 8000)
        frequency -= 2750;
    else
    {
        error = Error_INVALID_BW;
        goto exit;
    }

    error = xc_set_RF_frequency (frequency * 1000);
    if (error) goto exit;

    /** Tell f/w the stretched packet 0x83E3, 0x83E6, 0x83E7 */
    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83E3, 1);
    if (error) goto exit;

    /** Improve Performance by set reg 0x0010 to the value of 0x0087 */
    error = Standard_writeTunerRegisters (demodulator, chip, 0x0000, 4, amplitude);
    if (error) goto exit;

exit:
    /** Disable I2C clock dynamic speed */
    error = Standard_writeRegister (demodulator, 0, Processor_LINK, 0x83ED, 0);

    return (error);
}


Dword XC4000_close (
	IN  Demodulator*	demodulator,
	IN  Byte			chip
) {
	return (Error_NO_ERROR);
}


TunerDescription tuner_XC4000 = {
    XC4000_open,
    XC4000_close,
    XC4000_set,
    XC4000_scripts,
	XC4000_scriptSets,
    XC4000_ADDRESS,                  /** tuner i2c address */
    0,                               /** length of tuner register address */
    4560000,                         /** tuner if */
    True,                            /** spectrum inverse */
    0x2A,                            /** tuner id */
};