[Tinyos-2-commits]
CVS: tinyos-2.x/tools/platforms/msp430/cppbsl/src
Bsl.cc, NONE, 1.1 Bsl.h, NONE, 1.1 Makefile.am, NONE,
1.1 Makefile.in, NONE, 1.1 Parameters.cc, NONE,
1.1 Parameters.h, NONE, 1.1 Serial.cc, NONE, 1.1 Serial.h,
NONE, 1.1 cmdline.cc, NONE, 1.1 cmdline.h, NONE, 1.1 cppbsl.cc,
NONE, 1.1
akoepke
andreaskoepke at users.sourceforge.net
Wed Oct 31 11:54:58 PDT 2007
- Previous message: [Tinyos-2-commits]
CVS: tinyos-2.x/tools/platforms/msp430/cppbsl/config
Makefile.am, NONE, 1.1 Makefile.in, NONE, 1.1 depcomp, NONE,
1.1 install-sh, NONE, 1.1 missing, NONE, 1.1
- Next message: [Tinyos-2-commits] CVS: tinyos-2.x/tos/lib/net/ctp CtpInfo.nc, 1.4,
1.5 CtpRoutingEngineP.nc, 1.11, 1.12
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
Update of /cvsroot/tinyos/tinyos-2.x/tools/platforms/msp430/cppbsl/src
In directory sc8-pr-cvs10.sourceforge.net:/tmp/cvs-serv25145/src
Added Files:
Bsl.cc Bsl.h Makefile.am Makefile.in Parameters.cc
Parameters.h Serial.cc Serial.h cmdline.cc cmdline.h cppbsl.cc
Log Message:
added C++ bsl interface, use for resource constrained devices like the NSLU2
--- NEW FILE: Bsl.cc ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* hand rolled bsl tool, other ones are too slow
* @author Andreas Koepke <koepke at tkn.tu-berlin.de>
* @date 2007-04-16
*/
#include "Bsl.h"
using namespace std;
void Bsl::makeFrame(commands_t cmd, uint16_t A, uint16_t L, frame_t* frame, uint8_t dLen) {
frame->HDR = 0x80;
frame->CMD = (uint8_t)cmd;
frame->L1 = dLen + 4;
frame->L2 = dLen + 4;
frame->AL = A & 0xff;
frame->AH = (A>>8) & 0xff;
frame->LL = L & 0xff;
frame->LH = (L>>8) & 0xff;
}
int Bsl::rxPassword(int *err) {
frame_t txframe;
frame_t rxframe;
for(int i = 0; i < 32; i++) {
txframe.data[i] = 0xff;
}
makeFrame(RX_PWD, 0, 0, &txframe, 32);
cout << "Transmit default password ..." << endl;
return s->txrx(err, &txframe, &rxframe);
}
int Bsl::erase(int *err) {
int r = 0;
frame_t txframe;
frame_t rxframe;
makeFrame(MASS_ERASE, 0xff00, 0xa506, &txframe, 0);
for(int i = 0; i < 2; i++) {
r = s->invokeBsl(err);
if(r != -1) {
r = s->txrx(err, &txframe, &rxframe);
if(r != -1) {
cout << "Mass Erase..." << endl;
break;
}
else {
if(*err == EAGAIN) {
serial_delay(1000000);
}
else {
return -1;
}
}
}
else {
if(*err == EAGAIN) {
serial_delay(1000000);
}
else {
return -1;
}
}
}
return r;
}
int Bsl::install(int *err) {
unsigned len = 0;
int r = erase(err);
if(r == -1) {
cerr << "Bsl::install: could not erase node" << endl;
return r;
}
r = rxPassword(err);
if(r == -1) {
cerr << "Bsl::install: password not accepted" << endl;
return r;
}
r = parseIhex(err);
if(r == -1) {
cerr << "Bsl::install: could not parse ihex image" << endl;
return r;
}
r = highSpeed(err);
if(r == -1) {
cerr << " Bsl::install: could not switch to high speed mode" << endl;
return r;
}
cout << "Program ..." << endl;
for(std::list<Segment>::const_iterator it = prog.begin(); it != prog.end(); ++it) {
len += it->len;
r = writeData(err, (uint16_t)it->startAddr,it->data, it->len);
if(r == -1) {
cerr << " Bsl::install: could not write data" << endl;
return r;
}
}
cout << len << " bytes programmed." << endl;
r = s->reset(err);
if(r == -1) {
cerr << " Bsl::install: could not reset node" << endl;
}
return r;
}
int Bsl::writeBlock(int *err, const uint16_t addr, const uint8_t* data, const uint16_t len) {
frame_t txframe;
frame_t rxframe;
memcpy(txframe.data, data, len);
makeFrame(RX_DATA, addr, len, &txframe, len);
return s->txrx(err, &txframe, &rxframe);
}
int Bsl::writeData(int *err, const uint16_t addr, const uint8_t* data, const uint16_t len) {
int r = 0;
if(!data) {
cerr << "Command::write(): data==NULL" << endl;
return -1;
}
if(addr+len>0x10000) {
cerr << "Command::write(): addr+len>0x10000" << endl;
return -1;
}
int l;
uint16_t adr;
for(int i=0; i<len; i+=l) {
l=len-i;
if(l>250) l=250;
adr=addr+i;
r = writeBlock(err, adr, &data[i], l);
if(r == -1) {
break;
}
}
return r;
}
int Bsl::parseIhex(int *err) {
char buf[512];
Segment segment;
segment.len = 0;
segment.startAddr = 0;
int r;
FILE *readFD = fopen(image, "r");
if(readFD == NULL) {
*err = errno;
cerr << "Bsl::parseIhex: Could not open " << image << endl;
return -1;
};
while(fgets(buf, 512, readFD) == buf) {
uint16_t len;
uint16_t addr;
uint16_t recType;
uint16_t checksum = 0;
uint16_t byte;
unsigned i;
if(buf[0] != (uint8_t)':') {
cerr << "Bsl::parseIhex: " << image << "is not an ihex" << endl;
return -1;
}
sscanf(buf,":%2hx%4hx%2hx",&len, &addr, &recType);
checksum = len + (addr>>8) + (addr & 0xff) + recType;
if(recType == 0) {
if((segment.len != 0) && (segment.startAddr + segment.len != addr)) {
prog.push_back(segment);
segment.len = 0;
}
if(segment.len == 0) {
segment.startAddr = addr;
}
for(i = 0; i < len; i++) {
sscanf(buf+9+2*i,"%2hx",&byte);
segment.data[segment.len] = byte;
checksum += segment.data[segment.len];
++segment.len;
}
checksum = (-checksum) & 0xff;
sscanf(buf+9+2*i,"%4hx",&byte);
if(checksum == byte) {
} else {
cerr << "Bsl::parseIhex wrong data line format in " << image << endl;
fclose(readFD);
return -1;
}
}
else if(recType == 1) {
prog.push_back(segment);
}
}
r = fclose(readFD);
if(r == -1) {
*err = errno;
}
return r;
}
int Bsl::highSpeed(int *err) {
frame_t txframe;
frame_t rxframe;
int r;
for(int i = 0; i < 32; i++) {
txframe.data[i] = 0xff;
}
makeFrame(BAUDRATE, 0x87e0, 0x0002, &txframe, 0);
r = s->txrx(err, &txframe, &rxframe);
if(r != -1) {
serial_delay(10000);
r = s->highSpeed(err);
}
return r;
}
--- NEW FILE: Bsl.h ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* hand rolled bsl tool, other ones are too slow
* @author Andreas Koepke <koepke at tkn.tu-berlin.de>
* @date 2007-04-16
*/
#ifndef BSL_BSL_H
#define BSL_BSL_H
#include <string>
#include <iostream>
#include <list>
#include "Parameters.h"
#include "Serial.h"
class Bsl {
protected:
BaseSerial *s;
const char *image;
enum commands_t {
MASS_ERASE = 0x18,
RX_DATA = 0x12,
RX_PWD = 0x10,
BAUDRATE = 0x20
};
class Segment {
public:
unsigned startAddr;
unsigned len;
uint8_t data[65536];
Segment(const Segment& a) {
startAddr = a.startAddr;
len = a.len;
memcpy(data, a.data, len);
}
Segment() {
startAddr = 0;
len = 0;
}
};
std::list<Segment> prog;
protected:
int rxPassword(int* err);
int writeBlock(int *err, const uint16_t addr, const uint8_t* data, const uint16_t len);
int writeData(int *err, const uint16_t addr, const uint8_t* data, const uint16_t len);
int parseIhex(int *err);
void makeFrame(commands_t cmd, uint16_t A, uint16_t L, frame_t* frame, uint8_t dLen);
int highSpeed(int *err);
public:
Bsl(BaseSerial* ser, const char *img) : s(ser), image(img) {
};
~Bsl() {
}
int reset(int *err) { return s->reset(err);};
int erase(int *err);
int install(int *err);
};
#endif
--- NEW FILE: Makefile.am ---
## Makefile.am -- Process this file with automake to produce Makefile.in
## src/Makefile.am
MAINTAINERCLEANFILES = Makefile.in
bin_PROGRAMS=cppbsl
cppbsl_SOURCES=\
cmdline.h \
cmdline.cc \
Parameters.h \
Parameters.cc \
Serial.h \
Serial.cc \
Bsl.h \
Bsl.cc \
cppbsl.cc
--- NEW FILE: Makefile.in ---
# Makefile.in generated by automake 1.8.3 from Makefile.am.
# @configure_input@
# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
# 2003, 2004 Free Software Foundation, Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.
@SET_MAKE@
SOURCES = $(cppbsl_SOURCES)
srcdir = @srcdir@
top_srcdir = @top_srcdir@
VPATH = @srcdir@
pkgdatadir = $(datadir)/@PACKAGE@
pkglibdir = $(libdir)/@PACKAGE@
pkgincludedir = $(includedir)/@PACKAGE@
top_builddir = ..
am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
INSTALL = @INSTALL@
install_sh_DATA = $(install_sh) -c -m 644
install_sh_PROGRAM = $(install_sh) -c
install_sh_SCRIPT = $(install_sh) -c
INSTALL_HEADER = $(INSTALL_DATA)
transform = $(program_transform_name)
NORMAL_INSTALL = :
PRE_INSTALL = :
POST_INSTALL = :
NORMAL_UNINSTALL = :
PRE_UNINSTALL = :
POST_UNINSTALL = :
bin_PROGRAMS = cppbsl$(EXEEXT)
subdir = src
DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/configure.in
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
$(ACLOCAL_M4)
mkinstalldirs = $(mkdir_p)
CONFIG_HEADER = $(top_builddir)/config.h
CONFIG_CLEAN_FILES =
am__installdirs = "$(DESTDIR)$(bindir)"
binPROGRAMS_INSTALL = $(INSTALL_PROGRAM)
PROGRAMS = $(bin_PROGRAMS)
am_cppbsl_OBJECTS = cmdline.$(OBJEXT) Parameters.$(OBJEXT) \
Serial.$(OBJEXT) Bsl.$(OBJEXT) cppbsl.$(OBJEXT)
cppbsl_OBJECTS = $(am_cppbsl_OBJECTS)
cppbsl_LDADD = $(LDADD)
DEFAULT_INCLUDES = -I. -I$(srcdir) -I$(top_builddir)
depcomp = $(SHELL) $(top_srcdir)/config/depcomp
am__depfiles_maybe = depfiles
@AMDEP_TRUE at DEP_FILES = ./$(DEPDIR)/Bsl.Po ./$(DEPDIR)/Parameters.Po \
@AMDEP_TRUE@ ./$(DEPDIR)/Serial.Po ./$(DEPDIR)/cmdline.Po \
@AMDEP_TRUE@ ./$(DEPDIR)/cppbsl.Po
CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \
$(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS)
CXXLD = $(CXX)
CXXLINK = $(CXXLD) $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) $(LDFLAGS) \
-o $@
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) \
$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
CCLD = $(CC)
LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@
SOURCES = $(cppbsl_SOURCES)
DIST_SOURCES = $(cppbsl_SOURCES)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
ACLOCAL = @ACLOCAL@
AMDEP_FALSE = @AMDEP_FALSE@
AMDEP_TRUE = @AMDEP_TRUE@
AMTAR = @AMTAR@
AUTOCONF = @AUTOCONF@
AUTOHEADER = @AUTOHEADER@
AUTOMAKE = @AUTOMAKE@
AWK = @AWK@
CC = @CC@
CCDEPMODE = @CCDEPMODE@
CFLAGS = @CFLAGS@
CPP = @CPP@
CPPFLAGS = @CPPFLAGS@
CXX = @CXX@
CXXDEPMODE = @CXXDEPMODE@
CXXFLAGS = @CXXFLAGS@
CYGPATH_W = @CYGPATH_W@
DEFS = @DEFS@
DEPDIR = @DEPDIR@
ECHO_C = @ECHO_C@
ECHO_N = @ECHO_N@
ECHO_T = @ECHO_T@
EGREP = @EGREP@
EXEEXT = @EXEEXT@
INSTALL_DATA = @INSTALL_DATA@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_SCRIPT = @INSTALL_SCRIPT@
INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@
LDFLAGS = @LDFLAGS@
LIBOBJS = @LIBOBJS@
LIBS = @LIBS@
LTLIBOBJS = @LTLIBOBJS@
MAKEINFO = @MAKEINFO@
OBJEXT = @OBJEXT@
PACKAGE = @PACKAGE@
PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@
PACKAGE_NAME = @PACKAGE_NAME@
PACKAGE_STRING = @PACKAGE_STRING@
PACKAGE_TARNAME = @PACKAGE_TARNAME@
PACKAGE_VERSION = @PACKAGE_VERSION@
PATH_SEPARATOR = @PATH_SEPARATOR@
SET_MAKE = @SET_MAKE@
SHELL = @SHELL@
STRIP = @STRIP@
VERSION = @VERSION@
ac_ct_CC = @ac_ct_CC@
ac_ct_CXX = @ac_ct_CXX@
ac_ct_STRIP = @ac_ct_STRIP@
am__fastdepCC_FALSE = @am__fastdepCC_FALSE@
am__fastdepCC_TRUE = @am__fastdepCC_TRUE@
am__fastdepCXX_FALSE = @am__fastdepCXX_FALSE@
am__fastdepCXX_TRUE = @am__fastdepCXX_TRUE@
am__include = @am__include@
am__leading_dot = @am__leading_dot@
am__quote = @am__quote@
bindir = @bindir@
build_alias = @build_alias@
datadir = @datadir@
exec_prefix = @exec_prefix@
host_alias = @host_alias@
includedir = @includedir@
infodir = @infodir@
install_sh = @install_sh@
libdir = @libdir@
libexecdir = @libexecdir@
localstatedir = @localstatedir@
mandir = @mandir@
mkdir_p = @mkdir_p@
oldincludedir = @oldincludedir@
prefix = @prefix@
program_transform_name = @program_transform_name@
sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
sysconfdir = @sysconfdir@
target_alias = @target_alias@
MAINTAINERCLEANFILES = Makefile.in
cppbsl_SOURCES = \
cmdline.h \
cmdline.cc \
Parameters.h \
Parameters.cc \
Serial.h \
Serial.cc \
Bsl.h \
Bsl.cc \
cppbsl.cc
all: all-am
.SUFFIXES:
.SUFFIXES: .cc .o .obj
$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps)
@for dep in $?; do \
case '$(am__configure_deps)' in \
*$$dep*) \
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh \
&& exit 0; \
exit 1;; \
esac; \
done; \
echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu src/Makefile'; \
cd $(top_srcdir) && \
$(AUTOMAKE) --gnu src/Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
@case '$?' in \
*config.status*) \
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \
*) \
echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \
cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \
esac;
$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
$(top_srcdir)/configure: $(am__configure_deps)
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
$(ACLOCAL_M4): $(am__aclocal_m4_deps)
cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh
install-binPROGRAMS: $(bin_PROGRAMS)
@$(NORMAL_INSTALL)
test -z "$(bindir)" || $(mkdir_p) "$(DESTDIR)$(bindir)"
@list='$(bin_PROGRAMS)'; for p in $$list; do \
p1=`echo $$p|sed 's/$(EXEEXT)$$//'`; \
if test -f $$p \
; then \
f=`echo "$$p1" | sed 's,^.*/,,;$(transform);s/$$/$(EXEEXT)/'`; \
echo " $(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) '$$p' '$(DESTDIR)$(bindir)/$$f'"; \
$(INSTALL_PROGRAM_ENV) $(binPROGRAMS_INSTALL) "$$p" "$(DESTDIR)$(bindir)/$$f" || exit 1; \
else :; fi; \
done
uninstall-binPROGRAMS:
@$(NORMAL_UNINSTALL)
@list='$(bin_PROGRAMS)'; for p in $$list; do \
f=`echo "$$p" | sed 's,^.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/'`; \
echo " rm -f '$(DESTDIR)$(bindir)/$$f'"; \
rm -f "$(DESTDIR)$(bindir)/$$f"; \
done
clean-binPROGRAMS:
-test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)
cppbsl$(EXEEXT): $(cppbsl_OBJECTS) $(cppbsl_DEPENDENCIES)
@rm -f cppbsl$(EXEEXT)
$(CXXLINK) $(cppbsl_LDFLAGS) $(cppbsl_OBJECTS) $(cppbsl_LDADD) $(LIBS)
mostlyclean-compile:
-rm -f *.$(OBJEXT)
distclean-compile:
-rm -f *.tab.c
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Bsl.Po at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Parameters.Po at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/Serial.Po at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/cmdline.Po at am__quote@
@AMDEP_TRUE@@am__include@ @am__quote at ./$(DEPDIR)/cppbsl.Po at am__quote@
.cc.o:
@am__fastdepCXX_TRUE@ if $(CXXCOMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ $<; \
@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ $<
.cc.obj:
@am__fastdepCXX_TRUE@ if $(CXXCOMPILE) -MT $@ -MD -MP -MF "$(DEPDIR)/$*.Tpo" -c -o $@ `$(CYGPATH_W) '$<'`; \
@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; fi
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='$<' object='$@' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ depfile='$(DEPDIR)/$*.Po' tmpdepfile='$(DEPDIR)/$*.TPo' @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXXCOMPILE) -c -o $@ `$(CYGPATH_W) '$<'`
uninstall-info-am:
ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
unique=`for i in $$list; do \
if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
done | \
$(AWK) ' { files[$$0] = 1; } \
END { for (i in files) print i; }'`; \
mkid -fID $$unique
tags: TAGS
TAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
$(TAGS_FILES) $(LISP)
tags=; \
here=`pwd`; \
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
unique=`for i in $$list; do \
if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
done | \
$(AWK) ' { files[$$0] = 1; } \
END { for (i in files) print i; }'`; \
test -z "$(ETAGS_ARGS)$$tags$$unique" \
|| $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \
$$tags $$unique
ctags: CTAGS
CTAGS: $(HEADERS) $(SOURCES) $(TAGS_DEPENDENCIES) \
$(TAGS_FILES) $(LISP)
tags=; \
here=`pwd`; \
list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \
unique=`for i in $$list; do \
if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \
done | \
$(AWK) ' { files[$$0] = 1; } \
END { for (i in files) print i; }'`; \
test -z "$(CTAGS_ARGS)$$tags$$unique" \
|| $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \
$$tags $$unique
GTAGS:
here=`$(am__cd) $(top_builddir) && pwd` \
&& cd $(top_srcdir) \
&& gtags -i $(GTAGS_ARGS) $$here
distclean-tags:
-rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags
distdir: $(DISTFILES)
@srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; \
topsrcdirstrip=`echo "$(top_srcdir)" | sed 's|.|.|g'`; \
list='$(DISTFILES)'; for file in $$list; do \
case $$file in \
$(srcdir)/*) file=`echo "$$file" | sed "s|^$$srcdirstrip/||"`;; \
$(top_srcdir)/*) file=`echo "$$file" | sed "s|^$$topsrcdirstrip/|$(top_builddir)/|"`;; \
esac; \
if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \
dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \
if test "$$dir" != "$$file" && test "$$dir" != "."; then \
dir="/$$dir"; \
$(mkdir_p) "$(distdir)$$dir"; \
else \
dir=''; \
fi; \
if test -d $$d/$$file; then \
if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \
cp -pR $(srcdir)/$$file $(distdir)$$dir || exit 1; \
fi; \
cp -pR $$d/$$file $(distdir)$$dir || exit 1; \
else \
test -f $(distdir)/$$file \
|| cp -p $$d/$$file $(distdir)/$$file \
|| exit 1; \
fi; \
done
check-am: all-am
check: check-am
all-am: Makefile $(PROGRAMS)
installdirs:
for dir in "$(DESTDIR)$(bindir)"; do \
test -z "$$dir" || $(mkdir_p) "$$dir"; \
done
install: install-am
install-exec: install-exec-am
install-data: install-data-am
uninstall: uninstall-am
install-am: all-am
@$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am
installcheck: installcheck-am
install-strip:
$(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \
install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \
`test -z '$(STRIP)' || \
echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
mostlyclean-generic:
clean-generic:
distclean-generic:
-rm -f $(CONFIG_CLEAN_FILES)
maintainer-clean-generic:
@echo "This command is intended for maintainers to use"
@echo "it deletes files that may require special tools to rebuild."
-test -z "$(MAINTAINERCLEANFILES)" || rm -f $(MAINTAINERCLEANFILES)
clean: clean-am
clean-am: clean-binPROGRAMS clean-generic mostlyclean-am
distclean: distclean-am
-rm -rf ./$(DEPDIR)
-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic \
distclean-tags
dvi: dvi-am
dvi-am:
html: html-am
info: info-am
info-am:
install-data-am:
install-exec-am: install-binPROGRAMS
install-info: install-info-am
install-man:
installcheck-am:
maintainer-clean: maintainer-clean-am
-rm -rf ./$(DEPDIR)
-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic
mostlyclean: mostlyclean-am
mostlyclean-am: mostlyclean-compile mostlyclean-generic
pdf: pdf-am
pdf-am:
ps: ps-am
ps-am:
uninstall-am: uninstall-binPROGRAMS uninstall-info-am
.PHONY: CTAGS GTAGS all all-am check check-am clean clean-binPROGRAMS \
clean-generic ctags distclean distclean-compile \
distclean-generic distclean-tags distdir dvi dvi-am html \
html-am info info-am install install-am install-binPROGRAMS \
install-data install-data-am install-exec install-exec-am \
install-info install-info-am install-man install-strip \
installcheck installcheck-am installdirs maintainer-clean \
maintainer-clean-generic mostlyclean mostlyclean-compile \
mostlyclean-generic pdf pdf-am ps ps-am tags uninstall \
uninstall-am uninstall-binPROGRAMS uninstall-info-am
# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:
--- NEW FILE: Parameters.cc ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include "cmdline.h"
#include "Parameters.h"
using namespace std;
Parameters::Parameters(int argc, char **argv) {
action = NONE;
gengetopt_args_info args_info;
cmdline_parser_init(&args_info);
if(cmdline_parser(argc, argv, &args_info) != 0) {
exit(1);
}
if(args_info.invert_test_given) {
invertTest = true;
} else {
invertTest = false;
}
if(args_info.invert_reset_given) {
invertReset = true;
} else {
invertReset = false;
}
if(args_info.debug_given) {
verbose = true;
} else {
verbose = false;
}
if((args_info.erase_given) && (action < ERASE)) {
action = ERASE;
}
if((args_info.reset_given) && (action < RESET)) {
action = RESET;
}
if(args_info.program_given) {
action = FLASH;
img = args_info.program_arg;
}
if(args_info.comport_given) {
dev = args_info.comport_arg;
}
if(args_info.telosb_given || args_info.tmote_given) {
telosb = true;
invertReset = false;
invertTest = false;
}
cmdline_parser_free(&args_info);
};
--- NEW FILE: Parameters.h ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* keep parameters for bsl tool
* @author Andreas Koepke <koepke at tkn.tu-berlin.de>
* @date 2007-10-23
*/
#ifndef _PARAMETERS_H
#define _PARAMETERS_H
#include <string>
class Parameters
{
public:
enum actions_t {
NONE,
RESET,
ERASE,
FLASH
};
public:
const char *device;
std::string dev;
const char *image;
std::string img;
bool verbose;
bool invertTest;
bool invertReset;
bool intelhex;
bool telosb;
actions_t action;
public:
Parameters(int argc, char **argv);
};
extern Parameters *parameters;
#endif
--- NEW FILE: Serial.cc ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* hand rolled bsl tool, other ones are too slow
* @author Andreas Koepke <koepke at tkn.tu-berlin.de>
* @date 2007-04-16
*/
#include <stdio.h>
#include <iostream>
#include "Serial.h"
using namespace std;
int serial_connect(int* err, const char* dev, int* readFD, int* writeFD, termios* pt)
{
struct termios my_tios;
struct serial_struct serinfo;
int r = 0;
*readFD = -1;
*writeFD = -1;
for(int i = 0; i < 3; i++) {
*readFD = open(dev, O_RDONLY | O_NOCTTY | O_NONBLOCK);
*err = errno;
if(*readFD != -1) {
break;
}
else if((*readFD == -1) && (errno == EAGAIN)) {
serial_delay(1000000);
}
else {
return -1;
}
}
if(*readFD == -1) {
return -1;
}
for(int i = 0; i < 3; i++) {
*writeFD = open(dev, O_WRONLY | O_NOCTTY);
*err = errno;
if(*writeFD != -1) {
break;
}
else if((*writeFD == -1) && (errno == EAGAIN)) {
serial_delay(1000000);
}
else {
close(*readFD);
*readFD = -1;
return -1;
}
}
if(*writeFD == -1) {
close(*readFD);
*readFD = -1;
return -1;
}
/* prepare attributes */
r = tcgetattr(*writeFD, &my_tios);
if(r == -1) {
*err = errno;
close(*readFD);
close(*writeFD);
return -1;
}
*pt = my_tios;
cfmakeraw(&my_tios);
my_tios.c_iflag |= IGNBRK | INPCK;
my_tios.c_cflag |= (CS8 | CLOCAL | CREAD | PARENB);
cfsetispeed(&my_tios, B38400); // dummy
cfsetospeed(&my_tios, B38400); // dummy
r = tcsetattr(*readFD, TCSANOW, &my_tios);
if(r == -1) {
*err = errno;
r = tcsetattr(*writeFD, TCSANOW, pt);
close(*readFD);
close(*writeFD);
return -1;
}
/* hack for baudrate */
r = ioctl(*writeFD, TIOCGSERIAL, &serinfo);
if(r == -1) {
*err = errno;
r = tcsetattr(*writeFD, TCSANOW, pt);
close(*readFD);
close(*writeFD);
return -1;
}
serinfo.custom_divisor = serinfo.baud_base / 9600;
if(serinfo.custom_divisor == 0) serinfo.custom_divisor = 1;
serinfo.flags &= ~ASYNC_SPD_MASK;
serinfo.flags |= ASYNC_SPD_CUST;
r = ioctl(*writeFD, TIOCSSERIAL, &serinfo);
if(r == -1) {
*err = errno;
r = tcsetattr(*writeFD, TCSANOW, pt);
close(*readFD);
close(*writeFD);
return -1;
}
// clear buffers
r = tcflush(*writeFD, TCIOFLUSH);
if(r == -1) {
*err = errno;
r = tcsetattr(*writeFD, TCSANOW, pt);
close(*readFD);
close(*writeFD);
return -1;
}
if(r == -1) {
*err = errno;
r = tcsetattr(*writeFD, TCSANOW, pt);
close(*readFD);
close(*writeFD);
}
return r;
};
int BaseSerial::resetPins(int *err) {
int r = 0;
r = setRST(err);
r = clrTEST(err);
return r;
}
int BaseSerial::disconnect(int *err) {
int r;
if(serialWriteFD != -1) {
r = resetPins(err);
if(r == -1) {
cerr << "WARN: BaseSerial::disconnect could not reset pins, " << strerror(*err) << endl;
}
r = tcsetattr(serialWriteFD, TCSANOW, &oldtermios);
}
if(serialReadFD != -1) {
r = close(serialReadFD);
if(r == -1) {
*err = errno;
}
serialReadFD = -1;
}
if(serialWriteFD != -1) {
r = close(serialWriteFD);
if(r == -1) {
*err = errno;
}
serialWriteFD = -1;
}
return r;
}
int BaseSerial::reset(int *err) {
int r = 0;
r = setRST(err);
if(r == -1) return -1;
r = setTEST(err);
if(r == -1) return -1;
serial_delay(2500);
r = clrRST(err);
if(r == -1) return -1;
serial_delay(10);
r = setRST(err);
if(r == -1) return -1;
serial_delay(2500);
cout << "Reset device ..." << endl;
return clearBuffers(err);
};
int BaseSerial::invokeBsl(int *err) {
int r = 0;
r = setRST(err);
if(r == -1) return -1;
r = setTEST(err);
if(r == -1) return -1;
serial_delay(2500);
r = clrRST(err);
if(r == -1) return -1;
r = setTEST(err);
if(r == -1) return -1;
serial_delay(10);
r = clrTEST(err);
if(r == -1) return -1;
serial_delay(10);
r = setTEST(err);
if(r == -1) return -1;
serial_delay(10);
r = clrTEST(err);
if(r == -1) return -1;
serial_delay(10);
r = setRST(err);
if(r == -1) return -1;
serial_delay(10);
r = setTEST(err);
if(r == -1) return -1;
serial_delay(2500);
cout << "Invoking BSL..." << endl;
return clearBuffers(err);
}
int BaseSerial::readFD(int *err, char *buffer, int count, int maxCount) {
int cnt = 0;
int retries = 0;
timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
while(cnt == 0) {
int tmpCnt = read(serialReadFD, buffer, maxCount);
*err = errno;
if((tmpCnt == 0) || ((tmpCnt < 0) && (errno == EAGAIN))) {
FD_SET(serialReadFD, &rfds);
if(select(serialReadFD + 1, &rfds, NULL, NULL, &tv) < 0) {
*err = errno;
return -1;
}
FD_CLR(serialReadFD, &rfds);
if(retries++ >= 3) {
cerr << "FATAL: BaseSerial::readFD no data available after 3s" << endl;
return -1;
}
}
else if(tmpCnt > 0) {
cnt += tmpCnt;
}
else {
return -1;
}
}
return cnt;
}
int BaseSerial::txrx(int *err, frame_t *txframe, frame_t *rxframe) {
int r = 0;
char sync = SYNC;
uint8_t ack = 0;
if((txframe == NULL) || (txframe->L1 < 4) || ((txframe->L1 & 1) != 0) || (rxframe == NULL)) {
cerr << "BaseSerial::txrx: precondition not fulfilled, "
<< " txFrame: " << txframe
<< " rxFrame: " << rxframe
<< " txframe->L1: " << (unsigned) txframe->L1
<< endl;
return -1;
}
for(unsigned i = 0; i < 3; i++) {
r = write(serialWriteFD,&sync, 1);
if(r == -1) {
*err = errno;
if(errno != EAGAIN) return -1;
}
r = readFD(err, (char *)(&ack),1,1);
if(r == 1) {
if(ack == DATA_ACK) {
r = 0;
break;
}
else {
cerr << "WARN: BaseSerial::txrx: received " << hex << (unsigned) ack
<< " when trying to sync with node." << dec << endl;
}
}
else {
if((r == -1) && (errno == EAGAIN)) {
// retry to sync
}
else {
cerr << "FATAL: BaseSerial::txrx could not SYNC with node" << endl;
return -1;
}
}
}
if(r == -1) {
return -1;
}
r = clearBuffers(err);
if(r == -1) return r;
// transmit frame
checksum(txframe);
r = write(serialWriteFD, (char *)txframe, txframe->L1 + 6);
if(r < txframe->L1 + 6) {
*err = errno;
return -1;
}
// receive response
int len = 0;
rxframe->L1 = 4;
r = 0;
while(r >= 0) {
r = readFD(err, (char *)rxframe, sizeof(frame_t), sizeof(frame_t));
if(r == -1) {
return -1;
}
else if(r >= 1) {
len += r;
if(rxframe->HDR == DATA_ACK) {
break;
}
else if(rxframe->HDR == DATA_NAK) {
cerr << "BaseSerial::txrx frame not valid, command "
<< hex << (unsigned) txframe->CMD << dec
<< " not defined or not allowed" << endl;
return -1;
}
else if(rxframe->HDR == SYNC) {
if(len >= rxframe->L1 + 6) {
break;
}
}
else {
cerr << "FATAL: BaseSerial::txrx: received "
<< hex << (unsigned) rxframe->HDR
<< " when trying to execute " << hex << (unsigned) txframe->CMD << dec << endl;
break;
}
}
}
return r;
}
int BaseSerial::highSpeed(int *err) {
struct serial_struct serinfo;
int r = ioctl(serialWriteFD, TIOCGSERIAL, &serinfo);
if(r == -1) {
*err = errno;
return -1;
}
serinfo.custom_divisor = serinfo.baud_base / 38400;
if(serinfo.custom_divisor == 0) serinfo.custom_divisor = 1;
serinfo.flags &= ~ASYNC_SPD_MASK;
serinfo.flags |= ASYNC_SPD_CUST;
r = ioctl(serialWriteFD, TIOCSSERIAL, &serinfo);
if(r == -1) {
*err = errno;
return -1;
}
return r;
}
int TelosBSerial::reset(int *err) {
int r;
r = telosI2CWriteCmd(err, 0, 0);
if(r == -1) return r;
serial_delay(10000);
r = telosI2CWriteCmd(err, 0, 3);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 2);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 0);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 0);
if(r == -1) return r;
serial_delay(2500);
cout << "Reset device ..." << endl;
return clearBuffers(err);
}
int TelosBSerial::invokeBsl(int *err) {
int r;
r = telosI2CWriteCmd(err, 0, 0);
if(r == -1) return r;
serial_delay(10000);
r = telosI2CWriteCmd(err, 0, 1);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 3);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 1);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 3);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 2);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 0);
if(r == -1) return r;
r = telosI2CWriteCmd(err, 0, 0);
if(r == -1) return r;
serial_delay(2500);
cout << "Invoking BSL..." << endl;
return clearBuffers(err);
}
int TelosBSerial::telosI2CStart(int *err) {
int r;
r = telosSetSDA(err);
if(r == -1) return -1;
r = telosSetSCL(err);
if(r == -1) return -1;
r = telosClrSDA(err);
return r;
}
int TelosBSerial::telosI2CStop(int *err) {
int r;
r = telosClrSDA(err);
if(r == -1) return r;
r = telosSetSCL(err);
if(r == -1) return r;
r = telosSetSDA(err);
return r;
}
int TelosBSerial::telosI2CWriteBit(int *err, bool bit) {
int r = telosClrSCL(err);
if(r == -1) return r;
if(bit) {
r = telosSetSDA(err);
if(r == -1) return r;
} else {
r = telosClrSDA(err);
if(r == -1) return r;
}
r = telosSetSCL(err);
if(r == -1) return r;
r = telosClrSCL(err);
return r;
}
int TelosBSerial::telosI2CWriteByte(int *err, uint8_t byte) {
int r;
r = telosI2CWriteBit(err, byte & 0x80 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x40 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x20 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x10 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x08 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x04 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x02 );
if(r == -1) return r;
r = telosI2CWriteBit(err, byte & 0x01 );
if(r == -1) return r;
return telosI2CWriteBit(err, 0 );
}
int TelosBSerial::telosI2CWriteCmd(int *err, uint8_t addr, uint8_t cmdbyte) {
int r;
r = telosI2CStart(err);
if(r == -1) return r;
r = telosI2CWriteByte(err, 0x90 | (addr << 1) );
if(r == -1) return r;
r = telosI2CWriteByte(err, cmdbyte );
if(r == -1) return r;
return telosI2CStop(err);
}
int TelosBSerial::resetPins(int *err) {
int r = 0;
r = setRTS(err);
r = clrDTR(err);
return r;
}
--- NEW FILE: Serial.h ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* hand rolled bsl tool, other ones are too slow
* @author Andreas Koepke <koepke at tkn.tu-berlin.de>
* @date 2007-04-16
*/
#ifndef BSL_SERIAL_H
#define BSL_SERIAL_H
#include <string>
#include <inttypes.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include <linux/serial.h>
#include "Parameters.h"
inline void serial_delay(unsigned usec) {
struct timeval tv;
tv.tv_sec = usec/1000000;
tv.tv_usec = usec%1000000;
select(0,NULL,NULL,NULL, &tv);
};
struct frame_t {
uint8_t HDR;
uint8_t CMD;
uint8_t L1;
uint8_t L2;
uint8_t AL;
uint8_t AH;
uint8_t LL;
uint8_t LH;
uint8_t data[252];
} __attribute__ ((packed));
/**
* Connect with serial device (dev), returns the opened file descriptors in *
* readFD and writeFD. Returns on error with something != 0 and errno is *
* hopefully set correctly.
*/
int serial_connect(int* err, const char* dev, int* readFD, int* writeFD, termios* pt);
class BaseSerial {
protected:
const int switchdelay;
termios oldtermios;
protected:
int serialReadFD;
int serialWriteFD;
bool invertTest;
bool invertReset;
bool swapRstTest;
fd_set rfds;
enum {
CMD_FAILED = 0x70,
SYNC = 0x80,
DATA_ACK = 0x90,
DATA_NAK = 0xA0,
};
protected:
inline int setDTR(int *err) {
int i = TIOCM_DTR;
int r = ioctl(serialWriteFD, TIOCMBIS, &i);
if(r == -1) {
*err = errno;
std::cerr << "ERROR: BaseSerial::setDTR could not set DTR pin" << std::endl;
}
else {
serial_delay(switchdelay);
}
return r;
}
inline int clrDTR(int *err) {
int i = TIOCM_DTR;
int r = ioctl(serialWriteFD, TIOCMBIC, &i);
if(r == -1) {
*err = errno;
std::cerr << "ERROR: BaseSerial::clrDTR could not clr DTR pin" << std::endl;
}
else {
serial_delay(switchdelay);
}
return r;
}
inline int setRTS(int *err) {
int i = TIOCM_RTS;
int r = ioctl(serialWriteFD, TIOCMBIS, &i);
if(r == -1) {
*err = errno;
std::cerr << "ERROR: BaseSerial::setRTS could not set RTS pin" << std::endl;
}
else {
serial_delay(switchdelay);
}
return r;
}
inline int clrRTS(int *err) {
int i = TIOCM_RTS;
int r = ioctl(serialWriteFD, TIOCMBIC, &i);
if(r == -1) {
*err = errno;
std::cerr << "ERROR: BaseSerial::clrRTS could not clr RTS pin" << std::endl;
}
else {
serial_delay(switchdelay);
}
return r;
}
int setTEST(int *err) {
int r;
if(invertTest) { r = clrRTS(err); } else { r = setRTS(err); }
return r;
}
int clrTEST(int *err) {
int r;
if(invertTest) { r = setRTS(err); } else { r = clrRTS(err); }
return r;
}
int setRST(int *err) {
int r;
if(invertReset) { r = clrDTR(err); } else { r = setDTR(err); }
return r;
}
int clrRST(int *err) {
int r;
if(invertReset) { r= setDTR(err); } else { r = clrDTR(err); }
return r;
}
inline void checksum(frame_t *frame) {
uint8_t i;
uint8_t frameLen = frame->L1/2 + 2;
uint16_t *dat = (uint16_t *)frame;
uint16_t check = 0;
for(i = 0; i < frameLen; i++) {
check ^= dat[i];
}
dat[i] = ~check;
}
int readFD(int *err, char *buffer, int count, int maxCount);
virtual int resetPins(int *err);
public:
BaseSerial(const termios& term, int rFD, int wFD, bool T=false, bool R=false) :
switchdelay(10),
oldtermios(term),
serialReadFD(rFD), serialWriteFD(wFD),
invertTest(T), invertReset(R) {
int err;
FD_ZERO(&rfds);
setRST(&err);
setTEST(&err);
}
virtual ~BaseSerial() {
int r;
int err;
if((serialReadFD != -1) || (serialWriteFD != -1)) {
r = disconnect(&err);
}
}
// communicate
inline int clearBuffers(int *err) {
int r = tcflush(serialReadFD, TCIOFLUSH);
if(r != 0) {
*err = errno;
}
else {
r = tcflush(serialWriteFD, TCIOFLUSH);
if(r != 0) {
*err = errno;
}
}
return r;
};
int txrx(int *err, frame_t *txframe, frame_t *rxframe);
// handle connection
int disconnect(int *err);
// change connection speed
int highSpeed(int *err);
// do initial magic on serial interface
virtual int reset(int *err);
virtual int invokeBsl(int *err);
};
class TelosBSerial : public BaseSerial {
protected:
virtual int resetPins(int *err);
int telosSetSCL(int *err) {
return clrRTS(err);
}
int telosClrSCL(int *err) {
return setRTS(err);
}
int telosSetSDA(int *err) {
return clrDTR(err);
}
int telosClrSDA(int *err) {
return setDTR(err);
}
int telosI2CStart(int *err);
int telosI2CStop(int *err);
int telosI2CWriteBit(int *err, bool bit);
int telosI2CWriteByte(int* err, uint8_t byte);
int telosI2CWriteCmd(int*err, uint8_t addr, uint8_t cmdbyte);
public:
TelosBSerial(const termios& term, int rFD, int wFD, bool T=false, bool R=false) :
BaseSerial(term, rFD, wFD, T, R) {
}
virtual ~TelosBSerial() {
}
virtual int reset(int *err);
virtual int invokeBsl(int *err);
};
#endif
--- NEW FILE: cmdline.cc ---
/*
File autogenerated by gengetopt version 2.21
generated with the following command:
./src/gengetopt -i sample2.ggo
The developers of gengetopt consider the fixed text that goes in all
gengetopt output files to be in the public domain:
we make no copyright claims on it.
*/
/* If we use autoconf. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
#include "cmdline.h"
const char *gengetopt_args_info_purpose = "this is just a test program for gengetopt";
const char *gengetopt_args_info_usage = "Usage: " CMDLINE_PARSER_PACKAGE " [OPTIONS]...";
const char *gengetopt_args_info_description = "";
const char *gengetopt_args_info_help[] = {
" -h, --help Print help and exit",
" -V, --version Print version and exit",
" -T, --invert-test TEST pin is inverted (default=off)",
" -R, --invert-reset RESET pin is inverted (default=off)",
" --telosb Assume a TelosB node (default=off)",
" --tmote Assume a Tmote node (default=off)",
" -v, --debug be verbose for debug purposes (default=off)",
" -1, --f1x Specify CPU family (default=off)",
" -I, --intelhex force fileformat to be IntelHex (default=off)",
" -e, --erase erase device (default=off)",
" -r, --reset reset device (default=off)",
" -p, --program=STRING program file",
" -c, --comport=STRING communicate using this device",
0
};
static
void clear_given (struct gengetopt_args_info *args_info);
static
void clear_args (struct gengetopt_args_info *args_info);
static int
cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
struct cmdline_parser_params *params, const char *additional_error);
static int
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
static char *
gengetopt_strdup (const char *s);
static
void clear_given (struct gengetopt_args_info *args_info)
{
args_info->help_given = 0 ;
args_info->version_given = 0 ;
args_info->invert_test_given = 0 ;
args_info->invert_reset_given = 0 ;
args_info->telosb_given = 0 ;
args_info->tmote_given = 0 ;
args_info->debug_given = 0 ;
args_info->f1x_given = 0 ;
args_info->intelhex_given = 0 ;
args_info->erase_given = 0 ;
args_info->reset_given = 0 ;
args_info->program_given = 0 ;
args_info->comport_given = 0 ;
}
static
void clear_args (struct gengetopt_args_info *args_info)
{
args_info->invert_test_flag = 0;
args_info->invert_reset_flag = 0;
args_info->telosb_flag = 0;
args_info->tmote_flag = 0;
args_info->debug_flag = 0;
args_info->f1x_flag = 0;
args_info->intelhex_flag = 0;
args_info->erase_flag = 0;
args_info->reset_flag = 0;
args_info->program_arg = NULL;
args_info->program_orig = NULL;
args_info->comport_arg = NULL;
args_info->comport_orig = NULL;
}
static
void init_args_info(struct gengetopt_args_info *args_info)
{
args_info->help_help = gengetopt_args_info_help[0] ;
args_info->version_help = gengetopt_args_info_help[1] ;
args_info->invert_test_help = gengetopt_args_info_help[2] ;
args_info->invert_reset_help = gengetopt_args_info_help[3] ;
args_info->telosb_help = gengetopt_args_info_help[4] ;
args_info->tmote_help = gengetopt_args_info_help[5] ;
args_info->debug_help = gengetopt_args_info_help[6] ;
args_info->f1x_help = gengetopt_args_info_help[7] ;
args_info->intelhex_help = gengetopt_args_info_help[8] ;
args_info->erase_help = gengetopt_args_info_help[9] ;
args_info->reset_help = gengetopt_args_info_help[10] ;
args_info->program_help = gengetopt_args_info_help[11] ;
args_info->comport_help = gengetopt_args_info_help[12] ;
}
void
cmdline_parser_print_version (void)
{
printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
}
void
cmdline_parser_print_help (void)
{
int i = 0;
cmdline_parser_print_version ();
if (strlen(gengetopt_args_info_purpose) > 0)
printf("\n%s\n", gengetopt_args_info_purpose);
printf("\n%s\n\n", gengetopt_args_info_usage);
if (strlen(gengetopt_args_info_description) > 0)
printf("%s\n", gengetopt_args_info_description);
while (gengetopt_args_info_help[i])
printf("%s\n", gengetopt_args_info_help[i++]);
}
void
cmdline_parser_init (struct gengetopt_args_info *args_info)
{
clear_given (args_info);
clear_args (args_info);
init_args_info (args_info);
}
struct cmdline_parser_params *
cmdline_parser_params_init()
{
struct cmdline_parser_params *params =
(struct cmdline_parser_params *)malloc(sizeof(struct cmdline_parser_params));
if (params)
{
params->override = 0;
params->initialize = 0;
params->check_required = 0;
params->check_ambiguity = 0;
}
return params;
}
static void
cmdline_parser_release (struct gengetopt_args_info *args_info)
{
if (args_info->program_arg)
{
free (args_info->program_arg); /* free previous argument */
args_info->program_arg = 0;
}
if (args_info->program_orig)
{
free (args_info->program_orig); /* free previous argument */
args_info->program_orig = 0;
}
if (args_info->comport_arg)
{
free (args_info->comport_arg); /* free previous argument */
args_info->comport_arg = 0;
}
if (args_info->comport_orig)
{
free (args_info->comport_orig); /* free previous argument */
args_info->comport_orig = 0;
}
clear_given (args_info);
}
int
cmdline_parser_file_save(const char *filename, struct gengetopt_args_info *args_info)
{
FILE *outfile;
int i = 0;
outfile = fopen(filename, "w");
if (!outfile)
{
fprintf (stderr, "%s: cannot open file for writing: %s\n", CMDLINE_PARSER_PACKAGE, filename);
return EXIT_FAILURE;
}
if (args_info->help_given) {
fprintf(outfile, "%s\n", "help");
}
if (args_info->version_given) {
fprintf(outfile, "%s\n", "version");
}
if (args_info->invert_test_given) {
fprintf(outfile, "%s\n", "invert-test");
}
if (args_info->invert_reset_given) {
fprintf(outfile, "%s\n", "invert-reset");
}
if (args_info->telosb_given) {
fprintf(outfile, "%s\n", "telosb");
}
if (args_info->tmote_given) {
fprintf(outfile, "%s\n", "tmote");
}
if (args_info->debug_given) {
fprintf(outfile, "%s\n", "debug");
}
if (args_info->f1x_given) {
fprintf(outfile, "%s\n", "f1x");
}
if (args_info->intelhex_given) {
fprintf(outfile, "%s\n", "intelhex");
}
if (args_info->erase_given) {
fprintf(outfile, "%s\n", "erase");
}
if (args_info->reset_given) {
fprintf(outfile, "%s\n", "reset");
}
if (args_info->program_given) {
if (args_info->program_orig) {
fprintf(outfile, "%s=\"%s\"\n", "program", args_info->program_orig);
} else {
fprintf(outfile, "%s\n", "program");
}
}
if (args_info->comport_given) {
if (args_info->comport_orig) {
fprintf(outfile, "%s=\"%s\"\n", "comport", args_info->comport_orig);
} else {
fprintf(outfile, "%s\n", "comport");
}
}
fclose (outfile);
i = EXIT_SUCCESS;
return i;
}
void
cmdline_parser_free (struct gengetopt_args_info *args_info)
{
cmdline_parser_release (args_info);
}
/* gengetopt_strdup() */
/* strdup.c replacement of strdup, which is not standard */
char *
gengetopt_strdup (const char *s)
{
char *result = NULL;
if (!s)
return result;
result = (char*)malloc(strlen(s) + 1);
if (result == (char*)0)
return (char*)0;
strcpy(result, s);
return result;
}
int
cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
{
return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
}
int
cmdline_parser_ext (int argc, char * const *argv, struct gengetopt_args_info *args_info,
struct cmdline_parser_params *params)
{
int result;
result = cmdline_parser_internal (argc, argv, args_info, params, NULL);
if (result == EXIT_FAILURE)
{
cmdline_parser_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
{
int result;
struct cmdline_parser_params params;
params.override = override;
params.initialize = initialize;
params.check_required = check_required;
params.check_ambiguity = 0;
result = cmdline_parser_internal (argc, argv, args_info, ¶ms, NULL);
if (result == EXIT_FAILURE)
{
cmdline_parser_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
{
int result = EXIT_SUCCESS;
if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
result = EXIT_FAILURE;
if (result == EXIT_FAILURE)
{
cmdline_parser_free (args_info);
exit (EXIT_FAILURE);
}
return result;
}
int
cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
{
int error = 0;
/* checks for required options */
if (! args_info->comport_given)
{
fprintf (stderr, "%s: '--comport' ('-c') option required%s\n", prog_name, (additional_error ? additional_error : ""));
error = 1;
}
/* checks for dependences among options */
return error;
}
int
cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info,
struct cmdline_parser_params *params, const char *additional_error)
{
int c; /* Character of the parsed option. */
int error = 0;
struct gengetopt_args_info local_args_info;
int override;
int initialize;
int check_required;
int check_ambiguity;
override = params->override;
initialize = params->initialize;
check_required = params->check_required;
check_ambiguity = params->check_ambiguity;
if (initialize)
cmdline_parser_init (args_info);
cmdline_parser_init (&local_args_info);
optarg = 0;
optind = 0;
opterr = 1;
optopt = '?';
while (1)
{
int option_index = 0;
char *stop_char;
static struct option long_options[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'V' },
{ "invert-test", 0, NULL, 'T' },
{ "invert-reset", 0, NULL, 'R' },
{ "telosb", 0, NULL, 0 },
{ "tmote", 0, NULL, 0 },
{ "debug", 0, NULL, 'v' },
{ "f1x", 0, NULL, '1' },
{ "intelhex", 0, NULL, 'I' },
{ "erase", 0, NULL, 'e' },
{ "reset", 0, NULL, 'r' },
{ "program", 1, NULL, 'p' },
{ "comport", 1, NULL, 'c' },
{ NULL, 0, NULL, 0 }
};
stop_char = 0;
c = getopt_long (argc, argv, "hVTRv1Ierp:c:", long_options, &option_index);
if (c == -1) break; /* Exit from `while (1)' loop. */
switch (c)
{
case 'h': /* Print help and exit. */
cmdline_parser_print_help ();
cmdline_parser_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'V': /* Print version and exit. */
cmdline_parser_print_version ();
cmdline_parser_free (&local_args_info);
exit (EXIT_SUCCESS);
case 'T': /* TEST pin is inverted. */
if (local_args_info.invert_test_given || (check_ambiguity && args_info->invert_test_given))
{
fprintf (stderr, "%s: `--invert-test' (`-T') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->invert_test_given && ! override)
continue;
local_args_info.invert_test_given = 1;
args_info->invert_test_given = 1;
args_info->invert_test_flag = !(args_info->invert_test_flag);
break;
case 'R': /* RESET pin is inverted. */
if (local_args_info.invert_reset_given || (check_ambiguity && args_info->invert_reset_given))
{
fprintf (stderr, "%s: `--invert-reset' (`-R') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->invert_reset_given && ! override)
continue;
local_args_info.invert_reset_given = 1;
args_info->invert_reset_given = 1;
args_info->invert_reset_flag = !(args_info->invert_reset_flag);
break;
case 'v': /* be verbose for debug purposes. */
if (local_args_info.debug_given || (check_ambiguity && args_info->debug_given))
{
fprintf (stderr, "%s: `--debug' (`-v') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->debug_given && ! override)
continue;
local_args_info.debug_given = 1;
args_info->debug_given = 1;
args_info->debug_flag = !(args_info->debug_flag);
break;
case '1': /* Specify CPU family. */
if (local_args_info.f1x_given || (check_ambiguity && args_info->f1x_given))
{
fprintf (stderr, "%s: `--f1x' (`-1') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->f1x_given && ! override)
continue;
local_args_info.f1x_given = 1;
args_info->f1x_given = 1;
args_info->f1x_flag = !(args_info->f1x_flag);
break;
case 'I': /* force fileformat to be IntelHex. */
if (local_args_info.intelhex_given || (check_ambiguity && args_info->intelhex_given))
{
fprintf (stderr, "%s: `--intelhex' (`-I') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->intelhex_given && ! override)
continue;
local_args_info.intelhex_given = 1;
args_info->intelhex_given = 1;
args_info->intelhex_flag = !(args_info->intelhex_flag);
break;
case 'e': /* erase device. */
if (local_args_info.erase_given || (check_ambiguity && args_info->erase_given))
{
fprintf (stderr, "%s: `--erase' (`-e') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->erase_given && ! override)
continue;
local_args_info.erase_given = 1;
args_info->erase_given = 1;
args_info->erase_flag = !(args_info->erase_flag);
break;
case 'r': /* reset device. */
if (local_args_info.reset_given || (check_ambiguity && args_info->reset_given))
{
fprintf (stderr, "%s: `--reset' (`-r') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->reset_given && ! override)
continue;
local_args_info.reset_given = 1;
args_info->reset_given = 1;
args_info->reset_flag = !(args_info->reset_flag);
break;
case 'p': /* program file. */
if (local_args_info.program_given || (check_ambiguity && args_info->program_given))
{
fprintf (stderr, "%s: `--program' (`-p') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->program_given && ! override)
continue;
local_args_info.program_given = 1;
args_info->program_given = 1;
if (args_info->program_arg)
free (args_info->program_arg); /* free previous string */
args_info->program_arg = gengetopt_strdup (optarg);
if (args_info->program_orig)
free (args_info->program_orig); /* free previous string */
args_info->program_orig = gengetopt_strdup (optarg);
break;
case 'c': /* communicate using this device. */
if (local_args_info.comport_given || (check_ambiguity && args_info->comport_given))
{
fprintf (stderr, "%s: `--comport' (`-c') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->comport_given && ! override)
continue;
local_args_info.comport_given = 1;
args_info->comport_given = 1;
if (args_info->comport_arg)
free (args_info->comport_arg); /* free previous string */
args_info->comport_arg = gengetopt_strdup (optarg);
if (args_info->comport_orig)
free (args_info->comport_orig); /* free previous string */
args_info->comport_orig = gengetopt_strdup (optarg);
break;
case 0: /* Long option with no short option */
/* Assume a TelosB node. */
if (strcmp (long_options[option_index].name, "telosb") == 0)
{
if (local_args_info.telosb_given || (check_ambiguity && args_info->telosb_given))
{
fprintf (stderr, "%s: `--telosb' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->telosb_given && ! override)
continue;
local_args_info.telosb_given = 1;
args_info->telosb_given = 1;
args_info->telosb_flag = !(args_info->telosb_flag);
}
/* Assume a Tmote node. */
else if (strcmp (long_options[option_index].name, "tmote") == 0)
{
if (local_args_info.tmote_given || (check_ambiguity && args_info->tmote_given))
{
fprintf (stderr, "%s: `--tmote' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
goto failure;
}
if (args_info->tmote_given && ! override)
continue;
local_args_info.tmote_given = 1;
args_info->tmote_given = 1;
args_info->tmote_flag = !(args_info->tmote_flag);
}
break;
case '?': /* Invalid option. */
/* `getopt_long' already printed an error message. */
goto failure;
default: /* bug: option not considered. */
fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
abort ();
} /* switch */
} /* while */
if (check_required)
{
error += cmdline_parser_required2 (args_info, argv[0], additional_error);
}
cmdline_parser_release (&local_args_info);
if ( error )
return (EXIT_FAILURE);
return 0;
failure:
cmdline_parser_release (&local_args_info);
return (EXIT_FAILURE);
}
--- NEW FILE: cmdline.h ---
/** @file cmdline.h
* @brief The header file for the command line option parser
* generated by GNU Gengetopt version 2.21
* http://www.gnu.org/software/gengetopt.
* DO NOT modify this file, since it can be overwritten
* @author GNU Gengetopt by Lorenzo Bettini */
#ifndef CMDLINE_H
#define CMDLINE_H
/* If we use autoconf. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifndef CMDLINE_PARSER_PACKAGE
/** @brief the program name */
#define CMDLINE_PARSER_PACKAGE PACKAGE
#endif
#ifndef CMDLINE_PARSER_VERSION
/** @brief the program version */
#define CMDLINE_PARSER_VERSION VERSION
#endif
/** @brief Where the command line options are stored */
struct gengetopt_args_info
{
const char *help_help; /**< @brief Print help and exit help description. */
const char *version_help; /**< @brief Print version and exit help description. */
int invert_test_flag; /**< @brief TEST pin is inverted (default=off). */
const char *invert_test_help; /**< @brief TEST pin is inverted help description. */
int invert_reset_flag; /**< @brief RESET pin is inverted (default=off). */
const char *invert_reset_help; /**< @brief RESET pin is inverted help description. */
int telosb_flag; /**< @brief Assume a TelosB node (default=off). */
const char *telosb_help; /**< @brief Assume a TelosB node help description. */
int tmote_flag; /**< @brief Assume a Tmote node (default=off). */
const char *tmote_help; /**< @brief Assume a Tmote node help description. */
int debug_flag; /**< @brief be verbose for debug purposes (default=off). */
const char *debug_help; /**< @brief be verbose for debug purposes help description. */
int f1x_flag; /**< @brief Specify CPU family (default=off). */
const char *f1x_help; /**< @brief Specify CPU family help description. */
int intelhex_flag; /**< @brief force fileformat to be IntelHex (default=off). */
const char *intelhex_help; /**< @brief force fileformat to be IntelHex help description. */
int erase_flag; /**< @brief erase device (default=off). */
const char *erase_help; /**< @brief erase device help description. */
int reset_flag; /**< @brief reset device (default=off). */
const char *reset_help; /**< @brief reset device help description. */
char * program_arg; /**< @brief program file. */
char * program_orig; /**< @brief program file original value given at command line. */
const char *program_help; /**< @brief program file help description. */
char * comport_arg; /**< @brief communicate using this device. */
char * comport_orig; /**< @brief communicate using this device original value given at command line. */
const char *comport_help; /**< @brief communicate using this device help description. */
int help_given ; /**< @brief Whether help was given. */
int version_given ; /**< @brief Whether version was given. */
int invert_test_given ; /**< @brief Whether invert-test was given. */
int invert_reset_given ; /**< @brief Whether invert-reset was given. */
int telosb_given ; /**< @brief Whether telosb was given. */
int tmote_given ; /**< @brief Whether tmote was given. */
int debug_given ; /**< @brief Whether debug was given. */
int f1x_given ; /**< @brief Whether f1x was given. */
int intelhex_given ; /**< @brief Whether intelhex was given. */
int erase_given ; /**< @brief Whether erase was given. */
int reset_given ; /**< @brief Whether reset was given. */
int program_given ; /**< @brief Whether program was given. */
int comport_given ; /**< @brief Whether comport was given. */
} ;
/** @brief The additional parameters to pass to parser functions */
struct cmdline_parser_params
{
int override; /**< @brief whether to override possibly already present options (default 0) */
int initialize; /**< @brief whether to initialize the option structure gengetopt_args_info (default 0) */
int check_required; /**< @brief whether to check that all required options were provided (default 0) */
int check_ambiguity; /**< @brief whether to check for options already specified in the option structure gengetopt_args_info (default 0) */
} ;
/** @brief the purpose string of the program */
extern const char *gengetopt_args_info_purpose;
/** @brief the usage string of the program */
extern const char *gengetopt_args_info_usage;
/** @brief all the lines making the help output */
extern const char *gengetopt_args_info_help[];
/**
* The command line parser
* @param argc the number of command line options
* @param argv the command line options
* @param args_info the structure where option information will be stored
* @return 0 if everything went fine, NON 0 if an error took place
*/
int cmdline_parser (int argc, char * const *argv,
struct gengetopt_args_info *args_info);
/**
* The command line parser (version with additional parameters - deprecated)
* @param argc the number of command line options
* @param argv the command line options
* @param args_info the structure where option information will be stored
* @param override whether to override possibly already present options
* @param initialize whether to initialize the option structure my_args_info
* @param check_required whether to check that all required options were provided
* @return 0 if everything went fine, NON 0 if an error took place
* @deprecated use cmdline_parser_ext() instead
*/
int cmdline_parser2 (int argc, char * const *argv,
struct gengetopt_args_info *args_info,
int override, int initialize, int check_required);
/**
* The command line parser (version with additional parameters)
* @param argc the number of command line options
* @param argv the command line options
* @param args_info the structure where option information will be stored
* @param params additional parameters for the parser
* @return 0 if everything went fine, NON 0 if an error took place
*/
int cmdline_parser_ext (int argc, char * const *argv,
struct gengetopt_args_info *args_info,
struct cmdline_parser_params *params);
/**
* Save the contents of the option struct into a (text) file.
* This file can be read by the config file parser (if generated by gengetopt)
* @param filename the file where to save
* @param args_info the option struct to save
* @return 0 if everything went fine, NON 0 if an error took place
*/
int cmdline_parser_file_save(const char *filename,
struct gengetopt_args_info *args_info);
/**
* Print the help
*/
void cmdline_parser_print_help(void);
/**
* Print the version
*/
void cmdline_parser_print_version(void);
/**
* Allocates dynamically a cmdline_parser_params structure and initializes
* all its fields to 0
* @return the initialized cmdline_parser_params structure
*/
struct cmdline_parser_params *cmdline_parser_params_init();
/**
* Initializes the passed gengetopt_args_info structure's fields
* (also set default values for options that have a default)
* @param args_info the structure to initialize
*/
void cmdline_parser_init (struct gengetopt_args_info *args_info);
/**
* Deallocates the string fields of the gengetopt_args_info structure
* (but does not deallocate the structure itself)
* @param args_info the structure to deallocate
*/
void cmdline_parser_free (struct gengetopt_args_info *args_info);
/**
* Checks that all the required options were specified
* @param args_info the structure to check
* @param prog_name the name of the program that will be used to print
* possible errors
* @return
*/
int cmdline_parser_required (struct gengetopt_args_info *args_info,
const char *prog_name);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* CMDLINE_H */
--- NEW FILE: cppbsl.cc ---
/* -*- mode:c++; indent-tabs-mode:nil -*-
* Copyright (c) 2007, Technische Universitaet Berlin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* - Neither the name of the Technische Universitaet Berlin nor the names
* of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* hand rolled bsl tool, other ones are too slow
* @author Andreas Koepke <koepke at tkn.tu-berlin.de>
* @date 2007-04-16
*/
#include <fstream>
#include <iostream>
#include "Parameters.h"
#include "Serial.h"
#include "Bsl.h"
using namespace std;
Parameters *parameters;
void errMsg(int r, int err, const char *msg) {
cerr << msg;
if(err) {
cerr << ", system error: " << strerror(err);
} else {
cerr << ", internal error";
}
cerr << "." << endl;
}
int main(int argc, char *argv[]) {
int r, readFD, writeFD, err;
termios oldterm;
parameters = new Parameters(argc, argv);
BaseSerial *bs;
Bsl *bsl;
err = 0;
r = serial_connect(&err, parameters->dev.c_str(), &readFD, &writeFD, &oldterm);
if(r == -1) {
errMsg(r, err, "Could not connect to serial device");
delete parameters;
return -1;
}
if(parameters->telosb) {
bs = new TelosBSerial(oldterm, readFD, writeFD);
}
else {
bs = new BaseSerial(oldterm, readFD, writeFD, parameters->invertTest, parameters->invertReset);
}
bsl = new Bsl(bs, parameters->img.c_str());
switch(parameters->action) {
case Parameters::ERASE:
r = bsl->erase(&err);
if(r == -1) {
errMsg(r, err, "Could not erase node");
}
else {
r = bsl->reset(&err);
if(r == -1) {
errMsg(r, err, "Could not reset node");
}
}
break;
case Parameters::RESET:
r = bsl->reset(&err);
if(r == -1) {
errMsg(r, err, "Could not reset node");
}
break;
case Parameters::FLASH:
r = bsl->install(&err);
if(r == -1) {
errMsg(r, err, "Could not install image on node");
}
break;
default:
break;
}
delete bsl;
bs->disconnect(&err);
delete bs;
delete parameters;
return 0;
}
- Previous message: [Tinyos-2-commits]
CVS: tinyos-2.x/tools/platforms/msp430/cppbsl/config
Makefile.am, NONE, 1.1 Makefile.in, NONE, 1.1 depcomp, NONE,
1.1 install-sh, NONE, 1.1 missing, NONE, 1.1
- Next message: [Tinyos-2-commits] CVS: tinyos-2.x/tos/lib/net/ctp CtpInfo.nc, 1.4,
1.5 CtpRoutingEngineP.nc, 1.11, 1.12
- Messages sorted by:
[ date ]
[ thread ]
[ subject ]
[ author ]
More information about the Tinyos-2-commits
mailing list