Exemplo n.º 1
0
void TAvr::SetWriteTimings(){
  const char* val;

  page_size = GetWritePageSize();
  if (page_size)
    Info(3, "Page Write Enabled, size=%d\n", (int) page_size);
  else
    Info(3, "Page Write Disabled\n");

  /* defaults */
  t_wd_flash = CALC_FLASH_T_wd(AVR_DEFAULT_VOLTAGE);
  t_wd_eeprom = CALC_EEPROM_T_wd(AVR_DEFAULT_VOLTAGE);  
    
  /* set FLASH write delay */  
  if ((val=GetCmdParam("-dt_wd_flash"))){
    t_wd_flash = atol(val);
    Info(0, "t_wd_flash = %ld\n", t_wd_flash);
    if (t_wd_flash < CALC_FLASH_T_wd(AVR_MAX_VOLTAGE)){
      Info(0, " * According to the Atmel specs the t_wd_flash\n"
              "   should be at least %ld us\n", 
	      CALC_FLASH_T_wd(AVR_MAX_VOLTAGE));
#if 0
      throw Error_Device("-dt_wd_flash: Value out of range.");
#endif
    }
  }
  
  /* set EEPROM write delay */  
  if ((val=GetCmdParam("-dt_wd_eeprom"))){
    t_wd_eeprom = atol(val);
    if (t_wd_eeprom < CALC_EEPROM_T_wd(AVR_MAX_VOLTAGE)){
      Info(0, " * According to the Atmel specs the t_wd_eeprom\n"
              "   should be at least %ld us\n", 
	      CALC_EEPROM_T_wd(AVR_MAX_VOLTAGE));
#if 0
      throw Error_Device("-dt_wd_eeprom: Value out of range.");
#endif
    }
  }

  /* Set Timings according to the Power Supply Voltage */
  if ((val=GetCmdParam("-dvoltage"))){
    double voltage = atof(val);
    if (voltage < AVR_MIN_VOLTAGE || voltage > AVR_MAX_VOLTAGE){
      Info(0, " * Atmel AVR MCUs operate in range from %.1f to %.1f V\n",
           AVR_MIN_VOLTAGE, AVR_MAX_VOLTAGE);
	   
      throw Error_Device("-dvoltage: Value out of range.");
    }
    
    t_wd_flash = CALC_FLASH_T_wd(voltage);
    t_wd_eeprom = CALC_EEPROM_T_wd(voltage);
  }
  
  Info(3, "FLASH Write Delay (t_wd_flash): %ld us\n"
          "EEPROM Write Delay (t_wd_eeprom): %ld us\n",
	  t_wd_flash, t_wd_eeprom);
}
Exemplo n.º 2
0
void TAvr::Identify()
{
  const char* vendor = "Device";

  Info(3, "Vendor Code: 0x%02x\nPart Family: 0x%02x\nPart Number: 0x%02x\n",
    vendor_code, part_family, part_number);  

  /* Identify AVR Part according to the vendor_code ... */
  if (vendor_code==0x1e){vendor = "Atmel AVR";}
  
  if (vendor_code==0 && part_family==DEVICE_LOCKED && part_number==0x02){
    device_locked=true;
    Info(0, "Cannot identify device because it is locked.\n");
    /* XXX hack to avoid "invalid parameter" errors if device is locked */
    GetCmdParam("-dt_wd_eeprom");
    GetCmdParam("-dt_wd_flash");
    GetCmdParam("-dvoltage");
#if 0
    return;
#endif
  } else{device_locked=false;}
  if (part_family==TARGET_MISSING){
    Info(0, "An error has occurred during the AVR initialization.\n"
	    " * Target status:\n"
	    "   Vendor Code = 0x%02x, Part Family = 0x%02x, Part Number = 0x%02x\n\n",
	    vendor_code, part_family, part_number);
    throw 
      Error_Device("Probably the wiring is incorrect or target"
        " might be `damaged'.");
  }
  int i,n;
  for(i=0; parts[i].part_family != 0x0; i++){
    if (part_family == parts[i].part_family){
      for (n=i; parts[n].part_family==part_family; n++){
        if (part_number == parts[n].part_number){i=n; break;}
      }
      if (i==n){Info(1, "%s %s is found.\n", vendor, parts[i].name);}
      else{Info(1, "%s similar to the %s is found.\n", vendor, parts[i].name);}
      part = &parts[i];
      break;
    }
  }
  if (parts[i].part_family == 0x0) {
    throw Error_Device ("Probably the AVR MCU is not in the RESET state.\n"
			"Check it out and run me again.");}

  if (!GetCmdParam("--download", false))
    SetWriteTimings();
}
Exemplo n.º 3
0
Arquivo: SASA.C Projeto: iplusu/sarp
void
TSASA::PulseReset()
{
  close(dev_fd);

  Delay_usec(1000);

  dev_fd = open(dev_name, O_RDWR, 0);
  if (dev_fd == -1) {
    perror(dev_name);
    throw Error_Device("Failed to reopen ppdev.");
  }

}
Exemplo n.º 4
0
Arquivo: SASA.C Projeto: iplusu/sarp
TSASA::TSASA(): 
  dev_fd(-1)
{

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

  dev_fd = open(dev_name, O_RDWR, 0);
  if (dev_fd == -1) {
    perror(dev_name);
    throw Error_Device("Failed to open the SSP. Is the driver installed?");
  }

}
Exemplo n.º 5
0
void
TStk500::WriteParam(TByte param, TByte val)
{
  TByte buf[0x80];

  TByte wr_param[] = { 0x40, param, val, 0x20 };
  TByte wr_param_reply[] = { 0x14, 0x10 };

  memcpy(buf, wr_param, sizeof(wr_param));
  Send(buf, sizeof(wr_param), sizeof(wr_param_reply));

  if (memcmp(buf, wr_param_reply, sizeof(wr_param_reply)) != 0)
  {
      throw Error_Device ("Failed to write parameter", pNodename);
  }
}
Exemplo n.º 6
0
TByte
TStk500::ReadParam(TByte param)
{
  TByte buf[0x80];

  TByte rd_param[] = { 0x41, param, 0x20 };
  TByte rd_param_reply[] = { 0x14, '?', 0x10 };

  memcpy(buf, rd_param, sizeof(rd_param));
  Send(buf, sizeof(rd_param), sizeof(rd_param_reply));

  if ((buf[0] != rd_param_reply[0]) || (buf[2] != rd_param_reply[2]))
  {
      throw Error_Device ("Failed to read parameter", pNodename);
  }

  return buf[1];
}
Exemplo n.º 7
0
void TAvr::OverridePart(const char *part_name)
{
  int i;

  for (i = 0; parts[i].name[0]; i++) {
    if (strcasecmp(parts[i].name, part_name) == 0)
      break;
  }
  if (parts[i].name[0]) {
    if (vendor_code != 0x1e
	|| part_family != parts[i].part_family
	|| part_number != parts[i].part_number) {
      vendor_code = 0x1e;
      part_family = parts[i].part_family;
      part_number = parts[i].part_number;

      Info(3, "Override signature bytes, device %s assumed.\n",
	   parts[i].name);
    }
  } else
    throw Error_Device("Unknown device specified", part_name);
}
Exemplo n.º 8
0
Arquivo: DAPA.C Projeto: iplusu/sarp
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();
}
Exemplo n.º 9
0
void TAvrAtmel::CheckResponse(TByte x){
  if (x!=13){throw Error_Device ("Device is not responding correctly.");}
}