dib3000-watch.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Tool for watching the dib3000*-demodulators,
  3. * with an extended output.
  4. *
  5. * Copyright (C) 2005 by Patrick Boettcher <patrick.boettcher@desy.de>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
  22. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <errno.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <ctype.h>
  29. #include <getopt.h>
  30. #include <signal.h>
  31. #include <math.h>
  32. #include <fcntl.h>
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <sys/ioctl.h>
  36. #include <linux/types.h>
  37. #include "dib-i2c.h"
  38. #include "dib3000-watch.h"
  39. #include "dib3000.h"
  40. void usage (void)
  41. {
  42. verb("usage: dib3000-watch -d <i2c-device> -a <i2c-address> [-o <type>] [-i <seconds>]\n"
  43. " -d normally one of /dev/i2c-[0-255]\n"
  44. " -a is 8 for DiB3000M-B and 9, 10, 11 or 12 for DiB3000M-C or DiB3000-P\n"
  45. " -o output type (print|csv) (default: print)\n"
  46. " -i query interval in seconds (default: 0.1)\n"
  47. "\n"
  48. "Don't forget to run tzap or any other dvb-tune program (vdr, kaxtv) in order to tune a channel,\n"
  49. "tuning isn't done by this tool.\n"
  50. "\n"
  51. "A lot of thing have been taken for the dibusb, dib3000m[bc] driver from kernel and\n"
  52. "from t_demod-test software created by DiBcom. Both is GPL, so is dib-demod-watch.\n"
  53. "\n"
  54. "Copyright (C) 2005 by Patrick Boettcher <patrick.boettcher@desy.de>\n"
  55. "\n"
  56. "The source of this tool is released under the GPL.\n"
  57. );
  58. exit(1);
  59. }
  60. __u16 dib_read_reg(struct dib_demod *dib,__u16 reg)
  61. {
  62. int ret;
  63. __u8 wb[] = { ((reg >> 8) | 0x80) & 0xff, reg & 0xff };
  64. __u8 rb[2];
  65. struct i2c_msg msg[] = {
  66. { .addr = dib->i2c_addr, .flags = 0, .buf = wb, .len = 2 },
  67. { .addr = dib->i2c_addr, .flags = I2C_M_RD, .buf = rb, .len = 2 },
  68. };
  69. struct i2c_rdwr_ioctl_data i2c_data = {
  70. .msgs = msg,
  71. .nmsgs = 2,
  72. };
  73. if ((ret = ioctl(dib->fd,I2C_RDWR,&i2c_data)) != 2) {
  74. err("i2c_rdwr read failed. (%d)\n",ret);
  75. return 0;
  76. }
  77. return (rb[0] << 8)| rb[1];
  78. };
  79. int dib_write_reg(struct dib_demod *dib, __u16 reg, __u16 val)
  80. {
  81. int ret;
  82. __u8 b[] = {
  83. (reg >> 8) & 0xff, reg & 0xff,
  84. (val >> 8) & 0xff, val & 0xff,
  85. };
  86. struct i2c_msg msg[] = {
  87. { .addr = dib->i2c_addr, .flags = 0, .buf = b, .len = 4 }
  88. };
  89. struct i2c_rdwr_ioctl_data i2c_data = {
  90. .msgs = msg,
  91. .nmsgs = 1,
  92. };
  93. if ((ret = ioctl(dib->fd,I2C_RDWR,&i2c_data)) != 1) {
  94. err("i2c_rdwr write failed. (%d)\n",ret);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. int dib3000mb_monitoring(struct dib_demod *dib,struct dib3000mb_monitoring *m)
  100. {
  101. int dds_freq, p_dds_freq,
  102. n_agc_power = dib_read_reg(dib,DIB3000MB_REG_AGC_POWER),
  103. rf_power = dib_read_reg(dib,DIB3000MB_REG_RF_POWER),
  104. timing_offset;
  105. double ad_power_dB, minor_power;
  106. m->invspec = dib_read_reg(dib,DIB3000MB_REG_DDS_INV);
  107. m->nfft = dib_read_reg(dib,DIB3000MB_REG_TPS_FFT);
  108. m->agc_lock = dib_read_reg(dib,DIB3000MB_REG_AGC_LOCK);
  109. m->carrier_lock = dib_read_reg(dib,DIB3000MB_REG_CARRIER_LOCK);
  110. m->tps_lock = dib_read_reg(dib,DIB3000MB_REG_TPS_LOCK);
  111. m->vit_lock = dib_read_reg(dib,DIB3000MB_REG_VIT_LCK);
  112. m->ts_sync_lock = dib_read_reg(dib,DIB3000MB_REG_TS_SYNC_LOCK);
  113. m->ts_data_lock = dib_read_reg(dib,DIB3000MB_REG_TS_RS_LOCK);
  114. p_dds_freq = ((dib_read_reg(dib,DIB3000MB_REG_DDS_FREQ_MSB) & 0xff) << 8) |
  115. ((dib_read_reg(dib,DIB3000MB_REG_DDS_FREQ_LSB) & 0xff00) >> 8);
  116. dds_freq = ((dib_read_reg(dib,DIB3000MB_REG_DDS_VALUE_MSB) & 0xff) << 8) |
  117. ((dib_read_reg(dib,DIB3000MB_REG_DDS_VALUE_LSB) & 0xff00) >> 8);
  118. if (m->invspec)
  119. dds_freq = (1 << 16) - dds_freq;
  120. m->carrier_offset = (double)(dds_freq - p_dds_freq) / (double)(1 << 16) * DEF_SampFreq_KHz;
  121. m->ber = (double)((dib_read_reg(dib,DIB3000MB_REG_BER_MSB) << 16) | dib_read_reg(dib,DIB3000MB_REG_BER_LSB)) / (double) 1e8;
  122. m->per = dib_read_reg(dib,DIB3000MB_REG_PACKET_ERROR_RATE);
  123. m->unc = dib_read_reg(dib,DIB3000MB_REG_UNC);
  124. m->fft_pos = dib_read_reg(dib,DIB3000MB_REG_FFT_WINDOW_POS);
  125. m->snr = 10.0 * log10( (double)(dib_read_reg(dib,DIB3000MB_REG_SIGNAL_POWER) << 8) /
  126. (double)((dib_read_reg(dib,DIB3000MB_REG_NOISE_POWER_MSB) << 16) + dib_read_reg(dib,DIB3000MB_REG_NOISE_POWER_LSB)));
  127. m->mer = (double) ((dib_read_reg(dib,DIB3000MB_REG_MER_MSB) << 16) + dib_read_reg(dib,DIB3000MB_REG_MER_LSB))
  128. / (double) (1<<9) / (m->nfft ? 767.0 : 191.0);
  129. if (n_agc_power == 0)
  130. n_agc_power = 1;
  131. ad_power_dB = 10 * log10( (double)(n_agc_power) / (double)(1<<16));
  132. minor_power = ad_power_dB - DEF_agc_ref_dB ;
  133. m->rf_power = -DEF_gain_slope_dB * (double)rf_power/(double)(1<<16) + DEF_gain_delta_dB + minor_power;
  134. timing_offset =
  135. (dib_read_reg(dib,DIB3000MB_REG_TIMING_OFFSET_MSB) << 16) + dib_read_reg(dib,DIB3000MB_REG_TIMING_OFFSET_LSB);
  136. if (timing_offset >= 0x800000)
  137. timing_offset |= 0xff000000;
  138. m->timing_offset_ppm = -(double)timing_offset / (double)(m->nfft ? 8192 : 2048) * 1e6 / (double)(1<<20);
  139. return 0;
  140. }
  141. int dib3000mb_print_monitoring(struct dib3000mb_monitoring *m)
  142. {
  143. printf("DiB3000M-B status\n\n");
  144. printf(" AGC lock: %10d\n",m->agc_lock);
  145. printf(" carrier lock: %10d\n",m->carrier_lock);
  146. printf(" TPS synchronize lock: %10d\n",m->tps_lock);
  147. printf(" Viterbi lock: %10d\n",m->vit_lock);
  148. printf(" MPEG TS synchronize lock: %10d\n",m->ts_sync_lock);
  149. printf(" MPEG TS data lock: %10d\n",m->ts_data_lock);
  150. printf("\n\n");
  151. printf(" spectrum inversion: %10d\n",m->invspec);
  152. printf(" carrier offset: %3.7g\n",m->carrier_offset);
  153. printf("\n\n");
  154. printf(" bit error rate: %3.7g\n",m->ber);
  155. printf(" packet error rate: %10d\n",m->per);
  156. printf(" packet error count: %10d\n",m->unc);
  157. printf("\n\n");
  158. printf(" fft position: %10d\n",m->fft_pos);
  159. printf(" transmission mode: %10s\n",m->nfft ? "8k" : "2k");
  160. printf("\n\n");
  161. printf(" C / (N + I) = %3.7g\n",m->snr);
  162. printf(" MER = %3.7g dB\n",m->mer);
  163. printf(" RF power = %3.7g dBm\n",m->rf_power);
  164. printf(" timing offset = %3.7g ppm\n",m->timing_offset_ppm);
  165. return 0;
  166. }
  167. int interrupted;
  168. void sighandler (int sig)
  169. {
  170. (void)sig;
  171. interrupted = 1;
  172. }
  173. typedef enum {
  174. OUT_PRINT = 0,
  175. OUT_CSV,
  176. } dib3000m_output_t;
  177. int main (int argc, char * const argv[])
  178. {
  179. struct dib_demod dib;
  180. struct dib3000mb_monitoring mon;
  181. const char *dev = NULL;
  182. float intervall = 0.1;
  183. dib3000m_output_t out = OUT_PRINT;
  184. int c;
  185. while ((c = getopt(argc,argv,"d:a:o:i:")) != -1) {
  186. switch (c) {
  187. case 'd':
  188. dev = optarg;
  189. break;
  190. case 'a':
  191. dib.i2c_addr = atoi(optarg); /* The I2C address */
  192. break;
  193. case 'o':
  194. if (strcasecmp(optarg,"print") == 0) out = OUT_PRINT;
  195. else if (strcasecmp(optarg,"csv") == 0) out = OUT_CSV;
  196. else usage();
  197. break;
  198. case 'i':
  199. intervall = atof(optarg);
  200. break;
  201. default:
  202. usage();
  203. }
  204. }
  205. if (dev == NULL)
  206. usage();
  207. interrupted = 0;
  208. signal(SIGINT, sighandler);
  209. signal(SIGKILL, sighandler);
  210. signal(SIGHUP, sighandler);
  211. verb("will use '%s' as i2c-device and %d as i2c address.\n",dev,dib.i2c_addr);
  212. if ((dib.fd = open(dev,O_RDWR)) < 0) {
  213. err("could not open %s\n",dev);
  214. exit(1);
  215. }
  216. if (ioctl(dib.fd,I2C_SLAVE,dib.i2c_addr) < 0) {
  217. err("could not set i2c address\n");
  218. exit(1);
  219. }
  220. if (dib_read_reg(&dib,DIB3000_REG_MANUFACTOR_ID) != DIB3000_I2C_ID_DIBCOM) {
  221. err("could not find a dib3000 demodulator at i2c-address %d\n",dib.i2c_addr);
  222. exit(1);
  223. }
  224. switch (dib_read_reg(&dib,DIB3000_REG_DEVICE_ID)) {
  225. case DIB3000MB_DEVICE_ID:
  226. verb("found a DiB3000M-B demodulator.\n");
  227. dib.rev = DIB3000MB;
  228. break;
  229. case DIB3000MC_DEVICE_ID:
  230. verb("found a DiB3000M-C demodulator.\n");
  231. dib.rev = DIB3000MC;
  232. break;
  233. case DIB3000P_DEVICE_ID:
  234. verb("found a DiB3000-P demodulator.\n");
  235. dib.rev = DIB3000P;
  236. break;
  237. default:
  238. err("unsupported demodulator found.\n");
  239. }
  240. while (!interrupted) {
  241. switch (dib.rev) {
  242. case DIB3000MB:
  243. dib3000mb_monitoring(&dib,&mon);
  244. if (out == OUT_PRINT) {
  245. printf("\E[H\E[2J");
  246. dib3000mb_print_monitoring(&mon);
  247. } else if (out == OUT_CSV) {
  248. printf("no csv output implemented yet.\n");
  249. }
  250. break;
  251. default:
  252. interrupted=1;
  253. err("no monitoring writting for this demod, yet.\n");
  254. }
  255. usleep((int) (intervall * 1000000));
  256. }
  257. close(dib.fd);
  258. return 0;
  259. }