Skip to content

Commit 9dfaafa

Browse files
committed
No exceptions thrown
1 parent 915f94b commit 9dfaafa

File tree

4 files changed

+142
-107
lines changed

4 files changed

+142
-107
lines changed

drivers/src/main/java/info/martinmarinov/drivers/usb/cxusb/MygicaT230.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class MygicaT230 extends CxUsbDvbDevice {
3737
private final static String MYGICA_NAME = "Mygica T230 DVB-T/T2/C";
3838
final static DeviceFilter MYGICA_T230 = new DeviceFilter(DvbUsbIds.USB_VID_CONEXANT, DvbUsbIds.USB_PID_MYGICA_T230, MYGICA_NAME);
3939

40-
private DvbTuner tuner;
40+
private Si2168 frontend;
4141

4242
MygicaT230(UsbDevice usbDevice, Context context) throws DvbException {
4343
super(usbDevice, context, MYGICA_T230);
@@ -70,12 +70,12 @@ protected void readConfig() throws DvbException {
7070

7171
@Override
7272
protected DvbFrontend frontendAttatch() throws DvbException {
73-
return new Si2168(resources, i2CAdapter, 0x64, SI2168_TS_PARALLEL, true);
73+
return frontend = new Si2168(resources, i2CAdapter, 0x64, SI2168_TS_PARALLEL, true);
7474
}
7575

7676
@Override
7777
protected DvbTuner tunerAttatch() throws DvbException {
78-
return tuner = new Si2157(resources, i2CAdapter, 0x60, true, SI2157_CHIPTYPE_SI2157);
78+
return new Si2157(resources, i2CAdapter, frontend.gateControl(), 0x60, true, SI2157_CHIPTYPE_SI2157);
7979
}
8080

8181
@Override

drivers/src/main/java/info/martinmarinov/drivers/usb/cxusb/Si2157.java

Lines changed: 124 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import info.martinmarinov.drivers.DvbException;
3232
import info.martinmarinov.drivers.R;
3333
import info.martinmarinov.drivers.tools.I2cAdapter;
34+
import info.martinmarinov.drivers.tools.ThrowingRunnable;
3435
import info.martinmarinov.drivers.usb.DvbTuner;
3536

3637
import static info.martinmarinov.drivers.DvbException.ErrorCode.BAD_API_USAGE;
@@ -59,16 +60,18 @@ enum Type {
5960

6061
private final Resources resources;
6162
private final I2cAdapter i2c;
63+
private final I2cAdapter.I2GateControl i2GateControl;
6264
private final int addr;
6365
private final boolean if_port;
6466
private final Type chiptype;
6567

6668
private int if_frequency;
6769
private boolean active = false;
6870

69-
Si2157(Resources resources, I2cAdapter i2c, int addr, boolean if_port, Type chiptype) {
71+
Si2157(Resources resources, I2cAdapter i2c, I2cAdapter.I2GateControl i2GateControl, int addr, boolean if_port, Type chiptype) {
7072
this.resources = resources;
7173
this.i2c = i2c;
74+
this.i2GateControl = i2GateControl;
7275
this.addr = addr;
7376
this.if_port = if_port;
7477
this.chiptype = chiptype;
@@ -107,14 +110,24 @@ enum Type {
107110
public void attatch() throws DvbException {
108111
if_frequency = 5_000_000; /* default value of property 0x0706 */
109112

110-
/* check if the tuner is there */
111-
si2157_cmd_execute(new byte[0], 0, 1);
113+
i2GateControl.runInOpenGate(new ThrowingRunnable<DvbException>() {
114+
@Override
115+
public void run() throws DvbException {
116+
/* check if the tuner is there */
117+
si2157_cmd_execute(new byte[0], 0, 1);
118+
}
119+
});
112120
}
113121

114122
@Override
115123
public void release() {
116124
try {
117-
si2157_cmd_execute(new byte[] {(byte) 0x16, (byte) 0x00}, 2, 1);
125+
i2GateControl.runInOpenGate(new ThrowingRunnable<DvbException>() {
126+
@Override
127+
public void run() throws DvbException {
128+
si2157_cmd_execute(new byte[]{(byte) 0x16, (byte) 0x00}, 2, 1);
129+
}
130+
});
118131
} catch (DvbException e) {
119132
e.printStackTrace();
120133
}
@@ -123,51 +136,55 @@ public void release() {
123136

124137
@Override
125138
public void init() throws DvbException {
126-
/* Returned IF frequency is garbage when firmware is not running */
127-
byte[] res = si2157_cmd_execute(new byte[] {(byte) 0x15, (byte) 0x00, (byte) 0x06, (byte) 0x07}, 4, 4);
128-
129-
int if_freq_khz = (res[2] & 0xFF) | ((res[3] & 0xFF) << 8);
130-
Log.d(TAG, "IF frequency KHz "+if_freq_khz);
131-
132-
if (if_freq_khz != if_frequency / 1000) {
133-
/* power up */
134-
if (chiptype == SI2157_CHIPTYPE_SI2146) {
135-
si2157_cmd_execute(new byte[] {(byte) 0xc0, (byte) 0x05, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x00, (byte) 0x01}, 9, 1);
136-
} else {
137-
si2157_cmd_execute(new byte[] {(byte) 0xc0, (byte) 0x00, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x01}, 15, 1);
138-
}
139-
140-
/* query chip revision */
141-
res = si2157_cmd_execute(new byte[] {(byte) 0x02}, 1, 13);
142-
143-
int chip_id = ((res[1] & 0xFF) << 24) | ((res[2] & 0xFF) << 16) | ((res[3] & 0xFF) << 8) | (res[4] & 0xFF);
144-
145-
Log.d(TAG, "found a Silicon Labs Si21"+(res[2] & 0xFF)+"-"+((char) (res[1] & 0xFF))+" "+((char) (res[3] & 0xFF))+" "+((char) (res[4] & 0xFF)));
146-
147-
switch (chip_id) {
148-
case SI2158_A20:
149-
case SI2148_A20:
150-
loadFirmware(R.raw.dvbtunersi2158a2001fw);
151-
break;
152-
case SI2157_A30:
153-
case SI2147_A30:
154-
case SI2146_A10:
155-
Log.d(TAG, "No need to load fw");
156-
break;
157-
default:
158-
throw new DvbException(DVB_DEVICE_UNSUPPORTED, resources.getString(R.string.unsupported_tuner_on_device));
139+
i2GateControl.runInOpenGate(new ThrowingRunnable<DvbException>() {
140+
@Override
141+
public void run() throws DvbException {
142+
/* Returned IF frequency is garbage when firmware is not running */
143+
byte[] res = si2157_cmd_execute(new byte[]{(byte) 0x15, (byte) 0x00, (byte) 0x06, (byte) 0x07}, 4, 4);
144+
145+
int if_freq_khz = (res[2] & 0xFF) | ((res[3] & 0xFF) << 8);
146+
Log.d(TAG, "IF frequency KHz " + if_freq_khz);
147+
148+
if (if_freq_khz != if_frequency / 1000) {
149+
/* power up */
150+
if (chiptype == SI2157_CHIPTYPE_SI2146) {
151+
si2157_cmd_execute(new byte[]{(byte) 0xc0, (byte) 0x05, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x0b, (byte) 0x00, (byte) 0x00, (byte) 0x01}, 9, 1);
152+
} else {
153+
si2157_cmd_execute(new byte[]{(byte) 0xc0, (byte) 0x00, (byte) 0x0c, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x01}, 15, 1);
154+
}
155+
156+
/* query chip revision */
157+
res = si2157_cmd_execute(new byte[]{(byte) 0x02}, 1, 13);
158+
159+
int chip_id = ((res[1] & 0xFF) << 24) | ((res[2] & 0xFF) << 16) | ((res[3] & 0xFF) << 8) | (res[4] & 0xFF);
160+
161+
Log.d(TAG, "found a Silicon Labs Si21" + (res[2] & 0xFF) + "-" + ((char) (res[1] & 0xFF)) + " " + ((char) (res[3] & 0xFF)) + " " + ((char) (res[4] & 0xFF)));
162+
163+
switch (chip_id) {
164+
case SI2158_A20:
165+
case SI2148_A20:
166+
loadFirmware(R.raw.dvbtunersi2158a2001fw);
167+
break;
168+
case SI2157_A30:
169+
case SI2147_A30:
170+
case SI2146_A10:
171+
Log.d(TAG, "No need to load fw");
172+
break;
173+
default:
174+
throw new DvbException(DVB_DEVICE_UNSUPPORTED, resources.getString(R.string.unsupported_tuner_on_device));
175+
}
176+
177+
/* reboot the tuner with new firmware? */
178+
si2157_cmd_execute(new byte[]{(byte) 0x01, (byte) 0x01}, 2, 1);
179+
180+
/* query firmware version */
181+
res = si2157_cmd_execute(new byte[]{(byte) 0x11}, 1, 10);
182+
183+
Log.d(TAG, "firmware version: " + ((char) (res[6] & 0xFF)) + "." + ((char) (res[7] & 0xFF)) + "." + (res[8] & 0xFF));
184+
}
185+
active = true;
159186
}
160-
161-
/* reboot the tuner with new firmware? */
162-
si2157_cmd_execute(new byte[] {(byte) 0x01, (byte) 0x01}, 2, 1);
163-
164-
/* query firmware version */
165-
166-
res = si2157_cmd_execute(new byte[] {(byte) 0x11}, 1, 10);
167-
168-
Log.d(TAG, "firmware version: "+((char) (res[6] & 0xFF))+"."+((char) (res[7] & 0xFF))+"."+(res[8] & 0xFF));
169-
}
170-
active = true;
187+
});
171188
}
172189

173190
private void loadFirmware(int firmware) throws DvbException {
@@ -211,65 +228,70 @@ private byte[] readFirmware(int resource) throws IOException {
211228
}
212229

213230
@Override
214-
public void setParams(long frequency, long bandwidthHz, DeliverySystem deliverySystem) throws DvbException {
215-
if (!active) {
216-
throw new DvbException(BAD_API_USAGE, resources.getString(R.string.bad_api_usage));
217-
}
218-
219-
int bandwidth;
220-
if (bandwidthHz <= 6_000_000L) {
221-
bandwidth = 0x06;
222-
} else if (bandwidthHz <= 7_000_000L) {
223-
bandwidth = 0x07;
224-
} else if (bandwidthHz <= 8_000_000L) {
225-
bandwidth = 0x08;
226-
} else {
227-
bandwidth = 0x0f;
228-
}
231+
public void setParams(final long frequency, final long bandwidthHz, final DeliverySystem deliverySystem) throws DvbException {
232+
i2GateControl.runInOpenGate(new ThrowingRunnable<DvbException>() {
233+
@Override
234+
public void run() throws DvbException {
235+
if (!active) {
236+
throw new DvbException(BAD_API_USAGE, resources.getString(R.string.bad_api_usage));
237+
}
229238

230-
int delivery_system;
231-
int if_frequency;
232-
switch (deliverySystem) {
233-
case DVBT:
234-
case DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
235-
delivery_system = 0x20;
236-
if_frequency = 5_000_000;
237-
break;
238-
case DVBC:
239-
delivery_system = 0x30;
240-
if_frequency = 5_000_000;
241-
break;
242-
default:
243-
throw new DvbException(CANNOT_TUNE_TO_FREQ, resources.getString(R.string.unsupported_delivery_system));
244-
}
239+
int bandwidth;
240+
if (bandwidthHz <= 6_000_000L) {
241+
bandwidth = 0x06;
242+
} else if (bandwidthHz <= 7_000_000L) {
243+
bandwidth = 0x07;
244+
} else if (bandwidthHz <= 8_000_000L) {
245+
bandwidth = 0x08;
246+
} else {
247+
bandwidth = 0x0f;
248+
}
245249

246-
//noinspection ConstantConditions
247-
si2157_cmd_execute(
248-
new byte[] {
249-
(byte) 0x15, (byte) 0x00, (byte) 0x03, (byte) 0x07, (byte) (delivery_system | bandwidth), (byte) (INVERSION ? 1 : 0)
250-
}, 6, 4
251-
);
250+
int delivery_system;
251+
int if_frequency;
252+
switch (deliverySystem) {
253+
case DVBT:
254+
case DVBT2: /* it seems DVB-T and DVB-T2 both are 0x20 here */
255+
delivery_system = 0x20;
256+
if_frequency = 5_000_000;
257+
break;
258+
case DVBC:
259+
delivery_system = 0x30;
260+
if_frequency = 5_000_000;
261+
break;
262+
default:
263+
throw new DvbException(CANNOT_TUNE_TO_FREQ, resources.getString(R.string.unsupported_delivery_system));
264+
}
252265

253-
if (chiptype == SI2157_CHIPTYPE_SI2146) {
254-
si2157_cmd_execute(new byte[] {(byte) 0x14, (byte) 0x00, (byte) 0x02, (byte) 0x07, (byte) (if_port ? 1 : 0), (byte) 0x01}, 6, 4);
255-
} else {
256-
si2157_cmd_execute(new byte[] {(byte) 0x14, (byte) 0x00, (byte) 0x02, (byte) 0x07, (byte) (if_port ? 1 : 0), (byte) 0x00}, 6, 4);
257-
}
266+
//noinspection ConstantConditions
267+
si2157_cmd_execute(
268+
new byte[]{
269+
(byte) 0x15, (byte) 0x00, (byte) 0x03, (byte) 0x07, (byte) (delivery_system | bandwidth), (byte) (INVERSION ? 1 : 0)
270+
}, 6, 4
271+
);
272+
273+
if (chiptype == SI2157_CHIPTYPE_SI2146) {
274+
si2157_cmd_execute(new byte[]{(byte) 0x14, (byte) 0x00, (byte) 0x02, (byte) 0x07, (byte) (if_port ? 1 : 0), (byte) 0x01}, 6, 4);
275+
} else {
276+
si2157_cmd_execute(new byte[]{(byte) 0x14, (byte) 0x00, (byte) 0x02, (byte) 0x07, (byte) (if_port ? 1 : 0), (byte) 0x00}, 6, 4);
277+
}
258278

259-
/* set if frequency if needed */
260-
if (if_frequency != this.if_frequency) {
261-
si2157_cmd_execute(new byte[] {(byte) 0x14, (byte) 0x00, (byte) 0x06, (byte) 0x07, (byte) ((if_frequency / 1000) & 0xff), (byte) (((if_frequency / 1000) >> 8) & 0xff)}, 6, 4);
262-
this.if_frequency = if_frequency;
263-
}
279+
/* set if frequency if needed */
280+
if (if_frequency != Si2157.this.if_frequency) {
281+
si2157_cmd_execute(new byte[]{(byte) 0x14, (byte) 0x00, (byte) 0x06, (byte) 0x07, (byte) ((if_frequency / 1000) & 0xff), (byte) (((if_frequency / 1000) >> 8) & 0xff)}, 6, 4);
282+
Si2157.this.if_frequency = if_frequency;
283+
}
264284

265-
/* set frequency */
266-
si2157_cmd_execute(new byte[] {
267-
(byte) 0x41, (byte) 0x00, (byte) 0x00, (byte) 0x00,
268-
(byte) (frequency & 0xff),
269-
(byte) ((frequency >> 8) & 0xff),
270-
(byte) ((frequency >> 16) & 0xff),
271-
(byte) ((frequency >> 24) & 0xff)
272-
}, 8, 41);
285+
/* set frequency */
286+
si2157_cmd_execute(new byte[]{
287+
(byte) 0x41, (byte) 0x00, (byte) 0x00, (byte) 0x00,
288+
(byte) (frequency & 0xff),
289+
(byte) ((frequency >> 8) & 0xff),
290+
(byte) ((frequency >> 16) & 0xff),
291+
(byte) ((frequency >> 24) & 0xff)
292+
}, 8, 41);
293+
}
294+
});
273295
}
274296

275297
@Override

drivers/src/main/java/info/martinmarinov/drivers/usb/cxusb/Si2168.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
import static info.martinmarinov.drivers.DvbStatus.FE_HAS_VITERBI;
5353

5454
class Si2168 implements DvbFrontend {
55-
// TODO: si2168_select and si2168_deselect for i2c toggling?
56-
5755
private final static String TAG = Si2168.class.getSimpleName();
5856
private final static int TIMEOUT_MS = 70;
5957
private final static int NO_STREAM_ID_FILTER = 0xFFFFFFFF;
@@ -419,4 +417,19 @@ public void setPids(int... pids) throws DvbException {
419417
public void disablePidFilter() throws DvbException {
420418
// no-op
421419
}
420+
421+
I2cAdapter.I2GateControl gateControl() {
422+
return gateControl;
423+
}
424+
425+
private final I2cAdapter.I2GateControl gateControl = new I2cAdapter.I2GateControl() {
426+
@Override
427+
protected synchronized void i2cGateCtrl(boolean enable) throws DvbException {
428+
if (enable) {
429+
si2168_cmd_execute_wr(new byte[] {(byte) 0xc0, (byte) 0x0d, (byte) 0x01}, 3);
430+
} else {
431+
si2168_cmd_execute_wr(new byte[] {(byte) 0xc0, (byte) 0x0d, (byte) 0x00}, 3);
432+
}
433+
}
434+
};
422435
}
4.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)