예제 #1
0
static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
	struct i2c_msg *pmsg;
	int i;
	int ret = 0;
	unsigned long orig_jiffies = jiffies;
	struct mpc_i2c *i2c = i2c_get_adapdata(adap);

	mpc_i2c_start(i2c);

	/* Allow bus up to 1s to become not busy */
	while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
		if (signal_pending(current)) {
			pr_debug("I2C: Interrupted\n");
			writeccr(i2c, 0);
			return -EINTR;
		}
		if (time_after(jiffies, orig_jiffies + HZ)) {
			pr_debug("I2C: timeout\n");
			if (readb(i2c->base + MPC_I2C_SR) ==
			    (CSR_MCF | CSR_MBB | CSR_RXAK))
				mpc_i2c_fixup(i2c);
			return -EIO;
		}
		schedule();
	}

	for (i = 0; ret >= 0 && i < num; i++) {
		pmsg = &msgs[i];
		pr_debug("Doing %s %d bytes to 0x%02x - %d of %d messages\n",
			 pmsg->flags & I2C_M_RD ? "read" : "write",
			 pmsg->len, pmsg->addr, i + 1, num);
		if (pmsg->flags & I2C_M_RD)
			ret =
			    mpc_read(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
		else
			ret =
			    mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
	}
	mpc_i2c_stop(i2c);
	return (ret < 0) ? ret : num;
}
예제 #2
0
파일: testmpc.c 프로젝트: mdwarfgeek/lib
int main (int argc, char *argv[]) {
  struct dtai_table dtab;
  struct iers_table itab;
  struct jpleph_table jtab, ttab, *tptr = NULL;
  struct observer obs;

  struct timeval tv;
  int utcdn;
  double utcdf;

  double dtt, tt;

  /* MEarth */
  double longitude = -399161.0*AS_TO_RAD;  /* -110 52 41.0 */
  double latitude  =  114063.1*AS_TO_RAD;  /*  +31 41 03.1 */
  double height = 2384.0;
  double temperat = 283.16;
  double humidity = 0.3;
  double pressure = 767.1;
  double wavelength = 0.85;

  struct source *srclist = (struct source *) NULL, *src;
  int f, rv, isrc, nsrc = 0;

  double s[3], dsdt[3], a, d, dadt, dddt;
  char astr[64], dstr[64];
  double az, el, pr;

  if(argc < 2) {
    fprintf(stderr, "Usage:\t%s file [...]\n", argv[0]);
    exit(1);
  }

  tcutil_init();

  /* Setup Earth orientation data and JPL ephemerides */
  rv = dtai_read(&dtab, (char *) NULL);
  if(rv)
    fatal(1, "dtai_open: error %d", rv);

  rv = iers_open(&itab, &dtab, (char *) NULL);
  if(rv)
    fatal(1, "iers_open: %d", rv);

  rv = jpleph_open(&jtab, 0, (char *) NULL);
  if(rv)
    fatal(1, "jpleph_open: %d", rv);

  if(!jtab.has_time) {
    rv = jpleph_open(&ttab, 1, (char *) NULL);
    if(rv == -2) {
      printf("Time ephemeris problem, continuing without\n");
      tptr = NULL;
    }
    else if(rv)
      fatal(1, "jpleph_open: %d", rv);
    else
      tptr = &ttab;
  }
  else
    tptr = NULL;

  /* Setup observer structure */
  observer_init(&obs, longitude, latitude, height);

  refract_const(temperat, humidity, pressure, wavelength, height,
		obs.refco);

  /* Get UNIX time (UTC) */
  rv = gettimeofday(&tv, (struct timezone *) NULL);
  if(rv)
    error(1, "gettimeofday");
  
  /* Split into days since epoch and seconds since midnight.
     We want floor(tv_sec / DAY), computed here with integer
     arithmetic. */
  utcdn = tv.tv_sec / DAY;  /* truncation */
  if(tv.tv_sec < 0 && tv.tv_sec % DAY)
    utcdn--;  /* floor rather than truncating toward zero */
  
  /* Fraction, note subtle ordering to preserve precision */
  utcdf = ((tv.tv_sec - utcdn*DAY) +
	   tv.tv_usec / 1000000.0) / ((double) DAY);
  
  /* Convert to MJD */
  utcdn += JUNIX;
  
  /* Look up delta(TT) */
  dtt = dtai(&dtab, utcdn, utcdf) + DTT;

  /* Easy to use but less precise TT as MJD */
  tt = utcdn + utcdf + dtt/DAY;

  printf("utc=%lf tt=%lf\n", utcdn+utcdf, tt);
  
  rv = observer_update(&obs, &jtab, tptr, &itab, utcdn+utcdf, dtt,
                       OBSERVER_UPDATE_ALL);
  if(rv)
    fatal(1, "observer_update: %d (%s)", rv, strerror(errno));

  for(f = 1; f < argc; f++) {
    rv = mpc_read(argv[f], &srclist, &nsrc);
    if(rv < 0)
      fatal(1, "mpc_read: %d", rv);
  }

  tcutil_attr(ATTR_BOLD);
  printf("Name             RA (Astrometric ICRS) DEC  d(RA)/dt d(DEC)/dt dist/AU el/d");
  tcutil_attr(ATTR_NORM);
  printf("\n");

  for(isrc = 0; isrc < nsrc; isrc++) {
    src = srclist + isrc;
    
#ifdef TEST
    fprintf(stderr,
            "%s: r_0=(%le, %le, %le) v_0=(%le, %le, %le) t_0=%lf alpha=%le r_0=%le\n",
            src->name,
            src->ref_n[0], src->ref_n[1], src->ref_n[2],
            src->ref_dndt[0], src->ref_dndt[1], src->ref_dndt[2],
            src->ref_tdb,
            src->alpha, src->rref);
#endif
    
    /* Astrometric place of source */
    source_place(&obs, src, &jtab, TR_MOTION | TR_PLX, s, dsdt, &pr);
    
    /* Take off actual gravitational deflection, and put on
       false deflection appropriate for star coordinates. */
    observer_ast2obs(&obs, s, dsdt, pr, TR_DEFL);
    observer_obs2ast(&obs, s, 0, TR_DEFL);
    
    v_to_ad(s, 0, &a, &d);
    v_to_ad_dt(s, dsdt, 0, &dadt, &dddt);
    
    a = ranormp(a);

    base10_to_60(a, UNIT_RAD, astr, sizeof(astr), " ", "", 3, UNIT_HR);
    base10_to_60(d, UNIT_RAD, dstr, sizeof(dstr), " ", "+", 2, UNIT_DEG);
    
    /* Observed place */
    observer_ast2obs(&obs, s, dsdt, 0, TR_TO_OBS_AZ);
    v_to_ad(s, 0, &az, &el);
    
    printf("%-16s %s %s %9.6f %9.6f %7.4f %5.1f\n",
           src->name, astr, dstr,
           dadt*RAD_TO_AS/DAY, dddt*RAD_TO_AS/DAY,
           1.0/pr, el*RAD_TO_DEG);
  }
  
  return(0);
}
예제 #3
0
int main(int ac, char **av, char **ae)
{
  appsubclassname_t sc;
  char ch[256];
  channel_t chan[2];
  u_short cluster;
  pnode_t pnode, distnode;
  int nclusters;
  int i, res;
  fd_set readfds;
  mpc_chan_set readchs;
  struct timeval tv;
  int raw;

  if (ac > 1) raw = 1;
  else raw = 0;

  mpc_init();

  mpc_get_local_infos(&cluster, &pnode, &nclusters);

  distnode = (!pnode) ? 1 : 0;

  sc = make_subclass_prefnode(cluster, distnode, 0xAFAF);
  res = mpc_get_channel(APPCLASS_INTERNET, sc, &chan[0], &chan[1], HSL_PROTO_MDCP);
  if (res) printf("mpc_get_channel() failed\n");
  else if (!raw) printf("chan0 = 0x%x - chan1 = 0x%x\n", (int) chan[0], (int) chan[1]);

  for (;;) {
    FD_ZERO(&readfds);
    FD_SET(0, &readfds);
    MPC_CHAN_ZERO(&readchs);
    MPC_CHAN_SET(distnode, chan[0], &readchs);
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    if (!raw) printf("mpc_select()\n");
    res = mpc_select(1, &readfds, NULL, NULL, &readchs, NULL, NULL, &tv);
    if (res < 0) {
      perror("mpc_select");
      exit(1);
    }
    if (!raw) printf("mpc_select() returned %d\n", res);

#if 0
    printf("readchs.max_index = %d\n", readchs.max_index);
    for (i = 0; i <= readchs.max_index; i++)
      printf("readchs: idx=%d is_set=%d\n", i, readchs.chan_set[i].is_set);
#endif

    if (FD_ISSET(0, &readfds) == TRUE) {
      if (!raw) printf("WE CAN READ ON STDIN\n");
      res = read(0, ch, sizeof ch);
      if (res < 0) {
	perror("read");
	exit(1);
      }
      if (!res) {
	printf("EOF!\n");
	exit(0);
      }
      if (res > 0) {
	res = mpc_write(distnode, chan[0], ch, res);
	if (res < 0) {
	  perror("write");
	  exit(1);
	}
      }
    }
    if (MPC_CHAN_ISSET(distnode, chan[0], &readchs) == TRUE) {
      if (!raw) printf("WE CAN READ ON THE CHANNEL\n");
      res = mpc_read(distnode, chan[0], ch, sizeof ch);
      if (res < 0) perror("mpc_read");
      if (!raw) printf("mpc_read() -> %d\n", res);
      if (!res) {
	printf("EOF!\n");
	exit(0);
      }
      if (res > 0) {
	if (raw) write(1, ch, res);
	else
	  for (i = 0; i < res; i++)
	    printf("ch[%d] = %#x (%c)\n", i, (int) ch[i], ch[i]);
      }
    }
  }

#if 0
  res = mpc_close_channel(APPCLASS_INTERNET, sc);
  printf("status = %d\n", res);
#endif

  mpc_close();
  return 0;
}