From 373bb32332b117236720af0ff971769fc5930ba6 Mon Sep 17 00:00:00 2001 From: James McKenzie Date: Mon, 15 Dec 2014 12:05:41 +0000 Subject: fish --- .gitignore | 6 + contrib/cp210x-program/LICENSE | 510 ++++++++++++++++++++ contrib/cp210x-program/MANIFEST.in | 4 + contrib/cp210x-program/README | 92 ++++ .../cp210x-program/build/lib/cp210x/__init__.py | 4 + contrib/cp210x-program/build/lib/cp210x/cp210x.py | 398 +++++++++++++++ contrib/cp210x-program/build/lib/cp210x/eeprom.py | 180 +++++++ contrib/cp210x-program/build/lib/cp210x/usb.py | 274 +++++++++++ .../cp210x-program/build/lib/cp210x/valuefile.py | 234 +++++++++ .../build/scripts-2.6/cp210x-program | 266 ++++++++++ contrib/cp210x-program/cp210x-program | 266 ++++++++++ contrib/cp210x-program/cp210x-program.doap | 83 ++++ contrib/cp210x-program/cp210x/__init__.py | 4 + contrib/cp210x-program/cp210x/__init__.pyc | Bin 0 -> 259 bytes contrib/cp210x-program/cp210x/cp210x.py | 398 +++++++++++++++ contrib/cp210x-program/cp210x/cp210x.pyc | Bin 0 -> 17366 bytes contrib/cp210x-program/cp210x/eeprom.py | 180 +++++++ contrib/cp210x-program/cp210x/eeprom.pyc | Bin 0 -> 9905 bytes contrib/cp210x-program/cp210x/usb.py | 274 +++++++++++ contrib/cp210x-program/cp210x/usb.pyc | Bin 0 -> 9059 bytes contrib/cp210x-program/cp210x/valuefile.py | 234 +++++++++ contrib/cp210x-program/cp210x/valuefile.pyc | Bin 0 -> 8339 bytes .../cp210x-program/doc/45-cp210x-programming.rules | 9 + contrib/cp210x-program/doc/cp210x.txt | 107 +++++ contrib/cp210x-program/doc/index.html | 129 +++++ contrib/cp210x-program/setup.py | 48 ++ kernel/code/Makefile | 15 + kernel/code/gpio.c | 49 ++ kernel/code/libradiator.c | 463 ++++++++++++++++++ kernel/code/program_radiator.c | 23 + kernel/code/radiator.c | 34 ++ kernel/code/radiator.h | 19 + kernel/code/set.c | 63 +++ kernel/code/thing.c | 207 ++++++++ kernel/cp2103-3.11.10.patch | 491 +++++++++++++++++++ userland/Makefile | 20 + userland/cp210x.c | 534 +++++++++++++++++++++ userland/cp210x.h | 97 ++++ userland/cp210x_int.h | 82 ++++ userland/libradiator.c | 364 ++++++++++++++ userland/program_radiator.c | 23 + userland/radiator.c | 34 ++ userland/radiator.h | 21 + 43 files changed, 6239 insertions(+) create mode 100644 .gitignore create mode 100644 contrib/cp210x-program/LICENSE create mode 100644 contrib/cp210x-program/MANIFEST.in create mode 100644 contrib/cp210x-program/README create mode 100644 contrib/cp210x-program/build/lib/cp210x/__init__.py create mode 100644 contrib/cp210x-program/build/lib/cp210x/cp210x.py create mode 100644 contrib/cp210x-program/build/lib/cp210x/eeprom.py create mode 100644 contrib/cp210x-program/build/lib/cp210x/usb.py create mode 100644 contrib/cp210x-program/build/lib/cp210x/valuefile.py create mode 100755 contrib/cp210x-program/build/scripts-2.6/cp210x-program create mode 100755 contrib/cp210x-program/cp210x-program create mode 100644 contrib/cp210x-program/cp210x-program.doap create mode 100644 contrib/cp210x-program/cp210x/__init__.py create mode 100644 contrib/cp210x-program/cp210x/__init__.pyc create mode 100644 contrib/cp210x-program/cp210x/cp210x.py create mode 100644 contrib/cp210x-program/cp210x/cp210x.pyc create mode 100644 contrib/cp210x-program/cp210x/eeprom.py create mode 100644 contrib/cp210x-program/cp210x/eeprom.pyc create mode 100644 contrib/cp210x-program/cp210x/usb.py create mode 100644 contrib/cp210x-program/cp210x/usb.pyc create mode 100644 contrib/cp210x-program/cp210x/valuefile.py create mode 100644 contrib/cp210x-program/cp210x/valuefile.pyc create mode 100644 contrib/cp210x-program/doc/45-cp210x-programming.rules create mode 100644 contrib/cp210x-program/doc/cp210x.txt create mode 100644 contrib/cp210x-program/doc/index.html create mode 100755 contrib/cp210x-program/setup.py create mode 100644 kernel/code/Makefile create mode 100644 kernel/code/gpio.c create mode 100644 kernel/code/libradiator.c create mode 100644 kernel/code/program_radiator.c create mode 100644 kernel/code/radiator.c create mode 100644 kernel/code/radiator.h create mode 100644 kernel/code/set.c create mode 100644 kernel/code/thing.c create mode 100644 kernel/cp2103-3.11.10.patch create mode 100644 userland/Makefile create mode 100644 userland/cp210x.c create mode 100644 userland/cp210x.h create mode 100644 userland/cp210x_int.h create mode 100644 userland/libradiator.c create mode 100644 userland/program_radiator.c create mode 100644 userland/radiator.c create mode 100644 userland/radiator.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0f841ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.hg +.hgtags +*.o +*~ +program_radiator +radiator diff --git a/contrib/cp210x-program/LICENSE b/contrib/cp210x-program/LICENSE new file mode 100644 index 0000000..2d2d780 --- /dev/null +++ b/contrib/cp210x-program/LICENSE @@ -0,0 +1,510 @@ + + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations +below. + + When we speak of free software, we are referring to freedom of use, +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 and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it +becomes a de-facto standard. To achieve this, non-free programs must +be allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, 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 library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete 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 distribute a copy of this License along with the +Library. + + 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 Library or any portion +of it, thus forming a work based on the Library, 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) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +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 Library, 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 Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you 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. + + If distribution of 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 satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at least + three years, to give the same user the materials specified in + Subsection 6a, above, for a charge no more than the cost of + performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be 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. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library 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. + + 9. 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 Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +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 with +this License. + + 11. 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 Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library 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 Library. + +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. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library 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. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser 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 Library +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 Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +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 + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "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 +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. 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 LIBRARY 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 +LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms +of the ordinary General Public License). + + To apply these terms, attach the following notices to the library. +It is safest to attach them to the start of each source file to most +effectively convey the exclusion of warranty; and each file should +have at least the "copyright" line and a pointer to where the full +notice is found. + + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or +your school, if any, to sign a "copyright disclaimer" for the library, +if necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James + Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! + + diff --git a/contrib/cp210x-program/MANIFEST.in b/contrib/cp210x-program/MANIFEST.in new file mode 100644 index 0000000..519311e --- /dev/null +++ b/contrib/cp210x-program/MANIFEST.in @@ -0,0 +1,4 @@ +include doc/*.txt +include doc/*.rules +include LICENSE +include cp210x-program.doap diff --git a/contrib/cp210x-program/README b/contrib/cp210x-program/README new file mode 100644 index 0000000..2ac42a1 --- /dev/null +++ b/contrib/cp210x-program/README @@ -0,0 +1,92 @@ + + cp210x-program + ============== + +ATTENTION: THIS VERSION OF cp210x-program IS NOT FULLY TESTED. IT MAY RENDER + YOUR CP210x USELESS OR DESTROY IT. + +Be aware that the current version is only tested on CP2102. + +The goal of this library is to provide access to the EEPROM of an Silabs CP210x +under Linux. + +The CP210x is an USB-to-serial chip used in a lot of USB devices (similar to +FTDIs and PL2303). The CP210x has a EEPROM on the chip which can be programmed +via USB. Silabs provides already a library and gui programm to program this +EEPROM, but only for windows. + +This project uses results from monitoring the USB bus when the windows library +programms an CP210x. The windows library was not disassembled for this protocol +analysis. + +When the programm is finished, a later goal would be to provide a library which +can be used to access further functions of the CP210x like the general IO pins +of the CP2103. The goal is not to provide an tty driver, such driver exists +already for linux and BSD. + +Version: 0.1 +Author: Johannes Hölzl + +Dependencies +------------ + + * Python >= 2.4 + * ctypes >= 0.9 (only when Python 2.4 is used) + * libusb 0.1 + +Since libusb is available on most Linux, Mac OS X and FreeBSD cp210x-program +should run flawlessly on these platforms. Currently it is only tested on +Linux 2.6 (Ubuntu). + +If cp210x-program should run as non-root user, add the udev rule found in +doc/45-cp210x-programming.rules to /etc/udev/rules.d. When devices with already +programmed IDs are reprogrammed at this IDs to 45-cp210x-programming.rules. + +Usage +----- + +Read EEPROM content into hexfile: +$ cp210x-program --read-cp210x -F eeprom-content.hex + +Show EEPROM content from device 002 on bus 001: +$ cp210x-program --read-cp210x -m 001/002 + +Write some data to device with vendor id 0x10C4 and product id 0xEA62: +$ cp210x-program --write-cp210x -m 10C4:EA62 \ + --set-product-string="Product String" \ + --set-max-power=100 \ + --set-bus-powered=no + +TODO +---- + + * Test on other than CP2102 + + * Implement CP2103 GIOP settings + + * Implement GUI + +Links +----- + + * CP210x Product page on Silicon Labs: + http://www.silabs.com/tgwWebApp/public/web_content/products/Microcontrollers/Interface/en/interface.htm + + * AN114 Customization Guide: + http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an144.pdf + http://www.silabs.com/public/documents/software_doc/othersoftware/Microcontrollers/Interface/en/an144sw.zip + + * AN205 CP210x Baudrate Guide: + http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an205.pdf + http://www.silabs.com/public/documents/software_doc/othersoftware/Microcontrollers/en/AN205SW.zip + + * AN223 Port Configuration and GPIO for CP210x + http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an223.pdf + http://www.silabs.com/public/documents/software_doc/othersoftware/Microcontrollers/Interface/en/AN223SW.zip + +License +------- + +The python package 'cp210x' and the python script 'cp210x-program' are provided +under the terms of the GNU LGPL. See LICENSE. + diff --git a/contrib/cp210x-program/build/lib/cp210x/__init__.py b/contrib/cp210x-program/build/lib/cp210x/__init__.py new file mode 100644 index 0000000..b761032 --- /dev/null +++ b/contrib/cp210x-program/build/lib/cp210x/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +__author__ = "Johannes Hölzl " +__license__ = "GNU LGPL" diff --git a/contrib/cp210x-program/build/lib/cp210x/cp210x.py b/contrib/cp210x-program/build/lib/cp210x/cp210x.py new file mode 100644 index 0000000..c51cb35 --- /dev/null +++ b/contrib/cp210x-program/build/lib/cp210x/cp210x.py @@ -0,0 +1,398 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. +"""Provides access to the EEPROM of Silabs CP210x devices + +The following classes are available: + +class Cp210xProgrammer: + Provides direct access to the CP2101, can be used to write single data + directly or via an EEPROM image. + +class EEPROM: + Can be used to read or write a hex file containing the EEPROM content + of an CP2101. Provides also access to the single fields in the EEPROM. +""" + +import ctypes +import usb + +__all__ = ['Cp210xProgrammer', 'Cp210xError'] + +CP2101_UART = 0x00 +CP2101_CONFIG = 0xFF + +CP2101_UART_ENABLE = 0x0001 +CP2101_UART_DISABLE = 0x0000 + +REG_VENDOR_ID = 0x3701 +REG_PRODUCT_ID = 0x3702 +REG_PRODUCT_STRING = 0x3703 +REG_SERIAL_NUMBER = 0x3704 +REG_CFG_ATTRIBUTES = 0x3705 +REG_MAX_POWER = 0x3706 +REG_VERSION = 0x3707 +REG_UNKNOWN = 0x3708 +REG_EEPROM = 0x3709 +REG_LOCK_VALUE = 0x370A +REG_PART_NUMBER = 0x370B + +SIZE_EEPROM = 0x0400 +SIZE_PRODUCT_STRING = 0x007D +SIZE_SERIAL_NUMBER = 0x003F +SIZE_BAUDRATES = 32 +SIZE_BAUDRATE_CFG = 10 +SIZE_BAUDRATE_TABLE = SIZE_BAUDRATES * SIZE_BAUDRATE_CFG +SIZE_VENDOR_STRING = 24 + +LCK_LOCKED = 0x00 +LCK_UNLOCKED = 0xFF + +VID_SILABS = 0x10C4 +PID_CP210x = 0xEA60 + +VALUES = [ + ('product_string', 'string'), + ('serial_number', 'string'), + ('product_id', 'id'), + ('vendor_id', 'id'), + ('version', 'version'), + ('bus_powered', 'boolean'), + ('max_power', 'int'), + ('locked', 'boolean'), + ('part_number', 'int'), + ('vendor_string', 'string'), + ('baudrate_table', 'list'), +] + +def iif(v, a, b): + if v: + return a + else: + return b + +def to_div2(p): + value = int(p / 2) + if (value * 2) < p: + value += 1 + return value + +def to_bcd(i): + assert i >= 0 and i <= 99 + return (i // 10) << 4 | (i % 10) + +def to_bcd2( (i, j) ): + return to_bcd(i) << 8 | to_bcd(j) + +def from_bcd(num): + return num & 0x0F + (num >> 4) * 10 + +def from_bcd2(data): + return (from_bcd(data >> 8), from_bcd(data & 0xFF)) + +def from_binary(data, le=True): + value = 0 + if le: + data = data[::-1] + for byte in data: + value = value << 8 | ord(byte) + return value + +def to_binary(value, size=2, le=True): + data = '' + for i in range(size): + data += chr(value & 0xFF) + value >>= 8 + if le: + return data + else: + return data[::-1] + +def parse_baudrate_cfg(data): + return (from_binary(data[0:2], le=False), + from_binary(data[2:4], le=False), + from_binary(data[4:5]), + from_binary(data[6:10])) + +def build_baudrate_cfg(baudgen, timer0reload, prescaler, baudrate): + return (to_binary(baudgen, le=False) + to_binary(timer0reload, le=False) + + to_binary(prescaler, 1) + '\x00' + to_binary(baudrate, 4)) + +class Cp210xError(IOError): + pass + +class DeviceLocked(Cp210xError): + pass + +class Cp210xProgrammer(object): + """Program an Silabs CP2101, CP2102 or CP2103 + + This modul provides access to Silabs CP210x devices to set some USB + descriptor fields and some USB descriptor strings. + + The following fields can be set: + + * Vendor ID + * Product ID + * Product String + * Serial Number + * Device Version + * Bus Powered + * max. Power consumption + + Either use libusb to find a device, and provide the device description + to the constructor, or use Cp210xProgrammer.list_device() to list all + devices matching certain pattern. + + To progamm the device open() it, set the data, and close() it. To have the + changed fields reread call reset() before closing it. + """ + + TIMEOUT = 300 #ms + + @classmethod + def list_devices(self, patterns=[{ 'idVendor': VID_SILABS, + 'idProduct': PID_CP210x }]): + """Yields a list of devices matching certain patterns. + + param patterns: This must be a list of dictionaries or pairs of string. + Each device in the usb tree is matched against all pattern in the + list. + + When an item is a dictionary all fields of the descriptors + are compared against the corresponding values in the dictionary. If + each value is equal, the device is yielded. + + When an item is a pair of strings. The first string must be the + dirname of the bus and the second string the filename of the device. + + For example: + + >> list(Cp210xProgrammer.list_device([{ 'idVendor': VID_SILABS, + 'idProduct': PID_CP210x }])) + [device(...)] + + """ + + usb.find_busses() + usb.find_devices() + + bus = usb.get_busses() + while bus: + dev = bus.contents.devices + while dev: + for pattern in patterns: + if isinstance(pattern, dict): + for name, value in pattern.items(): + if getattr(dev.contents.descriptor, name) != value: + break + else: + yield self(dev) + break + elif isinstance(pattern, tuple): + if (bus.contents.dirname == pattern[0] and + dev.contents.filename == pattern[1]): + yield self(dev) + break + dev = dev.contents.next + bus = bus.contents.next + + def __init__(self, dev_info): + self.dev_info = dev_info + self.handle = None + self._locked = None + + def open(self): + """Opens the device. + + Only after an successful call to open() data can be read from and + written to the device. + + Claims all resources associated with this device. + """ + self.handle = usb.open(self.dev_info) + if self.handle == 0: + self.handle = None + raise Cp210xError("Can't open device.") + usb.set_configuration(self.handle, 1) + usb.claim_interface(self.handle, 0) + + def reset(self): + """Force the USB stack to reset the device. + + Resets the device through an hard reset over the port to which the + device is connected. After that happend the EEPROM content in the device + is reread and the device's descriptors are the one written to it. + """ + assert self.handle is not None + usb.reset(self.handle) + + def close(self): + """Closes the device. + + Releases all resources associated with this device. + """ + assert self.handle is not None + usb.release_interface(self.handle, 0) + usb.close(self.handle) + self.handle = None + + def __del__(self): + if self.handle is not None: + self.close() + + def _set_config(self, value, index=0, data=None, request=CP2101_CONFIG): + assert self.handle is not None + if self.get_locked(): + raise DeviceLocked() + + if data is not None: + data_length = len(data) + else: + data_length = 0 + res = usb.control_msg(self.handle, usb.ENDPOINT_OUT | usb.TYPE_VENDOR, + request, value, index, data, data_length, + self.TIMEOUT) + if res < 0: + raise Cp210xError("Unable to send request %04X result=%d" + % (value, res)) + + def _set_config_string(self, value, content, max_length): + assert isinstance(content, basestring) + encoded = content.encode('utf-16-le') + assert len(encoded) <= max_length + self._set_config(value, data=chr(len(encoded) + 2) + "\x03" + encoded) + + def _get_config(self, value, length, index=0, request=CP2101_CONFIG): + assert self.handle is not None + data = ctypes.create_string_buffer(length) + res = usb.control_msg(self.handle, usb.ENDPOINT_IN | usb.TYPE_VENDOR, + request, value, index, data, length, + self.TIMEOUT) + if res < 0: + raise Cp210xError("Unable to send request, %04X result=%d" + % (value, res)) + return data.raw[:res] + + def _get_int8_config(self, value, index=0, request=CP2101_CONFIG): + return ord(self._get_config(value, 1, index=index, request=request)) + + def _get_int16_config(self, value, index=0, request=CP2101_CONFIG): + data = self._get_config(value, 2, index=index, request=request) + return ord(data[0]) << 8 | ord(data[1]) + + def get_eeprom_content(self): + """Reads the entire EEPROM content as one big 1024-byte blob. + """ + return self._get_config(REG_EEPROM, SIZE_EEPROM) + + def get_baudrate_content(self): + """Return the baudrate table as binary data. + """ + return self._get_config(REG_EEPROM, SIZE_BAUDRATE_TABLE) + + def get_baudrate_table(self): + """Returns the baudrate table. + + A list containing 4-tuples are returnes. + Each tuple containes the following data: + + * BaudGen: Value used to generate the real baudrate. + * Time0Reset: Value used to generate the usb timeout. + * Prescaler: Used to generate the real baudrate. + * Baudrate: The baudrate which activates this entry. + """ + data = self.get_baudrate_content() + return [parse_baudrate_cfg(data[pos:pos+SIZE_BAUDRATE_CFG]) + for pos in range(0, SIZE_BAUDRATE_TABLE, SIZE_BAUDRATE_CFG)] + + def set_baudrate_table(self, baudrates): + """Writes the baudrate table. + + See get_baudrate_table() for the structure of the table. + """ + assert len(baudrates) == SIZE_BAUDRATES + self.set_baudrate_content(data=''.join(build_baudrate_cfg(*cfg) + for cfg in baudrates)) + baudrate_table = property(get_baudrate_table, set_baudrate_table) + + def get_part_number(self): + """ The part number of the device. + + Returns: 1 for an CP2101 + 2 for an CP2102 + 3 for an CP2103 + """ + return self._get_int8_config(REG_PART_NUMBER) + + def get_locked(self): + """ The lock value of the device. + + When True is returnes no data can be written to the device. + """ + if self._locked is None: + self._locked = self._get_int8_config(REG_LOCK_VALUE) == LCK_LOCKED + return self._locked + + def set_eeprom_content(self, content): + """Writes an 1024-byte blob to the EEPROM + """ + assert len(content) == SIZE_EEPROM, ("EEPROM data must be %i bytes." + % SIZE_EEPROM) + assert isinstance(content, str), "EEPROM data must be string." + self._set_config(REG_EEPROM, data=content) + + def set_product_id(self, pid): + """Sets the Product ID + """ + assert pid > 0x0000 and pid < 0xFFFF + self._set_config(REG_PRODUCT_ID, pid) + + def set_vendor_id(self, vid): + """Sets the Vendor ID + """ + assert vid > 0x0000 and vid < 0xFFFF + self._set_config(REG_VENDOR_ID, vid) + + def set_product_string(self, product_string): + """Sets the product string. + + Be aware that the string will be stored as UTF-16 encoded and should not + exceed SIZE_PRODUCT_STRING + """ + self._set_config_string(REG_PRODUCT_STRING, product_string, + SIZE_PRODUCT_STRING) + + def set_serial_number(self, serial_number): + self._set_config_string(REG_SERIAL_NUMBER, serial_number, + SIZE_SERIAL_NUMBER) + + def set_max_power(self, max_power): + assert max_power >= 0 and max_power <= 500 + self._set_config(REG_MAX_POWER, to_div2(max_power)) + + def set_bus_powered(self, bus_powered): + if bus_powered: + self._set_config(REG_CFG_ATTRIBUTES, 0xC0) + else: + self._set_config(REG_CFG_ATTRIBUTES, 0x80) + + def set_version(self, version): + self._set_config(REG_VERSION, to_bcd2(version)) + + def set_locked(self, locked): + """ The lock value of the device. + + When True is returnes no data can be written to the device. + """ + if locked: + self._set_config(REG_LOCK_VALUE, LCK_LOCKED) + else: + self._set_config(REG_LOCK_VALUE, LCK_UNLOCKED) + + def set_values(self, values): + for name, value in values.items(): + if name not in ['part_number', 'vendor_string']: + getattr(self, "set_" + name) (value) + diff --git a/contrib/cp210x-program/build/lib/cp210x/eeprom.py b/contrib/cp210x-program/build/lib/cp210x/eeprom.py new file mode 100644 index 0000000..25f0372 --- /dev/null +++ b/contrib/cp210x-program/build/lib/cp210x/eeprom.py @@ -0,0 +1,180 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. + +import cp210x +from cp210x import from_binary, to_binary, iif, VALUES + +__all__ = ['EEPROM', 'HexFileError'] + +POS_BAUDRATE_TABLE = 0x0000 +POS_PART_NUMBER = 0x01FF +POS_PRODUCT_STRING = 0x0208 +POS_SERIAL_NUMBER = 0x0307 +POS_PRODUCT_ID = 0x0390 +POS_VENDOR_ID = 0x0392 +POS_VERSION = 0x0394 +POS_CFG_ATTRIBUTES = 0x03A1 +POS_MAX_POWER = 0x03A2 +POS_VENDOR_STRING = 0x03C3 +POS_LOCK_VALUE = 0x03FF + +class HexFileError(StandardError): + pass + +def checksum(line): + return sum(ord(c) for c in line) & 0xFF + +def _int_value(position, size, read=lambda x:x, write=lambda x:x): + def get(self): + return read(from_binary(self.get(position, size))) + def set(self, value): + self.set(position, to_binary(write(value), size)) + return property(get, set) + +def _str_value(position, max_size): + def get(self): + size = from_binary(self.get(position, 1)) + assert size <= (max_size + 2) and size >= 2 + assert self.get(position + 1, 1) == '\x03', "Missing 0x03 at %04X" % (position + 1) + return self.get(position + 2, size - 2).decode('utf-16-le') + + def set(self, value): + encoded = value.encode('utf-16-le') + assert len(encoded) <= max_size + self.set(position, chr(len(encoded) + 2) + '\x03' + encoded) + + return property(get, set) + +class EEPROM(object): + START_ADDRESS = 0x3600 + def __init__(self, content=None): + if isinstance(content, str) or content is None: + assert content is None or len(content) == cp210x.SIZE_EEPROM + self.content = content + elif isinstance(content, cp210x.Cp210xProgrammer): + self.content = content.get_eeprom_content() + else: + self.parse_hex_file(content.read()) + + def write_to_cp210x(self, cp210xDevice): + cp210xDevice.set_eeprom_content(self.content) + + def parse_hex_file(self, hex_content): + self.content = '' + address = self.START_ADDRESS + for tag in hex_content.split('\n'): + if not tag.startswith(':'): + raise HexFileError("Line doesn't start with ':'") + + try: + content = tag[1:].decode('hex') + except TypeError: + raise HexFileError("Hex data expected") + + if len(content) < 5: + raise HexFileError("Line to short") + + if checksum(content) != 0: + raise HexFileError("Checksum error") + + size = from_binary(content[0]) + tag_address = from_binary(content[1:3], le=False) + tag_type = from_binary(content[3:4]) + line = content[4:-1] + + if tag_type == 0x00: + if tag_address != address: + raise HexFileError("Expected address %04X but found %04X" + % (address, tag_address)) + self.content += line + address += len(line) + elif tag_type == 0x01: + if size != 0 or len(line) != 0: + raise HexFileError("Defekt end tag") + break + + else: + raise HexFileError("Unknown tag type %02X" % tag_type) + + def build_hex_file(self): + for tag_start in range(0, len(self.content), 0x10): + line = self.content[tag_start:tag_start+0x10] + address = self.START_ADDRESS + tag_start + tag = (to_binary(len(line), 1) + + to_binary(address, le=False) + + '\x00' + + line) + cs = checksum(tag) + if cs == 0: + tag += '\x00' + else: + tag += chr(0x100 - cs) + yield ":%s\n" % tag.encode('hex') + yield ":00000001FF\n" + + def write_hex_file(self, f): + if isinstance(f, str): + f = file(f, 'wb') + do_close = True + else: + do_close = False + for line in self.build_hex_file(): + f.write(line) + if do_close: + f.close() + + def read_hex_file(self, f): + if isinstance(f, str): + f = file(f, 'rb') + do_close = True + else: + do_close = False + self.parse_hex_file(f.read()) + if do_close: + f.close() + + def get(self, pos, length): + return self.content[pos:pos+length] + + def set(self, pos, data): + self.content = (self.content[:pos] + + data + + self.content[pos + len(data):]) + + def _get_baudrate_table(self): + dat = self.get(POS_BAUDRATE_TABLE, cp210x.SIZE_BAUDRATE_TABLE) + return [cp210x.parse_baudrate_cfg(dat[pos:pos+cp210x.SIZE_BAUDRATE_CFG]) + for pos in range(0, cp210x.SIZE_BAUDRATE_TABLE, + cp210x.SIZE_BAUDRATE_CFG)] + def _set_baudrate_table(self, baudrates): + assert len(baudrates) == cp210x.SIZE_BAUDRATES + self.set(POS_BAUDRATE_TABLE, + ''.join(cp210x.build_baudrate_cfg(*cfg) for cfg in baudrates)) + baudrate_table = property(_get_baudrate_table, _set_baudrate_table) + product_string = _str_value(POS_PRODUCT_STRING, cp210x.SIZE_PRODUCT_STRING) + serial_number = _str_value(POS_SERIAL_NUMBER, cp210x.SIZE_SERIAL_NUMBER) + part_number = _int_value(POS_PART_NUMBER, 1) + product_id = _int_value(POS_PRODUCT_ID, 2) + vendor_id = _int_value(POS_VENDOR_ID, 2) + version = _int_value(POS_VERSION, 2, + cp210x.from_bcd2, cp210x.to_bcd2) + bus_powered = _int_value(POS_CFG_ATTRIBUTES, 1, + lambda a: bool(a & 0x40), + lambda a: iif(a, 0xC0, 0x80)) + max_power = _int_value(POS_MAX_POWER, 1, lambda p: p*2, cp210x.to_div2) + vendor_string = _str_value(POS_VENDOR_STRING, cp210x.SIZE_VENDOR_STRING) + locked = _int_value(POS_LOCK_VALUE, 1, + lambda l: l == cp210x.LCK_LOCKED, + lambda b: iif(b, cp210x.LCK_LOCKED, + cp210x.LCK_UNLOCKED)) + + def get_values(self): + return dict((name, getattr(self, name)) for name, type in VALUES) + + def set_values(self, values): + for name, value in values.items(): + setattr(self, name, value) + + diff --git a/contrib/cp210x-program/build/lib/cp210x/usb.py b/contrib/cp210x-program/build/lib/cp210x/usb.py new file mode 100644 index 0000000..be70f82 --- /dev/null +++ b/contrib/cp210x-program/build/lib/cp210x/usb.py @@ -0,0 +1,274 @@ +# Python interface for "usb.h" version 0.1.4.4.4 +# +# Copyright (c) 2005 Robert Hoelzl +# Copyright (c) 2007 Johannes Hoelzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. + +from ctypes import * +import sys + +class LibUsbNotInstalled(OSError): + pass + +try: + if sys.platform == 'darwin': + PATH_MAX = 1024 + dll=cdll.LoadLibrary("libusb.dylib") + elif sys.platform == 'linux2': + PATH_MAX = 4096 + dll=cdll.LoadLibrary("libusb.so") + else: + raise NotImplementedError("Platform %s not supported by usb.py" % sys.platform) +except OSError: + raise LibUsbNotInstalled() + + +# helper functions +def func(f, *args, **retval): + f.restype = retval.get('retval', None) + f.argtypes = args + if retval.has_key('rename'): globals()[retval['rename']] = f + else: globals()[f.__name__[4:]] = f + +# constants +CLASS_PER_INTERFACE = 0 +USB_CLASS_AUDIO = 1 +CLASS_COMM = 2 +CLASS_HID = 3 +CLASS_PRINTER = 7 +CLASS_PTP = 6 +CLASS_MASS_STORAGE = 8 +CLASS_HUB = 9 +CLASS_DATA = 10 +CLASS_VENDOR_SPEC = 0xff + +DT_DEVICE = 0x01 +DT_CONFIG = 0x02 +DT_STRING = 0x03 +DT_INTERFACE = 0x04 +DT_ENDPOINT = 0x05 + +DT_HID = 0x21 +DT_REPORT = 0x22 +DT_PHYSICAL = 0x23 +DT_HUB = 0x29 + +DT_DEVICE_SIZE = 18 +DT_CONFIG_SIZE = 9 +DT_INTERFACE_SIZE = 9 +DT_ENDPOINT_SIZE = 7 +DT_ENDPOINT_AUDIO_SIZE = 9 # Audio extension +DT_HUB_NONVAR_SIZE = 7 + + +class descriptor_header(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8) ] + + +class string_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("wData", c_uint*1) ] + +class hid_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bcdHID", c_uint16), + ("bCountryCode", c_uint8), + ("bNumDescriptors", c_uint8) ] + +MAXENDPOINTS = 32 +class endpoint_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bEndpointAddress", c_uint8), + ("bmAttributes", c_uint8), + ("wMaxPacketSize", c_uint16), + ("bInterval", c_uint8), + ("bRefresh", c_uint8), + ("bSynchAddress", c_uint8), + + ("extra", POINTER(c_uint8)), + ("extralen", c_int) ] + + +ENDPOINT_ADDRESS_MASK = 0x0f # in bEndpointAddress +ENDPOINT_DIR_MASK = 0x80 + +ENDPOINT_TYPE_MASK = 0x03 # in bmAttributes +ENDPOINT_TYPE_CONTROL = 0 +ENDPOINT_TYPE_ISOCHRONOUS = 1 +ENDPOINT_TYPE_BULK = 2 +ENDPOINT_TYPE_INTERRUPT = 3 + +MAXINTERFACES = 32 +class interface_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bInterfaceNumber", c_uint8), + ("bAlternateSetting", c_uint8), + ("bNumEndpoints", c_uint8), + ("bInterfaceClass", c_uint8), + ("bInterfaceSubClass", c_uint8), + ("bInterfaceProtocol", c_uint8), + ("iInterface", c_uint8), + + ("endpoint", POINTER(endpoint_descriptor)), + + ("extra", POINTER(c_uint8)), + ("extralen", c_int) ] + +MAXALTSETTING = 128 # Hard limit +class interface(Structure): _fields_ = [ + ("altsetting", POINTER(interface_descriptor)), + + ("num_altsetting", c_int) ] + +MAXCONFIG = 8 +class config_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("wTotalLength", c_uint16), + ("bNumInterfaces", c_uint8), + ("bConfigurationValue", c_uint8), + ("iConfiguration", c_uint16), + ("bmAttributes", c_uint8), + ("MaxPower", c_uint8), + + ("interface", POINTER(interface)), + + ("extra", POINTER(c_uint8)), # Extra descriptors + ("extralen", c_int) ] + +class device_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bcdUSB", c_uint16), + ("bDeviceClass", c_uint8), + ("bDeviceSubClass", c_uint8), + ("bDeviceProtocol", c_uint8), + ("bMaxPacketSize0", c_uint8), + ("idVendor", c_uint16), + ("idProduct", c_uint16), + ("bcdDevice", c_uint16), + ("iManufacturer", c_uint8), + ("iProduct", c_uint8), + ("iSerialNumber", c_uint8), + ("bNumConfigurations", c_uint8) ] + +class ctrl_setup(Structure): _fields_ = [ + ("bRequestType", c_uint8), + ("bRequest", c_uint8), + ("wValue", c_uint16), + ("wIndex", c_uint16), + ("wLength", c_uint16) ] + + +REQ_GET_STATUS = 0x00 +REQ_CLEAR_FEATURE = 0x01 +# 0x02 is reserved +REQ_SET_FEATURE = 0x03 +# 0x04 is reserved +REQ_SET_ADDRESS = 0x05 +REQ_GET_DESCRIPTOR = 0x06 +REQ_SET_DESCRIPTOR = 0x07 +REQ_GET_CONFIGURATION = 0x08 +REQ_SET_CONFIGURATION = 0x09 +REQ_GET_INTERFACE = 0x0A +REQ_SET_INTERFACE = 0x0B +REQ_SYNCH_FRAME = 0x0C + +TYPE_STANDARD = (0x00 << 5) +TYPE_CLASS = (0x01 << 5) +TYPE_VENDOR = (0x02 << 5) +TYPE_RESERVED = (0x03 << 5) + +RECIP_DEVICE = 0x00 +RECIP_INTERFACE = 0x01 +RECIP_ENDPOINT = 0x02 +RECIP_OTHER = 0x03 + +ENDPOINT_IN = 0x80 +ENDPOINT_OUT = 0x00 + +# Error codes +ERROR_BEGIN = 500000 + +#if 1 +#define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0) +#else +#define USB_LE16_TO_CPU(x) +#endif + +device_p = POINTER("device") +bus_p = POINTER("bus") + +class device(Structure): _fields_ = [ + ("next", device_p), + ("prev", device_p), + ("filename", c_char*(PATH_MAX + 1)), + + ("bus", bus_p), + ("descriptor", device_descriptor), + ("config", POINTER(config_descriptor)), + + ("dev", c_void_p), # Darwin support + + ("devnum", c_char), + + ("num_children", c_uint8), + ("children", POINTER(device_p)) ] +SetPointerType(device_p, device) + +class bus(Structure): _fields_ = [ + ("next", bus_p), + ("prev", bus_p), + + ("dirname", c_char*(PATH_MAX + 1)), + + ("devices", device_p), + ("location", c_uint), + + ("root_dev", device_p) ] +SetPointerType(bus_p, bus) + + +dev_handle_p = c_void_p + + +func(dll.usb_open, device_p, retval=dev_handle_p, rename="_open") +func(dll.usb_close, dev_handle_p, retval=c_int) +func(dll.usb_get_string, dev_handle_p, c_int, c_int, c_char_p, c_int, retval=c_int) +func(dll.usb_get_string_simple, dev_handle_p, c_int, c_char_p, c_int, retval=c_int) +func(dll.usb_get_descriptor_by_endpoint, dev_handle_p, c_int, c_uint8, c_uint8, c_void_p, c_int, retval=c_int) +func(dll.usb_get_descriptor, dev_handle_p, c_uint8, c_uint8, c_void_p, c_int, retval=c_int) + +func(dll.usb_bulk_write, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_bulk_read, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_interrupt_write, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_interrupt_read, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_control_msg, dev_handle_p, c_int, c_int, c_int, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_set_configuration, dev_handle_p, c_int, retval=c_int) +func(dll.usb_claim_interface, dev_handle_p, c_int, retval=c_int) +func(dll.usb_release_interface, dev_handle_p, c_int, retval=c_int) +func(dll.usb_set_altinterface, dev_handle_p, c_int, retval=c_int) +func(dll.usb_resetep, dev_handle_p, c_uint16, retval=c_int) +func(dll.usb_clear_halt, dev_handle_p, c_uint16, retval=c_int) +func(dll.usb_reset, dev_handle_p, retval=c_int) + +func(dll.usb_strerror, retval=c_char_p) + +func(dll.usb_init) +func(dll.usb_set_debug, c_int) +func(dll.usb_find_busses, retval=c_int) +func(dll.usb_find_devices, retval=c_int) +func(dll.usb_device, dev_handle_p, retval=device_p, rename="get_device") +func(dll.usb_get_busses, retval=bus_p) + +func(dll.usb_detach_kernel_driver_np, dev_handle_p, c_int, retval=c_int) + +# workaround for bug in ctypes 0.9.6 (cannot create functions with c_void_p as retval) +def open(dev): + return cast(_open(dev), dev_handle_p) diff --git a/contrib/cp210x-program/build/lib/cp210x/valuefile.py b/contrib/cp210x-program/build/lib/cp210x/valuefile.py new file mode 100644 index 0000000..ae665d2 --- /dev/null +++ b/contrib/cp210x-program/build/lib/cp210x/valuefile.py @@ -0,0 +1,234 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. + +# For documentation of the baudrate table see: +# +# [AN205] Silicon Labs Application Note 205 Rev. 0.3 +# http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an205.pdf + +import re +from ConfigParser import ConfigParser + +import cp210x +from cp210x import VALUES, SIZE_BAUDRATES + +__all__ = ['read_file', 'write_file', 'update_values', 'PrescalerIsZero', + 'ValuesFileError'] + +class ValuesError(StandardError): + pass + +class PrescalerIsZero(ValuesError): + pass + +class ValuesFileError(ValuesError): + pass + +version_pattern = re.compile(r'^\s*(\d\d?)\.(\d\d?)\s*$') +def read_version(s): + match = version_pattern.match(s) + if match is None: + raise ValueError("Version does not match 'xx.yy'") + return (int(match.group(1)), int(match.group(2))) +def write_version(v): + return "%d.%02d" % v + +def read_hex(s): + return int(s.strip(), 16) + +def write_hex(num): + return "%04X" % num + +def write_bool(b): + if b: + return 'yes' + else: + return 'no' + +def read_bool(s): + s = s.strip().lower() + if s not in ['true', 'yes', 'false', 'no']: + raise ValueError("Boolean must be either 'true', 'yes', 'false' or 'no'.") + return s in ['true', 'yes'] + +def read_baudrate_info(s): + values = s.split(',') + if len(values) != 3: + raise ValueError("Baudrate info must be three comma-separated items") + try: + baudgen = read_hex(values[0]) + except ValueError: + raise ValueError("The first baudrate info must be a hex-value") + try: + timer0 = read_hex(values[1]) + except ValueError: + raise ValueError("The second baudrate info must be a hex-value") + try: + prescale = int(values[2]) + except ValueError: + raise ValueError("The thirdbaudrate info must be a number") + return (baudgen, timer0, prescale) + +TYPES = { + 'boolean': (read_bool, write_bool), + 'int': (int, str), + 'id': (read_hex, write_hex), + 'string': (str, str), + 'version': (read_version, write_version), +} + +def read_file(fp): + cp = ConfigParser() + if isinstance(fp, str): + cp.read([fp]) + else: + cp.readfp(fp) + + values = {} + + for name, type in VALUES: + if name is 'baudrate_table': + continue + reader, _ = TYPES[type] + if cp.has_option('usb device', name): + try: + values[name] = reader(cp.get('usb device', name)) + except ValueError, err: + raise ValuesFileError("Key '%s': %s" % (name, str(err))) + + if cp.has_section('baudrate table'): + baudrate_table = [] + for name, value in cp.items('baudrate table'): + try: + baudrate = int(name) + except ValueError: + raise ValuesFileError("Key names in 'baudrate table' must be" + " baudrate numbers.") + try: + baudrate_table.append(read_baudrate_info(value) + (baudrate, )) + except ValueError, err: + raise ValuesFileError("Wrong baudrate info %i: %s" + % (baudrate, str(err))) + baudrate_table.sort(key=(lambda i: i[3]), reverse=True) + + values['baudrate_table'] = baudrate_table + return values + +def write_file(fp, values): + fp.write("[usb device]\n") + + for name, type in VALUES: + if name == 'baudrate_table': + continue + _, writer = TYPES[type] + if name in values: + fp.write("%s = %s\n" % (name, writer(values[name]))) + + if 'baudrate_table' in values: + fp.write("\n") + fp.write("[baudrate table]\n") + for (baudgen, timegen, prescaler, + baudrate) in sorted(values['baudrate_table'], key=(lambda i: i[3]), + reverse=True): + fp.write("%7d = %04X, %04X, %d # %s\n" + % (baudrate, baudgen, timegen, prescaler, + show_baudrate(baudgen, timegen, prescaler))) + +def calc_baudrate(baudgen, timegen, prescaler): + # This formulas are from AN205 page 5. + if prescaler == 0: + raise PrescalerIsZero("Prescaler is 0") + baudrate = (24000000. / prescaler) / (0x10000 - baudgen) + return (baudrate, (0x10000 - timegen) * 2) + +def show_baudrate(baudgen, timegen, prescaler): + try: + baudrate, timeout = calc_baudrate(baudgen, timegen, prescaler) + except PrescalerIsZero: + return "Wrong data, Prescaler is 0." + if timeout >= 1000: + timeout = "%1.3f ms" % (float(timeout) / 1000) + else: + timeout = "%d us" % timeout + if baudrate is None: + return ", %s" % (baudrate, timeout) + else: + return "%7.0f Baud, %s" % (baudrate, timeout) + +def update_values(v, new, dev): + old_baudrate_table = v.get('baudrate_table') + new_baudrate_table = new.get('baudrate_table') + + v.update(new) + + # update baudrate table + # it is needed, that the baudrate table has 32 entries when it is written + # to the eeprom or device. + if ((old_baudrate_table is not None or new_baudrate_table is not None) and + (new_baudrate_table is None or + len(new_baudrate_table) < SIZE_BAUDRATES)): + + if old_baudrate_table is not None: + if len(old_baudrate_table) < SIZE_BAUDRATES: + baudrate_table = old_baudrate_table + else: + baudrate_table = list(merge_baudrate_table(dev.baudrate_table, + old_baudrate_table)) + else: + baudrate_table = dev.baudrate_table + + if new_baudrate_table: + baudrate_table = list(merge_baudrate_table(baudrate_table, + new_baudrate_table)) + v['baudrate_table'] = baudrate_table + + + +def merge_baudrate_table(old, new): + for (old_info, (start, stop)) in zip(old, REQUEST_BAUDRATE_RANGES): + for baudgen, timer, prescaler, baudrate in new: + if ((start is None or baudrate <= start) and + baudrate >= stop): + yield (baudgen, timer, prescaler, baudrate) + break + else: + yield old_info + +REQUEST_BAUDRATE_RANGES = [ +# The table data is from AN205 Table 1 on page 1. +# Start End Default Baudrate + (None, 2457601), # Undefined + (2457600, 1474561), # Undefined + (1474560, 1053258), # Undefined + (1053257, 670255), # 921600 + ( 670254, 567139), # 576000 + ( 567138, 491521), # 500000 + ( 491520, 273067), # 460800 + ( 273066, 254235), # 256000 + ( 254234, 237833), # 250000 + ( 237832, 156869), # 230400 + ( 156868, 129348), # 153600 + ( 129347, 117029), # 128000 + ( 117028, 77609), # 115200 + ( 77608, 64112), # 76800 + ( 64111, 58054), # 64000 + ( 58053, 56281), # 57600 + ( 56280, 51559), # 56000 + ( 51558, 38602), # 51200 + ( 38601, 28913), # 38400 + ( 28912, 19251), # 28800 + ( 19250, 16063), # 19200 + ( 16062, 14429), # 16000 + ( 14428, 9613), # 14400 + ( 9612, 7208), # 9600 + ( 7207, 4804), # 7200 + ( 4803, 4001), # 4800 + ( 4000, 2401), # 4000 + ( 2400, 1801), # 2400 + ( 1800, 1201), # 1800 + ( 1200, 601), # 1200 + ( 600, 301), # 600 + ( 300, 57), # 300 +] diff --git a/contrib/cp210x-program/build/scripts-2.6/cp210x-program b/contrib/cp210x-program/build/scripts-2.6/cp210x-program new file mode 100755 index 0000000..524a123 --- /dev/null +++ b/contrib/cp210x-program/build/scripts-2.6/cp210x-program @@ -0,0 +1,266 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. +"""\ +Provides access to the EEPROM in an Silabs CP210x. The data can be directly to +the device read or written. +""" +__author__ = "Johannes Hölzl " +__license__ = "GNU LGPL" +__version__ = "0.2" + +import sys +import re +import string +import traceback +import optparse + +from cp210x import usb, valuefile, cp210x +from cp210x.eeprom import EEPROM, HexFileError +from cp210x.valuefile import read_baudrate_info, update_values, ValuesFileError +TRANS_UNDERSCORE = string.maketrans('_', '-') + +ERR_OK = 0 +ERR_WRONG_INPUT = -1 +ERR_INTERNAL = -2 +ERR_DEVICE_LOCKED = -3 +ERR_DEVICE_NOT_FOUND = -4 +ERR_DEVICE_ERROR = -5 + +def error(message, retval=-1): + sys.stderr.write(message + "\n") + sys.exit(retval) + +class Option(optparse.Option): + TYPES = list(optparse.Option.TYPES) + TYPE_CHECKER = dict(optparse.Option.TYPE_CHECKER) + for type, (reader, _) in valuefile.TYPES.items(): + if type in TYPES: + continue + TYPES.append(type) + def checker(self, name, value, reader=reader): + try: + return reader(value) + except ValueError, err: + raise optparse.OptionValueError("option %s: %s" % (name, + str(err))) + TYPE_CHECKER[type] = checker + +class OptionParser(optparse.OptionParser): + def error(self, msg): + error(msg, ERR_WRONG_INPUT) + + def __init__(self, *args, **kwargs): + if 'option_class' not in kwargs: + kwargs['option_class'] = Option + optparse.OptionParser.__init__(self, *args, **kwargs) + +def input_file(arg): + if arg is None or arg == '-': + return sys.stdin + else: + return file(arg, 'rb') + +def output_file(arg): + if arg is None or arg == '-': + return sys.stdout + else: + return file(arg, 'wb') + +def options_to_values(options): + values = {} + for name, type in cp210x.VALUES: + if name == "baudrate_table": + continue + value = getattr(options, name) + if value is not None: + values[name] = value + + if options.baudrate_table: + baudrate_table = [] + for s in options.baudrate_table: + try: + baudrate, info = s.split(':') + except TypeError: + error("option --set-baudrate: need two parts separated by ':'", + ERR_WRONG_INPUT) + try: + baudrate_table.append(read_baudrate_info(info) + + (int(baudrate), )) + except ValueError, err: + error("option --set-baudrate: %s" % str(err), + ERR_WRONG_INPUT) + + values['baudrate_table'] = baudrate_table + return values + +def find_device(patterns): + usb_patterns = [] + for pattern in patterns: + if ':' in pattern: + try: + vidString, pidString = pattern.split(':') + vid = int(vidString, 16) + pid = int(pidString, 16) + + except (TypeError, ValueError): + error("Match be either 'ddd/ddd' or 'hhhh:hhhh'.", + ERR_WRONG_INPUT) + + usb_patterns.append(dict(idVendor=vid, idProduct=pid)) + elif '/' in pattern: + try: + bus, device = pattern.split('/') + usb_patterns.append( (bus, device) ) + except (TypeError, ValueError): + error("Match be either 'ddd/ddd' or 'hhhh:hhhh'.", + ERR_WRONG_INPUT) + else: + error("Match be either 'ddd/ddd' or 'hhhh:hhhh'.", + ERR_WRONG_INPUT) + + for dev in cp210x.Cp210xProgrammer.list_devices(usb_patterns): + return dev + + error("No devices found", ERR_DEVICE_NOT_FOUND) + +def read_cp210x(options): + dev = find_device(options.match) + dev.open() + try: + eeprom = EEPROM(dev) + finally: + dev.close() + + if options.hex_output: + eeprom.write_hex_file(output_file(options.hex_output)) + if options.ini_output or not options.hex_output: + valuefile.write_file(output_file(options.ini_output), eeprom.get_values()) + +def write_cp210x(options): + dev = find_device(options.match) + dev.open() + try: + if options.hex_input or options.force_eeprom: + + if options.hex_input: + eeprom = EEPROM(input_file(options.hex_input)) + else: + eeprom = EEPROM(dev) + + values = eeprom.get_values() + if options.ini_input: + values = valuefile.read_file(input_file(options.ini_input)) + update_values(values, options_to_values(options), eeprom) + + eeprom.set_values(values) + + eeprom.write_to_cp210x(dev) + if options.reset_device: + dev.reset() + + else: + if options.ini_input: + values = valuefile.read_file(input_file(options.ini_input)) + else: + values = {} + update_values(values, options_to_values(options), dev) + dev.set_values(values) + + if options.reset_device: + dev.reset() + finally: + dev.close() + +def change_hexfile(options): + eeprom = EEPROM(input_file(options.hex_input)) + values = {} + if options.ini_input: + update_values(values, + valuefile.read_file(input_file(options.ini_input)), + eeprom) + update_values(values, options_to_values(options), eeprom) + eeprom.set_values(values) + if options.ini_output: + valuefile.write_file(output_file(options.ini_output), + eeprom.get_values()) + eeprom.write_hex_file(output_file(options.hex_output)) + +def parse_hexfile(options): + eeprom = EEPROM(input_file(options.hex_input)) + valuefile.write_file(output_file(options.ini_output), eeprom.get_values()) + +parser = OptionParser(version=__version__, description=__doc__) +parser.add_option("-r", "--read-cp210x", const=read_cp210x, + dest="action", action="store_const") +parser.add_option("-w", "--write-cp210x", const=write_cp210x, + dest="action", action="store_const") +parser.add_option("-c", "--change-hexfile", const=change_hexfile, + dest="action", action="store_const") +parser.add_option("-p", "--parse-hexfile", const=parse_hexfile, + dest="action", action="store_const") +parser.add_option("-F", "--hex-input", metavar="FILE") +parser.add_option("-f", "--hex-output", metavar="FILE") +parser.add_option("-I", "--ini-input", metavar="FILE") +parser.add_option("-i", "--ini-output", metavar="FILE") +for name, type in cp210x.VALUES: + if name == 'baudrate_table': + continue + parser.add_option("--set-" + name.translate(TRANS_UNDERSCORE), + dest=name, metavar=name.upper(), type=type) +parser.add_option("--set-baudrate", action="append", dest="baudrate_table") +parser.add_option("-m", "--match", action="append", metavar="PATTERN") +parser.add_option("--reset-device", action="store_true") +parser.add_option("--force-eeprom", action="store_true") + +parser.set_defaults( + action=read_cp210x, + hex_input=None, + hex_output=None, + ini_input=None, + ini_output=None, + match=[], + baudrate_table=[], + reset_device=False, + force_eeprom=False, +) + +def main(): + (options, _) = parser.parse_args() + if not options.match: + options.match = ['10C4:EA60', '10C4:EA61'] + options.action(options) + +if __name__ == '__main__': + try: + usb.init() + main() + + except cp210x.DeviceLocked: + error("Can not write data to device. Device is locked.", + ERR_DEVICE_LOCKED) + + except cp210x.Cp210xError, err: + error(str(err), ERR_DEVICE_ERROR) + + except IOError, err: + error(str(err), ERR_OTHER) + + except HexFileError, err: + error(str(err), ERR_OTHER) + + except ValuesFileError, err: + error(str(err), ERR_OTHER) + + except usb.LibUsbNotInstalled: + error("Package 'libusb' not installed.") + + except SystemExit, err: + raise + + except: + traceback.print_exc() + sys.exit(ERR_INTERNAL) + diff --git a/contrib/cp210x-program/cp210x-program b/contrib/cp210x-program/cp210x-program new file mode 100755 index 0000000..be97be8 --- /dev/null +++ b/contrib/cp210x-program/cp210x-program @@ -0,0 +1,266 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. +"""\ +Provides access to the EEPROM in an Silabs CP210x. The data can be directly to +the device read or written. +""" +__author__ = "Johannes Hölzl " +__license__ = "GNU LGPL" +__version__ = "0.2" + +import sys +import re +import string +import traceback +import optparse + +from cp210x import usb, valuefile, cp210x +from cp210x.eeprom import EEPROM, HexFileError +from cp210x.valuefile import read_baudrate_info, update_values, ValuesFileError +TRANS_UNDERSCORE = string.maketrans('_', '-') + +ERR_OK = 0 +ERR_WRONG_INPUT = -1 +ERR_INTERNAL = -2 +ERR_DEVICE_LOCKED = -3 +ERR_DEVICE_NOT_FOUND = -4 +ERR_DEVICE_ERROR = -5 + +def error(message, retval=-1): + sys.stderr.write(message + "\n") + sys.exit(retval) + +class Option(optparse.Option): + TYPES = list(optparse.Option.TYPES) + TYPE_CHECKER = dict(optparse.Option.TYPE_CHECKER) + for type, (reader, _) in valuefile.TYPES.items(): + if type in TYPES: + continue + TYPES.append(type) + def checker(self, name, value, reader=reader): + try: + return reader(value) + except ValueError, err: + raise optparse.OptionValueError("option %s: %s" % (name, + str(err))) + TYPE_CHECKER[type] = checker + +class OptionParser(optparse.OptionParser): + def error(self, msg): + error(msg, ERR_WRONG_INPUT) + + def __init__(self, *args, **kwargs): + if 'option_class' not in kwargs: + kwargs['option_class'] = Option + optparse.OptionParser.__init__(self, *args, **kwargs) + +def input_file(arg): + if arg is None or arg == '-': + return sys.stdin + else: + return file(arg, 'rb') + +def output_file(arg): + if arg is None or arg == '-': + return sys.stdout + else: + return file(arg, 'wb') + +def options_to_values(options): + values = {} + for name, type in cp210x.VALUES: + if name == "baudrate_table": + continue + value = getattr(options, name) + if value is not None: + values[name] = value + + if options.baudrate_table: + baudrate_table = [] + for s in options.baudrate_table: + try: + baudrate, info = s.split(':') + except TypeError: + error("option --set-baudrate: need two parts separated by ':'", + ERR_WRONG_INPUT) + try: + baudrate_table.append(read_baudrate_info(info) + + (int(baudrate), )) + except ValueError, err: + error("option --set-baudrate: %s" % str(err), + ERR_WRONG_INPUT) + + values['baudrate_table'] = baudrate_table + return values + +def find_device(patterns): + usb_patterns = [] + for pattern in patterns: + if ':' in pattern: + try: + vidString, pidString = pattern.split(':') + vid = int(vidString, 16) + pid = int(pidString, 16) + + except (TypeError, ValueError): + error("Match be either 'ddd/ddd' or 'hhhh:hhhh'.", + ERR_WRONG_INPUT) + + usb_patterns.append(dict(idVendor=vid, idProduct=pid)) + elif '/' in pattern: + try: + bus, device = pattern.split('/') + usb_patterns.append( (bus, device) ) + except (TypeError, ValueError): + error("Match be either 'ddd/ddd' or 'hhhh:hhhh'.", + ERR_WRONG_INPUT) + else: + error("Match be either 'ddd/ddd' or 'hhhh:hhhh'.", + ERR_WRONG_INPUT) + + for dev in cp210x.Cp210xProgrammer.list_devices(usb_patterns): + return dev + + error("No devices found", ERR_DEVICE_NOT_FOUND) + +def read_cp210x(options): + dev = find_device(options.match) + dev.open() + try: + eeprom = EEPROM(dev) + finally: + dev.close() + + if options.hex_output: + eeprom.write_hex_file(output_file(options.hex_output)) + if options.ini_output or not options.hex_output: + valuefile.write_file(output_file(options.ini_output), eeprom.get_values()) + +def write_cp210x(options): + dev = find_device(options.match) + dev.open() + try: + if options.hex_input or options.force_eeprom: + + if options.hex_input: + eeprom = EEPROM(input_file(options.hex_input)) + else: + eeprom = EEPROM(dev) + + values = eeprom.get_values() + if options.ini_input: + values = valuefile.read_file(input_file(options.ini_input)) + update_values(values, options_to_values(options), eeprom) + + eeprom.set_values(values) + + eeprom.write_to_cp210x(dev) + if options.reset_device: + dev.reset() + + else: + if options.ini_input: + values = valuefile.read_file(input_file(options.ini_input)) + else: + values = {} + update_values(values, options_to_values(options), dev) + dev.set_values(values) + + if options.reset_device: + dev.reset() + finally: + dev.close() + +def change_hexfile(options): + eeprom = EEPROM(input_file(options.hex_input)) + values = {} + if options.ini_input: + update_values(values, + valuefile.read_file(input_file(options.ini_input)), + eeprom) + update_values(values, options_to_values(options), eeprom) + eeprom.set_values(values) + if options.ini_output: + valuefile.write_file(output_file(options.ini_output), + eeprom.get_values()) + eeprom.write_hex_file(output_file(options.hex_output)) + +def parse_hexfile(options): + eeprom = EEPROM(input_file(options.hex_input)) + valuefile.write_file(output_file(options.ini_output), eeprom.get_values()) + +parser = OptionParser(version=__version__, description=__doc__) +parser.add_option("-r", "--read-cp210x", const=read_cp210x, + dest="action", action="store_const") +parser.add_option("-w", "--write-cp210x", const=write_cp210x, + dest="action", action="store_const") +parser.add_option("-c", "--change-hexfile", const=change_hexfile, + dest="action", action="store_const") +parser.add_option("-p", "--parse-hexfile", const=parse_hexfile, + dest="action", action="store_const") +parser.add_option("-F", "--hex-input", metavar="FILE") +parser.add_option("-f", "--hex-output", metavar="FILE") +parser.add_option("-I", "--ini-input", metavar="FILE") +parser.add_option("-i", "--ini-output", metavar="FILE") +for name, type in cp210x.VALUES: + if name == 'baudrate_table': + continue + parser.add_option("--set-" + name.translate(TRANS_UNDERSCORE), + dest=name, metavar=name.upper(), type=type) +parser.add_option("--set-baudrate", action="append", dest="baudrate_table") +parser.add_option("-m", "--match", action="append", metavar="PATTERN") +parser.add_option("--reset-device", action="store_true") +parser.add_option("--force-eeprom", action="store_true") + +parser.set_defaults( + action=read_cp210x, + hex_input=None, + hex_output=None, + ini_input=None, + ini_output=None, + match=[], + baudrate_table=[], + reset_device=False, + force_eeprom=False, +) + +def main(): + (options, _) = parser.parse_args() + if not options.match: + options.match = ['10C4:EA60', '10C4:EA61','413C:9500' ] + options.action(options) + +if __name__ == '__main__': + try: + usb.init() + main() + + except cp210x.DeviceLocked: + error("Can not write data to device. Device is locked.", + ERR_DEVICE_LOCKED) + + except cp210x.Cp210xError, err: + error(str(err), ERR_DEVICE_ERROR) + + except IOError, err: + error(str(err), ERR_OTHER) + + except HexFileError, err: + error(str(err), ERR_OTHER) + + except ValuesFileError, err: + error(str(err), ERR_OTHER) + + except usb.LibUsbNotInstalled: + error("Package 'libusb' not installed.") + + except SystemExit, err: + raise + + except: + traceback.print_exc() + sys.exit(ERR_INTERNAL) + diff --git a/contrib/cp210x-program/cp210x-program.doap b/contrib/cp210x-program/cp210x-program.doap new file mode 100644 index 0000000..2e84f2e --- /dev/null +++ b/contrib/cp210x-program/cp210x-program.doap @@ -0,0 +1,83 @@ + + + + CP210x Programmer + cp210x-program + + + 2007-04-02 + + + Provides access to the EEPROM in an Silabs CP210x + + + + + + +The goal of this library is to provide access to the EEPROM of an Silabs CP210x +under Linux. + +The CP210x is an USB-to-serial chip used in a lot of USB devices (similar to +FTDIs and PL2303). The CP210x has a EEPROM on the chip which can be programmed +via USB. Silabs provides already a library and gui programm to program this +EEPROM, but only for windows. + +For more information see: +* http://www.silabs.com/tgwWebApp/public/web_content/products/Microcontrollers/Interface/en/interface.htm +* http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an144.pdf + +Requires: +* libusb: http://libusb.sourceforge.net/ +* ctypes: http://starship.python.net/crew/theller/ctypes/ + + + + + + + Johannes Hölzl + + + + + + + + + + + + + + Python + + + + Linux + + + + FreeBSD + + + + Mac OS X + + + + + + + unstable + 2007-04-05 + 0.1 + + + + diff --git a/contrib/cp210x-program/cp210x/__init__.py b/contrib/cp210x-program/cp210x/__init__.py new file mode 100644 index 0000000..b761032 --- /dev/null +++ b/contrib/cp210x-program/cp210x/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- + +__author__ = "Johannes Hölzl " +__license__ = "GNU LGPL" diff --git a/contrib/cp210x-program/cp210x/__init__.pyc b/contrib/cp210x-program/cp210x/__init__.pyc new file mode 100644 index 0000000..70aea84 Binary files /dev/null and b/contrib/cp210x-program/cp210x/__init__.pyc differ diff --git a/contrib/cp210x-program/cp210x/cp210x.py b/contrib/cp210x-program/cp210x/cp210x.py new file mode 100644 index 0000000..c51cb35 --- /dev/null +++ b/contrib/cp210x-program/cp210x/cp210x.py @@ -0,0 +1,398 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. +"""Provides access to the EEPROM of Silabs CP210x devices + +The following classes are available: + +class Cp210xProgrammer: + Provides direct access to the CP2101, can be used to write single data + directly or via an EEPROM image. + +class EEPROM: + Can be used to read or write a hex file containing the EEPROM content + of an CP2101. Provides also access to the single fields in the EEPROM. +""" + +import ctypes +import usb + +__all__ = ['Cp210xProgrammer', 'Cp210xError'] + +CP2101_UART = 0x00 +CP2101_CONFIG = 0xFF + +CP2101_UART_ENABLE = 0x0001 +CP2101_UART_DISABLE = 0x0000 + +REG_VENDOR_ID = 0x3701 +REG_PRODUCT_ID = 0x3702 +REG_PRODUCT_STRING = 0x3703 +REG_SERIAL_NUMBER = 0x3704 +REG_CFG_ATTRIBUTES = 0x3705 +REG_MAX_POWER = 0x3706 +REG_VERSION = 0x3707 +REG_UNKNOWN = 0x3708 +REG_EEPROM = 0x3709 +REG_LOCK_VALUE = 0x370A +REG_PART_NUMBER = 0x370B + +SIZE_EEPROM = 0x0400 +SIZE_PRODUCT_STRING = 0x007D +SIZE_SERIAL_NUMBER = 0x003F +SIZE_BAUDRATES = 32 +SIZE_BAUDRATE_CFG = 10 +SIZE_BAUDRATE_TABLE = SIZE_BAUDRATES * SIZE_BAUDRATE_CFG +SIZE_VENDOR_STRING = 24 + +LCK_LOCKED = 0x00 +LCK_UNLOCKED = 0xFF + +VID_SILABS = 0x10C4 +PID_CP210x = 0xEA60 + +VALUES = [ + ('product_string', 'string'), + ('serial_number', 'string'), + ('product_id', 'id'), + ('vendor_id', 'id'), + ('version', 'version'), + ('bus_powered', 'boolean'), + ('max_power', 'int'), + ('locked', 'boolean'), + ('part_number', 'int'), + ('vendor_string', 'string'), + ('baudrate_table', 'list'), +] + +def iif(v, a, b): + if v: + return a + else: + return b + +def to_div2(p): + value = int(p / 2) + if (value * 2) < p: + value += 1 + return value + +def to_bcd(i): + assert i >= 0 and i <= 99 + return (i // 10) << 4 | (i % 10) + +def to_bcd2( (i, j) ): + return to_bcd(i) << 8 | to_bcd(j) + +def from_bcd(num): + return num & 0x0F + (num >> 4) * 10 + +def from_bcd2(data): + return (from_bcd(data >> 8), from_bcd(data & 0xFF)) + +def from_binary(data, le=True): + value = 0 + if le: + data = data[::-1] + for byte in data: + value = value << 8 | ord(byte) + return value + +def to_binary(value, size=2, le=True): + data = '' + for i in range(size): + data += chr(value & 0xFF) + value >>= 8 + if le: + return data + else: + return data[::-1] + +def parse_baudrate_cfg(data): + return (from_binary(data[0:2], le=False), + from_binary(data[2:4], le=False), + from_binary(data[4:5]), + from_binary(data[6:10])) + +def build_baudrate_cfg(baudgen, timer0reload, prescaler, baudrate): + return (to_binary(baudgen, le=False) + to_binary(timer0reload, le=False) + + to_binary(prescaler, 1) + '\x00' + to_binary(baudrate, 4)) + +class Cp210xError(IOError): + pass + +class DeviceLocked(Cp210xError): + pass + +class Cp210xProgrammer(object): + """Program an Silabs CP2101, CP2102 or CP2103 + + This modul provides access to Silabs CP210x devices to set some USB + descriptor fields and some USB descriptor strings. + + The following fields can be set: + + * Vendor ID + * Product ID + * Product String + * Serial Number + * Device Version + * Bus Powered + * max. Power consumption + + Either use libusb to find a device, and provide the device description + to the constructor, or use Cp210xProgrammer.list_device() to list all + devices matching certain pattern. + + To progamm the device open() it, set the data, and close() it. To have the + changed fields reread call reset() before closing it. + """ + + TIMEOUT = 300 #ms + + @classmethod + def list_devices(self, patterns=[{ 'idVendor': VID_SILABS, + 'idProduct': PID_CP210x }]): + """Yields a list of devices matching certain patterns. + + param patterns: This must be a list of dictionaries or pairs of string. + Each device in the usb tree is matched against all pattern in the + list. + + When an item is a dictionary all fields of the descriptors + are compared against the corresponding values in the dictionary. If + each value is equal, the device is yielded. + + When an item is a pair of strings. The first string must be the + dirname of the bus and the second string the filename of the device. + + For example: + + >> list(Cp210xProgrammer.list_device([{ 'idVendor': VID_SILABS, + 'idProduct': PID_CP210x }])) + [device(...)] + + """ + + usb.find_busses() + usb.find_devices() + + bus = usb.get_busses() + while bus: + dev = bus.contents.devices + while dev: + for pattern in patterns: + if isinstance(pattern, dict): + for name, value in pattern.items(): + if getattr(dev.contents.descriptor, name) != value: + break + else: + yield self(dev) + break + elif isinstance(pattern, tuple): + if (bus.contents.dirname == pattern[0] and + dev.contents.filename == pattern[1]): + yield self(dev) + break + dev = dev.contents.next + bus = bus.contents.next + + def __init__(self, dev_info): + self.dev_info = dev_info + self.handle = None + self._locked = None + + def open(self): + """Opens the device. + + Only after an successful call to open() data can be read from and + written to the device. + + Claims all resources associated with this device. + """ + self.handle = usb.open(self.dev_info) + if self.handle == 0: + self.handle = None + raise Cp210xError("Can't open device.") + usb.set_configuration(self.handle, 1) + usb.claim_interface(self.handle, 0) + + def reset(self): + """Force the USB stack to reset the device. + + Resets the device through an hard reset over the port to which the + device is connected. After that happend the EEPROM content in the device + is reread and the device's descriptors are the one written to it. + """ + assert self.handle is not None + usb.reset(self.handle) + + def close(self): + """Closes the device. + + Releases all resources associated with this device. + """ + assert self.handle is not None + usb.release_interface(self.handle, 0) + usb.close(self.handle) + self.handle = None + + def __del__(self): + if self.handle is not None: + self.close() + + def _set_config(self, value, index=0, data=None, request=CP2101_CONFIG): + assert self.handle is not None + if self.get_locked(): + raise DeviceLocked() + + if data is not None: + data_length = len(data) + else: + data_length = 0 + res = usb.control_msg(self.handle, usb.ENDPOINT_OUT | usb.TYPE_VENDOR, + request, value, index, data, data_length, + self.TIMEOUT) + if res < 0: + raise Cp210xError("Unable to send request %04X result=%d" + % (value, res)) + + def _set_config_string(self, value, content, max_length): + assert isinstance(content, basestring) + encoded = content.encode('utf-16-le') + assert len(encoded) <= max_length + self._set_config(value, data=chr(len(encoded) + 2) + "\x03" + encoded) + + def _get_config(self, value, length, index=0, request=CP2101_CONFIG): + assert self.handle is not None + data = ctypes.create_string_buffer(length) + res = usb.control_msg(self.handle, usb.ENDPOINT_IN | usb.TYPE_VENDOR, + request, value, index, data, length, + self.TIMEOUT) + if res < 0: + raise Cp210xError("Unable to send request, %04X result=%d" + % (value, res)) + return data.raw[:res] + + def _get_int8_config(self, value, index=0, request=CP2101_CONFIG): + return ord(self._get_config(value, 1, index=index, request=request)) + + def _get_int16_config(self, value, index=0, request=CP2101_CONFIG): + data = self._get_config(value, 2, index=index, request=request) + return ord(data[0]) << 8 | ord(data[1]) + + def get_eeprom_content(self): + """Reads the entire EEPROM content as one big 1024-byte blob. + """ + return self._get_config(REG_EEPROM, SIZE_EEPROM) + + def get_baudrate_content(self): + """Return the baudrate table as binary data. + """ + return self._get_config(REG_EEPROM, SIZE_BAUDRATE_TABLE) + + def get_baudrate_table(self): + """Returns the baudrate table. + + A list containing 4-tuples are returnes. + Each tuple containes the following data: + + * BaudGen: Value used to generate the real baudrate. + * Time0Reset: Value used to generate the usb timeout. + * Prescaler: Used to generate the real baudrate. + * Baudrate: The baudrate which activates this entry. + """ + data = self.get_baudrate_content() + return [parse_baudrate_cfg(data[pos:pos+SIZE_BAUDRATE_CFG]) + for pos in range(0, SIZE_BAUDRATE_TABLE, SIZE_BAUDRATE_CFG)] + + def set_baudrate_table(self, baudrates): + """Writes the baudrate table. + + See get_baudrate_table() for the structure of the table. + """ + assert len(baudrates) == SIZE_BAUDRATES + self.set_baudrate_content(data=''.join(build_baudrate_cfg(*cfg) + for cfg in baudrates)) + baudrate_table = property(get_baudrate_table, set_baudrate_table) + + def get_part_number(self): + """ The part number of the device. + + Returns: 1 for an CP2101 + 2 for an CP2102 + 3 for an CP2103 + """ + return self._get_int8_config(REG_PART_NUMBER) + + def get_locked(self): + """ The lock value of the device. + + When True is returnes no data can be written to the device. + """ + if self._locked is None: + self._locked = self._get_int8_config(REG_LOCK_VALUE) == LCK_LOCKED + return self._locked + + def set_eeprom_content(self, content): + """Writes an 1024-byte blob to the EEPROM + """ + assert len(content) == SIZE_EEPROM, ("EEPROM data must be %i bytes." + % SIZE_EEPROM) + assert isinstance(content, str), "EEPROM data must be string." + self._set_config(REG_EEPROM, data=content) + + def set_product_id(self, pid): + """Sets the Product ID + """ + assert pid > 0x0000 and pid < 0xFFFF + self._set_config(REG_PRODUCT_ID, pid) + + def set_vendor_id(self, vid): + """Sets the Vendor ID + """ + assert vid > 0x0000 and vid < 0xFFFF + self._set_config(REG_VENDOR_ID, vid) + + def set_product_string(self, product_string): + """Sets the product string. + + Be aware that the string will be stored as UTF-16 encoded and should not + exceed SIZE_PRODUCT_STRING + """ + self._set_config_string(REG_PRODUCT_STRING, product_string, + SIZE_PRODUCT_STRING) + + def set_serial_number(self, serial_number): + self._set_config_string(REG_SERIAL_NUMBER, serial_number, + SIZE_SERIAL_NUMBER) + + def set_max_power(self, max_power): + assert max_power >= 0 and max_power <= 500 + self._set_config(REG_MAX_POWER, to_div2(max_power)) + + def set_bus_powered(self, bus_powered): + if bus_powered: + self._set_config(REG_CFG_ATTRIBUTES, 0xC0) + else: + self._set_config(REG_CFG_ATTRIBUTES, 0x80) + + def set_version(self, version): + self._set_config(REG_VERSION, to_bcd2(version)) + + def set_locked(self, locked): + """ The lock value of the device. + + When True is returnes no data can be written to the device. + """ + if locked: + self._set_config(REG_LOCK_VALUE, LCK_LOCKED) + else: + self._set_config(REG_LOCK_VALUE, LCK_UNLOCKED) + + def set_values(self, values): + for name, value in values.items(): + if name not in ['part_number', 'vendor_string']: + getattr(self, "set_" + name) (value) + diff --git a/contrib/cp210x-program/cp210x/cp210x.pyc b/contrib/cp210x-program/cp210x/cp210x.pyc new file mode 100644 index 0000000..912a046 Binary files /dev/null and b/contrib/cp210x-program/cp210x/cp210x.pyc differ diff --git a/contrib/cp210x-program/cp210x/eeprom.py b/contrib/cp210x-program/cp210x/eeprom.py new file mode 100644 index 0000000..25f0372 --- /dev/null +++ b/contrib/cp210x-program/cp210x/eeprom.py @@ -0,0 +1,180 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. + +import cp210x +from cp210x import from_binary, to_binary, iif, VALUES + +__all__ = ['EEPROM', 'HexFileError'] + +POS_BAUDRATE_TABLE = 0x0000 +POS_PART_NUMBER = 0x01FF +POS_PRODUCT_STRING = 0x0208 +POS_SERIAL_NUMBER = 0x0307 +POS_PRODUCT_ID = 0x0390 +POS_VENDOR_ID = 0x0392 +POS_VERSION = 0x0394 +POS_CFG_ATTRIBUTES = 0x03A1 +POS_MAX_POWER = 0x03A2 +POS_VENDOR_STRING = 0x03C3 +POS_LOCK_VALUE = 0x03FF + +class HexFileError(StandardError): + pass + +def checksum(line): + return sum(ord(c) for c in line) & 0xFF + +def _int_value(position, size, read=lambda x:x, write=lambda x:x): + def get(self): + return read(from_binary(self.get(position, size))) + def set(self, value): + self.set(position, to_binary(write(value), size)) + return property(get, set) + +def _str_value(position, max_size): + def get(self): + size = from_binary(self.get(position, 1)) + assert size <= (max_size + 2) and size >= 2 + assert self.get(position + 1, 1) == '\x03', "Missing 0x03 at %04X" % (position + 1) + return self.get(position + 2, size - 2).decode('utf-16-le') + + def set(self, value): + encoded = value.encode('utf-16-le') + assert len(encoded) <= max_size + self.set(position, chr(len(encoded) + 2) + '\x03' + encoded) + + return property(get, set) + +class EEPROM(object): + START_ADDRESS = 0x3600 + def __init__(self, content=None): + if isinstance(content, str) or content is None: + assert content is None or len(content) == cp210x.SIZE_EEPROM + self.content = content + elif isinstance(content, cp210x.Cp210xProgrammer): + self.content = content.get_eeprom_content() + else: + self.parse_hex_file(content.read()) + + def write_to_cp210x(self, cp210xDevice): + cp210xDevice.set_eeprom_content(self.content) + + def parse_hex_file(self, hex_content): + self.content = '' + address = self.START_ADDRESS + for tag in hex_content.split('\n'): + if not tag.startswith(':'): + raise HexFileError("Line doesn't start with ':'") + + try: + content = tag[1:].decode('hex') + except TypeError: + raise HexFileError("Hex data expected") + + if len(content) < 5: + raise HexFileError("Line to short") + + if checksum(content) != 0: + raise HexFileError("Checksum error") + + size = from_binary(content[0]) + tag_address = from_binary(content[1:3], le=False) + tag_type = from_binary(content[3:4]) + line = content[4:-1] + + if tag_type == 0x00: + if tag_address != address: + raise HexFileError("Expected address %04X but found %04X" + % (address, tag_address)) + self.content += line + address += len(line) + elif tag_type == 0x01: + if size != 0 or len(line) != 0: + raise HexFileError("Defekt end tag") + break + + else: + raise HexFileError("Unknown tag type %02X" % tag_type) + + def build_hex_file(self): + for tag_start in range(0, len(self.content), 0x10): + line = self.content[tag_start:tag_start+0x10] + address = self.START_ADDRESS + tag_start + tag = (to_binary(len(line), 1) + + to_binary(address, le=False) + + '\x00' + + line) + cs = checksum(tag) + if cs == 0: + tag += '\x00' + else: + tag += chr(0x100 - cs) + yield ":%s\n" % tag.encode('hex') + yield ":00000001FF\n" + + def write_hex_file(self, f): + if isinstance(f, str): + f = file(f, 'wb') + do_close = True + else: + do_close = False + for line in self.build_hex_file(): + f.write(line) + if do_close: + f.close() + + def read_hex_file(self, f): + if isinstance(f, str): + f = file(f, 'rb') + do_close = True + else: + do_close = False + self.parse_hex_file(f.read()) + if do_close: + f.close() + + def get(self, pos, length): + return self.content[pos:pos+length] + + def set(self, pos, data): + self.content = (self.content[:pos] + + data + + self.content[pos + len(data):]) + + def _get_baudrate_table(self): + dat = self.get(POS_BAUDRATE_TABLE, cp210x.SIZE_BAUDRATE_TABLE) + return [cp210x.parse_baudrate_cfg(dat[pos:pos+cp210x.SIZE_BAUDRATE_CFG]) + for pos in range(0, cp210x.SIZE_BAUDRATE_TABLE, + cp210x.SIZE_BAUDRATE_CFG)] + def _set_baudrate_table(self, baudrates): + assert len(baudrates) == cp210x.SIZE_BAUDRATES + self.set(POS_BAUDRATE_TABLE, + ''.join(cp210x.build_baudrate_cfg(*cfg) for cfg in baudrates)) + baudrate_table = property(_get_baudrate_table, _set_baudrate_table) + product_string = _str_value(POS_PRODUCT_STRING, cp210x.SIZE_PRODUCT_STRING) + serial_number = _str_value(POS_SERIAL_NUMBER, cp210x.SIZE_SERIAL_NUMBER) + part_number = _int_value(POS_PART_NUMBER, 1) + product_id = _int_value(POS_PRODUCT_ID, 2) + vendor_id = _int_value(POS_VENDOR_ID, 2) + version = _int_value(POS_VERSION, 2, + cp210x.from_bcd2, cp210x.to_bcd2) + bus_powered = _int_value(POS_CFG_ATTRIBUTES, 1, + lambda a: bool(a & 0x40), + lambda a: iif(a, 0xC0, 0x80)) + max_power = _int_value(POS_MAX_POWER, 1, lambda p: p*2, cp210x.to_div2) + vendor_string = _str_value(POS_VENDOR_STRING, cp210x.SIZE_VENDOR_STRING) + locked = _int_value(POS_LOCK_VALUE, 1, + lambda l: l == cp210x.LCK_LOCKED, + lambda b: iif(b, cp210x.LCK_LOCKED, + cp210x.LCK_UNLOCKED)) + + def get_values(self): + return dict((name, getattr(self, name)) for name, type in VALUES) + + def set_values(self, values): + for name, value in values.items(): + setattr(self, name, value) + + diff --git a/contrib/cp210x-program/cp210x/eeprom.pyc b/contrib/cp210x-program/cp210x/eeprom.pyc new file mode 100644 index 0000000..46f86dc Binary files /dev/null and b/contrib/cp210x-program/cp210x/eeprom.pyc differ diff --git a/contrib/cp210x-program/cp210x/usb.py b/contrib/cp210x-program/cp210x/usb.py new file mode 100644 index 0000000..be70f82 --- /dev/null +++ b/contrib/cp210x-program/cp210x/usb.py @@ -0,0 +1,274 @@ +# Python interface for "usb.h" version 0.1.4.4.4 +# +# Copyright (c) 2005 Robert Hoelzl +# Copyright (c) 2007 Johannes Hoelzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. + +from ctypes import * +import sys + +class LibUsbNotInstalled(OSError): + pass + +try: + if sys.platform == 'darwin': + PATH_MAX = 1024 + dll=cdll.LoadLibrary("libusb.dylib") + elif sys.platform == 'linux2': + PATH_MAX = 4096 + dll=cdll.LoadLibrary("libusb.so") + else: + raise NotImplementedError("Platform %s not supported by usb.py" % sys.platform) +except OSError: + raise LibUsbNotInstalled() + + +# helper functions +def func(f, *args, **retval): + f.restype = retval.get('retval', None) + f.argtypes = args + if retval.has_key('rename'): globals()[retval['rename']] = f + else: globals()[f.__name__[4:]] = f + +# constants +CLASS_PER_INTERFACE = 0 +USB_CLASS_AUDIO = 1 +CLASS_COMM = 2 +CLASS_HID = 3 +CLASS_PRINTER = 7 +CLASS_PTP = 6 +CLASS_MASS_STORAGE = 8 +CLASS_HUB = 9 +CLASS_DATA = 10 +CLASS_VENDOR_SPEC = 0xff + +DT_DEVICE = 0x01 +DT_CONFIG = 0x02 +DT_STRING = 0x03 +DT_INTERFACE = 0x04 +DT_ENDPOINT = 0x05 + +DT_HID = 0x21 +DT_REPORT = 0x22 +DT_PHYSICAL = 0x23 +DT_HUB = 0x29 + +DT_DEVICE_SIZE = 18 +DT_CONFIG_SIZE = 9 +DT_INTERFACE_SIZE = 9 +DT_ENDPOINT_SIZE = 7 +DT_ENDPOINT_AUDIO_SIZE = 9 # Audio extension +DT_HUB_NONVAR_SIZE = 7 + + +class descriptor_header(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8) ] + + +class string_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("wData", c_uint*1) ] + +class hid_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bcdHID", c_uint16), + ("bCountryCode", c_uint8), + ("bNumDescriptors", c_uint8) ] + +MAXENDPOINTS = 32 +class endpoint_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bEndpointAddress", c_uint8), + ("bmAttributes", c_uint8), + ("wMaxPacketSize", c_uint16), + ("bInterval", c_uint8), + ("bRefresh", c_uint8), + ("bSynchAddress", c_uint8), + + ("extra", POINTER(c_uint8)), + ("extralen", c_int) ] + + +ENDPOINT_ADDRESS_MASK = 0x0f # in bEndpointAddress +ENDPOINT_DIR_MASK = 0x80 + +ENDPOINT_TYPE_MASK = 0x03 # in bmAttributes +ENDPOINT_TYPE_CONTROL = 0 +ENDPOINT_TYPE_ISOCHRONOUS = 1 +ENDPOINT_TYPE_BULK = 2 +ENDPOINT_TYPE_INTERRUPT = 3 + +MAXINTERFACES = 32 +class interface_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bInterfaceNumber", c_uint8), + ("bAlternateSetting", c_uint8), + ("bNumEndpoints", c_uint8), + ("bInterfaceClass", c_uint8), + ("bInterfaceSubClass", c_uint8), + ("bInterfaceProtocol", c_uint8), + ("iInterface", c_uint8), + + ("endpoint", POINTER(endpoint_descriptor)), + + ("extra", POINTER(c_uint8)), + ("extralen", c_int) ] + +MAXALTSETTING = 128 # Hard limit +class interface(Structure): _fields_ = [ + ("altsetting", POINTER(interface_descriptor)), + + ("num_altsetting", c_int) ] + +MAXCONFIG = 8 +class config_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("wTotalLength", c_uint16), + ("bNumInterfaces", c_uint8), + ("bConfigurationValue", c_uint8), + ("iConfiguration", c_uint16), + ("bmAttributes", c_uint8), + ("MaxPower", c_uint8), + + ("interface", POINTER(interface)), + + ("extra", POINTER(c_uint8)), # Extra descriptors + ("extralen", c_int) ] + +class device_descriptor(Structure): _fields_ = [ + ("bLength", c_uint8), + ("bDescriptorType", c_uint8), + ("bcdUSB", c_uint16), + ("bDeviceClass", c_uint8), + ("bDeviceSubClass", c_uint8), + ("bDeviceProtocol", c_uint8), + ("bMaxPacketSize0", c_uint8), + ("idVendor", c_uint16), + ("idProduct", c_uint16), + ("bcdDevice", c_uint16), + ("iManufacturer", c_uint8), + ("iProduct", c_uint8), + ("iSerialNumber", c_uint8), + ("bNumConfigurations", c_uint8) ] + +class ctrl_setup(Structure): _fields_ = [ + ("bRequestType", c_uint8), + ("bRequest", c_uint8), + ("wValue", c_uint16), + ("wIndex", c_uint16), + ("wLength", c_uint16) ] + + +REQ_GET_STATUS = 0x00 +REQ_CLEAR_FEATURE = 0x01 +# 0x02 is reserved +REQ_SET_FEATURE = 0x03 +# 0x04 is reserved +REQ_SET_ADDRESS = 0x05 +REQ_GET_DESCRIPTOR = 0x06 +REQ_SET_DESCRIPTOR = 0x07 +REQ_GET_CONFIGURATION = 0x08 +REQ_SET_CONFIGURATION = 0x09 +REQ_GET_INTERFACE = 0x0A +REQ_SET_INTERFACE = 0x0B +REQ_SYNCH_FRAME = 0x0C + +TYPE_STANDARD = (0x00 << 5) +TYPE_CLASS = (0x01 << 5) +TYPE_VENDOR = (0x02 << 5) +TYPE_RESERVED = (0x03 << 5) + +RECIP_DEVICE = 0x00 +RECIP_INTERFACE = 0x01 +RECIP_ENDPOINT = 0x02 +RECIP_OTHER = 0x03 + +ENDPOINT_IN = 0x80 +ENDPOINT_OUT = 0x00 + +# Error codes +ERROR_BEGIN = 500000 + +#if 1 +#define USB_LE16_TO_CPU(x) do { x = ((x & 0xff) << 8) | ((x & 0xff00) >> 8); } while(0) +#else +#define USB_LE16_TO_CPU(x) +#endif + +device_p = POINTER("device") +bus_p = POINTER("bus") + +class device(Structure): _fields_ = [ + ("next", device_p), + ("prev", device_p), + ("filename", c_char*(PATH_MAX + 1)), + + ("bus", bus_p), + ("descriptor", device_descriptor), + ("config", POINTER(config_descriptor)), + + ("dev", c_void_p), # Darwin support + + ("devnum", c_char), + + ("num_children", c_uint8), + ("children", POINTER(device_p)) ] +SetPointerType(device_p, device) + +class bus(Structure): _fields_ = [ + ("next", bus_p), + ("prev", bus_p), + + ("dirname", c_char*(PATH_MAX + 1)), + + ("devices", device_p), + ("location", c_uint), + + ("root_dev", device_p) ] +SetPointerType(bus_p, bus) + + +dev_handle_p = c_void_p + + +func(dll.usb_open, device_p, retval=dev_handle_p, rename="_open") +func(dll.usb_close, dev_handle_p, retval=c_int) +func(dll.usb_get_string, dev_handle_p, c_int, c_int, c_char_p, c_int, retval=c_int) +func(dll.usb_get_string_simple, dev_handle_p, c_int, c_char_p, c_int, retval=c_int) +func(dll.usb_get_descriptor_by_endpoint, dev_handle_p, c_int, c_uint8, c_uint8, c_void_p, c_int, retval=c_int) +func(dll.usb_get_descriptor, dev_handle_p, c_uint8, c_uint8, c_void_p, c_int, retval=c_int) + +func(dll.usb_bulk_write, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_bulk_read, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_interrupt_write, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_interrupt_read, dev_handle_p, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_control_msg, dev_handle_p, c_int, c_int, c_int, c_int, c_char_p, c_int, c_int, retval=c_int) +func(dll.usb_set_configuration, dev_handle_p, c_int, retval=c_int) +func(dll.usb_claim_interface, dev_handle_p, c_int, retval=c_int) +func(dll.usb_release_interface, dev_handle_p, c_int, retval=c_int) +func(dll.usb_set_altinterface, dev_handle_p, c_int, retval=c_int) +func(dll.usb_resetep, dev_handle_p, c_uint16, retval=c_int) +func(dll.usb_clear_halt, dev_handle_p, c_uint16, retval=c_int) +func(dll.usb_reset, dev_handle_p, retval=c_int) + +func(dll.usb_strerror, retval=c_char_p) + +func(dll.usb_init) +func(dll.usb_set_debug, c_int) +func(dll.usb_find_busses, retval=c_int) +func(dll.usb_find_devices, retval=c_int) +func(dll.usb_device, dev_handle_p, retval=device_p, rename="get_device") +func(dll.usb_get_busses, retval=bus_p) + +func(dll.usb_detach_kernel_driver_np, dev_handle_p, c_int, retval=c_int) + +# workaround for bug in ctypes 0.9.6 (cannot create functions with c_void_p as retval) +def open(dev): + return cast(_open(dev), dev_handle_p) diff --git a/contrib/cp210x-program/cp210x/usb.pyc b/contrib/cp210x-program/cp210x/usb.pyc new file mode 100644 index 0000000..d03aed5 Binary files /dev/null and b/contrib/cp210x-program/cp210x/usb.pyc differ diff --git a/contrib/cp210x-program/cp210x/valuefile.py b/contrib/cp210x-program/cp210x/valuefile.py new file mode 100644 index 0000000..ae665d2 --- /dev/null +++ b/contrib/cp210x-program/cp210x/valuefile.py @@ -0,0 +1,234 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2007 Johannes Hölzl +# +# This library is covered by the GNU LGPL, read LICENSE for details. + +# For documentation of the baudrate table see: +# +# [AN205] Silicon Labs Application Note 205 Rev. 0.3 +# http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an205.pdf + +import re +from ConfigParser import ConfigParser + +import cp210x +from cp210x import VALUES, SIZE_BAUDRATES + +__all__ = ['read_file', 'write_file', 'update_values', 'PrescalerIsZero', + 'ValuesFileError'] + +class ValuesError(StandardError): + pass + +class PrescalerIsZero(ValuesError): + pass + +class ValuesFileError(ValuesError): + pass + +version_pattern = re.compile(r'^\s*(\d\d?)\.(\d\d?)\s*$') +def read_version(s): + match = version_pattern.match(s) + if match is None: + raise ValueError("Version does not match 'xx.yy'") + return (int(match.group(1)), int(match.group(2))) +def write_version(v): + return "%d.%02d" % v + +def read_hex(s): + return int(s.strip(), 16) + +def write_hex(num): + return "%04X" % num + +def write_bool(b): + if b: + return 'yes' + else: + return 'no' + +def read_bool(s): + s = s.strip().lower() + if s not in ['true', 'yes', 'false', 'no']: + raise ValueError("Boolean must be either 'true', 'yes', 'false' or 'no'.") + return s in ['true', 'yes'] + +def read_baudrate_info(s): + values = s.split(',') + if len(values) != 3: + raise ValueError("Baudrate info must be three comma-separated items") + try: + baudgen = read_hex(values[0]) + except ValueError: + raise ValueError("The first baudrate info must be a hex-value") + try: + timer0 = read_hex(values[1]) + except ValueError: + raise ValueError("The second baudrate info must be a hex-value") + try: + prescale = int(values[2]) + except ValueError: + raise ValueError("The thirdbaudrate info must be a number") + return (baudgen, timer0, prescale) + +TYPES = { + 'boolean': (read_bool, write_bool), + 'int': (int, str), + 'id': (read_hex, write_hex), + 'string': (str, str), + 'version': (read_version, write_version), +} + +def read_file(fp): + cp = ConfigParser() + if isinstance(fp, str): + cp.read([fp]) + else: + cp.readfp(fp) + + values = {} + + for name, type in VALUES: + if name is 'baudrate_table': + continue + reader, _ = TYPES[type] + if cp.has_option('usb device', name): + try: + values[name] = reader(cp.get('usb device', name)) + except ValueError, err: + raise ValuesFileError("Key '%s': %s" % (name, str(err))) + + if cp.has_section('baudrate table'): + baudrate_table = [] + for name, value in cp.items('baudrate table'): + try: + baudrate = int(name) + except ValueError: + raise ValuesFileError("Key names in 'baudrate table' must be" + " baudrate numbers.") + try: + baudrate_table.append(read_baudrate_info(value) + (baudrate, )) + except ValueError, err: + raise ValuesFileError("Wrong baudrate info %i: %s" + % (baudrate, str(err))) + baudrate_table.sort(key=(lambda i: i[3]), reverse=True) + + values['baudrate_table'] = baudrate_table + return values + +def write_file(fp, values): + fp.write("[usb device]\n") + + for name, type in VALUES: + if name == 'baudrate_table': + continue + _, writer = TYPES[type] + if name in values: + fp.write("%s = %s\n" % (name, writer(values[name]))) + + if 'baudrate_table' in values: + fp.write("\n") + fp.write("[baudrate table]\n") + for (baudgen, timegen, prescaler, + baudrate) in sorted(values['baudrate_table'], key=(lambda i: i[3]), + reverse=True): + fp.write("%7d = %04X, %04X, %d # %s\n" + % (baudrate, baudgen, timegen, prescaler, + show_baudrate(baudgen, timegen, prescaler))) + +def calc_baudrate(baudgen, timegen, prescaler): + # This formulas are from AN205 page 5. + if prescaler == 0: + raise PrescalerIsZero("Prescaler is 0") + baudrate = (24000000. / prescaler) / (0x10000 - baudgen) + return (baudrate, (0x10000 - timegen) * 2) + +def show_baudrate(baudgen, timegen, prescaler): + try: + baudrate, timeout = calc_baudrate(baudgen, timegen, prescaler) + except PrescalerIsZero: + return "Wrong data, Prescaler is 0." + if timeout >= 1000: + timeout = "%1.3f ms" % (float(timeout) / 1000) + else: + timeout = "%d us" % timeout + if baudrate is None: + return ", %s" % (baudrate, timeout) + else: + return "%7.0f Baud, %s" % (baudrate, timeout) + +def update_values(v, new, dev): + old_baudrate_table = v.get('baudrate_table') + new_baudrate_table = new.get('baudrate_table') + + v.update(new) + + # update baudrate table + # it is needed, that the baudrate table has 32 entries when it is written + # to the eeprom or device. + if ((old_baudrate_table is not None or new_baudrate_table is not None) and + (new_baudrate_table is None or + len(new_baudrate_table) < SIZE_BAUDRATES)): + + if old_baudrate_table is not None: + if len(old_baudrate_table) < SIZE_BAUDRATES: + baudrate_table = old_baudrate_table + else: + baudrate_table = list(merge_baudrate_table(dev.baudrate_table, + old_baudrate_table)) + else: + baudrate_table = dev.baudrate_table + + if new_baudrate_table: + baudrate_table = list(merge_baudrate_table(baudrate_table, + new_baudrate_table)) + v['baudrate_table'] = baudrate_table + + + +def merge_baudrate_table(old, new): + for (old_info, (start, stop)) in zip(old, REQUEST_BAUDRATE_RANGES): + for baudgen, timer, prescaler, baudrate in new: + if ((start is None or baudrate <= start) and + baudrate >= stop): + yield (baudgen, timer, prescaler, baudrate) + break + else: + yield old_info + +REQUEST_BAUDRATE_RANGES = [ +# The table data is from AN205 Table 1 on page 1. +# Start End Default Baudrate + (None, 2457601), # Undefined + (2457600, 1474561), # Undefined + (1474560, 1053258), # Undefined + (1053257, 670255), # 921600 + ( 670254, 567139), # 576000 + ( 567138, 491521), # 500000 + ( 491520, 273067), # 460800 + ( 273066, 254235), # 256000 + ( 254234, 237833), # 250000 + ( 237832, 156869), # 230400 + ( 156868, 129348), # 153600 + ( 129347, 117029), # 128000 + ( 117028, 77609), # 115200 + ( 77608, 64112), # 76800 + ( 64111, 58054), # 64000 + ( 58053, 56281), # 57600 + ( 56280, 51559), # 56000 + ( 51558, 38602), # 51200 + ( 38601, 28913), # 38400 + ( 28912, 19251), # 28800 + ( 19250, 16063), # 19200 + ( 16062, 14429), # 16000 + ( 14428, 9613), # 14400 + ( 9612, 7208), # 9600 + ( 7207, 4804), # 7200 + ( 4803, 4001), # 4800 + ( 4000, 2401), # 4000 + ( 2400, 1801), # 2400 + ( 1800, 1201), # 1800 + ( 1200, 601), # 1200 + ( 600, 301), # 600 + ( 300, 57), # 300 +] diff --git a/contrib/cp210x-program/cp210x/valuefile.pyc b/contrib/cp210x-program/cp210x/valuefile.pyc new file mode 100644 index 0000000..e3c4115 Binary files /dev/null and b/contrib/cp210x-program/cp210x/valuefile.pyc differ diff --git a/contrib/cp210x-program/doc/45-cp210x-programming.rules b/contrib/cp210x-program/doc/45-cp210x-programming.rules new file mode 100644 index 0000000..84c32b2 --- /dev/null +++ b/contrib/cp210x-program/doc/45-cp210x-programming.rules @@ -0,0 +1,9 @@ +# udev rules file for CP210x device to be programmable py +# cp210x-program + +SUBSYSTEM!="usb_device", GOTO="cp210x-programming_rules_end" + +SYSFS{idVendor}=="10C4", SYSFS{idProduct}=="EA60", MODE="0660", GROUP="plugdev" +SYSFS{idVendor}=="10C4", SYSFS{idProduct}=="EA61", MODE="0660", GROUP="plugdev" + +LABEL="cp210x-programming_rules_end" diff --git a/contrib/cp210x-program/doc/cp210x.txt b/contrib/cp210x-program/doc/cp210x.txt new file mode 100644 index 0000000..f9b2156 --- /dev/null +++ b/contrib/cp210x-program/doc/cp210x.txt @@ -0,0 +1,107 @@ + + +COMMAND: 0xFF (ACCESS EEPROM) +INDEX SIZE VALUE +0x3701 2 VENDOR ID +0x3702 2 PRODUCT ID +0x3703 - PRODUCT STRING +0x3704 - SERIAL NUMBER +0x3705 ? ? (bmAttribute?) +0x3706 1 MAX POWER (2mA) - +0x3707 2 BCD DEV VERSION - +0x3708 ? ? +0x3709 1024 EEPROM DUMP +0x370A 1 LOCK VALUE +0x370B 1 PART NUMBER + +COMMAND: 0x0F UNKNOWN + +HEXFILE: + +0x3808: 256 Bytes PRODUCT STRING (incl length and language) +0x3908: 128 Bytes SERIAL NUMBER (incl length and language) +0x3987: 1 Byte probably PART NUMBER +0x3988: 18 Byte DEVICE DESCRIPTOR +0x399A: 32 Byte CONFIG DESCRIPTOR +0x39C3: ? Byte VENDOR STRING +0x39FF: 1 Byte LOCK VALUE + +1201100100000040ad139999020001020301 + +02 +1201100100000040AD139999326401020301 + +090220000101008096 +0904000002FF000002 +07058102400000 +07050102400000 +000000000000000000 +1A03530069006C00690063006F006E0020004C00610062007300000000 +00000000000000000000000000000000 +000000000000000000000000000000FF + +:10360000FFF0FFFA010060E31600FFF0FFFA01008F +:1036100060E31600FFECFFF80100804F1200FFE4AA +:10362000FF40010050F80C00FFD6FF40010000CA27 +:103630000800FFD0FFEE010020A10700FFCCFFEC47 +:10364000010000080700FFA2FFDC010000E8030002 +:10365000FFA0FFDC010090D00300FF98FFD901001C +:1036600000840300FF64FFC5010000580200FF440E +:10367000FFB9010000F40100FF2CFF40010000C26F +:103680000100FEC8FF8B0100002C0100FE89FF73C2 +:10369000010000FA0000FE5FFF63010000E100008E +:1036A000FE53FF5F0100C0DA0000FE2BFF50010057 +:1036B00000C80000FD8FFF15010000960000FCBF50 +:1036C000FEC7010080700000FB1EFE2B0100004BB6 +:1036D0000000FA24FE0C0100803E0000F97DFE0C83 +:1036E000010040380000F63CFE0C0100802500007F +:1036F000F2FBFE0C0100201C0000EC78FE0C010027 +:10370000C0120000E890FE0C0100A00F0000D8F0ED +:10371000FE0C010060090000CBEBFE0C0100080765 +:103720000000B1E0FE0C0100B004000063C0FE0C1C +:10373000010058020000B1E0FE0C04002C01000062 +:103740000000000000000000000000000000000079 +:103750000000000000000000000000000000000069 +:103760000000000000000000000000000000000059 +:103770000000000000000000000000000000000049 +:103780000000000000000000000000000000000039 +:103790000000000000000000000000000000000029 +:1037A0000000000000000000000000000000000019 +:1037B0000000000000000000000000000000000009 +:1037C00000000000000000000000000000000000F9 +:1037D00000000000000000000000000000000000E9 +:1037E00000000000000000000000000000000000D9 +:1037F00000000000000000000000000000000002C7 +:1038000004030904000000002203500052004F008E +:10381000440055004300540020005300540052005F +:1038200049004E0047002100210065007200200081 +:1038300042007200690064006700650020004300D8 +:103840006F006E00740072006F006C006C00650009 +:1038500072000000000000000000000000000000F6 +:103860000000000000000000000000000000000058 +:103870000000000000000000000000000000000048 +:103880000000000000000000000000000000000038 +:103890000000000000000000000000000000000028 +:1038A0000000000000000000000000000000000018 +:1038B0000000000000000000000000000000000008 +:1038C00000000000000000000000000000000000F8 +:1038D00000000000000000000000000000000000E8 +:1038E00000000000000000000000000000000000D8 +:1038F00000000000000000000000000000000000C8 +:1039000000000000000000120331003100300030E0 +:1039100000300033003200380000000000000000DA +:103920000000000000000000000000000000000097 +:103930000000000000000000000000000000000087 +:103940000000000000000000000000000000000077 +:103950000000000000000000000000000000000067 +:103960000000000000000000000000000000000057 +:103970000000000000000000000000000000000047 +:1039800000000000000000021201100100000040D1 +:10399000AD1399993264010203010902200001016B +:1039A0000080960904000002FF0000020705810262 +:1039B0004000000705010240000000000000000078 +:1039C0000000001A03530069006C00690063006F77 +:1039D000006E0020004C00610062007300000000D7 +:1039E00000000000000000000000000000000000D7 +:1039F000000000000000000000000000000000FFC8 +:00000001FF diff --git a/contrib/cp210x-program/doc/index.html b/contrib/cp210x-program/doc/index.html new file mode 100644 index 0000000..1998469 --- /dev/null +++ b/contrib/cp210x-program/doc/index.html @@ -0,0 +1,129 @@ + + + + + +CP210x Programmer + + +

CP210x Programmer

+ +

+ ATTENTION: THIS VERSION OF cp210x-program IS NOT FULLY TESTED. +IT MAY RENDER YOUR CP210x USELESS OR DESTROY IT. +

+ + + +

+Be aware that the current version is only tested on CP2102. +

+ +

+The goal of this library is to provide access to the EEPROM of an Silabs CP210x +under Linux. +

+ +

+The CP210x is an USB-to-serial chip used in a lot of USB devices (similar to +FTDIs and PL2303). The CP210x has a EEPROM on the chip which can be programmed +via USB. Silabs provides already a library and gui programm to program this +EEPROM, but only for windows. +

+ +

+This project uses results from monitoring the USB bus when the windows library +programms an CP210x. The windows library was not disassembled for this protocol +analysis. +

+ +

+When the programm is finished, a later goal would be to provide a library which +can be used to access further functions of the CP210x like the general IO pins +of the CP2103. The goal is not to provide an tty driver, such driver exists +already for linux and BSD. +

+ + +
+
Version:
0.2
+
Author:
Johannes Hölzl <johannes.hoelzl@gmx.de>
+
+ +

Dependencies

+ +
    +
  • Python >= 2.4
  • +
  • ctypes >= 0.9 (only when Python 2.4 is used)
  • +
  • libusb 0.1
  • +
+ +

+Since libusb is available on most Linux, Mac OS X and FreeBSD cp210x-program +should run flawlessly on these platforms. Currently it is only tested on +Linux 2.6 (Ubuntu). +

+ +

+If cp210x-program should run as non-root user, add the udev rule found in +doc/45-cp210x-programming.rules to /etc/udev/rules.d. When devices with already +programmed IDs are reprogrammed at this IDs to 45-cp210x-programming.rules. +

+ +

Usage

+ +

Read EEPROM content into hexfile:

+
$ cp210x-program --read-cp210x -F eeprom-content.hex
+
+ +

Show EEPROM content from device 002 on bus 001:

+
$ cp210x-program --read-cp210x -m 001/002
+
+ +

Write some data to device with vendor id 0x10C4 and product id 0xEA62:

+
$ cp210x-program --write-cp210x -m 10C4:EA62 \
+                 --set-product-string="Product String" \
+                 --set-max-power=100 \
+                 --set-bus-powered=no
+
+ +

TODO

+ +
    +
  • Test on other than CP2102
  • +
  • Implement CP2103 GIOP settings
  • +
  • Implement GUI
  • +
+ +

Development

+ +
    +
  • Clone repository: hg clone http://cp210x-program.hg.sourceforge.net/hgweb/cp210x-program/cp210x-program/
  • +
  • Run, Change, Debug, Commit, Repeat
  • +
  • Ask me to get commit rights
  • +
  • Profit!
  • +
+ +

Links

+ + + +

License

+ +

+ The python package 'cp210x' and the python script 'cp210x-program' are provided +under the terms of the GNU LGPL. See LICENSE. +

+ + diff --git a/contrib/cp210x-program/setup.py b/contrib/cp210x-program/setup.py new file mode 100755 index 0000000..4889f35 --- /dev/null +++ b/contrib/cp210x-program/setup.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from distutils.core import setup + +setup( + name="cp210x-program", + version="0.2", + description="Provides access to the EEPROM in an Silabs CP210x", + long_description=""" +The goal of this library is to provide access to the EEPROM of an Silabs CP210x +under Linux. + +The CP210x is an USB-to-serial chip used in a lot of USB devices (similar to +FTDIs and PL2303). The CP210x has a EEPROM on the chip which can be programmed +via USB. Silabs provides already a library and gui programm to program this +EEPROM, but only for windows. + +For more information see: +* http://www.silabs.com/tgwWebApp/public/web_content/products/Microcontrollers/Interface/en/interface.htm +* http://www.silabs.com/public/documents/tpub_doc/anote/Microcontrollers/Interface/en/an144.pdf + +Requires: +* libusb: http://libusb.sourceforge.net/ +* ctypes: http://starship.python.net/crew/theller/ctypes/ +""", + author="Johannes Hölzl", + author_email="johannes.hoelzl@gmx.de", + url="http://cp210x-program.sourceforge.net/", + license="GNU LGPL", + platforms="POSIX", + classifiers=[ + "License :: OSI-Approved Open Source :: GNU Library or Lesser General Public License (LGPL)", + "Intended Audience :: Manufacturing", + "Development Status :: 4 - Beta", + "Topic :: System :: Hardware :: Hardware Drivers", + "Programming Language :: Python", + "Operating System :: POSIX :: Linux", + "Operating System :: POSIX :: BSD :: FreeBSD", + "Operating System :: MacOS :: MacOS X", + ], + packages=[ + 'cp210x' + ], + scripts=[ + 'cp210x-program' + ], +) diff --git a/kernel/code/Makefile b/kernel/code/Makefile new file mode 100644 index 0000000..a478c0c --- /dev/null +++ b/kernel/code/Makefile @@ -0,0 +1,15 @@ + +PROGS=radiator program_radiator +CSRCS=libradiator.c + +OBJS=${CSRCS:%.c=%.o} + +default:${PROGS} + + +${PROGS}: %:%.o ${OBJS} + ${CC} -o $@ ${OBJS} $@.o + + +clean: + /bin/rm -f core ${OBJS} ${PROGS} ${PROGS:%=%.o} diff --git a/kernel/code/gpio.c b/kernel/code/gpio.c new file mode 100644 index 0000000..549eb81 --- /dev/null +++ b/kernel/code/gpio.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + + +#define log_ioctl(fd,op,arg) do_ioctl(fd,#op,op,arg) + +int +do_ioctl (int fd, char *sop, int op, void *arg) +{ + int ret; + + errno = 0; + + ret = ioctl (fd, op, arg); + + printf ("ioctl(%d,%s(0x%x),%p)=%d (errno=%d(%s))\n", fd, sop, op, arg, ret, + errno, strerror (errno)); + + return ret; +} + +int +main (int argc, char *argv[]) +{ + struct cp210x_port_config config; + + int fd = open (argv[1], O_RDWR); + + memset (&config, 0, sizeof (config)); + + log_ioctl (fd, CPIOC_PORTCONFGET, &config); + + printf ("reset=(%x,%x,%x), suspend=(%x,%x,%x), enhanced_fxn=%x\n", + config.reset.mode, + config.reset.low_power, + config.reset.latch, + config.suspend.mode, + config.suspend.low_power, + config.suspend.latch, config.enhanced_fxn); + + +} diff --git a/kernel/code/libradiator.c b/kernel/code/libradiator.c new file mode 100644 index 0000000..7baed2e --- /dev/null +++ b/kernel/code/libradiator.c @@ -0,0 +1,463 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "radiator.h" + + + + + +/* + * bit 0: 1 left leg NPN base + * bit 1: 2 left leg PNP base + * bit 2: 4 right leg PNP base + * bit 3: 8 right leg NPN base + */ + +#define LEFT_NPN_OFF 0 +#define LEFT_NPN_ON 1 +#define LEFT_PNP_OFF 2 +#define LEFT_PNP_ON 0 + +#define LEFT_LOW (LEFT_NPN_ON | LEFT_PNP_OFF) +#define LEFT_HI (LEFT_NPN_OFF | LEFT_PNP_ON) +#define LEFT_OFF (LEFT_NPN_OFF | LEFT_PNP_OFF) + +#define RIGHT_NPN_OFF 0 +#define RIGHT_NPN_ON 8 +#define RIGHT_PNP_OFF 4 +#define RIGHT_PNP_ON 0 + + +#define RIGHT_LOW (RIGHT_NPN_ON | RIGHT_PNP_OFF) +#define RIGHT_HI (RIGHT_NPN_OFF | RIGHT_PNP_ON) +#define RIGHT_OFF (RIGHT_NPN_OFF | RIGHT_PNP_OFF) + +#define BRAKE (RIGHT_LOW | LEFT_LOW) +#define FORWARDS (RIGHT_LOW | LEFT_HI) +#define BACKWARDS (RIGHT_HI | LEFT_LOW) +#define OFF (RIGHT_OFF| LEFT_OFF) + +#define TIMEOUT 750000 /*75ms */ +#define RUN_IN 50 +#define RELEASE_TENSION 10 + +static int brake_motor(int fd) +{ + int i=BRAKE; + printf (" Motor braking\n"); + return ioctl (fd, CPIOC_GPIOSET, &i); +} + +static int +drive_motor (int fd, int dir) +{ + int i; + + if (dir > 0) + { + printf (" Motor forwards\n"); + i = FORWARDS; + } + else if (dir < 0) + { + printf (" Motor backwards\n"); + i = BACKWARDS; + } + else + { + printf (" Motor off\n"); + i = OFF; + } + + + return ioctl (fd, CPIOC_GPIOSET, &i); +} + +static int +sensor_led_power (int fd, int on) +{ + int i = TIOCM_DTR; + + printf (" Rotation sensor power %s\n", on ? "on" : "off"); + + return ioctl (fd, on ? TIOCMBIS : TIOCMBIC, &i); +} + + +#if 0 +static int +count_steps (int fd, int count) +{ + struct timeval tv, tv_changed, tv_diff; + int cts, old_cts = -1; + int counted = 0; + + printf (" Counting %d steps", count); + + gettimeofday (&tv_changed, NULL); + + while (count) + { + ioctl (fd, TIOCMGET, &cts); + cts &= TIOCM_CTS; + + if (cts != old_cts) + { + if (cts) + { + counted++; + count--; + printf ("."); + fflush (stdout); + } + gettimeofday (&tv_changed, NULL); + old_cts = cts; + } + gettimeofday (&tv, NULL); + timersub (&tv, &tv_changed, &tv_diff); + + if (tv_diff.tv_sec || (tv_diff.tv_usec > TIMEOUT)) + break; + + } + + printf ("%d steps\n", counted); + return counted; +} +#else +count_steps (int fd, int count) +{ + int counted = 0; + struct timeval tv; + char buf[128]; + int i; + fd_set rfds; + + printf (" Counting %d steps", count); + +FD_ZERO(&rfds); + +tcflush(fd,TCIFLUSH); + + + while (count) + { + tv.tv_sec=0; + tv.tv_usec=TIMEOUT; + FD_SET(fd,&rfds); + + if (!select(fd+1,&rfds,NULL,NULL,&tv)) break; + + i=read(fd,buf,sizeof(buf)); + if (i<0) continue; + + counted+=i; + if (count>0) { + count-=i; + if (count<0) count=0; + } + + printf ("."); + fflush (stdout); + } + + printf ("%d steps\n", counted); + return counted; +} +#endif + + + + +static int +drive_motor_count (Radiator * r, int dir, int steps) +{ + int s1, s2, ret; + + sensor_led_power (r->fd, 1); + drive_motor (r->fd, dir); + s1 = count_steps (r->fd, steps); + if (s1 != steps) { + printf (" Hit end stop\n"); + drive_motor (r->fd, 0); + } else { + brake_motor(r->fd); + } + s2 = count_steps (r->fd, -1); + sensor_led_power (r->fd, 0); + drive_motor (r->fd, 0); + + /*did we hit an end stop? */ + if (s1 != steps) + ret = s1 - s2; + else + ret = s1 + s2; + + printf (" %d steps under power (of %d), %d steps coasting -> %d steps\n", + s1, steps, s2, ret); + + r->pos += ret * dir; + + /*Recalibrate our notion of ends */ + + if (s1 != steps) + { + if (dir == -1) + { + r->pos = 0; + } + else + { + r->max = r->pos; + } + } + + return ret; +} + + + + +int +radiator_set_pos (Radiator * r, int wanted) +{ + int guard, delta; + + delta = wanted - r->pos; + printf ("Now at %d want %d, delta %d\n", r->pos, wanted, delta); + + if (wanted == 0) + { + drive_motor_count (r, -1, -1); + return 0; + } + else if (r->max && (wanted >= r->max)) + { + drive_motor_count (r, 1, -1); + return 0; + } + + + guard = r->overshoot * 3; + + if ((delta > -guard) && (delta < guard)) + { + printf ("Too close\n"); + + drive_motor_count (r, (r->pos > r->half) ? -1 : 1, RUN_IN); + + delta = wanted - r->pos; + printf ("Now at %d want %d, delta %d\n", r->pos, wanted, delta); + } + + + if (delta < 0) + { + drive_motor_count (r, -1, (-delta) - r->overshoot); + } + else if (delta > 0) + { + drive_motor_count (r, 1, delta - r->overshoot); + } + + printf ("Wanted %d, got %d\n", wanted, r->pos); + + return 0; +} + +int +radiator_calibrate (Radiator * r) +{ + radiator_set_pos (r, 0); + + drive_motor_count (r, 1, RUN_IN); + + r->overshoot = r->pos - RUN_IN; +} + + +static int utf16(uint8_t *dst,uint8_t *src) +{ +int len=0; + + +while (*src) { + *(dst++)=*(src++); + len+=2; + *(dst++)=0; +} + +return len; +} + + +static void make_usb_descriptor(uint8_t *dst,uint8_t *src) +{ +int len; +len=utf16(dst+2,src); +dst[0]=len+2; +dst[1]=0x3; +} + + + + +int +radiator_program (Radiator * r) +{ + struct cp210x_port_config config; + uint8_t buf[256]; + + +#if 0 + memset (&config, 0, sizeof (config)); + ioctl (r->fd, CPIOC_PORTCONFGET, &config); + + + printf + ("config was reset=(%x,%x,%x), suspend=(%x,%x,%x), enhanced_fxn=%x\n", + config.reset.mode, config.reset.low_power, config.reset.latch, + config.suspend.mode, config.suspend.low_power, config.suspend.latch, + config.enhanced_fxn); + + + memset (&config, 0, sizeof (config)); + + config.reset.mode = ~CP_INPUT_PINS; + config.reset.low_power = 0; + config.reset.latch = CP_INPUT_PINS | CP_PIN_GPIO_1 | CP_PIN_GPIO_2; + config.suspend.mode = ~CP_INPUT_PINS; + config.suspend.low_power = 0; + config.suspend.latch = CP_INPUT_PINS | CP_PIN_GPIO_1 | CP_PIN_GPIO_2; + config.enhanced_fxn = CP_EFXN_ENABLE_WPU; + + ioctl (r->fd, CPIOC_PORTCONFSET, &config); + + memset (&config, 0, sizeof (config)); + ioctl (r->fd, CPIOC_PORTCONFGET, &config); + + + printf + ("config is reset=(%x,%x,%x), suspend=(%x,%x,%x), enhanced_fxn=%x\n", + config.reset.mode, config.reset.low_power, config.reset.latch, + config.suspend.mode, config.suspend.low_power, config.suspend.latch, + config.enhanced_fxn); + +#endif + memset(buf,0,sizeof(buf)); + +{ +int i=0x413c; + ioctl (r->fd, CPIOC_SETVID, &i); +} + +{ +int i=0x9500; + ioctl (r->fd, CPIOC_SETPID, &i); +} + + make_usb_descriptor(buf,"USB Radiator Valve"); + ioctl (r->fd, CPIOC_SETPRODUCT, buf); + + make_usb_descriptor(buf,"000001"); + ioctl (r->fd, CPIOC_SETSERIAL, buf); + +#if 0 + make_usb_descriptor(buf,"Global Panaceas"); + ioctl (r->fd, CPIOC_SETMFG, buf); +#endif + + + ioctl (r->fd, CPIOC_DEVICERESET, 0); + + + + + return 0; +} + + +static void tty_setup(int fd) +{ +struct termios tios; + +tcgetattr(fd, &tios); + +cfmakeraw( &tios); + + tios.c_iflag = PARMRK | INPCK; + tios.c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0; + tios.c_lflag = 0; + tios.c_cflag = CS8 | CREAD | CLOCAL; + tios.c_cc[VMIN]=1; + tios.c_cc[VTIME]=0; + + cfsetispeed (&tios, B115200); + cfsetospeed (&tios, B115200); + + tcsetattr (fd, TCSANOW, &tios); +} + + + +static void +set_nonblocking (int fd) +{ + long arg = 0; + arg = fcntl (fd, F_GETFL, arg); + arg |= O_NONBLOCK; + fcntl (fd, F_SETFL, arg); +} + +static void +set_blocking (int fd) +{ + long arg = 0; + arg = fcntl (fd, F_GETFL, arg); + arg &= ~O_NONBLOCK; + fcntl (fd, F_SETFL, arg); +} + + +Radiator * +radiator_open (char *s, int quiet) +{ + Radiator *r = malloc (sizeof (Radiator)); + + r->fd = open (s, O_RDWR |O_NOCTTY); + + if (r->fd < 0) + { + free (r); + return NULL; + } + + tty_setup(r->fd); + set_nonblocking(r->fd); + + r->half = 5 * RUN_IN; + r->overshoot = 0; + r->max = 0; + r->pos = 0; + + if (!quiet) + radiator_calibrate (r); +} + +void +radiator_close (Radiator * r) +{ + close (r->fd); + free (r); +} diff --git a/kernel/code/program_radiator.c b/kernel/code/program_radiator.c new file mode 100644 index 0000000..75077da --- /dev/null +++ b/kernel/code/program_radiator.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include "radiator.h" + +int +main (int argc, char *argv[]) +{ + Radiator *r; + int pos; + char buf[1024]; + + + r = radiator_open (argv[1],1); + + if (!r) + return -1; + + radiator_program(r); + + return 0; +} diff --git a/kernel/code/radiator.c b/kernel/code/radiator.c new file mode 100644 index 0000000..2d594da --- /dev/null +++ b/kernel/code/radiator.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include "radiator.h" + +int +main (int argc, char *argv[]) +{ + Radiator *r; + int pos; + char buf[1024]; + + + r = radiator_open (argv[1],0); + + if (!r) + return -1; + + + for (;;) + { + printf ("Current position %d (end stop at %d, overshoots by %d)\n", + r->pos, r->max, r->overshoot); + printf ("Enter new>"); + fflush (stdout); + buf[sizeof (buf) - 1] = 0; + fgets (buf, sizeof (buf) - 1, stdin); + radiator_set_pos (r, atoi (buf)); + } + + + return 0; +} diff --git a/kernel/code/radiator.h b/kernel/code/radiator.h new file mode 100644 index 0000000..a16edb8 --- /dev/null +++ b/kernel/code/radiator.h @@ -0,0 +1,19 @@ +#ifndef _RADIATOR_H_ +#define _RADIATOR_H_ + +typedef struct +{ + int fd; + int pos; + int half; + int overshoot; + int max; +} Radiator; + +int radiator_set_pos (Radiator * r, int wanted); +int radiator_calibrate (Radiator * r); +int radiator_program (Radiator * r); +Radiator *radiator_open (char *s, int quiet); +void radiator_close (Radiator * r); + +#endif /* _RADIATOR_H_ */ diff --git a/kernel/code/set.c b/kernel/code/set.c new file mode 100644 index 0000000..4c8de0e --- /dev/null +++ b/kernel/code/set.c @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + + +#define log_ioctl(fd,op,arg) do_ioctl(fd,#op,op,arg) + +int +do_ioctl (int fd, char *sop, int op, void *arg) +{ + int ret; + + errno = 0; + + ret = ioctl (fd, op, arg); + + printf ("ioctl(%d,%s(0x%x),%p)=%d (errno=%d(%s))\n", fd, sop, op, arg, ret, + errno, strerror (errno)); + + return ret; +} + +int +main (int argc, char *argv[]) +{ + struct cp210x_port_config config; + + int fd = open (argv[1], O_RDWR); + + memset (&config, 0, sizeof (config)); + +// config.reset.mode=0xf05f; + config.reset.mode = ~CP_INPUT_PINS; + config.reset.low_power = 0; + config.reset.latch = CP_INPUT_PINS | CP_PIN_GPIO_1 | CP_PIN_GPIO_2; +// config.suspend.mode=0xf05f; + config.suspend.mode = ~CP_INPUT_PINS; + config.suspend.low_power = 0; + config.suspend.latch = CP_INPUT_PINS | CP_PIN_GPIO_1 | CP_PIN_GPIO_2; +// config.enhanced_fxn=0x30; + config.enhanced_fxn = CP_EFXN_ENABLE_WPU; + + + log_ioctl (fd, CPIOC_PORTCONFSET, &config); + log_ioctl (fd, CPIOC_PORTCONFGET, &config); + + printf ("reset=(%x,%x,%x), suspend=(%x,%x,%x), enhanced_fxn=%x\n", + config.reset.mode, + config.reset.low_power, + config.reset.latch, + config.suspend.mode, + config.suspend.low_power, + config.suspend.latch, config.enhanced_fxn); + + log_ioctl (fd, CPIOC_DEVICERESET, 0); + +} diff --git a/kernel/code/thing.c b/kernel/code/thing.c new file mode 100644 index 0000000..d9b5eac --- /dev/null +++ b/kernel/code/thing.c @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * bit 0: 1 left leg NPN base + * bit 1: 2 left leg PNP base + * bit 2: 4 right leg PNP base + * bit 3: 8 right leg NPN base + */ + +#define LEFT_NPN_OFF 0 +#define LEFT_NPN_ON 1 +#define LEFT_PNP_OFF 2 +#define LEFT_PNP_ON 0 + +#define LEFT_LOW (LEFT_NPN_ON | LEFT_PNP_OFF) +#define LEFT_HI (LEFT_NPN_OFF | LEFT_PNP_ON) +#define LEFT_OFF (LEFT_NPN_OFF | LEFT_PNP_OFF) + +#define RIGHT_NPN_OFF 0 +#define RIGHT_NPN_ON 8 +#define RIGHT_PNP_OFF 4 +#define RIGHT_PNP_ON 0 + + +#define RIGHT_LOW (RIGHT_NPN_ON | RIGHT_PNP_OFF) +#define RIGHT_HI (RIGHT_NPN_OFF | RIGHT_PNP_ON) +#define RIGHT_OFF (RIGHT_NPN_OFF | RIGHT_PNP_OFF) + +#define FORWARDS (RIGHT_LOW | LEFT_HI) +#define BACKWARDS (RIGHT_HI | LEFT_LOW) +#define OFF (RIGHT_OFF| LEFT_OFF) + +#define TIMEOUT 750000 /*75ms */ +#define RUN_IN 50 +#define RELEASE_TENSION 10 + +int +drive_motor (int fd, int dir) +{ + int i; + + if (dir > 0) + { + printf (" Motor forwards\n"); + i = FORWARDS; + } + else if (dir < 0) + { + printf (" Motor backwards\n"); + i = BACKWARDS; + } + else + { + printf (" Motor off\n"); + i = OFF; + } + + + return ioctl (fd, CPIOC_GPIOSET, &i); +} + +int +sensor_led_power (int fd, int on) +{ + int i = TIOCM_DTR; + + printf (" Rotation sensor power %s\n", on ? "on" : "off"); + + return ioctl (fd, on ? TIOCMBIS : TIOCMBIC, &i); +} + + +static int +count_steps (int fd, int count) +{ + struct timeval tv, tv_changed, tv_diff; + int cts, old_cts = -1; + int counted = 0; + + printf (" Counting %d steps", count); + + gettimeofday (&tv_changed, NULL); + + while (count) + { + ioctl (fd, TIOCMGET, &cts); + cts &= TIOCM_CTS; + + if (cts != old_cts) + { + if (cts) + { + counted++; + count--; + printf ("."); + fflush (stdout); + } + gettimeofday (&tv_changed, NULL); + old_cts = cts; + } + gettimeofday (&tv, NULL); + timersub (&tv, &tv_changed, &tv_diff); + + if (tv_diff.tv_sec || (tv_diff.tv_usec > TIMEOUT)) + break; + + } + + printf ("%d steps\n", counted); + return counted; +} + + + + +static int +drive_motor_home (int fd) +{ + int ret; + sensor_led_power (fd, 1); + drive_motor (fd, -1); + ret = count_steps (fd, -1); + drive_motor (fd, 0); + ret -= count_steps (fd, -1); + sensor_led_power (fd, 0); + return ret; +} + +static int +drive_motor_count (int fd, int dir, int steps) +{ + int ret; + sensor_led_power (fd, 1); + drive_motor (fd, dir); + ret = count_steps (fd, steps); + drive_motor (fd, 0); + ret += count_steps (fd, -1); + sensor_led_power (fd, 0); + return ret; +} + +int +main (int argc, char *argv[]) +{ + int overshoot, guard, pos, wanted, delta; + + int fd = open (argv[1], O_RDWR); + + if (fd < 0) + return -1; + + + pos = drive_motor_home (fd); + + printf ("Motor was %d steps from home\n", pos); + + + if (argc == 2) + { + pos = drive_motor_count (fd, 1, RELEASE_TENSION); + + printf ("Motor now %d steps from home\n", pos); + return 0; + } + + pos = drive_motor_count (fd, 1, RUN_IN); + + overshoot = pos - RUN_IN; /*overshot */ + + wanted = atoi (argv[2]); + + delta = wanted - pos; + printf ("Now at %d want %d, delta %d\n", pos, wanted, delta); + + guard = overshoot * 3; + + if ((delta > -guard) && (delta < guard)) + { + printf ("Too close\n"); + pos += drive_motor_count (fd, 1, RUN_IN); + delta = wanted - pos; + printf ("Now at %d want %d, delta %d\n", pos, wanted, delta); + } + + + if (delta < 0) + { + pos -= drive_motor_count (fd, -1, (-delta) - overshoot); + } + else if (delta > 0) + { + pos += drive_motor_count (fd, 1, delta - overshoot); + } + + printf ("Wanted %d steps, got %d steps\n", wanted, pos); + + return 0; +} diff --git a/kernel/cp2103-3.11.10.patch b/kernel/cp2103-3.11.10.patch new file mode 100644 index 0000000..949d962 --- /dev/null +++ b/kernel/cp2103-3.11.10.patch @@ -0,0 +1,491 @@ +diff --git a/drivers/usb/serial/cp210x.c b/drivers/usb/serial/cp210x.c +index 0eae4ba..3e0b558 100644 +--- a/drivers/usb/serial/cp210x.c ++++ b/drivers/usb/serial/cp210x.c +@@ -23,14 +23,22 @@ + #include + #include + #include ++#include + + #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver" + ++#define CP210x_MAX_DEVICE_STRLEN 256 ++#define CP210x_MAX_PRODUCT_STRLEN 126 ++#define CP210x_MAX_SERIAL_STRLEN 63 ++#define CP210x_MAX_MAXPOWER 250 ++ ++ + /* + * Function Prototypes + */ + static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *); + static void cp210x_close(struct usb_serial_port *); ++static int cp210x_ioctl(struct tty_struct *, struct file *, unsigned long); + static void cp210x_get_termios(struct tty_struct *, struct usb_serial_port *); + static void cp210x_get_termios_port(struct usb_serial_port *port, + unsigned int *cflagp, unsigned int *baudp); +@@ -198,6 +206,7 @@ static struct usb_serial_driver cp210x_device = { + .bulk_out_size = 256, + .open = cp210x_open, + .close = cp210x_close, ++ .ioctl = cp210x_ioctl, + .break_ctl = cp210x_break_ctl, + .set_termios = cp210x_set_termios, + .tiocmget = cp210x_tiocmget, +@@ -286,6 +295,185 @@ static struct usb_serial_driver * const serial_drivers[] = { + #define CONTROL_WRITE_DTR 0x0100 + #define CONTROL_WRITE_RTS 0x0200 + ++ ++ ++/* ++ * cp210x_ctlmsg ++ * A generic usb control message interface. ++ * Returns the actual size of the data read or written within the message, 0 ++ * if no data were read or written, or a negative value to indicate an error. ++ */ ++static int cp210x_ctlmsg(struct usb_serial_port *port, u8 request, ++ u8 requestype, u16 value, u16 index, void *data, u16 size) ++{ ++ struct usb_serial *serial = port->serial; ++ u8 *tbuf; ++ int ret; ++ ++ if (!(tbuf = kmalloc(size, GFP_KERNEL))) ++ return -ENOMEM; ++ if (requestype & 0x80) { ++ ret = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0), request, ++ requestype, value, index, tbuf, size, 300); ++ if (ret > 0 && size) ++ memcpy(data, tbuf, size); ++ } else { ++ if (size) ++ memcpy(tbuf, data, size); ++ ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0), request, ++ requestype, value, index, tbuf, size, 300); ++ } ++ kfree(tbuf); ++ if (ret < 0 && ret != -EPIPE) { ++ dev_err(&port->dev, "cp210x: control failed cmd rqt %u " ++ "rq %u len %u ret %d\n", requestype, request, size, ret); ++ } ++ return ret; ++} ++ ++ ++static int cp210x_reset(struct usb_serial_port *port) ++{ ++ /* Is this better than usb_device_reset? It may be. Once a client issues ++ * the reset ioctl, it must disconnect and reconnect, since the USB ++ * connections are torn down. We also ignore the error return, since ++ * the part resets and doesn't send one... ++ */ ++ cp210x_ctlmsg(port, 0xff, 0x40, 0x0008, 0x00, 0, 0); ++ ++ return 0; ++} ++ ++// // cp210x_get_partnum ++ ++static inline int cp210x_setu16(struct usb_serial_port *port, int cmd, ++ unsigned int value) ++{ ++ return cp210x_ctlmsg(port, 0xff, 0x40, 0x3700 | (cmd & 0xff), ++ value, 0, 0); ++} ++ ++ ++// make_usb_string ++/* ++ * cp210x_setstr ++ * ++ * Set a USB string descriptor using proprietary cp210x control messages. ++ * Return the number of characters actually written. ++ */ ++static int cp210x_setstr(struct usb_serial_port *port, int cmd, uint8_t *usbstr,size_t len) ++{ ++ int ret = cp210x_ctlmsg(port, 0xff, 0x40, 0x3700 | (cmd & 0xff), 0, ++ usbstr, len); ++ dbg("%s - cmd 0x%02x len %d ret %d", __FUNCTION__, cmd, len, ret); ++ printk(KERN_ERR "%s - cmd 0x%02x len %d ret %d %x %x %x %x %x %x", __FUNCTION__, cmd, len, ret, ++ usbstr[0],usbstr[1],usbstr[2],usbstr[3],usbstr[4],usbstr[5]); ++ return ret; ++} ++ ++ ++ ++/* Set all gpio simultaneously */ ++static int cp210x_gpioset(struct usb_serial_port *port, int gpio) ++{ ++ dbg("%s - port %d, gpio = 0x%.2x", __FUNCTION__, port->number, gpio); ++ ++ return cp210x_ctlmsg(port, 0xff, 0x40, 0x37e1, ++ ((uint16_t)gpio << 8) | CP_GPIO_MASK, 0, 0); ++} ++ ++/* Set select gpio bits */ ++static int cp210x_gpiosetb(struct usb_serial_port *port, int set, int clear) ++{ ++ u16 gpio = 0; ++ ++ gpio |= set | clear; ++ gpio |= set << 8; ++ ++ dbg("%s - port %d, gpiob = 0x%.4x", __FUNCTION__, port->number, gpio); ++ ++ /* FIXME: how about REQTYPE_HOST_TO_DEVICE instead of 0x40? */ ++ return cp210x_ctlmsg(port, 0xff, 0x40, 0x37e1, gpio, 0, 0); ++} ++ ++static int cp210x_gpioget(struct usb_serial_port *port, int *gpio_ret) ++{ ++ int ret; ++ u8 gpio; ++ ++ dbg("%s - port %d", __FUNCTION__, port->number); ++ ++ /* FIXME: how about REQTYPE_DEVICE_TO_HOST instead of 0xc0? */ ++ ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x00c2, 0, &gpio, 1); ++ ++ dbg("%s - gpio = 0x%.2x (%d)", __FUNCTION__, gpio, ret); ++ ++ *gpio_ret=gpio; ++ ++ return (ret == 1) ? 0 : -EPROTO; ++} ++ ++static int cp210x_portconfset(struct usb_serial_port *port, ++ struct cp210x_port_config *config) ++{ ++ struct cp210x_port_config be_config; ++ int ret; ++ ++ dbg("%s", __FUNCTION__); ++ ++ memset(&be_config, 0, sizeof(be_config)); ++ ++ be_config.reset.mode = cpu_to_be16(config->reset.mode); ++ be_config.reset.low_power = 0; // cpu_to_be16(config->reset.low_power); ++ be_config.reset.latch = cpu_to_be16(config->reset.latch); ++ ++ be_config.suspend.mode = cpu_to_be16(config->suspend.mode); ++ be_config.suspend.low_power = 0; // cpu_to_be16(config->suspend.low_power); ++ be_config.suspend.latch = cpu_to_be16(config->suspend.latch); ++ ++ be_config.enhanced_fxn = config->enhanced_fxn; ++ ++ ++ ret = cp210x_ctlmsg(port, 0xff, 0x40, 0x370c, 0, &be_config, sizeof(be_config)); ++ if (ret == sizeof(be_config)) ++ return 0; ++ else if (ret >= 0) ++ return -EPROTO; ++ else ++ return ret; ++} ++ ++static int cp210x_portconfget(struct usb_serial_port *port, ++ struct cp210x_port_config *config) ++{ ++ int ret; ++ ++ dbg("%s", __FUNCTION__); ++ ++ ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x370c, 0, config, sizeof(*config)); ++ ++ if (ret != sizeof(*config)) ++ return (ret >= 0) ? -EPROTO : ret; ++ ++ /* Words from cp2103 are MSB */ ++ ++ config->reset.mode = be16_to_cpu(config->reset.mode); ++ config->reset.low_power = be16_to_cpu(config->reset.low_power); ++ config->reset.latch = be16_to_cpu(config->reset.latch); ++ ++ config->suspend.mode = be16_to_cpu(config->suspend.mode); ++ config->suspend.low_power = be16_to_cpu(config->suspend.low_power); ++ config->suspend.latch = be16_to_cpu(config->suspend.latch); ++ ++ /* apparently not implemented yet */ ++ config->reset.low_power = 0; ++ config->suspend.low_power = 0; ++ ++ return 0; ++} ++ ++ ++ + /* + * cp210x_get_config + * Reads from the CP210x configuration registers +@@ -469,6 +657,163 @@ static void cp210x_close(struct usb_serial_port *port) + cp210x_set_config_single(port, CP210X_IFC_ENABLE, UART_DISABLE); + } + ++static int cp210x_string_ioctl(struct usb_serial_port *port, unsigned long arg, int cmd,size_t max_len) ++{ ++ uint8_t buf[CP210x_MAX_STRLEN]; ++ size_t len; ++ ++ if (copy_from_user(buf,(uint8_t *)arg,1)) ++ return -EFAULT; ++ ++ len=buf[0]; ++ ++ if (len>max_len) ++ return -EMSGSIZE; ++ ++ if (copy_from_user(buf,(uint8_t *)arg,len)) ++ return -EFAULT; ++ ++ if (len && cp210x_setstr(port, cmd, buf, len) != len) ++ return -EPROTO; ++ ++ return 0; ++} ++ ++ ++static int cp210x_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) ++{ ++ struct usb_serial_port *port = tty->driver_data; ++ struct cp210x_port_config config; ++ int iarg; ++ int ret = -ENOIOCTLCMD; ++ ++ dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd); ++ ++ switch (cmd) { ++ case CPIOC_GPIOGET: ++ /*if (cp210x_get_partnum(port) != CP210x_CP2103_VERSION) break; */ ++ ret = cp210x_gpioget(port, &iarg); ++ ++ if (ret) ++ break; ++ ++ if (copy_to_user((int *)arg, &iarg, sizeof(iarg))) ++ ret = -EFAULT; ++ ++ break; ++ ++ case CPIOC_GPIOSET: ++ /*if (cp210x_get_partnum(port) != CP210x_CP2103_VERSION) break; */ ++ ++ if (copy_from_user(&iarg,(int *)arg, sizeof(iarg))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_gpioset(port, iarg); ++ ++ break; ++ ++ case CPIOC_GPIOBIS: ++ /*if (cp210x_get_partnum(port) != CP210x_CP2103_VERSION) break; */ ++ ++ if (copy_from_user(&iarg, (int *)arg, sizeof(iarg))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_gpiosetb(port, iarg, 0); ++ ++ break; ++ ++ case CPIOC_GPIOBIC: ++ /*if (cp210x_get_partnum(port) != CP210x_CP2103_VERSION) break; */ ++ ++ if (copy_from_user(&iarg, (int *)arg, sizeof(iarg))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_gpiosetb(port, 0, iarg); ++ ++ break; ++ ++ case CPIOC_DEVICERESET: ++ ret = cp210x_reset(port); ++ break; ++ ++ case CPIOC_PORTCONFGET: ++ ret = cp210x_portconfget(port, &config); ++ ++ if (ret) break; ++ ++ if (copy_to_user((struct cp210x_port_state *)arg, &config, sizeof(config))) ++ ret = -EFAULT; ++ ++ break; ++ ++ case CPIOC_PORTCONFSET: ++ if (copy_from_user(&config, (struct cp210x_port_state *)arg, sizeof(config))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_portconfset(port, &config); ++ break; ++ ++ case CPIOC_SETVID: ++ if (copy_from_user(&iarg, (int *)arg,sizeof(iarg))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_setu16(port, 0x01, iarg); ++ ++ break; ++ ++ case CPIOC_SETPID: ++ if (copy_from_user(&iarg, (int *)arg,sizeof(iarg))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_setu16(port, 0x02, iarg); ++ ++ break; ++ ++ case CPIOC_SETMFG: ++ ret = cp210x_string_ioctl(port, arg, 0x0, CP210x_MAX_MFG_STRLEN); ++ break; ++ ++ case CPIOC_SETPRODUCT: ++ ret = cp210x_string_ioctl(port, arg, 0x3, CP210x_MAX_PRODUCT_STRLEN); ++ ++ break; ++ ++ case CPIOC_SETSERIAL: ++ ret = cp210x_string_ioctl(port, arg, 0x3, CP210x_MAX_SERIAL_STRLEN); ++ break; ++ ++ case CPIOC_SETDEVVER: ++ if (copy_from_user(&iarg, (int *)arg,sizeof(iarg))) { ++ ret = -EFAULT; ++ break; ++ } ++ ++ ret = cp210x_setu16(port, 0x07, iarg); ++ ++ break; ++ default: ++ dbg("%s not supported = 0x%04x", __FUNCTION__, cmd); ++ break; ++ } ++ ++ return ret; ++} ++ ++ ++ ++ + /* + * cp210x_get_termios + * Reads the baud rate, data bits, parity, stop bits and flow control mode +diff --git a/include/linux/cp210x.h b/include/linux/cp210x.h +new file mode 100644 +index 0000000..39faf96 +--- /dev/null ++++ b/include/linux/cp210x.h +@@ -0,0 +1,100 @@ ++/* ++ * ++ */ ++#ifndef _CPIO210X_H ++#define _CPIO210X_H ++ ++#define CP210X_IOCTL_BASE 'C' ++ ++//struct watchdog_info { ++// __u32 options; /* Options the card/driver supports */ ++// __u32 firmware_version; /* Firmware version of the card */ ++// __u8 identity[32]; /* Identity of the board */ ++//}; ++ ++struct cp210x_port_state { ++ uint16_t mode; /* push-pull = 1, open-drain = 0 */ ++ uint16_t low_power; /* 1 = ground the pin, and disable */ ++ uint16_t latch; ++} __attribute__((packed)); ++ ++/* LOPWR MODE LATCH */ ++/* 0 0 0 Pin drivers are totem pole, and inital config sinks current to GND */ ++/* 0 0 1 Pin drivers are totem pole, and inital config sources current from VIO */ ++/* 0 1 0 Pin drivers are open drain, and inital config sinks current to GND */ ++/* 0 1 1 Pin drivers are open drain, and inital config leaves pin HI-Z (you want this for inputs) */ ++/* 1 X X Pin drivers are disabled, and pin sinks current to GND */ ++ ++ ++struct cp210x_port_config { ++ struct cp210x_port_state reset; ++ struct cp210x_port_state suspend; ++ uint8_t enhanced_fxn; ++} __attribute__((packed)); ++ ++ ++#define CP_PIN_RI (1 << 0) ++#define CP_PIN_DCD (1 << 1) ++#define CP_PIN_DTR (1 << 2) ++#define CP_PIN_DSR (1 << 3) ++#define CP_PIN_TXD (1 << 4) ++#define CP_PIN_RXD (1 << 5) ++#define CP_PIN_RTS (1 << 6) ++#define CP_PIN_CTS (1 << 7) ++#define CP_PIN_GPIO_0 (1 << 8) ++#define CP_PIN_GPIO_1 (1 << 9) ++#define CP_PIN_GPIO_2 (1 << 10) ++#define CP_PIN_GPIO_3 (1 << 11) ++#define CP_PIN_UNUSED_1 (1 << 12) ++#define CP_PIN_UNUSED_2 (1 << 13) ++#define CP_PIN_GPIO_SUSPEND (1 << 14) ++#define CP_PIN_GPIO_NSUSPEND (1 << 15) ++ ++#define CP_INPUT_PINS (CP_PIN_RXD|CP_PIN_CTS|CP_PIN_DSR|CP_PIN_RI|CP_PIN_DCD) ++ ++ ++#define CP_EFXN_GPIO_0_IS_TXLED (1 << 0) ++#define CP_EFXN_GPIO_1_IS_RXLED (1 << 1) ++#define CP_EFXN_GPIO_2_IS_RS485_TX (1 << 2) ++#define CP_EFXN_UNUSED_1 (1 << 3) /* Set to zero */ ++#define CP_EFXN_ENABLE_WPU (1 << 4) ++#define CP_EFXN_UNUSED_2 (1 << 5) /* Set to zero */ ++#define CP_EFXN_SERIAL_AUTOOFF (1 << 6) ++#define CP_EFXN_GPIOL_AUTOOFF (1 << 7) ++ ++ ++ ++ ++ ++ ++#define CPIOC_GPIOGET _IOR(CP210X_IOCTL_BASE, 0, int) ++#define CPIOC_GPIOSET _IOW(CP210X_IOCTL_BASE, 1, int) ++#define CPIOC_GPIOBIC _IOW(CP210X_IOCTL_BASE, 2, int) ++#define CPIOC_GPIOBIS _IOW(CP210X_IOCTL_BASE, 3, int) ++ ++#define CPIOC_DEVICERESET _IO(CP210X_IOCTL_BASE,4) ++#define CPIOC_PORTCONFGET _IOR(CP210X_IOCTL_BASE, 5, struct cp210x_port_config) ++#define CPIOC_PORTCONFSET _IOW(CP210X_IOCTL_BASE, 6, struct cp210x_port_config) ++ ++#define CPIOC_SETVID _IOW(CP210X_IOCTL_BASE,7, int) ++#define CPIOC_SETPID _IOW(CP210X_IOCTL_BASE,8, int) ++#define CPIOC_SETMFG _IOW(CP210X_IOCTL_BASE,9, char *) ++#define CPIOC_SETPRODUCT _IOW(CP210X_IOCTL_BASE,10, char *) ++#define CPIOC_SETSERIAL _IOW(CP210X_IOCTL_BASE,11, char *) ++#define CPIOC_SETDEVVER _IOW(CP210X_IOCTL_BASE,12, int) ++ ++/* CP2103 GPIO */ ++#define CP_GPIO_0 0x01 ++#define CP_GPIO_1 0x02 ++#define CP_GPIO_2 0x04 ++#define CP_GPIO_3 0x08 ++#define CP_GPIO_MASK (CP_GPIO_0|CP_GPIO_1|CP_GPIO_2|CP_GPIO_3) ++ ++ ++#define CP210x_MAX_MFG_STRLEN 128 ++#define CP210x_MAX_PRODUCT_STRLEN 126 ++#define CP210x_MAX_SERIAL_STRLEN 63 ++#define CP210x_MAX_MAXPOWER 250 ++ ++#endif /* _CPIO210X_H */ ++ diff --git a/userland/Makefile b/userland/Makefile new file mode 100644 index 0000000..d45447c --- /dev/null +++ b/userland/Makefile @@ -0,0 +1,20 @@ + +PROGS=radiator program_radiator +CSRCS=libradiator.c cp210x.c + +USBINC=$(shell pkg-config --cflags libusb-1.0) +USBLIB=$(shell pkg-config --libs libusb-1.0) + +CFLAGS=${USBINC} -g -Wall -Wno-unused +LIBS=${USBLIB} -g + +OBJS=${CSRCS:%.c=%.o} + +default:${PROGS} + +${PROGS}: %:%.o ${OBJS} + ${CC} ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS} $@.o ${LIBS} + + +clean: + /bin/rm -f core ${OBJS} ${PROGS} ${PROGS:%=%.o} diff --git a/userland/cp210x.c b/userland/cp210x.c new file mode 100644 index 0000000..2c1892b --- /dev/null +++ b/userland/cp210x.c @@ -0,0 +1,534 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cp210x_int.h" +#include "cp210x.h" + + +#define cpu_to_le32(a) htole32(a) +#define le32_to_cpu(a) le32toh(a) + +#define cpu_to_be16(a) htobe16(a) +#define be16_to_cpu(a) be16toh(a) + +#define TIMEOUT 300 + + +static int +cp210x_ctlmsg (CP210X * c, uint8_t request, + uint8_t requestype, uint16_t value, uint16_t index, void *data, + uint16_t size) +{ + uint8_t *tbuf; + int ret; + + if (!(tbuf = malloc (size))) + return -ENOMEM; + if (requestype & 0x80) + { + ret = libusb_control_transfer (c->handle, requestype, + request, value, index, tbuf, size, + TIMEOUT); + if (ret > 0 && size) + memcpy (data, tbuf, size); + } + else + { + if (size) + memcpy (tbuf, data, size); + ret = libusb_control_transfer (c->handle, requestype, + request, value, index, tbuf, size, + TIMEOUT); + } + free (tbuf); + + if (ret < 0 && ret != -EPIPE) + { + fprintf (stderr, "cp210x: control failed cmd rqt %u " + "rq %u len %u ret %d\n", requestype, request, size, ret); + } + return ret; +} + + + +static int +cp210x_get_config (CP210X * c, uint8_t request, unsigned int *data, int size) +{ + uint32_t *buf; + int result, i, length; + + /* Number of integers required to contain the array */ + length = (((size - 1) | 3) + 1) / 4; + + buf = calloc (length, sizeof (uint32_t)); + if (!buf) + { + fprintf (stderr, "%s - out of memory.\n", __FUNCTION__); + return -ENOMEM; + } + + /* Issue the request, attempting to read 'size' bytes */ + result = libusb_control_transfer (c->handle, + REQTYPE_INTERFACE_TO_HOST, request, + 0x0000, c->interface, (uint8_t *) buf, + size, TIMEOUT); + + /* Convert data into an array of integers */ + for (i = 0; i < length; i++) + data[i] = le32_to_cpu (buf[i]); + + free (buf); + + if (result != size) + { + fprintf (stderr, + "%s - Unable to send config request, request=0x%x size=%d result=%d\n", + __FUNCTION__, request, size, result); + if (result > 0) + result = -EPROTO; + + return result; + } + + return 0; +} + + +int +cp210x_set_config (CP210X * c, uint8_t request, unsigned int *data, int size) +{ + uint32_t *buf; + int result, i, length; + + /* Number of integers required to contain the array */ + length = (((size - 1) | 3) + 1) / 4; + + buf = malloc (length * sizeof (uint32_t)); + if (!buf) + { + fprintf (stderr, "%s - out of memory.\n", __FUNCTION__); + return -ENOMEM; + } + + /* Array of integers into bytes */ + for (i = 0; i < length; i++) + buf[i] = cpu_to_le32 (data[i]); + + if (size > 2) + { + result = libusb_control_transfer (c->handle, + REQTYPE_HOST_TO_INTERFACE, request, + 0x0000, c->interface, (uint8_t *) buf, + size, TIMEOUT); + } + else + { + result = libusb_control_transfer (c->handle, + REQTYPE_HOST_TO_INTERFACE, request, + data[0], c->interface, NULL, 0, + TIMEOUT); + } + + free (buf); + + if ((size > 2 && result != size) || result < 0) + { + fprintf (stderr, + "%s - Unable to send request, request=0x%x size=%d result=%d\n", + __FUNCTION__, request, size, result); + if (result > 0) + result = -EPROTO; + + return result; + } + + return 0; +} + + +static int +cp210x_set_config_single (CP210X * c, uint8_t request, unsigned int data) +{ + return cp210x_set_config (c, request, &data, 2); +} + +static int +cp210x_set_u16 (CP210X * c, int cmd, unsigned int value) +{ + return cp210x_ctlmsg (c, 0xff, 0x40, 0x3700 | (cmd & 0xff), value, 0, 0); +} + +static int +cp210x_set_string (CP210X * c, int cmd, uint8_t * usbstr, size_t len, + size_t maxlen) +{ + + int ret; + + if (len > maxlen) + return -EMSGSIZE; + + + ret = cp210x_ctlmsg (c, 0xff, 0x40, 0x3700 | (cmd & 0xff), 0, usbstr, len); + + fprintf (stderr, "%s - cmd 0x%02x len %d ret %d %x %x %x %x %x %x\n", + __FUNCTION__, cmd, (int) len, ret, usbstr[0], usbstr[1], usbstr[2], + usbstr[3], usbstr[4], usbstr[5]); + + return ret; +} + + +int +cp210x_set_gpio (CP210X * c, uint8_t gpio) +{ + return cp210x_ctlmsg (c, 0xff, REQTYPE_HOST_TO_DEVICE, 0x37e1, + ((uint16_t) gpio << 8) | CP_GPIO_MASK, NULL, 0); +} + + +int +cp210x_set_portconf (CP210X * c, struct cp210x_port_config *config) +{ + int ret; + struct cp210x_port_config be_config; + + memset (&be_config, 0, sizeof (be_config)); + + be_config.reset.mode = cpu_to_be16 (config->reset.mode); + be_config.reset.low_power = 0; // cpu_to_be16(config->reset.low_power); + be_config.reset.latch = cpu_to_be16 (config->reset.latch); + + be_config.suspend.mode = cpu_to_be16 (config->suspend.mode); + be_config.suspend.low_power = 0; // cpu_to_be16(config->suspend.low_power); + be_config.suspend.latch = cpu_to_be16 (config->suspend.latch); + + be_config.enhanced_fxn = config->enhanced_fxn; + + ret = + cp210x_ctlmsg (c, 0xff, REQTYPE_HOST_TO_DEVICE, 0x370c, 0, &be_config, + sizeof (be_config)); + if (ret == sizeof (be_config)) + return 0; + else if (ret >= 0) + return -EPROTO; + else + return ret; +} + + +int +cp210x_get_portconf (CP210X * c, struct cp210x_port_config *config) +{ + int ret; + + ret = + cp210x_ctlmsg (c, 0xff, REQTYPE_DEVICE_TO_HOST, 0x370c, 0, config, + sizeof (*config)); + + if (ret != sizeof (*config)) + return (ret >= 0) ? -EPROTO : ret; + + /* Words from cp2103 are MSB */ + + config->reset.mode = be16_to_cpu (config->reset.mode); + config->reset.low_power = be16_to_cpu (config->reset.low_power); + config->reset.latch = be16_to_cpu (config->reset.latch); + + config->suspend.mode = be16_to_cpu (config->suspend.mode); + config->suspend.low_power = be16_to_cpu (config->suspend.low_power); + config->suspend.latch = be16_to_cpu (config->suspend.latch); + + /* apparently not implemented yet */ + config->reset.low_power = 0; + config->suspend.low_power = 0; + + return 0; +} + + + +int +cp210x_set_dtr (CP210X * c, int on) +{ + unsigned int control; + control = on ? CONTROL_DTR : 0; + control |= CONTROL_WRITE_DTR; + + return cp210x_set_config (c, CP210X_SET_MHS, &control, 2); +} + +int +cp210x_set_vid (CP210X * c, uint16_t vid) +{ + return cp210x_set_u16 (c, 1, vid); +} + +int +cp210x_set_pid (CP210X * c, uint16_t pid) +{ + return cp210x_set_u16 (c, 2, pid); +} + + +int +cp210x_set_dev_ver (CP210X * c, uint16_t ver) +{ + return cp210x_set_u16 (c, 7, ver); +} + + +ssize_t +cp210x_read (CP210X * c, void *buf, size_t len, unsigned int timeout) +{ + int red = 0; + int err; + + timeout /= 1000; + + err = libusb_bulk_transfer (c->handle, 0x81, buf, len, &red, timeout); + + switch (err) + { + case 0: + break; + case LIBUSB_ERROR_TIMEOUT: + return 0; + default: + return -1; + } + + return red; +} + + + +int +cp210x_set_mfg (CP210X * c, uint8_t *s,int len) +{ + return cp210x_set_string (c, 0x0, s, len, + CP210X_MAX_MFG_STRLEN); +} + + +int +cp210x_set_product (CP210X * c, uint8_t *s,int len) +{ + + return cp210x_set_string (c, 0x3, s, len, + CP210X_MAX_PRODUCT_STRLEN); +} + + +int +cp210x_set_serial (CP210X * c, uint8_t *s,int len) +{ + + return cp210x_set_string (c, 0x4, s, len, + CP210X_MAX_SERIAL_STRLEN); +} + + + + +int +cp210x_setup (CP210X * c) +{ + + unsigned int bits; + unsigned int modem_ctl[4]; + uint32_t baud; + + + if (cp210x_set_config_single (c, CP210X_IFC_ENABLE, UART_ENABLE)) + { + + fprintf (stderr, "failed to start uart\n"); + return -1; + } + + + baud = 115200; + + if (cp210x_set_config (c, CP210X_SET_BAUDRATE, &baud, sizeof (baud))) + { + fprintf (stderr, "failed to set baud rate to %u\n", baud); + return -1; + } + + + + cp210x_get_config (c, CP210X_GET_LINE_CTL, &bits, 2); + + bits &= ~BITS_DATA_MASK; + bits |= BITS_DATA_8; + if (cp210x_set_config (c, CP210X_SET_LINE_CTL, &bits, 2)) + { + fprintf (stderr, + "Number of data bits requested not supported by device\n"); + return -1; + } + + + cp210x_get_config (c, CP210X_GET_LINE_CTL, &bits, 2); + bits &= ~BITS_PARITY_MASK; + bits |= BITS_PARITY_SPACE; + if (cp210x_set_config (c, CP210X_SET_LINE_CTL, &bits, 2)) + { + fprintf (stderr, "Parity mode not supported by device\n"); + return -1; + } + + cp210x_get_config (c, CP210X_GET_LINE_CTL, &bits, 2); + bits &= ~BITS_STOP_MASK; + bits |= BITS_STOP_1; + if (cp210x_set_config (c, CP210X_SET_LINE_CTL, &bits, 2)) + { + fprintf (stderr, + "Number of stop bits requested not supported by device\n"); + return -1; + } + + + cp210x_get_config (c, CP210X_GET_FLOW, modem_ctl, 16); + modem_ctl[0] &= ~0x7B; + modem_ctl[0] |= 0x01; + modem_ctl[1] |= 0x40; + + if (cp210x_set_config (c, CP210X_SET_FLOW, modem_ctl, 16)) + { + fprintf (stderr, "Couldn't disable modem control\n"); + return -1; + } + + + return 0; +} + + + +static libusb_device * +find_device (void) +{ + libusb_device **list; + libusb_device *found = NULL; + ssize_t cnt; + ssize_t i = 0; + + libusb_init (NULL); + + cnt = libusb_get_device_list (NULL, &list); + + if (cnt < 0) + return NULL; + + + for (i = 0; i < cnt; i++) + { + struct libusb_device_descriptor desc = { 0 }; + libusb_device *device = list[i]; + + libusb_get_device_descriptor (device, &desc); + + + if (((desc.idVendor == 0x10c4) && (desc.idProduct == 0xea60)) + ||((desc.idVendor == 0x413c) && (desc.idProduct == 0x9500))) + { + libusb_ref_device (device); + libusb_free_device_list (list, 1); + + return device; + break; + } + } + libusb_free_device_list (list, 1); + return NULL; +} + + + +int +cp210x_reset (CP210X * c) +{ + libusb_reset_device (c->handle); + + if (libusb_set_configuration (c->handle, 1) < 0 || + libusb_claim_interface (c->handle, 0) < 0) + return -1; + + + if (cp210x_setup (c)) + return -1; + + + return 0; +} + +CP210X * +cp210x_open (void) +{ + CP210X *c = malloc (sizeof (CP210X)); + + c->device = find_device (); + c->handle = NULL; + + if (!c->device) + { + free (c); + return NULL; + } + libusb_open (c->device, &c->handle); + + if (!c->handle) + { + free (c); + return NULL; + } + + libusb_detach_kernel_driver (c->handle, 0); + libusb_detach_kernel_driver (c->handle, 1); + + libusb_reset_device (c->handle); + + c->interface = 0; + if (libusb_set_configuration (c->handle, 1) < 0 || + libusb_claim_interface (c->handle, 0) < 0) + { + + libusb_close (c->handle); + libusb_unref_device (c->device); + free (c); + return NULL; + + + } + + + if (cp210x_setup (c)) + { + libusb_close (c->handle); + libusb_unref_device (c->device); + free (c); + return NULL; + } + + return c; +} + +void +cp210x_close (CP210X * c) +{ + libusb_close (c->handle); + libusb_unref_device (c->device); + free (c); +} diff --git a/userland/cp210x.h b/userland/cp210x.h new file mode 100644 index 0000000..4b9beff --- /dev/null +++ b/userland/cp210x.h @@ -0,0 +1,97 @@ +#ifndef _CP210X_H_ +#define _CP210X_H_ + +#include +typedef struct { + libusb_device *device; + libusb_device_handle *handle; + int interface; +} CP210X; + +struct cp210x_port_state { + uint16_t mode; /* push-pull = 1, open-drain = 0 */ + uint16_t low_power; /* 1 = ground the pin, and disable */ + uint16_t latch; +} __attribute__((packed)); + +/* LOPWR MODE LATCH */ +/* 0 0 0 Pin drivers are totem pole, and inital config sinks current to GND */ +/* 0 0 1 Pin drivers are totem pole, and inital config sources current from VIO */ +/* 0 1 0 Pin drivers are open drain, and inital config sinks current to GND */ +/* 0 1 1 Pin drivers are open drain, and inital config leaves pin HI-Z (you want this for inputs) */ +/* 1 X X Pin drivers are disabled, and pin sinks current to GND */ + +struct cp210x_port_config { + struct cp210x_port_state reset; + struct cp210x_port_state suspend; + uint8_t enhanced_fxn; +} __attribute__((packed)); + + +#define CP_EFXN_GPIO_0_IS_TXLED (1 << 0) +#define CP_EFXN_GPIO_1_IS_RXLED (1 << 1) +#define CP_EFXN_GPIO_2_IS_RS485_TX (1 << 2) +#define CP_EFXN_UNUSED_1 (1 << 3) /* Set to zero */ +#define CP_EFXN_ENABLE_WPU (1 << 4) +#define CP_EFXN_UNUSED_2 (1 << 5) /* Set to zero */ +#define CP_EFXN_SERIAL_AUTOOFF (1 << 6) +#define CP_EFXN_GPIOL_AUTOOFF (1 << 7) + + +#define CP_PIN_RI (1 << 0) +#define CP_PIN_DCD (1 << 1) +#define CP_PIN_DTR (1 << 2) +#define CP_PIN_DSR (1 << 3) +#define CP_PIN_TXD (1 << 4) +#define CP_PIN_RXD (1 << 5) +#define CP_PIN_RTS (1 << 6) +#define CP_PIN_CTS (1 << 7) +#define CP_PIN_GPIO_0 (1 << 8) +#define CP_PIN_GPIO_1 (1 << 9) +#define CP_PIN_GPIO_2 (1 << 10) +#define CP_PIN_GPIO_3 (1 << 11) +#define CP_PIN_UNUSED_1 (1 << 12) +#define CP_PIN_UNUSED_2 (1 << 13) +#define CP_PIN_GPIO_SUSPEND (1 << 14) +#define CP_PIN_GPIO_NSUSPEND (1 << 15) + +#define CP_INPUT_PINS (CP_PIN_RXD|CP_PIN_CTS|CP_PIN_DSR|CP_PIN_RI|CP_PIN_DCD) + + + + + + +/* CP2103 GPIO */ +#define CP_GPIO_0 0x01 +#define CP_GPIO_1 0x02 +#define CP_GPIO_2 0x04 +#define CP_GPIO_3 0x08 +#define CP_GPIO_MASK (CP_GPIO_0|CP_GPIO_1|CP_GPIO_2|CP_GPIO_3) + + +#define CP210X_MAX_PRODUCT_STRLEN 0x7d +#define CP210X_MAX_SERIAL_STRLEN 0x3f +#define CP210X_MAX_MFG_STRLEN 0x18 +#define CP210X_MAX_MAXPOWER 250 + + + + +extern int cp210x_set_config(CP210X *c, uint8_t request, unsigned int *data, int size); +extern int cp210x_set_gpio(CP210X *c, uint8_t gpio); +extern int cp210x_set_portconf(CP210X *c, struct cp210x_port_config *config); +extern int cp210x_get_portconf(CP210X *c, struct cp210x_port_config *config); +extern int cp210x_set_dtr(CP210X *c, int on); +extern int cp210x_set_vid(CP210X *c, uint16_t vid); +extern int cp210x_set_pid(CP210X *c, uint16_t pid); +extern int cp210x_set_dev_ver(CP210X *c, uint16_t ver); +extern ssize_t cp210x_read(CP210X *c, void *buf, size_t len, unsigned int timeout); +extern int cp210x_set_mfg(CP210X *c, uint8_t *s, int len); +extern int cp210x_set_product(CP210X *c, uint8_t *s, int len); +extern int cp210x_set_serial(CP210X *c, uint8_t *s, int len); +extern int cp210x_setup(CP210X *c); +extern int cp210x_reset(CP210X *c); +extern CP210X *cp210x_open(void); +extern void cp210x_close(CP210X *c); +#endif diff --git a/userland/cp210x_int.h b/userland/cp210x_int.h new file mode 100644 index 0000000..5a5ee3a --- /dev/null +++ b/userland/cp210x_int.h @@ -0,0 +1,82 @@ +#ifndef _CPIO210X_H +#define _CPIO210X_H + +/* Config request types */ +#define REQTYPE_HOST_TO_INTERFACE 0x41 +#define REQTYPE_INTERFACE_TO_HOST 0xc1 +#define REQTYPE_HOST_TO_DEVICE 0x40 +#define REQTYPE_DEVICE_TO_HOST 0xc0 + +/* Config request codes */ +#define CP210X_IFC_ENABLE 0x00 +#define CP210X_SET_BAUDDIV 0x01 +#define CP210X_GET_BAUDDIV 0x02 +#define CP210X_SET_LINE_CTL 0x03 +#define CP210X_GET_LINE_CTL 0x04 +#define CP210X_SET_BREAK 0x05 +#define CP210X_IMM_CHAR 0x06 +#define CP210X_SET_MHS 0x07 +#define CP210X_GET_MDMSTS 0x08 +#define CP210X_SET_XON 0x09 +#define CP210X_SET_XOFF 0x0A +#define CP210X_SET_EVENTMASK 0x0B +#define CP210X_GET_EVENTMASK 0x0C +#define CP210X_SET_CHAR 0x0D +#define CP210X_GET_CHARS 0x0E +#define CP210X_GET_PROPS 0x0F +#define CP210X_GET_COMM_STATUS 0x10 +#define CP210X_RESET 0x11 +#define CP210X_PURGE 0x12 +#define CP210X_SET_FLOW 0x13 +#define CP210X_GET_FLOW 0x14 +#define CP210X_EMBED_EVENTS 0x15 +#define CP210X_GET_EVENTSTATE 0x16 +#define CP210X_SET_CHARS 0x19 +#define CP210X_GET_BAUDRATE 0x1D +#define CP210X_SET_BAUDRATE 0x1E + +/* CP210X_IFC_ENABLE */ +#define UART_ENABLE 0x0001 +#define UART_DISABLE 0x0000 + +/* CP210X_(SET|GET)_BAUDDIV */ +#define BAUD_RATE_GEN_FREQ 0x384000 + +/* CP210X_(SET|GET)_LINE_CTL */ +#define BITS_DATA_MASK 0X0f00 +#define BITS_DATA_5 0X0500 +#define BITS_DATA_6 0X0600 +#define BITS_DATA_7 0X0700 +#define BITS_DATA_8 0X0800 +#define BITS_DATA_9 0X0900 + +#define BITS_PARITY_MASK 0x00f0 +#define BITS_PARITY_NONE 0x0000 +#define BITS_PARITY_ODD 0x0010 +#define BITS_PARITY_EVEN 0x0020 +#define BITS_PARITY_MARK 0x0030 +#define BITS_PARITY_SPACE 0x0040 + +#define BITS_STOP_MASK 0x000f +#define BITS_STOP_1 0x0000 +#define BITS_STOP_1_5 0x0001 +#define BITS_STOP_2 0x0002 + +/* CP210X_SET_BREAK */ +#define BREAK_ON 0x0001 +#define BREAK_OFF 0x0000 + +/* CP210X_(SET_MHS|GET_MDMSTS) */ +#define CONTROL_DTR 0x0001 +#define CONTROL_RTS 0x0002 +#define CONTROL_CTS 0x0010 +#define CONTROL_DSR 0x0020 +#define CONTROL_RING 0x0040 +#define CONTROL_DCD 0x0080 +#define CONTROL_WRITE_DTR 0x0100 +#define CONTROL_WRITE_RTS 0x0200 + + +#endif /* _CPIO210X_H */ + + diff --git a/userland/libradiator.c b/userland/libradiator.c new file mode 100644 index 0000000..15d2e21 --- /dev/null +++ b/userland/libradiator.c @@ -0,0 +1,364 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "radiator.h" + + + + +/* + * bit 0: 1 left leg NPN base + * bit 1: 2 left leg PNP base + * bit 2: 4 right leg PNP base + * bit 3: 8 right leg NPN base + */ + +#define LEFT_NPN_OFF 0 +#define LEFT_NPN_ON 1 +#define LEFT_PNP_OFF 2 +#define LEFT_PNP_ON 0 + +#define LEFT_LOW (LEFT_NPN_ON | LEFT_PNP_OFF) +#define LEFT_HI (LEFT_NPN_OFF | LEFT_PNP_ON) +#define LEFT_OFF (LEFT_NPN_OFF | LEFT_PNP_OFF) + +#define RIGHT_NPN_OFF 0 +#define RIGHT_NPN_ON 8 +#define RIGHT_PNP_OFF 4 +#define RIGHT_PNP_ON 0 + + +#define RIGHT_LOW (RIGHT_NPN_ON | RIGHT_PNP_OFF) +#define RIGHT_HI (RIGHT_NPN_OFF | RIGHT_PNP_ON) +#define RIGHT_OFF (RIGHT_NPN_OFF | RIGHT_PNP_OFF) + +#define BRAKE (RIGHT_LOW | LEFT_LOW) +#define FORWARDS (RIGHT_LOW | LEFT_HI) +#define BACKWARDS (RIGHT_HI | LEFT_LOW) +#define OFF (RIGHT_OFF| LEFT_OFF) + +#define TIMEOUT 750000 /*75ms */ +#define RUN_IN 50 +#define RELEASE_TENSION 10 + + +static int brake_motor(Radiator *r) +{ + int i=BRAKE; + printf (" Motor braking\n"); + return cp210x_set_gpio(r->cp210x,i); +} + +static int +drive_motor (Radiator*r, int dir) +{ + int i; + + if (dir > 0) + { + printf (" Motor forwards\n"); + i = FORWARDS; + } + else if (dir < 0) + { + printf (" Motor backwards\n"); + i = BACKWARDS; + } + else + { + printf (" Motor off\n"); + i = OFF; + } + + return cp210x_set_gpio(r->cp210x,i); +} + + +static int +sensor_led_power (Radiator *r, int on) +{ + +unsigned int control; + +printf (" Rotation sensor power %s\n", on ? "on" : "off"); + +return cp210x_set_dtr(r->cp210x,on); + +} + + +static int count_steps (Radiator *r, int count) +{ + int counted = 0; + struct timeval tv; + char buf[128]; + int i; + fd_set rfds; + + printf (" Counting %d steps", count); + fflush(stdout); + + + while (count) + { + i=cp210x_read(r->cp210x,buf,sizeof(buf),TIMEOUT); + + if (i==0) break; + + if (i<0) continue; + + counted+=i; + if (count>0) { + count-=i; + if (count<0) count=0; + } + + printf ("."); + fflush (stdout); + } + + printf ("%d steps\n", counted); + return counted; +} + + + + +static int +drive_motor_count (Radiator * r, int dir, int steps) +{ + int s1, s2, ret; + + sensor_led_power (r, 1); + drive_motor (r, dir); + s1 = count_steps (r, steps); + if (s1 != steps) { + printf (" Hit end stop\n"); + drive_motor (r, 0); + } else { + brake_motor(r); + } + s2 = count_steps (r, -1); + sensor_led_power (r, 0); + drive_motor (r, 0); + + /*did we hit an end stop? */ + if (s1 != steps) + ret = s1 - s2; + else + ret = s1 + s2; + + printf (" %d steps under power (of %d), %d steps coasting -> %d steps\n", + s1, steps, s2, ret); + + r->pos += ret * dir; + + /*Recalibrate our notion of ends */ + + if (s1 != steps) + { + if (dir == -1) + { + r->pos = 0; + } + else + { + r->max = r->pos; + } + } + + return ret; +} + + + + +int +radiator_set_pos (Radiator * r, int wanted) +{ + int guard, delta; + + delta = wanted - r->pos; + printf ("Now at %d want %d, delta %d\n", r->pos, wanted, delta); + + if (wanted == 0) + { + drive_motor_count (r, -1, -1); + return 0; + } + else if (r->max && (wanted >= r->max)) + { + drive_motor_count (r, 1, -1); + return 0; + } + + + guard = r->overshoot * 3; + + if ((delta > -guard) && (delta < guard)) + { + printf ("Too close\n"); + + drive_motor_count (r, (r->pos > r->half) ? -1 : 1, RUN_IN); + + delta = wanted - r->pos; + printf ("Now at %d want %d, delta %d\n", r->pos, wanted, delta); + } + + + if (delta < 0) + { + drive_motor_count (r, -1, (-delta) - r->overshoot); + } + else if (delta > 0) + { + drive_motor_count (r, 1, delta - r->overshoot); + } + + printf ("Wanted %d, got %d\n", wanted, r->pos); + + return 0; +} + +void +radiator_calibrate (Radiator * r) +{ + radiator_set_pos (r, 0); + + drive_motor_count (r, 1, RUN_IN); + + r->overshoot = r->pos - RUN_IN; +} + + + +static int utf16(uint8_t *dst,uint8_t *src) +{ +int len=0; + + +while (*src) { + *(dst++)=*(src++); + len+=2; + *(dst++)=0; +} +len+=2; + +return len; +} + + +static int make_usb_descriptor(uint8_t *dst,char *src) +{ +int len; +len=utf16(dst+2,(uint8_t *) src); +//memcpy(dst+2,src,len); +dst[0]=len+2; +dst[1]=0x3; +return len+2; +} + + +int +radiator_program (Radiator * r) +{ + uint8_t buf[256]; + struct cp210x_port_config config; + memset (&config, 0, sizeof (config)); + uint16_t vid,pid; + int len; + + + cp210x_get_portconf(r->cp210x,&config); + + printf + ("config was reset=(%x,%x,%x), suspend=(%x,%x,%x), enhanced_fxn=%x\n", + config.reset.mode, config.reset.low_power, config.reset.latch, + config.suspend.mode, config.suspend.low_power, config.suspend.latch, + config.enhanced_fxn); + + + memset (&config, 0, sizeof (config)); + + config.reset.mode = ~CP_INPUT_PINS; + config.reset.low_power = 0; + config.reset.latch = CP_INPUT_PINS | CP_PIN_GPIO_1 | CP_PIN_GPIO_2; + config.suspend.mode = ~CP_INPUT_PINS; + config.suspend.low_power = 0; + config.suspend.latch = CP_INPUT_PINS | CP_PIN_GPIO_1 | CP_PIN_GPIO_2; + config.enhanced_fxn = CP_EFXN_ENABLE_WPU; + + + cp210x_set_portconf(r->cp210x,&config); + + memset (&config, 0, sizeof (config)); + cp210x_get_portconf(r->cp210x,&config); + + + printf + ("config is reset=(%x,%x,%x), suspend=(%x,%x,%x), enhanced_fxn=%x\n", + config.reset.mode, config.reset.low_power, config.reset.latch, + config.suspend.mode, config.suspend.low_power, config.suspend.latch, + config.enhanced_fxn); + + memset(buf,0,sizeof(buf)); + +vid=0x413c; + + cp210x_set_vid(r->cp210x,vid); + +pid=0x9500; + cp210x_set_pid(r->cp210x,pid); + + len=make_usb_descriptor(buf,"USB Radiator Valve"); + cp210x_set_product(r->cp210x, buf,len); + + len=make_usb_descriptor(buf,"000001"); + cp210x_set_serial (r->cp210x,buf,len); + + len=make_usb_descriptor(buf,"Global Panaceas"); + cp210x_set_mfg (r->cp210x,buf,len); + + cp210x_reset(r->cp210x); + + return 0; +} + +Radiator * +radiator_open (char *path, int quiet) +{ + Radiator *r = malloc (sizeof (Radiator)); + + + r->cp210x=cp210x_open(); + + if (!r->cp210x) { + free(r); + return NULL; + } + + + if (!quiet) + radiator_calibrate (r); + + return r; +} + + +void +radiator_close (Radiator * r) +{ + cp210x_close(r->cp210x); + free (r); +} + + + diff --git a/userland/program_radiator.c b/userland/program_radiator.c new file mode 100644 index 0000000..75077da --- /dev/null +++ b/userland/program_radiator.c @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include "radiator.h" + +int +main (int argc, char *argv[]) +{ + Radiator *r; + int pos; + char buf[1024]; + + + r = radiator_open (argv[1],1); + + if (!r) + return -1; + + radiator_program(r); + + return 0; +} diff --git a/userland/radiator.c b/userland/radiator.c new file mode 100644 index 0000000..2d594da --- /dev/null +++ b/userland/radiator.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include +#include "radiator.h" + +int +main (int argc, char *argv[]) +{ + Radiator *r; + int pos; + char buf[1024]; + + + r = radiator_open (argv[1],0); + + if (!r) + return -1; + + + for (;;) + { + printf ("Current position %d (end stop at %d, overshoots by %d)\n", + r->pos, r->max, r->overshoot); + printf ("Enter new>"); + fflush (stdout); + buf[sizeof (buf) - 1] = 0; + fgets (buf, sizeof (buf) - 1, stdin); + radiator_set_pos (r, atoi (buf)); + } + + + return 0; +} diff --git a/userland/radiator.h b/userland/radiator.h new file mode 100644 index 0000000..da912c0 --- /dev/null +++ b/userland/radiator.h @@ -0,0 +1,21 @@ +#ifndef _RADIATOR_H_ +#define _RADIATOR_H_ + +#include "cp210x.h" + +typedef struct +{ + CP210X *cp210x; + int pos; + int half; + int overshoot; + int max; +} Radiator; + +int radiator_set_pos (Radiator * r, int wanted); +void radiator_calibrate (Radiator * r); +int radiator_program (Radiator * r); +Radiator *radiator_open (char *s, int quiet); +void radiator_close (Radiator * r); + +#endif /* _RADIATOR_H_ */ -- cgit v1.2.3