2005-09-18 20:12:23 +00:00
|
|
|
/*
|
|
|
|
* avrdude - A Downloader/Uploader for AVR device programmers
|
|
|
|
* Copyright (C) 2000, 2001, 2002, 2003 Brian S. Dean <bsd@bsdhome.com>
|
|
|
|
* Copyright (C) 2005 Michael Holzt <kju-avr@fqdn.org>
|
2006-08-30 14:09:58 +00:00
|
|
|
* Copyright (C) 2006 Joerg Wunsch <j@uriah.heep.sax.de>
|
2005-09-18 20:12:23 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2012-11-20 14:03:50 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2005-09-18 20:12:23 +00:00
|
|
|
*/
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Posix serial bitbanging interface for avrdude.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(WIN32NATIVE)
|
|
|
|
|
|
|
|
#include "ac_cfg.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
|
2007-01-24 21:07:54 +00:00
|
|
|
#include "avrdude.h"
|
2014-05-19 10:01:59 +00:00
|
|
|
#include "libavrdude.h"
|
|
|
|
|
2005-09-18 20:12:23 +00:00
|
|
|
#include "bitbang.h"
|
2012-01-31 17:03:43 +00:00
|
|
|
#include "serbb.h"
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
2007-01-24 21:07:54 +00:00
|
|
|
static struct termios oldmode;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
serial port/pin mapping
|
|
|
|
|
|
|
|
1 cd <-
|
2006-08-30 14:09:58 +00:00
|
|
|
2 (rxd) <-
|
2005-09-18 20:12:23 +00:00
|
|
|
3 txd ->
|
|
|
|
4 dtr ->
|
2006-08-30 14:09:58 +00:00
|
|
|
5 GND
|
|
|
|
6 dsr <-
|
|
|
|
7 rts ->
|
|
|
|
8 cts <-
|
|
|
|
9 ri <-
|
2005-09-18 20:12:23 +00:00
|
|
|
*/
|
|
|
|
|
2006-08-30 14:09:58 +00:00
|
|
|
#define DB9PINS 9
|
|
|
|
|
|
|
|
static int serregbits[DB9PINS + 1] =
|
|
|
|
{ 0, TIOCM_CD, 0, 0, TIOCM_DTR, 0, TIOCM_DSR, TIOCM_RTS, TIOCM_CTS, TIOCM_RI };
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2006-08-30 14:09:58 +00:00
|
|
|
static char *serpins[DB9PINS + 1] =
|
|
|
|
{ "NONE", "CD", "RXD", "TXD", "DTR", "GND", "DSR", "RTS", "CTS", "RI" };
|
2005-09-18 20:12:23 +00:00
|
|
|
#endif
|
|
|
|
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
static int serbb_setpin(PROGRAMMER * pgm, int pinfunc, int value)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
unsigned int ctl;
|
2006-08-31 22:07:39 +00:00
|
|
|
int r;
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
int pin = pgm->pinno[pinfunc]; // get its value
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
if (pin & PIN_INVERSE)
|
|
|
|
{
|
|
|
|
value = !value;
|
|
|
|
pin &= PIN_MASK;
|
|
|
|
}
|
|
|
|
|
2006-08-30 14:09:58 +00:00
|
|
|
if ( pin < 1 || pin > DB9PINS )
|
2005-11-01 23:02:06 +00:00
|
|
|
return -1;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("%s to %d\n",serpins[pin],value);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch ( pin )
|
|
|
|
{
|
2006-08-30 14:09:58 +00:00
|
|
|
case 3: /* txd */
|
2006-12-11 12:47:35 +00:00
|
|
|
r = ioctl(pgm->fd.ifd, value ? TIOCSBRK : TIOCCBRK, 0);
|
2006-08-31 22:07:39 +00:00
|
|
|
if (r < 0) {
|
|
|
|
perror("ioctl(\"TIOCxBRK\")");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-15 16:40:17 +00:00
|
|
|
break;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
2006-08-30 14:09:58 +00:00
|
|
|
case 4: /* dtr */
|
|
|
|
case 7: /* rts */
|
2006-12-11 12:47:35 +00:00
|
|
|
r = ioctl(pgm->fd.ifd, TIOCMGET, &ctl);
|
2006-08-31 22:07:39 +00:00
|
|
|
if (r < 0) {
|
|
|
|
perror("ioctl(\"TIOCMGET\")");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-18 20:12:23 +00:00
|
|
|
if ( value )
|
|
|
|
ctl |= serregbits[pin];
|
|
|
|
else
|
|
|
|
ctl &= ~(serregbits[pin]);
|
2006-12-11 12:47:35 +00:00
|
|
|
r = ioctl(pgm->fd.ifd, TIOCMSET, &ctl);
|
2006-08-31 22:07:39 +00:00
|
|
|
if (r < 0) {
|
|
|
|
perror("ioctl(\"TIOCMSET\")");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-01-15 16:40:17 +00:00
|
|
|
break;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
default: /* impossible */
|
2005-11-01 23:02:06 +00:00
|
|
|
return -1;
|
2005-09-18 20:12:23 +00:00
|
|
|
}
|
2010-01-15 16:40:17 +00:00
|
|
|
|
|
|
|
if (pgm->ispdelay > 1)
|
|
|
|
bitbang_delay(pgm->ispdelay);
|
|
|
|
|
|
|
|
return 0;
|
2005-09-18 20:12:23 +00:00
|
|
|
}
|
|
|
|
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
static int serbb_getpin(PROGRAMMER * pgm, int pinfunc)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
unsigned int ctl;
|
|
|
|
unsigned char invert;
|
2006-08-31 22:07:39 +00:00
|
|
|
int r;
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
int pin = pgm->pinno[pinfunc]; // get its value
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
if (pin & PIN_INVERSE)
|
|
|
|
{
|
|
|
|
invert = 1;
|
|
|
|
pin &= PIN_MASK;
|
|
|
|
} else
|
|
|
|
invert = 0;
|
|
|
|
|
2006-08-30 14:09:58 +00:00
|
|
|
if ( pin < 1 || pin > DB9PINS )
|
2005-09-18 20:12:23 +00:00
|
|
|
return(-1);
|
|
|
|
|
|
|
|
switch ( pin )
|
|
|
|
{
|
2006-08-30 14:09:58 +00:00
|
|
|
case 2: /* rxd, currently not implemented, FIXME */
|
2005-09-18 20:12:23 +00:00
|
|
|
return(-1);
|
|
|
|
|
2006-08-30 14:09:58 +00:00
|
|
|
case 1: /* cd */
|
|
|
|
case 6: /* dsr */
|
|
|
|
case 8: /* cts */
|
|
|
|
case 9: /* ri */
|
2006-12-11 12:47:35 +00:00
|
|
|
r = ioctl(pgm->fd.ifd, TIOCMGET, &ctl);
|
2006-08-31 22:07:39 +00:00
|
|
|
if (r < 0) {
|
|
|
|
perror("ioctl(\"TIOCMGET\")");
|
|
|
|
return -1;
|
|
|
|
}
|
2005-09-18 20:12:23 +00:00
|
|
|
if ( !invert )
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("%s is %d\n",serpins[pin],(ctl & serregbits[pin]) ? 1 : 0 );
|
|
|
|
#endif
|
|
|
|
return ( (ctl & serregbits[pin]) ? 1 : 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("%s is %d (~)\n",serpins[pin],(ctl & serregbits[pin]) ? 0 : 1 );
|
|
|
|
#endif
|
|
|
|
return (( ctl & serregbits[pin]) ? 0 : 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
default: /* impossible */
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
static int serbb_highpulsepin(PROGRAMMER * pgm, int pinfunc)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
int pin = pgm->pinno[pinfunc]; // replace pin name by its value
|
|
|
|
|
2010-01-08 16:31:54 +00:00
|
|
|
if ( (pin & PIN_MASK) < 1 || (pin & PIN_MASK) > DB9PINS )
|
2005-09-18 20:12:23 +00:00
|
|
|
return -1;
|
|
|
|
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
serbb_setpin(pgm, pinfunc, 1);
|
|
|
|
serbb_setpin(pgm, pinfunc, 0);
|
2005-09-18 20:12:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-01-30 13:41:54 +00:00
|
|
|
static void serbb_display(PROGRAMMER *pgm, const char *p)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
/* MAYBE */
|
|
|
|
}
|
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
static void serbb_enable(PROGRAMMER *pgm)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
static void serbb_disable(PROGRAMMER *pgm)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
static void serbb_powerup(PROGRAMMER *pgm)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
static void serbb_powerdown(PROGRAMMER *pgm)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
/* nothing */
|
|
|
|
}
|
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
static int serbb_open(PROGRAMMER *pgm, char *port)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
|
|
|
struct termios mode;
|
|
|
|
int flags;
|
2006-08-31 22:07:39 +00:00
|
|
|
int r;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
2014-05-16 15:52:25 +00:00
|
|
|
if (bitbang_check_prerequisites(pgm) < 0)
|
|
|
|
return -1;
|
2006-08-23 21:06:28 +00:00
|
|
|
|
2005-09-18 20:12:23 +00:00
|
|
|
/* adapted from uisp code */
|
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
pgm->fd.ifd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
|
2005-09-18 20:12:23 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
if (pgm->fd.ifd < 0) {
|
2006-08-31 22:07:39 +00:00
|
|
|
perror(port);
|
2005-11-01 23:02:06 +00:00
|
|
|
return(-1);
|
2006-08-31 22:07:39 +00:00
|
|
|
}
|
2005-09-18 20:12:23 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
r = tcgetattr(pgm->fd.ifd, &mode);
|
2006-08-31 22:07:39 +00:00
|
|
|
if (r < 0) {
|
2014-05-18 08:41:46 +00:00
|
|
|
avrdude_message("%s: ", port);
|
2006-08-31 22:07:39 +00:00
|
|
|
perror("tcgetattr");
|
|
|
|
return(-1);
|
|
|
|
}
|
2005-11-01 23:02:06 +00:00
|
|
|
oldmode = mode;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
mode.c_iflag = IGNBRK | IGNPAR;
|
|
|
|
mode.c_oflag = 0;
|
|
|
|
mode.c_cflag = CLOCAL | CREAD | CS8 | B9600;
|
|
|
|
mode.c_cc [VMIN] = 1;
|
|
|
|
mode.c_cc [VTIME] = 0;
|
2005-09-18 20:12:23 +00:00
|
|
|
|
2006-12-11 12:47:35 +00:00
|
|
|
r = tcsetattr(pgm->fd.ifd, TCSANOW, &mode);
|
2006-08-31 22:07:39 +00:00
|
|
|
if (r < 0) {
|
2014-05-18 08:41:46 +00:00
|
|
|
avrdude_message("%s: ", port);
|
2006-08-31 22:07:39 +00:00
|
|
|
perror("tcsetattr");
|
|
|
|
return(-1);
|
|
|
|
}
|
2005-11-01 23:02:06 +00:00
|
|
|
|
|
|
|
/* Clear O_NONBLOCK flag. */
|
2006-12-11 12:47:35 +00:00
|
|
|
flags = fcntl(pgm->fd.ifd, F_GETFL, 0);
|
2005-11-01 23:02:06 +00:00
|
|
|
if (flags == -1)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
2014-05-18 08:41:46 +00:00
|
|
|
avrdude_message("%s: Can not get flags: %s\n",
|
2006-08-31 22:07:39 +00:00
|
|
|
progname, strerror(errno));
|
2005-09-18 20:12:23 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
2005-11-01 23:02:06 +00:00
|
|
|
flags &= ~O_NONBLOCK;
|
2006-12-11 12:47:35 +00:00
|
|
|
if (fcntl(pgm->fd.ifd, F_SETFL, flags) == -1)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
2014-05-18 08:41:46 +00:00
|
|
|
avrdude_message("%s: Can not clear nonblock flag: %s\n",
|
2006-08-31 22:07:39 +00:00
|
|
|
progname, strerror(errno));
|
2005-09-18 20:12:23 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2005-11-01 23:02:06 +00:00
|
|
|
static void serbb_close(PROGRAMMER *pgm)
|
2005-09-18 20:12:23 +00:00
|
|
|
{
|
2006-12-11 12:47:35 +00:00
|
|
|
if (pgm->fd.ifd != -1)
|
2006-08-31 22:07:39 +00:00
|
|
|
{
|
2006-12-11 12:47:35 +00:00
|
|
|
(void)tcsetattr(pgm->fd.ifd, TCSANOW, &oldmode);
|
Rework of bitbanging functions setpin, getpin, highpulsepin to make simplier use of new pindefs data in pgm structure
* linuxgpio.c, bitbang.c, buspirate.c, par.c, pgm.h, term.c, serbb_*.c: changed
interface of setpin, getpin, highpulsepin to take pin function as parameter
(not the real number, which can be found by pgm->pinno[function])
git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1252 81a1dc3b-b13d-400b-aceb-764788c761c2
2013-12-04 19:02:55 +00:00
|
|
|
pgm->setpin(pgm, PIN_AVR_RESET, 1);
|
2006-12-11 12:47:35 +00:00
|
|
|
close(pgm->fd.ifd);
|
2006-08-31 10:55:20 +00:00
|
|
|
}
|
2005-09-18 20:12:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-31 17:03:43 +00:00
|
|
|
const char serbb_desc[] = "Serial port bitbanging";
|
|
|
|
|
2005-09-18 20:12:23 +00:00
|
|
|
void serbb_initpgm(PROGRAMMER *pgm)
|
|
|
|
{
|
|
|
|
strcpy(pgm->type, "SERBB");
|
|
|
|
|
2013-05-03 22:35:00 +00:00
|
|
|
pgm_fill_old_pins(pgm); // TODO to be removed if old pin data no longer needed
|
|
|
|
|
2005-09-18 20:12:23 +00:00
|
|
|
pgm->rdy_led = bitbang_rdy_led;
|
|
|
|
pgm->err_led = bitbang_err_led;
|
|
|
|
pgm->pgm_led = bitbang_pgm_led;
|
|
|
|
pgm->vfy_led = bitbang_vfy_led;
|
|
|
|
pgm->initialize = bitbang_initialize;
|
|
|
|
pgm->display = serbb_display;
|
|
|
|
pgm->enable = serbb_enable;
|
|
|
|
pgm->disable = serbb_disable;
|
|
|
|
pgm->powerup = serbb_powerup;
|
|
|
|
pgm->powerdown = serbb_powerdown;
|
|
|
|
pgm->program_enable = bitbang_program_enable;
|
|
|
|
pgm->chip_erase = bitbang_chip_erase;
|
|
|
|
pgm->cmd = bitbang_cmd;
|
2011-08-23 21:03:36 +00:00
|
|
|
pgm->cmd_tpi = bitbang_cmd_tpi;
|
2005-09-18 20:12:23 +00:00
|
|
|
pgm->open = serbb_open;
|
|
|
|
pgm->close = serbb_close;
|
2005-11-01 23:02:06 +00:00
|
|
|
pgm->setpin = serbb_setpin;
|
|
|
|
pgm->getpin = serbb_getpin;
|
|
|
|
pgm->highpulsepin = serbb_highpulsepin;
|
2006-11-20 15:08:13 +00:00
|
|
|
pgm->read_byte = avr_read_byte_default;
|
|
|
|
pgm->write_byte = avr_write_byte_default;
|
2005-09-18 20:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WIN32NATIVE */
|