⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dapa.c

📁 tinyos-2.x.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
    if (b) par_data |= MAXI_DOUT; else par_data &= ~MAXI_DOUT;
    ParportWriteData();
    break;

  case PAT_XIL:
    if (b) par_data |= XIL_DOUT; else par_data &= ~XIL_DOUT;
    ParportWriteData();
    break;

  case PAT_DASA:
#if defined(TIOCMGET) && defined(TIOCCBRK)
    ioctl(ppdev_fd, b ? TIOCSBRK : TIOCCBRK, 0);
#endif /* TIOCMGET */
    break;

  case PAT_DASA2:
#if defined(TIOCMGET)
    if (b) ser_ctrl |= TIOCM_DTR; else ser_ctrl &= ~TIOCM_DTR;
    SerialWriteCtrl();
#endif /* TIOCMGET */
    break;
  }
}

int
TDAPA::InData()
{
  int b = 0;

  switch (pa_type) {
  case PAT_DAPA:
  case PAT_DAPA_2:
  case PAT_ABB:
  case PAT_DT006:
    ParportReadStatus();
    b = (~par_status & PARPORT_STATUS_BUSY);
    break;
  case PAT_ETT:
    ParportReadStatus();
    b = (par_status & ETT_DIN);
    break;
  case PAT_STK200:
  case PAT_AVRISP:
  case PAT_BSD:
  case PAT_FBPRG:
  case PAT_MAXI:
    ParportReadStatus();
    b = (par_status & PARPORT_STATUS_ACK);
    break;
  case PAT_XIL:
    ParportReadStatus();
    b = (par_status & PARPORT_STATUS_SELECT);
    break;
  case PAT_DASA:
  case PAT_DASA2:
#ifdef TIOCMGET
    SerialReadCtrl();
#ifdef DEBUG1
    printf("%c",(ser_ctrl & TIOCM_CTS)?'I':'i');
#endif
    b = (ser_ctrl & TIOCM_CTS);
#endif /* TIOCMGET */
    break;
  }
  if (miso_invert)
    b = !b;
  return b;
}

void
TDAPA::Init()
{
  /* data=1, reset=0, sck=0 */
  switch (pa_type) {
  case PAT_DAPA:
    par_ctrl = DAPA_SCK;
    par_data = 0xFF;
    par_data &= ~0x6; //0x6
    par_data |= 0x0; //0x6
    break;
  case PAT_DAPA_2:
    par_ctrl = DAPA_SCK;
    par_data = 0xFF;
    par_data &= ~0x6; //0x6
    par_data |= 0x4; //0x6
    break;

  case PAT_STK200:
    par_ctrl = 0;
    par_data = 0xFF & ~(STK2_ENA1 | STK2_SCK);
    break;

  case PAT_ABB:
    par_ctrl = ABB_EN;
    par_data = 0xFF & ~ABB_SCK;
    break;

  case PAT_AVRISP:
    par_ctrl = 0;
    par_data = 0xFF & ~(AISP_ENA | AISP_SCK);
    break;

  case PAT_BSD:
    par_ctrl = 0;
    par_data = BSD_POWER | BSD_RESET;
    break;

  case PAT_FBPRG:
    par_ctrl = 0;
    par_data = FBPRG_POW | FBPRG_RESET;
    break;

  case PAT_ETT:
    par_ctrl = ETT_DOUT;
    par_data = ETT_SCK | ETT_RESET;
    mosi_invert = 1;
    break;

  case PAT_DT006:
  case PAT_MAXI:
    par_ctrl = 0;
    par_data = 0xFF;
    break;

  case PAT_XIL:
    par_ctrl = 0;
    par_data = 0xFF & ~(XIL_ENA | XIL_SCK | XIL_RESET);
    break;

  case PAT_DASA:
  case PAT_DASA2:
    break;
  }

  if (!pa_type_is_serial) {
    ParportWriteCtrl();
    ParportWriteData();
    SckDelay();
    ParportReadStatus();
  }

  OutEnaReset(1);
  OutReset(0);
  OutEnaSck(1);
  OutSck(0);
  /* Wait 100 ms as recommended for ATmega163 (SCK not low on power up).  */
  Delay_usec(100000);
  PulseReset();
}

int
TDAPA::SendRecv(int b)
{
  unsigned int mask, received=0;

  for (mask = 0x80; mask; mask >>= 1) {
     OutData(b & mask);
     SckDelay();
     /* MM 20020613: we used to read the bit here, but ... */
     OutSck(1);
     SckDelay();
     /* ... here we have more room for propagation delays (almost the
	whole SCK period, instead of half of it) - good for long cables,
	slow RS232 drivers/receivers, opto-isolated interfaces, etc.  */
     if (InData())
       received |= mask;
     OutSck(0);
  }
  return received;
}

int
TDAPA::Send (unsigned char* queue, int queueSize, int rec_queueSize)
{
  unsigned char *p = queue, ch;
  int i = queueSize;
  
  if (rec_queueSize==-1){rec_queueSize = queueSize;}
#ifdef DEBUG
  printf ("send(recv): ");
#endif
  while (i--){
#ifdef DEBUG
    printf ("%02X(", (unsigned int)*p);
#endif    
    ch = SendRecv(*p);
#ifdef DEBUG    
    printf ("%02X) ", (unsigned int)ch);
#endif    
    *p++ = ch;
  }
#ifdef DEBUG  
  printf ("\n");
#endif  
  return queueSize;
}


TDAPA::TDAPA(): 
  parport_base(0x378), ppdev_fd(-1)
{
  const char *val;

  /* If the user doesn't specify -dlpt option, use /dev/parport0 as the
     default instead of defaulting to using ioperm (ick!). If the user wants
     to run uisp as root (or setuid root) they should know what they are doing
     and can suffer the consequences. Joe user should not be told about ioperm
     failure due to permission denied. */
#ifdef __CYGWIN__
  /* But on cygwin, /dev/parport0 does not exist. So... */
  const char *ppdev_name = NULL;
#else
  const char *ppdev_name = "/dev/parport0";
#endif

  /* Enable Parallel Port */
  val = GetCmdParam("-dprog");
  if (val && strcmp(val, "dapa") == 0)
    pa_type = PAT_DAPA;
  else if (val && strcmp(val, "dapa_2") == 0)
    pa_type = PAT_DAPA_2;
  else if (val && strcmp(val, "stk200") == 0)
    pa_type = PAT_STK200;
  else if (val && strcmp(val, "abb") == 0)
    pa_type = PAT_ABB;
  else if (val && strcmp(val, "avrisp") == 0)
    pa_type = PAT_AVRISP;
  else if (val && strcmp(val, "bsd") == 0)
    pa_type = PAT_BSD;
  else if (val && strcmp(val, "fbprg") == 0)
    pa_type = PAT_FBPRG;
  else if (val && strcmp(val, "dt006") == 0)
    pa_type = PAT_DT006;
  else if (val && strcmp(val, "ett") == 0)
    pa_type = PAT_ETT;
  else if (val && strcmp(val, "maxi") == 0)
    pa_type = PAT_MAXI;
  else if (val && strcmp(val, "xil") == 0)
    pa_type = PAT_XIL;
  else if (val && strcmp(val, "dasa") == 0)
    pa_type = PAT_DASA;
  else if (val && strcmp(val, "dasa2") == 0)
    pa_type = PAT_DASA2;
  else {
    throw Error_Device("Direct Parallel Access not defined.");
  }
  pa_type_is_serial = (pa_type == PAT_DASA || pa_type == PAT_DASA2);
  /* Parse Command Line Switches */
#ifndef NO_DIRECT_IO
  if ((val = GetCmdParam("-dlpt")) != NULL) {
    if (!strcmp(val, "1")) {
      parport_base = 0x378;
      ppdev_name = NULL;
    }
    else if (!strcmp(val, "2")) {
      parport_base = 0x278;
      ppdev_name = NULL;
    }
    else if (!strcmp(val, "3")) {
      parport_base = 0x3bc;
      ppdev_name = NULL;
    }    
    else if (isdigit(*val)) {
      parport_base = strtol(val, NULL, 0);
      ppdev_name = NULL;
    }
    else {
      ppdev_name = val;
    }
  }
  if (!ppdev_name && !pa_type_is_serial) {
    if (parport_base!=0x278 && parport_base!=0x378 && parport_base!=0x3bc) {
      /* TODO: option to override this if you really know
	 what you're doing (only if running as root).  */
      throw Error_Device("Bad device address.");
    }
    if (ioport_enable(IOBASE, IOSIZE) != 0) {
      perror("ioperm");
      throw Error_Device("Failed to get direct I/O port access.");
    }
  }
#endif

  /* Drop privileges (if installed setuid root - NOT RECOMMENDED).  */
  setgid(getgid());
  setuid(getuid());

#ifdef NO_DIRECT_IO
  if ((val = GetCmdParam("-dlpt")) != NULL) {
    ppdev_name = val;
  }
#endif

  if (ppdev_name) {
    if (pa_type_is_serial) {
      ppdev_fd = open(ppdev_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
      if (ppdev_fd != -1) {
	struct termios pmode;

	tcgetattr(ppdev_fd, &pmode);
	saved_modes = pmode;

	cfmakeraw(&pmode);
	pmode.c_iflag &= ~(INPCK | IXOFF | IXON);
	pmode.c_cflag &= ~(HUPCL | CSTOPB | CRTSCTS);
	pmode.c_cflag |= (CLOCAL | CREAD);
	pmode.c_cc [VMIN] = 1;
	pmode.c_cc [VTIME] = 0;

	tcsetattr(ppdev_fd, TCSANOW, &pmode);

	/* Clear O_NONBLOCK flag.  */
	int flags = fcntl(ppdev_fd, F_GETFL, 0);
	if (flags == -1) { throw Error_C("Can not get flags"); }
	flags &= ~O_NONBLOCK;
	if (fcntl(ppdev_fd, F_SETFL, flags) == -1) { 
          throw Error_C("Can not clear nonblock flag");
        }
      }
    } else {
      ppdev_fd = open(ppdev_name, O_RDWR, 0);
    }
    if (ppdev_fd == -1) {
      perror(ppdev_name);
      throw Error_Device("Failed to open ppdev.");
    }
    if (!pa_type_is_serial && par_claim(ppdev_fd) != 0) {
      perror("ioctl PPCLAIM");
      close(ppdev_fd);
      ppdev_fd = -1;
      throw Error_Device("Failed to claim ppdev.");
    }
  }
  t_sck = SCK_DELAY;
  if (pa_type_is_serial)
    t_sck *= 3;  /* more delay for slow RS232 drivers */
  val = GetCmdParam("-dt_sck");
  if (val)
    t_sck = strtol(val, NULL, 0);

  sck_invert = 0;
  mosi_invert = 0;
  miso_invert = 0;
  reset_invert = 0;
  if ((val=GetCmdParam("-dinvert")))
    {
#define MAXLINESIZE    256
      char temp[MAXLINESIZE];
      char * p;
      strncpy(temp, val, MAXLINESIZE-1);
      temp[MAXLINESIZE-1] = '\0';
      for (p=temp; *p; p++)
        *p=toupper(*p);
      Info(3, "Inverting %s\n",temp);
      if (strstr(temp,"SCK"))
        sck_invert=1;

      if (strstr(temp,"MOSI"))
        mosi_invert=1;

      if (strstr(temp,"MISO"))
        miso_invert=1;

      if (strstr(temp,"RESET"))
        reset_invert=1;
    }

  reset_high_time = RESET_HIGH_TIME;
  if ((val=GetCmdParam("-dt_reset")))
    {
      reset_high_time = atoi(val);
    }
  Info(3, "Reset inactive time (t_reset) %d us\n", reset_high_time);

  Init();
}

TDAPA::~TDAPA()
{
  OutData(1); SckDelay();
  OutSck(1); SckDelay();
  OutEnaSck(0);
  OutReset(1);
  OutEnaReset(0);

  if (ppdev_fd != -1) {
    if (pa_type_is_serial)
      tcsetattr(ppdev_fd, TCSADRAIN, &saved_modes);
    else
      par_release(ppdev_fd);
    close(ppdev_fd);
    ppdev_fd = -1;
  } else
    (void) ioport_disable(IOBASE, IOSIZE);
}

#endif
/* eof */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -