From 58494668ef864db0ad6cd35eca62106cbbed0543 Mon Sep 17 00:00:00 2001 From: TC Wan Date: Wed, 22 Dec 2010 17:30:29 +0800 Subject: added libnxt as a dependent library Added libnxt as a dependent library instead of using system provided package. This is mainly due to the difficulty in getting libnxt installed using MacPorts. --- Host/libnxt-0.3/AUTHORS | 1 + Host/libnxt-0.3/COPYING | 280 +++++++++++++++++++++ Host/libnxt-0.3/README | 129 ++++++++++ Host/libnxt-0.3/SConstruct | 64 +++++ Host/libnxt-0.3/error.c | 41 +++ Host/libnxt-0.3/error.h | 48 ++++ Host/libnxt-0.3/firmware.c | 152 +++++++++++ Host/libnxt-0.3/firmware.h | 31 +++ Host/libnxt-0.3/flash.c | 113 +++++++++ Host/libnxt-0.3/flash.h | 34 +++ Host/libnxt-0.3/flash_routine.h | 49 ++++ Host/libnxt-0.3/flash_routine.h.base | 36 +++ Host/libnxt-0.3/flash_write/Makefile | 19 ++ Host/libnxt-0.3/flash_write/crt0.s | 37 +++ Host/libnxt-0.3/flash_write/flash.c | 46 ++++ Host/libnxt-0.3/fwexec | Bin 0 -> 18112 bytes Host/libnxt-0.3/fwflash | Bin 0 -> 24072 bytes Host/libnxt-0.3/lowlevel.c | 171 +++++++++++++ Host/libnxt-0.3/lowlevel.h | 40 +++ Host/libnxt-0.3/main_fwexec.c | 129 ++++++++++ Host/libnxt-0.3/main_fwflash.c | 101 ++++++++ Host/libnxt-0.3/make_flash_header.py | 109 ++++++++ Host/libnxt-0.3/samba.c | 182 ++++++++++++++ Host/libnxt-0.3/samba.h | 51 ++++ Host/packages/libnxt-0.3.tar.gz | Bin 0 -> 16247 bytes .../patch-libnxt-sconstruct-libusb-legacy.diff | 71 ++++++ 26 files changed, 1934 insertions(+) create mode 100644 Host/libnxt-0.3/AUTHORS create mode 100644 Host/libnxt-0.3/COPYING create mode 100644 Host/libnxt-0.3/README create mode 100644 Host/libnxt-0.3/SConstruct create mode 100644 Host/libnxt-0.3/error.c create mode 100644 Host/libnxt-0.3/error.h create mode 100644 Host/libnxt-0.3/firmware.c create mode 100644 Host/libnxt-0.3/firmware.h create mode 100644 Host/libnxt-0.3/flash.c create mode 100644 Host/libnxt-0.3/flash.h create mode 100644 Host/libnxt-0.3/flash_routine.h create mode 100644 Host/libnxt-0.3/flash_routine.h.base create mode 100644 Host/libnxt-0.3/flash_write/Makefile create mode 100644 Host/libnxt-0.3/flash_write/crt0.s create mode 100644 Host/libnxt-0.3/flash_write/flash.c create mode 100755 Host/libnxt-0.3/fwexec create mode 100755 Host/libnxt-0.3/fwflash create mode 100644 Host/libnxt-0.3/lowlevel.c create mode 100644 Host/libnxt-0.3/lowlevel.h create mode 100644 Host/libnxt-0.3/main_fwexec.c create mode 100644 Host/libnxt-0.3/main_fwflash.c create mode 100755 Host/libnxt-0.3/make_flash_header.py create mode 100644 Host/libnxt-0.3/samba.c create mode 100644 Host/libnxt-0.3/samba.h create mode 100644 Host/packages/libnxt-0.3.tar.gz create mode 100644 Host/patches/patch-libnxt-sconstruct-libusb-legacy.diff (limited to 'Host') diff --git a/Host/libnxt-0.3/AUTHORS b/Host/libnxt-0.3/AUTHORS new file mode 100644 index 0000000..6dc3d6f --- /dev/null +++ b/Host/libnxt-0.3/AUTHORS @@ -0,0 +1 @@ +David Anderson diff --git a/Host/libnxt-0.3/COPYING b/Host/libnxt-0.3/COPYING new file mode 100644 index 0000000..960fe74 --- /dev/null +++ b/Host/libnxt-0.3/COPYING @@ -0,0 +1,280 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS diff --git a/Host/libnxt-0.3/README b/Host/libnxt-0.3/README new file mode 100644 index 0000000..5b54bd3 --- /dev/null +++ b/Host/libnxt-0.3/README @@ -0,0 +1,129 @@ + LibNXT and FwFlash + v0.3 + David Anderson + ==================== + +What? +===== + +LibNXT is an utility library for talking to the LEGO Mindstorms NXT +intelligent brick at a relatively low level. It currently does: + + - Handling USB communication and locating the NXT in the USB tree. + - Interaction with the Atmel AT91SAM boot assistant. + - Flashing of a firmware image to the NXT. + - Execution of code directly in RAM. + +(If you have ideas of other stuff it should do, please suggest!) + +LibNXT is targetted at the platforms that the official Lego Minstorms +NXT software overlooks. Specifically, it will work on any +POSIX-compliant operating system where libusb 0.1 +(http://libusb.sf.net/) is supported. + +The design of LibNXT is layered, meaning you can plug into it at any +level of complexity or abstraction you desire, from the lowest level +USB bulk bit-pushing interface, to an API exposing the SAM-BA +commandset, right up to just calling nxt_firmware_flash() and having +everything taken care of! + + +`fwflash` is the first utility program that uses LibNXT. As its name +hints, its purpose is to take a NXT firmware image file and flash it +to a connected NXT device. + +`fwexec` is another cool utility, originally written by the folks of +the Lejos project (http://lejos.sourceforge.net/). It takes a +specially compiled firmware image, uploads it to the NXT's RAM, and +executes it directly from there. While this firmware will only last +until the brick is powered down, it is a great tool for testing +firmwares during development without wearing down the flash memory. + + +Who? +==== + +Uh, that would be me. David Anderson, linux/open source developer and +enthusiast, and fan of embedded stuff in general. As mentionned above, +the `fwexec` utility was originally written by Lawrie Griffiths, over +at Lejos. + +When? +===== + +Started the day after receiving the NXT kit, I started by working out +the firmware flashing procedure from the specs and a USB dump of the +windows software at work. Since then, after obtaining a +proof-of-concept application, I've been implementing this library to +abstract the horrors of low-level USB chatter into a nice, clean +library. + + - Release 0.3 (22/04/2007) : more than a year later, there are a few + changes, and it can't hurt to push them out in an + actual release. This release includes a more + intelligent build system that handles a missing flash + driver elegantly, and the `fwexec` utility program. + Unless people have specific itches that need + scratching, or bugs that should be fixed, this will + probably be the final release of libnxt. My personal + itch is scratched, and I've moved on to using libnxt + to write firmware code :-). + + - Release 0.2 (17/03/2006) : firmware flashing is now done entirely + with open source code. The flash driver routine has + been replaced with open source code implementing the + flashing procedure. + + - Release 0.1.1 (10/03/2006) : add big-endian host arch support, and + make the firmware image reading process a little less + linux-centric (use of the posix open/read/close API + instead of mmap). + + - Release 0.1 (10/03/2006) : featuring the FwFlash utility and a + LibNXT with just enough API in it to get FwFlash + working :-) + +How? +==== + +To compile all of this you'll need a copy of libusb 0.1 on your +system, as well as the scons project manager. + + - Libusb 0.1: http://libusb.sf.net/ + - Scons: http://www.scons.org/ + +When you have all that, just run 'scons' in the libnxt directory, and +compilation should follow. Once you're done, you can try fwflash out +by resetting your NXT (see your user manual for details on this) and +running: + +./fwflash nxtos.bin + +nxtos.bin is the official Lego Mindstorms NXT firmware. You can find +this on your current installation of the Mindstorms software, or on +LEGO's website. + +If all goes well, fwflash should inform you that it has found the NXT +on your USB device bus, and that flashing has started. After a few +seconds, it should announce successful flashing, and say that it has +booted the new firmware, which should be answered by the greeting +sound of the LEGO firmware as the brick starts up :-). + +If it doesn't, well it's either a problem with your USB device +permissions (if fwflash can't find the NXT), or it's a bug (if the brick +doesn't reboot properly, or if some weird error is reported by fwflash +and it bombs out. + +The release tarball comes with a 'flash.bin'. This file is the +compiled version of the embedded flash driver, which is uploaded to +the NXT's RAM and required to write data into flash memory. + +If you do not have a release tarball, or accidentally deleted your +copy of flash.bin, there are several options. If you have an ARM7 +cross-compiler toolchain, you can simply type 'make' in the +'flash_write' subdirectory to rebuild the flash driver. + +If you don't have a suitable cross-compiler, you can just run 'scons' +as usual. The build system will see that the flash driver is missing, +and offer to download a binary copy from the LibNXT website and use +that. diff --git a/Host/libnxt-0.3/SConstruct b/Host/libnxt-0.3/SConstruct new file mode 100644 index 0000000..115d19e --- /dev/null +++ b/Host/libnxt-0.3/SConstruct @@ -0,0 +1,64 @@ +##### +# Note: For Macports, libusb-legacy need to be used? +# I can't get it to build with libusb-compat +#### + +import os +import os.path +from glob import glob + +############################################################### +# Utility functions. +############################################################### + +# Similar to env.WhereIs, but always searches os.environ. +def find_on_path(filename): + paths = os.environ.get('PATH') + if not paths: + return None + for p in paths.split(':'): + path = os.path.abspath(os.path.join(p, filename)) + if os.path.isfile(path): + return p + return None + +auto_libs = 'libusb-legacy' + +cmdname = 'pkg-config' +pkg_config_path = find_on_path(cmdname) + ('/%s' % cmdname) + +# Detect the system's endianness +from sys import byteorder +if byteorder == 'big': + endian = '_NXT_BIG_ENDIAN' +else: + endian = '_NXT_LITTLE_ENDIAN' + +BuildEnv = Environment(CCFLAGS=['-Wall', '-std=gnu99', + '-g', '-ggdb', '-D' + endian]) +if auto_libs: + cmdstr = '%s --cflags --libs ' % pkg_config_path + BuildEnv.ParseConfig(cmdstr + ' --cflags --libs ' + auto_libs) + +BuildEnv.Command('flash_routine.h', + 'flash_routine.h.base', + './make_flash_header.py') + +BuildEnv.Append(LIBPATH='.') +libnxt = Default(BuildEnv.Library('nxt', + [x for x in glob('*.c') + if not x.startswith('main_')], + LIBS='usb-legacy')) + +fwflash = Default(BuildEnv.Program('fwflash', 'main_fwflash.c', + LIBS=['usb-legacy', 'nxt'])) + +fwexec = Default(BuildEnv.Program('fwexec', 'main_fwexec.c', + LIBS=['usb-legacy', 'nxt'])) + +# the install target +#BuildEnv.Alias("install", BuildEnv.Install(os.path.join(os.environ['DESTDIR'], "lib"), libnxt)) +#BuildEnv.Alias("install", BuildEnv.Install(os.path.join(os.environ['DESTDIR'], "bin"), fwflash, fwexec)) +#BuildEnv.Command("uninstall", None, Delete(FindInstalledFiles())) + + diff --git a/Host/libnxt-0.3/error.c b/Host/libnxt-0.3/error.c new file mode 100644 index 0000000..fd45100 --- /dev/null +++ b/Host/libnxt-0.3/error.c @@ -0,0 +1,41 @@ +/** + * NXT bootstrap interface; error handling code. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include "error.h" + +static const char const *err_str[] = { + "Success", + "NXT not found on USB bus", + "Error trying to configure the NXT USB link", + "NXT USB interface is already claimed by another program", + "USB write error", + "USB read error", + "SAM-BA protocol error", + "NXT handshake failed", + "File open/handling error", + "Invalid firmware image", +}; + +const char const * +nxt_str_error(nxt_error_t err) +{ + return err_str[err]; +} diff --git a/Host/libnxt-0.3/error.h b/Host/libnxt-0.3/error.h new file mode 100644 index 0000000..6d6c681 --- /dev/null +++ b/Host/libnxt-0.3/error.h @@ -0,0 +1,48 @@ +/** + * NXT bootstrap interface; error handling code. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __ERROR_H__ +#define __ERROR_H__ + +typedef enum +{ + NXT_OK = 0, + NXT_NOT_PRESENT = 1, + NXT_CONFIGURATION_ERROR = 2, + NXT_IN_USE = 3, + NXT_USB_WRITE_ERROR = 4, + NXT_USB_READ_ERROR = 5, + NXT_SAMBA_PROTOCOL_ERROR = 6, + NXT_HANDSHAKE_FAILED = 7, + NXT_FILE_ERROR = 8, + NXT_INVALID_FIRMWARE = 9, +} nxt_error_t; + +const char const *nxt_str_error(nxt_error_t err); + +#define NXT_ERR(expr) \ + do { \ + nxt_error_t nxt__err_temp = (expr); \ + if (nxt__err_temp) \ + return nxt__err_temp; \ + } while(0) + +#endif /* __ERROR_H__ */ diff --git a/Host/libnxt-0.3/firmware.c b/Host/libnxt-0.3/firmware.c new file mode 100644 index 0000000..1c4092b --- /dev/null +++ b/Host/libnxt-0.3/firmware.c @@ -0,0 +1,152 @@ +/** + * NXT bootstrap interface; NXT firmware handling code. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "error.h" +#include "lowlevel.h" +#include "samba.h" +#include "flash.h" +#include "firmware.h" +#include "flash_routine.h" + +static nxt_error_t +nxt_flash_prepare(nxt_t *nxt) +{ + // Put the clock in PLL/2 mode + NXT_ERR(nxt_write_word(nxt, 0xFFFFFC30, 0x7)); + + // Unlock the flash chip + NXT_ERR(nxt_flash_unlock_all_regions(nxt)); + + // Send the flash writing routine + NXT_ERR(nxt_send_file(nxt, 0x202000, flash_bin, flash_len)); + + return NXT_OK; +} + + +static nxt_error_t +nxt_flash_block(nxt_t *nxt, nxt_word_t block_num, char *buf) +{ + // Set the target block number + NXT_ERR(nxt_write_word(nxt, 0x202300, block_num)); + + // Send the block to flash + NXT_ERR(nxt_send_file(nxt, 0x202100, buf, 256)); + + // Jump into the flash writing routine + NXT_ERR(nxt_jump(nxt, 0x202000)); + + return NXT_OK; +} + + +static nxt_error_t +nxt_flash_finish(nxt_t *nxt) +{ + return nxt_flash_wait_ready(nxt); +} + + +static nxt_error_t +nxt_firmware_validate_fd(int fd) +{ + struct stat s; + + if (fstat(fd, &s) < 0) + return NXT_FILE_ERROR; + + if (s.st_size > 256*1024) + return NXT_INVALID_FIRMWARE; + + return NXT_OK; +} + + +nxt_error_t +nxt_firmware_validate(char *fw_path) +{ + nxt_error_t err; + int fd; + + fd = open(fw_path, O_RDONLY); + if (fd < 0) + return NXT_FILE_ERROR; + + err = nxt_firmware_validate_fd(fd); + close(fd); + + return err; +} + + +nxt_error_t +nxt_firmware_flash(nxt_t *nxt, char *fw_path) +{ + int fd, i, err; + + fd = open(fw_path, O_RDONLY); + if (fd < 0) + return NXT_FILE_ERROR; + + err = nxt_firmware_validate_fd(fd); + if (err != NXT_OK) + { + close(fd); + return NXT_INVALID_FIRMWARE; + } + + NXT_ERR(nxt_flash_prepare(nxt)); + + for (i = 0; i < 1024; i++) //256*1024; i += 256) + { + char buf[256]; + int ret; + + memset(buf, 0, 256); + ret = read(fd, buf, 256); + + if (ret != -1) + NXT_ERR(nxt_flash_block(nxt, i, buf)); + + if (ret < 256) + { + close(fd); + NXT_ERR(nxt_flash_finish(nxt)); + + return ret == -1 ? NXT_FILE_ERROR : NXT_OK; + } + + NXT_ERR(nxt_flash_block(nxt, i, buf)); + } + + close(fd); + NXT_ERR(nxt_flash_finish(nxt)); + + return NXT_OK; +} diff --git a/Host/libnxt-0.3/firmware.h b/Host/libnxt-0.3/firmware.h new file mode 100644 index 0000000..bdda58e --- /dev/null +++ b/Host/libnxt-0.3/firmware.h @@ -0,0 +1,31 @@ +/** + * NXT bootstrap interface; NXT firmware handling code. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __FLASH_H__ +#define __FLASH_H__ + +#include "error.h" +#include "lowlevel.h" + +nxt_error_t nxt_firmware_flash(nxt_t *nxt, char *fw_path); +nxt_error_t nxt_firmware_validate(char *fw_path); + +#endif /* __FLASH_H__ */ diff --git a/Host/libnxt-0.3/flash.c b/Host/libnxt-0.3/flash.c new file mode 100644 index 0000000..d757ce2 --- /dev/null +++ b/Host/libnxt-0.3/flash.c @@ -0,0 +1,113 @@ +/** + * NXT bootstrap interface; NXT flash chip code. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include +#include +#include +#include +#include +#include + +#include "error.h" +#include "lowlevel.h" +#include "samba.h" +#include "flash.h" + +enum nxt_flash_commands +{ + FLASH_CMD_LOCK = 0x2, + FLASH_CMD_UNLOCK = 0x4, +}; + +nxt_error_t +nxt_flash_wait_ready(nxt_t *nxt) +{ + nxt_word_t flash_status; + + do + { + NXT_ERR(nxt_read_word(nxt, 0xFFFFFF68, &flash_status)); + + /* Bit 0 is the FRDY field. Set to 1 if the flash controller is + * ready to run a new command. + */ + } while (!(flash_status & 0x1)); + + return NXT_OK; +} + +static nxt_error_t +nxt_flash_alter_lock(nxt_t *nxt, int region_num, + enum nxt_flash_commands cmd) +{ + nxt_word_t w = 0x5A000000 | ((64 * region_num) << 8); + w += cmd; + + NXT_ERR(nxt_flash_wait_ready(nxt)); + + /* Flash mode register: FCMN 0x5, FWS 0x1 + * Flash command register: KEY 0x5A, FCMD = clear-lock-bit (0x4) + * Flash mode register: FCMN 0x34, FWS 0x1 + */ + NXT_ERR(nxt_write_word(nxt, 0xFFFFFF60, 0x00050100)); + NXT_ERR(nxt_write_word(nxt, 0xFFFFFF64, w)); + NXT_ERR(nxt_write_word(nxt, 0xFFFFFF60, 0x00340100)); + + return NXT_OK; +} + + +nxt_error_t +nxt_flash_lock_region(nxt_t *nxt, int region_num) +{ + return nxt_flash_alter_lock(nxt, region_num, FLASH_CMD_LOCK); +} + + +nxt_error_t +nxt_flash_unlock_region(nxt_t *nxt, int region_num) +{ + return nxt_flash_alter_lock(nxt, region_num, FLASH_CMD_UNLOCK); +} + + +nxt_error_t +nxt_flash_lock_all_regions(nxt_t *nxt) +{ + int i; + + for (i = 0; i < 16; i++) + NXT_ERR(nxt_flash_lock_region(nxt, i)); + + return NXT_OK; +} + + +nxt_error_t +nxt_flash_unlock_all_regions(nxt_t *nxt) +{ + int i; + + for (i = 0; i < 16; i++) + NXT_ERR(nxt_flash_unlock_region(nxt, i)); + + return NXT_OK; +} diff --git a/Host/libnxt-0.3/flash.h b/Host/libnxt-0.3/flash.h new file mode 100644 index 0000000..8d157d7 --- /dev/null +++ b/Host/libnxt-0.3/flash.h @@ -0,0 +1,34 @@ +/** + * NXT bootstrap interface; NXT flash chip access code. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __FLASH_H__ +#define __FLASH_H__ + +#include "error.h" +#include "lowlevel.h" + +nxt_error_t nxt_flash_wait_ready(nxt_t *nxt); +nxt_error_t nxt_flash_lock_region(nxt_t *nxt, int region_num); +nxt_error_t nxt_flash_unlock_region(nxt_t *nxt, int region_num); +nxt_error_t nxt_flash_lock_all_regions(nxt_t *nxt); +nxt_error_t nxt_flash_unlock_all_regions(nxt_t *nxt); + +#endif /* __FLASH_H__ */ diff --git a/Host/libnxt-0.3/flash_routine.h b/Host/libnxt-0.3/flash_routine.h new file mode 100644 index 0000000..1d3484c --- /dev/null +++ b/Host/libnxt-0.3/flash_routine.h @@ -0,0 +1,49 @@ +/** + * Flash routine. Hardcodes the ARM7 bytecode for writing data to + * flash memory in the downloader binary. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __FLASH_ROUTINE_H__ +#define __FLASH_ROUTINE_H__ + +/* + * An array containing all the bits of the flash routine bytecode. + */ +static char flash_bin[] = { + 0x21, 0xd8, 0xa0, 0xe3, 0x00, 0x40, 0x2d, 0xe9, 0x00, 0x00, 0x00, 0xeb, + 0x00, 0x80, 0xbd, 0xe8, 0x00, 0x20, 0xe0, 0xe3, 0x97, 0x30, 0x12, 0xe5, + 0x01, 0x00, 0x13, 0xe3, 0xfc, 0xff, 0xff, 0x0a, 0x02, 0xc6, 0xa0, 0xe3, + 0x0c, 0x00, 0xa0, 0xe1, 0x21, 0x0c, 0x80, 0xe2, 0x02, 0xca, 0x8c, 0xe2, + 0x00, 0x10, 0xa0, 0xe3, 0x00, 0x33, 0x9c, 0xe5, 0x03, 0x33, 0x81, 0xe0, + 0x01, 0x21, 0x90, 0xe7, 0x03, 0x31, 0xa0, 0xe1, 0x01, 0x10, 0x81, 0xe2, + 0x01, 0x36, 0x83, 0xe2, 0x40, 0x00, 0x51, 0xe3, 0x00, 0x20, 0x83, 0xe5, + 0xf6, 0xff, 0xff, 0x1a, 0x00, 0x33, 0x9c, 0xe5, 0x03, 0x3b, 0xa0, 0xe1, + 0x23, 0x3b, 0xa0, 0xe1, 0x03, 0x34, 0xa0, 0xe1, 0x5a, 0x34, 0x83, 0xe2, + 0x01, 0x30, 0x83, 0xe2, 0x00, 0x20, 0xe0, 0xe3, 0x9b, 0x30, 0x02, 0xe5, + 0x97, 0x30, 0x12, 0xe5, 0x01, 0x00, 0x13, 0xe3, 0xfc, 0xff, 0xff, 0x0a, + 0x1e, 0xff, 0x2f, 0xe1 +}; + +/* + * The number of bytes in the above array. + */ +static unsigned long flash_len = 136; + +#endif /* __FLASH_ROUTINE_H__ */ diff --git a/Host/libnxt-0.3/flash_routine.h.base b/Host/libnxt-0.3/flash_routine.h.base new file mode 100644 index 0000000..116fa92 --- /dev/null +++ b/Host/libnxt-0.3/flash_routine.h.base @@ -0,0 +1,36 @@ +/** + * Flash routine. Hardcodes the ARM7 bytecode for writing data to + * flash memory in the downloader binary. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __FLASH_ROUTINE_H__ +#define __FLASH_ROUTINE_H__ + +/* + * An array containing all the bits of the flash routine bytecode. + */ +static char flash_bin[] = {___FLASH_BIN___}; + +/* + * The number of bytes in the above array. + */ +static unsigned long flash_len = ___FLASH_LEN___; + +#endif /* __FLASH_ROUTINE_H__ */ diff --git a/Host/libnxt-0.3/flash_write/Makefile b/Host/libnxt-0.3/flash_write/Makefile new file mode 100644 index 0000000..93bb888 --- /dev/null +++ b/Host/libnxt-0.3/flash_write/Makefile @@ -0,0 +1,19 @@ +#all: +# gcc -Wall -O0 -mcpu=arm7tdmi-s -mapcs -mthumb-interwork -o flash.o flash.c -nostartfiles -nodefaultlibs -nostdlib -Wl,-e,main +# objcopy -Obinary -j.text flash.o flash.bin +# objdump --disassemble-all -bbinary -marm7tdmi flash.bin > flash.asm +# + +CC=`which arm-elf-gcc` +AS=`which arm-elf-as` +LD=`which arm-elf-ld` +OBJCOPY=`which arm-elf-objcopy` + +all: + $(CC) -W -Wall -O3 -msoft-float -mcpu=arm7tdmi -mapcs -c -o flash.o flash.c + $(AS) --warn -mfpu=softfpa -mcpu=arm7tdmi -mapcs-32 -o crt0.o crt0.s + $(LD) -O3 --gc-sections crt0.o flash.o -o flash.elf + $(OBJCOPY) -O binary flash.elf flash.bin + +clean: + rm -f flash.o crt0.o flash.elf flash.bin diff --git a/Host/libnxt-0.3/flash_write/crt0.s b/Host/libnxt-0.3/flash_write/crt0.s new file mode 100644 index 0000000..680573b --- /dev/null +++ b/Host/libnxt-0.3/flash_write/crt0.s @@ -0,0 +1,37 @@ +/** + * NXT bootstrap interface; NXT onboard flashing driver bootstrap. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +.text +.align 4 +.globl _start + +_start: + /* Initialize the stack */ + mov sp, #0x210000 + + /* Preserve old link register */ + stmfd sp!, {lr} + + /* Call main */ + bl do_flash_write + + /* Return */ + ldmfd sp!, {pc} diff --git a/Host/libnxt-0.3/flash_write/flash.c b/Host/libnxt-0.3/flash_write/flash.c new file mode 100644 index 0000000..824a088 --- /dev/null +++ b/Host/libnxt-0.3/flash_write/flash.c @@ -0,0 +1,46 @@ +/** + * NXT bootstrap interface; NXT onboard flashing driver. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#define VINTPTR(addr) ((volatile unsigned int *)(addr)) +#define VINT(addr) (*(VINTPTR(addr))) + +#define USER_PAGE VINTPTR(0x00202100) +#define USER_PAGE_NUM VINT(0x00202300) + +#define FLASH_BASE VINTPTR(0x00100000) +#define FLASH_CMD_REG VINT(0xFFFFFF64) +#define FLASH_STATUS_REG VINT(0xFFFFFF68) +#define OFFSET_PAGE_NUM ((USER_PAGE_NUM & 0x000003FF) << 8) +#define FLASH_CMD_WRITE (0x5A000001 + OFFSET_PAGE_NUM) + +void do_flash_write(void) +{ + unsigned long i; + + while (!(FLASH_STATUS_REG & 0x1)); + + for (i = 0; i < 64; i++) + FLASH_BASE[(USER_PAGE_NUM*64)+i] = USER_PAGE[i]; + + FLASH_CMD_REG = FLASH_CMD_WRITE; + + while (!(FLASH_STATUS_REG & 0x1)); +} diff --git a/Host/libnxt-0.3/fwexec b/Host/libnxt-0.3/fwexec new file mode 100755 index 0000000..db268be Binary files /dev/null and b/Host/libnxt-0.3/fwexec differ diff --git a/Host/libnxt-0.3/fwflash b/Host/libnxt-0.3/fwflash new file mode 100755 index 0000000..9b92b2a Binary files /dev/null and b/Host/libnxt-0.3/fwflash differ diff --git a/Host/libnxt-0.3/lowlevel.c b/Host/libnxt-0.3/lowlevel.c new file mode 100644 index 0000000..49b2745 --- /dev/null +++ b/Host/libnxt-0.3/lowlevel.c @@ -0,0 +1,171 @@ +/** + * NXT bootstrap interface; low-level USB functions. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "lowlevel.h" + +enum nxt_usb_ids { + VENDOR_LEGO = 0x0694, + VENDOR_ATMEL = 0x03EB, + PRODUCT_NXT = 0x0002, + PRODUCT_SAMBA = 0x6124 +}; + +struct nxt_t { + struct usb_device *dev; + struct usb_dev_handle *hdl; + int is_in_reset_mode; +}; + + +nxt_error_t nxt_init(nxt_t **nxt) +{ + usb_init(); + *nxt = calloc(1, sizeof(**nxt)); + + return NXT_OK; +} + + +nxt_error_t nxt_find(nxt_t *nxt) +{ + struct usb_bus *busses, *bus; + + usb_find_busses(); + usb_find_devices(); + + busses = usb_get_busses(); + + for (bus = busses; bus != NULL; bus = bus->next) + { + struct usb_device *dev; + + for (dev = bus->devices; dev != NULL; dev = dev->next) + { + if (dev->descriptor.idVendor == VENDOR_ATMEL && + dev->descriptor.idProduct == PRODUCT_SAMBA) + { + nxt->dev = dev; + nxt->is_in_reset_mode = 1; + return NXT_OK; + } + else if (dev->descriptor.idVendor == VENDOR_LEGO && + dev->descriptor.idProduct == PRODUCT_NXT) + { + nxt->dev = dev; + return NXT_OK; + } + } + } + + return NXT_NOT_PRESENT; +} + + +nxt_error_t +nxt_open(nxt_t *nxt) +{ + char buf[2]; + int ret; + + nxt->hdl = usb_open(nxt->dev); + + ret = usb_set_configuration(nxt->hdl, 1); + if (ret < 0) + { + usb_close(nxt->hdl); + return NXT_CONFIGURATION_ERROR; + } + + ret = usb_claim_interface(nxt->hdl, 1); + if (ret < 0) + { + usb_close(nxt->hdl); + return NXT_IN_USE; + } + + /* NXT handshake */ + nxt_send_str(nxt, "N#"); + nxt_recv_buf(nxt, buf, 2); + if (memcmp(buf, "\n\r", 2) != 0) + { + usb_release_interface(nxt->hdl, 1); + usb_close(nxt->hdl); + return NXT_HANDSHAKE_FAILED; + } + + return NXT_OK; +} + + +nxt_error_t +nxt_close(nxt_t *nxt) +{ + usb_release_interface(nxt->hdl, 1); + usb_close(nxt->hdl); + free(nxt); + + return NXT_OK; +} + + +int +nxt_in_reset_mode(nxt_t *nxt) +{ + return nxt->is_in_reset_mode; +} + + +nxt_error_t +nxt_send_buf(nxt_t *nxt, char *buf, int len) +{ + int ret = usb_bulk_write(nxt->hdl, 0x1, buf, len, 0); + if (ret < 0) + return NXT_USB_WRITE_ERROR; + + return NXT_OK; +} + + +nxt_error_t +nxt_send_str(nxt_t *nxt, char *str) +{ + return nxt_send_buf(nxt, str, strlen(str)); +} + + +nxt_error_t +nxt_recv_buf(nxt_t *nxt, char *buf, int len) +{ + int ret = usb_bulk_read(nxt->hdl, 0x82, buf, len, 0); + if (ret < 0) + return NXT_USB_READ_ERROR; + + return NXT_OK; +} diff --git a/Host/libnxt-0.3/lowlevel.h b/Host/libnxt-0.3/lowlevel.h new file mode 100644 index 0000000..14889e0 --- /dev/null +++ b/Host/libnxt-0.3/lowlevel.h @@ -0,0 +1,40 @@ +/** + * NXT bootstrap interface; low-level USB functions. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __LOWLEVEL_H__ +#define __LOWLEVEL_H__ + +#include +#include "error.h" + +struct nxt_t; +typedef struct nxt_t nxt_t; + +nxt_error_t nxt_init(nxt_t **nxt); +nxt_error_t nxt_find(nxt_t *nxt); +nxt_error_t nxt_open(nxt_t *nxt); +nxt_error_t nxt_close(nxt_t *nxt); +int nxt_in_reset_mode(nxt_t *nxt); +nxt_error_t nxt_send_buf(nxt_t *nxt, char *buf, int len); +nxt_error_t nxt_send_str(nxt_t *nxt, char *str); +nxt_error_t nxt_recv_buf(nxt_t *nxt, char *buf, int len); + +#endif /* __LOWLEVEL_H__ */ diff --git a/Host/libnxt-0.3/main_fwexec.c b/Host/libnxt-0.3/main_fwexec.c new file mode 100644 index 0000000..aedfede --- /dev/null +++ b/Host/libnxt-0.3/main_fwexec.c @@ -0,0 +1,129 @@ +/** + * Main program code for the runc utility. + * + * Copyright 2006 Lawrie Griffiths + * 2007 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include +#include + +#include "error.h" +#include "lowlevel.h" +#include "samba.h" +#include "firmware.h" + +#define NXT_HANDLE_ERR(expr, nxt, msg) \ + do { \ + nxt_error_t nxt__err_temp = (expr); \ + if (nxt__err_temp) \ + handle_error(nxt, msg, nxt__err_temp); \ + } while(0) + +static int handle_error(nxt_t *nxt, char *msg, nxt_error_t err) +{ + printf("%s: %s\n", msg, nxt_str_error(err)); + if (nxt != NULL) + nxt_close(nxt); + exit(err); +} + +void get_firmware(char **firmware, int *len, char *filename) +{ + FILE *f; + + f = fopen(filename, "rb"); + if (f == NULL) NXT_HANDLE_ERR(NXT_FILE_ERROR, NULL, "Error opening file"); + + fseek(f, 0, SEEK_END); + *len = ftell(f); + rewind(f); + + if (*len > 56*1024) + NXT_HANDLE_ERR(NXT_INVALID_FIRMWARE, NULL, + "Firmware image is too big to fit in RAM."); + + *firmware = malloc(*len); + if (*firmware == NULL) NXT_HANDLE_ERR(NXT_FILE_ERROR, NULL, + "Error allocating memory"); + + if (fread(*firmware, 1, *len, f) != *len) + NXT_HANDLE_ERR(NXT_FILE_ERROR, NULL, "Error reading file"); + + printf("Firmware size is %d bytes\n", *len); + + fclose(f); +} + +int main(int argc, char *argv[]) +{ + nxt_t *nxt; + nxt_error_t err; + char *firmware; + int firmware_len; + + if (argc != 2) + { + printf("Syntax: %s \n" + "\n" + "Example: %s beep.bin\n", argv[0], argv[0]); + exit(1); + } + + get_firmware(&firmware, &firmware_len, argv[1]); + + NXT_HANDLE_ERR(nxt_init(&nxt), NULL, + "Error during library initialization"); + + err = nxt_find(nxt); + if (err) + { + if (err == NXT_NOT_PRESENT) + printf("NXT not found. Is it properly plugged in via USB?\n"); + else + NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT"); + exit(1); + } + + if (!nxt_in_reset_mode(nxt)) + { + printf("NXT found, but not running in reset mode.\n"); + printf("Please reset your NXT manually and restart this program.\n"); + exit(2); + } + + NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT"); + + printf("NXT device in reset mode located and opened.\n" + "Uploading firmware...\n"); + + // Send the C program + NXT_HANDLE_ERR(nxt_send_file(nxt, 0x202000, firmware, firmware_len), nxt, + "Error Sending file"); + + printf("Firmware uploaded, executing...\n"); + NXT_HANDLE_ERR(nxt_jump(nxt, 0x202000), nxt, + "Error jumping to C program"); + + NXT_HANDLE_ERR(nxt_close(nxt), NULL, + "Error while closing connection to NXT"); + + printf("Firmware started.\n"); + + return 0; +} diff --git a/Host/libnxt-0.3/main_fwflash.c b/Host/libnxt-0.3/main_fwflash.c new file mode 100644 index 0000000..e38af8e --- /dev/null +++ b/Host/libnxt-0.3/main_fwflash.c @@ -0,0 +1,101 @@ +/** + * Main program code for the fwflash utility. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include +#include + +#include "error.h" +#include "lowlevel.h" +#include "samba.h" +#include "firmware.h" + +#define NXT_HANDLE_ERR(expr, nxt, msg) \ + do { \ + nxt_error_t nxt__err_temp = (expr); \ + if (nxt__err_temp) \ + return handle_error(nxt, msg, nxt__err_temp); \ + } while(0) + +static int handle_error(nxt_t *nxt, char *msg, nxt_error_t err) +{ + printf("%s: %s\n", msg, nxt_str_error(err)); + if (nxt != NULL) + nxt_close(nxt); + exit(err); +} + +int main(int argc, char *argv[]) +{ + nxt_t *nxt; + nxt_error_t err; + char *fw_file; + + if (argc != 2) + { + printf("Syntax: %s \n" + "\n" + "Example: %s nxtos.bin\n", argv[0], argv[0]); + exit(1); + } + + fw_file = argv[1]; + + printf("Checking firmware... "); + NXT_HANDLE_ERR(nxt_firmware_validate(fw_file), NULL, + "Error"); + printf("OK.\n"); + + NXT_HANDLE_ERR(nxt_init(&nxt), NULL, + "Error during library initialization"); + + err = nxt_find(nxt); + if (err) + { + if (err == NXT_NOT_PRESENT) + printf("NXT not found. Is it properly plugged in via USB?\n"); + else + NXT_HANDLE_ERR(0, NULL, "Error while scanning for NXT"); + exit(1); + } + + if (!nxt_in_reset_mode(nxt)) + { + printf("NXT found, but not running in reset mode.\n"); + printf("Please reset your NXT manually and restart this program.\n"); + exit(2); + } + + NXT_HANDLE_ERR(nxt_open(nxt), NULL, "Error while connecting to NXT"); + + printf("NXT device in reset mode located and opened.\n" + "Starting firmware flash procedure now...\n"); + + NXT_HANDLE_ERR(nxt_firmware_flash(nxt, fw_file), nxt, + "Error flashing firmware"); + printf("Firmware flash complete.\n"); + NXT_HANDLE_ERR(nxt_jump(nxt, 0x00100000), nxt, + "Error booting new firmware"); + printf("New firmware started!\n"); + + NXT_HANDLE_ERR(nxt_close(nxt), NULL, + "Error while closing connection to NXT"); + return 0; +} diff --git a/Host/libnxt-0.3/make_flash_header.py b/Host/libnxt-0.3/make_flash_header.py new file mode 100755 index 0000000..9734c86 --- /dev/null +++ b/Host/libnxt-0.3/make_flash_header.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python +# +# Take the flash_routine.bin file, and embed it as an array of bytes +# in a flash_routine.h, ready for packaging with the C firmware +# flasher. +# +# If a file name is provided on the commandline, load that file as the +# firmware flashing routine instead. +# + +import sys +import os +import os.path +import urllib2 +import sha + +FLASH_DIR = 'flash_write' +FLASH_BIN = 'flash.bin' +FLASH_PATH = os.path.join(FLASH_DIR, FLASH_BIN) + +DOWNLOAD_FLASH_CHECKSUM = '589501072d76be483f873a787080adcab20841f4' +DOWNLOAD_FLASH_URL = 'http://libnxt.googlecode.com/files/flash.bin' + +def check_flash_size(): + statinfo = os.stat(FLASH_PATH) + if statinfo.st_size > 1024: + print "The flash driver looks too big, refusing to embed." + return False + return True + +def ensure_flash_bin(): + # If the flash binary does exist, just check its size and return. + if os.path.isfile(FLASH_PATH): + return check_flash_size() + + # If the binary doesn't exist, offer to download a binary build + # from Google Code. + print """ +Embedded flash driver not found. This is required to build LibNXT. + +If you have an ARM7 cross-compiler toolchain available, you can build +the flash driver by interrupting (ctrl-C) this build and running +'make' in the 'flash_write' subdirectory. Then rerun this build again, +and everything should work great. + +If you do not have a cross-compiler, do not despair! I can also +download a copy of the compiled driver (built by the libnxt developer) +from the project website and use that. +""" + reply = raw_input("Is that okay? (y/n) ") + if reply not in ('y', 'Y', 'yes'): + print ("Okay, you're the boss. But that does mean I can't build " + "LibNXT. Sorry.") + return False + f = urllib2.urlopen(DOWNLOAD_FLASH_URL) + data = f.read() + f.close() + + # Verify the SHA-1 checksum + checksum = sha.new(data).hexdigest() + if checksum != DOWNLOAD_FLASH_CHECKSUM: + print "Oops, the flash binary I downloaded has the wrong checksum!" + print "Aborting :(" + return False + + f = open(FLASH_PATH, 'w') + f.write(data) + f.close() + + if os.path.isfile(FLASH_PATH): + return check_flash_size() + + # Dude, you're really not lucky, are you. + return False + + +def main(): + if not ensure_flash_bin(): + sys.exit(1) + + f = file(FLASH_PATH) + fwbin = f.read() + f.close() + + data = ['0x%s' % c.encode('hex') for c in fwbin] + + for i in range(0, len(data), 12): + data[i] = "\n " + data[i] + + data_str = ', '.join(data) + len_data = "%d" % len(data) + + # Read in the template + tplfile = file('flash_routine.h.base') + template = tplfile.read() + tplfile.close() + + # Replace the values in the template + template = template.replace('___FLASH_BIN___', data_str + '\n') + template = template.replace('___FLASH_LEN___', len_data) + + # Output the done header + out = file('flash_routine.h', 'w') + out.write(template) + out.close() + +if __name__ == '__main__': + main() + diff --git a/Host/libnxt-0.3/samba.c b/Host/libnxt-0.3/samba.c new file mode 100644 index 0000000..ff3e037 --- /dev/null +++ b/Host/libnxt-0.3/samba.c @@ -0,0 +1,182 @@ +/** + * NXT bootstrap interface; NXT Bootstrap control functions. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#include +#include +#include "error.h" +#include "lowlevel.h" +#include "samba.h" + +static nxt_error_t +nxt_format_command2(char *buf, char cmd, + nxt_addr_t addr, nxt_word_t word) +{ + snprintf(buf, 20, "%c%08X,%08X#", cmd, addr, word); + + return NXT_OK; +} + +static nxt_error_t +nxt_format_command(char *buf, char cmd, nxt_addr_t addr) +{ + snprintf(buf, 20, "%c%08X#", cmd, addr); + + return NXT_OK; +} + + +static nxt_error_t +nxt_write_common(nxt_t *nxt, char type, nxt_addr_t addr, nxt_word_t w) +{ + char buf[21] = {0}; + + NXT_ERR(nxt_format_command2(buf, type, addr, w)); + NXT_ERR(nxt_send_str(nxt, buf)); + + return NXT_OK; +} + + +nxt_error_t +nxt_write_byte(nxt_t *nxt, nxt_addr_t addr, nxt_byte_t b) +{ + return nxt_write_common(nxt, 'O', addr, b); +} + + +nxt_error_t +nxt_write_hword(nxt_t *nxt, nxt_addr_t addr, nxt_hword_t hw) +{ + return nxt_write_common(nxt, 'H', addr, hw); +} + + +nxt_error_t +nxt_write_word(nxt_t *nxt, nxt_addr_t addr, nxt_word_t w) +{ + return nxt_write_common(nxt, 'W', addr, w); +} + + +static nxt_error_t +nxt_read_common(nxt_t *nxt, char cmd, int len, + nxt_addr_t addr, nxt_word_t *word) +{ + char buf[20] = {0}; + nxt_word_t w; + + NXT_ERR(nxt_format_command2(buf, cmd, addr, len)); + NXT_ERR(nxt_send_str(nxt, buf)); + NXT_ERR(nxt_recv_buf(nxt, buf, len)); + + w = *((nxt_word_t*)buf); + +#ifdef _NXT_BIG_ENDIAN + /* The value returned is in little-endian byte ordering, so swap + bytes on a big-endian architecture. */ + w = (((w & 0x000000FF) << 24) + + ((w & 0x0000FF00) << 8) + + ((w & 0x00FF0000) >> 8) + + ((w & 0xFF000000) >> 24)); +#endif /* _NXT_BIG_ENDIAN */ + + *word = w; + return NXT_OK; +} + + +nxt_error_t +nxt_read_byte(nxt_t *nxt, nxt_addr_t addr, nxt_byte_t *b) +{ + nxt_word_t w; + NXT_ERR(nxt_read_common(nxt, 'o', 1, addr, &w)); + *b = (nxt_byte_t)w; + + return NXT_OK; +} + + +nxt_error_t +nxt_read_hword(nxt_t *nxt, nxt_addr_t addr, nxt_hword_t *hw) +{ + nxt_word_t w; + + NXT_ERR(nxt_read_common(nxt, 'h', 2, addr, &w)); + *hw = (nxt_hword_t)w; + + return NXT_OK; +} + + +nxt_error_t +nxt_read_word(nxt_t *nxt, nxt_addr_t addr, nxt_word_t *w) +{ + return nxt_read_common(nxt, 'w', 4, addr, w); +} + + +nxt_error_t +nxt_send_file(nxt_t *nxt, nxt_addr_t addr, char *file, unsigned short len) +{ + char buf[20]; + + NXT_ERR(nxt_format_command2(buf, 'S', addr, len)); + NXT_ERR(nxt_send_str(nxt, buf)); + NXT_ERR(nxt_send_buf(nxt, file, len)); + + return NXT_OK; +} + + +nxt_error_t +nxt_recv_file(nxt_t *nxt, nxt_addr_t addr, char *file, unsigned short len) +{ + char buf[20]; + + NXT_ERR(nxt_format_command2(buf, 'R', addr, len)); + NXT_ERR(nxt_send_str(nxt, buf)); + NXT_ERR(nxt_recv_buf(nxt, file, len+1)); + return NXT_OK; +} + + +nxt_error_t +nxt_jump(nxt_t *nxt, nxt_addr_t addr) +{ + char buf[20]; + + NXT_ERR(nxt_format_command(buf, 'G', addr)); + + NXT_ERR(nxt_send_str(nxt, buf)); + return NXT_OK; +} + + +nxt_error_t +nxt_samba_version(nxt_t *nxt, char *version) +{ + char buf[3]; + strcpy(buf, "V#"); + NXT_ERR(nxt_send_str(nxt, buf)); + NXT_ERR(nxt_recv_buf(nxt, version, 4)); + version[4] = 0; + return NXT_OK; +} diff --git a/Host/libnxt-0.3/samba.h b/Host/libnxt-0.3/samba.h new file mode 100644 index 0000000..6471b5b --- /dev/null +++ b/Host/libnxt-0.3/samba.h @@ -0,0 +1,51 @@ +/** + * NXT bootstrap interface; NXT bootstrap control functions. + * + * Copyright 2006 David Anderson + * + * 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 + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA + */ + +#ifndef __SAMBA_H__ +#define __SAMBA_H__ + +#include +#include "error.h" +#include "lowlevel.h" + +typedef uint32_t nxt_addr_t; +typedef uint32_t nxt_word_t; +typedef uint16_t nxt_hword_t; +typedef unsigned char nxt_byte_t; + +nxt_error_t nxt_write_byte(nxt_t *nxt, nxt_addr_t addr, nxt_byte_t b); +nxt_error_t nxt_write_hword(nxt_t *nxt, nxt_addr_t addr, nxt_hword_t hw); +nxt_error_t nxt_write_word(nxt_t *nxt, nxt_addr_t addr, nxt_word_t w); + +nxt_error_t nxt_read_byte(nxt_t *nxt, nxt_addr_t addr, nxt_byte_t *b); +nxt_error_t nxt_read_hword(nxt_t *nxt, nxt_addr_t addr, nxt_hword_t *hw); +nxt_error_t nxt_read_word(nxt_t *nxt, nxt_addr_t addr, nxt_word_t *w); + +nxt_error_t nxt_send_file(nxt_t *nxt, nxt_addr_t addr, + char *file, unsigned short len); +nxt_error_t nxt_recv_file(nxt_t *nxt, nxt_addr_t addr, + char *file, unsigned short len); + +nxt_error_t nxt_jump(nxt_t *nxt, nxt_addr_t addr); + +nxt_error_t nxt_samba_version(nxt_t *nxt, char *version); + +#endif /* __SAMBA_H__ */ diff --git a/Host/packages/libnxt-0.3.tar.gz b/Host/packages/libnxt-0.3.tar.gz new file mode 100644 index 0000000..c4ab4bc Binary files /dev/null and b/Host/packages/libnxt-0.3.tar.gz differ diff --git a/Host/patches/patch-libnxt-sconstruct-libusb-legacy.diff b/Host/patches/patch-libnxt-sconstruct-libusb-legacy.diff new file mode 100644 index 0000000..52b276c --- /dev/null +++ b/Host/patches/patch-libnxt-sconstruct-libusb-legacy.diff @@ -0,0 +1,71 @@ +--- SConstruct.orig 2007-04-22 12:41:39.000000000 +0800 ++++ SConstruct 2010-12-22 17:21:51.000000000 +0800 +@@ -1,6 +1,31 @@ ++##### ++# Note: For Macports, libusb-legacy need to be used? ++# I can't get it to build with libusb-compat ++#### ++ ++import os ++import os.path + from glob import glob + +-auto_libs = '' ++############################################################### ++# Utility functions. ++############################################################### ++ ++# Similar to env.WhereIs, but always searches os.environ. ++def find_on_path(filename): ++ paths = os.environ.get('PATH') ++ if not paths: ++ return None ++ for p in paths.split(':'): ++ path = os.path.abspath(os.path.join(p, filename)) ++ if os.path.isfile(path): ++ return p ++ return None ++ ++auto_libs = 'libusb-legacy' ++ ++cmdname = 'pkg-config' ++pkg_config_path = find_on_path(cmdname) + ('/%s' % cmdname) + + # Detect the system's endianness + from sys import byteorder +@@ -12,19 +37,28 @@ + BuildEnv = Environment(CCFLAGS=['-Wall', '-std=gnu99', + '-g', '-ggdb', '-D' + endian]) + if auto_libs: +- BuildEnv.ParseConfig('pkg-config --cflags --libs ' + auto_libs) ++ cmdstr = '%s --cflags --libs ' % pkg_config_path ++ BuildEnv.ParseConfig(cmdstr + ' --cflags --libs ' + auto_libs) + + BuildEnv.Command('flash_routine.h', + 'flash_routine.h.base', + './make_flash_header.py') + +-Default(BuildEnv.Library('nxt', ++BuildEnv.Append(LIBPATH='.') ++libnxt = Default(BuildEnv.Library('nxt', + [x for x in glob('*.c') + if not x.startswith('main_')], +- LIBS='usb')) ++ LIBS='usb-legacy')) ++ ++fwflash = Default(BuildEnv.Program('fwflash', 'main_fwflash.c', ++ LIBS=['usb-legacy', 'nxt'])) ++ ++fwexec = Default(BuildEnv.Program('fwexec', 'main_fwexec.c', ++ LIBS=['usb-legacy', 'nxt'])) ++ ++# the install target ++#BuildEnv.Alias("install", BuildEnv.Install(os.path.join(os.environ['DESTDIR'], "lib"), libnxt)) ++#BuildEnv.Alias("install", BuildEnv.Install(os.path.join(os.environ['DESTDIR'], "bin"), fwflash, fwexec)) ++#BuildEnv.Command("uninstall", None, Delete(FindInstalledFiles())) + +-Default(BuildEnv.Program('fwflash', 'main_fwflash.c', +- LIBS=['usb', 'nxt'], LIBPATH='.')) + +-Default(BuildEnv.Program('fwexec', 'main_fwexec.c', +- LIBS=['usb', 'nxt'], LIBPATH='.')) -- cgit v1.2.3