Ejemplo n.º 1
0
/*
 * issue the 'program enable' command to the AVR device
 */
static int ft245r_program_enable(PROGRAMMER * pgm, AVRPART * p) {
    int retry_count = 0;
    unsigned char cmd[4];
    unsigned char res[4];
    int i,reset_ok;

    ft245r_set_bitclock(pgm);

retry:
    reset_ok = 0;
    set_reset(pgm, 0);
    usleep(5000); // 5ms
    set_reset(pgm, 1);
    usleep(5000); // 5ms
    set_reset(pgm, 0);
    usleep(5000); // 5ms

    cmd[0] = 0xAC;
    cmd[1] = 0x53;
    cmd[2] = 0;
    cmd[3] = 0;
    ft245r_cmd(pgm, cmd, res);
    if (res[2] == 0x53 ) reset_ok = 1;
    for (i=0; i<3; i++) {
        cmd[0] = 0x30;
        cmd[1] = 0;
        cmd[2] = i;
        cmd[3] = 0;
        ft245r_cmd(pgm, cmd, res);
        saved_signature[i] = res[3];
    }
    if (reset_ok && (saved_signature[0] == 0x1e)) // success
        return 0;

    if (retry_count < 5) {
        if (retry_count == 3) {
            ft245r_drain (pgm, 0);
            tail = head;
        }
        retry_count++;
        goto retry;
    }
    if ((verbose>=1) || FT245R_DEBUG) {
        fprintf(stderr,
                "%s: ft245r_program_enable: failed\n", progname);
        fflush(stderr);
    }
    return -1;
}
Ejemplo n.º 2
0
/*
 * issue the 'program enable' command to the AVR device
 */
static int ft245r_program_enable(PROGRAMMER * pgm, AVRPART * p) {
    unsigned char cmd[4] = {0,0,0,0};
    unsigned char res[4];
    int i;

    if (p->op[AVR_OP_PGM_ENABLE] == NULL) {
        avrdude_message(MSG_INFO, "%s: AVR_OP_PGM_ENABLE command not defined for %s\n",
                        progname, p->desc);
        fflush(stderr);
        return -1;
    }

    avr_set_bits(p->op[AVR_OP_PGM_ENABLE], cmd);

    for(i = 0; i < 4; i++) {
        ft245r_cmd(pgm, cmd, res);

        if (res[p->pollindex-1] == p->pollvalue) return 0;

        if (FT245R_DEBUG) {
            avrdude_message(MSG_NOTICE, "%s: Program enable command not successful. Retrying.\n",
                            progname);
            fflush(stderr);
        }
        set_pin(pgm, PIN_AVR_RESET, ON);
        usleep(20);
        set_pin(pgm, PIN_AVR_RESET, OFF);

        if (i == 3) {
            ft245r_drain(pgm, 0);
            tail = head;
        }
    }

    avrdude_message(MSG_INFO, "%s: Device is not responding to program enable. Check connection.\n",
                    progname);
    fflush(stderr);

    return -1;
}
Ejemplo n.º 3
0
static int ft245r_open(PROGRAMMER * pgm, char * port)
{
  FT_STATUS r;
  int devnum = -1;
  strcpy(pgm->port, port);

#ifdef _WIN32
  if (!strncasecmp("ft", port, 2) && '0' <= port[2] && port[2] <= '9') {
     devnum = port[2] - '0';
  } else {
    fprintf(stderr,
	    "%s: invalid portname  %s: use ft0 - ft9\n",
	    progname,port);
    exit(1);
  }
  r = FT_Open(devnum, &handle);
#else
  for (devnum=0; devnum<9; devnum++) {
     r = FT_Open(devnum, &handle);
     if (r == FT_OK) break;
  }
#endif
  if (r != FT_OK) {
    fprintf(stderr,
	    "%s: %s open failed \n",
	    progname, port);
    exit(1);
  }
  r = FT_SetBitMode(handle, 0, 0x4); // set Synchronuse BitBang
  if (r != FT_OK) {
    fprintf(stderr,
	    "%s: Synchronuse BitBangMode is not supported\n",
	    progname);
     exit(1);
  }
#if FT245R_DEBUG
  fprintf(stderr, "%s: BitBang OK \n", progname);
  fflush(stderr);
#endif

  ft245r_ddr = 0;
  setbit(&ft245r_ddr, pgm->pinno[PIN_AVR_SCK], 1);
  setbit(&ft245r_ddr, pgm->pinno[PIN_AVR_MOSI], 1);
  setbit(&ft245r_ddr, pgm->pinno[PIN_AVR_RESET], 1);
  ft245r_sck = 0;
  setbit(&ft245r_sck, pgm->pinno[PIN_AVR_SCK], 1);
  ft245r_mosi = 0;
  setbit(&ft245r_mosi, pgm->pinno[PIN_AVR_MOSI], 1);
  ft245r_reset = 0;
  setbit(&ft245r_reset, pgm->pinno[PIN_AVR_RESET], 1);
  ft245r_miso = 0;
  setbit(&ft245r_miso, pgm->pinno[PIN_AVR_MISO], 1);
  if ((verbose>=1) || FT245R_DEBUG) {
    fprintf(stderr,
	    "%s: pin assign miso %d sck %d mosi %d reset %d\n",
	      progname,
              pgm->pinno[PIN_AVR_MISO],
              pgm->pinno[PIN_AVR_SCK],
              pgm->pinno[PIN_AVR_MOSI],
              pgm->pinno[PIN_AVR_RESET]);
  }

  /*
   * drain any extraneous input
   */
  ft245r_drain (pgm, 0);
#if FT245R_DEBUG
  fprintf(stderr, "%s: drain OK \n", progname);
  fflush(stderr);
#endif
	
  return 0;
}
Ejemplo n.º 4
0
static int ft245r_open(PROGRAMMER * pgm, char * port) {
    int rv;
    int devnum = -1;

    rv = pins_check(pgm,pin_checklist,sizeof(pin_checklist)/sizeof(pin_checklist[0]), true);
    if(rv) {
        pgm->display(pgm, progbuf);
        return rv;
    }

    strcpy(pgm->port, port);

    if (strcmp(port,DEFAULT_USB) != 0) {
        if (strncasecmp("ft", port, 2) == 0) {
            char *startptr = port + 2;
            char *endptr = NULL;
            devnum = strtol(startptr,&endptr,10);
            if ((startptr==endptr) || (*endptr != '\0')) {
                devnum = -1;
            }
        }
        if (devnum < 0) {
            avrdude_message(MSG_INFO, "%s: invalid portname '%s': use 'ft[0-9]+'\n",
                            progname,port);
            return -1;
        }
    } else {
        devnum = 0;
    }

    handle = malloc (sizeof (struct ftdi_context));
    ftdi_init(handle);
    LNODEID usbpid = lfirst(pgm->usbpid);
    int pid;
    if (usbpid) {
      pid = *(int *)(ldata(usbpid));
      if (lnext(usbpid))
	avrdude_message(MSG_INFO, "%s: Warning: using PID 0x%04x, ignoring remaining PIDs in list\n",
		progname, pid);
    } else {
      pid = USB_DEVICE_FT245;
    }
    rv = ftdi_usb_open_desc_index(handle,
                                  pgm->usbvid?pgm->usbvid:USB_VENDOR_FTDI,
                                  pid,
                                  pgm->usbproduct[0]?pgm->usbproduct:NULL,
                                  pgm->usbsn[0]?pgm->usbsn:NULL,
                                  devnum);
    if (rv) {
        avrdude_message(MSG_INFO, "can't open ftdi device %d. (%s)\n", devnum, ftdi_get_error_string(handle));
        goto cleanup_no_usb;
    }

    ft245r_ddr = 
         pgm->pin[PIN_AVR_SCK].mask[0]
       | pgm->pin[PIN_AVR_MOSI].mask[0]
       | pgm->pin[PIN_AVR_RESET].mask[0]
       | pgm->pin[PPI_AVR_BUFF].mask[0]
       | pgm->pin[PPI_AVR_VCC].mask[0]
       | pgm->pin[PIN_LED_ERR].mask[0]
       | pgm->pin[PIN_LED_RDY].mask[0]
       | pgm->pin[PIN_LED_PGM].mask[0]
       | pgm->pin[PIN_LED_VFY].mask[0];

    /* set initial values for outputs, no reset everything else is off */
    ft245r_out = 0;
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_AVR_RESET,1);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_AVR_SCK,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_AVR_MOSI,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PPI_AVR_BUFF,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PPI_AVR_VCC,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_LED_ERR,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_LED_RDY,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_LED_PGM,0);
    ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_LED_VFY,0);


    rv = ftdi_set_bitmode(handle, ft245r_ddr, BITMODE_SYNCBB); // set Synchronous BitBang
    if (rv) {
        avrdude_message(MSG_INFO, "%s: Synchronous BitBangMode is not supported (%s)\n",
                        progname, ftdi_get_error_string(handle));
        goto cleanup;
    }

    rv = ft245r_set_bitclock(pgm);
    if (rv) {
        goto cleanup;
    }

    /* We start a new thread to read the output from the FTDI. This is
     * necessary because otherwise we'll deadlock. We cannot finish
     * writing because the ftdi cannot send the results because we
     * haven't provided a read buffer yet. */

    sem_init (&buf_data, 0, 0);
    sem_init (&buf_space, 0, BUFSIZE);
    pthread_create (&readerthread, NULL, reader, handle);

    /*
     * drain any extraneous input
     */
    ft245r_drain (pgm, 0);

    ft245r_send (pgm, &ft245r_out, 1);
    ft245r_recv (pgm, &ft245r_in, 1);

    return 0;

cleanup:
    ftdi_usb_close(handle);
cleanup_no_usb:
    ftdi_deinit (handle);
    free(handle);
    handle = NULL;
    return -1;
}