From: knilch Date: Sun, 20 Feb 2011 11:06:13 +0000 (+0100) Subject: import original fnordlichtmini firmware X-Git-Url: https://git.zerfleddert.de/cgi-bin/gitweb.cgi/fnordlicht-mini/commitdiff_plain/ec1bef8e19888e982ffc53129f74d58868e8d203?ds=sidebyside import original fnordlichtmini firmware --- ec1bef8e19888e982ffc53129f74d58868e8d203 diff --git a/firmware/.gitignore b/firmware/.gitignore new file mode 100644 index 0000000..dc9c13b --- /dev/null +++ b/firmware/.gitignore @@ -0,0 +1,11 @@ +.*.swp +.dep +*.o +*.i +*.s +*.hex +*.lst +*.elf +config.mk +tags +*.bin diff --git a/firmware/.gitmodules b/firmware/.gitmodules new file mode 100644 index 0000000..d1c4f7d --- /dev/null +++ b/firmware/.gitmodules @@ -0,0 +1,3 @@ +[submodule "fnordlicht-controller-bootloader"] + path = fnordlicht-controller-bootloader + url = git://github.com/fd0/usbload.git diff --git a/firmware/COPYING b/firmware/COPYING new file mode 100644 index 0000000..4432540 --- /dev/null +++ b/firmware/COPYING @@ -0,0 +1,676 @@ + + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If 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 convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state 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 program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. + diff --git a/firmware/README b/firmware/README new file mode 100644 index 0000000..c794b02 --- /dev/null +++ b/firmware/README @@ -0,0 +1,48 @@ +fnordlicht and fnordlichtmini firmware +====================================== + +This is the firmware sourcecode for different hardware devices, namely: +* fnordlicht (for details see [1]) +* fnordlichtmini (for details see [2]) +* fnordlicht-controller (for details see [3]) + +Directory layout +================ + +This repository consists of several different subdirectories: + +bugs/ files for the ditz[3] issue tracking system +doc/ further documentation and serial protocol description +fnordlicht-firmware/ firmware sourcecode for fnordlicht and fnordlichtmini +fnordlicht-bootloader/ serial bootloader for fnordlicht and fnordlichtmini +fnordlicht-controller/ firmware sourcecode for fnordlicht-controller +fnordlicht-controller-bootloader/ sourcecode for the fnordlicht-controller usb bootloader +common/ common C header files used by all programs +tests/ ruby-scripts for testing the serial + protocol implementation +compiled/ compiled binaries for several processors + + +git submodules +============== + +If the directory 'fnordlicht-controller-bootloader' does not contain any files +(and you cloned the repository with git), you need to initialize the submodule: +$ git submodule init +[...] +$ git submodule update +[...] + +License +======= + +Each file contains a header, which states the author(s) and license for that +file. Most code is licensed as GPL (version 3). The text for this license can +be found in the file COPYING. + +Links +===== + +[1] http://www.lochraster.org/fnordlicht +[2] http://www.lochraster.org/fnordlichtmini +[3] http://ditz.rubyforge.org diff --git a/firmware/bugs/issue-244266014c809654b6e835107426dca90a3d95ad.yaml b/firmware/bugs/issue-244266014c809654b6e835107426dca90a3d95ad.yaml new file mode 100644 index 0000000..4517cc7 --- /dev/null +++ b/firmware/bugs/issue-244266014c809654b6e835107426dca90a3d95ad.yaml @@ -0,0 +1,38 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: startup configuration +desc: |- + implement a startup configuration which is read from eeprom + + see doc/PROTOCOL for details +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-29 15:28:28.707953 Z +references: [] + +id: 244266014c809654b6e835107426dca90a3d95ad +log_events: +- - 2009-11-29 15:28:30.071928 Z + - Alexander Neumann + - created + - "" +- - 2009-11-30 23:30:53.847597 Z + - Alexander Neumann + - changed status from unstarted to in_progress + - "" +- - 2009-11-30 23:46:54.883479 Z + - Alexander Neumann + - changed status from in_progress to paused + - "" +- - 2009-12-01 20:09:50.442780 Z + - Alexander Neumann + - changed status from paused to in_progress + - "" +- - 2009-12-01 22:08:48.560111 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-3ff28fa194caf3f5257950dbc1ba5a822174cf7a.yaml b/firmware/bugs/issue-3ff28fa194caf3f5257950dbc1ba5a822174cf7a.yaml new file mode 100644 index 0000000..490ee90 --- /dev/null +++ b/firmware/bugs/issue-3ff28fa194caf3f5257950dbc1ba5a822174cf7a.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: implement power-saving sleep-command +desc: "" +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-12-12 15:42:07.526416 Z +references: [] + +id: 3ff28fa194caf3f5257950dbc1ba5a822174cf7a +log_events: +- - 2009-12-12 15:42:08.571800 Z + - Alexander Neumann + - created + - "" +- - 2009-12-15 17:16:01.442787 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-4f1c9213bbc86277a65648f86fcf4b56a8a18760.yaml b/firmware/bugs/issue-4f1c9213bbc86277a65648f86fcf4b56a8a18760.yaml new file mode 100644 index 0000000..f118b8a --- /dev/null +++ b/firmware/bugs/issue-4f1c9213bbc86277a65648f86fcf4b56a8a18760.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: make eeprom startup config write non-blocking +desc: "" +type: :task +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-29 18:56:56.900484 Z +references: [] + +id: 4f1c9213bbc86277a65648f86fcf4b56a8a18760 +log_events: +- - 2009-11-29 18:56:58.065338 Z + - Alexander Neumann + - created + - "" +- - 2009-12-06 17:37:45.156896 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-59a83776afe696f64182449daa87e51ceffa1013.yaml b/firmware/bugs/issue-59a83776afe696f64182449daa87e51ceffa1013.yaml new file mode 100644 index 0000000..9cd2da2 --- /dev/null +++ b/firmware/bugs/issue-59a83776afe696f64182449daa87e51ceffa1013.yaml @@ -0,0 +1,19 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: implement command queue +desc: several commands can be queue, eg fade to different colors one after another, without using the eeprom +type: :feature +component: fnordlicht/firmware +release: +reporter: Alexander Neumann +status: :unstarted +disposition: +creation_time: 2009-12-09 14:00:55.390739 Z +references: [] + +id: 59a83776afe696f64182449daa87e51ceffa1013 +log_events: +- - 2009-12-09 14:00:56.122668 Z + - Alexander Neumann + - created + - "" +git_branch: diff --git a/firmware/bugs/issue-61beb8974c8bf4ad603c4fdb8940487ebffde9a6.yaml b/firmware/bugs/issue-61beb8974c8bf4ad603c4fdb8940487ebffde9a6.yaml new file mode 100644 index 0000000..b2b906c --- /dev/null +++ b/firmware/bugs/issue-61beb8974c8bf4ad603c4fdb8940487ebffde9a6.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: add documentation for configuring the brown out detector (fuses.txt) +desc: "" +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-12-04 15:56:25.514881 Z +references: [] + +id: 61beb8974c8bf4ad603c4fdb8940487ebffde9a6 +log_events: +- - 2009-12-04 15:56:26.538767 Z + - Alexander Neumann + - created + - "" +- - 2009-12-12 19:38:50.260839 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-7012d3ed34a62b0e2e3bb264dd9d03ee448a1d14.yaml b/firmware/bugs/issue-7012d3ed34a62b0e2e3bb264dd9d03ee448a1d14.yaml new file mode 100644 index 0000000..09bb290 --- /dev/null +++ b/firmware/bugs/issue-7012d3ed34a62b0e2e3bb264dd9d03ee448a1d14.yaml @@ -0,0 +1,33 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: pwm on external pins +desc: |- + output the pwm signal on the three different external pins + (for fnordlichtmini-hardware on PD5-PD7) +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-29 15:42:12.589133 Z +references: [] + +id: 7012d3ed34a62b0e2e3bb264dd9d03ee448a1d14 +log_events: +- - 2009-11-29 15:42:13.950222 Z + - Alexander Neumann + - created + - "" +- - 2009-11-29 15:42:27.355696 Z + - Alexander Neumann + - assigned to release 0.3 from unassigned + - "" +- - 2009-12-01 22:21:34.771341 Z + - Alexander Neumann + - changed status from unstarted to in_progress + - "" +- - 2009-12-01 22:30:42.109647 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-8b9fb39d7d3c1b3e43a7a3f6f25544216bcfa774.yaml b/firmware/bugs/issue-8b9fb39d7d3c1b3e43a7a3f6f25544216bcfa774.yaml new file mode 100644 index 0000000..b30ee83 --- /dev/null +++ b/firmware/bugs/issue-8b9fb39d7d3c1b3e43a7a3f6f25544216bcfa774.yaml @@ -0,0 +1,26 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: color storage to eeprom +desc: |- + implement the eeprom color storage and fade methods. + + see doc/PROTOCOL for details +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-29 15:29:06.376182 Z +references: [] + +id: 8b9fb39d7d3c1b3e43a7a3f6f25544216bcfa774 +log_events: +- - 2009-11-29 15:29:07.420075 Z + - Alexander Neumann + - created + - "" +- - 2009-11-30 23:25:25.206248 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-8e74763383d0294015d6d2020b581fac85d730f2.yaml b/firmware/bugs/issue-8e74763383d0294015d6d2020b581fac85d730f2.yaml new file mode 100644 index 0000000..26733b7 --- /dev/null +++ b/firmware/bugs/issue-8e74763383d0294015d6d2020b581fac85d730f2.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: "implement startup config: fade to hsv color" +desc: "" +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :wontfix +creation_time: 2009-11-29 17:34:30.185144 Z +references: [] + +id: 8e74763383d0294015d6d2020b581fac85d730f2 +log_events: +- - 2009-11-29 17:34:31.068870 Z + - Alexander Neumann + - created + - "" +- - 2009-12-06 18:13:42.208841 Z + - Alexander Neumann + - closed with disposition wontfix + - "" +git_branch: diff --git a/firmware/bugs/issue-a4b86aa484d57e757b3fa546ea148304f97a0b3f.yaml b/firmware/bugs/issue-a4b86aa484d57e757b3fa546ea148304f97a0b3f.yaml new file mode 100644 index 0000000..508e26f --- /dev/null +++ b/firmware/bugs/issue-a4b86aa484d57e757b3fa546ea148304f97a0b3f.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: implement remote command PULL_INT +desc: see doc/PROTOCOL for details +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-29 15:43:24.781273 Z +references: [] + +id: a4b86aa484d57e757b3fa546ea148304f97a0b3f +log_events: +- - 2009-11-29 15:43:26.058394 Z + - Alexander Neumann + - created + - "" +- - 2009-12-06 16:27:04.471650 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-beee9c2c38a457348812b326a658d1581daee105.yaml b/firmware/bugs/issue-beee9c2c38a457348812b326a658d1581daee105.yaml new file mode 100644 index 0000000..96b0246 --- /dev/null +++ b/firmware/bugs/issue-beee9c2c38a457348812b326a658d1581daee105.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: reorder command opcodes +desc: "" +type: :bugfix +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-12-10 09:48:01.894996 Z +references: [] + +id: beee9c2c38a457348812b326a658d1581daee105 +log_events: +- - 2009-12-10 09:48:02.623046 Z + - Alexander Neumann + - created + - "" +- - 2009-12-10 23:30:24.609411 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-c98e5fca4fe34df3b03cdad259fdce136f60fc68.yaml b/firmware/bugs/issue-c98e5fca4fe34df3b03cdad259fdce136f60fc68.yaml new file mode 100644 index 0000000..267a1eb --- /dev/null +++ b/firmware/bugs/issue-c98e5fca4fe34df3b03cdad259fdce136f60fc68.yaml @@ -0,0 +1,19 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: pwm smoothing +desc: implement fix-comma addition in pwm module, so that fading is done more smoothly +type: :feature +component: fnordlicht/firmware +release: +reporter: Alexander Neumann +status: :unstarted +disposition: +creation_time: 2009-11-29 15:31:52.504764 Z +references: [] + +id: c98e5fca4fe34df3b03cdad259fdce136f60fc68 +log_events: +- - 2009-11-29 15:31:53.916447 Z + - " <>" + - created + - "" +git_branch: diff --git a/firmware/bugs/issue-c9fff2901ae2f8dc90c984836aa18384cba65f9b.yaml b/firmware/bugs/issue-c9fff2901ae2f8dc90c984836aa18384cba65f9b.yaml new file mode 100644 index 0000000..7ab1ef2 --- /dev/null +++ b/firmware/bugs/issue-c9fff2901ae2f8dc90c984836aa18384cba65f9b.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: implement bootloader +desc: implement a bootloader which speaks the protocol defined at the end of doc/PROTOCOL +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-29 15:46:48.049565 Z +references: [] + +id: c9fff2901ae2f8dc90c984836aa18384cba65f9b +log_events: +- - 2009-11-29 15:46:48.878575 Z + - Alexander Neumann + - created + - "" +- - 2009-12-10 23:02:51.890685 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-da9279e2b239b6a471288d40d6d5c89782eb6a7f.yaml b/firmware/bugs/issue-da9279e2b239b6a471288d40d6d5c89782eb6a7f.yaml new file mode 100644 index 0000000..0a21de1 --- /dev/null +++ b/firmware/bugs/issue-da9279e2b239b6a471288d40d6d5c89782eb6a7f.yaml @@ -0,0 +1,27 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: implement 'master' jumper +desc: check state of master solder jumper, start nice programs on attached devices +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-12-06 14:17:53.988035 Z +references: [] + +id: da9279e2b239b6a471288d40d6d5c89782eb6a7f +log_events: +- - 2009-12-06 14:17:54.640372 Z + - Alexander Neumann + - created + - "" +- - 2009-12-14 20:18:49.622114 Z + - Alexander Neumann + - changed status from unstarted to in_progress + - "" +- - 2009-12-15 01:06:08.146017 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-ea40110c70faf3411d7acd59723143edbd4cfe59.yaml b/firmware/bugs/issue-ea40110c70faf3411d7acd59723143edbd4cfe59.yaml new file mode 100644 index 0000000..74c7306 --- /dev/null +++ b/firmware/bugs/issue-ea40110c70faf3411d7acd59723143edbd4cfe59.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: document static programs in doc/PROTOCOL +desc: "" +type: :task +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :fixed +creation_time: 2009-11-30 23:04:35.729104 Z +references: [] + +id: ea40110c70faf3411d7acd59723143edbd4cfe59 +log_events: +- - 2009-11-30 23:04:36.585003 Z + - Alexander Neumann + - created + - "" +- - 2009-12-02 20:29:34.644532 Z + - Alexander Neumann + - closed with disposition fixed + - "" +git_branch: diff --git a/firmware/bugs/issue-f4d5135d6501687d16dbfbcaf0b9fe9bd0b476c2.yaml b/firmware/bugs/issue-f4d5135d6501687d16dbfbcaf0b9fe9bd0b476c2.yaml new file mode 100644 index 0000000..4edb943 --- /dev/null +++ b/firmware/bugs/issue-f4d5135d6501687d16dbfbcaf0b9fe9bd0b476c2.yaml @@ -0,0 +1,23 @@ +--- !ditz.rubyforge.org,2008-03-06/issue +title: add code to configure and use the watchdog +desc: "" +type: :feature +component: fnordlicht/firmware +release: "0.3" +reporter: Alexander Neumann +status: :closed +disposition: :wontfix +creation_time: 2009-12-04 15:55:46.690700 Z +references: [] + +id: f4d5135d6501687d16dbfbcaf0b9fe9bd0b476c2 +log_events: +- - 2009-12-04 15:55:47.554594 Z + - Alexander Neumann + - created + - "" +- - 2009-12-12 19:56:26.417875 Z + - Alexander Neumann + - closed with disposition wontfix + - "" +git_branch: diff --git a/firmware/bugs/project.yaml b/firmware/bugs/project.yaml new file mode 100644 index 0000000..183f3c7 --- /dev/null +++ b/firmware/bugs/project.yaml @@ -0,0 +1,26 @@ +--- !ditz.rubyforge.org,2008-03-06/project +name: fnordlicht +version: "0.5" +components: +- !ditz.rubyforge.org,2008-03-06/component + name: fnordlicht/firmware +- !ditz.rubyforge.org,2008-03-06/component + name: fnordlicht/bootloader +- !ditz.rubyforge.org,2008-03-06/component + name: fnordlicht-controller/firmware +- !ditz.rubyforge.org,2008-03-06/component + name: fnordlicht-controller/bootloader +releases: +- !ditz.rubyforge.org,2008-03-06/release + name: "0.3" + status: :released + release_time: 2009-12-15 20:26:20.796707 Z + log_events: + - - 2009-11-29 15:27:19.181082 Z + - Alexander Neumann + - created + - "" + - - 2009-12-15 20:26:20.796732 Z + - Alexander Neumann + - released + - "" diff --git a/firmware/common/common.h b/firmware/common/common.h new file mode 100644 index 0000000..7a3b7b9 --- /dev/null +++ b/firmware/common/common.h @@ -0,0 +1,53 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef COMMON_H +#define COMMON_H + +#include + +/* macros for extracting low and high byte */ +#define LO8(x) (uint8_t)(0x00ff & (x)) +#define HI8(x) (uint8_t)((0xff00 & (x)) >> 8) + +/* macros for concatenating PORT, PIN and DDR */ +#define _CONCAT(a, b) a ## b +#define _OUTPORT(name) _CONCAT(PORT, name) +#define _INPORT(name) _CONCAT(PIN, name) +#define _DDRPORT(name) _CONCAT(DDR, name) +#define _PCIE(name) _CONCAT(PCIE, name) +#define _PCIF(name) _CONCAT(PCIF, name) +#define _PCMSK(name) _CONCAT(PCMSK, name) +#define _PCINT(name) _CONCAT(PCINT, name) + +/* __noinline attribute (opposite of inline attribute */ +#define __noinline __attribute__((noinline)) + +/* structure for accessing bytes and words in an uint32_t */ +union uint32_t_access { + uint8_t bytes[4]; + uint16_t words[2]; + uint32_t raw; +}; + +#endif diff --git a/firmware/common/io.h b/firmware/common/io.h new file mode 100644 index 0000000..81a3ed3 --- /dev/null +++ b/firmware/common/io.h @@ -0,0 +1,101 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef IO_H +#define IO_H + +#include + +/* cpu specific configuration registers */ +#if defined(__AVR_ATmega8__) + +#define _TIMSK_TIMER1 TIMSK +#define _TIFR_TIMER1 TIFR +#define _UCSRB_UART0 UCSRB +#define _UCSRA_UART0 UCSRA +#define _UDRIE_UART0 UDRIE +#define _UDRE_UART0 UDRE +#define _RXC_UART0 RXC +#define _TXC_UART0 TXC +#define _TXEN_UART0 TXEN +#define _RXEN_UART0 RXEN +#define _RXCIE_UART0 RXCIE +#define _UBRRH_UART0 UBRRH +#define _UBRRL_UART0 UBRRL +#define _UCSRC_UART0 UCSRC +#define _UCSZ0_UART0 UCSZ0 +#define _UCSZ1_UART0 UCSZ1 +#define _U2X_UART0 U2X +#define _SIG_UART_RECV_UART0 SIG_UART_RECV +#define _SIG_UART_DATA_UART0 SIG_UART_DATA +#define _UDR_UART0 UDR +#define UCSR0A UCSRA +#define UCSR0C UCSRC +#define MPCM0 MPCM +#define UCSZ00 UCSZ0 +#define UCSZ01 UCSZ1 +#define UCSZ02 UCSZ2 +#define UBRR0H UBRRH +#define UBRR0L UBRRL +#define UCSR0B UCSRB +#define RXEN0 RXEN +#define TXEN0 TXEN +#define RXC0 RXC +#define RXB80 RXB8 +#define UDR0 UDR +#define _IVREG GICR +#ifndef MCUSR +#define MCUSR MCUCSR +#endif +#define _IFR_INT0 GIFR +#define _ICR_INT0 GICR + +#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) + +#define _TIMSK_TIMER1 TIMSK1 +#define _TIFR_TIMER1 TIFR1 +#define _UCSRA_UART0 UCSR0A +#define _UCSRB_UART0 UCSR0B +#define _UDRIE_UART0 UDRIE0 +#define _UDRE_UART0 UDRE0 +#define _RXC_UART0 RXC0 +#define _TXC_UART0 TXC0 +#define _TXEN_UART0 TXEN0 +#define _RXEN_UART0 RXEN0 +#define _RXCIE_UART0 RXCIE0 +#define _UBRRH_UART0 UBRR0H +#define _UBRRL_UART0 UBRR0L +#define _UCSRC_UART0 UCSR0C +#define _UCSZ0_UART0 UCSZ00 +#define _UCSZ1_UART0 UCSZ01 +#define _U2X_UART0 U2X0 +#define _SIG_UART_RECV_UART0 SIG_USART_RECV +#define _SIG_UART_DATA_UART0 SIG_USART_DATA +#define _UDR_UART0 UDR0 +#define _IVREG MCUCR +#define _IFR_INT0 EIFR +#define _ICR_INT0 EIMSK +#endif + + +#endif diff --git a/firmware/common/pt/lc-switch.h b/firmware/common/pt/lc-switch.h new file mode 100644 index 0000000..dbdde01 --- /dev/null +++ b/firmware/common/pt/lc-switch.h @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2004-2005, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Adam Dunkels + * + * $Id: lc-switch.h,v 1.4 2006/06/03 11:29:43 adam Exp $ + */ + +/** + * \addtogroup lc + * @{ + */ + +/** + * \file + * Implementation of local continuations based on switch() statment + * \author Adam Dunkels + * + * This implementation of local continuations uses the C switch() + * statement to resume execution of a function somewhere inside the + * function's body. The implementation is based on the fact that + * switch() statements are able to jump directly into the bodies of + * control structures such as if() or while() statmenets. + * + * This implementation borrows heavily from Simon Tatham's coroutines + * implementation in C: + * http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html + */ + +#ifndef __LC_SWITCH_H__ +#define __LC_SWITCH_H__ + +/* WARNING! lc implementation using switch() does not work if an + LC_SET() is done within another switch() statement! */ + +/** \hideinitializer */ +typedef unsigned short lc_t; + +#define LC_INIT(s) s = 0; + +#define LC_RESUME(s) switch(s) { case 0: + +#define LC_SET(s) s = __LINE__; case __LINE__: + +#define LC_END(s) } + +#endif /* __LC_SWITCH_H__ */ + +/** @} */ diff --git a/firmware/common/pt/lc.h b/firmware/common/pt/lc.h new file mode 100644 index 0000000..3aa61f9 --- /dev/null +++ b/firmware/common/pt/lc.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2004-2005, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Author: Adam Dunkels + * + * $Id: lc.h,v 1.2 2005/02/24 10:36:59 adam Exp $ + */ + +/** + * \addtogroup pt + * @{ + */ + +/** + * \defgroup lc Local continuations + * @{ + * + * Local continuations form the basis for implementing protothreads. A + * local continuation can be set in a specific function to + * capture the state of the function. After a local continuation has + * been set can be resumed in order to restore the state of the + * function at the point where the local continuation was set. + * + * + */ + +/** + * \file lc.h + * Local continuations + * \author + * Adam Dunkels + * + */ + +#ifdef DOXYGEN +/** + * Initialize a local continuation. + * + * This operation initializes the local continuation, thereby + * unsetting any previously set continuation state. + * + * \hideinitializer + */ +#define LC_INIT(lc) + +/** + * Set a local continuation. + * + * The set operation saves the state of the function at the point + * where the operation is executed. As far as the set operation is + * concerned, the state of the function does not include the + * call-stack or local (automatic) variables, but only the program + * counter and such CPU registers that needs to be saved. + * + * \hideinitializer + */ +#define LC_SET(lc) + +/** + * Resume a local continuation. + * + * The resume operation resumes a previously set local continuation, thus + * restoring the state in which the function was when the local + * continuation was set. If the local continuation has not been + * previously set, the resume operation does nothing. + * + * \hideinitializer + */ +#define LC_RESUME(lc) + +/** + * Mark the end of local continuation usage. + * + * The end operation signifies that local continuations should not be + * used any more in the function. This operation is not needed for + * most implementations of local continuation, but is required by a + * few implementations. + * + * \hideinitializer + */ +#define LC_END(lc) + +/** + * \var typedef lc_t; + * + * The local continuation type. + * + * \hideinitializer + */ +#endif /* DOXYGEN */ + +#ifndef __LC_H__ +#define __LC_H__ + + +#ifdef LC_INCLUDE +#include LC_INCLUDE +#else +#include "lc-switch.h" +#endif /* LC_INCLUDE */ + +#endif /* __LC_H__ */ + +/** @} */ +/** @} */ diff --git a/firmware/common/pt/pt.h b/firmware/common/pt/pt.h new file mode 100644 index 0000000..92856cb --- /dev/null +++ b/firmware/common/pt/pt.h @@ -0,0 +1,323 @@ +/* + * Copyright (c) 2004-2005, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * Author: Adam Dunkels + * + * $Id: pt.h,v 1.7 2006/10/02 07:52:56 adam Exp $ + */ + +/** + * \addtogroup pt + * @{ + */ + +/** + * \file + * Protothreads implementation. + * \author + * Adam Dunkels + * + */ + +#ifndef __PT_H__ +#define __PT_H__ + +#include "lc.h" + +struct pt { + lc_t lc; +}; + +#define PT_WAITING 0 +#define PT_YIELDED 1 +#define PT_EXITED 2 +#define PT_ENDED 3 + +/** + * \name Initialization + * @{ + */ + +/** + * Initialize a protothread. + * + * Initializes a protothread. Initialization must be done prior to + * starting to execute the protothread. + * + * \param pt A pointer to the protothread control structure. + * + * \sa PT_SPAWN() + * + * \hideinitializer + */ +#define PT_INIT(pt) LC_INIT((pt)->lc) + +/** @} */ + +/** + * \name Declaration and definition + * @{ + */ + +/** + * Declaration of a protothread. + * + * This macro is used to declare a protothread. All protothreads must + * be declared with this macro. + * + * \param name_args The name and arguments of the C function + * implementing the protothread. + * + * \hideinitializer + */ +#define PT_THREAD(name_args) char name_args + +/** + * Declare the start of a protothread inside the C function + * implementing the protothread. + * + * This macro is used to declare the starting point of a + * protothread. It should be placed at the start of the function in + * which the protothread runs. All C statements above the PT_BEGIN() + * invokation will be executed each time the protothread is scheduled. + * + * \param pt A pointer to the protothread control structure. + * + * \hideinitializer + */ +#define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc) + +/** + * Declare the end of a protothread. + * + * This macro is used for declaring that a protothread ends. It must + * always be used together with a matching PT_BEGIN() macro. + * + * \param pt A pointer to the protothread control structure. + * + * \hideinitializer + */ +#define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \ + PT_INIT(pt); return PT_ENDED; } + +/** @} */ + +/** + * \name Blocked wait + * @{ + */ + +/** + * Block and wait until condition is true. + * + * This macro blocks the protothread until the specified condition is + * true. + * + * \param pt A pointer to the protothread control structure. + * \param condition The condition. + * + * \hideinitializer + */ +#define PT_WAIT_UNTIL(pt, condition) \ + do { \ + LC_SET((pt)->lc); \ + if(!(condition)) { \ + return PT_WAITING; \ + } \ + } while(0) + +/** + * Block and wait while condition is true. + * + * This function blocks and waits while condition is true. See + * PT_WAIT_UNTIL(). + * + * \param pt A pointer to the protothread control structure. + * \param cond The condition. + * + * \hideinitializer + */ +#define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond)) + +/** @} */ + +/** + * \name Hierarchical protothreads + * @{ + */ + +/** + * Block and wait until a child protothread completes. + * + * This macro schedules a child protothread. The current protothread + * will block until the child protothread completes. + * + * \note The child protothread must be manually initialized with the + * PT_INIT() function before this function is used. + * + * \param pt A pointer to the protothread control structure. + * \param thread The child protothread with arguments + * + * \sa PT_SPAWN() + * + * \hideinitializer + */ +#define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread)) + +/** + * Spawn a child protothread and wait until it exits. + * + * This macro spawns a child protothread and waits until it exits. The + * macro can only be used within a protothread. + * + * \param pt A pointer to the protothread control structure. + * \param child A pointer to the child protothread's control structure. + * \param thread The child protothread with arguments + * + * \hideinitializer + */ +#define PT_SPAWN(pt, child, thread) \ + do { \ + PT_INIT((child)); \ + PT_WAIT_THREAD((pt), (thread)); \ + } while(0) + +/** @} */ + +/** + * \name Exiting and restarting + * @{ + */ + +/** + * Restart the protothread. + * + * This macro will block and cause the running protothread to restart + * its execution at the place of the PT_BEGIN() call. + * + * \param pt A pointer to the protothread control structure. + * + * \hideinitializer + */ +#define PT_RESTART(pt) \ + do { \ + PT_INIT(pt); \ + return PT_WAITING; \ + } while(0) + +/** + * Exit the protothread. + * + * This macro causes the protothread to exit. If the protothread was + * spawned by another protothread, the parent protothread will become + * unblocked and can continue to run. + * + * \param pt A pointer to the protothread control structure. + * + * \hideinitializer + */ +#define PT_EXIT(pt) \ + do { \ + PT_INIT(pt); \ + return PT_EXITED; \ + } while(0) + +/** @} */ + +/** + * \name Calling a protothread + * @{ + */ + +/** + * Schedule a protothread. + * + * This function shedules a protothread. The return value of the + * function is non-zero if the protothread is running or zero if the + * protothread has exited. + * + * \param f The call to the C function implementing the protothread to + * be scheduled + * + * \hideinitializer + */ +#define PT_SCHEDULE(f) ((f) < PT_EXITED) + +/** @} */ + +/** + * \name Yielding from a protothread + * @{ + */ + +/** + * Yield from the current protothread. + * + * This function will yield the protothread, thereby allowing other + * processing to take place in the system. + * + * \param pt A pointer to the protothread control structure. + * + * \hideinitializer + */ +#define PT_YIELD(pt) \ + do { \ + PT_YIELD_FLAG = 0; \ + LC_SET((pt)->lc); \ + if(PT_YIELD_FLAG == 0) { \ + return PT_YIELDED; \ + } \ + } while(0) + +/** + * \brief Yield from the protothread until a condition occurs. + * \param pt A pointer to the protothread control structure. + * \param cond The condition. + * + * This function will yield the protothread, until the + * specified condition evaluates to true. + * + * + * \hideinitializer + */ +#define PT_YIELD_UNTIL(pt, cond) \ + do { \ + PT_YIELD_FLAG = 0; \ + LC_SET((pt)->lc); \ + if((PT_YIELD_FLAG == 0) || !(cond)) { \ + return PT_YIELDED; \ + } \ + } while(0) + +/** @} */ + +#endif /* __PT_H__ */ + +/** @} */ diff --git a/firmware/common/remote-proto.h b/firmware/common/remote-proto.h new file mode 100644 index 0000000..6b40475 --- /dev/null +++ b/firmware/common/remote-proto.h @@ -0,0 +1,117 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __REMOTE_PROTO_COMMON +#define __REMOTE_PROTO_COMMON 1 + +#define REMOTE_MSG_LEN 15 +#define REMOTE_SYNC_LEN 15 + +/* normal commands */ +#define REMOTE_CMD_FADE_RGB 0x01 +#define REMOTE_CMD_FADE_HSV 0x02 +#define REMOTE_CMD_SAVE_RGB 0x03 +#define REMOTE_CMD_SAVE_HSV 0x04 +#define REMOTE_CMD_SAVE_CURRENT 0x05 +#define REMOTE_CMD_CONFIG_OFFSETS 0x06 +#define REMOTE_CMD_START_PROGRAM 0x07 +#define REMOTE_CMD_STOP 0x08 +#define REMOTE_CMD_MODIFY_CURRENT 0x09 +#define REMOTE_CMD_PULL_INT 0x0A +#define REMOTE_CMD_CONFIG_STARTUP 0x0B +#define REMOTE_CMD_POWERDOWN 0x0C + +#define REMOTE_CMD_RESYNC 0x1b + +/* max mode for startup configuration is 1 */ +#define REMOTE_STARTUP_MAX_MODE 1 +/* maximum parameter size (for copy loop), size of structure storage_config_t, + * minus 1 for startup_mode enum */ +#define REMOTE_STARTUP_MAX_PARAMSIZE 11 + +/* bootloader commands */ +#define REMOTE_CMD_BOOTLOADER 0x80 +#define REMOTE_CMD_BOOT_CONFIG 0x81 +#define REMOTE_CMD_BOOT_INIT 0x82 +#define REMOTE_CMD_BOOT_DATA 0x83 +#define REMOTE_CMD_CRC_CHECK 0x84 +#define REMOTE_CMD_CRC_FLASH 0x85 +#define REMOTE_CMD_FLASH 0x86 +#define REMOTE_CMD_ENTER_APP 0x87 + +#define REMOTE_ADDR_BROADCAST 0xff + +/* normal commands */ +struct remote_msg_t +{ + uint8_t address; + uint8_t cmd; + uint8_t data[REMOTE_MSG_LEN-2]; +}; + +/* bootloader commands */ +#define BOOTLOADER_MAGIC_BYTE1 0x6b +#define BOOTLOADER_MAGIC_BYTE2 0x56 +#define BOOTLOADER_MAGIC_BYTE3 0x27 +#define BOOTLOADER_MAGIC_BYTE4 0xfc +struct remote_msg_bootloader_t +{ + uint8_t address; + uint8_t cmd; + uint8_t magic[4]; +}; + +struct remote_msg_boot_config_t +{ + uint8_t address; + uint8_t cmd; + uint16_t start_address; + uint8_t buffersize; +}; + +struct remote_msg_boot_data_t +{ + uint8_t address; + uint8_t cmd; + uint8_t data[REMOTE_MSG_LEN-2]; +}; + +struct remote_msg_boot_crc_check_t +{ + uint8_t address; + uint8_t cmd; + uint16_t len; + uint16_t checksum; + uint8_t delay; +}; + +struct remote_msg_boot_crc_flash_t +{ + uint8_t address; + uint8_t cmd; + uint16_t start; + uint16_t len; + uint16_t checksum; + uint8_t delay; +}; + +#endif diff --git a/firmware/doc/PROTOCOL b/firmware/doc/PROTOCOL new file mode 100644 index 0000000..1b61d78 --- /dev/null +++ b/firmware/doc/PROTOCOL @@ -0,0 +1,603 @@ +SERIAL PROTOCOL FOR FNORDLICHT-NG FIRMWARE +========================================== + +Physical bus description +------------------------ + +The serial bus consists of up to 254 devices, each device having the uart rx +pin connected to the uart tx pin of the predecessor, the uart tx pin connected +to the tx successor. The uart is used with a baud rate of 19200. A device +will retransmit each received byte immediately and unaltered, the only +exception is the address byte in the sync sequence (which is incremented by one +before retransmission). An interrupt line is connected to each device. + +Diagram: + + +----------------+ +----------------+ + | device N | | device N+1 | +UART: ... --> >-|RX TX|-> --> >-|RX TX|-> ... + | INT | | INT | + +-------+--------+ +-------+--------+ + | | +INT: -------------------+--------------------------+----------> ... + + +Bootloader startup procedure +---------------------------- + +For updating the firmware installed on the devices of a bus, each device should +be equipped with a bootloader program. At power-on, the bootloader should be +started, sleep for 100ms and check afterwards, if the INT line is pulled down +(by a bus-master). If INT is not pulled down, the bootloader is allowed to +start the main firmware. Otherwise it should not attempt to start the main +firmware, but wait for commands received over the bus. This procedure allows a +recovery from a broken firmware which does not process commands received over +the bus (such as the BOOTLOADER command). To enter the bootloader via the +BOOTLOADER command, first pull down the INT line, then issue the command. + +Initial sync sequence +--------------------- + +For the purpose of (automatic) address discovery and resetting the bus, a +sync sequence consisting of 15x ESC (0x1b) followed by an address byte is used. +The controller sends 15x ESC, followed by a null byte. Please note that the +length of the sync sequence (16 byte) is different from the length of a +command packet, which is 15 byte! + +A device MUST be able to detect this sequence asynchronously, i.e. even if it is +in the middle of receiving a command packet. The device does not need to +withhold the execution of the received command packet until it is clear if a +sequence of ESC bytes constitutes a sync sequence. + +Example: + A device received 4 bytes, say A B C D, of a packet, then a sync sequence. + It may (or may not) execute the packet + + A B C D ESC ESC ... ESC + + but the sync sequence must be detected, too. + +Asynchronous detection of the sync sequence can be done by just counting the +number of consecutive ESC bytes, as 15 consecutive ESC bytes can only occur +in a sync sequence, never in a command packet (ESC cannot occur as a command in +the second byte of a command packet). + +Flow: +* controller sends 15x ESC, followed by the address of the first device + (usually a null) +* first device on the bus receives and retransmits 15x ESC, receives address + byte, stores own address in ram, increments address byte and transmits new + address to next device on the bus + +Result: Each device on the bus knows it's own address (=position). + + +Commands +-------- + +Commands are sent in packets, 15 bytes in length, and are passed from device +to device unmodified. The first byte is the destination address (0-254, 255 +is broadcast), the second byte contains the command. The meaning of the +following 13 bytes depends on the value of the second byte (command). A +device ignores non-broadcast packets for which the address byte does not +match it's own address. Bytes which are declared as "don't care" bytes SHOULD +be set to zero, this makes future extensions easier. + + +List of commands: +----------------- + +command | function | description +-------------------------------------------------------- + 0x01 | FADE_RGB | set color/fade to color (RGB) + 0x02 | FADE_HSV | set color/fade to color (HSV) + 0x03 | SAVE_RGB | save color to EEPROM (RGB) + 0x04 | SAVE_HSV | save color to EEPROM (HSV) + 0x05 | SAVE_CURRENT | save current color to EEPROM + 0x06 | CONFIG_OFFSETS | set global offset values + 0x07 | START_PROGRAM | start program + 0x08 | STOP | stop color changing + 0x09 | MODIFY_CURRENT | modify current color + 0x0A | PULL_INT | pull down INT line + 0x0B | CONFIG_STARTUP | configure startup + 0x0C | POWERDOWN | power down the device + 0x1B | | RESERVED (sync-sequence) + + 0x80 | BOOTLOADER | start bootloader + 0x81 | BOOT_CONFIG | configure bootloader + 0x82 | BOOT_INIT | initialize bootloader data buffer + 0x83 | BOOT_DATA | store data in bootloader data buffer + 0x84 | BOOT_CRC_CHECK | compare check-sum + 0x85 | BOOT_CRC_FLASH | compare check-sum with flash + 0x86 | BOOT_FLASH | write provided data to flash + 0x87 | BOOT_ENTER_APP | start application + + + Command: FADE_RGB - set color/fade to color (RGB) (0x01) + -------------------------------------------------------- + + Instruct the device to fade to the specified absolute color, given as a + RGB value, with a specific speed. Step and delay might be modified by + application of the global offsets (see CONFIG_OFFSETS), color values + (red, green, blue) are used unmodified. + + All values are unsigned 8 bit integers. If step is set to 255, the target + color is set without fading. If delay is 0, the target color is set + directly after receiving the command packet. + + byte offset | name | description + --------------------------------- + 2 | step | increment step for fading + 3 | delay | delay between steps when fading (in 10ms) + 4 | red | red value + 5 | green | green value + 6 | blue | blue value + 7-14 | - | don't care + + + Command: FADE_HSV - set color/fade to color (HSV) (0x02) + -------------------------------------------------------- + + Instruct the device to fade to the specified absolute color, given as a + HSV value, with a specific speed. Step, delay and hue might be modified + by application of the global offsets, saturation and value might be scaled + by global scales (see CONFIG_OFFSETS). + + All values are unsigned 8 bit integers, except hue, which is a 16 bit + little endian integer. If step is set to 255, the target color is set + without fading. If delay is 0, the target color is set directly after + receiving the command packet. + + byte offset | name | description + ------------------------------------ + 2 | step | increment step for fading + 3 | delay | delay between steps when fading (in 10ms) + 4-5 | hue | hue, 0-360, little endian + 6 | saturation | saturation + 7 | value | value (brightness) + 8-14 | - | don't care + + + Command: SAVE_RGB - save color to EEPROM (RGB) (0x03) + ----------------------------------------------------- + + Save a color in RGB format, a fade speed (step and delay) and a pause + length to the EEPROM. The EEPROM can store 60 color and speed/delay + values (see section "EEPROM color storage" below). While writing the + data to the EEPROM, the INT line is pulled down. + + All values are unsigned 8 bit integers, except pause, which is a little + endian 16 bit integer. + + byte offset | name | description + ------------------------------ + 2 | slot | slot in the EEPROM (0-59) + 3 | step | increment step for fading + 4 | delay | delay between steps when fading (in 10ms) + 5-6 | pause | time to wait before fading to next color (in 100ms) + 7 | red | red value + 8 | green | green value + 9 | blue | blue value + 10-14 | - | don't care + + + Command: SAVE_HSV - save color to EEPROM (HSV) (0x04) + ----------------------------------------------------- + + Save a color in HSV format, a fade speed (step and delay) and a pause + length to the EEPROM. The EEPROM can store 60 color and speed/delay + values (see section "EEPROM color storage" below). While writing the + data to the EEPROM, the INT line is pulled down. + + All values are unsigned 8 bit integers, except pause and hue, which are + little endian 16 bit integers. + + byte offset | name | description + ----------------------------------- + 2 | slot | slot in the EEPROM (0-59) + 3 | step | increment step for fading + 4 | delay | delay between steps when fading (in 10ms) + 5-6 | pause | time to wait before fading to next color + | | (in 100ms) + 7-8 | hue | hue, 0-360, little endian + 9 | saturation | saturation + 10 | value | value (brightness) + 11-14 | - | don't care + + + Command: SAVE_CURRENT - save current color to EEPROM (0x05) + ----------------------------------------------------------- + + Save the current color to EEPROM (RGB), together with a fade speed and a + pause length. While writing the data to the EEPROM, the INT line is + pulled down. + + All values are unsigned 8 bit integers, except pause, which is a little + endian 16 bit integers. + + byte offset | name | description + ---------------------------------------- + 2 | slot | slot in the EEPROM (0-59) + 3 | step | increment step for fading + 4 | delay | delay between steps when fading (in 10ms) + 5-6 | pause | time to wait before fading to next color + | | (in 100ms) + 7-14 | - | don't care + + + Command: CONFIG_OFFSETS - set global offset values (0x06) + --------------------------------------------------------- + + Set global values which influence how fast and what colors are when using + FADE_RGB or FADE_HSV or the static programs documented below. All bytes are + signed 8 bit integers, except hue, which is a little endian signed 16 bit + integer. Saturation and value are scales. This means, the final saturation + and value will be scaled with saturation/255 and value/255, respectively. + + byte offset | name | description + ------------------------------------------- + 2 | step | increment step for fading (offset) + 3 | delay | delay between steps when fading + | | (in 10ms, offset) + 4-5 | hue | hue offset, signed, little endian + 6 | saturation | saturation scale, unsigned, 0-255 + 7 | value | value scale, unsigned, 0-255 + 8-14 | - | don't care + + + Command: START_PROGRAM - start program (0x07) + --------------------------------------------- + + Start a program (a function compiled into the firmware) with given + parameters. This command stops all other programs (and EEPROM fade + sequences). For a list of programs and parameters see section + "Static Programs". + + byte offset | name | description + --------------------------------- + 2 | program | program id, 0-255 + 3-11 | params | 10 byte parameters passed to program + 12-13 | - | don't care + + + Command: STOP - stop color changing (0x08) + ------------------------------------------ + + Stop all processes modifying the current color. Optionally, also stop + the current fading process. + + byte offset | name | description + --------------------------------- + 2 | fade | stop fading if set (1) + 3-14 | - | don't care + + + Command: MODIFY_CURRENT - modify current color (0x09) + ----------------------------------------------------- + + Instruct the device to fade to a new target color, which is determined + relatively to the one currently visible. This works only if no + program (or EEPROM fade sequence) is running. The current color is + faded to the target color with the given step and delay values. The + RGB offsets are applied before the HSV offsets. Setting either one to + zero will not modify the color in that color space. + + Step and delay are unsigned 8 bit integers, all other values are signed 8 + (or 16) bit integers. + + byte offset | name | description + ------------------------------------- + 2 | step | increment step for fading + 3 | delay | delay between steps when fading (in 10ms) + 4 | red | red offset + 5 | green | green offset + 6 | blue | blue offset + 4-5 | hue | hue offset + 6 | saturation | saturation offset + 7 | value | value offset + 8-14 | - | don't care + + + Command: PULL_INT - pull down INT line (0x0A) + --------------------------------------------- + + Instruct the adressed device to immediately pull down the INT line + (connected to all devices in parallel) for a given amount of time. This + can be efficiently used to determine the number of devices listening to + the bus (eg. by binary search). + + byte offset | name | description + --------------------------------- + 2 | delay | time to wait before releasing the INT line + | (in 50ms, maximum 2550ms, jitter +-10ms) + 3-14 | - | don't care + + + Command: CONFIG_STARTUP - configure startup (0x0B) + -------------------------------------------------- + + Configure what a device should perform after power-up. Mode is an + unsigned 8 bit integer, selecting the desired startup mode. + + Two different modes can be configured: + + * NOTHING (mode == 0): + Do nothing after startup (ie do not show any color, stay black) + + * PROGRAM (mode == 1): + Start a static program compiled into the firmware, using the + following 10 bytes as parameters for the program (for details + see section "Static Programs"). + + If startup mode is PROGRAM, the CONFIGURE_STARTUP packet is constructed in + this way (for the program indexes and meaning of the parameters see + section "Static Programs"): + + byte offset | name | description + ------------------------------------ + 2 | mode | desired startup mode (1 in this case) + 3 | program | static program index + 4-14 | parameters | parameters to configured program + + + Command: POWERDOWN - power down device (0x0C) + --------------------------------------------- + + Power down the device. After receiving this command, the device should + power down all light outputs and suspend itself to reduce the power + consumption to a minimum. A device must resume operation after a falling + edge on the INT pin. It is allowed to activate a pull-up resistor on + that pin, to pull the pin to a defined level. + + byte offset | name | description + ------------------------------------ + 2-14 | don't care | + + + Command: BOOTLOADER - start bootloader (0x80) + --------------------------------------------- + + If a bootloader is installed, jump to the bootloader, otherwise just + restart the device. A magic byte sequence of 0xfc27566b (little endian) + must follow the command byte. Pull down the INT line BEFORE sending this + command, otherwise the bootloader might not be able to detect wether to + start the main firmware or remain within the bootloader code. + + byte offset | name | description + --------------------------------- + 2 | byte1 | magic byte 1 (0x6b) + 3 | byte2 | magic byte 2 (0x56) + 4 | byte3 | magic byte 3 (0x27) + 5 | byte4 | magic byte 4 (0xfc) + 6-14 | - | don't care + + + Command: BOOT_CONFIG - configure bootloader (0x81) + -------------------------------------------------- + + Set configuration options of the bootloader. Currently the only + options is the start address. + + Start address is an unsigned 16-bit integer automatically incremented + by the configured packet size upon each flash command. + + byte offset | name | description + --------------------------------- + 2-3 | start | address to flash to + 4-14 | - | don't care + + + Command: BOOT_INIT - initialize bootloader data buffer (0x82) + ------------------------------------------------------------- + + This resets and clears the internal bootloader data buffer. + + byte offset | name | description + --------------------------------- + 2-14 | data | payload + + + Command: BOOT_DATA - store data in the bootloader data buffer (0x83) + -------------------------------------------------------------------- + + Store data (up to 13 data bytes) at the end of the bootloader data + buffer. If the buffer is full, additional bytes will be ignored. + + byte offset | name | description + --------------------------------- + 2-14 | data | payload + + + Command: BOOT_CRC_CHECK - compare check-sum (0x84) + -------------------------------------------------- + + Compare provided CRC-16 checksum against calculated checksum over the + first len bytes of previously received data in the data buffer. If both + checksums do not match, the boot loader MUST immediately pull down the + INT line. The INT line must be held down for the time specified in delay + and be released afterwards. chksum is a little endian unsigned 16 bit + integer. + + The CRC calculation uses the polynomial x^16 + x^15 + x^2 + 1 (0xa001) + and 0xffff as initial value. + + byte offset | name | description + --------------------------------- + 2-3 | len | checksum over the first len buffer bytes in the buffer + 4-5 | chksum | CRC-16 checksum over the data chunk + 6 | delay | time to wait before releasing the INT line + | (in 50ms, maximum 2550ms, jitter +-10ms) + 7-14 | - | don't care + + + Command: BOOT_CRC_FLASH - compare check-sum with flash (0x85) + ------------------------------------------------------------- + + Compare provided CRC-16 checksum against calculated checksum over + len bytes from flash starting at addr. If both checksums do not match, + the boot loader MUST immediately pull down the INT line. The INT line + must be held down for the time specified in delay and be released + afterwards. Chksum, start and len are unsigned 16 bit little endian + integers. + + The CRC calculation uses the polynomial x^16 + x^15 + x^2 + 1 (0xa001) + and 0xffff as initial value. + + byte offset | name | description + --------------------------------- + 2-3 | addr | checksum len bytes starting at this address + 4-6 | len | checksum over the first len buffer bytes in the buffer + 6-8 | chksum | CRC-16 checksum over the data chunk + 9 | delay | time to wait before releasing the INT line + | (in 50ms, maximum 2550ms, jitter +-10ms) + 10-14 | - | don't care + + + Command: BOOT_FLASH - write provided data to flash (0x86) + --------------------------------------------------------- + + Write the data provided earlier to the flash memory of the device. + + The device pulls the interrupt line down upon reception of the command + and releases it on successful completion of the command. This way the + master node is capable of detecting when the successing initial data + packet may be provided by continuously sensing the interrupt line. + + After completion of this command the address where to flash to is + incremented by the configured chunk data size. + + + byte offset | name | description + --------------------------------- + 2-14 | - | don't care + + + Command: BOOT_ENTER_APP - start application (0x87) + -------------------------------------------------- + + Leave bootloader and launch application code. + + + byte offset | name | description + ------------------------------------ + 2-14 | - | don't care + + +Static Programs +--------------- + +This section describes the programs which are (in the default version) +compiled into the firmware. + + program "colorwheel", program index 0 + ------------------------------------- + + This program fades throught the HSV color space, with fixed saturation and + value, starting at hue_start. In each step, hue_step is added to the + current hue value, afterwards the device fades to the resulting color (using + fade_step and fade_delay) and waits for the time specified by fade_sleep. + + If add_addr is nonzero (attention: signed integer!), the fade ist not + started at hue_start but (hue_start + addr_add * device_address * hue_step). + + This program honors the globally defined offsets set with the CONFIG_OFFSETS + command in each step. If a global offset is reconfigured, the new value + will be applied in the next color calculation of this program. + + byte offset | name | description + --------------------------------------------------------- + 0 | fade_step | used for fading to new color + | | (unsigned 8 bit integer) + 1 | fade_delay | used for fading to now color + | | (unsigned 8 bit integer) + 2 | fade_sleep | sleep between steps (in seconds) + | | (unsigned 8 bit integer) + 3-4 | hue_start | start at this hue value + | | (unsigned 16 bit integer + 5-6 | hue_step | add this to the hue in each step + | | (signed 16 bit integer) + 7 | add_addr | add hue_step address-times before start + | | (signed 8 bit integer) + 8 | saturation | saturation + | | (unsigned 8 bit integer) + 9 | value | value + | | (unsigned 8 bit integer) + + + program "random", program index 1 + --------------------------------- + + When this program is started, the pseudo-random generator is initialized + with the seed value. If the use_address-bit within the flags byte is set, + the address of a device is XORed with the seed before initializing the + pseudo-random generator, so the random values also depend on the address of + the device. + + In each step, this program generates a new random hue value which is at + least min_distance away from the current hue value. Afterwards the device + fades to the new color (using the generated hue and the configured + saturation and value from the parameters). The parameters fade_step and + fade_delay are used in each fade. If the wait_for_fade-bit within the + flags byte is set, the device waits until the target color is reached. + Afterwards the device sleeps for a configurable amount of time (fade_sleep). + + This program honors the globally defined offsets set with the CONFIG_OFFSETS + command in each step. If a global offset is reconfigured, the new value + will be applied in the next color calculation of this program. + + flags (1 byte) + -------------- + + Bits: + MSB LSB + 7 6 5 4 3 2 1 0 + | | | | + | | | \- use_address -- XOR device address and seed before + | | | initializing the pseudo random generator + | | \--- wait_for_fade -- wait until the (newly generated) + | | target color is reached before sleeping + \---------+----- reserved + + byte offset | name | description + ---------------------------------------------------------------------- + 0-1 | seed | used for initializing the random generator + | | (unsigned 16 bit integer) + 2 | flags | flags + | | (unsigned 8 bit integer) + 3 | fade_step | used for fading to new color + | | (unsigned 8 bit integer) + 4 | fade_delay | used for fading to now color + | | (unsigned 8 bit integer) + 5-6 | fade_sleep | sleep between steps (in 100ms) + | | (unsigned 16 bit integer) + 7 | saturation | saturation + | | (unsigned 8 bit integer) + 8 | value | value + | | (unsigned 8 bit integer) + 9 | min_distance | minimal distance for new hue value + | | (unsigned 8 bit integer) + + + program "replay", program index 2 + --------------------------------- + + The commands SAVE_RGB, SAVE_HSV and SAVE_CURRENT stores a color, + fade_step, fade_delay and fade_sleep to the EEPROM. This program replays + the stored colors, from start to end. If repeat is 0, the sequence stops + when end is reached, if repeat is 1, the sequence is restarted from the + beginning and if repeat is 2, the color sequence is played in reverse + order, from end to beginning. + + This program honors the globally defined offsets set with the CONFIG_OFFSETS + command for hsv colors in each step. If a global offset is reconfigured, + the new value will be applied in the next color calculation of this program. + + byte offset | name | description + ---------------------------------------------------------------------- + 0 | start | index of the first color + | | (unsigned 8 bit integer) + 1 | end | index of the last color (included) + | | (unsigned 8 bit integer) + 3 | repeat | configure repetition + | | (unsigned 8 bit integer) + 4-9 | don't care | diff --git a/firmware/doc/pwm_table.py b/firmware/doc/pwm_table.py new file mode 100644 index 0000000..07f228e --- /dev/null +++ b/firmware/doc/pwm_table.py @@ -0,0 +1,20 @@ +#!/usr/bin/python +# +# compute pwm reference table + +clock = 16000000 +basefreq = 125 +levels = 256 +interval_cycles = 64000 + +if __name__ == "__main__": + last = 0 + sum = 0 + for i in range(1,levels): + cycles = (i/float(levels-1))**2*float(clock)/float(basefreq) + delta = round(cycles-last) + print "%6d: %10d %6d %6d" % (i, round(cycles), delta, round(cycles) % interval_cycles) + last = cycles + sum+=delta + +print "sum: %d" % sum diff --git a/firmware/fnordlicht-bootloader/Makefile b/firmware/fnordlicht-bootloader/Makefile new file mode 100644 index 0000000..bcc7b1f --- /dev/null +++ b/firmware/fnordlicht-bootloader/Makefile @@ -0,0 +1,263 @@ +#################################################### +# fnordlicht-ng Makefile +#################################################### + +# ATTENTION: +# Any of these variables are overridden with the values from the file +# "config.mk", you can tune your settings there. The file "config.mk" is not +# under version control. +# Just run 'make' to create a "config.mk" with the default settings + +# hardware type, possible values (default is unset): +# ATTENTION: please configure in config.mk, this is just included here for reference! +# - fnordlicht -- original fnordlicht hardware +# - fnordlichtmini -- fnordlichtmini hardware +#HARDWARE = fnordlicht + +# controller +MCU = atmega8 + +# frequency +F_CPU = 16000000UL + +# main application name (without .hex) +# eg 'test' when the main function is defined in 'test.c' +TARGET = fboot + +# c sourcecode files +# eg. 'test.c foo.c foobar/baz.c' +SRC = $(wildcard *.c) + +# asm sourcecode files +# eg. 'interrupts.S foobar/another.S' +ASRC = $(wildcard *.S) + +# headers which should be considered when recompiling +# eg. 'global.h foobar/important.h' +HEADERS = $(wildcard *.h) + +# include directories (used for both, c and asm) +# eg '. usbdrv/' +INCLUDES = + + +# use more debug-flags when compiling +DEBUG = 0 + +# default baudrate +CONFIG_SERIAL_BAUDRATE = 19200 + +# avrdude programmer protocol +PROG = usbasp +# avrdude programmer device +DEV = usb +# further flags for avrdude +AVRDUDE_FLAGS = + +CFLAGS += -DHARDWARE_$(HARDWARE)=1 +CFLAGS += -DCONFIG_SERIAL_BAUDRATE=$(CONFIG_SERIAL_BAUDRATE) + +# use a custom linker script +LDFLAGS += -L$(CURDIR)/ldscripts + +.PHONY: all + +# main make target (moved up here because of the config.mk target) +all: $(TARGET).hex + +# create config.mk (if it does not exist yet) +$(CURDIR)/config.mk: + @$(CP) config.mk.template config.mk + @echo "====================================================" + @echo "created file $@" + @echo 'please tune your settings (especially $$HARDWARE)' + @echo "there, then run 'make' again" + @echo "====================================================" + @exit 1 + +# include config file +-include $(CURDIR)/config.mk + +# bootloader section start +# (see datasheet) +ifeq ($(MCU),atmega8) + # atmega8 with 1024 words bootloader: + # bootloader section starts at 0xc00 (word-address) == 0x1800 (byte-address) + #BOOT_SECTION_START = 0x1800 + # + # atmega8 with 512 words bootloader: + # bootloader section starts at 0xe00 (word-address) == 0x1c00 (byte-address) + BOOT_SECTION_START = 0x1c00 + # 7kb data memory (raw memory minus bootloader space) + DATA_MEM = 7 + + # use custom linker script + LDFLAGS += -T avr4.x +endif +ifeq ($(MCU),atmega88) + # atmega88 with 1024 words bootloader: + # bootloader section starts at 0xc00 (word-address) == 0x1800 (byte-address) + #BOOT_SECTION_START = 0x1800 + # + # atmega88 with 512 words bootloader: + # bootloader section starts at 0xe00 (word-address) == 0x1c00 (byte-address) + BOOT_SECTION_START = 0x1c00 + # 7kb data memory (raw memory minus bootloader space) + DATA_MEM = 7 + + # use custom linker script + LDFLAGS += -T avr4.x +endif +ifeq ($(MCU),atmega168) + # atmega168 with 1024 words bootloader: + # bootloader section starts at 0x1c00 (word-address) == 0x3800 (byte-address) + #BOOT_SECTION_START = 0x3800 + # + # atmega168 with 512 words bootloader: + # bootloader section starts at 0x1e00 (word-address) == 0x3c00 (byte-address) + BOOT_SECTION_START = 0x3c00 + # 15kb data memory (raw memory minus bootloader space) + DATA_MEM = 15 + + # use custom linker script + LDFLAGS += -T avr5.x +endif + +LDFLAGS += -Wl,--section-start=.text=$(BOOT_SECTION_START) +CFLAGS += -DBOOT_SECTION_START=$(BOOT_SECTION_START) + + +#################################################### +# 'make' configuration +#################################################### +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AS = avr-as +SIZE = avr-size +CP = cp +RM = rm -f +RMDIR = rm -rf +MKDIR = mkdir +AVRDUDE = avrdude + +# flags for automatic dependency handling +DEPFLAGS = -MD -MP -MF .dep/$(@F).d + +# flags for the compiler (for .c files) +CFLAGS += -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -std=gnu99 -fshort-enums $(DEPFLAGS) +CFLAGS += $(addprefix -I,$(INCLUDES)) +CFLAGS += -fno-split-wide-types +CFLAGS += --param inline-call-cost=2 -finline-limit=3 -fno-inline-small-functions +CFLAGS += -Wl,--relax + +# flags for the compiler (for .S files) +ASFLAGS += -g -mmcu=$(MCU) -DF_CPU=$(F_CPU) -x assembler-with-cpp $(DEPFLAGS) +ASFLAGS += $(addprefix -I,$(INCLUDES)) + +# flags for the linker +LDFLAGS += -mmcu=$(MCU) + +# fill in object files +OBJECTS += $(SRC:.c=.o) +OBJECTS += $(ASRC:.S=.o) + +# include more debug flags, if $(DEBUG) is 1 +ifeq ($(DEBUG),1) + CFLAGS += -Wall -W -Wchar-subscripts -Wmissing-prototypes + CFLAGS += -Wmissing-declarations -Wredundant-decls + CFLAGS += -Wstrict-prototypes -Wshadow -Wbad-function-cast + CFLAGS += -Winline -Wpointer-arith -Wsign-compare + CFLAGS += -Wunreachable-code -Wdisabled-optimization + CFLAGS += -Wcast-align -Wwrite-strings -Wnested-externs -Wundef + CFLAGS += -Wa,-adhlns=$(basename $@).lst + CFLAGS += -DCONFIG_DEBUG=1 +endif + +#################################################### +# avrdude configuration +#################################################### +ifeq ($(MCU),atmega8) + AVRDUDE_MCU=m8 +endif +ifeq ($(MCU),atmega48) + AVRDUDE_MCU=m48 +endif +ifeq ($(MCU),atmega88) + AVRDUDE_MCU=m88 +endif +ifeq ($(MCU),atmega168) + AVRDUDE_MCU=m168 +endif + +AVRDUDE_FLAGS += -p $(AVRDUDE_MCU) + +#################################################### +# make targets +#################################################### + +.PHONY: hardware-check clean distclean avrdude-terminal + +# check if HARDWARE is set +hardware-check: +ifeq ($(HARDWARE),) + @echo 'please edit config.mk and set $$HARDWARE' + @exit 1 +endif + +$(TARGET).elf: hardware-check $(OBJECTS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJECTS) + +# all objects (.o files) and config.mk +$(OBJECTS): $(HEADERS) config.mk + +# remove all compiled files +clean: + $(RM) $(foreach ext,elf hex eep.hex map,$(TARGET).$(ext)) \ + $(foreach file,$(patsubst %.o,%,$(OBJECTS)),$(foreach ext,o lst lss,$(file).$(ext))) + +# additionally remove the dependency makefile and config.mk +distclean: clean + $(RMDIR) .dep + $(RM) config.mk + +# avrdude-related targets +install program: program-$(TARGET) + +avrdude-terminal: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -t + +program-%: %.hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U flash:w:$< -U lock:w:0x2f:m + +program-eeprom-%: %.eep.hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U eeprom:w:$< + +# special programming targets +%.hex: %.elf + $(OBJCOPY) -O ihex -R .eeprom $< $@ + @echo "====================================================" + @echo "$@ compiled for hardware: $(HARDWARE)" + @echo "using controller $(MCU)" + @echo -n "size for $< is " + @$(SIZE) -A $@ | grep '\.sec1' | tr -s ' ' | cut -d" " -f2 + @echo "====================================================" + +%.eep.hex: %.elf + $(OBJCOPY) --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex -j .eeprom $< $@ + +%.lss: %.elf + $(OBJDUMP) -h -S $< > $@ + +.PHONY: fuses-atmega8-fnordlichtmini-with-bootloader fuses-lock + +fuses-atmega8-fnordlichtmini-with-bootloader: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lfuse:w:0x3f:m -U hfuse:w:0xda:m + +fuses-atmega8-fnordlichtmini-without-bootloader: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lfuse:w:0x3f:m -U hfuse:w:0xd9:m + +fuses-lock: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lock:w:0x2f:m + +-include $(shell $(MKDIR) .dep 2>/dev/null) $(wildcard .dep/*) diff --git a/firmware/fnordlicht-bootloader/config.h b/firmware/fnordlicht-bootloader/config.h new file mode 100644 index 0000000..eea4448 --- /dev/null +++ b/firmware/fnordlicht-bootloader/config.h @@ -0,0 +1,37 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlichtmini serial bootloader + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __CONFIG_H +#define __CONFIG_H + +/* configure bootloader buffer size */ +#ifndef CONFIG_BOOTLOADER_BUFSIZE +#define CONFIG_BOOTLOADER_BUFSIZE 512 +#endif + +/* configure exit delay (in 50ms) */ +#ifndef CONFIG_EXIT_DELAY +#define CONFIG_EXIT_DELAY 2 +#endif + +#endif diff --git a/firmware/fnordlicht-bootloader/config.mk.template b/firmware/fnordlicht-bootloader/config.mk.template new file mode 100644 index 0000000..858cfa5 --- /dev/null +++ b/firmware/fnordlicht-bootloader/config.mk.template @@ -0,0 +1,49 @@ +# Put your own config here! +# +# This file is not under version control, and your settings +# will not be modified when upgrading the repository + + +################################################################### +# HARDWARE PLATFORM TYPE +# +# possible values: +# - fnordlicht -- original fnordlicht hardware +# - fnordlichtmini -- fnordlichtmini hardware +#HARDWARE = fnordlicht + +################################################################### +# PROGRAMMER +# +# avrdude programmer protocol +#PROG = usbasp + +# avrdude programmer device +#DEV = usb + +# further flags for avrdude +#AVRDUDE_FLAGS = + + +################################################################### +# controller +# +# possible values: +# - atmega8 +# - atmega88 +# - atmega168 +# +#MCU = atmega8 + +# frequency +#F_CPU = 16000000UL + +# use more debug-flags when compiling +#DEBUG = 1 + +################################################################### +# FIRMWARE CONFIGURATION +# + +# default baudrate +#CONFIG_SERIAL_BAUDRATE = 19200 diff --git a/firmware/fnordlicht-bootloader/fboot.c b/firmware/fnordlicht-bootloader/fboot.c new file mode 100644 index 0000000..fb06fd7 --- /dev/null +++ b/firmware/fnordlicht-bootloader/fboot.c @@ -0,0 +1,348 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlichtmini serial bootloader + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "../common/io.h" +#include "../common/remote-proto.h" +#include "../common/common.h" +#include "uart.h" +#include "global.h" + +struct global_t +{ + /* for bus synchronization */ + uint8_t synced; + uint8_t sync_len; + + /* own device address */ + uint8_t address; + + /* for crc checking */ + bool crc_match; + uint8_t delay; + + /* for enter_application command */ + bool request_exit; + uint8_t exit_delay; + + /* for internal message buffer */ + uint8_t len; + union { + uint8_t buf[REMOTE_MSG_LEN]; + struct remote_msg_t msg; + }; + + /* length and address for data buffer */ + uint16_t data_len; + uint8_t *data_address; + union { + uint8_t data_buf[CONFIG_BOOTLOADER_BUFSIZE]; + uint16_t data_buf16[CONFIG_BOOTLOADER_BUFSIZE/2]; + }; +}; + +struct global_t __global; +#define global (&__global) + +/* disable watchdog - NEVER CALL DIRECTLY! */ +void disable_watchdog(void) \ + __attribute__((naked)) \ + __attribute__((section(".init3"))); +void disable_watchdog(void) +{ + MCUSR = 0; + wdt_disable(); +} + +static void (*jump_to_application)(void) = 0; + +static void start_application(void) __attribute__((noreturn)); +static void start_application(void) +{ + /* pwm pins */ + P_PORT = 0; + P_DDR = 0; + + /* in pin */ + R_PORT = 0; + R_DDR = 0; + + /* uart */ + _UBRRH_UART0 = 0; + _UBRRL_UART0 = 0; + _UCSRA_UART0 = _BV(_TXC_UART0); + _UCSRB_UART0 = 0; + + /* timer1 */ + TCCR1B = 0; + OCR1A = 0; + TCNT1 = 0; + _TIFR_TIMER1 = _TIFR_TIMER1; + + /* move interrupt vectors and start real application */ + jump_to_application(); +} + +static void parse_boot_config(struct remote_msg_boot_config_t *msg) +{ + /* remember flash address */ + global->data_address = (uint8_t *)msg->start_address; +} + +static void parse_data(struct remote_msg_boot_data_t *msg) +{ + uint8_t len = sizeof(msg->data); + if (global->data_len + len > CONFIG_BOOTLOADER_BUFSIZE) + len = CONFIG_BOOTLOADER_BUFSIZE - global->data_len; + + memcpy(&global->data_buf[global->data_len], msg->data, len); + global->data_len += len; +} + +static void parse_crc(struct remote_msg_boot_crc_check_t *msg) +{ + /* compute crc16 over buffer */ + uint16_t checksum = 0xffff; + + uint8_t *ptr = &global->data_buf[0]; + uint16_t i = msg->len+1; + while (--i) + checksum = _crc16_update(checksum, *ptr++); + + if (checksum != msg->checksum) { + global->delay = msg->delay; + global->crc_match = false; + } else + global->crc_match = true; +} + +static void parse_crc_flash(struct remote_msg_boot_crc_flash_t *msg) +{ + /* compute crc16 over flash */ + uint16_t checksum = 0xffff; + + uint8_t *address = (uint8_t *)msg->start; + uint16_t i = msg->len+1; + while (--i) { + uint8_t data = pgm_read_byte(address++); + checksum = _crc16_update(checksum, data); + } + + if (checksum != msg->checksum) { + global->delay = msg->delay; + global->crc_match = false; + } else + global->crc_match = true; +} + +static void flash(void) +{ + /* pull int */ + R_DDR |= _BV(INTPIN); + + uint8_t *addr = global->data_address; + uint16_t *data = &global->data_buf16[0]; + + uint8_t last_page = global->data_len/SPM_PAGESIZE; + if (global->data_len % SPM_PAGESIZE) + last_page++; + + for (uint8_t page = 0; page < last_page; page++) + { + PWM_PIN_ON(PWM_GREEN); + + /* erase page */ + boot_page_erase(addr); + boot_spm_busy_wait(); + + for (uint16_t i = 0; i < SPM_PAGESIZE; i += 2) { + /* fill internal buffer */ + boot_page_fill(addr+i, *data++); + boot_spm_busy_wait(); + } + + /* after filling the temp buffer, write the page and wait till we're done */ + boot_page_write(addr); + boot_spm_busy_wait(); + + /* re-enable application flash section, so we can read it again */ + boot_rww_enable(); + + PWM_PIN_OFF(PWM_GREEN); + addr += SPM_PAGESIZE; + } + + global->data_address = addr; + + /* release int */ + R_DDR &= ~_BV(INTPIN); +} + +static void remote_parse_msg(struct remote_msg_t *msg) +{ + /* verify address */ + if (msg->address != global->address && msg->address != REMOTE_ADDR_BROADCAST) + return; + + /* parse command */ + switch (msg->cmd) { + case REMOTE_CMD_BOOT_CONFIG: parse_boot_config((struct remote_msg_boot_config_t *)msg); + break; + case REMOTE_CMD_BOOT_INIT: global->data_len = 0; + break; + case REMOTE_CMD_BOOT_DATA: parse_data((struct remote_msg_boot_data_t *)msg); + break; + case REMOTE_CMD_CRC_CHECK: parse_crc((struct remote_msg_boot_crc_check_t *)msg); + break; + case REMOTE_CMD_CRC_FLASH: parse_crc_flash((struct remote_msg_boot_crc_flash_t *)msg); + break; + case REMOTE_CMD_FLASH: flash(); + break; + case REMOTE_CMD_ENTER_APP: global->request_exit = true; + global->exit_delay = CONFIG_EXIT_DELAY; + break; + } + + if (global->delay && global->crc_match == false) { + /* pull int to gnd */ + R_DDR |= _BV(INTPIN); + + PWM_PIN_ON(PWM_RED); + } +} + +static void remote_poll(void) +{ + /* wait for a byte */ + if (uart_receive_complete()) + { + uint8_t data = uart_getc(); + + /* check if sync sequence has been received before */ + if (global->sync_len == REMOTE_SYNC_LEN) { + /* synced, safe address and send next address to following device */ + global->address = data; + uart_putc(data+1); + + /* reset buffer */ + global->len = 0; + + /* enable remote command thread */ + global->synced = 1; + } else { + /* just pass through data */ + uart_putc(data); + + /* put data into remote buffer + * (just processed if remote.synced == 1) */ + if (global->len < sizeof(global->buf)) + global->buf[global->len++] = data; + } + + /* remember the number of sync bytes received so far */ + if (data == REMOTE_CMD_RESYNC) + global->sync_len++; + else + global->sync_len = 0; + } + + if (global->synced && global->len == REMOTE_MSG_LEN) { + remote_parse_msg(&global->msg); + global->len = 0; + } +} + +static void check_startup(void) +{ + /* check for valid reset vector */ + if (pgm_read_word(NULL) == 0xffff) + return; + + /* configure pullup resistor at int pin */ + R_PORT |= _BV(INTPIN); + + /* sleep 100ms (TCNT1 == 2, -> TCNT1L == 2) */ + while (TCNT1L < 2); + + /* if int pin is pulled down, remain in bootloader */ + if (!(R_PIN & _BV(INTPIN))) + return; + + /* else cleanup and start application */ + start_application(); +} + +int __attribute__ ((noreturn,OS_main)) main(void) +{ + /* initialize timer1, CTC at 50ms, prescaler 1024 */ + OCR1A = F_CPU/1024/20; + TCCR1B = _BV(WGM12) | _BV(CS12) | _BV(CS10); + + /* start application if necessary */ + check_startup(); + + /* configure and enable uart */ + uart_init(); + + /* configure outputs */ + P_DDR = PWM_CHANNEL_MASK; + +#ifdef PWM_INVERTED + P_PORT = PWM_CHANNEL_MASK; +#endif + + while(1) { + remote_poll(); + + if (_TIFR_TIMER1 & _BV(OCF1A)) { + _TIFR_TIMER1 = _BV(OCF1A); + + static uint8_t c; + if (c++ == 20) { + /* blink */ + PWM_PIN_TOGGLE(PWM_BLUE); + c = 0; + } + + /* if int is pulled, decrement delay */ + if (R_DDR & _BV(INTPIN)) { + if (--global->delay == 0) { + /* release int */ + R_DDR &= ~_BV(INTPIN); + P_PORT &= ~1; + } + } + + /* exit (after exit_delay) if requested */ + if (global->request_exit && global->exit_delay-- == 0) + start_application(); + } + } +} diff --git a/firmware/fnordlicht-bootloader/global.h b/firmware/fnordlicht-bootloader/global.h new file mode 100644 index 0000000..170de6a --- /dev/null +++ b/firmware/fnordlicht-bootloader/global.h @@ -0,0 +1,79 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlichtmini serial bootloader + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __GLOBAL_H +#define __GLOBAL_H + +#include "config.h" + +/* configure primary pwm pins (MUST be three successive pins in a port) */ +#define PWM_PORT B +#define PWM_CHANNELS 3 +#define PWM_CHANNEL_MASK (_BV(PB0) | _BV(PB1) | _BV(PB2)) +#define PWM_SHIFT 0 + +/* configure INT pin */ +#define REMOTE_INT_PORT D +#define REMOTE_INT_PIN PD2 + +/* abbreviations for port, ddr and pin */ +#define P_PORT _OUTPORT(PWM_PORT) +#define P_DDR _DDRPORT(PWM_PORT) +#define R_PORT _OUTPORT(REMOTE_INT_PORT) +#define R_DDR _DDRPORT(REMOTE_INT_PORT) +#define R_PIN _INPORT(REMOTE_INT_PORT) +#define INTPIN REMOTE_INT_PIN + +/* check if hardware is valid */ +#if defined(HARDWARE_fnordlicht) + /* specific settings for old fnordlicht hardware */ + #if !defined(PWM_INVERTED) + #define PWM_INVERTED + #endif +#elif defined(HARDWARE_fnordlichtmini) + /* specific settings for fnordlichtmini hardware */ + +#else +#error "unknown HARDWARE platform!" +#endif + +/* turn pwm output pins on and off */ +#ifdef PWM_INVERTED +#define PWM_PIN_ON(bit) P_PORT &= ~_BV(bit) << (PWM_SHIFT) +#define PWM_PIN_OFF(bit) P_PORT |= _BV(bit) << (PWM_SHIFT) +#else +#define PWM_PIN_ON(bit) P_PORT |= _BV(bit) << (PWM_SHIFT) +#define PWM_PIN_OFF(bit) P_PORT &= ~_BV(bit) << (PWM_SHIFT) +#endif + +#define PWM_PIN_TOGGLE(bit) P_PORT ^= _BV(bit) << (PWM_SHIFT) + +#define PWM_RED 0 +#define PWM_GREEN 1 +#define PWM_BLUE 2 + +/* wait 100ms before checking INT pin (for delay_loop_2(50000)) */ +#define CONFIG_DELAY F_CPU/4/50000/10 +#define CONFIG_DELAY_LOOP2 50000 + +#endif diff --git a/firmware/fnordlicht-bootloader/interrupts.S b/firmware/fnordlicht-bootloader/interrupts.S new file mode 100644 index 0000000..b72f678 --- /dev/null +++ b/firmware/fnordlicht-bootloader/interrupts.S @@ -0,0 +1,13 @@ +.extern __init +.global __vector_default +.section .vectors.bootloader + +/* micro-jumptable, we are using just the reset vector */ +exit: +__vector_default: + /* use jmp if available on this architecture */ + #ifdef __AVR_HAVE_JMP_CALL__ + jmp __init + #else + rjmp __init + #endif diff --git a/firmware/fnordlicht-bootloader/io.h b/firmware/fnordlicht-bootloader/io.h new file mode 100644 index 0000000..f91ae68 --- /dev/null +++ b/firmware/fnordlicht-bootloader/io.h @@ -0,0 +1,84 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef IO_H +#define IO_H + +#include + +/* cpu specific configuration registers */ +#if defined(__AVR_ATmega8__) + +#define _TIMSK_TIMER1 TIMSK +#define _UCSRB_UART0 UCSRB +#define _UCSRA_UART0 UCSRA +#define _UDRIE_UART0 UDRIE +#define _RXC_UART0 RXC +#define _TXEN_UART0 TXEN +#define _RXEN_UART0 RXEN +#define _RXCIE_UART0 RXCIE +#define _UBRRH_UART0 UBRRH +#define _UBRRL_UART0 UBRRL +#define _UCSRC_UART0 UCSRC +#define _UCSZ0_UART0 UCSZ0 +#define _UCSZ1_UART0 UCSZ1 +#define _SIG_UART_RECV_UART0 SIG_UART_RECV +#define _SIG_UART_DATA_UART0 SIG_UART_DATA +#define _UDR_UART0 UDR +#define UCSR0A UCSRA +#define UCSR0C UCSRC +#define MPCM0 MPCM +#define UCSZ00 UCSZ0 +#define UCSZ01 UCSZ1 +#define UCSZ02 UCSZ2 +#define UBRR0H UBRRH +#define UBRR0L UBRRL +#define UCSR0B UCSRB +#define RXEN0 RXEN +#define TXEN0 TXEN +#define RXC0 RXC +#define RXB80 RXB8 +#define UDR0 UDR + +#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) + +#define _TIMSK_TIMER1 TIMSK1 +#define _UCSRA_UART0 UCSR0A +#define _UCSRB_UART0 UCSR0B +#define _UDRIE_UART0 UDRIE0 +#define _RXC_UART0 RXC0 +#define _TXEN_UART0 TXEN0 +#define _RXEN_UART0 RXEN0 +#define _RXCIE_UART0 RXCIE0 +#define _UBRRH_UART0 UBRR0H +#define _UBRRL_UART0 UBRR0L +#define _UCSRC_UART0 UCSR0C +#define _UCSZ0_UART0 UCSZ00 +#define _UCSZ1_UART0 UCSZ01 +#define _SIG_UART_RECV_UART0 SIG_USART_RECV +#define _SIG_UART_DATA_UART0 SIG_USART_DATA +#define _UDR_UART0 UDR0 +#endif + + +#endif diff --git a/firmware/fnordlicht-bootloader/ldscripts/avr4.x b/firmware/fnordlicht-bootloader/ldscripts/avr4.x new file mode 100644 index 0000000..cc00f38 --- /dev/null +++ b/firmware/fnordlicht-bootloader/ldscripts/avr4.x @@ -0,0 +1,232 @@ +/* Default linker script, for normal executables */ +OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr") +OUTPUT_ARCH(avr:4) +MEMORY +{ + text (rx) : ORIGIN = 0, LENGTH = 8K + data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0 + eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K + fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K + lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K + signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K +} +SECTIONS +{ + /* Read-only sections, merged into text segment: */ + .hash : { *(.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .rel.init : { *(.rel.init) } + .rela.init : { *(.rela.init) } + .rel.text : + { + *(.rel.text) + *(.rel.text.*) + *(.rel.gnu.linkonce.t*) + } + .rela.text : + { + *(.rela.text) + *(.rela.text.*) + *(.rela.gnu.linkonce.t*) + } + .rel.fini : { *(.rel.fini) } + .rela.fini : { *(.rela.fini) } + .rel.rodata : + { + *(.rel.rodata) + *(.rel.rodata.*) + *(.rel.gnu.linkonce.r*) + } + .rela.rodata : + { + *(.rela.rodata) + *(.rela.rodata.*) + *(.rela.gnu.linkonce.r*) + } + .rel.data : + { + *(.rel.data) + *(.rel.data.*) + *(.rel.gnu.linkonce.d*) + } + .rela.data : + { + *(.rela.data) + *(.rela.data.*) + *(.rela.gnu.linkonce.d*) + } + .rel.ctors : { *(.rel.ctors) } + .rela.ctors : { *(.rela.ctors) } + .rel.dtors : { *(.rel.dtors) } + .rela.dtors : { *(.rela.dtors) } + .rel.got : { *(.rel.got) } + .rela.got : { *(.rela.got) } + .rel.bss : { *(.rel.bss) } + .rela.bss : { *(.rela.bss) } + .rel.plt : { *(.rel.plt) } + .rela.plt : { *(.rela.plt) } + + /* discard default interrupt jumptable */ + /DISCARD/ : { *(.vectors) } + + /* Internal text space or external memory. */ + .text : + { + *(.vectors.bootloader) + KEEP(*(.vectors.bootloader)) + /* For data that needs to reside in the lower 64k of progmem. */ + *(.progmem.gcc*) + *(.progmem*) + . = ALIGN(2); + __trampolines_start = . ; + /* The jump trampolines for the 16-bit limited relocs will reside here. */ + *(.trampolines) + *(.trampolines*) + __trampolines_end = . ; + /* For future tablejump instruction arrays for 3 byte pc devices. + We don't relax jump/call instructions within these sections. */ + *(.jumptables) + *(.jumptables*) + /* For code that needs to reside in the lower 128k progmem. */ + *(.lowtext) + *(.lowtext*) + __ctors_start = . ; + *(.ctors) + __ctors_end = . ; + __dtors_start = . ; + *(.dtors) + __dtors_end = . ; + KEEP(SORT(*)(.ctors)) + KEEP(SORT(*)(.dtors)) + /* From this point on, we don't bother about wether the insns are + below or above the 16 bits boundary. */ + *(.init0) /* Start here after reset. */ + KEEP (*(.init0)) + *(.init1) + KEEP (*(.init1)) + *(.init2) /* Clear __zero_reg__, set up stack pointer. */ + KEEP (*(.init2)) + *(.init3) + KEEP (*(.init3)) + *(.init4) /* Initialize data and BSS. */ + KEEP (*(.init4)) + *(.init5) + KEEP (*(.init5)) + *(.init6) /* C++ constructors. */ + KEEP (*(.init6)) + *(.init7) + KEEP (*(.init7)) + *(.init8) + KEEP (*(.init8)) + *(.init9) /* Call main(). */ + KEEP (*(.init9)) + *(.text) + . = ALIGN(2); + *(.text.*) + . = ALIGN(2); + *(.fini9) /* _exit() starts here. */ + KEEP (*(.fini9)) + *(.fini8) + KEEP (*(.fini8)) + *(.fini7) + KEEP (*(.fini7)) + *(.fini6) /* C++ destructors. */ + KEEP (*(.fini6)) + *(.fini5) + KEEP (*(.fini5)) + *(.fini4) + KEEP (*(.fini4)) + *(.fini3) + KEEP (*(.fini3)) + *(.fini2) + KEEP (*(.fini2)) + *(.fini1) + KEEP (*(.fini1)) + *(.fini0) /* Infinite loop after program termination. */ + KEEP (*(.fini0)) + _etext = . ; + } > text + .data : AT (ADDR (.text) + SIZEOF (.text)) + { + PROVIDE (__data_start = .) ; + *(.data) + *(.data*) + *(.rodata) /* We need to include .rodata here if gcc is used */ + *(.rodata*) /* with -fdata-sections. */ + *(.gnu.linkonce.d*) + . = ALIGN(2); + _edata = . ; + PROVIDE (__data_end = .) ; + } > data + .bss : AT (ADDR (.bss)) + { + PROVIDE (__bss_start = .) ; + *(.bss) + *(.bss*) + *(COMMON) + PROVIDE (__bss_end = .) ; + } > data + __data_load_start = LOADADDR(.data); + __data_load_end = __data_load_start + SIZEOF(.data); + /* Global data not cleared after reset. */ + .noinit : + { + PROVIDE (__noinit_start = .) ; + *(.noinit*) + PROVIDE (__noinit_end = .) ; + _end = . ; + PROVIDE (__heap_start = .) ; + } > data + .eeprom : + { + *(.eeprom*) + __eeprom_end = . ; + } > eeprom + .fuse : + { + KEEP(*(.fuse)) + KEEP(*(.lfuse)) + KEEP(*(.hfuse)) + KEEP(*(.efuse)) + } > fuse + .lock : + { + KEEP(*(.lock*)) + } > lock + .signature : + { + KEEP(*(.signature*)) + } > signature + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +} diff --git a/firmware/fnordlicht-bootloader/ldscripts/avr5.x b/firmware/fnordlicht-bootloader/ldscripts/avr5.x new file mode 100644 index 0000000..ce55d5b --- /dev/null +++ b/firmware/fnordlicht-bootloader/ldscripts/avr5.x @@ -0,0 +1,232 @@ +/* Default linker script, for normal executables */ +OUTPUT_FORMAT("elf32-avr","elf32-avr","elf32-avr") +OUTPUT_ARCH(avr:5) +MEMORY +{ + text (rx) : ORIGIN = 0, LENGTH = 128K + data (rw!x) : ORIGIN = 0x800060, LENGTH = 0xffa0 + eeprom (rw!x) : ORIGIN = 0x810000, LENGTH = 64K + fuse (rw!x) : ORIGIN = 0x820000, LENGTH = 1K + lock (rw!x) : ORIGIN = 0x830000, LENGTH = 1K + signature (rw!x) : ORIGIN = 0x840000, LENGTH = 1K +} +SECTIONS +{ + /* Read-only sections, merged into text segment: */ + .hash : { *(.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .gnu.version : { *(.gnu.version) } + .gnu.version_d : { *(.gnu.version_d) } + .gnu.version_r : { *(.gnu.version_r) } + .rel.init : { *(.rel.init) } + .rela.init : { *(.rela.init) } + .rel.text : + { + *(.rel.text) + *(.rel.text.*) + *(.rel.gnu.linkonce.t*) + } + .rela.text : + { + *(.rela.text) + *(.rela.text.*) + *(.rela.gnu.linkonce.t*) + } + .rel.fini : { *(.rel.fini) } + .rela.fini : { *(.rela.fini) } + .rel.rodata : + { + *(.rel.rodata) + *(.rel.rodata.*) + *(.rel.gnu.linkonce.r*) + } + .rela.rodata : + { + *(.rela.rodata) + *(.rela.rodata.*) + *(.rela.gnu.linkonce.r*) + } + .rel.data : + { + *(.rel.data) + *(.rel.data.*) + *(.rel.gnu.linkonce.d*) + } + .rela.data : + { + *(.rela.data) + *(.rela.data.*) + *(.rela.gnu.linkonce.d*) + } + .rel.ctors : { *(.rel.ctors) } + .rela.ctors : { *(.rela.ctors) } + .rel.dtors : { *(.rel.dtors) } + .rela.dtors : { *(.rela.dtors) } + .rel.got : { *(.rel.got) } + .rela.got : { *(.rela.got) } + .rel.bss : { *(.rel.bss) } + .rela.bss : { *(.rela.bss) } + .rel.plt : { *(.rel.plt) } + .rela.plt : { *(.rela.plt) } + + /* discard default interrupt jumptable */ + /DISCARD/ : { *(.vectors) } + + /* Internal text space or external memory. */ + .text : + { + *(.vectors.bootloader) + KEEP(*(.vectors.bootloader)) + /* For data that needs to reside in the lower 64k of progmem. */ + *(.progmem.gcc*) + *(.progmem*) + . = ALIGN(2); + __trampolines_start = . ; + /* The jump trampolines for the 16-bit limited relocs will reside here. */ + *(.trampolines) + *(.trampolines*) + __trampolines_end = . ; + /* For future tablejump instruction arrays for 3 byte pc devices. + We don't relax jump/call instructions within these sections. */ + *(.jumptables) + *(.jumptables*) + /* For code that needs to reside in the lower 128k progmem. */ + *(.lowtext) + *(.lowtext*) + __ctors_start = . ; + *(.ctors) + __ctors_end = . ; + __dtors_start = . ; + *(.dtors) + __dtors_end = . ; + KEEP(SORT(*)(.ctors)) + KEEP(SORT(*)(.dtors)) + /* From this point on, we don't bother about wether the insns are + below or above the 16 bits boundary. */ + *(.init0) /* Start here after reset. */ + KEEP (*(.init0)) + *(.init1) + KEEP (*(.init1)) + *(.init2) /* Clear __zero_reg__, set up stack pointer. */ + KEEP (*(.init2)) + *(.init3) + KEEP (*(.init3)) + *(.init4) /* Initialize data and BSS. */ + KEEP (*(.init4)) + *(.init5) + KEEP (*(.init5)) + *(.init6) /* C++ constructors. */ + KEEP (*(.init6)) + *(.init7) + KEEP (*(.init7)) + *(.init8) + KEEP (*(.init8)) + *(.init9) /* Call main(). */ + KEEP (*(.init9)) + *(.text) + . = ALIGN(2); + *(.text.*) + . = ALIGN(2); + *(.fini9) /* _exit() starts here. */ + KEEP (*(.fini9)) + *(.fini8) + KEEP (*(.fini8)) + *(.fini7) + KEEP (*(.fini7)) + *(.fini6) /* C++ destructors. */ + KEEP (*(.fini6)) + *(.fini5) + KEEP (*(.fini5)) + *(.fini4) + KEEP (*(.fini4)) + *(.fini3) + KEEP (*(.fini3)) + *(.fini2) + KEEP (*(.fini2)) + *(.fini1) + KEEP (*(.fini1)) + *(.fini0) /* Infinite loop after program termination. */ + KEEP (*(.fini0)) + _etext = . ; + } > text + .data : AT (ADDR (.text) + SIZEOF (.text)) + { + PROVIDE (__data_start = .) ; + *(.data) + *(.data*) + *(.rodata) /* We need to include .rodata here if gcc is used */ + *(.rodata*) /* with -fdata-sections. */ + *(.gnu.linkonce.d*) + . = ALIGN(2); + _edata = . ; + PROVIDE (__data_end = .) ; + } > data + .bss : AT (ADDR (.bss)) + { + PROVIDE (__bss_start = .) ; + *(.bss) + *(.bss*) + *(COMMON) + PROVIDE (__bss_end = .) ; + } > data + __data_load_start = LOADADDR(.data); + __data_load_end = __data_load_start + SIZEOF(.data); + /* Global data not cleared after reset. */ + .noinit : + { + PROVIDE (__noinit_start = .) ; + *(.noinit*) + PROVIDE (__noinit_end = .) ; + _end = . ; + PROVIDE (__heap_start = .) ; + } > data + .eeprom : + { + *(.eeprom*) + __eeprom_end = . ; + } > eeprom + .fuse : + { + KEEP(*(.fuse)) + KEEP(*(.lfuse)) + KEEP(*(.hfuse)) + KEEP(*(.efuse)) + } > fuse + .lock : + { + KEEP(*(.lock*)) + } > lock + .signature : + { + KEEP(*(.signature*)) + } > signature + /* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } + /* DWARF debug sections. + Symbols in the DWARF debugging sections are relative to the beginning + of the section so we begin them at 0. */ + /* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } + /* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } + /* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } + /* DWARF 2 */ + .debug_info 0 : { *(.debug_info) *(.gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +} diff --git a/firmware/fnordlicht-bootloader/uart.h b/firmware/fnordlicht-bootloader/uart.h new file mode 100644 index 0000000..e305f37 --- /dev/null +++ b/firmware/fnordlicht-bootloader/uart.h @@ -0,0 +1,82 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef UART_H +#define UART_H + +#include + + +/* define uart mode (8N1) */ +#if defined(__AVR_ATmega8__) +/* in atmega8, we need a special switching bit + * for addressing UCSRC */ +#define UART_UCSRC _BV(URSEL) | _BV(UCSZ0) | _BV(UCSZ1) + +#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) +/* in atmega88, this isn't needed any more */ +#define UART_UCSRC _BV(_UCSZ0_UART0) | _BV(_UCSZ1_UART0) +#endif + + +static inline void uart_init(void) +{ + #define BAUD CONFIG_SERIAL_BAUDRATE + #include + /* set baud rate */ + _UBRRH_UART0 = UBRRH_VALUE; + _UBRRL_UART0 = UBRRL_VALUE; + + #if USE_2X + _UCSRA_UART0 |= (1 << _U2X_UART0); + #endif + + /* set mode */ + _UCSRC_UART0 = UART_UCSRC; + + /* enable transmitter and receiver */ + _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0); +} + +static inline bool uart_receive_complete(void) +{ + return _UCSRA_UART0 & _BV(_RXC_UART0); +} + +static inline bool uart_send_complete(void) +{ + return _BV(_UDRE_UART0) & _UCSRA_UART0; +} + +static inline uint8_t uart_getc(void) +{ + return _UDR_UART0; +} + +static void uart_putc(uint8_t data) +{ + while(!(_BV(_UDRE_UART0) & _UCSRA_UART0)); + UDR0 = data; +} + +#endif diff --git a/firmware/fnordlicht-controller/Makefile b/firmware/fnordlicht-controller/Makefile new file mode 100644 index 0000000..7149a2f --- /dev/null +++ b/firmware/fnordlicht-controller/Makefile @@ -0,0 +1,199 @@ +#################################################### +# fnordlicht-ng Makefile +#################################################### + +# ATTENTION: +# Any of these variables are overridden with the values from the file +# "config.mk", you can tune your settings there. The file "config.mk" is not +# under version control. +# Just run 'make' to create a "config.mk" with the default settings + +# controller +MCU = atmega168 + +# frequency +F_CPU = 16000000UL + +# main application name (without .hex) +# eg 'test' when the main function is defined in 'test.c' +TARGET = fcontrol + +# c sourcecode files +# eg. 'test.c foo.c foobar/baz.c' +SRC = $(wildcard *.c) usbdrv/usbdrv.c + +# asm sourcecode files +# eg. 'interrupts.S foobar/another.S' +ASRC = $(wildcard *.S) usbdrv/usbdrvasm.S + +# headers which should be considered when recompiling +# eg. 'global.h foobar/important.h' +HEADERS = $(wildcard *.h) + +# include directories (used for both, c and asm) +# eg '. usbdrv/' +INCLUDES = . usbdrv/ + + +# use more debug-flags when compiling +DEBUG = 0 + +# default baudrate +CONFIG_SERIAL_BAUDRATE = 19200 + +# avrdude programmer protocol +PROG = usbasp +# avrdude programmer device +DEV = usb +# further flags for avrdude +AVRDUDE_FLAGS = + +CFLAGS += -DCONFIG_SERIAL_BAUDRATE=$(CONFIG_SERIAL_BAUDRATE) + +# use a custom linker script +LDFLAGS += -L$(CURDIR)/ldscripts + +.PHONY: all + +# main make target (moved up here because of the config.mk target) +all: $(TARGET).hex + +# create config.mk (if it does not exist yet) +$(CURDIR)/config.mk: + @$(CP) config.mk.template config.mk + @echo "====================================================" + @echo "created file $@" + @echo "please tune your settings there, then run 'make' again" + @echo "====================================================" + @exit 1 + +# include config file +-include $(CURDIR)/config.mk + +#################################################### +# 'make' configuration +#################################################### +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AS = avr-as +SIZE = avr-size +CP = cp +RM = rm -f +RMDIR = rm -rf +MKDIR = mkdir +AVRDUDE = avrdude + +# flags for automatic dependency handling +DEPFLAGS = -MD -MP -MF .dep/$(@F).d + +# flags for the compiler (for .c files) +CFLAGS += -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -std=gnu99 -fshort-enums $(DEPFLAGS) +CFLAGS += $(addprefix -I,$(INCLUDES)) +CFLAGS += -fno-split-wide-types +CFLAGS += --param inline-call-cost=2 -finline-limit=3 -fno-inline-small-functions +#CFLAGS += -Wl,--relax + +# flags for the compiler (for .S files) +ASFLAGS += -g -mmcu=$(MCU) -DF_CPU=$(F_CPU) -x assembler-with-cpp $(DEPFLAGS) +ASFLAGS += $(addprefix -I,$(INCLUDES)) + +# flags for the linker +LDFLAGS += -mmcu=$(MCU) + +# fill in object files +OBJECTS += $(SRC:.c=.o) +OBJECTS += $(ASRC:.S=.o) + +# include more debug flags, if $(DEBUG) is 1 +ifeq ($(DEBUG),1) + CFLAGS += -Wall -W -Wchar-subscripts -Wmissing-prototypes + CFLAGS += -Wmissing-declarations -Wredundant-decls + CFLAGS += -Wstrict-prototypes -Wshadow -Wbad-function-cast + CFLAGS += -Winline -Wpointer-arith -Wsign-compare + CFLAGS += -Wunreachable-code -Wdisabled-optimization + CFLAGS += -Wcast-align -Wwrite-strings -Wnested-externs -Wundef + CFLAGS += -Wa,-adhlns=$(basename $@).lst + CFLAGS += -DCONFIG_DEBUG=1 +endif + +#################################################### +# avrdude configuration +#################################################### +ifeq ($(MCU),atmega8) + AVRDUDE_MCU=m8 +endif +ifeq ($(MCU),atmega48) + AVRDUDE_MCU=m48 +endif +ifeq ($(MCU),atmega88) + AVRDUDE_MCU=m88 +endif +ifeq ($(MCU),atmega168) + AVRDUDE_MCU=m168 +endif + +AVRDUDE_FLAGS += -p $(AVRDUDE_MCU) + +#################################################### +# make targets +#################################################### + +.PHONY: clean distclean avrdude-terminal + +$(TARGET).elf: $(OBJECTS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJECTS) + +# all objects (.o files) and config.mk +$(OBJECTS): $(HEADERS) config.mk + +# remove all compiled files +clean: + $(RM) $(foreach ext,elf hex eep.hex map,$(TARGET).$(ext)) \ + $(foreach file,$(patsubst %.o,%,$(OBJECTS)),$(foreach ext,o lst lss,$(file).$(ext))) + +# additionally remove the dependency makefile and config.mk +distclean: clean + $(RMDIR) .dep + $(RM) config.mk + +# avrdude-related targets +install program: program-$(TARGET) + +avrdude-terminal: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -t + +program-%: %.hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U flash:w:$< + +program-eeprom-%: %.eep.hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U eeprom:w:$< + +# special programming targets +%.hex: %.elf + $(OBJCOPY) -O ihex -R .eeprom $< $@ + @echo "====================================================" + @echo "$@ compiled for fnordlicht-controller" + @echo "using controller $(MCU)" + @echo -n "size for $< is " + @$(SIZE) -A $@ | grep '\.sec1' | tr -s ' ' | cut -d" " -f2 + @echo "====================================================" + +%.eep.hex: %.elf + $(OBJCOPY) --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex -j .eeprom $< $@ + +%.lss: %.elf + $(OBJDUMP) -h -S $< > $@ + +.PHONY: fuses-atmega8-fnordlichtmini-with-bootloader fuses-lock + +fuses-atmega8-fnordlichtmini-with-bootloader: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lfuse:w:0x3f:m -U hfuse:w:0xda:m + +fuses-atmega8-fnordlichtmini-without-bootloader: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lfuse:w:0x3f:m -U hfuse:w:0xd9:m + +fuses-lock: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lock:w:0x2f:m + +-include $(shell $(MKDIR) .dep 2>/dev/null) $(wildcard .dep/*) diff --git a/firmware/fnordlicht-controller/config.h b/firmware/fnordlicht-controller/config.h new file mode 100644 index 0000000..5f16469 --- /dev/null +++ b/firmware/fnordlicht-controller/config.h @@ -0,0 +1,40 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/* + * ATTENTION: + * This file is under version control. Please use the file "config.mk" (which + * is not under version control) for configuring the fnordlicht firmware. + * A file with all the default values will be created by running 'make' the + * first time. + */ + +#ifndef _FNORDLICHT_CONFIG_H +#define _FNORDLICHT_CONFIG_H + +/* debug defines */ +#ifndef CONFIG_DEBUG +#define CONFIG_DEBUG 0 +#endif + +#endif /* _FNORDLICHT_CONFIG_H */ diff --git a/firmware/fnordlicht-controller/config.mk.template b/firmware/fnordlicht-controller/config.mk.template new file mode 100644 index 0000000..7798544 --- /dev/null +++ b/firmware/fnordlicht-controller/config.mk.template @@ -0,0 +1,39 @@ +# Put your own config here! +# +# This file is not under version control, and your settings +# will not be modified when upgrading the repository + + +################################################################### +# PROGRAMMER +# +# avrdude programmer protocol +#PROG = usbasp + +# avrdude programmer device +#DEV = usb + +# further flags for avrdude +#AVRDUDE_FLAGS = + + +################################################################### +# controller +# +# possible values: +# - atmega168 +# +#MCU = atmega168 + +# frequency +#F_CPU = 16000000UL + +# use more debug-flags when compiling +#DEBUG = 1 + +################################################################### +# FIRMWARE CONFIGURATION +# + +# default baudrate +#CONFIG_SERIAL_BAUDRATE = 19200 diff --git a/firmware/fnordlicht-controller/fcontrol.c b/firmware/fnordlicht-controller/fcontrol.c new file mode 100644 index 0000000..f5bce8f --- /dev/null +++ b/firmware/fnordlicht-controller/fcontrol.c @@ -0,0 +1,73 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/* includes */ +#include "globals.h" +#include "../common/io.h" + +#include +#include +#include +#include + +#include "../common/common.h" +#include "usb.h" +#include "timer.h" +#include "uart.h" +#include "ui.h" +#include "ir.h" + +/* NEVER CALL DIRECTLY! */ +void disable_watchdog(void) \ + __attribute__((naked)) \ + __attribute__((section(".init3"))); +void disable_watchdog(void) +{ + MCUSR = 0; + wdt_disable(); +} + +/** main function + */ +int main(void) +{ + usb_init(); + timer_init(); + uart_init(); + ui_init(); + ir_init(); + ir_set_mode(IR_RECEIVE); + + /* enable interrupts globally */ + sei(); + + usb_enable(); + + ui_blink(0x03, 0); + + while (1) { + usb_poll(); + ui_poll(); + ir_poll(); + } +} diff --git a/firmware/fnordlicht-controller/fifo.c b/firmware/fnordlicht-controller/fifo.c new file mode 100644 index 0000000..e20696b --- /dev/null +++ b/firmware/fnordlicht-controller/fifo.c @@ -0,0 +1,61 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "fifo.h" + +void fifo_init(fifo_t *f) +{ + f->read = 0; + f->write = 0; +} + +void fifo_enqueue(fifo_t *f, fifo_content_t data) +{ + f->buffer[f->write] = data; + f->write = (f->write + 1) % CONFIG_FIFO_SIZE; +} + +fifo_content_t fifo_dequeue(fifo_t *f) +{ + fifo_content_t data = f->buffer[f->read]; + f->read = (f->read + 1) % CONFIG_FIFO_SIZE; + return data; +} + +fifo_size_t fifo_fill(fifo_t *f) +{ + if (f->write >= f->read) + return f->write - f->read; + else + return CONFIG_FIFO_SIZE - (f->read - f->write); +} + +bool fifo_empty(fifo_t *f) +{ + return f->read == f->write; +} + +bool fifo_full(fifo_t *f) +{ + return fifo_fill(f) == CONFIG_FIFO_SIZE-1; +} diff --git a/firmware/fnordlicht-controller/fifo.h b/firmware/fnordlicht-controller/fifo.h new file mode 100644 index 0000000..c54035a --- /dev/null +++ b/firmware/fnordlicht-controller/fifo.h @@ -0,0 +1,48 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __FIFO_H +#define __FIFO_H + +#include +#include +#include "globals.h" + +typedef uint8_t fifo_content_t; +typedef uint8_t fifo_size_t; + +typedef struct +{ + fifo_size_t read; + fifo_size_t write; + fifo_content_t buffer[CONFIG_FIFO_SIZE]; +} fifo_t; + +void fifo_init(fifo_t *f); +void fifo_enqueue(fifo_t *f, fifo_content_t data); +fifo_content_t fifo_dequeue(fifo_t *f); +fifo_size_t fifo_fill(fifo_t *f); +bool fifo_empty(fifo_t *f); +bool fifo_full(fifo_t *f); + +#endif diff --git a/firmware/fnordlicht-controller/globals.h b/firmware/fnordlicht-controller/globals.h new file mode 100644 index 0000000..309f04c --- /dev/null +++ b/firmware/fnordlicht-controller/globals.h @@ -0,0 +1,75 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _FNORDLICHT_GLOBALS_H +#define _FNORDLICHT_GLOBALS_H + +#include +#include "config.h" +#include "../common/common.h" + +/* check for avr-libc version */ +#if __AVR_LIBC_VERSION__ < 10600UL +#error "newer libc version (>= 1.6.0) needed!" +#endif + +/* check if cpu speed is defined */ +#ifndef F_CPU +#error "please define F_CPU! (see Makefile)" +#endif + +/* check if this cpu is supported */ +#if !(defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)) +#error "this cpu is not supported yet!" +#endif + +/* fifo size should be a power of 2 and below 128 */ +#define CONFIG_FIFO_SIZE 64 + +/* serial communication is required */ +#define CONFIG_SERIAL 1 + +/* leds */ +#define LED_PORTNAME B +#define LED1_PIN 2 +#define LED2_PIN 1 + +/* buttons */ +#define BTN_PORTNAME C +#define BTN1_PIN 5 +#define BTN2_PIN 4 + +/* infrared receiver */ +#define IR_PORTNAME C +#define IR_PIN 1 +#define IR_INTNUM 9 /* PC1 is PCINT9 */ + +/* convenient naming */ +#define LED_PORT _OUTPORT(LED_PORTNAME) +#define LED_DDR _DDRPORT(LED_PORTNAME) + +#define BTN_PORT _OUTPORT(BTN_PORTNAME) +#define BTN_DDR _DDRPORT(BTN_PORTNAME) +#define BTN_PIN _INPORT(BTN_PORTNAME) + +#endif /* _FNORDLICHT_CONFIG_H */ diff --git a/firmware/fnordlicht-controller/ir-cluster.c b/firmware/fnordlicht-controller/ir-cluster.c new file mode 100644 index 0000000..e4dcb63 --- /dev/null +++ b/firmware/fnordlicht-controller/ir-cluster.c @@ -0,0 +1,160 @@ +/* + * ox - infrared to usb keyboard/mouse adapter + * + * by Alexander Neumann + * + * inspired by InfraHID by Alex Badea, + * see http://vamposdecampos.googlepages.com/infrahid.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#include "ir-cluster.h" + +/* return the position of the (first) minimum value within data[], + * considering only values at even positions */ +static uint8_t minimum(uint16_t data[], uint8_t len) +{ + uint8_t min = 0; + + for (uint8_t i = 0; i < 2*len; i += 2) { + if (data[i] < data[min]) + min = i; + } + + return min; +} + +/* search next bigger value (starting at data[from]) within data[], just + * considering the even values, return -1 on error (no bigger value found) */ +static int8_t next(uint16_t data[], uint8_t len, uint8_t from) +{ + len *= 2; + + uint16_t old = data[from]; + + /* test if the same value appears again within data[] after from */ + for (uint8_t i = from+2; i < len; i += 2) { + if (data[i] == old) + /* found the same value again, at pos i */ + return i; + } + + /* else search for the next bigger value */ + int16_t pos = -1; + for (uint8_t i = 0; i < len; i += 2) { + + /* if the current value is lower than the old value, try next */ + if (data[i] <= old) + continue; + + /* if we haven't found a bigger value yet, or if the current value + * is smaller than the value we looked at before, + * consider the current position as the next value */ + if (pos < 0 || data[i] < data[pos]) + pos = i; + } + + return pos; +} + +/* search for (one-dimensional) clusters within data[], + * consider only values at even positions */ +uint8_t ir_cluster(uint16_t data[], uint8_t len, uint16_t cluster[], uint8_t max) +{ + uint8_t cindex = 0; + + /* search minimum within data[] */ + uint8_t pos = minimum(data, len); + + /* initialize mean value */ + uint32_t mean = data[pos]; + uint8_t count = 1; + + /* iterate over data[], processing the values (at even positions) in + * ascending order */ + for (uint8_t i = 0; i < len-1; i++) { + + /* search position of the next element within data[] */ + uint8_t nextpos = next(data, len, pos); + + /* as a shortcut, name values a and b */ + uint16_t a = data[pos]; + uint16_t b = data[nextpos]; + + /* check if b > 1.5*a */ + a += a/2; + if (b > a) { + + /* reached a step, found a cluster */ + mean /= count; + cluster[cindex++] = (uint16_t)mean; + + /* stop processing since max cluster values is reached */ + if (cindex == max) + return max; + + /* reset mean value */ + mean = 0; + count = 0; + } + + /* add value to mean */ + mean += b; + count++; + + /* advance position */ + pos = nextpos; + } + + /* if there are some values left in mean, this is the last cluster */ + if (count > 0) { + mean /= count; + cluster[cindex++] = (uint16_t)mean; + } + + return cindex; +} + +/* get cluster index belonging to data */ +uint8_t ir_min_cluster(uint16_t data, uint16_t cluster[], uint8_t len) +{ + uint8_t min = 0; + uint16_t diff = 0xffff; + + /* iterate over possible clusters */ + for (uint8_t i = 0; i < len; i++) { + uint16_t curdiff; + + /* get positive difference */ + if (data < cluster[i]) + curdiff = cluster[i] - data; + else + curdiff = data - cluster[i]; + + /* if difference is lower, remember this cluster */ + if (curdiff < diff) { + diff = curdiff; + min = i; + } else + /* stop here, since difference is larger than for the last cluster + * (cluster[] is ordered ascending */ + break; + } + + return min; +} diff --git a/firmware/fnordlicht-controller/ir-cluster.h b/firmware/fnordlicht-controller/ir-cluster.h new file mode 100644 index 0000000..8ffa362 --- /dev/null +++ b/firmware/fnordlicht-controller/ir-cluster.h @@ -0,0 +1,38 @@ +/* + * ox - infrared to usb keyboard/mouse adapter + * + * by Alexander Neumann + * + * inspired by InfraHID by Alex Badea, + * see http://vamposdecampos.googlepages.com/infrahid.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#include + +#ifndef __IR_CLUSTER_H +#define __IR_CLUSTER_H + +/* search for (one-dimensional) clusters within data[], + * consider only values at even positions */ +uint8_t ir_cluster(uint16_t data[], uint8_t len, uint16_t cluster[], uint8_t max); + +/* get cluster index belonging to data */ +uint8_t ir_min_cluster(uint16_t data, uint16_t cluster[], uint8_t len); + +#endif diff --git a/firmware/fnordlicht-controller/ir.c b/firmware/fnordlicht-controller/ir.c new file mode 100644 index 0000000..73c5ac8 --- /dev/null +++ b/firmware/fnordlicht-controller/ir.c @@ -0,0 +1,196 @@ +/* + * ox - infrared to usb keyboard/mouse adapter + * + * by Alexander Neumann + * + * inspired by InfraHID by Alex Badea, + * see http://vamposdecampos.googlepages.com/infrahid.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#include +#include +#include +#include +#include +#include "config.h" +#include "globals.h" +#include "../common/common.h" +#include "usb.h" +#include "ir.h" +#include "ir-cluster.h" +#include "ui.h" + +/* macro magic */ +#define IR_DDR _DDRPORT(IR_PORTNAME) +#define IR_PORT _OUTPORT(IR_PORTNAME) + +#if IR_INTNUM < 8 +# define IR_BANK 0 +# define IR_VECT PCINT0_vect +#elif IR_INTNUM < 16 +# define IR_BANK 1 +# define IR_VECT PCINT1_vect +#else +# define IR_BANK 2 +# define IR_VECT PCINT2_vect +#endif + +#define IR_INT (IR_INTNUM - IR_BANK * 8) +#define IR_PCIE _PCIE(IR_BANK) +#define IR_PCIF _PCIF(IR_BANK) +#define IR_PCMSK _PCMSK(IR_BANK) + +/* 8 should be enough for everybody! */ +#define MAX_CLUSTERS 8 + +/* internal state machine */ +static enum { + READY = 0, + LISTEN = 1, + DONE = 2, +} state; + +volatile struct ir_global_t ir_global; + +void ir_init(void) +{ + /* initialize input pin (with pullup) */ + IR_DDR &= ~_BV(IR_PIN); + IR_PORT |= _BV(IR_PIN); + + /* configure timer1 with prescaler 64 and CTC for measuring ir timings */ + TCCR1B = _BV(CS11) | _BV(CS10) | _BV(WGM12); + /* configure timer action after 20ms: 20mhz/64/50 */ + OCR1A = F_CPU/50/64; +} + +void ir_set_mode(enum ir_mode_t mode) +{ + if (ir_global.mode != IR_DISABLED && mode == IR_DISABLED) { + /* disable pin change interrupt */ + IR_PCMSK &= ~_BV(IR_INT); + PCICR &= ~_BV(IR_PCIE); + + /* clear interrupt flags */ + TIFR1 = TIFR1; + } else if (ir_global.mode == IR_DISABLED && mode != IR_DISABLED) { + /* clear interrupt flags */ + TIFR1 = TIFR1; + + /* reset variables */ + ir_global.pos = 0; + state = READY; + + /* enable pin change interrupt */ + IR_PCMSK |= _BV(IR_INT); + PCICR |= _BV(IR_PCIE); + } + + ir_global.mode = mode; + + if (mode == IR_DECODE || mode == IR_RAW) + ui_blink(0, 0x5); +} + +ISR(IR_VECT, ISR_NOBLOCK) +{ + /* store code */ + uint16_t value = TCNT1; + TCNT1 = 0; + + /* do not store the first 'off' timing value */ + if (state == READY) + state = LISTEN; + else if (state == LISTEN) { + ir_global.time[ir_global.pos++] = value; + + /* check if we reached the end of the array */ + if (ir_global.pos == MAX_CODE_LENGTH) + state = DONE; + } +} + +void ir_poll(void) +{ + if (ir_global.mode == IR_DISABLED) + return; + + /* check for timeout */ + if (TIFR1 & _BV(OCF1A)) { + /* clear interrupt flag */ + TIFR1 = _BV(OCF1A); + + /* if ir has been detected, process code, else return to listening */ + if (ir_global.pos > 1) { + state = DONE; + + /* set last timing to zero */ + if (ir_global.pos % 2 == 0) + ir_global.time[ir_global.pos-1] = 0; + else + ir_global.time[ir_global.pos++] = 0; + } else + state = READY; + } + + /* if code is complete, process! */ + if (state == DONE) { + /* compute clusters */ + if (ir_global.mode == IR_RECEIVE || ir_global.mode == IR_DECODE) { + uint16_t cluster_on[MAX_CLUSTERS]; + uint16_t cluster_off[MAX_CLUSTERS]; + + uint8_t con = ir_cluster(&ir_global.time[0], ir_global.pos/2, &cluster_on[0], MAX_CLUSTERS); + uint8_t coff = ir_cluster(&ir_global.time[1], ir_global.pos/2-1, &cluster_off[0], MAX_CLUSTERS); + + for (uint8_t i = 0; i < ir_global.pos/2; i++) { + uint8_t data_on = ir_min_cluster(ir_global.time[2*i], cluster_on, con); + uint8_t data_off = ir_min_cluster(ir_global.time[2*i+1], cluster_off, coff); + + ir_global.time[i] = data_on | (data_off << 8); + } + + ir_global.pos /= 2; + } + + if (ir_global.mode == IR_RECEIVE) { + uint16_t crc = 0; + for (uint8_t i = 0; i < ir_global.pos; i++) { + crc = _crc16_update(crc, LO8(ir_global.time[i])); + crc = _crc16_update(crc, HI8(ir_global.time[i])); + } + + /* remember code and length */ + ir_global.last = crc; + ir_global.length = ir_global.pos/2; + + /* + * insert code evaluation here... + */ + + ui_blink(0x1, 0); + + ir_global.pos = 0; + state = READY; + } else { + ui_blink(0, 0x1); + ir_set_mode(IR_DISABLED); + } + } +} diff --git a/firmware/fnordlicht-controller/ir.h b/firmware/fnordlicht-controller/ir.h new file mode 100644 index 0000000..f51dd16 --- /dev/null +++ b/firmware/fnordlicht-controller/ir.h @@ -0,0 +1,61 @@ +/* + * ox - infrared to usb keyboard/mouse adapter + * + * by Alexander Neumann + * + * inspired by InfraHID by Alex Badea, + * see http://vamposdecampos.googlepages.com/infrahid.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#ifndef __IR_H +#define __IR_H + +enum ir_mode_t +{ + IR_DISABLED = 0, + IR_RAW = 1, /* just receive a code, disable ir afterwards (-> DISABLE) */ + IR_DECODE = 2, /* just receive and decode vaules, disable ir afterwards (-> DISABLE) */ + IR_RECEIVE = 3, /* receive, decode and process codes */ +}; + +struct ir_global_t +{ + enum ir_mode_t mode; + uint16_t last; + uint8_t length; + + #define MAX_CODE_LENGTH 160 + /* internal storage for received code: allocate 160*2 = 320 byte memory for + * storing a code, this means we can store 80 on/off sequence timings */ + + uint16_t time[MAX_CODE_LENGTH]; + uint8_t pos; +}; + +extern volatile struct ir_global_t ir_global; + +void ir_init(void); +void ir_poll(void); + +void ir_set_mode(enum ir_mode_t mode); + +#define ir_disable() ir_set_mode(IR_DISABLED); +#define ir_enable() ir_set_mode(IR_RECEIVE); + +#endif diff --git a/firmware/fnordlicht-controller/remote-proto.h b/firmware/fnordlicht-controller/remote-proto.h new file mode 100644 index 0000000..ba34bdd --- /dev/null +++ b/firmware/fnordlicht-controller/remote-proto.h @@ -0,0 +1,36 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __REMOTE_PROTO +#define __REMOTE_PROTO 1 + +#include "../common/remote-proto.h" + +struct remote_msg_start_program_t +{ + uint8_t address; + uint8_t cmd; + uint8_t script; + uint8_t params[REMOTE_STARTUP_MAX_PARAMSIZE]; +}; + +#endif diff --git a/firmware/fnordlicht-controller/timer.c b/firmware/fnordlicht-controller/timer.c new file mode 100644 index 0000000..8bd5263 --- /dev/null +++ b/firmware/fnordlicht-controller/timer.c @@ -0,0 +1,78 @@ +/* + * unzap firmware + * + * (c) by Alexander Neumann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#include +#include +#include "../common/io.h" +#include "timer.h" + +static volatile uint8_t internal_counter; + +void timer_init(void) +{ +#if defined(__AVR_ATmega8__) + /* initialize timer2, CTC at 10ms, prescaler 1024 */ + OCR2 = F_CPU/1024/100; + TCCR2 = _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20); + TIMSK |= _BV(OCIE2); +#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) + /* initialize timer2, CTC at 10ms, prescaler 1024 */ + OCR2A = F_CPU/1024/100; + TCCR2A = _BV(WGM21); + TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); + TIMSK2 = _BV(OCIE2A); +#else +#error "unknown controller, unable to initialize timer2" +#endif +} + +void timer_set(timer_t *t, uint8_t timeout) +{ + t->current = internal_counter; + t->timeout = timeout; +} + +bool timer_expired(timer_t *t) +{ + if (t->timeout == 0) + return true; + + /* attention: this is not correct, if internal_counter is incremented by more than one + * between two calls of timer_expired()! */ + if (t->current != internal_counter) { + t->timeout--; + t->current = internal_counter; + } + + return false; +} + +/* timer interrupt function */ +#if defined(__AVR_ATmega8__) +ISR(TIMER2_COMP_vect, ISR_NOBLOCK) { + internal_counter++; +} +#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) +ISR(TIMER2_COMPA_vect, ISR_NOBLOCK) { + internal_counter++; +} +#endif diff --git a/firmware/fnordlicht-controller/timer.h b/firmware/fnordlicht-controller/timer.h new file mode 100644 index 0000000..4bd8d43 --- /dev/null +++ b/firmware/fnordlicht-controller/timer.h @@ -0,0 +1,42 @@ +/* + * unzap firmware + * + * (c) by Alexander Neumann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +/* small timer library, uses timer2 */ + +#ifndef __TIMER_H +#define __TIMER_H + +#include +#include + +/* structures */ +typedef struct { + uint8_t current; + uint8_t timeout; +} timer_t; + +/* functions */ +void timer_init(void); +void timer_set(timer_t *t, uint8_t timeout); +bool timer_expired(timer_t *t); + +#endif diff --git a/firmware/fnordlicht-controller/uart.c b/firmware/fnordlicht-controller/uart.c new file mode 100644 index 0000000..c23f747 --- /dev/null +++ b/firmware/fnordlicht-controller/uart.c @@ -0,0 +1,113 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "globals.h" + +#if CONFIG_SERIAL + +#include "../common/io.h" +#include + +#include "globals.h" +#include "../common/common.h" +#include "fifo.h" +#include "uart.h" + +/* define uart mode (8N1) */ +#if defined(__AVR_ATmega8__) +/* in atmega8, we need a special switching bit + * for addressing UCSRC */ +#define UART_UCSRC _BV(URSEL) | _BV(UCSZ0) | _BV(UCSZ1) + +#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) +/* in atmega88, this isn't needed any more */ +#define UART_UCSRC _BV(_UCSZ0_UART0) | _BV(_UCSZ1_UART0) +#endif + +/* global variables */ +volatile struct global_uart_t global_uart; + +/** output one character */ +void uart_putc(uint8_t data) +{ + /* store data */ + fifo_enqueue((fifo_t *)&global_uart.tx, data); + + /* enable interrupt */ + _UCSRB_UART0 |= _BV(_UDRIE_UART0); +} + +/** init the hardware uart */ +void uart_init(void) +{ + #define BAUD CONFIG_SERIAL_BAUDRATE + #include + + /* set baud rate */ + _UBRRH_UART0 = UBRRH_VALUE; + _UBRRL_UART0 = UBRRL_VALUE; + + #if USE_2X + _UCSRA_UART0 |= (1 << _U2X_UART0); + #endif + + /* set mode */ + _UCSRC_UART0 = UART_UCSRC; + + /* enable transmitter, receiver and receiver complete interrupt */ + _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0) | _BV(_RXCIE_UART0); + + /* init fifos */ + fifo_init((fifo_t *)&global_uart.rx); + fifo_init((fifo_t *)&global_uart.tx); +} + + +/** interrupts*/ + +/** uart receive interrupt */ +ISR(_SIG_UART_RECV_UART0) +{ + + /* store received data */ + fifo_enqueue((fifo_t *)&global_uart.rx, _UDR_UART0); + +} + +/** uart data register empty interrupt */ +ISR(_SIG_UART_DATA_UART0) +{ + + /* load next byte to transfer */ + _UDR_UART0 = fifo_dequeue((fifo_t *)&global_uart.tx); + + /* check if this interrupt is still needed */ + if ( fifo_fill((fifo_t *)&global_uart.tx) == 0) { + /* disable this interrupt */ + _UCSRB_UART0 &= ~_BV(_UDRIE_UART0); + } + +} + + +#endif diff --git a/firmware/fnordlicht-controller/uart.h b/firmware/fnordlicht-controller/uart.h new file mode 100644 index 0000000..ceeb4f0 --- /dev/null +++ b/firmware/fnordlicht-controller/uart.h @@ -0,0 +1,58 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef UART_H +#define UART_H + +#include "globals.h" +#include "fifo.h" +#include "../common/io.h" + +#if !CONFIG_SERIAL + +#define uart_init(...) +#define uart_putc(...) + +#else + +/* structs */ +struct global_uart_t { + fifo_t rx; + fifo_t tx; +}; + +/* global variables */ +extern volatile struct global_uart_t global_uart; + +/* prototypes */ +void uart_init(void); +void uart_putc(uint8_t data); + +static inline bool uart_send_complete(void) +{ + return _BV(_UDRE_UART0) & _UCSRA_UART0; +} + +#endif + +#endif diff --git a/firmware/fnordlicht-controller/ui.c b/firmware/fnordlicht-controller/ui.c new file mode 100644 index 0000000..a9c559a --- /dev/null +++ b/firmware/fnordlicht-controller/ui.c @@ -0,0 +1,299 @@ +/* + * unzap firmware + * + * (c) by Alexander Neumann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#include +#include +#include +#include "globals.h" +#include "ui.h" +#include "timer.h" +#include "uart.h" +#include "../common/pt/pt.h" +#include "remote-proto.h" + +/* module local variables */ + +static struct pt btn_thread; +/* buttons are idle at start (internal pullups active) */ +static uint8_t btn_state = _BV(BTN1_PIN) | _BV(BTN2_PIN); +static uint8_t btn_last_sample = _BV(BTN1_PIN) | _BV(BTN2_PIN); +static uint8_t btn_press = 0; + +static struct pt blink_thread; +static uint8_t blink_seq_led1 = 0; +static uint8_t blink_seq_led2 = 0; + +static struct pt input_thread; + +/* initialize the button and led pins */ +void ui_init(void) +{ + /* initialize buttons */ + BTN_DDR &= ~(_BV(BTN1_PIN) | _BV(BTN2_PIN)); + BTN_PORT |= _BV(BTN1_PIN) | _BV(BTN2_PIN); + + /* initialize leds */ + LED_DDR |= _BV(LED1_PIN) | _BV(LED2_PIN); + LED_PORT &= ~(_BV(LED1_PIN) | _BV(LED2_PIN)); + + /* initialize button sample thread and variables */ + PT_INIT(&btn_thread); + + /* initialize blink thread and variables */ + PT_INIT(&blink_thread); + + /* initialize input thread */ + PT_INIT(&input_thread); +} + +/* blink out a sequence (LSB first), every bit is 150ms long */ +void ui_blink(uint8_t sequence1, uint8_t sequence2) +{ + blink_seq_led1 = sequence1; + blink_seq_led2 = sequence2; +} + +/* check if the current blink sequency is done */ +bool ui_blinking(void) +{ + return !(blink_seq_led1 || blink_seq_led2); +} + +/* sample buttons, set bit in btn_press if a button has been pressed */ +static PT_THREAD(ui_sample_buttons(struct pt *thread)) +{ + /* make sure no variable is created on the stack */ + static timer_t btn_timer; + + PT_BEGIN(thread); + + while(1) { + /* only execute this thread every 10ms */ + timer_set(&btn_timer, 1); + PT_WAIT_UNTIL(thread, timer_expired(&btn_timer)); + + /* sample buttons */ + /* ATTENTION: make SURE, btn_sample is NOT created on the stack! */ + register uint8_t btn_sample = BTN_PIN & (_BV(BTN1_PIN) | _BV(BTN2_PIN)); + + /* mark bits which stayed the same since last sample */ + btn_last_sample ^= ~btn_sample; + + /* mark bits which have not changed, but whose state is different */ + btn_last_sample = btn_state ^ (btn_sample & btn_last_sample); + + /* if old state is one (button not pressed), new state is zero (button pressed), + * so set these bits in btn_press */ + btn_press |= btn_last_sample & btn_state; + + /* remember new state and last sample */ + btn_state ^= btn_last_sample; + btn_last_sample = btn_sample; + } + + PT_END(thread); +} + +/* execute led blinking */ +static PT_THREAD(ui_do_blinking(struct pt *thread)) +{ + static timer_t t; + + PT_BEGIN(thread); + + while (1) { + + /* wait until there is something to do */ + PT_WAIT_UNTIL(thread, blink_seq_led1 || blink_seq_led2); + + /* turn leds on/off, according to blink sequence */ + if (blink_seq_led1 & 1) + LED1_ON(); + else + LED1_OFF(); + + if (blink_seq_led2 & 1) + LED2_ON(); + else + LED2_OFF(); + + blink_seq_led1 >>= 1; + blink_seq_led2 >>= 1; + + /* wait 150ms */ + timer_set(&t, 15); + PT_WAIT_UNTIL(thread, timer_expired(&t)); + + /* turn off leds */ + LED1_OFF(); + LED2_OFF(); + + } + + PT_END(thread); +} + +#define MASTER_PROGRAMS 4 +static PROGMEM uint8_t master_parameters[] = { + /* colorwheel forward rainbow */ + 0, /* program index */ + 5, /* fade step */ + 1, /* fade delay */ + 0, /* fade sleep */ + 0, 0, /* hue start (little endian) */ + 20, 0, /* hue step (little endian) */ + -1, /* addr add */ + 255, /* saturation */ + 255, /* value */ + + /* colorwheel all same color */ + 0, /* program index */ + 5, /* fade step */ + 1, /* fade delay */ + 0, /* fade sleep */ + 0, 0, /* hue start (little endian) */ + 20, 0, /* hue step (little endian) */ + 0, /* addr add */ + 255, /* saturation */ + 255, /* value */ + + /* colorwheel all same color, low saturation */ + 0, /* program index */ + 5, /* fade step */ + 1, /* fade delay */ + 0, /* fade sleep */ + 0, 0, /* hue start (little endian) */ + 20, 0, /* hue step (little endian) */ + 0, /* addr add */ + 150, /* saturation */ + 255, /* value */ + + /* colorwheel all same color, low saturation */ + 0, /* program index */ + 5, /* fade step */ + 1, /* fade delay */ + 0, /* fade sleep */ + 0, 0, /* hue start (little endian) */ + 120, 0, /* hue step (little endian) */ + 0, /* addr add */ + 255, /* saturation */ + 120, /* value */ +}; + +void start_program(uint8_t index) +{ + /* load parameters */ + union { + uint8_t raw[REMOTE_MSG_LEN]; + struct remote_msg_start_program_t msg; + } data; + + /* reset data structure */ + memset(&data, '\0', REMOTE_MSG_LEN); + + data.msg.address = 255; + data.msg.cmd = REMOTE_CMD_START_PROGRAM; + + uint8_t *ptr = &master_parameters[index*REMOTE_STARTUP_MAX_PARAMSIZE]; + data.msg.script = pgm_read_byte(ptr++); + + for (uint8_t i = 0; i < REMOTE_STARTUP_MAX_PARAMSIZE; i++) { + uint8_t d = pgm_read_byte(ptr++); + data.msg.params[i] = d; + } + + for (uint8_t i = 0; i < REMOTE_MSG_LEN; i++) + uart_putc(data.raw[i]); + + ui_blink(0x01, 0); +} + +static void send_resync(uint8_t addr) +{ + for (uint8_t i = 0; i < REMOTE_SYNC_LEN; i++) + uart_putc(REMOTE_CMD_RESYNC); + uart_putc(addr); +} + +static void send_stop(uint8_t addr) +{ + /* load parameters */ + union { + uint8_t raw[REMOTE_MSG_LEN]; + struct remote_msg_t msg; + } data; + + data.msg.cmd = REMOTE_CMD_STOP; + data.msg.address = addr; + data.msg.data[0] = 1; + + for (uint8_t i = 0; i < REMOTE_MSG_LEN; i++) + uart_putc(data.raw[i]); +} + +/* parse input */ +static PT_THREAD(ui_input(struct pt*thread)) +{ + static timer_t t; + static uint8_t option_set; + static uint8_t current_program; + + PT_BEGIN(thread); + + current_program = 0; + + while(1) { + + if (btn_press & _BV(BTN1_PIN)) { + PT_WAIT_UNTIL(thread, fifo_empty(&global_uart.tx)); + send_resync(0); + PT_WAIT_UNTIL(thread, fifo_empty(&global_uart.tx)); + send_stop(255); + PT_WAIT_UNTIL(thread, fifo_empty(&global_uart.tx)); + start_program(current_program); + current_program = (current_program+1) % MASTER_PROGRAMS; + } + + if (btn_press & _BV(BTN2_PIN)) { + //uart_putc('2'); + } + + btn_press = 0; + + PT_YIELD(thread); + } + + PT_END(thread); +} + +/* poll for user actions */ +void ui_poll(void) +{ + /* sample buttons */ + PT_SCHEDULE(ui_sample_buttons(&btn_thread)); + + /* do blinking */ + PT_SCHEDULE(ui_do_blinking(&blink_thread)); + + /* process input */ + PT_SCHEDULE(ui_input(&input_thread)); +} diff --git a/firmware/fnordlicht-controller/ui.h b/firmware/fnordlicht-controller/ui.h new file mode 100644 index 0000000..bd70942 --- /dev/null +++ b/firmware/fnordlicht-controller/ui.h @@ -0,0 +1,49 @@ +/* + * unzap firmware + * + * (c) by Alexander Neumann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#ifndef __UI_H +#define __UI_H + +#include +#include + +/* helper macros */ +#define LED1_ON() LED_PORT |= _BV(LED1_PIN) +#define LED1_OFF() LED_PORT &= ~_BV(LED1_PIN) +#define LED1_TOGGLE() LED_PORT ^= _BV(LED1_PIN) +#define LED2_ON() LED_PORT |= _BV(LED2_PIN) +#define LED2_OFF() LED_PORT &= ~_BV(LED2_PIN) +#define LED2_TOGGLE() LED_PORT ^= _BV(LED2_PIN) + +/* initialize the button and led pins */ +void ui_init(void); + +/* blink out a sequence (LSB first), every bit is 150ms long */ +void ui_blink(uint8_t sequence1, uint8_t sequence2); + +/* check if the current blink sequency is done */ +bool ui_blinking(void); + +/* poll for user actions */ +void ui_poll(void); + +#endif diff --git a/firmware/fnordlicht-controller/usb.c b/firmware/fnordlicht-controller/usb.c new file mode 100644 index 0000000..bbfaace --- /dev/null +++ b/firmware/fnordlicht-controller/usb.c @@ -0,0 +1,159 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/* includes */ +#include "globals.h" +#include "../common/io.h" + +#include +#include + +#include "../common/common.h" +#include "usbdrv/usbdrv.h" +#include "timer.h" +#include "uart.h" +#include "ir.h" + +static bool usb_status; + + /* supply custom usbDeviceConnect() and usbDeviceDisconnect() macros + * which turn the interrupt on and off at the right times, + * and prevent the execution of an interrupt while the pullup resistor + * is switched off */ +#ifdef USB_CFG_PULLUP_IOPORTNAME +#undef usbDeviceConnect +#define usbDeviceConnect() do { \ + USB_PULLUP_DDR |= (1<bRequest == USBRQ_SEND && req->wLength.word > 0) { + usb.state = USB_STATE_SEND; + if (req->wLength.word < 256) { + usb.bytes_remaining = req->wLength.bytes[0]; + return USB_NO_MSG; + } else { + /* too long */ + buf[0] = 1; + len = 1; + } + } else if (req->bRequest == USBRQ_IR_STATE) { + /* read or set state? */ + if (req->bmRequestType & USBRQ_DIR_MASK) { + /* read */ + buf[0] = ir_global.mode; + len = 1; + } else { + /* write */ + uint8_t new_state = req->wValue.bytes[0]; + if (new_state <= IR_RECEIVE) + ir_set_mode(new_state); + } + } else if (req->bRequest == USBRQ_IR_READ) { + if (ir_global.mode == IR_DISABLED) { + usbMsgPtr = (void *)ir_global.time; + return 2*ir_global.pos; + } + } + + return len; +} + +uchar usbFunctionWrite(uchar *data, uchar len) +{ + if (len > usb.bytes_remaining) + len = usb.bytes_remaining; + + usb.bytes_remaining -= len; + + while (len--) { + /* ugly hack: busy wait until the fifo is ready for another byte... */ + while (fifo_full(&global_uart.tx)); + uart_putc(*data++); + } + + if (usb.bytes_remaining == 0) { + return true; + usb.state == USB_STATE_IDLE; + } else + return len; +} + +void usb_init(void) +{ + usbInit(); +} + +void usb_enable(void) +{ + usbDeviceConnect(); + usb_status = true; +} + +void usb_disable(void) +{ + usbDeviceDisconnect(); + usb_status = false; +} + +bool usb_enabled(void) +{ + return usb_status; +} + +void usb_poll(void) +{ + if (usb_status) + usbPoll(); +} diff --git a/firmware/fnordlicht-controller/usb.h b/firmware/fnordlicht-controller/usb.h new file mode 100644 index 0000000..18a075d --- /dev/null +++ b/firmware/fnordlicht-controller/usb.h @@ -0,0 +1,35 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __USB_H +#define __USB_H + +#include +#include + +void usb_init(void); +void usb_poll(void); +void usb_enable(void); +void usb_disable(void); +bool usb_enabled(void); + +#endif diff --git a/firmware/fnordlicht-controller/usbconfig.h b/firmware/fnordlicht-controller/usbconfig.h new file mode 100644 index 0000000..8d4b66c --- /dev/null +++ b/firmware/fnordlicht-controller/usbconfig.h @@ -0,0 +1,369 @@ +/* Name: usbconfig.h + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Christian Starkjohann + * Creation Date: 2005-04-01 + * Tabsize: 4 + * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id: usbconfig-prototype.h 767 2009-08-22 11:39:22Z cs $ + */ + +#ifndef __usbconfig_h_included__ +#define __usbconfig_h_included__ + +/* +General Description: +This file is an example configuration (with inline documentation) for the USB +driver. It configures V-USB for USB D+ connected to Port D bit 2 (which is +also hardware interrupt 0 on many devices) and USB D- to Port D bit 4. You may +wire the lines to any other port, as long as D+ is also wired to INT0 (or any +other hardware interrupt, as long as it is the highest level interrupt, see +section at the end of this file). ++ To create your own usbconfig.h file, copy this file to your project's ++ firmware source directory) and rename it to "usbconfig.h". ++ Then edit it accordingly. +*/ + +/* ---------------------------- Hardware Config ---------------------------- */ + +#define USB_CFG_IOPORTNAME D +/* This is the port where the USB bus is connected. When you configure it to + * "B", the registers PORTB, PINB and DDRB will be used. + */ +#define USB_CFG_DMINUS_BIT 4 +/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected. + * This may be any bit in the port. + */ +#define USB_CFG_DPLUS_BIT 3 +/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected. + * This may be any bit in the port. Please note that D+ must also be connected + * to interrupt pin INT0! [You can also use other interrupts, see section + * "Optional MCU Description" below, or you can connect D- to the interrupt, as + * it is required if you use the USB_COUNT_SOF feature. If you use D- for the + * interrupt, the USB interrupt will also be triggered at Start-Of-Frame + * markers every millisecond.] + */ +#define USB_CFG_CLOCK_KHZ (F_CPU/1000) +/* Clock rate of the AVR in kHz. Legal values are 12000, 12800, 15000, 16000, + * 16500 and 20000. The 12.8 MHz and 16.5 MHz versions of the code require no + * crystal, they tolerate +/- 1% deviation from the nominal frequency. All + * other rates require a precision of 2000 ppm and thus a crystal! + * Default if not specified: 12 MHz + */ +#define USB_CFG_CHECK_CRC 0 +/* Define this to 1 if you want that the driver checks integrity of incoming + * data packets (CRC checks). CRC checks cost quite a bit of code size and are + * currently only available for 18 MHz crystal clock. You must choose + * USB_CFG_CLOCK_KHZ = 18000 if you enable this option. + */ + +/* ----------------------- Optional Hardware Config ------------------------ */ + +#define USB_CFG_PULLUP_IOPORTNAME C +/* If you connect the 1.5k pullup resistor from D- to a port pin instead of + * V+, you can connect and disconnect the device from firmware by calling + * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h). + * This constant defines the port on which the pullup resistor is connected. + */ +#define USB_CFG_PULLUP_BIT 3 +/* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined + * above) where the 1.5k pullup resistor is connected. See description + * above for details. + */ + +/* --------------------------- Functional Range ---------------------------- */ + +#define USB_CFG_HAVE_INTRIN_ENDPOINT 0 +/* Define this to 1 if you want to compile a version with two endpoints: The + * default control endpoint 0 and an interrupt-in endpoint (any other endpoint + * number). + */ +#define USB_CFG_HAVE_INTRIN_ENDPOINT3 0 +/* Define this to 1 if you want to compile a version with three endpoints: The + * default control endpoint 0, an interrupt-in endpoint 3 (or the number + * configured below) and a catch-all default interrupt-in endpoint as above. + * You must also define USB_CFG_HAVE_INTRIN_ENDPOINT to 1 for this feature. + */ +#define USB_CFG_EP3_NUMBER 3 +/* If the so-called endpoint 3 is used, it can now be configured to any other + * endpoint number (except 0) with this macro. Default if undefined is 3. + */ +/* #define USB_INITIAL_DATATOKEN USBPID_DATA1 */ +/* The above macro defines the startup condition for data toggling on the + * interrupt/bulk endpoints 1 and 3. Defaults to USBPID_DATA1. + * Since the token is toggled BEFORE sending any data, the first packet is + * sent with the oposite value of this configuration! + */ +#define USB_CFG_IMPLEMENT_HALT 0 +/* Define this to 1 if you also want to implement the ENDPOINT_HALT feature + * for endpoint 1 (interrupt endpoint). Although you may not need this feature, + * it is required by the standard. We have made it a config option because it + * bloats the code considerably. + */ +#define USB_CFG_SUPPRESS_INTR_CODE 0 +/* Define this to 1 if you want to declare interrupt-in endpoints, but don't + * want to send any data over them. If this macro is defined to 1, functions + * usbSetInterrupt() and usbSetInterrupt3() are omitted. This is useful if + * you need the interrupt-in endpoints in order to comply to an interface + * (e.g. HID), but never want to send any data. This option saves a couple + * of bytes in flash memory and the transmit buffers in RAM. + */ +#define USB_CFG_INTR_POLL_INTERVAL 10 +/* If you compile a version with endpoint 1 (interrupt-in), this is the poll + * interval. The value is in milliseconds and must not be less than 10 ms for + * low speed devices. + */ +#define USB_CFG_IS_SELF_POWERED 1 +/* Define this to 1 if the device has its own power supply. Set it to 0 if the + * device is powered from the USB bus. + */ +#define USB_CFG_MAX_BUS_POWER 100 +/* Set this variable to the maximum USB bus power consumption of your device. + * The value is in milliamperes. [It will be divided by two since USB + * communicates power requirements in units of 2 mA.] + */ +#define USB_CFG_IMPLEMENT_FN_WRITE 1 +/* Set this to 1 if you want usbFunctionWrite() to be called for control-out + * transfers. Set it to 0 if you don't need it and want to save a couple of + * bytes. + */ +#define USB_CFG_IMPLEMENT_FN_READ 0 +/* Set this to 1 if you need to send control replies which are generated + * "on the fly" when usbFunctionRead() is called. If you only want to send + * data from a static buffer, set it to 0 and return the data from + * usbFunctionSetup(). This saves a couple of bytes. + */ +#define USB_CFG_IMPLEMENT_FN_WRITEOUT 0 +/* Define this to 1 if you want to use interrupt-out (or bulk out) endpoints. + * You must implement the function usbFunctionWriteOut() which receives all + * interrupt/bulk data sent to any endpoint other than 0. The endpoint number + * can be found in 'usbRxToken'. + */ +#define USB_CFG_HAVE_FLOWCONTROL 0 +/* Define this to 1 if you want flowcontrol over USB data. See the definition + * of the macros usbDisableAllRequests() and usbEnableAllRequests() in + * usbdrv.h. + */ +#define USB_CFG_LONG_TRANSFERS 1 +/* Define this to 1 if you want to send/receive blocks of more than 254 bytes + * in a single control-in or control-out transfer. Note that the capability + * for long transfers increases the driver size. + */ +/* #define USB_RX_USER_HOOK(data, len) if(usbRxToken == (uchar)USBPID_SETUP) blinkLED(); */ +/* This macro is a hook if you want to do unconventional things. If it is + * defined, it's inserted at the beginning of received message processing. + * If you eat the received message and don't want default processing to + * proceed, do a return after doing your things. One possible application + * (besides debugging) is to flash a status LED on each packet. + */ +/* #define USB_RESET_HOOK(resetStarts) if(!resetStarts){hadUsbReset();} */ +/* This macro is a hook if you need to know when an USB RESET occurs. It has + * one parameter which distinguishes between the start of RESET state and its + * end. + */ +/* #define USB_SET_ADDRESS_HOOK() hadAddressAssigned(); */ +/* This macro (if defined) is executed when a USB SET_ADDRESS request was + * received. + */ +#define USB_COUNT_SOF 0 +/* define this macro to 1 if you need the global variable "usbSofCount" which + * counts SOF packets. This feature requires that the hardware interrupt is + * connected to D- instead of D+. + */ +/* #ifdef __ASSEMBLER__ + * macro myAssemblerMacro + * in YL, TCNT0 + * sts timer0Snapshot, YL + * endm + * #endif + * #define USB_SOF_HOOK myAssemblerMacro + * This macro (if defined) is executed in the assembler module when a + * Start Of Frame condition is detected. It is recommended to define it to + * the name of an assembler macro which is defined here as well so that more + * than one assembler instruction can be used. The macro may use the register + * YL and modify SREG. If it lasts longer than a couple of cycles, USB messages + * immediately after an SOF pulse may be lost and must be retried by the host. + * What can you do with this hook? Since the SOF signal occurs exactly every + * 1 ms (unless the host is in sleep mode), you can use it to tune OSCCAL in + * designs running on the internal RC oscillator. + * Please note that Start Of Frame detection works only if D- is wired to the + * interrupt, not D+. THIS IS DIFFERENT THAN MOST EXAMPLES! + */ +#define USB_CFG_CHECK_DATA_TOGGLING 0 +/* define this macro to 1 if you want to filter out duplicate data packets + * sent by the host. Duplicates occur only as a consequence of communication + * errors, when the host does not receive an ACK. Please note that you need to + * implement the filtering yourself in usbFunctionWriteOut() and + * usbFunctionWrite(). Use the global usbCurrentDataToken and a static variable + * for each control- and out-endpoint to check for duplicate packets. + */ +#define USB_CFG_HAVE_MEASURE_FRAME_LENGTH 0 +/* define this macro to 1 if you want the function usbMeasureFrameLength() + * compiled in. This function can be used to calibrate the AVR's RC oscillator. + */ +#define USB_USE_FAST_CRC 0 +/* The assembler module has two implementations for the CRC algorithm. One is + * faster, the other is smaller. This CRC routine is only used for transmitted + * messages where timing is not critical. The faster routine needs 31 cycles + * per byte while the smaller one needs 61 to 69 cycles. The faster routine + * may be worth the 32 bytes bigger code size if you transmit lots of data and + * run the AVR close to its limit. + */ + +/* -------------------------- Device Description --------------------------- */ + +#define USB_CFG_VENDOR_ID 0xc0, 0x16 /* = 0x16c0 = 5824 = voti.nl */ +/* USB vendor ID for the device, low byte first. If you have registered your + * own Vendor ID, define it here. Otherwise you may use one of obdev's free + * shared VID/PID pairs. Be sure to read USB-IDs-for-free.txt for rules! + * *** IMPORTANT NOTE *** + * This template uses obdev's shared VID/PID pair for Vendor Class devices + * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand + * the implications! + */ +#define USB_CFG_DEVICE_ID 0xdc, 0x05 /* = 0x05dc = 1500 */ +/* This is the ID of the product, low byte first. It is interpreted in the + * scope of the vendor ID. If you have registered your own VID with usb.org + * or if you have licensed a PID from somebody else, define it here. Otherwise + * you may use one of obdev's free shared VID/PID pairs. See the file + * USB-IDs-for-free.txt for details! + * *** IMPORTANT NOTE *** + * This template uses obdev's shared VID/PID pair for Vendor Class devices + * with libusb: 0x16c0/0x5dc. Use this VID/PID pair ONLY if you understand + * the implications! + */ +#define USB_CFG_DEVICE_VERSION 0x00, 0x01 +/* Version number of the device: Minor number first, then major number. + */ +#define USB_CFG_VENDOR_NAME 'l', 'o', 'c', 'h', 'r', 'a', 's', 't', 'e', 'r', '.', 'o', 'r', 'g' +#define USB_CFG_VENDOR_NAME_LEN 14 +/* These two values define the vendor name returned by the USB device. The name + * must be given as a list of characters under single quotes. The characters + * are interpreted as Unicode (UTF-16) entities. + * If you don't want a vendor name string, undefine these macros. + * ALWAYS define a vendor name containing your Internet domain name if you use + * obdev's free shared VID/PID pair. See the file USB-IDs-for-free.txt for + * details. + */ +#define USB_CFG_DEVICE_NAME 'f', 'n', 'o', 'r', 'd', 'l', 'i', 'c', 'h', 't', '-', 'c', 'o', 'n', 't', 'r', 'o', 'l', 'l', 'e', 'r' +#define USB_CFG_DEVICE_NAME_LEN 21 +/* Same as above for the device name. If you don't want a device name, undefine + * the macros. See the file USB-IDs-for-free.txt before you assign a name if + * you use a shared VID/PID. + */ +/*#define USB_CFG_SERIAL_NUMBER 'N', 'o', 'n', 'e' */ +/*#define USB_CFG_SERIAL_NUMBER_LEN 0 */ +/* Same as above for the serial number. If you don't want a serial number, + * undefine the macros. + * It may be useful to provide the serial number through other means than at + * compile time. See the section about descriptor properties below for how + * to fine tune control over USB descriptors such as the string descriptor + * for the serial number. + */ +#define USB_CFG_DEVICE_CLASS 0xff /* set to 0 if deferred to interface */ +#define USB_CFG_DEVICE_SUBCLASS 0 +/* See USB specification if you want to conform to an existing device class. + * Class 0xff is "vendor specific". + */ +#define USB_CFG_INTERFACE_CLASS 0 /* define class here if not at device level */ +#define USB_CFG_INTERFACE_SUBCLASS 0 +#define USB_CFG_INTERFACE_PROTOCOL 0 +/* See USB specification if you want to conform to an existing device class or + * protocol. The following classes must be set at interface level: + * HID class is 3, no subclass and protocol required (but may be useful!) + * CDC class is 2, use subclass 2 and protocol 1 for ACM + */ +/* #define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH 42 */ +/* Define this to the length of the HID report descriptor, if you implement + * an HID device. Otherwise don't define it or define it to 0. + * If you use this define, you must add a PROGMEM character array named + * "usbHidReportDescriptor" to your code which contains the report descriptor. + * Don't forget to keep the array and this define in sync! + */ + +/* #define USB_PUBLIC static */ +/* Use the define above if you #include usbdrv.c instead of linking against it. + * This technique saves a couple of bytes in flash memory. + */ + +/* ------------------- Fine Control over USB Descriptors ------------------- */ +/* If you don't want to use the driver's default USB descriptors, you can + * provide our own. These can be provided as (1) fixed length static data in + * flash memory, (2) fixed length static data in RAM or (3) dynamically at + * runtime in the function usbFunctionDescriptor(). See usbdrv.h for more + * information about this function. + * Descriptor handling is configured through the descriptor's properties. If + * no properties are defined or if they are 0, the default descriptor is used. + * Possible properties are: + * + USB_PROP_IS_DYNAMIC: The data for the descriptor should be fetched + * at runtime via usbFunctionDescriptor(). If the usbMsgPtr mechanism is + * used, the data is in FLASH by default. Add property USB_PROP_IS_RAM if + * you want RAM pointers. + * + USB_PROP_IS_RAM: The data returned by usbFunctionDescriptor() or found + * in static memory is in RAM, not in flash memory. + * + USB_PROP_LENGTH(len): If the data is in static memory (RAM or flash), + * the driver must know the descriptor's length. The descriptor itself is + * found at the address of a well known identifier (see below). + * List of static descriptor names (must be declared PROGMEM if in flash): + * char usbDescriptorDevice[]; + * char usbDescriptorConfiguration[]; + * char usbDescriptorHidReport[]; + * char usbDescriptorString0[]; + * int usbDescriptorStringVendor[]; + * int usbDescriptorStringDevice[]; + * int usbDescriptorStringSerialNumber[]; + * Other descriptors can't be provided statically, they must be provided + * dynamically at runtime. + * + * Descriptor properties are or-ed or added together, e.g.: + * #define USB_CFG_DESCR_PROPS_DEVICE (USB_PROP_IS_RAM | USB_PROP_LENGTH(18)) + * + * The following descriptors are defined: + * USB_CFG_DESCR_PROPS_DEVICE + * USB_CFG_DESCR_PROPS_CONFIGURATION + * USB_CFG_DESCR_PROPS_STRINGS + * USB_CFG_DESCR_PROPS_STRING_0 + * USB_CFG_DESCR_PROPS_STRING_VENDOR + * USB_CFG_DESCR_PROPS_STRING_PRODUCT + * USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER + * USB_CFG_DESCR_PROPS_HID + * USB_CFG_DESCR_PROPS_HID_REPORT + * USB_CFG_DESCR_PROPS_UNKNOWN (for all descriptors not handled by the driver) + * + * Note about string descriptors: String descriptors are not just strings, they + * are Unicode strings prefixed with a 2 byte header. Example: + * int serialNumberDescriptor[] = { + * USB_STRING_DESCRIPTOR_HEADER(6), + * 'S', 'e', 'r', 'i', 'a', 'l' + * }; + */ + +#define USB_CFG_DESCR_PROPS_DEVICE 0 +#define USB_CFG_DESCR_PROPS_CONFIGURATION 0 +#define USB_CFG_DESCR_PROPS_STRINGS 0 +#define USB_CFG_DESCR_PROPS_STRING_0 0 +#define USB_CFG_DESCR_PROPS_STRING_VENDOR 0 +#define USB_CFG_DESCR_PROPS_STRING_PRODUCT 0 +#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER 0 +#define USB_CFG_DESCR_PROPS_HID 0 +#define USB_CFG_DESCR_PROPS_HID_REPORT 0 +#define USB_CFG_DESCR_PROPS_UNKNOWN 0 + +/* ----------------------- Optional MCU Description ------------------------ */ + +/* The following configurations have working defaults in usbdrv.h. You + * usually don't need to set them explicitly. Only if you want to run + * the driver on a device which is not yet supported or with a compiler + * which is not fully supported (such as IAR C) or if you use a differnt + * interrupt than INT0, you may have to define some of these. + */ +/* #define USB_INTR_CFG MCUCR */ +#define USB_INTR_CFG_SET ((1 << ISC10) | (1 << ISC11)) +/* #define USB_INTR_CFG_CLR 0 */ +/* #define USB_INTR_ENABLE GIMSK */ +#define USB_INTR_ENABLE_BIT INT1 +/* #define USB_INTR_PENDING GIFR */ +#define USB_INTR_PENDING_BIT INTF1 +#define USB_INTR_VECTOR SIG_INTERRUPT1 + +#endif /* __usbconfig_h_included__ */ diff --git a/firmware/fnordlicht-controller/usbdrv/Changelog.txt b/firmware/fnordlicht-controller/usbdrv/Changelog.txt new file mode 100644 index 0000000..655a9d4 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/Changelog.txt @@ -0,0 +1,296 @@ +This file documents changes in the firmware-only USB driver for atmel's AVR +microcontrollers. New entries are always appended to the end of the file. +Scroll down to the bottom to see the most recent changes. + +2005-04-01: + - Implemented endpoint 1 as interrupt-in endpoint. + - Moved all configuration options to usbconfig.h which is not part of the + driver. + - Changed interface for usbVendorSetup(). + - Fixed compatibility with ATMega8 device. + - Various minor optimizations. + +2005-04-11: + - Changed interface to application: Use usbFunctionSetup(), usbFunctionRead() + and usbFunctionWrite() now. Added configuration options to choose which + of these functions to compile in. + - Assembler module delivers receive data non-inverted now. + - Made register and bit names compatible with more AVR devices. + +2005-05-03: + - Allow address of usbRxBuf on any memory page as long as the buffer does + not cross 256 byte page boundaries. + - Better device compatibility: works with Mega88 now. + - Code optimization in debugging module. + - Documentation updates. + +2006-01-02: + - Added (free) default Vendor- and Product-IDs bought from voti.nl. + - Added USBID-License.txt file which defines the rules for using the free + shared VID/PID pair. + - Added Readme.txt to the usbdrv directory which clarifies administrative + issues. + +2006-01-25: + - Added "configured state" to become more standards compliant. + - Added "HALT" state for interrupt endpoint. + - Driver passes the "USB Command Verifier" test from usb.org now. + - Made "serial number" a configuration option. + - Minor optimizations, we now recommend compiler option "-Os" for best + results. + - Added a version number to usbdrv.h + +2006-02-03: + - New configuration variable USB_BUFFER_SECTION for the memory section where + the USB rx buffer will go. This defaults to ".bss" if not defined. Since + this buffer MUST NOT cross 256 byte pages (not even touch a page at the + end), the user may want to pass a linker option similar to + "-Wl,--section-start=.mybuffer=0x800060". + - Provide structure for usbRequest_t. + - New defines for USB constants. + - Prepared for HID implementations. + - Increased data size limit for interrupt transfers to 8 bytes. + - New macro usbInterruptIsReady() to query interrupt buffer state. + +2006-02-18: + - Ensure that the data token which is sent as an ack to an OUT transfer is + always zero sized. This fixes a bug where the host reports an error after + sending an out transfer to the device, although all data arrived at the + device. + - Updated docs in usbdrv.h to reflect changed API in usbFunctionWrite(). + +* Release 2006-02-20 + + - Give a compiler warning when compiling with debugging turned on. + - Added Oleg Semyonov's changes for IAR-cc compatibility. + - Added new (optional) functions usbDeviceConnect() and usbDeviceDisconnect() + (also thanks to Oleg!). + - Rearranged tests in usbPoll() to save a couple of instructions in the most + likely case that no actions are pending. + - We need a delay between the SET ADDRESS request until the new address + becomes active. This delay was handled in usbPoll() until now. Since the + spec says that the delay must not exceed 2ms, previous versions required + aggressive polling during the enumeration phase. We have now moved the + handling of the delay into the interrupt routine. + - We must not reply with NAK to a SETUP transaction. We can only achieve this + by making sure that the rx buffer is empty when SETUP tokens are expected. + We therefore don't pass zero sized data packets from the status phase of + a transfer to usbPoll(). This change MAY cause troubles if you rely on + receiving a less than 8 bytes long packet in usbFunctionWrite() to + identify the end of a transfer. usbFunctionWrite() will NEVER be called + with a zero length. + +* Release 2006-03-14 + + - Improved IAR C support: tiny memory model, more devices + - Added template usbconfig.h file under the name usbconfig-prototype.h + +* Release 2006-03-26 + + - Added provision for one more interrupt-in endpoint (endpoint 3). + - Added provision for one interrupt-out endpoint (endpoint 1). + - Added flowcontrol macros for USB. + - Added provision for custom configuration descriptor. + - Allow ANY two port bits for D+ and D-. + - Merged (optional) receive endpoint number into global usbRxToken variable. + - Use USB_CFG_IOPORTNAME instead of USB_CFG_IOPORT. We now construct the + variable name from the single port letter instead of computing the address + of related ports from the output-port address. + +* Release 2006-06-26 + + - Updated documentation in usbdrv.h and usbconfig-prototype.h to reflect the + new features. + - Removed "#warning" directives because IAR does not understand them. Use + unused static variables instead to generate a warning. + - Do not include when compiling with IAR. + - Introduced USB_CFG_DESCR_PROPS_* in usbconfig.h to configure how each + USB descriptor should be handled. It is now possible to provide descriptor + data in Flash, RAM or dynamically at runtime. + - STALL is now a status in usbTxLen* instead of a message. We can now conform + to the spec and leave the stall status pending until it is cleared. + - Made usbTxPacketCnt1 and usbTxPacketCnt3 public. This allows the + application code to reset data toggling on interrupt pipes. + +* Release 2006-07-18 + + - Added an #if !defined __ASSEMBLER__ to the warning in usbdrv.h. This fixes + an assembler error. + - usbDeviceDisconnect() takes pull-up resistor to high impedance now. + +* Release 2007-02-01 + + - Merged in some code size improvements from usbtiny (thanks to Dick + Streefland for these optimizations!) + - Special alignment requirement for usbRxBuf not required any more. Thanks + again to Dick Streefland for this hint! + - Reverted to "#warning" instead of unused static variables -- new versions + of IAR CC should handle this directive. + - Changed Open Source license to GNU GPL v2 in order to make linking against + other free libraries easier. We no longer require publication of the + circuit diagrams, but we STRONGLY encourage it. If you improve the driver + itself, PLEASE grant us a royalty free license to your changes for our + commercial license. + +* Release 2007-03-29 + + - New configuration option "USB_PUBLIC" in usbconfig.h. + - Set USB version number to 1.10 instead of 1.01. + - Code used USB_CFG_DESCR_PROPS_STRING_DEVICE and + USB_CFG_DESCR_PROPS_STRING_PRODUCT inconsistently. Changed all occurrences + to USB_CFG_DESCR_PROPS_STRING_PRODUCT. + - New assembler module for 16.5 MHz RC oscillator clock with PLL in receiver + code. + - New assembler module for 16 MHz crystal. + - usbdrvasm.S contains common code only, clock-specific parts have been moved + to usbdrvasm12.S, usbdrvasm16.S and usbdrvasm165.S respectively. + +* Release 2007-06-25 + + - 16 MHz module: Do SE0 check in stuffed bits as well. + +* Release 2007-07-07 + + - Define hi8(x) for IAR compiler to limit result to 8 bits. This is necessary + for negative values. + - Added 15 MHz module contributed by V. Bosch. + - Interrupt vector name can now be configured. This is useful if somebody + wants to use a different hardware interrupt than INT0. + +* Release 2007-08-07 + + - Moved handleIn3 routine in usbdrvasm16.S so that relative jump range is + not exceeded. + - More config options: USB_RX_USER_HOOK(), USB_INITIAL_DATATOKEN, + USB_COUNT_SOF + - USB_INTR_PENDING can now be a memory address, not just I/O + +* Release 2007-09-19 + + - Split out common parts of assembler modules into separate include file + - Made endpoint numbers configurable so that given interface definitions + can be matched. See USB_CFG_EP3_NUMBER in usbconfig-prototype.h. + - Store endpoint number for interrupt/bulk-out so that usbFunctionWriteOut() + can handle any number of endpoints. + - Define usbDeviceConnect() and usbDeviceDisconnect() even if no + USB_CFG_PULLUP_IOPORTNAME is defined. Directly set D+ and D- to 0 in this + case. + +* Release 2007-12-01 + + - Optimize usbDeviceConnect() and usbDeviceDisconnect() for less code size + when USB_CFG_PULLUP_IOPORTNAME is not defined. + +* Release 2007-12-13 + + - Renamed all include-only assembler modules from *.S to *.inc so that + people don't add them to their project sources. + - Distribute leap bits in tx loop more evenly for 16 MHz module. + - Use "macro" and "endm" instead of ".macro" and ".endm" for IAR + - Avoid compiler warnings for constant expr range by casting some values in + USB descriptors. + +* Release 2008-01-21 + + - Fixed bug in 15 and 16 MHz module where the new address set with + SET_ADDRESS was already accepted at the next NAK or ACK we send, not at + the next data packet we send. This caused problems when the host polled + too fast. Thanks to Alexander Neumann for his help and patience debugging + this issue! + +* Release 2008-02-05 + + - Fixed bug in 16.5 MHz module where a register was used in the interrupt + handler before it was pushed. This bug was introduced with version + 2007-09-19 when common parts were moved to a separate file. + - Optimized CRC routine (thanks to Reimar Doeffinger). + +* Release 2008-02-16 + + - Removed outdated IAR compatibility stuff (code sections). + - Added hook macros for USB_RESET_HOOK() and USB_SET_ADDRESS_HOOK(). + - Added optional routine usbMeasureFrameLength() for calibration of the + internal RC oscillator. + +* Release 2008-02-28 + + - USB_INITIAL_DATATOKEN defaults to USBPID_DATA1 now, which means that we + start with sending USBPID_DATA0. + - Changed defaults in usbconfig-prototype.h + - Added free USB VID/PID pair for MIDI class devices + - Restructured AVR-USB as separate package, not part of PowerSwitch any more. + +* Release 2008-04-18 + + - Restructured usbdrv.c so that it is easier to read and understand. + - Better code optimization with gcc 4. + - If a second interrupt in endpoint is enabled, also add it to config + descriptor. + - Added config option for long transfers (above 254 bytes), see + USB_CFG_LONG_TRANSFERS in usbconfig.h. + - Added 20 MHz module contributed by Jeroen Benschop. + +* Release 2008-05-13 + + - Fixed bug in libs-host/hiddata.c function usbhidGetReport(): length + was not incremented, pointer to length was incremented instead. + - Added code to command line tool(s) which claims an interface. This code + is disabled by default, but may be necessary on newer Linux kernels. + - Added usbconfig.h option "USB_CFG_CHECK_DATA_TOGGLING". + - New header "usbportability.h" prepares ports to other development + environments. + - Long transfers (above 254 bytes) did not work when usbFunctionRead() was + used to supply the data. Fixed this bug. [Thanks to Alexander Neumann!] + - In hiddata.c (example code for sending/receiving data over HID), use + USB_RECIP_DEVICE instead of USB_RECIP_INTERFACE for control transfers so + that we need not claim the interface. + - in usbPoll() loop 20 times polling for RESET state instead of 10 times. + This accounts for the higher clock rates we now support. + - Added a module for 12.8 MHz RC oscillator with PLL in receiver loop. + - Added hook to SOF code so that oscillator can be tuned to USB frame clock. + - Added timeout to waitForJ loop. Helps preventing unexpected hangs. + - Added example code for oscillator tuning to libs-device (thanks to + Henrik Haftmann for the idea to this routine). + - Implemented option USB_CFG_SUPPRESS_INTR_CODE. + +* Release 2008-10-22 + + - Fixed libs-device/osctune.h: OSCCAL is memory address on ATMega88 and + similar, not offset of 0x20 needs to be added. + - Allow distribution under GPLv3 for those who have to link against other + code distributed under GPLv3. + +* Release 2008-11-26 + + - Removed libusb-win32 dependency for hid-data example in Makefile.windows. + It was never required and confused many people. + - Added extern uchar usbRxToken to usbdrv.h. + - Integrated a module with CRC checks at 18 MHz by Lukas Schrittwieser. + +* Release 2009-03-23 + + - Hid-mouse example used settings from hid-data example, fixed that. + - Renamed project to V-USB due to a trademark issue with Atmel(r). + - Changed CommercialLicense.txt and USBID-License.txt to make the + background of USB ID registration clearer. + +* Release 2009-04-15 + + - Changed CommercialLicense.txt to reflect the new range of PIDs from + Jason Kotzin. + - Removed USBID-License.txt in favor of USB-IDs-for-free.txt and + USB-ID-FAQ.txt + - Fixed a bug in the 12.8 MHz module: End Of Packet decection was made in + the center between bit 0 and 1 of each byte. This is where the data lines + are expected to change and the sampled data may therefore be nonsense. + We therefore check EOP ONLY if bits 0 AND 1 have both been read as 0 on D-. + - Fixed a bitstuffing problem in the 16 MHz module: If bit 6 was stuffed, + the unstuffing code in the receiver routine was 1 cycle too long. If + multiple bytes had the unstuffing in bit 6, the error summed up until the + receiver was out of sync. + - Included option for faster CRC routine. + Thanks to Slawomir Fras (BoskiDialer) for this code! + - Updated bits in Configuration Descriptor's bmAttributes according to + USB 1.1 (in particular bit 7, it is a must-be-set bit now). + +* Release 2009-08-22 diff --git a/firmware/fnordlicht-controller/usbdrv/CommercialLicense.txt b/firmware/fnordlicht-controller/usbdrv/CommercialLicense.txt new file mode 100644 index 0000000..11d07d9 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/CommercialLicense.txt @@ -0,0 +1,166 @@ +V-USB Driver Software License Agreement +Version 2009-08-03 + +THIS LICENSE AGREEMENT GRANTS YOU CERTAIN RIGHTS IN A SOFTWARE. YOU CAN +ENTER INTO THIS AGREEMENT AND ACQUIRE THE RIGHTS OUTLINED BELOW BY PAYING +THE AMOUNT ACCORDING TO SECTION 4 ("PAYMENT") TO OBJECTIVE DEVELOPMENT. + + +1 DEFINITIONS + +1.1 "OBJECTIVE DEVELOPMENT" shall mean OBJECTIVE DEVELOPMENT Software GmbH, +Grosse Schiffgasse 1A/7, 1020 Wien, AUSTRIA. + +1.2 "You" shall mean the Licensee. + +1.3 "V-USB" shall mean all files included in the package distributed under +the name "vusb" by OBJECTIVE DEVELOPMENT (http://www.obdev.at/vusb/) +unless otherwise noted. This includes the firmware-only USB device +implementation for Atmel AVR microcontrollers, some simple device examples +and host side software examples and libraries. + + +2 LICENSE GRANTS + +2.1 Source Code. OBJECTIVE DEVELOPMENT shall furnish you with the source +code of V-USB. + +2.2 Distribution and Use. OBJECTIVE DEVELOPMENT grants you the +non-exclusive right to use, copy and distribute V-USB with your hardware +product(s), restricted by the limitations in section 3 below. + +2.3 Modifications. OBJECTIVE DEVELOPMENT grants you the right to modify +the source code and your copy of V-USB according to your needs. + +2.4 USB IDs. OBJECTIVE DEVELOPMENT furnishes you with one or two USB +Product ID(s), sent to you in e-mail. These Product IDs are reserved +exclusively for you. OBJECTIVE DEVELOPMENT has obtained USB Product ID +ranges under the Vendor ID 5824 from Wouter van Ooijen (Van Ooijen +Technische Informatica, www.voti.nl) and under the Vendor ID 8352 from +Jason Kotzin (Clay Logic, www.claylogic.com). Both owners of the Vendor IDs +have obtained these IDs from the USB Implementers Forum, Inc. +(www.usb.org). OBJECTIVE DEVELOPMENT disclaims all liability which might +arise from the assignment of USB IDs. + +2.5 USB Certification. Although not part of this agreement, we want to make +it clear that you cannot become USB certified when you use V-USB or a USB +Product ID assigned by OBJECTIVE DEVELOPMENT. AVR microcontrollers don't +meet the electrical specifications required by the USB specification and +the USB Implementers Forum certifies only members who bought a Vendor ID of +their own. + + +3 LICENSE RESTRICTIONS + +3.1 Number of Units. Only one of the following three definitions is +applicable. Which one is determined by the amount you pay to OBJECTIVE +DEVELOPMENT, see section 4 ("Payment") below. + +Hobby License: You may use V-USB according to section 2 above in no more +than 5 hardware units. These units must not be sold for profit. + +Entry Level License: You may use V-USB according to section 2 above in no +more than 150 hardware units. + +Professional License: You may use V-USB according to section 2 above in +any number of hardware units, except for large scale production ("unlimited +fair use"). Quantities below 10,000 units are not considered large scale +production. If your reach quantities which are obviously large scale +production, you must pay a license fee of 0.10 EUR per unit for all units +above 10,000. + +3.2 Rental. You may not rent, lease, or lend V-USB or otherwise encumber +any copy of V-USB, or any of the rights granted herein. + +3.3 Transfer. You may not transfer your rights under this Agreement to +another party without OBJECTIVE DEVELOPMENT's prior written consent. If +such consent is obtained, you may permanently transfer this License to +another party. The recipient of such transfer must agree to all terms and +conditions of this Agreement. + +3.4 Reservation of Rights. OBJECTIVE DEVELOPMENT retains all rights not +expressly granted. + +3.5 Non-Exclusive Rights. Your license rights under this Agreement are +non-exclusive. + +3.6 Third Party Rights. This Agreement cannot grant you rights controlled +by third parties. In particular, you are not allowed to use the USB logo or +other trademarks owned by the USB Implementers Forum, Inc. without their +consent. Since such consent depends on USB certification, it should be +noted that V-USB will not pass certification because it does not +implement checksum verification and the microcontroller ports do not meet +the electrical specifications. + + +4 PAYMENT + +The payment amount depends on the variation of this agreement (according to +section 3.1) into which you want to enter. Concrete prices are listed on +OBJECTIVE DEVELOPMENT's web site, usually at +http://www.obdev.at/vusb/license.html. You agree to pay the amount listed +there to OBJECTIVE DEVELOPMENT or OBJECTIVE DEVELOPMENT's payment processor +or reseller. + + +5 COPYRIGHT AND OWNERSHIP + +V-USB is protected by copyright laws and international copyright +treaties, as well as other intellectual property laws and treaties. V-USB +is licensed, not sold. + + +6 TERM AND TERMINATION + +6.1 Term. This Agreement shall continue indefinitely. However, OBJECTIVE +DEVELOPMENT may terminate this Agreement and revoke the granted license and +USB-IDs if you fail to comply with any of its terms and conditions. + +6.2 Survival of Terms. All provisions regarding secrecy, confidentiality +and limitation of liability shall survive termination of this agreement. + + +7 DISCLAIMER OF WARRANTY AND LIABILITY + +LIMITED WARRANTY. V-USB IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, OBJECTIVE +DEVELOPMENT AND ITS SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES, EITHER +EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND +NON-INFRINGEMENT, WITH REGARD TO V-USB, AND THE PROVISION OF OR FAILURE +TO PROVIDE SUPPORT SERVICES. THIS LIMITED WARRANTY GIVES YOU SPECIFIC LEGAL +RIGHTS. YOU MAY HAVE OTHERS, WHICH VARY FROM STATE/JURISDICTION TO +STATE/JURISDICTION. + +LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, +IN NO EVENT SHALL OBJECTIVE DEVELOPMENT OR ITS SUPPLIERS BE LIABLE FOR ANY +SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER +(INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, +BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY +LOSS) ARISING OUT OF THE USE OF OR INABILITY TO USE V-USB OR THE +PROVISION OF OR FAILURE TO PROVIDE SUPPORT SERVICES, EVEN IF OBJECTIVE +DEVELOPMENT HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. IN ANY +CASE, OBJECTIVE DEVELOPMENT'S ENTIRE LIABILITY UNDER ANY PROVISION OF THIS +AGREEMENT SHALL BE LIMITED TO THE AMOUNT ACTUALLY PAID BY YOU FOR V-USB. + + +8 MISCELLANEOUS TERMS + +8.1 Marketing. OBJECTIVE DEVELOPMENT has the right to mention for marketing +purposes that you entered into this agreement. + +8.2 Entire Agreement. This document represents the entire agreement between +OBJECTIVE DEVELOPMENT and you. It may only be modified in writing signed by +an authorized representative of both, OBJECTIVE DEVELOPMENT and you. + +8.3 Severability. In case a provision of these terms and conditions should +be or become partly or entirely invalid, ineffective, or not executable, +the validity of all other provisions shall not be affected. + +8.4 Applicable Law. This agreement is governed by the laws of the Republic +of Austria. + +8.5 Responsible Courts. The responsible courts in Vienna/Austria will have +exclusive jurisdiction regarding all disputes in connection with this +agreement. + diff --git a/firmware/fnordlicht-controller/usbdrv/License.txt b/firmware/fnordlicht-controller/usbdrv/License.txt new file mode 100644 index 0000000..4460cfb --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/License.txt @@ -0,0 +1,361 @@ +OBJECTIVE DEVELOPMENT GmbH's V-USB driver software is distributed under the +terms and conditions of the GNU GPL version 2 or the GNU GPL version 3. It is +your choice whether you apply the terms of version 2 or version 3. The full +text of GPLv2 is included below. In addition to the requirements in the GPL, +we STRONGLY ENCOURAGE you to do the following: + +(1) Publish your entire project on a web site and drop us a note with the URL. +Use the form at http://www.obdev.at/vusb/feedback.html for your submission. + +(2) Adhere to minimum publication standards. Please include AT LEAST: + - a circuit diagram in PDF, PNG or GIF format + - full source code for the host software + - a Readme.txt file in ASCII format which describes the purpose of the + project and what can be found in which directories and which files + - a reference to http://www.obdev.at/vusb/ + +(3) If you improve the driver firmware itself, please give us a free license +to your modifications for our commercial license offerings. + + + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. 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 program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/firmware/fnordlicht-controller/usbdrv/Readme.txt b/firmware/fnordlicht-controller/usbdrv/Readme.txt new file mode 100644 index 0000000..a010d97 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/Readme.txt @@ -0,0 +1,158 @@ +This is the Readme file to Objective Development's firmware-only USB driver +for Atmel AVR microcontrollers. For more information please visit +http://www.obdev.at/vusb/ + +This directory contains the USB firmware only. Copy it as-is to your own +project and add all .c and .S files to your project (these files are marked +with an asterisk in the list below). Then copy usbconfig-prototype.h as +usbconfig.h to your project and edit it according to your configuration. + + +TECHNICAL DOCUMENTATION +======================= +The technical documentation (API) for the firmware driver is contained in the +file "usbdrv.h". Please read all of it carefully! Configuration options are +documented in "usbconfig-prototype.h". + +The driver consists of the following files: + Readme.txt ............. The file you are currently reading. + Changelog.txt .......... Release notes for all versions of the driver. + usbdrv.h ............... Driver interface definitions and technical docs. +* usbdrv.c ............... High level language part of the driver. Link this + module to your code! +* usbdrvasm.S ............ Assembler part of the driver. This module is mostly + a stub and includes one of the usbdrvasm*.S files + depending on processor clock. Link this module to + your code! + usbdrvasm*.inc ......... Assembler routines for particular clock frequencies. + Included by usbdrvasm.S, don't link it directly! + asmcommon.inc .......... Common assembler routines. Included by + usbdrvasm*.inc, don't link it directly! + usbconfig-prototype.h .. Prototype for your own usbdrv.h file. +* oddebug.c .............. Debug functions. Only used when DEBUG_LEVEL is + defined to a value greater than 0. Link this module + to your code! + oddebug.h .............. Interface definitions of the debug module. + usbportability.h ....... Header with compiler-dependent stuff. + usbdrvasm.asm .......... Compatibility stub for IAR-C-compiler. Use this + module instead of usbdrvasm.S when you assembler + with IAR's tools. + License.txt ............ Open Source license for this driver. + CommercialLicense.txt .. Optional commercial license for this driver. + USB-ID-FAQ.txt ......... General infos about USB Product- and Vendor-IDs. + USB-IDs-for-free.txt ... List and terms of use for free shared PIDs. + +(*) ... These files should be linked to your project. + + +CPU CORE CLOCK FREQUENCY +======================== +We supply assembler modules for clock frequencies of 12 MHz, 12.8 MHz, 15 MHz, +16 MHz, 16.5 MHz 18 MHz and 20 MHz. Other clock rates are not supported. The +actual clock rate must be configured in usbdrv.h unless you use the default +12 MHz. + +12 MHz Clock +This is the traditional clock rate of V-USB because it's the lowest clock +rate where the timing constraints of the USB spec can be met. + +15 MHz Clock +Similar to 12 MHz, but some NOPs inserted. On the other hand, the higher clock +rate allows for some loops which make the resulting code size somewhat smaller +than the 12 MHz version. + +16 MHz Clock +This clock rate has been added for users of the Arduino board and other +ready-made boards which come with a fixed 16 MHz crystal. It's also an option +if you need the slightly higher clock rate for performance reasons. Since +16 MHz is not divisible by the USB low speed bit clock of 1.5 MHz, the code +is somewhat tricky and has to insert a leap cycle every third byte. + +12.8 MHz and 16.5 MHz Clock +The assembler modules for these clock rates differ from the other modules +because they have been built for an RC oscillator with only 1% precision. The +receiver code inserts leap cycles to compensate for clock deviations. 1% is +also the precision which can be achieved by calibrating the internal RC +oscillator of the AVR. Please note that only AVRs with internal 64 MHz PLL +oscillator can reach 16.5 MHz with the RC oscillator. This includes the very +popular ATTiny25, ATTiny45, ATTiny85 series as well as the ATTiny26. Almost +all AVRs can reach 12.8 MHz, although this is outside the specified range. + +See the EasyLogger example at http://www.obdev.at/vusb/easylogger.html for +code which calibrates the RC oscillator based on the USB frame clock. + +18 MHz Clock +This module is closer to the USB specification because it performs an on the +fly CRC check for incoming packets. Packets with invalid checksum are +discarded as required by the spec. If you also implement checks for data +PID toggling on application level (see option USB_CFG_CHECK_DATA_TOGGLING +in usbconfig.h for more info), this ensures data integrity. Due to the CRC +tables and alignment requirements, this code is bigger than modules for other +clock rates. To activate this module, you must define USB_CFG_CHECK_CRC to 1 +and USB_CFG_CLOCK_KHZ to 18000 in usbconfig.h. + +20 MHz Clock +This module is for people who won't do it with less than the maximum. Since +20 MHz is not divisible by the USB low speed bit clock of 1.5 MHz, the code +uses similar tricks as the 16 MHz module to insert leap cycles. + + +USB IDENTIFIERS +=============== +Every USB device needs a vendor- and a product-identifier (VID and PID). VIDs +are obtained from usb.org for a price of 1,500 USD. Once you have a VID, you +can assign PIDs at will. + +Since an entry level cost of 1,500 USD is too high for most small companies +and hobbyists, we provide some VID/PID pairs for free. See the file +USB-IDs-for-free.txt for details. + +Objective Development also has some license offerings which include product +IDs. See http://www.obdev.at/vusb/ for details. + + +DEVELOPMENT SYSTEM +================== +This driver has been developed and optimized for the GNU compiler version 3 +(gcc 3). It does work well with gcc 4, but with bigger code size. We recommend +that you use the GNU compiler suite because it is freely available. V-USB +has also been ported to the IAR compiler and assembler. It has been tested +with IAR 4.10B/W32 and 4.12A/W32 on an ATmega8 with the "small" and "tiny" +memory model. Not every release is tested with IAR CC and the driver may +therefore fail to compile with IAR. Please note that gcc is more efficient for +usbdrv.c because this module has been deliberately optimized for gcc. + + +USING V-USB FOR FREE +==================== +The AVR firmware driver is published under the GNU General Public License +Version 2 (GPL2) and the GNU General Public License Version 3 (GPL3). It is +your choice whether you apply the terms of version 2 or version 3. + +If you decide for the free GPL2 or GPL3, we STRONGLY ENCOURAGE you to do the +following things IN ADDITION to the obligations from the GPL: + +(1) Publish your entire project on a web site and drop us a note with the URL. +Use the form at http://www.obdev.at/vusb/feedback.html for your submission. +If you don't have a web site, you can publish the project in obdev's +documentation wiki at +http://www.obdev.at/goto.php?t=vusb-wiki&p=hosted-projects. + +(2) Adhere to minimum publication standards. Please include AT LEAST: + - a circuit diagram in PDF, PNG or GIF format + - full source code for the host software + - a Readme.txt file in ASCII format which describes the purpose of the + project and what can be found in which directories and which files + - a reference to http://www.obdev.at/vusb/ + +(3) If you improve the driver firmware itself, please give us a free license +to your modifications for our commercial license offerings. + + +COMMERCIAL LICENSES FOR V-USB +============================= +If you don't want to publish your source code under the terms of the GPL, +you can simply pay money for V-USB. As an additional benefit you get +USB PIDs for free, reserved exclusively to you. See the file +"CommercialLicense.txt" for details. + diff --git a/firmware/fnordlicht-controller/usbdrv/USB-ID-FAQ.txt b/firmware/fnordlicht-controller/usbdrv/USB-ID-FAQ.txt new file mode 100644 index 0000000..d1de8fb --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/USB-ID-FAQ.txt @@ -0,0 +1,149 @@ +Version 2009-08-22 + +========================== +WHY DO WE NEED THESE IDs? +========================== + +USB is more than a low level protocol for data transport. It also defines a +common set of requests which must be understood by all devices. And as part +of these common requests, the specification defines data structures, the +USB Descriptors, which are used to describe the properties of the device. + +From the perspective of an operating system, it is therefore possible to find +out basic properties of a device (such as e.g. the manufacturer and the name +of the device) without a device-specific driver. This is essential because +the operating system can choose a driver to load based on this information +(Plug-And-Play). + +Among the most important properties in the Device Descriptor are the USB +Vendor- and Product-ID. Both are 16 bit integers. The most simple form of +driver matching is based on these IDs. The driver announces the Vendor- and +Product-IDs of the devices it can handle and the operating system loads the +appropriate driver when the device is connected. + +It is obvious that this technique only works if the pair Vendor- plus +Product-ID is unique: Only devices which require the same driver can have the +same pair of IDs. + + +===================================================== +HOW DOES THE USB STANDARD ENSURE THAT IDs ARE UNIQUE? +===================================================== + +Since it is so important that USB IDs are unique, the USB Implementers Forum, +Inc. (usb.org) needs a way to enforce this legally. It is not forbidden by +law to build a device and assign it any random numbers as IDs. Usb.org +therefore needs an agreement to regulate the use of USB IDs. The agreement +binds only parties who agreed to it, of course. Everybody else is free to use +any numbers for their IDs. + +So how can usb.org ensure that every manufacturer of USB devices enters into +an agreement with them? They do it via trademark licensing. Usb.org has +registered the trademark "USB", all associated logos and related terms. If +you want to put an USB logo on your product or claim that it is USB +compliant, you must license these trademarks from usb.org. And this is where +you enter into an agreement. See the "USB-IF Trademark License Agreement and +Usage Guidelines for the USB-IF Logo" at +http://www.usb.org/developers/logo_license/. + +Licensing the USB trademarks requires that you buy a USB Vendor-ID from +usb.org (one-time fee of ca. 2,000 USD), that you become a member of usb.org +(yearly fee of ca. 4,000 USD) and that you meet all the technical +specifications from the USB spec. + +This means that most hobbyists and small companies will never be able to +become USB compliant, just because membership is so expensive. And you can't +be compliant with a driver based on V-USB anyway, because the AVR's port pins +don't meet the electrical specifications for USB. So, in principle, all +hobbyists and small companies are free to choose any random numbers for their +IDs. They have nothing to lose... + +There is one exception worth noting, though: If you use a sub-component which +implements USB, the vendor of the sub-components may guarantee USB +compliance. This might apply to some or all of FTDI's solutions. + + +======================================================================= +WHY SHOULD YOU OBTAIN USB IDs EVEN IF YOU DON'T LICENSE USB TRADEMARKS? +======================================================================= + +You have learned in the previous section that you are free to choose any +numbers for your IDs anyway. So why not do exactly this? There is still the +technical issue. If you choose IDs which are already in use by somebody else, +operating systems will load the wrong drivers and your device won't work. +Even if you choose IDs which are not currently in use, they may be in use in +the next version of the operating system or even after an automatic update. + +So what you need is a pair of Vendor- and Product-IDs for which you have the +guarantee that no USB compliant product uses them. This implies that no +operating system will ever ship with drivers responsible for these IDs. + + +============================================== +HOW DOES OBJECTIVE DEVELOPMENT HANDLE USB IDs? +============================================== + +Objective Development gives away pairs of USB-IDs with their V-USB licenses. +In order to ensure that these IDs are unique, Objective Development has an +agreement with the company/person who has bought the USB Vendor-ID from +usb.org. This agreement ensures that a range of USB Product-IDs is reserved +for assignment by Objective Development and that the owner of the Vendor-ID +won't give it to anybody else. + +This means that you have to trust three parties to ensure uniqueness of +your IDs: + + - Objective Development, that they don't give the same PID to more than + one person. + - The owner of the Vendor-ID that they don't assign PIDs from the range + assigned to Objective Development to anybody else. + - Usb.org that they don't assign the same Vendor-ID a second time. + + +================================== +WHO IS THE OWNER OF THE VENDOR-ID? +================================== + +Objective Development has obtained ranges of USB Product-IDs under two +Vendor-IDs: Under Vendor-ID 5824 from Wouter van Ooijen (Van Ooijen +Technische Informatica, www.voti.nl) and under Vendor-ID 8352 from Jason +Kotzin (Clay Logic, www.claylogic.com). Both VID owners have received their +Vendor-ID directly from usb.org. + + +========================================================================= +CAN I USE USB-IDs FROM OBJECTIVE DEVELOPMENT WITH OTHER DRIVERS/HARDWARE? +========================================================================= + +The short answer is: Yes. All you get is a guarantee that the IDs are never +assigned to anybody else. What more do you need? + + +============================ +WHAT ABOUT SHARED ID PAIRS? +============================ + +Objective Development has reserved some PID/VID pairs for shared use. You +have no guarantee of uniqueness for them, except that no USB compliant device +uses them. In order to avoid technical problems, we must ensure that all +devices with the same pair of IDs use the same driver on kernel level. For +details, see the file USB-IDs-for-free.txt. + + +====================================================== +I HAVE HEARD THAT SUB-LICENSING OF USB-IDs IS ILLEGAL? +====================================================== + +A 16 bit integer number cannot be protected by copyright laws. It is not +sufficiently complex. And since none of the parties involved entered into the +USB-IF Trademark License Agreement, we are not bound by this agreement. So +there is no reason why it should be illegal to sub-license USB-IDs. + + +============================================= +WHO IS LIABLE IF THERE ARE INCOMPATIBILITIES? +============================================= + +Objective Development disclaims all liabilities which might arise from the +assignment of IDs. If you guarantee product features to your customers +without proper disclaimer, YOU are liable for that. diff --git a/firmware/fnordlicht-controller/usbdrv/USB-IDs-for-free.txt b/firmware/fnordlicht-controller/usbdrv/USB-IDs-for-free.txt new file mode 100644 index 0000000..2f4d59a --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/USB-IDs-for-free.txt @@ -0,0 +1,148 @@ +Version 2009-08-22 + +=========================== +FREE USB-IDs FOR SHARED USE +=========================== + +Objective Development has reserved a set of USB Product-IDs for use according +to the guidelines outlined below. For more information about the concept of +USB IDs please see the file USB-ID-FAQ.txt. Objective Development guarantees +that the IDs listed below are not used by any USB compliant devices. + + +==================== +MECHANISM OF SHARING +==================== + +From a technical point of view, two different devices can share the same USB +Vendor- and Product-ID if they require the same driver on operating system +level. We make use of this fact by assigning separate IDs for various device +classes. On application layer, devices must be distinguished by their textual +name or serial number. We offer separate sets of IDs for discrimination by +textual name and for serial number. + +Examples for shared use of USB IDs are included with V-USB in the "examples" +subdirectory. + + +====================================== +IDs FOR DISCRIMINATION BY TEXTUAL NAME +====================================== + +If you use one of the IDs listed below, your device and host-side software +must conform to these rules: + +(1) The USB device MUST provide a textual representation of the manufacturer +and product identification. The manufacturer identification MUST be available +at least in USB language 0x0409 (English/US). + +(2) The textual manufacturer identification MUST contain either an Internet +domain name (e.g. "mycompany.com") registered and owned by you, or an e-mail +address under your control (e.g. "myname@gmx.net"). You can embed the domain +name or e-mail address in any string you like, e.g. "Objective Development +http://www.obdev.at/vusb/". + +(3) You are responsible for retaining ownership of the domain or e-mail +address for as long as any of your products are in use. + +(4) You may choose any string for the textual product identification, as long +as this string is unique within the scope of your textual manufacturer +identification. + +(5) Application side device look-up MUST be based on the textual manufacturer +and product identification in addition to VID/PID matching. The driver +matching MUST be a comparison of the entire strings, NOT a sub-string match. + +(6) For devices which implement a particular USB device class (e.g. HID), the +operating system's default class driver MUST be used. If an operating system +driver for Vendor Class devices is needed, this driver must be libusb or +libusb-win32 (see http://libusb.org/ and +http://libusb-win32.sourceforge.net/). + +Table if IDs for discrimination by textual name: + +PID dec (hex) | VID dec (hex) | Description of use +==============+===============+============================================ +1500 (0x05dc) | 5824 (0x16c0) | For Vendor Class devices with libusb +--------------+---------------+-------------------------------------------- +1503 (0x05df) | 5824 (0x16c0) | For generic HID class devices (which are + | | NOT mice, keyboards or joysticks) +--------------+---------------+-------------------------------------------- +1505 (0x05e1) | 5824 (0x16c0) | For CDC-ACM class devices (modems) +--------------+---------------+-------------------------------------------- +1508 (0x05e4) | 5824 (0x16c0) | For MIDI class devices +--------------+---------------+-------------------------------------------- + +Note that Windows caches the textual product- and vendor-description for +mice, keyboards and joysticks. Name-bsed discrimination is therefore not +recommended for these device classes. + + +======================================= +IDs FOR DISCRIMINATION BY SERIAL NUMBER +======================================= + +If you use one of the IDs listed below, your device and host-side software +must conform to these rules: + +(1) The USB device MUST provide a textual representation of the serial +number. The serial number string MUST be available at least in USB language +0x0409 (English/US). + +(2) The serial number MUST start with either an Internet domain name (e.g. +"mycompany.com") registered and owned by you, or an e-mail address under your +control (e.g. "myname@gmx.net"), both terminated with a colon (":") character. +You MAY append any string you like for further discrimination of your devices. + +(3) You are responsible for retaining ownership of the domain or e-mail +address for as long as any of your products are in use. + +(5) Application side device look-up MUST be based on the serial number string +in addition to VID/PID matching. The matching must start at the first +character of the serial number string and include the colon character +terminating your domain or e-mail address. It MAY stop anywhere after that. + +(6) For devices which implement a particular USB device class (e.g. HID), the +operating system's default class driver MUST be used. If an operating system +driver for Vendor Class devices is needed, this driver must be libusb or +libusb-win32 (see http://libusb.org/ and +http://libusb-win32.sourceforge.net/). + +Table if IDs for discrimination by serial number string: + +PID dec (hex) | VID dec (hex) | Description of use +===============+===============+=========================================== +10200 (0x27d8) | 5824 (0x16c0) | For Vendor Class devices with libusb +---------------+---------------+------------------------------------------- +10201 (0x27d9) | 5824 (0x16c0) | For generic HID class devices (which are + | | NOT mice, keyboards or joysticks) +---------------+---------------+------------------------------------------- +10202 (0x27da) | 5824 (0x16c0) | For USB Mice +---------------+---------------+------------------------------------------- +10203 (0x27db) | 5824 (0x16c0) | For USB Keyboards +---------------+---------------+------------------------------------------- +10204 (0x27db) | 5824 (0x16c0) | For USB Joysticks +---------------+---------------+------------------------------------------- +10205 (0x27dc) | 5824 (0x16c0) | For CDC-ACM class devices (modems) +---------------+---------------+------------------------------------------- +10206 (0x27dd) | 5824 (0x16c0) | For MIDI class devices +---------------+---------------+------------------------------------------- + + +================= +ORIGIN OF USB-IDs +================= + +OBJECTIVE DEVELOPMENT Software GmbH has obtained all VID/PID pairs listed +here from Wouter van Ooijen (see www.voti.nl) for exclusive disposition. +Wouter van Ooijen has obtained the VID from the USB Implementers Forum, Inc. +(see www.usb.org). The VID is registered for the company name "Van Ooijen +Technische Informatica". + + +========== +DISCLAIMER +========== + +OBJECTIVE DEVELOPMENT Software GmbH disclaims all liability for any +problems which are caused by the shared use of these VID/PID pairs. diff --git a/firmware/fnordlicht-controller/usbdrv/USBID-License.txt b/firmware/fnordlicht-controller/usbdrv/USBID-License.txt new file mode 100644 index 0000000..c40be92 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/USBID-License.txt @@ -0,0 +1,154 @@ +Royalty-Free Non-Exclusive Use of USB Product-IDs +================================================= + +Version 2009-04-13 + +Strictly speaking, this is not a license. You can't give a license to use +a simple number (such as e.g. 1500) for any purpose. This is a set of rules +which should make it possible to build USB devices without the requirement +for individual USB IDs. If you break one of the rules, you will run into +technical problems sooner or later, but you don't risk legal trouble. + + +OBJECTIVE DEVELOPMENT Software GmbH hereby grants you the non-exclusive +right to use four USB.org vendor-ID (VID) / product-ID (PID) pairs with +products based on Objective Development's firmware-only USB driver for +Atmel AVR microcontrollers: + + * VID = 5824 (=0x16c0) / PID = 1500 (=0x5dc) for devices implementing no + USB device class (vendor-class devices with USB class = 0xff). Devices + using this pair will be referred to as "VENDOR CLASS" devices. + + * VID = 5824 (=0x16c0) / PID = 1503 (=0x5df) for HID class devices + (excluding mice and keyboards). Devices using this pair will be referred + to as "HID CLASS" devices. + + * VID = 5824 (=0x16c0) / PID = 1505 (=0x5e1) for CDC class modem devices + Devices using this pair will be referred to as "CDC-ACM CLASS" devices. + + * VID = 5824 (=0x16c0) / PID = 1508 (=0x5e4) for MIDI class devices + Devices using this pair will be referred to as "MIDI CLASS" devices. + +Since the granted right is non-exclusive, the same VID/PID pairs may be +used by many companies and individuals for different products. To avoid +conflicts, your device and host driver software MUST adhere to the rules +outlined below. + +OBJECTIVE DEVELOPMENT Software GmbH has obtained these VID/PID pairs from +Wouter van Ooijen (see www.voti.nl) for exclusive disposition. Wouter van +Ooijen has obtained the VID from the USB Implementers Forum, Inc. +(see www.usb.org). The VID is registered for the company name +"Van Ooijen Technische Informatica". + + +RULES AND RESTRICTIONS +====================== + +(1) The USB device MUST provide a textual representation of the +manufacturer and product identification. The manufacturer identification +MUST be available at least in USB language 0x0409 (English/US). + +(2) The textual manufacturer identification MUST contain either an Internet +domain name (e.g. "mycompany.com") registered and owned by you, or an +e-mail address under your control (e.g. "myname@gmx.net"). You can embed +the domain name or e-mail address in any string you like, e.g. "Objective +Development http://www.obdev.at/vusb/". + +(3) You are responsible for retaining ownership of the domain or e-mail +address for as long as any of your products are in use. + +(4) You may choose any string for the textual product identification, as +long as this string is unique within the scope of your textual manufacturer +identification. + +(5) Matching of device-specific drivers MUST be based on the textual +manufacturer and product identification in addition to the usual VID/PID +matching. This means that operating system features which are based on +VID/PID matching only (e.g. Windows kernel level drivers, automatic actions +when the device is plugged in etc) MUST NOT be used. The driver matching +MUST be a comparison of the entire strings, NOT a sub-string match. For +CDC-ACM CLASS and MIDI CLASS devices, a generic class driver should be used +and the matching is based on the USB device class. + +(6) The extent to which VID/PID matching is allowed for non device-specific +drivers or features depends on the operating system and particular VID/PID +pair used: + + * Mac OS X, Linux, FreeBSD and other Unixes: No VID/PID matching is + required and hence no VID/PID-only matching is allowed at all. + + * Windows: The operating system performs VID/PID matching for the kernel + level driver. You are REQUIRED to use libusb-win32 (see + http://libusb-win32.sourceforge.net/) as the kernel level driver for + VENDOR CLASS devices. HID CLASS devices all use the generic HID class + driver shipped with Windows, except mice and keyboards. You therefore + MUST NOT use any of the shared VID/PID pairs for mice or keyboards. + CDC-ACM CLASS devices require a ".inf" file which matches on the VID/PID + pair. This ".inf" file MUST load the "usbser" driver to configure the + device as modem (COM-port). + +(7) OBJECTIVE DEVELOPMENT Software GmbH disclaims all liability for any +problems which are caused by the shared use of these VID/PID pairs. You +have been warned that the sharing of VID/PID pairs may cause problems. If +you want to avoid them, get your own VID/PID pair for exclusive use. + + +HOW TO IMPLEMENT THESE RULES +============================ + +The following rules are for VENDOR CLASS and HID CLASS devices. CDC-ACM +CLASS and MIDI CLASS devices use the operating system's class driver and +don't need a custom driver. + +The host driver MUST iterate over all devices with the given VID/PID +numbers in their device descriptors and query the string representation for +the manufacturer name in USB language 0x0409 (English/US). It MUST compare +the ENTIRE string with your textual manufacturer identification chosen in +(2) above. A substring search for your domain or e-mail address is NOT +acceptable. The driver MUST NOT touch the device (other than querying the +descriptors) unless the strings match. + +For all USB devices with matching VID/PID and textual manufacturer +identification, the host driver must query the textual product +identification and string-compare it with the name of the product it can +control. It may only initialize the device if the product matches exactly. + +Objective Development provides examples for these matching rules with the +"PowerSwitch" project (using libusb) and with the "Automator" project +(using Windows calls on Windows and libusb on Unix). + + +Technical Notes: +================ + +Sharing the same VID/PID pair among devices is possible as long as ALL +drivers which match the VID/PID also perform matching on the textual +identification strings. This is easy on all operating systems except +Windows, since Windows establishes a static connection between the VID/PID +pair and a kernel level driver. All devices with the same VID/PID pair must +therefore use THE SAME kernel level driver. + +We therefore demand that you use libusb-win32 for VENDOR CLASS devices. +This is a generic kernel level driver which allows all types of USB access +for user space applications. This is only a partial solution of the +problem, though, because different device drivers may come with different +versions of libusb-win32 and they may not work with the libusb version of +the respective other driver. You are therefore encouraged to test your +driver against a broad range of libusb-win32 versions. Do not use new +features in new versions, or check for their existence before you use them. +When a new libusb-win32 becomes available, make sure that your driver is +compatible with it. + +For HID CLASS devices it is necessary that all those devices bind to the +same kernel driver: Microsoft's generic USB HID driver. This is true for +all HID devices except those with a specialized driver. Currently, the only +HIDs with specialized drivers are mice and keyboards. You therefore MUST +NOT use a shared VID/PID with mouse and keyboard devices. + +Sharing the same VID/PID among different products is unusual and probably +violates the USB specification. If you do it, you do it at your own risk. + +To avoid possible incompatibilities, we highly recommend that you get your +own VID/PID pair if you intend to sell your product. Objective +Development's commercial licenses for V-USB include a PID for +unrestricted exclusive use. diff --git a/firmware/fnordlicht-controller/usbdrv/asmcommon.inc b/firmware/fnordlicht-controller/usbdrv/asmcommon.inc new file mode 100644 index 0000000..07d692b --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/asmcommon.inc @@ -0,0 +1,188 @@ +/* Name: asmcommon.inc + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Christian Starkjohann + * Creation Date: 2007-11-05 + * Tabsize: 4 + * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * Revision: $Id$ + */ + +/* Do not link this file! Link usbdrvasm.S instead, which includes the + * appropriate implementation! + */ + +/* +General Description: +This file contains assembler code which is shared among the USB driver +implementations for different CPU cocks. Since the code must be inserted +in the middle of the module, it's split out into this file and #included. + +Jump destinations called from outside: + sofError: Called when no start sequence was found. + se0: Called when a package has been successfully received. + overflow: Called when receive buffer overflows. + doReturn: Called after sending data. + +Outside jump destinations used by this module: + waitForJ: Called to receive an already arriving packet. + sendAckAndReti: + sendNakAndReti: + sendCntAndReti: + usbSendAndReti: + +The following macros must be defined before this file is included: + .macro POP_STANDARD + .endm + .macro POP_RETI + .endm +*/ + +#define token x1 + +overflow: + ldi x2, 1< 0 + +#warning "Never compile production devices with debugging enabled" + +static void uartPutc(char c) +{ + while(!(ODDBG_USR & (1 << ODDBG_UDRE))); /* wait for data register empty */ + ODDBG_UDR = c; +} + +static uchar hexAscii(uchar h) +{ + h &= 0xf; + if(h >= 10) + h += 'a' - (uchar)10 - '0'; + h += '0'; + return h; +} + +static void printHex(uchar c) +{ + uartPutc(hexAscii(c >> 4)); + uartPutc(hexAscii(c)); +} + +void odDebug(uchar prefix, uchar *data, uchar len) +{ + printHex(prefix); + uartPutc(':'); + while(len--){ + uartPutc(' '); + printHex(*data++); + } + uartPutc('\r'); + uartPutc('\n'); +} + +#endif diff --git a/firmware/fnordlicht-controller/usbdrv/oddebug.h b/firmware/fnordlicht-controller/usbdrv/oddebug.h new file mode 100644 index 0000000..d61309d --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/oddebug.h @@ -0,0 +1,123 @@ +/* Name: oddebug.h + * Project: AVR library + * Author: Christian Starkjohann + * Creation Date: 2005-01-16 + * Tabsize: 4 + * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id: oddebug.h 692 2008-11-07 15:07:40Z cs $ + */ + +#ifndef __oddebug_h_included__ +#define __oddebug_h_included__ + +/* +General Description: +This module implements a function for debug logs on the serial line of the +AVR microcontroller. Debugging can be configured with the define +'DEBUG_LEVEL'. If this macro is not defined or defined to 0, all debugging +calls are no-ops. If it is 1, DBG1 logs will appear, but not DBG2. If it is +2, DBG1 and DBG2 logs will be printed. + +A debug log consists of a label ('prefix') to indicate which debug log created +the output and a memory block to dump in hex ('data' and 'len'). +*/ + + +#ifndef F_CPU +# define F_CPU 12000000 /* 12 MHz */ +#endif + +/* make sure we have the UART defines: */ +#include "usbportability.h" + +#ifndef uchar +# define uchar unsigned char +#endif + +#if DEBUG_LEVEL > 0 && !(defined TXEN || defined TXEN0) /* no UART in device */ +# warning "Debugging disabled because device has no UART" +# undef DEBUG_LEVEL +#endif + +#ifndef DEBUG_LEVEL +# define DEBUG_LEVEL 0 +#endif + +/* ------------------------------------------------------------------------- */ + +#if DEBUG_LEVEL > 0 +# define DBG1(prefix, data, len) odDebug(prefix, data, len) +#else +# define DBG1(prefix, data, len) +#endif + +#if DEBUG_LEVEL > 1 +# define DBG2(prefix, data, len) odDebug(prefix, data, len) +#else +# define DBG2(prefix, data, len) +#endif + +/* ------------------------------------------------------------------------- */ + +#if DEBUG_LEVEL > 0 +extern void odDebug(uchar prefix, uchar *data, uchar len); + +/* Try to find our control registers; ATMEL likes to rename these */ + +#if defined UBRR +# define ODDBG_UBRR UBRR +#elif defined UBRRL +# define ODDBG_UBRR UBRRL +#elif defined UBRR0 +# define ODDBG_UBRR UBRR0 +#elif defined UBRR0L +# define ODDBG_UBRR UBRR0L +#endif + +#if defined UCR +# define ODDBG_UCR UCR +#elif defined UCSRB +# define ODDBG_UCR UCSRB +#elif defined UCSR0B +# define ODDBG_UCR UCSR0B +#endif + +#if defined TXEN +# define ODDBG_TXEN TXEN +#else +# define ODDBG_TXEN TXEN0 +#endif + +#if defined USR +# define ODDBG_USR USR +#elif defined UCSRA +# define ODDBG_USR UCSRA +#elif defined UCSR0A +# define ODDBG_USR UCSR0A +#endif + +#if defined UDRE +# define ODDBG_UDRE UDRE +#else +# define ODDBG_UDRE UDRE0 +#endif + +#if defined UDR +# define ODDBG_UDR UDR +#elif defined UDR0 +# define ODDBG_UDR UDR0 +#endif + +static inline void odDebugInit(void) +{ + ODDBG_UCR |= (1<len & 0x10){ /* packet buffer was empty */ + txStatus->buffer[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */ + }else{ + txStatus->len = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */ + } + p = txStatus->buffer + 1; + i = len; + do{ /* if len == 0, we still copy 1 byte, but that's no problem */ + *p++ = *data++; + }while(--i > 0); /* loop control at the end is 2 bytes shorter than at beginning */ + usbCrc16Append(&txStatus->buffer[1], len); + txStatus->len = len + 4; /* len must be given including sync byte */ + DBG2(0x21 + (((int)txStatus >> 3) & 3), txStatus->buffer, len + 3); +} + +USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len) +{ + usbGenericSetInterrupt(data, len, &usbTxStatus1); +} +#endif + +#if USB_CFG_HAVE_INTRIN_ENDPOINT3 +USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len) +{ + usbGenericSetInterrupt(data, len, &usbTxStatus3); +} +#endif +#endif /* USB_CFG_SUPPRESS_INTR_CODE */ + +/* ------------------ utilities for code following below ------------------- */ + +/* Use defines for the switch statement so that we can choose between an + * if()else if() and a switch/case based implementation. switch() is more + * efficient for a LARGE set of sequential choices, if() is better in all other + * cases. + */ +#if USB_CFG_USE_SWITCH_STATEMENT +# define SWITCH_START(cmd) switch(cmd){{ +# define SWITCH_CASE(value) }break; case (value):{ +# define SWITCH_CASE2(v1,v2) }break; case (v1): case(v2):{ +# define SWITCH_CASE3(v1,v2,v3) }break; case (v1): case(v2): case(v3):{ +# define SWITCH_DEFAULT }break; default:{ +# define SWITCH_END }} +#else +# define SWITCH_START(cmd) {uchar _cmd = cmd; if(0){ +# define SWITCH_CASE(value) }else if(_cmd == (value)){ +# define SWITCH_CASE2(v1,v2) }else if(_cmd == (v1) || _cmd == (v2)){ +# define SWITCH_CASE3(v1,v2,v3) }else if(_cmd == (v1) || _cmd == (v2) || (_cmd == v3)){ +# define SWITCH_DEFAULT }else{ +# define SWITCH_END }} +#endif + +#ifndef USB_RX_USER_HOOK +#define USB_RX_USER_HOOK(data, len) +#endif +#ifndef USB_SET_ADDRESS_HOOK +#define USB_SET_ADDRESS_HOOK() +#endif + +/* ------------------------------------------------------------------------- */ + +/* We use if() instead of #if in the macro below because #if can't be used + * in macros and the compiler optimizes constant conditions anyway. + * This may cause problems with undefined symbols if compiled without + * optimizing! + */ +#define GET_DESCRIPTOR(cfgProp, staticName) \ + if(cfgProp){ \ + if((cfgProp) & USB_PROP_IS_RAM) \ + flags = 0; \ + if((cfgProp) & USB_PROP_IS_DYNAMIC){ \ + len = usbFunctionDescriptor(rq); \ + }else{ \ + len = USB_PROP_LENGTH(cfgProp); \ + usbMsgPtr = (uchar *)(staticName); \ + } \ + } + +/* usbDriverDescriptor() is similar to usbFunctionDescriptor(), but used + * internally for all types of descriptors. + */ +static inline usbMsgLen_t usbDriverDescriptor(usbRequest_t *rq) +{ +usbMsgLen_t len = 0; +uchar flags = USB_FLG_MSGPTR_IS_ROM; + + SWITCH_START(rq->wValue.bytes[1]) + SWITCH_CASE(USBDESCR_DEVICE) /* 1 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice) + SWITCH_CASE(USBDESCR_CONFIG) /* 2 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration) + SWITCH_CASE(USBDESCR_STRING) /* 3 */ +#if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC + if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM) + flags = 0; + len = usbFunctionDescriptor(rq); +#else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ + SWITCH_START(rq->wValue.bytes[0]) + SWITCH_CASE(0) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0) + SWITCH_CASE(1) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor) + SWITCH_CASE(2) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT, usbDescriptorStringDevice) + SWITCH_CASE(3) + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber) + SWITCH_DEFAULT + if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ + len = usbFunctionDescriptor(rq); + } + SWITCH_END +#endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ +#if USB_CFG_DESCR_PROPS_HID_REPORT /* only support HID descriptors if enabled */ + SWITCH_CASE(USBDESCR_HID) /* 0x21 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18) + SWITCH_CASE(USBDESCR_HID_REPORT)/* 0x22 */ + GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport) +#endif + SWITCH_DEFAULT + if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ + len = usbFunctionDescriptor(rq); + } + SWITCH_END + usbMsgFlags = flags; + return len; +} + +/* ------------------------------------------------------------------------- */ + +/* usbDriverSetup() is similar to usbFunctionSetup(), but it's used for + * standard requests instead of class and custom requests. + */ +static inline usbMsgLen_t usbDriverSetup(usbRequest_t *rq) +{ +uchar len = 0, *dataPtr = usbTxBuf + 9; /* there are 2 bytes free space at the end of the buffer */ +uchar value = rq->wValue.bytes[0]; +#if USB_CFG_IMPLEMENT_HALT +uchar index = rq->wIndex.bytes[0]; +#endif + + dataPtr[0] = 0; /* default reply common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */ + SWITCH_START(rq->bRequest) + SWITCH_CASE(USBRQ_GET_STATUS) /* 0 */ + uchar recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */ + if(USB_CFG_IS_SELF_POWERED && recipient == USBRQ_RCPT_DEVICE) + dataPtr[0] = USB_CFG_IS_SELF_POWERED; +#if USB_CFG_IMPLEMENT_HALT + if(recipient == USBRQ_RCPT_ENDPOINT && index == 0x81) /* request status for endpoint 1 */ + dataPtr[0] = usbTxLen1 == USBPID_STALL; +#endif + dataPtr[1] = 0; + len = 2; +#if USB_CFG_IMPLEMENT_HALT + SWITCH_CASE2(USBRQ_CLEAR_FEATURE, USBRQ_SET_FEATURE) /* 1, 3 */ + if(value == 0 && index == 0x81){ /* feature 0 == HALT for endpoint == 1 */ + usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL; + usbResetDataToggling(); + } +#endif + SWITCH_CASE(USBRQ_SET_ADDRESS) /* 5 */ + usbNewDeviceAddr = value; + USB_SET_ADDRESS_HOOK(); + SWITCH_CASE(USBRQ_GET_DESCRIPTOR) /* 6 */ + len = usbDriverDescriptor(rq); + goto skipMsgPtrAssignment; + SWITCH_CASE(USBRQ_GET_CONFIGURATION) /* 8 */ + dataPtr = &usbConfiguration; /* send current configuration value */ + len = 1; + SWITCH_CASE(USBRQ_SET_CONFIGURATION) /* 9 */ + usbConfiguration = value; + usbResetStall(); + SWITCH_CASE(USBRQ_GET_INTERFACE) /* 10 */ + len = 1; +#if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE + SWITCH_CASE(USBRQ_SET_INTERFACE) /* 11 */ + usbResetDataToggling(); + usbResetStall(); +#endif + SWITCH_DEFAULT /* 7=SET_DESCRIPTOR, 12=SYNC_FRAME */ + /* Should we add an optional hook here? */ + SWITCH_END + usbMsgPtr = dataPtr; +skipMsgPtrAssignment: + return len; +} + +/* ------------------------------------------------------------------------- */ + +/* usbProcessRx() is called for every message received by the interrupt + * routine. It distinguishes between SETUP and DATA packets and processes + * them accordingly. + */ +static inline void usbProcessRx(uchar *data, uchar len) +{ +usbRequest_t *rq = (void *)data; + +/* usbRxToken can be: + * 0x2d 00101101 (USBPID_SETUP for setup data) + * 0xe1 11100001 (USBPID_OUT: data phase of setup transfer) + * 0...0x0f for OUT on endpoint X + */ + DBG2(0x10 + (usbRxToken & 0xf), data, len + 2); /* SETUP=1d, SETUP-DATA=11, OUTx=1x */ + USB_RX_USER_HOOK(data, len) +#if USB_CFG_IMPLEMENT_FN_WRITEOUT + if(usbRxToken < 0x10){ /* OUT to endpoint != 0: endpoint number in usbRxToken */ + usbFunctionWriteOut(data, len); + return; + } +#endif + if(usbRxToken == (uchar)USBPID_SETUP){ + if(len != 8) /* Setup size must be always 8 bytes. Ignore otherwise. */ + return; + usbMsgLen_t replyLen; + usbTxBuf[0] = USBPID_DATA0; /* initialize data toggling */ + usbTxLen = USBPID_NAK; /* abort pending transmit */ + usbMsgFlags = 0; + uchar type = rq->bmRequestType & USBRQ_TYPE_MASK; + if(type != USBRQ_TYPE_STANDARD){ /* standard requests are handled by driver */ + replyLen = usbFunctionSetup(data); + }else{ + replyLen = usbDriverSetup(rq); + } +#if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE + if(replyLen == USB_NO_MSG){ /* use user-supplied read/write function */ + /* do some conditioning on replyLen, but on IN transfers only */ + if((rq->bmRequestType & USBRQ_DIR_MASK) != USBRQ_DIR_HOST_TO_DEVICE){ + if(sizeof(replyLen) < sizeof(rq->wLength.word)){ /* help compiler with optimizing */ + replyLen = rq->wLength.bytes[0]; + }else{ + replyLen = rq->wLength.word; + } + } + usbMsgFlags = USB_FLG_USE_USER_RW; + }else /* The 'else' prevents that we limit a replyLen of USB_NO_MSG to the maximum transfer len. */ +#endif + if(sizeof(replyLen) < sizeof(rq->wLength.word)){ /* help compiler with optimizing */ + if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */ + replyLen = rq->wLength.bytes[0]; + }else{ + if(replyLen > rq->wLength.word) /* limit length to max */ + replyLen = rq->wLength.word; + } + usbMsgLen = replyLen; + }else{ /* usbRxToken must be USBPID_OUT, which means data phase of setup (control-out) */ +#if USB_CFG_IMPLEMENT_FN_WRITE + if(usbMsgFlags & USB_FLG_USE_USER_RW){ + uchar rval = usbFunctionWrite(data, len); + if(rval == 0xff){ /* an error occurred */ + usbTxLen = USBPID_STALL; + }else if(rval != 0){ /* This was the final package */ + usbMsgLen = 0; /* answer with a zero-sized data packet */ + } + } +#endif + } +} + +/* ------------------------------------------------------------------------- */ + +/* This function is similar to usbFunctionRead(), but it's also called for + * data handled automatically by the driver (e.g. descriptor reads). + */ +static uchar usbDeviceRead(uchar *data, uchar len) +{ + if(len > 0){ /* don't bother app with 0 sized reads */ +#if USB_CFG_IMPLEMENT_FN_READ + if(usbMsgFlags & USB_FLG_USE_USER_RW){ + len = usbFunctionRead(data, len); + }else +#endif + { + uchar i = len, *r = usbMsgPtr; + if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */ + do{ + uchar c = USB_READ_FLASH(r); /* assign to char size variable to enforce byte ops */ + *data++ = c; + r++; + }while(--i); + }else{ /* RAM data */ + do{ + *data++ = *r++; + }while(--i); + } + usbMsgPtr = r; + } + } + return len; +} + +/* ------------------------------------------------------------------------- */ + +/* usbBuildTxBlock() is called when we have data to transmit and the + * interrupt routine's transmit buffer is empty. + */ +static inline void usbBuildTxBlock(void) +{ +usbMsgLen_t wantLen; +uchar len; + + wantLen = usbMsgLen; + if(wantLen > 8) + wantLen = 8; + usbMsgLen -= wantLen; + usbTxBuf[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* DATA toggling */ + len = usbDeviceRead(usbTxBuf + 1, wantLen); + if(len <= 8){ /* valid data packet */ + usbCrc16Append(&usbTxBuf[1], len); + len += 4; /* length including sync byte */ + if(len < 12) /* a partial package identifies end of message */ + usbMsgLen = USB_NO_MSG; + }else{ + len = USBPID_STALL; /* stall the endpoint */ + usbMsgLen = USB_NO_MSG; + } + usbTxLen = len; + DBG2(0x20, usbTxBuf, len-1); +} + +/* ------------------------------------------------------------------------- */ + +static inline void usbHandleResetHook(uchar notResetState) +{ +#ifdef USB_RESET_HOOK +static uchar wasReset; +uchar isReset = !notResetState; + + if(wasReset != isReset){ + USB_RESET_HOOK(isReset); + wasReset = isReset; + } +#endif +} + +/* ------------------------------------------------------------------------- */ + +USB_PUBLIC void usbPoll(void) +{ +schar len; +uchar i; + + len = usbRxLen - 3; + if(len >= 0){ +/* We could check CRC16 here -- but ACK has already been sent anyway. If you + * need data integrity checks with this driver, check the CRC in your app + * code and report errors back to the host. Since the ACK was already sent, + * retries must be handled on application level. + * unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3); + */ + usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len); +#if USB_CFG_HAVE_FLOWCONTROL + if(usbRxLen > 0) /* only mark as available if not inactivated */ + usbRxLen = 0; +#else + usbRxLen = 0; /* mark rx buffer as available */ +#endif + } + if(usbTxLen & 0x10){ /* transmit system idle */ + if(usbMsgLen != USB_NO_MSG){ /* transmit data pending? */ + usbBuildTxBlock(); + } + } + for(i = 20; i > 0; i--){ + uchar usbLineStatus = USBIN & USBMASK; + if(usbLineStatus != 0) /* SE0 has ended */ + goto isNotReset; + } + /* RESET condition, called multiple times during reset */ + usbNewDeviceAddr = 0; + usbDeviceAddr = 0; + usbResetStall(); + DBG1(0xff, 0, 0); +isNotReset: + usbHandleResetHook(i); +} + +/* ------------------------------------------------------------------------- */ + +USB_PUBLIC void usbInit(void) +{ +#if USB_INTR_CFG_SET != 0 + USB_INTR_CFG |= USB_INTR_CFG_SET; +#endif +#if USB_INTR_CFG_CLR != 0 + USB_INTR_CFG &= ~(USB_INTR_CFG_CLR); +#endif + USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT); + usbResetDataToggling(); +#if USB_CFG_HAVE_INTRIN_ENDPOINT && !USB_CFG_SUPPRESS_INTR_CODE + usbTxLen1 = USBPID_NAK; +#if USB_CFG_HAVE_INTRIN_ENDPOINT3 + usbTxLen3 = USBPID_NAK; +#endif +#endif +} + +/* ------------------------------------------------------------------------- */ diff --git a/firmware/fnordlicht-controller/usbdrv/usbdrv.h b/firmware/fnordlicht-controller/usbdrv/usbdrv.h new file mode 100644 index 0000000..dc97912 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/usbdrv.h @@ -0,0 +1,735 @@ +/* Name: usbdrv.h + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Christian Starkjohann + * Creation Date: 2004-12-29 + * Tabsize: 4 + * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id: usbdrv.h 769 2009-08-22 11:49:05Z cs $ + */ + +#ifndef __usbdrv_h_included__ +#define __usbdrv_h_included__ +#include "usbconfig.h" +#include "usbportability.h" + +/* +Hardware Prerequisites: +======================= +USB lines D+ and D- MUST be wired to the same I/O port. We recommend that D+ +triggers the interrupt (best achieved by using INT0 for D+), but it is also +possible to trigger the interrupt from D-. If D- is used, interrupts are also +triggered by SOF packets. D- requires a pull-up of 1.5k to +3.5V (and the +device must be powered at 3.5V) to identify as low-speed USB device. A +pull-down or pull-up of 1M SHOULD be connected from D+ to +3.5V to prevent +interference when no USB master is connected. If you use Zener diodes to limit +the voltage on D+ and D-, you MUST use a pull-down resistor, not a pull-up. +We use D+ as interrupt source and not D- because it does not trigger on +keep-alive and RESET states. If you want to count keep-alive events with +USB_COUNT_SOF, you MUST use D- as an interrupt source. + +As a compile time option, the 1.5k pull-up resistor on D- can be made +switchable to allow the device to disconnect at will. See the definition of +usbDeviceConnect() and usbDeviceDisconnect() further down in this file. + +Please adapt the values in usbconfig.h according to your hardware! + +The device MUST be clocked at exactly 12 MHz, 15 MHz, 16 MHz or 20 MHz +or at 12.8 MHz resp. 16.5 MHz +/- 1%. See usbconfig-prototype.h for details. + + +Limitations: +============ +Robustness with respect to communication errors: +The driver assumes error-free communication. It DOES check for errors in +the PID, but does NOT check bit stuffing errors, SE0 in middle of a byte, +token CRC (5 bit) and data CRC (16 bit). CRC checks can not be performed due +to timing constraints: We must start sending a reply within 7 bit times. +Bit stuffing and misplaced SE0 would have to be checked in real-time, but CPU +performance does not permit that. The driver does not check Data0/Data1 +toggling, but application software can implement the check. + +Input characteristics: +Since no differential receiver circuit is used, electrical interference +robustness may suffer. The driver samples only one of the data lines with +an ordinary I/O pin's input characteristics. However, since this is only a +low speed USB implementation and the specification allows for 8 times the +bit rate over the same hardware, we should be on the safe side. Even the spec +requires detection of asymmetric states at high bit rate for SE0 detection. + +Number of endpoints: +The driver supports the following endpoints: + +- Endpoint 0, the default control endpoint. +- Any number of interrupt- or bulk-out endpoints. The data is sent to + usbFunctionWriteOut() and USB_CFG_IMPLEMENT_FN_WRITEOUT must be defined + to 1 to activate this feature. The endpoint number can be found in the + global variable 'usbRxToken'. +- One default interrupt- or bulk-in endpoint. This endpoint is used for + interrupt- or bulk-in transfers which are not handled by any other endpoint. + You must define USB_CFG_HAVE_INTRIN_ENDPOINT in order to activate this + feature and call usbSetInterrupt() to send interrupt/bulk data. +- One additional interrupt- or bulk-in endpoint. This was endpoint 3 in + previous versions of this driver but can now be configured to any endpoint + number. You must define USB_CFG_HAVE_INTRIN_ENDPOINT3 in order to activate + this feature and call usbSetInterrupt3() to send interrupt/bulk data. The + endpoint number can be set with USB_CFG_EP3_NUMBER. + +Please note that the USB standard forbids bulk endpoints for low speed devices! +Most operating systems allow them anyway, but the AVR will spend 90% of the CPU +time in the USB interrupt polling for bulk data. + +Maximum data payload: +Data payload of control in and out transfers may be up to 254 bytes. In order +to accept payload data of out transfers, you need to implement +'usbFunctionWrite()'. + +USB Suspend Mode supply current: +The USB standard limits power consumption to 500uA when the bus is in suspend +mode. This is not a problem for self-powered devices since they don't need +bus power anyway. Bus-powered devices can achieve this only by putting the +CPU in sleep mode. The driver does not implement suspend handling by itself. +However, the application may implement activity monitoring and wakeup from +sleep. The host sends regular SE0 states on the bus to keep it active. These +SE0 states can be detected by using D- as the interrupt source. Define +USB_COUNT_SOF to 1 and use the global variable usbSofCount to check for bus +activity. + +Operation without an USB master: +The driver behaves neutral without connection to an USB master if D- reads +as 1. To avoid spurious interrupts, we recommend a high impedance (e.g. 1M) +pull-down or pull-up resistor on D+ (interrupt). If Zener diodes are used, +use a pull-down. If D- becomes statically 0, the driver may block in the +interrupt routine. + +Interrupt latency: +The application must ensure that the USB interrupt is not disabled for more +than 25 cycles (this is for 12 MHz, faster clocks allow longer latency). +This implies that all interrupt routines must either be declared as "INTERRUPT" +instead of "SIGNAL" (see "avr/signal.h") or that they are written in assembler +with "sei" as the first instruction. + +Maximum interrupt duration / CPU cycle consumption: +The driver handles all USB communication during the interrupt service +routine. The routine will not return before an entire USB message is received +and the reply is sent. This may be up to ca. 1200 cycles @ 12 MHz (= 100us) if +the host conforms to the standard. The driver will consume CPU cycles for all +USB messages, even if they address another (low-speed) device on the same bus. + +*/ + +/* ------------------------------------------------------------------------- */ +/* --------------------------- Module Interface ---------------------------- */ +/* ------------------------------------------------------------------------- */ + +#define USBDRV_VERSION 20090822 +/* This define uniquely identifies a driver version. It is a decimal number + * constructed from the driver's release date in the form YYYYMMDD. If the + * driver's behavior or interface changes, you can use this constant to + * distinguish versions. If it is not defined, the driver's release date is + * older than 2006-01-25. + */ + + +#ifndef USB_PUBLIC +#define USB_PUBLIC +#endif +/* USB_PUBLIC is used as declaration attribute for all functions exported by + * the USB driver. The default is no attribute (see above). You may define it + * to static either in usbconfig.h or from the command line if you include + * usbdrv.c instead of linking against it. Including the C module of the driver + * directly in your code saves a couple of bytes in flash memory. + */ + +#ifndef __ASSEMBLER__ +#ifndef uchar +#define uchar unsigned char +#endif +#ifndef schar +#define schar signed char +#endif +/* shortcuts for well defined 8 bit integer types */ + +#if USB_CFG_LONG_TRANSFERS /* if more than 254 bytes transfer size required */ +# define usbMsgLen_t unsigned +#else +# define usbMsgLen_t uchar +#endif +/* usbMsgLen_t is the data type used for transfer lengths. By default, it is + * defined to uchar, allowing a maximum of 254 bytes (255 is reserved for + * USB_NO_MSG below). If the usbconfig.h defines USB_CFG_LONG_TRANSFERS to 1, + * a 16 bit data type is used, allowing up to 16384 bytes (the rest is used + * for flags in the descriptor configuration). + */ +#define USB_NO_MSG ((usbMsgLen_t)-1) /* constant meaning "no message" */ + +struct usbRequest; /* forward declaration */ + +USB_PUBLIC void usbInit(void); +/* This function must be called before interrupts are enabled and the main + * loop is entered. We exepct that the PORT and DDR bits for D+ and D- have + * not been changed from their default status (which is 0). If you have changed + * them, set both back to 0 (configure them as input with no internal pull-up). + */ +USB_PUBLIC void usbPoll(void); +/* This function must be called at regular intervals from the main loop. + * Maximum delay between calls is somewhat less than 50ms (USB timeout for + * accepting a Setup message). Otherwise the device will not be recognized. + * Please note that debug outputs through the UART take ~ 0.5ms per byte + * at 19200 bps. + */ +extern uchar *usbMsgPtr; +/* This variable may be used to pass transmit data to the driver from the + * implementation of usbFunctionWrite(). It is also used internally by the + * driver for standard control requests. + */ +USB_PUBLIC usbMsgLen_t usbFunctionSetup(uchar data[8]); +/* This function is called when the driver receives a SETUP transaction from + * the host which is not answered by the driver itself (in practice: class and + * vendor requests). All control transfers start with a SETUP transaction where + * the host communicates the parameters of the following (optional) data + * transfer. The SETUP data is available in the 'data' parameter which can + * (and should) be casted to 'usbRequest_t *' for a more user-friendly access + * to parameters. + * + * If the SETUP indicates a control-in transfer, you should provide the + * requested data to the driver. There are two ways to transfer this data: + * (1) Set the global pointer 'usbMsgPtr' to the base of the static RAM data + * block and return the length of the data in 'usbFunctionSetup()'. The driver + * will handle the rest. Or (2) return USB_NO_MSG in 'usbFunctionSetup()'. The + * driver will then call 'usbFunctionRead()' when data is needed. See the + * documentation for usbFunctionRead() for details. + * + * If the SETUP indicates a control-out transfer, the only way to receive the + * data from the host is through the 'usbFunctionWrite()' call. If you + * implement this function, you must return USB_NO_MSG in 'usbFunctionSetup()' + * to indicate that 'usbFunctionWrite()' should be used. See the documentation + * of this function for more information. If you just want to ignore the data + * sent by the host, return 0 in 'usbFunctionSetup()'. + * + * Note that calls to the functions usbFunctionRead() and usbFunctionWrite() + * are only done if enabled by the configuration in usbconfig.h. + */ +USB_PUBLIC usbMsgLen_t usbFunctionDescriptor(struct usbRequest *rq); +/* You need to implement this function ONLY if you provide USB descriptors at + * runtime (which is an expert feature). It is very similar to + * usbFunctionSetup() above, but it is called only to request USB descriptor + * data. See the documentation of usbFunctionSetup() above for more info. + */ +#if USB_CFG_HAVE_INTRIN_ENDPOINT +USB_PUBLIC void usbSetInterrupt(uchar *data, uchar len); +/* This function sets the message which will be sent during the next interrupt + * IN transfer. The message is copied to an internal buffer and must not exceed + * a length of 8 bytes. The message may be 0 bytes long just to indicate the + * interrupt status to the host. + * If you need to transfer more bytes, use a control read after the interrupt. + */ +#define usbInterruptIsReady() (usbTxLen1 & 0x10) +/* This macro indicates whether the last interrupt message has already been + * sent. If you set a new interrupt message before the old was sent, the + * message already buffered will be lost. + */ +#if USB_CFG_HAVE_INTRIN_ENDPOINT3 +USB_PUBLIC void usbSetInterrupt3(uchar *data, uchar len); +#define usbInterruptIsReady3() (usbTxLen3 & 0x10) +/* Same as above for endpoint 3 */ +#endif +#endif /* USB_CFG_HAVE_INTRIN_ENDPOINT */ +#if USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH /* simplified interface for backward compatibility */ +#define usbHidReportDescriptor usbDescriptorHidReport +/* should be declared as: PROGMEM char usbHidReportDescriptor[]; */ +/* If you implement an HID device, you need to provide a report descriptor. + * The HID report descriptor syntax is a bit complex. If you understand how + * report descriptors are constructed, we recommend that you use the HID + * Descriptor Tool from usb.org, see http://www.usb.org/developers/hidpage/. + * Otherwise you should probably start with a working example. + */ +#endif /* USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH */ +#if USB_CFG_IMPLEMENT_FN_WRITE +USB_PUBLIC uchar usbFunctionWrite(uchar *data, uchar len); +/* This function is called by the driver to provide a control transfer's + * payload data (control-out). It is called in chunks of up to 8 bytes. The + * total count provided in the current control transfer can be obtained from + * the 'length' property in the setup data. If an error occurred during + * processing, return 0xff (== -1). The driver will answer the entire transfer + * with a STALL token in this case. If you have received the entire payload + * successfully, return 1. If you expect more data, return 0. If you don't + * know whether the host will send more data (you should know, the total is + * provided in the usbFunctionSetup() call!), return 1. + * NOTE: If you return 0xff for STALL, 'usbFunctionWrite()' may still be called + * for the remaining data. You must continue to return 0xff for STALL in these + * calls. + * In order to get usbFunctionWrite() called, define USB_CFG_IMPLEMENT_FN_WRITE + * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. + */ +#endif /* USB_CFG_IMPLEMENT_FN_WRITE */ +#if USB_CFG_IMPLEMENT_FN_READ +USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len); +/* This function is called by the driver to ask the application for a control + * transfer's payload data (control-in). It is called in chunks of up to 8 + * bytes each. You should copy the data to the location given by 'data' and + * return the actual number of bytes copied. If you return less than requested, + * the control-in transfer is terminated. If you return 0xff, the driver aborts + * the transfer with a STALL token. + * In order to get usbFunctionRead() called, define USB_CFG_IMPLEMENT_FN_READ + * to 1 in usbconfig.h and return 0xff in usbFunctionSetup().. + */ +#endif /* USB_CFG_IMPLEMENT_FN_READ */ + +extern uchar usbRxToken; /* may be used in usbFunctionWriteOut() below */ +#if USB_CFG_IMPLEMENT_FN_WRITEOUT +USB_PUBLIC void usbFunctionWriteOut(uchar *data, uchar len); +/* This function is called by the driver when data is received on an interrupt- + * or bulk-out endpoint. The endpoint number can be found in the global + * variable usbRxToken. You must define USB_CFG_IMPLEMENT_FN_WRITEOUT to 1 in + * usbconfig.h to get this function called. + */ +#endif /* USB_CFG_IMPLEMENT_FN_WRITEOUT */ +#ifdef USB_CFG_PULLUP_IOPORTNAME +#define usbDeviceConnect() ((USB_PULLUP_DDR |= (1<device, 1=device->host + * t ..... type: 0=standard, 1=class, 2=vendor, 3=reserved + * r ..... recipient: 0=device, 1=interface, 2=endpoint, 3=other + */ + +/* USB setup recipient values */ +#define USBRQ_RCPT_MASK 0x1f +#define USBRQ_RCPT_DEVICE 0 +#define USBRQ_RCPT_INTERFACE 1 +#define USBRQ_RCPT_ENDPOINT 2 + +/* USB request type values */ +#define USBRQ_TYPE_MASK 0x60 +#define USBRQ_TYPE_STANDARD (0<<5) +#define USBRQ_TYPE_CLASS (1<<5) +#define USBRQ_TYPE_VENDOR (2<<5) + +/* USB direction values: */ +#define USBRQ_DIR_MASK 0x80 +#define USBRQ_DIR_HOST_TO_DEVICE (0<<7) +#define USBRQ_DIR_DEVICE_TO_HOST (1<<7) + +/* USB Standard Requests */ +#define USBRQ_GET_STATUS 0 +#define USBRQ_CLEAR_FEATURE 1 +#define USBRQ_SET_FEATURE 3 +#define USBRQ_SET_ADDRESS 5 +#define USBRQ_GET_DESCRIPTOR 6 +#define USBRQ_SET_DESCRIPTOR 7 +#define USBRQ_GET_CONFIGURATION 8 +#define USBRQ_SET_CONFIGURATION 9 +#define USBRQ_GET_INTERFACE 10 +#define USBRQ_SET_INTERFACE 11 +#define USBRQ_SYNCH_FRAME 12 + +/* USB descriptor constants */ +#define USBDESCR_DEVICE 1 +#define USBDESCR_CONFIG 2 +#define USBDESCR_STRING 3 +#define USBDESCR_INTERFACE 4 +#define USBDESCR_ENDPOINT 5 +#define USBDESCR_HID 0x21 +#define USBDESCR_HID_REPORT 0x22 +#define USBDESCR_HID_PHYS 0x23 + +//#define USBATTR_BUSPOWER 0x80 // USB 1.1 does not define this value any more +#define USBATTR_SELFPOWER 0x40 +#define USBATTR_REMOTEWAKE 0x20 + +/* USB HID Requests */ +#define USBRQ_HID_GET_REPORT 0x01 +#define USBRQ_HID_GET_IDLE 0x02 +#define USBRQ_HID_GET_PROTOCOL 0x03 +#define USBRQ_HID_SET_REPORT 0x09 +#define USBRQ_HID_SET_IDLE 0x0a +#define USBRQ_HID_SET_PROTOCOL 0x0b + +/* ------------------------------------------------------------------------- */ + +#endif /* __usbdrv_h_included__ */ diff --git a/firmware/fnordlicht-controller/usbdrv/usbdrvasm.S b/firmware/fnordlicht-controller/usbdrv/usbdrvasm.S new file mode 100644 index 0000000..80877e4 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/usbdrvasm.S @@ -0,0 +1,385 @@ +/* Name: usbdrvasm.S + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Christian Starkjohann + * Creation Date: 2007-06-13 + * Tabsize: 4 + * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * Revision: $Id: usbdrvasm.S 761 2009-08-12 16:30:23Z cs $ + */ + +/* +General Description: +This module is the assembler part of the USB driver. This file contains +general code (preprocessor acrobatics and CRC computation) and then includes +the file appropriate for the given clock rate. +*/ + +#define __SFR_OFFSET 0 /* used by avr-libc's register definitions */ +#include "usbportability.h" +#include "usbdrv.h" /* for common defs */ + +/* register names */ +#define x1 r16 +#define x2 r17 +#define shift r18 +#define cnt r19 +#define x3 r20 +#define x4 r21 +#define x5 r22 +#define bitcnt x5 +#define phase x4 +#define leap x4 + +/* Some assembler dependent definitions and declarations: */ + +#ifdef __IAR_SYSTEMS_ASM__ + extern usbRxBuf, usbDeviceAddr, usbNewDeviceAddr, usbInputBufOffset + extern usbCurrentTok, usbRxLen, usbRxToken, usbTxLen + extern usbTxBuf, usbTxStatus1, usbTxStatus3 +# if USB_COUNT_SOF + extern usbSofCount +# endif + public usbCrc16 + public usbCrc16Append + + COMMON INTVEC +# ifndef USB_INTR_VECTOR + ORG INT0_vect +# else /* USB_INTR_VECTOR */ + ORG USB_INTR_VECTOR +# undef USB_INTR_VECTOR +# endif /* USB_INTR_VECTOR */ +# define USB_INTR_VECTOR usbInterruptHandler + rjmp USB_INTR_VECTOR + RSEG CODE + +#else /* __IAR_SYSTEMS_ASM__ */ + +# ifndef USB_INTR_VECTOR /* default to hardware interrupt INT0 */ +# define USB_INTR_VECTOR SIG_INTERRUPT0 +# endif + .text + .global USB_INTR_VECTOR + .type USB_INTR_VECTOR, @function + .global usbCrc16 + .global usbCrc16Append +#endif /* __IAR_SYSTEMS_ASM__ */ + + +#if USB_INTR_PENDING < 0x40 /* This is an I/O address, use in and out */ +# define USB_LOAD_PENDING(reg) in reg, USB_INTR_PENDING +# define USB_STORE_PENDING(reg) out USB_INTR_PENDING, reg +#else /* It's a memory address, use lds and sts */ +# define USB_LOAD_PENDING(reg) lds reg, USB_INTR_PENDING +# define USB_STORE_PENDING(reg) sts USB_INTR_PENDING, reg +#endif + +#define usbTxLen1 usbTxStatus1 +#define usbTxBuf1 (usbTxStatus1 + 1) +#define usbTxLen3 usbTxStatus3 +#define usbTxBuf3 (usbTxStatus3 + 1) + + +;---------------------------------------------------------------------------- +; Utility functions +;---------------------------------------------------------------------------- + +#ifdef __IAR_SYSTEMS_ASM__ +/* Register assignments for usbCrc16 on IAR cc */ +/* Calling conventions on IAR: + * First parameter passed in r16/r17, second in r18/r19 and so on. + * Callee must preserve r4-r15, r24-r29 (r28/r29 is frame pointer) + * Result is passed in r16/r17 + * In case of the "tiny" memory model, pointers are only 8 bit with no + * padding. We therefore pass argument 1 as "16 bit unsigned". + */ +RTMODEL "__rt_version", "3" +/* The line above will generate an error if cc calling conventions change. + * The value "3" above is valid for IAR 4.10B/W32 + */ +# define argLen r18 /* argument 2 */ +# define argPtrL r16 /* argument 1 */ +# define argPtrH r17 /* argument 1 */ + +# define resCrcL r16 /* result */ +# define resCrcH r17 /* result */ + +# define ptrL ZL +# define ptrH ZH +# define ptr Z +# define byte r22 +# define bitCnt r19 +# define polyL r20 +# define polyH r21 +# define scratch r23 + +#else /* __IAR_SYSTEMS_ASM__ */ +/* Register assignments for usbCrc16 on gcc */ +/* Calling conventions on gcc: + * First parameter passed in r24/r25, second in r22/23 and so on. + * Callee must preserve r1-r17, r28/r29 + * Result is passed in r24/r25 + */ +# define argLen r22 /* argument 2 */ +# define argPtrL r24 /* argument 1 */ +# define argPtrH r25 /* argument 1 */ + +# define resCrcL r24 /* result */ +# define resCrcH r25 /* result */ + +# define ptrL XL +# define ptrH XH +# define ptr x +# define byte r18 +# define bitCnt r19 +# define polyL r20 +# define polyH r21 +# define scratch r23 + +#endif + +#if USB_USE_FAST_CRC + +; This implementation is faster, but has bigger code size +; Thanks to Slawomir Fras (BoskiDialer) for this code! +; It implements the following C pseudo-code: +; unsigned table(unsigned char x) +; { +; unsigned value; +; +; value = (unsigned)x << 6; +; value ^= (unsigned)x << 7; +; if(parity(x)) +; value ^= 0xc001; +; return value; +; } +; unsigned usbCrc16(unsigned char *argPtr, unsigned char argLen) +; { +; unsigned crc = 0xffff; +; +; while(argLen--) +; crc = table(lo8(crc) ^ *argPtr++) ^ hi8(crc); +; return ~crc; +; } + +; extern unsigned usbCrc16(unsigned char *argPtr, unsigned char argLen); +; argPtr r24+25 / r16+r17 +; argLen r22 / r18 +; temp variables: +; byte r18 / r22 +; scratch r23 +; resCrc r24+r25 / r16+r17 +; ptr X / Z +usbCrc16: + mov ptrL, argPtrL + mov ptrH, argPtrH + ldi resCrcL, 0xFF + ldi resCrcH, 0xFF + rjmp usbCrc16LoopTest +usbCrc16ByteLoop: + ld byte, ptr+ + eor resCrcL, byte ; resCrcL is now 'x' in table() + mov byte, resCrcL ; compute parity of 'x' + swap byte + eor byte, resCrcL + mov scratch, byte + lsr byte + lsr byte + eor byte, scratch + inc byte + lsr byte + andi byte, 1 ; byte is now parity(x) + mov scratch, resCrcL + mov resCrcL, resCrcH + eor resCrcL, byte ; low byte of if(parity(x)) value ^= 0xc001; + neg byte + andi byte, 0xc0 + mov resCrcH, byte ; high byte of if(parity(x)) value ^= 0xc001; + clr byte + lsr scratch + ror byte + eor resCrcH, scratch + eor resCrcL, byte + lsr scratch + ror byte + eor resCrcH, scratch + eor resCrcL, byte +usbCrc16LoopTest: + subi argLen, 1 + brsh usbCrc16ByteLoop + com resCrcL + com resCrcH + ret + +#else /* USB_USE_FAST_CRC */ + +; This implementation is slower, but has less code size +; +; extern unsigned usbCrc16(unsigned char *argPtr, unsigned char argLen); +; argPtr r24+25 / r16+r17 +; argLen r22 / r18 +; temp variables: +; byte r18 / r22 +; bitCnt r19 +; poly r20+r21 +; scratch r23 +; resCrc r24+r25 / r16+r17 +; ptr X / Z +usbCrc16: + mov ptrL, argPtrL + mov ptrH, argPtrH + ldi resCrcL, 0 + ldi resCrcH, 0 + ldi polyL, lo8(0xa001) + ldi polyH, hi8(0xa001) + com argLen ; argLen = -argLen - 1: modified loop to ensure that carry is set + ldi bitCnt, 0 ; loop counter with starnd condition = end condition + rjmp usbCrcLoopEntry +usbCrcByteLoop: + ld byte, ptr+ + eor resCrcL, byte +usbCrcBitLoop: + ror resCrcH ; carry is always set here (see brcs jumps to here) + ror resCrcL + brcs usbCrcNoXor + eor resCrcL, polyL + eor resCrcH, polyH +usbCrcNoXor: + subi bitCnt, 224 ; (8 * 224) % 256 = 0; this loop iterates 8 times + brcs usbCrcBitLoop +usbCrcLoopEntry: + subi argLen, -1 + brcs usbCrcByteLoop +usbCrcReady: + ret +; Thanks to Reimar Doeffinger for optimizing this CRC routine! + +#endif /* USB_USE_FAST_CRC */ + +; extern unsigned usbCrc16Append(unsigned char *data, unsigned char len); +usbCrc16Append: + rcall usbCrc16 + st ptr+, resCrcL + st ptr+, resCrcH + ret + +#undef argLen +#undef argPtrL +#undef argPtrH +#undef resCrcL +#undef resCrcH +#undef ptrL +#undef ptrH +#undef ptr +#undef byte +#undef bitCnt +#undef polyL +#undef polyH +#undef scratch + + +#if USB_CFG_HAVE_MEASURE_FRAME_LENGTH +#ifdef __IAR_SYSTEMS_ASM__ +/* Register assignments for usbMeasureFrameLength on IAR cc */ +/* Calling conventions on IAR: + * First parameter passed in r16/r17, second in r18/r19 and so on. + * Callee must preserve r4-r15, r24-r29 (r28/r29 is frame pointer) + * Result is passed in r16/r17 + * In case of the "tiny" memory model, pointers are only 8 bit with no + * padding. We therefore pass argument 1 as "16 bit unsigned". + */ +# define resL r16 +# define resH r17 +# define cnt16L r30 +# define cnt16H r31 +# define cntH r18 + +#else /* __IAR_SYSTEMS_ASM__ */ +/* Register assignments for usbMeasureFrameLength on gcc */ +/* Calling conventions on gcc: + * First parameter passed in r24/r25, second in r22/23 and so on. + * Callee must preserve r1-r17, r28/r29 + * Result is passed in r24/r25 + */ +# define resL r24 +# define resH r25 +# define cnt16L r24 +# define cnt16H r25 +# define cntH r26 +#endif +# define cnt16 cnt16L + +; extern unsigned usbMeasurePacketLength(void); +; returns time between two idle strobes in multiples of 7 CPU clocks +.global usbMeasureFrameLength +usbMeasureFrameLength: + ldi cntH, 6 ; wait ~ 10 ms for D- == 0 + clr cnt16L + clr cnt16H +usbMFTime16: + dec cntH + breq usbMFTimeout +usbMFWaitStrobe: ; first wait for D- == 0 (idle strobe) + sbiw cnt16, 1 ;[0] [6] + breq usbMFTime16 ;[2] + sbic USBIN, USBMINUS ;[3] + rjmp usbMFWaitStrobe ;[4] +usbMFWaitIdle: ; then wait until idle again + sbis USBIN, USBMINUS ;1 wait for D- == 1 + rjmp usbMFWaitIdle ;2 + ldi cnt16L, 1 ;1 represents cycles so far + clr cnt16H ;1 +usbMFWaitLoop: + in cntH, USBIN ;[0] [7] + adiw cnt16, 1 ;[1] + breq usbMFTimeout ;[3] + andi cntH, USBMASK ;[4] + brne usbMFWaitLoop ;[5] +usbMFTimeout: +#if resL != cnt16L + mov resL, cnt16L + mov resH, cnt16H +#endif + ret + +#undef resL +#undef resH +#undef cnt16 +#undef cnt16L +#undef cnt16H +#undef cntH + +#endif /* USB_CFG_HAVE_MEASURE_FRAME_LENGTH */ + +;---------------------------------------------------------------------------- +; Now include the clock rate specific code +;---------------------------------------------------------------------------- + +#ifndef USB_CFG_CLOCK_KHZ +# define USB_CFG_CLOCK_KHZ 12000 +#endif + +#if USB_CFG_CHECK_CRC /* separate dispatcher for CRC type modules */ +# if USB_CFG_CLOCK_KHZ == 18000 +# include "usbdrvasm18-crc.inc" +# else +# error "USB_CFG_CLOCK_KHZ is not one of the supported crc-rates!" +# endif +#else /* USB_CFG_CHECK_CRC */ +# if USB_CFG_CLOCK_KHZ == 12000 +# include "usbdrvasm12.inc" +# elif USB_CFG_CLOCK_KHZ == 12800 +# include "usbdrvasm128.inc" +# elif USB_CFG_CLOCK_KHZ == 15000 +# include "usbdrvasm15.inc" +# elif USB_CFG_CLOCK_KHZ == 16000 +# include "usbdrvasm16.inc" +# elif USB_CFG_CLOCK_KHZ == 16500 +# include "usbdrvasm165.inc" +# elif USB_CFG_CLOCK_KHZ == 20000 +# include "usbdrvasm20.inc" +# else +# error "USB_CFG_CLOCK_KHZ is not one of the supported non-crc-rates!" +# endif +#endif /* USB_CFG_CHECK_CRC */ diff --git a/firmware/fnordlicht-controller/usbdrv/usbdrvasm.asm b/firmware/fnordlicht-controller/usbdrv/usbdrvasm.asm new file mode 100644 index 0000000..9cc4e4d --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/usbdrvasm.asm @@ -0,0 +1,21 @@ +/* Name: usbdrvasm.asm + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Christian Starkjohann + * Creation Date: 2006-03-01 + * Tabsize: 4 + * Copyright: (c) 2006 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id$ + */ + +/* +General Description: +The IAR compiler/assembler system prefers assembler files with file extension +".asm". We simply provide this file as an alias for usbdrvasm.S. + +Thanks to Oleg Semyonov for his help with the IAR tools port! +*/ + +#include "usbdrvasm.S" + +end diff --git a/firmware/fnordlicht-controller/usbdrv/usbdrvasm12.inc b/firmware/fnordlicht-controller/usbdrv/usbdrvasm12.inc new file mode 100644 index 0000000..c116758 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/usbdrvasm12.inc @@ -0,0 +1,393 @@ +/* Name: usbdrvasm12.inc + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Christian Starkjohann + * Creation Date: 2004-12-29 + * Tabsize: 4 + * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * This Revision: $Id: usbdrvasm12.inc 740 2009-04-13 18:23:31Z cs $ + */ + +/* Do not link this file! Link usbdrvasm.S instead, which includes the + * appropriate implementation! + */ + +/* +General Description: +This file is the 12 MHz version of the asssembler part of the USB driver. It +requires a 12 MHz crystal (not a ceramic resonator and not a calibrated RC +oscillator). + +See usbdrv.h for a description of the entire driver. + +Since almost all of this code is timing critical, don't change unless you +really know what you are doing! Many parts require not only a maximum number +of CPU cycles, but even an exact number of cycles! + + +Timing constraints according to spec (in bit times): +timing subject min max CPUcycles +--------------------------------------------------------------------------- +EOP of OUT/SETUP to sync pattern of DATA0 (both rx) 2 16 16-128 +EOP of IN to sync pattern of DATA0 (rx, then tx) 2 7.5 16-60 +DATAx (rx) to ACK/NAK/STALL (tx) 2 7.5 16-60 +*/ + +;Software-receiver engine. Strict timing! Don't change unless you can preserve timing! +;interrupt response time: 4 cycles + insn running = 7 max if interrupts always enabled +;max allowable interrupt latency: 34 cycles -> max 25 cycles interrupt disable +;max stack usage: [ret(2), YL, SREG, YH, shift, x1, x2, x3, cnt, x4] = 11 bytes +;Numbers in brackets are maximum cycles since SOF. +USB_INTR_VECTOR: +;order of registers pushed: YL, SREG [sofError], YH, shift, x1, x2, x3, cnt + push YL ;2 [35] push only what is necessary to sync with edge ASAP + in YL, SREG ;1 [37] + push YL ;2 [39] +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +;---------------------------------------------------------------------------- +;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +;sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +waitForK: +;The following code results in a sampling window of 1/4 bit which meets the spec. + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError +foundK: +;{3, 5} after falling D- edge, average delay: 4 cycles [we want 4 for center sampling] +;we have 1 bit time for setup purposes, then sample again. Numbers in brackets +;are cycles from center of first sync (double K) bit after the instruction + push YH ;2 [2] + lds YL, usbInputBufOffset;2 [4] + clr YH ;1 [5] + subi YL, lo8(-(usbRxBuf));1 [6] + sbci YH, hi8(-(usbRxBuf));1 [7] + + sbis USBIN, USBMINUS ;1 [8] we want two bits K [sample 1 cycle too early] + rjmp haveTwoBitsK ;2 [10] + pop YH ;2 [11] undo the push from before + rjmp waitForK ;2 [13] this was not the end of sync, retry +haveTwoBitsK: +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- + push shift ;2 [16] + push x1 ;2 [12] + push x2 ;2 [14] + + in x1, USBIN ;1 [17] <-- sample bit 0 + ldi shift, 0xff ;1 [18] + bst x1, USBMINUS ;1 [19] + bld shift, 0 ;1 [20] + push x3 ;2 [22] + push cnt ;2 [24] + + in x2, USBIN ;1 [25] <-- sample bit 1 + ser x3 ;1 [26] [inserted init instruction] + eor x1, x2 ;1 [27] + bst x1, USBMINUS ;1 [28] + bld shift, 1 ;1 [29] + ldi cnt, USB_BUFSIZE;1 [30] [inserted init instruction] + rjmp rxbit2 ;2 [32] + +;---------------------------------------------------------------------------- +; Receiver loop (numbers in brackets are cycles within byte after instr) +;---------------------------------------------------------------------------- + +unstuff0: ;1 (branch taken) + andi x3, ~0x01 ;1 [15] + mov x1, x2 ;1 [16] x2 contains last sampled (stuffed) bit + in x2, USBIN ;1 [17] <-- sample bit 1 again + ori shift, 0x01 ;1 [18] + rjmp didUnstuff0 ;2 [20] + +unstuff1: ;1 (branch taken) + mov x2, x1 ;1 [21] x1 contains last sampled (stuffed) bit + andi x3, ~0x02 ;1 [22] + ori shift, 0x02 ;1 [23] + nop ;1 [24] + in x1, USBIN ;1 [25] <-- sample bit 2 again + rjmp didUnstuff1 ;2 [27] + +unstuff2: ;1 (branch taken) + andi x3, ~0x04 ;1 [29] + ori shift, 0x04 ;1 [30] + mov x1, x2 ;1 [31] x2 contains last sampled (stuffed) bit + nop ;1 [32] + in x2, USBIN ;1 [33] <-- sample bit 3 + rjmp didUnstuff2 ;2 [35] + +unstuff3: ;1 (branch taken) + in x2, USBIN ;1 [34] <-- sample stuffed bit 3 [one cycle too late] + andi x3, ~0x08 ;1 [35] + ori shift, 0x08 ;1 [36] + rjmp didUnstuff3 ;2 [38] + +unstuff4: ;1 (branch taken) + andi x3, ~0x10 ;1 [40] + in x1, USBIN ;1 [41] <-- sample stuffed bit 4 + ori shift, 0x10 ;1 [42] + rjmp didUnstuff4 ;2 [44] + +unstuff5: ;1 (branch taken) + andi x3, ~0x20 ;1 [48] + in x2, USBIN ;1 [49] <-- sample stuffed bit 5 + ori shift, 0x20 ;1 [50] + rjmp didUnstuff5 ;2 [52] + +unstuff6: ;1 (branch taken) + andi x3, ~0x40 ;1 [56] + in x1, USBIN ;1 [57] <-- sample stuffed bit 6 + ori shift, 0x40 ;1 [58] + rjmp didUnstuff6 ;2 [60] + +; extra jobs done during bit interval: +; bit 0: store, clear [SE0 is unreliable here due to bit dribbling in hubs] +; bit 1: se0 check +; bit 2: overflow check +; bit 3: recovery from delay [bit 0 tasks took too long] +; bit 4: none +; bit 5: none +; bit 6: none +; bit 7: jump, eor +rxLoop: + eor x3, shift ;1 [0] reconstruct: x3 is 0 at bit locations we changed, 1 at others + in x1, USBIN ;1 [1] <-- sample bit 0 + st y+, x3 ;2 [3] store data + ser x3 ;1 [4] + nop ;1 [5] + eor x2, x1 ;1 [6] + bst x2, USBMINUS;1 [7] + bld shift, 0 ;1 [8] + in x2, USBIN ;1 [9] <-- sample bit 1 (or possibly bit 0 stuffed) + andi x2, USBMASK ;1 [10] + breq se0 ;1 [11] SE0 check for bit 1 + andi shift, 0xf9 ;1 [12] +didUnstuff0: + breq unstuff0 ;1 [13] + eor x1, x2 ;1 [14] + bst x1, USBMINUS;1 [15] + bld shift, 1 ;1 [16] +rxbit2: + in x1, USBIN ;1 [17] <-- sample bit 2 (or possibly bit 1 stuffed) + andi shift, 0xf3 ;1 [18] + breq unstuff1 ;1 [19] do remaining work for bit 1 +didUnstuff1: + subi cnt, 1 ;1 [20] + brcs overflow ;1 [21] loop control + eor x2, x1 ;1 [22] + bst x2, USBMINUS;1 [23] + bld shift, 2 ;1 [24] + in x2, USBIN ;1 [25] <-- sample bit 3 (or possibly bit 2 stuffed) + andi shift, 0xe7 ;1 [26] + breq unstuff2 ;1 [27] +didUnstuff2: + eor x1, x2 ;1 [28] + bst x1, USBMINUS;1 [29] + bld shift, 3 ;1 [30] +didUnstuff3: + andi shift, 0xcf ;1 [31] + breq unstuff3 ;1 [32] + in x1, USBIN ;1 [33] <-- sample bit 4 + eor x2, x1 ;1 [34] + bst x2, USBMINUS;1 [35] + bld shift, 4 ;1 [36] +didUnstuff4: + andi shift, 0x9f ;1 [37] + breq unstuff4 ;1 [38] + nop2 ;2 [40] + in x2, USBIN ;1 [41] <-- sample bit 5 + eor x1, x2 ;1 [42] + bst x1, USBMINUS;1 [43] + bld shift, 5 ;1 [44] +didUnstuff5: + andi shift, 0x3f ;1 [45] + breq unstuff5 ;1 [46] + nop2 ;2 [48] + in x1, USBIN ;1 [49] <-- sample bit 6 + eor x2, x1 ;1 [50] + bst x2, USBMINUS;1 [51] + bld shift, 6 ;1 [52] +didUnstuff6: + cpi shift, 0x02 ;1 [53] + brlo unstuff6 ;1 [54] + nop2 ;2 [56] + in x2, USBIN ;1 [57] <-- sample bit 7 + eor x1, x2 ;1 [58] + bst x1, USBMINUS;1 [59] + bld shift, 7 ;1 [60] +didUnstuff7: + cpi shift, 0x04 ;1 [61] + brsh rxLoop ;2 [63] loop control +unstuff7: + andi x3, ~0x80 ;1 [63] + ori shift, 0x80 ;1 [64] + in x2, USBIN ;1 [65] <-- sample stuffed bit 7 + nop ;1 [66] + rjmp didUnstuff7 ;2 [68] + +macro POP_STANDARD ; 12 cycles + pop cnt + pop x3 + pop x2 + pop x1 + pop shift + pop YH + endm +macro POP_RETI ; 5 cycles + pop YL + out SREG, YL + pop YL + endm + +#include "asmcommon.inc" + +;---------------------------------------------------------------------------- +; Transmitting data +;---------------------------------------------------------------------------- + +txByteLoop: +txBitloop: +stuffN1Delay: ; [03] + ror shift ;[-5] [11] [59] + brcc doExorN1 ;[-4] [60] + subi x4, 1 ;[-3] + brne commonN1 ;[-2] + lsl shift ;[-1] compensate ror after rjmp stuffDelay + nop ;[00] stuffing consists of just waiting 8 cycles + rjmp stuffN1Delay ;[01] after ror, C bit is reliably clear + +sendNakAndReti: ;0 [-19] 19 cycles until SOP + ldi x3, USBPID_NAK ;1 [-18] + rjmp usbSendX3 ;2 [-16] +sendAckAndReti: ;0 [-19] 19 cycles until SOP + ldi x3, USBPID_ACK ;1 [-18] + rjmp usbSendX3 ;2 [-16] +sendCntAndReti: ;0 [-17] 17 cycles until SOP + mov x3, cnt ;1 [-16] +usbSendX3: ;0 [-16] + ldi YL, 20 ;1 [-15] 'x3' is R20 + ldi YH, 0 ;1 [-14] + ldi cnt, 2 ;1 [-13] +; rjmp usbSendAndReti fallthrough + +; USB spec says: +; idle = J +; J = (D+ = 0), (D- = 1) or USBOUT = 0x01 +; K = (D+ = 1), (D- = 0) or USBOUT = 0x02 +; Spec allows 7.5 bit times from EOP to SOP for replies (= 60 cycles) + +;usbSend: +;pointer to data in 'Y' +;number of bytes in 'cnt' -- including sync byte +;uses: x1...x2, x4, shift, cnt, Y [x1 = mirror USBOUT, x2 = USBMASK, x4 = bitstuff cnt] +;Numbers in brackets are time since first bit of sync pattern is sent (start of instruction) +usbSendAndReti: + in x2, USBDDR ;[-12] 12 cycles until SOP + ori x2, USBMASK ;[-11] + sbi USBOUT, USBMINUS ;[-10] prepare idle state; D+ and D- must have been 0 (no pullups) + out USBDDR, x2 ;[-8] <--- acquire bus + in x1, USBOUT ;[-7] port mirror for tx loop + ldi shift, 0x40 ;[-6] sync byte is first byte sent (we enter loop after ror) + ldi x2, USBMASK ;[-5] + push x4 ;[-4] +doExorN1: + eor x1, x2 ;[-2] [06] [62] + ldi x4, 6 ;[-1] [07] [63] +commonN1: +stuffN2Delay: + out USBOUT, x1 ;[00] [08] [64] <--- set bit + ror shift ;[01] + brcc doExorN2 ;[02] + subi x4, 1 ;[03] + brne commonN2 ;[04] + lsl shift ;[05] compensate ror after rjmp stuffDelay + rjmp stuffN2Delay ;[06] after ror, C bit is reliably clear +doExorN2: + eor x1, x2 ;[04] [12] + ldi x4, 6 ;[05] [13] +commonN2: + nop ;[06] [14] + subi cnt, 171 ;[07] [15] trick: (3 * 171) & 0xff = 1 + out USBOUT, x1 ;[08] [16] <--- set bit + brcs txBitloop ;[09] [25] [41] + +stuff6Delay: + ror shift ;[42] [50] + brcc doExor6 ;[43] + subi x4, 1 ;[44] + brne common6 ;[45] + lsl shift ;[46] compensate ror after rjmp stuffDelay + nop ;[47] stuffing consists of just waiting 8 cycles + rjmp stuff6Delay ;[48] after ror, C bit is reliably clear +doExor6: + eor x1, x2 ;[45] [53] + ldi x4, 6 ;[46] +common6: +stuff7Delay: + ror shift ;[47] [55] + out USBOUT, x1 ;[48] <--- set bit + brcc doExor7 ;[49] + subi x4, 1 ;[50] + brne common7 ;[51] + lsl shift ;[52] compensate ror after rjmp stuffDelay + rjmp stuff7Delay ;[53] after ror, C bit is reliably clear +doExor7: + eor x1, x2 ;[51] [59] + ldi x4, 6 ;[52] +common7: + ld shift, y+ ;[53] + tst cnt ;[55] + out USBOUT, x1 ;[56] <--- set bit + brne txByteLoop ;[57] + +;make SE0: + cbr x1, USBMASK ;[58] prepare SE0 [spec says EOP may be 15 to 18 cycles] + lds x2, usbNewDeviceAddr;[59] + lsl x2 ;[61] we compare with left shifted address + subi YL, 2 + 20 ;[62] Only assign address on data packets, not ACK/NAK in x3 + sbci YH, 0 ;[63] + out USBOUT, x1 ;[00] <-- out SE0 -- from now 2 bits = 16 cycles until bus idle +;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: +;set address only after data packet was sent, not after handshake + breq skipAddrAssign ;[01] + sts usbDeviceAddr, x2 ; if not skipped: SE0 is one cycle longer +skipAddrAssign: +;end of usbDeviceAddress transfer + ldi x2, 1< 12.5625 MHz +max frequency: 69.286 cycles for 8 bit -> 12.99 MHz +nominal frequency: 12.77 MHz ( = sqrt(min * max)) + +sampling positions: (next even number in range [+/- 0.5]) +cycle index range: 0 ... 66 +bits: +.5, 8.875, 17.25, 25.625, 34, 42.375, 50.75, 59.125 +[0/1], [9], [17], [25/+26], [34], [+42/43], [51], [59] + +bit number: 0 1 2 3 4 5 6 7 +spare cycles 1 2 1 2 1 1 1 0 + +operations to perform: duration cycle + ---------------- + eor fix, shift 1 -> 00 + andi phase, USBMASK 1 -> 08 + breq se0 1 -> 16 (moved to 11) + st y+, data 2 -> 24, 25 + mov data, fix 1 -> 33 + ser data 1 -> 41 + subi cnt, 1 1 -> 49 + brcs overflow 1 -> 50 + +layout of samples and operations: +[##] = sample bit +<##> = sample phase +*##* = operation + +0: *00* [01] 02 03 04 <05> 06 07 +1: *08* [09] 10 11 12 <13> 14 15 *16* +2: [17] 18 19 20 <21> 22 23 +3: *24* *25* [26] 27 28 29 <30> 31 32 +4: *33* [34] 35 36 37 <38> 39 40 +5: *41* [42] 43 44 45 <46> 47 48 +6: *49* *50* [51] 52 53 54 <55> 56 57 58 +7: [59] 60 61 62 <63> 64 65 66 +*****************************************************************************/ + +/* we prefer positive expressions (do if condition) instead of negative + * (skip if condition), therefore use defines for skip instructions: + */ +#define ifioclr sbis +#define ifioset sbic +#define ifrclr sbrs +#define ifrset sbrc + +/* The registers "fix" and "data" swap their meaning during the loop. Use + * defines to keep their name constant. + */ +#define fix x2 +#define data x1 +#undef phase /* phase has a default definition to x4 */ +#define phase x3 + + +USB_INTR_VECTOR: +;order of registers pushed: YL, SREG [sofError], YH, shift, x1, x2, x3, cnt, r0 + push YL ;2 push only what is necessary to sync with edge ASAP + in YL, SREG ;1 + push YL ;2 +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +;---------------------------------------------------------------------------- +;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +;sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +waitForK: +;The following code results in a sampling window of 1/4 bit which meets the spec. + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS ;[0] + rjmp foundK ;[1] +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError + +foundK: +;{3, 5} after falling D- edge, average delay: 4 cycles [we want 4 for center sampling] +;we have 1 bit time for setup purposes, then sample again. Numbers in brackets +;are cycles from center of first sync (double K) bit after the instruction + push YH ;[2] + lds YL, usbInputBufOffset;[4] + clr YH ;[6] + subi YL, lo8(-(usbRxBuf));[7] + sbci YH, hi8(-(usbRxBuf));[8] + + sbis USBIN, USBMINUS ;[9] we want two bits K [we want to sample at 8 + 4 - 1.5 = 10.5] + rjmp haveTwoBitsK ;[10] + pop YH ;[11] undo the push from before + rjmp waitForK ;[13] this was not the end of sync, retry +haveTwoBitsK: +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- +#define fix x2 +#define data x1 + + push shift ;[12] + push x1 ;[14] + push x2 ;[16] + ldi shift, 0x80 ;[18] prevent bit-unstuffing but init low bits to 0 + ifioset USBIN, USBMINUS ;[19] [01] <--- bit 0 [10.5 + 8 = 18.5] + ori shift, 1<<0 ;[02] + push x3 ;[03] + push cnt ;[05] + push r0 ;[07] + ifioset USBIN, USBMINUS ;[09] <--- bit 1 + ori shift, 1<<1 ;[10] + ser fix ;[11] + ldi cnt, USB_BUFSIZE ;[12] + mov data, shift ;[13] + lsl shift ;[14] + nop2 ;[15] + ifioset USBIN, USBMINUS ;[17] <--- bit 2 + ori data, 3<<2 ;[18] store in bit 2 AND bit 3 + eor shift, data ;[19] do nrzi decoding + andi data, 1<<3 ;[20] + in phase, USBIN ;[21] <- phase + brne jumpToEntryAfterSet ;[22] if USBMINS at bit 3 was 1 + nop ;[23] + rjmp entryAfterClr ;[24] +jumpToEntryAfterSet: + rjmp entryAfterSet ;[24] + +;---------------------------------------------------------------------------- +; Receiver loop (numbers in brackets are cycles within byte after instr) +;---------------------------------------------------------------------------- +#undef fix +#define fix x1 +#undef data +#define data x2 + +bit7IsSet: + ifrclr phase, USBMINUS ;[62] check phase only if D- changed + lpm ;[63] + in phase, USBIN ;[64] <- phase (one cycle too late) + ori shift, 1 << 7 ;[65] + nop ;[66] +;;;;rjmp bit0AfterSet ; -> [00] == [67] moved block up to save jump +bit0AfterSet: + eor fix, shift ;[00] +#undef fix +#define fix x2 +#undef data +#define data x1 /* we now have result in data, fix is reset to 0xff */ + ifioclr USBIN, USBMINUS ;[01] <--- sample 0 + rjmp bit0IsClr ;[02] + andi shift, ~(7 << 0) ;[03] + breq unstuff0s ;[04] + in phase, USBIN ;[05] <- phase + rjmp bit1AfterSet ;[06] +unstuff0s: + in phase, USBIN ;[06] <- phase (one cycle too late) + andi fix, ~(1 << 0) ;[07] + ifioclr USBIN, USBMINUS ;[00] + ifioset USBIN, USBPLUS ;[01] + rjmp bit0IsClr ;[02] executed if first expr false or second true +se0AndStore: ; executed only if both bits 0 + st y+, x1 ;[15/17] cycles after start of byte + rjmp se0 ;[17/19] + +bit0IsClr: + ifrset phase, USBMINUS ;[04] check phase only if D- changed + lpm ;[05] + in phase, USBIN ;[06] <- phase (one cycle too late) + ori shift, 1 << 0 ;[07] +bit1AfterClr: + andi phase, USBMASK ;[08] + ifioset USBIN, USBMINUS ;[09] <--- sample 1 + rjmp bit1IsSet ;[10] + breq se0AndStore ;[11] if D- was 0 in bits 0 AND 1 and D+ was 0 in between, we have SE0 + andi shift, ~(7 << 1) ;[12] + in phase, USBIN ;[13] <- phase + breq unstuff1c ;[14] + rjmp bit2AfterClr ;[15] +unstuff1c: + andi fix, ~(1 << 1) ;[16] + nop2 ;[08] + nop2 ;[10] +bit1IsSet: + ifrclr phase, USBMINUS ;[12] check phase only if D- changed + lpm ;[13] + in phase, USBIN ;[14] <- phase (one cycle too late) + ori shift, 1 << 1 ;[15] + nop ;[16] +bit2AfterSet: + ifioclr USBIN, USBMINUS ;[17] <--- sample 2 + rjmp bit2IsClr ;[18] + andi shift, ~(7 << 2) ;[19] + breq unstuff2s ;[20] + in phase, USBIN ;[21] <- phase + rjmp bit3AfterSet ;[22] +unstuff2s: + in phase, USBIN ;[22] <- phase (one cycle too late) + andi fix, ~(1 << 2) ;[23] + nop2 ;[16] + nop2 ;[18] +bit2IsClr: + ifrset phase, USBMINUS ;[20] check phase only if D- changed + lpm ;[21] + in phase, USBIN ;[22] <- phase (one cycle too late) + ori shift, 1 << 2 ;[23] +bit3AfterClr: + st y+, data ;[24] +entryAfterClr: + ifioset USBIN, USBMINUS ;[26] <--- sample 3 + rjmp bit3IsSet ;[27] + andi shift, ~(7 << 3) ;[28] + breq unstuff3c ;[29] + in phase, USBIN ;[30] <- phase + rjmp bit4AfterClr ;[31] +unstuff3c: + in phase, USBIN ;[31] <- phase (one cycle too late) + andi fix, ~(1 << 3) ;[32] + nop2 ;[25] + nop2 ;[27] +bit3IsSet: + ifrclr phase, USBMINUS ;[29] check phase only if D- changed + lpm ;[30] + in phase, USBIN ;[31] <- phase (one cycle too late) + ori shift, 1 << 3 ;[32] +bit4AfterSet: + mov data, fix ;[33] undo this move by swapping defines +#undef fix +#define fix x1 +#undef data +#define data x2 + ifioclr USBIN, USBMINUS ;[34] <--- sample 4 + rjmp bit4IsClr ;[35] + andi shift, ~(7 << 4) ;[36] + breq unstuff4s ;[37] + in phase, USBIN ;[38] <- phase + rjmp bit5AfterSet ;[39] +unstuff4s: + in phase, USBIN ;[39] <- phase (one cycle too late) + andi fix, ~(1 << 4) ;[40] + nop2 ;[33] + nop2 ;[35] +bit4IsClr: + ifrset phase, USBMINUS ;[37] check phase only if D- changed + lpm ;[38] + in phase, USBIN ;[39] <- phase (one cycle too late) + ori shift, 1 << 4 ;[40] +bit5AfterClr: + ser data ;[41] + ifioset USBIN, USBMINUS ;[42] <--- sample 5 + rjmp bit5IsSet ;[43] + andi shift, ~(7 << 5) ;[44] + breq unstuff5c ;[45] + in phase, USBIN ;[46] <- phase + rjmp bit6AfterClr ;[47] +unstuff5c: + in phase, USBIN ;[47] <- phase (one cycle too late) + andi fix, ~(1 << 5) ;[48] + nop2 ;[41] + nop2 ;[43] +bit5IsSet: + ifrclr phase, USBMINUS ;[45] check phase only if D- changed + lpm ;[46] + in phase, USBIN ;[47] <- phase (one cycle too late) + ori shift, 1 << 5 ;[48] +bit6AfterSet: + subi cnt, 1 ;[49] + brcs jumpToOverflow ;[50] + ifioclr USBIN, USBMINUS ;[51] <--- sample 6 + rjmp bit6IsClr ;[52] + andi shift, ~(3 << 6) ;[53] + cpi shift, 2 ;[54] + in phase, USBIN ;[55] <- phase + brlt unstuff6s ;[56] + rjmp bit7AfterSet ;[57] + +jumpToOverflow: + rjmp overflow + +unstuff6s: + andi fix, ~(1 << 6) ;[50] + lpm ;[51] +bit6IsClr: + ifrset phase, USBMINUS ;[54] check phase only if D- changed + lpm ;[55] + in phase, USBIN ;[56] <- phase (one cycle too late) + ori shift, 1 << 6 ;[57] + nop ;[58] +bit7AfterClr: + ifioset USBIN, USBMINUS ;[59] <--- sample 7 + rjmp bit7IsSet ;[60] + andi shift, ~(1 << 7) ;[61] + cpi shift, 4 ;[62] + in phase, USBIN ;[63] <- phase + brlt unstuff7c ;[64] + rjmp bit0AfterClr ;[65] -> [00] == [67] +unstuff7c: + andi fix, ~(1 << 7) ;[58] + nop ;[59] + rjmp bit7IsSet ;[60] + +bit7IsClr: + ifrset phase, USBMINUS ;[62] check phase only if D- changed + lpm ;[63] + in phase, USBIN ;[64] <- phase (one cycle too late) + ori shift, 1 << 7 ;[65] + nop ;[66] +;;;;rjmp bit0AfterClr ; -> [00] == [67] moved block up to save jump +bit0AfterClr: + eor fix, shift ;[00] +#undef fix +#define fix x2 +#undef data +#define data x1 /* we now have result in data, fix is reset to 0xff */ + ifioset USBIN, USBMINUS ;[01] <--- sample 0 + rjmp bit0IsSet ;[02] + andi shift, ~(7 << 0) ;[03] + breq unstuff0c ;[04] + in phase, USBIN ;[05] <- phase + rjmp bit1AfterClr ;[06] +unstuff0c: + in phase, USBIN ;[06] <- phase (one cycle too late) + andi fix, ~(1 << 0) ;[07] + ifioclr USBIN, USBMINUS ;[00] + ifioset USBIN, USBPLUS ;[01] + rjmp bit0IsSet ;[02] executed if first expr false or second true + rjmp se0AndStore ;[03] executed only if both bits 0 +bit0IsSet: + ifrclr phase, USBMINUS ;[04] check phase only if D- changed + lpm ;[05] + in phase, USBIN ;[06] <- phase (one cycle too late) + ori shift, 1 << 0 ;[07] +bit1AfterSet: + andi shift, ~(7 << 1) ;[08] compensated by "ori shift, 1<<1" if bit1IsClr + ifioclr USBIN, USBMINUS ;[09] <--- sample 1 + rjmp bit1IsClr ;[10] + breq unstuff1s ;[11] + nop2 ;[12] do not check for SE0 if bit 0 was 1 + in phase, USBIN ;[14] <- phase (one cycle too late) + rjmp bit2AfterSet ;[15] +unstuff1s: + in phase, USBIN ;[13] <- phase + andi fix, ~(1 << 1) ;[14] + lpm ;[07] + nop2 ;[10] +bit1IsClr: + ifrset phase, USBMINUS ;[12] check phase only if D- changed + lpm ;[13] + in phase, USBIN ;[14] <- phase (one cycle too late) + ori shift, 1 << 1 ;[15] + nop ;[16] +bit2AfterClr: + ifioset USBIN, USBMINUS ;[17] <--- sample 2 + rjmp bit2IsSet ;[18] + andi shift, ~(7 << 2) ;[19] + breq unstuff2c ;[20] + in phase, USBIN ;[21] <- phase + rjmp bit3AfterClr ;[22] +unstuff2c: + in phase, USBIN ;[22] <- phase (one cycle too late) + andi fix, ~(1 << 2) ;[23] + nop2 ;[16] + nop2 ;[18] +bit2IsSet: + ifrclr phase, USBMINUS ;[20] check phase only if D- changed + lpm ;[21] + in phase, USBIN ;[22] <- phase (one cycle too late) + ori shift, 1 << 2 ;[23] +bit3AfterSet: + st y+, data ;[24] +entryAfterSet: + ifioclr USBIN, USBMINUS ;[26] <--- sample 3 + rjmp bit3IsClr ;[27] + andi shift, ~(7 << 3) ;[28] + breq unstuff3s ;[29] + in phase, USBIN ;[30] <- phase + rjmp bit4AfterSet ;[31] +unstuff3s: + in phase, USBIN ;[31] <- phase (one cycle too late) + andi fix, ~(1 << 3) ;[32] + nop2 ;[25] + nop2 ;[27] +bit3IsClr: + ifrset phase, USBMINUS ;[29] check phase only if D- changed + lpm ;[30] + in phase, USBIN ;[31] <- phase (one cycle too late) + ori shift, 1 << 3 ;[32] +bit4AfterClr: + mov data, fix ;[33] undo this move by swapping defines +#undef fix +#define fix x1 +#undef data +#define data x2 + ifioset USBIN, USBMINUS ;[34] <--- sample 4 + rjmp bit4IsSet ;[35] + andi shift, ~(7 << 4) ;[36] + breq unstuff4c ;[37] + in phase, USBIN ;[38] <- phase + rjmp bit5AfterClr ;[39] +unstuff4c: + in phase, USBIN ;[39] <- phase (one cycle too late) + andi fix, ~(1 << 4) ;[40] + nop2 ;[33] + nop2 ;[35] +bit4IsSet: + ifrclr phase, USBMINUS ;[37] check phase only if D- changed + lpm ;[38] + in phase, USBIN ;[39] <- phase (one cycle too late) + ori shift, 1 << 4 ;[40] +bit5AfterSet: + ser data ;[41] + ifioclr USBIN, USBMINUS ;[42] <--- sample 5 + rjmp bit5IsClr ;[43] + andi shift, ~(7 << 5) ;[44] + breq unstuff5s ;[45] + in phase, USBIN ;[46] <- phase + rjmp bit6AfterSet ;[47] +unstuff5s: + in phase, USBIN ;[47] <- phase (one cycle too late) + andi fix, ~(1 << 5) ;[48] + nop2 ;[41] + nop2 ;[43] +bit5IsClr: + ifrset phase, USBMINUS ;[45] check phase only if D- changed + lpm ;[46] + in phase, USBIN ;[47] <- phase (one cycle too late) + ori shift, 1 << 5 ;[48] +bit6AfterClr: + subi cnt, 1 ;[49] + brcs overflow ;[50] + ifioset USBIN, USBMINUS ;[51] <--- sample 6 + rjmp bit6IsSet ;[52] + andi shift, ~(3 << 6) ;[53] + cpi shift, 2 ;[54] + in phase, USBIN ;[55] <- phase + brlt unstuff6c ;[56] + rjmp bit7AfterClr ;[57] +unstuff6c: + andi fix, ~(1 << 6) ;[50] + lpm ;[51] +bit6IsSet: + ifrclr phase, USBMINUS ;[54] check phase only if D- changed + lpm ;[55] + in phase, USBIN ;[56] <- phase (one cycle too late) + ori shift, 1 << 6 ;[57] +bit7AfterSet: + ifioclr USBIN, USBMINUS ;[59] <--- sample 7 + rjmp bit7IsClr ;[60] + andi shift, ~(1 << 7) ;[61] + cpi shift, 4 ;[62] + in phase, USBIN ;[63] <- phase + brlt unstuff7s ;[64] + rjmp bit0AfterSet ;[65] -> [00] == [67] +unstuff7s: + andi fix, ~(1 << 7) ;[58] + nop ;[59] + rjmp bit7IsClr ;[60] + +macro POP_STANDARD ; 14 cycles + pop r0 + pop cnt + pop x3 + pop x2 + pop x1 + pop shift + pop YH + endm +macro POP_RETI ; 5 cycles + pop YL + out SREG, YL + pop YL + endm + +#include "asmcommon.inc" + +;---------------------------------------------------------------------------- +; Transmitting data +;---------------------------------------------------------------------------- + +txByteLoop: +txBitloop: +stuffN1Delay: ; [03] + ror shift ;[-5] [11] [63] + brcc doExorN1 ;[-4] [64] + subi x3, 1 ;[-3] + brne commonN1 ;[-2] + lsl shift ;[-1] compensate ror after rjmp stuffDelay + nop ;[00] stuffing consists of just waiting 8 cycles + rjmp stuffN1Delay ;[01] after ror, C bit is reliably clear + +sendNakAndReti: + ldi cnt, USBPID_NAK ;[-19] + rjmp sendCntAndReti ;[-18] +sendAckAndReti: + ldi cnt, USBPID_ACK ;[-17] +sendCntAndReti: + mov r0, cnt ;[-16] + ldi YL, 0 ;[-15] R0 address is 0 + ldi YH, 0 ;[-14] + ldi cnt, 2 ;[-13] +; rjmp usbSendAndReti fallthrough + +; USB spec says: +; idle = J +; J = (D+ = 0), (D- = 1) or USBOUT = 0x01 +; K = (D+ = 1), (D- = 0) or USBOUT = 0x02 +; Spec allows 7.5 bit times from EOP to SOP for replies (= 60 cycles) + +;usbSend: +;pointer to data in 'Y' +;number of bytes in 'cnt' -- including sync byte +;uses: x1...x3, shift, cnt, Y [x1 = mirror USBOUT, x2 = USBMASK, x3 = bitstuff cnt] +;Numbers in brackets are time since first bit of sync pattern is sent (start of instruction) +usbSendAndReti: + in x2, USBDDR ;[-10] 10 cycles until SOP + ori x2, USBMASK ;[-9] + sbi USBOUT, USBMINUS ;[-8] prepare idle state; D+ and D- must have been 0 (no pullups) + out USBDDR, x2 ;[-6] <--- acquire bus + in x1, USBOUT ;[-5] port mirror for tx loop + ldi shift, 0x40 ;[-4] sync byte is first byte sent (we enter loop after ror) + ldi x2, USBMASK ;[-3] +doExorN1: + eor x1, x2 ;[-2] [06] [62] + ldi x3, 6 ;[-1] [07] [63] +commonN1: +stuffN2Delay: + out USBOUT, x1 ;[00] [08] [64] <--- set bit + ror shift ;[01] + brcc doExorN2 ;[02] + subi x3, 1 ;[03] + brne commonN2 ;[04] + lsl shift ;[05] compensate ror after rjmp stuffDelay + rjmp stuffN2Delay ;[06] after ror, C bit is reliably clear +doExorN2: + eor x1, x2 ;[04] [12] + ldi x3, 6 ;[05] [13] +commonN2: + nop2 ;[06] [14] + subi cnt, 171 ;[08] [16] trick: (3 * 171) & 0xff = 1 + out USBOUT, x1 ;[09] [17] <--- set bit + brcs txBitloop ;[10] [27] [44] + +stuff6Delay: + ror shift ;[45] [53] + brcc doExor6 ;[46] + subi x3, 1 ;[47] + brne common6 ;[48] + lsl shift ;[49] compensate ror after rjmp stuffDelay + nop ;[50] stuffing consists of just waiting 8 cycles + rjmp stuff6Delay ;[51] after ror, C bit is reliably clear +doExor6: + eor x1, x2 ;[48] [56] + ldi x3, 6 ;[49] +common6: +stuff7Delay: + ror shift ;[50] [58] + out USBOUT, x1 ;[51] <--- set bit + brcc doExor7 ;[52] + subi x3, 1 ;[53] + brne common7 ;[54] + lsl shift ;[55] compensate ror after rjmp stuffDelay + rjmp stuff7Delay ;[56] after ror, C bit is reliably clear +doExor7: + eor x1, x2 ;[54] [62] + ldi x3, 6 ;[55] +common7: + ld shift, y+ ;[56] + nop ;[58] + tst cnt ;[59] + out USBOUT, x1 ;[60] [00]<--- set bit + brne txByteLoop ;[61] [01] +;make SE0: + cbr x1, USBMASK ;[02] prepare SE0 [spec says EOP may be 15 to 18 cycles] + lds x2, usbNewDeviceAddr;[03] + lsl x2 ;[05] we compare with left shifted address + subi YL, 2 + 0 ;[06] Only assign address on data packets, not ACK/NAK in r0 + sbci YH, 0 ;[07] + out USBOUT, x1 ;[00] <-- out SE0 -- from now 2 bits = 16 cycles until bus idle +;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: +;set address only after data packet was sent, not after handshake + breq skipAddrAssign ;[01] + sts usbDeviceAddr, x2 ; if not skipped: SE0 is one cycle longer +skipAddrAssign: +;end of usbDeviceAddress transfer + ldi x2, 1< 0) + echo "$s\n"; + } +} + +function printBit($isAfterSet, $bitNum) +{ + ob_start(); + if($isAfterSet){ +?> + ifioclr USBIN, USBMINUS ;[00] <--- sample + rjmp bit#IsClr ;[01] + andi shift, ~(7 << #) ;[02] + breq unstuff#s ;[03] + in phase, USBIN ;[04] <- phase + rjmp bit@AfterSet ;[05] +unstuff#s: + in phase, USBIN ;[05] <- phase (one cycle too late) + andi fix, ~(1 << #) ;[06] + nop2 ;[-1] + nop2 ;[01] +bit#IsClr: + ifrset phase, USBMINUS ;[03] check phase only if D- changed + lpm ;[04] + in phase, USBIN ;[05] <- phase (one cycle too late) + ori shift, 1 << # ;[06] + + ifioset USBIN, USBMINUS ;[00] <--- sample + rjmp bit#IsSet ;[01] + andi shift, ~(7 << #) ;[02] + breq unstuff#c ;[03] + in phase, USBIN ;[04] <- phase + rjmp bit@AfterClr ;[05] +unstuff#c: + in phase, USBIN ;[05] <- phase (one cycle too late) + andi fix, ~(1 << #) ;[06] + nop2 ;[-1] + nop2 ;[01] +bit#IsSet: + ifrclr phase, USBMINUS ;[03] check phase only if D- changed + lpm ;[04] + in phase, USBIN ;[05] <- phase (one cycle too late) + ori shift, 1 << # ;[06] + +*****************************************************************************/ diff --git a/firmware/fnordlicht-controller/usbdrv/usbdrvasm15.inc b/firmware/fnordlicht-controller/usbdrv/usbdrvasm15.inc new file mode 100644 index 0000000..401b7f8 --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/usbdrvasm15.inc @@ -0,0 +1,423 @@ +/* Name: usbdrvasm15.inc + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: contributed by V. Bosch + * Creation Date: 2007-08-06 + * Tabsize: 4 + * Copyright: (c) 2007 by OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * Revision: $Id: usbdrvasm15.inc 740 2009-04-13 18:23:31Z cs $ + */ + +/* Do not link this file! Link usbdrvasm.S instead, which includes the + * appropriate implementation! + */ + +/* +General Description: +This file is the 15 MHz version of the asssembler part of the USB driver. It +requires a 15 MHz crystal (not a ceramic resonator and not a calibrated RC +oscillator). + +See usbdrv.h for a description of the entire driver. + +Since almost all of this code is timing critical, don't change unless you +really know what you are doing! Many parts require not only a maximum number +of CPU cycles, but even an exact number of cycles! +*/ + +;max stack usage: [ret(2), YL, SREG, YH, bitcnt, shift, x1, x2, x3, x4, cnt] = 12 bytes +;nominal frequency: 15 MHz -> 10.0 cycles per bit, 80.0 cycles per byte +; Numbers in brackets are clocks counted from center of last sync bit +; when instruction starts + +;---------------------------------------------------------------------------- +; order of registers pushed: +; YL, SREG [sofError] YH, shift, x1, x2, x3, bitcnt, cnt, x4 +;---------------------------------------------------------------------------- +USB_INTR_VECTOR: + push YL ;2 push only what is necessary to sync with edge ASAP + in YL, SREG ;1 + push YL ;2 +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +; +; sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +; sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +;------------------------------------------------------------------------------- +; The following code results in a sampling window of < 1/4 bit +; which meets the spec. +;------------------------------------------------------------------------------- +waitForK: ;- + sbis USBIN, USBMINUS ;1 [00] <-- sample + rjmp foundK ;2 [01] + sbis USBIN, USBMINUS ; <-- sample + rjmp foundK + sbis USBIN, USBMINUS ; <-- sample + rjmp foundK + sbis USBIN, USBMINUS ; <-- sample + rjmp foundK + sbis USBIN, USBMINUS ; <-- sample + rjmp foundK + sbis USBIN, USBMINUS ; <-- sample + rjmp foundK +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError +;------------------------------------------------------------------------------ +; {3, 5} after falling D- edge, average delay: 4 cycles [we want 5 for +; center sampling] +; we have 1 bit time for setup purposes, then sample again. +; Numbers in brackets are cycles from center of first sync (double K) +; bit after the instruction +;------------------------------------------------------------------------------ +foundK: ;- [02] + lds YL, usbInputBufOffset;2 [03+04] tx loop + push YH ;2 [05+06] + clr YH ;1 [07] + subi YL, lo8(-(usbRxBuf)) ;1 [08] [rx loop init] + sbci YH, hi8(-(usbRxBuf)) ;1 [09] [rx loop init] + push shift ;2 [10+11] + ser shift ;1 [12] + sbis USBIN, USBMINUS ;1 [-1] [13] <--sample:we want two bits K (sample 1 cycle too early) + rjmp haveTwoBitsK ;2 [00] [14] + pop shift ;2 [15+16] undo the push from before + pop YH ;2 [17+18] undo the push from before + rjmp waitForK ;2 [19+20] this was not the end of sync, retry +; The entire loop from waitForK until rjmp waitForK above must not exceed two +; bit times (= 20 cycles). + +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- +haveTwoBitsK: ;- [01] + push x1 ;2 [02+03] + push x2 ;2 [04+05] + push x3 ;2 [06+07] + push bitcnt ;2 [08+09] + in x1, USBIN ;1 [00] [10] <-- sample bit 0 + bst x1, USBMINUS ;1 [01] + bld shift, 0 ;1 [02] + push cnt ;2 [03+04] + ldi cnt, USB_BUFSIZE ;1 [05] + push x4 ;2 [06+07] tx loop + rjmp rxLoop ;2 [08] +;---------------------------------------------------------------------------- +; Receiver loop (numbers in brackets are cycles within byte after instr) +;---------------------------------------------------------------------------- +unstuff0: ;- [07] (branch taken) + andi x3, ~0x01 ;1 [08] + mov x1, x2 ;1 [09] x2 contains last sampled (stuffed) bit + in x2, USBIN ;1 [00] [10] <-- sample bit 1 again + andi x2, USBMASK ;1 [01] + breq se0Hop ;1 [02] SE0 check for bit 1 + ori shift, 0x01 ;1 [03] 0b00000001 + nop ;1 [04] + rjmp didUnstuff0 ;2 [05] +;----------------------------------------------------- +unstuff1: ;- [05] (branch taken) + mov x2, x1 ;1 [06] x1 contains last sampled (stuffed) bit + andi x3, ~0x02 ;1 [07] + ori shift, 0x02 ;1 [08] 0b00000010 + nop ;1 [09] + in x1, USBIN ;1 [00] [10] <-- sample bit 2 again + andi x1, USBMASK ;1 [01] + breq se0Hop ;1 [02] SE0 check for bit 2 + rjmp didUnstuff1 ;2 [03] +;----------------------------------------------------- +unstuff2: ;- [05] (branch taken) + andi x3, ~0x04 ;1 [06] + ori shift, 0x04 ;1 [07] 0b00000100 + mov x1, x2 ;1 [08] x2 contains last sampled (stuffed) bit + nop ;1 [09] + in x2, USBIN ;1 [00] [10] <-- sample bit 3 + andi x2, USBMASK ;1 [01] + breq se0Hop ;1 [02] SE0 check for bit 3 + rjmp didUnstuff2 ;2 [03] +;----------------------------------------------------- +unstuff3: ;- [00] [10] (branch taken) + in x2, USBIN ;1 [01] [11] <-- sample stuffed bit 3 one cycle too late + andi x2, USBMASK ;1 [02] + breq se0Hop ;1 [03] SE0 check for stuffed bit 3 + andi x3, ~0x08 ;1 [04] + ori shift, 0x08 ;1 [05] 0b00001000 + rjmp didUnstuff3 ;2 [06] +;---------------------------------------------------------------------------- +; extra jobs done during bit interval: +; +; bit 0: store, clear [SE0 is unreliable here due to bit dribbling in hubs], +; overflow check, jump to the head of rxLoop +; bit 1: SE0 check +; bit 2: SE0 check, recovery from delay [bit 0 tasks took too long] +; bit 3: SE0 check, recovery from delay [bit 0 tasks took too long] +; bit 4: SE0 check, none +; bit 5: SE0 check, none +; bit 6: SE0 check, none +; bit 7: SE0 check, reconstruct: x3 is 0 at bit locations we changed, 1 at others +;---------------------------------------------------------------------------- +rxLoop: ;- [09] + in x2, USBIN ;1 [00] [10] <-- sample bit 1 (or possibly bit 0 stuffed) + andi x2, USBMASK ;1 [01] + brne SkipSe0Hop ;1 [02] +se0Hop: ;- [02] + rjmp se0 ;2 [03] SE0 check for bit 1 +SkipSe0Hop: ;- [03] + ser x3 ;1 [04] + andi shift, 0xf9 ;1 [05] 0b11111001 + breq unstuff0 ;1 [06] +didUnstuff0: ;- [06] + eor x1, x2 ;1 [07] + bst x1, USBMINUS ;1 [08] + bld shift, 1 ;1 [09] + in x1, USBIN ;1 [00] [10] <-- sample bit 2 (or possibly bit 1 stuffed) + andi x1, USBMASK ;1 [01] + breq se0Hop ;1 [02] SE0 check for bit 2 + andi shift, 0xf3 ;1 [03] 0b11110011 + breq unstuff1 ;1 [04] do remaining work for bit 1 +didUnstuff1: ;- [04] + eor x2, x1 ;1 [05] + bst x2, USBMINUS ;1 [06] + bld shift, 2 ;1 [07] + nop2 ;2 [08+09] + in x2, USBIN ;1 [00] [10] <-- sample bit 3 (or possibly bit 2 stuffed) + andi x2, USBMASK ;1 [01] + breq se0Hop ;1 [02] SE0 check for bit 3 + andi shift, 0xe7 ;1 [03] 0b11100111 + breq unstuff2 ;1 [04] +didUnstuff2: ;- [04] + eor x1, x2 ;1 [05] + bst x1, USBMINUS ;1 [06] + bld shift, 3 ;1 [07] +didUnstuff3: ;- [07] + andi shift, 0xcf ;1 [08] 0b11001111 + breq unstuff3 ;1 [09] + in x1, USBIN ;1 [00] [10] <-- sample bit 4 + andi x1, USBMASK ;1 [01] + breq se0Hop ;1 [02] SE0 check for bit 4 + eor x2, x1 ;1 [03] + bst x2, USBMINUS ;1 [04] + bld shift, 4 ;1 [05] +didUnstuff4: ;- [05] + andi shift, 0x9f ;1 [06] 0b10011111 + breq unstuff4 ;1 [07] + nop2 ;2 [08+09] + in x2, USBIN ;1 [00] [10] <-- sample bit 5 + andi x2, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for bit 5 + eor x1, x2 ;1 [03] + bst x1, USBMINUS ;1 [04] + bld shift, 5 ;1 [05] +didUnstuff5: ;- [05] + andi shift, 0x3f ;1 [06] 0b00111111 + breq unstuff5 ;1 [07] + nop2 ;2 [08+09] + in x1, USBIN ;1 [00] [10] <-- sample bit 6 + andi x1, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for bit 6 + eor x2, x1 ;1 [03] + bst x2, USBMINUS ;1 [04] + bld shift, 6 ;1 [05] +didUnstuff6: ;- [05] + cpi shift, 0x02 ;1 [06] 0b00000010 + brlo unstuff6 ;1 [07] + nop2 ;2 [08+09] + in x2, USBIN ;1 [00] [10] <-- sample bit 7 + andi x2, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for bit 7 + eor x1, x2 ;1 [03] + bst x1, USBMINUS ;1 [04] + bld shift, 7 ;1 [05] +didUnstuff7: ;- [05] + cpi shift, 0x04 ;1 [06] 0b00000100 + brlo unstuff7 ;1 [07] + eor x3, shift ;1 [08] reconstruct: x3 is 0 at bit locations we changed, 1 at others + nop ;1 [09] + in x1, USBIN ;1 [00] [10] <-- sample bit 0 + st y+, x3 ;2 [01+02] store data + eor x2, x1 ;1 [03] + bst x2, USBMINUS ;1 [04] + bld shift, 0 ;1 [05] + subi cnt, 1 ;1 [06] + brcs overflow ;1 [07] + rjmp rxLoop ;2 [08] +;----------------------------------------------------- +unstuff4: ;- [08] + andi x3, ~0x10 ;1 [09] + in x1, USBIN ;1 [00] [10] <-- sample stuffed bit 4 + andi x1, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for stuffed bit 4 + ori shift, 0x10 ;1 [03] + rjmp didUnstuff4 ;2 [04] +;----------------------------------------------------- +unstuff5: ;- [08] + ori shift, 0x20 ;1 [09] + in x2, USBIN ;1 [00] [10] <-- sample stuffed bit 5 + andi x2, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for stuffed bit 5 + andi x3, ~0x20 ;1 [03] + rjmp didUnstuff5 ;2 [04] +;----------------------------------------------------- +unstuff6: ;- [08] + andi x3, ~0x40 ;1 [09] + in x1, USBIN ;1 [00] [10] <-- sample stuffed bit 6 + andi x1, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for stuffed bit 6 + ori shift, 0x40 ;1 [03] + rjmp didUnstuff6 ;2 [04] +;----------------------------------------------------- +unstuff7: ;- [08] + andi x3, ~0x80 ;1 [09] + in x2, USBIN ;1 [00] [10] <-- sample stuffed bit 7 + andi x2, USBMASK ;1 [01] + breq se0 ;1 [02] SE0 check for stuffed bit 7 + ori shift, 0x80 ;1 [03] + rjmp didUnstuff7 ;2 [04] + +macro POP_STANDARD ; 16 cycles + pop x4 + pop cnt + pop bitcnt + pop x3 + pop x2 + pop x1 + pop shift + pop YH + endm +macro POP_RETI ; 5 cycles + pop YL + out SREG, YL + pop YL + endm + +#include "asmcommon.inc" + +;--------------------------------------------------------------------------- +; USB spec says: +; idle = J +; J = (D+ = 0), (D- = 1) +; K = (D+ = 1), (D- = 0) +; Spec allows 7.5 bit times from EOP to SOP for replies +;--------------------------------------------------------------------------- +bitstuffN: ;- [04] + eor x1, x4 ;1 [05] + clr x2 ;1 [06] + nop ;1 [07] + rjmp didStuffN ;1 [08] +;--------------------------------------------------------------------------- +bitstuff6: ;- [04] + eor x1, x4 ;1 [05] + clr x2 ;1 [06] + rjmp didStuff6 ;1 [07] +;--------------------------------------------------------------------------- +bitstuff7: ;- [02] + eor x1, x4 ;1 [03] + clr x2 ;1 [06] + nop ;1 [05] + rjmp didStuff7 ;1 [06] +;--------------------------------------------------------------------------- +sendNakAndReti: ;- [-19] + ldi x3, USBPID_NAK ;1 [-18] + rjmp sendX3AndReti ;1 [-17] +;--------------------------------------------------------------------------- +sendAckAndReti: ;- [-17] + ldi cnt, USBPID_ACK ;1 [-16] +sendCntAndReti: ;- [-16] + mov x3, cnt ;1 [-15] +sendX3AndReti: ;- [-15] + ldi YL, 20 ;1 [-14] x3==r20 address is 20 + ldi YH, 0 ;1 [-13] + ldi cnt, 2 ;1 [-12] +; rjmp usbSendAndReti fallthrough +;--------------------------------------------------------------------------- +;usbSend: +;pointer to data in 'Y' +;number of bytes in 'cnt' -- including sync byte [range 2 ... 12] +;uses: x1...x4, btcnt, shift, cnt, Y +;Numbers in brackets are time since first bit of sync pattern is sent +;We need not to match the transfer rate exactly because the spec demands +;only 1.5% precision anyway. +usbSendAndReti: ;- [-13] 13 cycles until SOP + in x2, USBDDR ;1 [-12] + ori x2, USBMASK ;1 [-11] + sbi USBOUT, USBMINUS ;2 [-09-10] prepare idle state; D+ and D- must have been 0 (no pullups) + in x1, USBOUT ;1 [-08] port mirror for tx loop + out USBDDR, x2 ;1 [-07] <- acquire bus + ; need not init x2 (bitstuff history) because sync starts with 0 + ldi x4, USBMASK ;1 [-06] exor mask + ldi shift, 0x80 ;1 [-05] sync byte is first byte sent + ldi bitcnt, 6 ;1 [-04] +txBitLoop: ;- [-04] [06] + sbrs shift, 0 ;1 [-03] [07] + eor x1, x4 ;1 [-02] [08] + ror shift ;1 [-01] [09] +didStuffN: ;- [09] + out USBOUT, x1 ;1 [00] [10] <-- out N + ror x2 ;1 [01] + cpi x2, 0xfc ;1 [02] + brcc bitstuffN ;1 [03] + dec bitcnt ;1 [04] + brne txBitLoop ;1 [05] + sbrs shift, 0 ;1 [06] + eor x1, x4 ;1 [07] + ror shift ;1 [08] +didStuff6: ;- [08] + nop ;1 [09] + out USBOUT, x1 ;1 [00] [10] <-- out 6 + ror x2 ;1 [01] + cpi x2, 0xfc ;1 [02] + brcc bitstuff6 ;1 [03] + sbrs shift, 0 ;1 [04] + eor x1, x4 ;1 [05] + ror shift ;1 [06] + ror x2 ;1 [07] +didStuff7: ;- [07] + ldi bitcnt, 6 ;1 [08] + cpi x2, 0xfc ;1 [09] + out USBOUT, x1 ;1 [00] [10] <-- out 7 + brcc bitstuff7 ;1 [01] + ld shift, y+ ;2 [02+03] + dec cnt ;1 [04] + brne txBitLoop ;1 [05] +makeSE0: + cbr x1, USBMASK ;1 [06] prepare SE0 [spec says EOP may be 19 to 23 cycles] + lds x2, usbNewDeviceAddr;2 [07+08] + lsl x2 ;1 [09] we compare with left shifted address +;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: +;set address only after data packet was sent, not after handshake + out USBOUT, x1 ;1 [00] [10] <-- out SE0-- from now 2 bits==20 cycl. until bus idle + subi YL, 20 + 2 ;1 [01] Only assign address on data packets, not ACK/NAK in x3 + sbci YH, 0 ;1 [02] + breq skipAddrAssign ;1 [03] + sts usbDeviceAddr, x2 ;2 [04+05] if not skipped: SE0 is one cycle longer +;---------------------------------------------------------------------------- +;end of usbDeviceAddress transfer +skipAddrAssign: ;- [03/04] + ldi x2, 1< 10.6666666 cycles per bit, 85.333333333 cycles per byte +; Numbers in brackets are clocks counted from center of last sync bit +; when instruction starts + +USB_INTR_VECTOR: +;order of registers pushed: YL, SREG YH, [sofError], bitcnt, shift, x1, x2, x3, x4, cnt + push YL ;[-25] push only what is necessary to sync with edge ASAP + in YL, SREG ;[-23] + push YL ;[-22] + push YH ;[-20] +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +;---------------------------------------------------------------------------- +;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +;sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +waitForK: +;The following code results in a sampling window of < 1/4 bit which meets the spec. + sbis USBIN, USBMINUS ;[-15] + rjmp foundK ;[-14] + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError +foundK: ;[-12] +;{3, 5} after falling D- edge, average delay: 4 cycles [we want 5 for center sampling] +;we have 1 bit time for setup purposes, then sample again. Numbers in brackets +;are cycles from center of first sync (double K) bit after the instruction + push bitcnt ;[-12] +; [---] ;[-11] + lds YL, usbInputBufOffset;[-10] +; [---] ;[-9] + clr YH ;[-8] + subi YL, lo8(-(usbRxBuf));[-7] [rx loop init] + sbci YH, hi8(-(usbRxBuf));[-6] [rx loop init] + push shift ;[-5] +; [---] ;[-4] + ldi bitcnt, 0x55 ;[-3] [rx loop init] + sbis USBIN, USBMINUS ;[-2] we want two bits K (sample 2 cycles too early) + rjmp haveTwoBitsK ;[-1] + pop shift ;[0] undo the push from before + pop bitcnt ;[2] undo the push from before + rjmp waitForK ;[4] this was not the end of sync, retry +; The entire loop from waitForK until rjmp waitForK above must not exceed two +; bit times (= 21 cycles). + +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- +haveTwoBitsK: + push x1 ;[1] + push x2 ;[3] + push x3 ;[5] + ldi shift, 0 ;[7] + ldi x3, 1<<4 ;[8] [rx loop init] first sample is inverse bit, compensate that + push x4 ;[9] == leap + + in x1, USBIN ;[11] <-- sample bit 0 + andi x1, USBMASK ;[12] + bst x1, USBMINUS ;[13] + bld shift, 7 ;[14] + push cnt ;[15] + ldi leap, 0 ;[17] [rx loop init] + ldi cnt, USB_BUFSIZE;[18] [rx loop init] + rjmp rxbit1 ;[19] arrives at [21] + +;---------------------------------------------------------------------------- +; Receiver loop (numbers in brackets are cycles within byte after instr) +;---------------------------------------------------------------------------- + +; duration of unstuffing code should be 10.66666667 cycles. We adjust "leap" +; accordingly to approximate this value in the long run. + +unstuff6: + andi x2, USBMASK ;[03] + ori x3, 1<<6 ;[04] will not be shifted any more + andi shift, ~0x80;[05] + mov x1, x2 ;[06] sampled bit 7 is actually re-sampled bit 6 + subi leap, -1 ;[07] total duration = 11 bits -> subtract 1/3 + rjmp didUnstuff6 ;[08] + +unstuff7: + ori x3, 1<<7 ;[09] will not be shifted any more + in x2, USBIN ;[00] [10] re-sample bit 7 + andi x2, USBMASK ;[01] + andi shift, ~0x80;[02] + subi leap, 2 ;[03] total duration = 10 bits -> add 1/3 + rjmp didUnstuff7 ;[04] + +unstuffEven: + ori x3, 1<<6 ;[09] will be shifted right 6 times for bit 0 + in x1, USBIN ;[00] [10] + andi shift, ~0x80;[01] + andi x1, USBMASK ;[02] + breq se0 ;[03] + subi leap, -1 ;[04] total duration = 11 bits -> subtract 1/3 + nop2 ;[05] + rjmp didUnstuffE ;[06] + +unstuffOdd: + ori x3, 1<<5 ;[09] will be shifted right 4 times for bit 1 + in x2, USBIN ;[00] [10] + andi shift, ~0x80;[01] + andi x2, USBMASK ;[02] + breq se0 ;[03] + subi leap, -1 ;[04] total duration = 11 bits -> subtract 1/3 + nop2 ;[05] + rjmp didUnstuffO ;[06] + +rxByteLoop: + andi x1, USBMASK ;[03] + eor x2, x1 ;[04] + subi leap, 1 ;[05] + brpl skipLeap ;[06] + subi leap, -3 ;1 one leap cycle every 3rd byte -> 85 + 1/3 cycles per byte + nop ;1 +skipLeap: + subi x2, 1 ;[08] + ror shift ;[09] +didUnstuff6: + cpi shift, 0xfc ;[10] + in x2, USBIN ;[00] [11] <-- sample bit 7 + brcc unstuff6 ;[01] + andi x2, USBMASK ;[02] + eor x1, x2 ;[03] + subi x1, 1 ;[04] + ror shift ;[05] +didUnstuff7: + cpi shift, 0xfc ;[06] + brcc unstuff7 ;[07] + eor x3, shift ;[08] reconstruct: x3 is 1 at bit locations we changed, 0 at others + st y+, x3 ;[09] store data +rxBitLoop: + in x1, USBIN ;[00] [11] <-- sample bit 0/2/4 + andi x1, USBMASK ;[01] + eor x2, x1 ;[02] + andi x3, 0x3f ;[03] topmost two bits reserved for 6 and 7 + subi x2, 1 ;[04] + ror shift ;[05] + cpi shift, 0xfc ;[06] + brcc unstuffEven ;[07] +didUnstuffE: + lsr x3 ;[08] + lsr x3 ;[09] +rxbit1: + in x2, USBIN ;[00] [10] <-- sample bit 1/3/5 + andi x2, USBMASK ;[01] + breq se0 ;[02] + eor x1, x2 ;[03] + subi x1, 1 ;[04] + ror shift ;[05] + cpi shift, 0xfc ;[06] + brcc unstuffOdd ;[07] +didUnstuffO: + subi bitcnt, 0xab;[08] == addi 0x55, 0x55 = 0x100/3 + brcs rxBitLoop ;[09] + + subi cnt, 1 ;[10] + in x1, USBIN ;[00] [11] <-- sample bit 6 + brcc rxByteLoop ;[01] + rjmp overflow + +macro POP_STANDARD ; 14 cycles + pop cnt + pop x4 + pop x3 + pop x2 + pop x1 + pop shift + pop bitcnt + endm +macro POP_RETI ; 7 cycles + pop YH + pop YL + out SREG, YL + pop YL + endm + +#include "asmcommon.inc" + +; USB spec says: +; idle = J +; J = (D+ = 0), (D- = 1) +; K = (D+ = 1), (D- = 0) +; Spec allows 7.5 bit times from EOP to SOP for replies + +bitstuffN: + eor x1, x4 ;[5] + ldi x2, 0 ;[6] + nop2 ;[7] + nop ;[9] + out USBOUT, x1 ;[10] <-- out + rjmp didStuffN ;[0] + +bitstuff6: + eor x1, x4 ;[5] + ldi x2, 0 ;[6] Carry is zero due to brcc + rol shift ;[7] compensate for ror shift at branch destination + rjmp didStuff6 ;[8] + +bitstuff7: + ldi x2, 0 ;[2] Carry is zero due to brcc + rjmp didStuff7 ;[3] + + +sendNakAndReti: + ldi x3, USBPID_NAK ;[-18] + rjmp sendX3AndReti ;[-17] +sendAckAndReti: + ldi cnt, USBPID_ACK ;[-17] +sendCntAndReti: + mov x3, cnt ;[-16] +sendX3AndReti: + ldi YL, 20 ;[-15] x3==r20 address is 20 + ldi YH, 0 ;[-14] + ldi cnt, 2 ;[-13] +; rjmp usbSendAndReti fallthrough + +;usbSend: +;pointer to data in 'Y' +;number of bytes in 'cnt' -- including sync byte [range 2 ... 12] +;uses: x1...x4, btcnt, shift, cnt, Y +;Numbers in brackets are time since first bit of sync pattern is sent +;We don't match the transfer rate exactly (don't insert leap cycles every third +;byte) because the spec demands only 1.5% precision anyway. +usbSendAndReti: ; 12 cycles until SOP + in x2, USBDDR ;[-12] + ori x2, USBMASK ;[-11] + sbi USBOUT, USBMINUS;[-10] prepare idle state; D+ and D- must have been 0 (no pullups) + in x1, USBOUT ;[-8] port mirror for tx loop + out USBDDR, x2 ;[-7] <- acquire bus +; need not init x2 (bitstuff history) because sync starts with 0 + ldi x4, USBMASK ;[-6] exor mask + ldi shift, 0x80 ;[-5] sync byte is first byte sent +txByteLoop: + ldi bitcnt, 0x35 ;[-4] [6] binary 0011 0101 +txBitLoop: + sbrs shift, 0 ;[-3] [7] + eor x1, x4 ;[-2] [8] + out USBOUT, x1 ;[-1] [9] <-- out N + ror shift ;[0] [10] + ror x2 ;[1] +didStuffN: + cpi x2, 0xfc ;[2] + brcc bitstuffN ;[3] + lsr bitcnt ;[4] + brcc txBitLoop ;[5] + brne txBitLoop ;[6] + + sbrs shift, 0 ;[7] + eor x1, x4 ;[8] +didStuff6: + out USBOUT, x1 ;[-1] [9] <-- out 6 + ror shift ;[0] [10] + ror x2 ;[1] + cpi x2, 0xfc ;[2] + brcc bitstuff6 ;[3] + ror shift ;[4] +didStuff7: + ror x2 ;[5] + sbrs x2, 7 ;[6] + eor x1, x4 ;[7] + nop ;[8] + cpi x2, 0xfc ;[9] + out USBOUT, x1 ;[-1][10] <-- out 7 + brcc bitstuff7 ;[0] [11] + ld shift, y+ ;[1] + dec cnt ;[3] + brne txByteLoop ;[4] +;make SE0: + cbr x1, USBMASK ;[5] prepare SE0 [spec says EOP may be 21 to 25 cycles] + lds x2, usbNewDeviceAddr;[6] + lsl x2 ;[8] we compare with left shifted address + subi YL, 20 + 2 ;[9] Only assign address on data packets, not ACK/NAK in x3 + sbci YH, 0 ;[10] + out USBOUT, x1 ;[11] <-- out SE0 -- from now 2 bits = 22 cycles until bus idle +;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: +;set address only after data packet was sent, not after handshake + breq skipAddrAssign ;[0] + sts usbDeviceAddr, x2; if not skipped: SE0 is one cycle longer +skipAddrAssign: +;end of usbDeviceAddress transfer + ldi x2, 1< max 52 cycles interrupt disable +;max stack usage: [ret(2), r0, SREG, YL, YH, shift, x1, x2, x3, x4, cnt] = 12 bytes +;nominal frequency: 16.5 MHz -> 11 cycles per bit +; 16.3125 MHz < F_CPU < 16.6875 MHz (+/- 1.1%) +; Numbers in brackets are clocks counted from center of last sync bit +; when instruction starts + + +USB_INTR_VECTOR: +;order of registers pushed: YL, SREG [sofError], r0, YH, shift, x1, x2, x3, x4, cnt + push YL ;[-23] push only what is necessary to sync with edge ASAP + in YL, SREG ;[-21] + push YL ;[-20] +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +;---------------------------------------------------------------------------- +;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +;sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +waitForK: +;The following code results in a sampling window of < 1/4 bit which meets the spec. + sbis USBIN, USBMINUS ;[-15] + rjmp foundK ;[-14] + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError +foundK: ;[-12] +;{3, 5} after falling D- edge, average delay: 4 cycles [we want 5 for center sampling] +;we have 1 bit time for setup purposes, then sample again. Numbers in brackets +;are cycles from center of first sync (double K) bit after the instruction + push r0 ;[-12] +; [---] ;[-11] + push YH ;[-10] +; [---] ;[-9] + lds YL, usbInputBufOffset;[-8] +; [---] ;[-7] + clr YH ;[-6] + subi YL, lo8(-(usbRxBuf));[-5] [rx loop init] + sbci YH, hi8(-(usbRxBuf));[-4] [rx loop init] + mov r0, x2 ;[-3] [rx loop init] + sbis USBIN, USBMINUS ;[-2] we want two bits K (sample 2 cycles too early) + rjmp haveTwoBitsK ;[-1] + pop YH ;[0] undo the pushes from before + pop r0 ;[2] + rjmp waitForK ;[4] this was not the end of sync, retry +; The entire loop from waitForK until rjmp waitForK above must not exceed two +; bit times (= 22 cycles). + +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- +haveTwoBitsK: ;[1] + push shift ;[1] + push x1 ;[3] + push x2 ;[5] + push x3 ;[7] + ldi shift, 0xff ;[9] [rx loop init] + ori x3, 0xff ;[10] [rx loop init] == ser x3, clear zero flag + + in x1, USBIN ;[11] <-- sample bit 0 + bst x1, USBMINUS ;[12] + bld shift, 0 ;[13] + push x4 ;[14] == phase +; [---] ;[15] + push cnt ;[16] +; [---] ;[17] + ldi phase, 0 ;[18] [rx loop init] + ldi cnt, USB_BUFSIZE;[19] [rx loop init] + rjmp rxbit1 ;[20] +; [---] ;[21] + +;---------------------------------------------------------------------------- +; Receiver loop (numbers in brackets are cycles within byte after instr) +;---------------------------------------------------------------------------- +/* +byte oriented operations done during loop: +bit 0: store data +bit 1: SE0 check +bit 2: overflow check +bit 3: catch up +bit 4: rjmp to achieve conditional jump range +bit 5: PLL +bit 6: catch up +bit 7: jump, fixup bitstuff +; 87 [+ 2] cycles +------------------------------------------------------------------ +*/ +continueWithBit5: + in x2, USBIN ;[055] <-- bit 5 + eor r0, x2 ;[056] + or phase, r0 ;[057] + sbrc phase, USBMINUS ;[058] + lpm ;[059] optional nop3; modifies r0 + in phase, USBIN ;[060] <-- phase + eor x1, x2 ;[061] + bst x1, USBMINUS ;[062] + bld shift, 5 ;[063] + andi shift, 0x3f ;[064] + in x1, USBIN ;[065] <-- bit 6 + breq unstuff5 ;[066] *** unstuff escape + eor phase, x1 ;[067] + eor x2, x1 ;[068] + bst x2, USBMINUS ;[069] + bld shift, 6 ;[070] +didUnstuff6: ;[ ] + in r0, USBIN ;[071] <-- phase + cpi shift, 0x02 ;[072] + brlo unstuff6 ;[073] *** unstuff escape +didUnstuff5: ;[ ] + nop2 ;[074] +; [---] ;[075] + in x2, USBIN ;[076] <-- bit 7 + eor x1, x2 ;[077] + bst x1, USBMINUS ;[078] + bld shift, 7 ;[079] +didUnstuff7: ;[ ] + eor r0, x2 ;[080] + or phase, r0 ;[081] + in r0, USBIN ;[082] <-- phase + cpi shift, 0x04 ;[083] + brsh rxLoop ;[084] +; [---] ;[085] +unstuff7: ;[ ] + andi x3, ~0x80 ;[085] + ori shift, 0x80 ;[086] + in x2, USBIN ;[087] <-- sample stuffed bit 7 + nop ;[088] + rjmp didUnstuff7 ;[089] +; [---] ;[090] + ;[080] + +unstuff5: ;[067] + eor phase, x1 ;[068] + andi x3, ~0x20 ;[069] + ori shift, 0x20 ;[070] + in r0, USBIN ;[071] <-- phase + mov x2, x1 ;[072] + nop ;[073] + nop2 ;[074] +; [---] ;[075] + in x1, USBIN ;[076] <-- bit 6 + eor r0, x1 ;[077] + or phase, r0 ;[078] + eor x2, x1 ;[079] + bst x2, USBMINUS ;[080] + bld shift, 6 ;[081] no need to check bitstuffing, we just had one + in r0, USBIN ;[082] <-- phase + rjmp didUnstuff5 ;[083] +; [---] ;[084] + ;[074] + +unstuff6: ;[074] + andi x3, ~0x40 ;[075] + in x1, USBIN ;[076] <-- bit 6 again + ori shift, 0x40 ;[077] + nop2 ;[078] +; [---] ;[079] + rjmp didUnstuff6 ;[080] +; [---] ;[081] + ;[071] + +unstuff0: ;[013] + eor r0, x2 ;[014] + or phase, r0 ;[015] + andi x2, USBMASK ;[016] check for SE0 + in r0, USBIN ;[017] <-- phase + breq didUnstuff0 ;[018] direct jump to se0 would be too long + andi x3, ~0x01 ;[019] + ori shift, 0x01 ;[020] + mov x1, x2 ;[021] mov existing sample + in x2, USBIN ;[022] <-- bit 1 again + rjmp didUnstuff0 ;[023] +; [---] ;[024] + ;[014] + +unstuff1: ;[024] + eor r0, x1 ;[025] + or phase, r0 ;[026] + andi x3, ~0x02 ;[027] + in r0, USBIN ;[028] <-- phase + ori shift, 0x02 ;[029] + mov x2, x1 ;[030] + rjmp didUnstuff1 ;[031] +; [---] ;[032] + ;[022] + +unstuff2: ;[035] + eor r0, x2 ;[036] + or phase, r0 ;[037] + andi x3, ~0x04 ;[038] + in r0, USBIN ;[039] <-- phase + ori shift, 0x04 ;[040] + mov x1, x2 ;[041] + rjmp didUnstuff2 ;[042] +; [---] ;[043] + ;[033] + +unstuff3: ;[043] + in x2, USBIN ;[044] <-- bit 3 again + eor r0, x2 ;[045] + or phase, r0 ;[046] + andi x3, ~0x08 ;[047] + ori shift, 0x08 ;[048] + nop ;[049] + in r0, USBIN ;[050] <-- phase + rjmp didUnstuff3 ;[051] +; [---] ;[052] + ;[042] + +unstuff4: ;[053] + andi x3, ~0x10 ;[054] + in x1, USBIN ;[055] <-- bit 4 again + ori shift, 0x10 ;[056] + rjmp didUnstuff4 ;[057] +; [---] ;[058] + ;[048] + +rxLoop: ;[085] + eor x3, shift ;[086] reconstruct: x3 is 0 at bit locations we changed, 1 at others + in x1, USBIN ;[000] <-- bit 0 + st y+, x3 ;[001] +; [---] ;[002] + eor r0, x1 ;[003] + or phase, r0 ;[004] + eor x2, x1 ;[005] + in r0, USBIN ;[006] <-- phase + ser x3 ;[007] + bst x2, USBMINUS ;[008] + bld shift, 0 ;[009] + andi shift, 0xf9 ;[010] +rxbit1: ;[ ] + in x2, USBIN ;[011] <-- bit 1 + breq unstuff0 ;[012] *** unstuff escape + andi x2, USBMASK ;[013] SE0 check for bit 1 +didUnstuff0: ;[ ] Z only set if we detected SE0 in bitstuff + breq se0 ;[014] + eor r0, x2 ;[015] + or phase, r0 ;[016] + in r0, USBIN ;[017] <-- phase + eor x1, x2 ;[018] + bst x1, USBMINUS ;[019] + bld shift, 1 ;[020] + andi shift, 0xf3 ;[021] +didUnstuff1: ;[ ] + in x1, USBIN ;[022] <-- bit 2 + breq unstuff1 ;[023] *** unstuff escape + eor r0, x1 ;[024] + or phase, r0 ;[025] + subi cnt, 1 ;[026] overflow check + brcs overflow ;[027] + in r0, USBIN ;[028] <-- phase + eor x2, x1 ;[029] + bst x2, USBMINUS ;[030] + bld shift, 2 ;[031] + andi shift, 0xe7 ;[032] +didUnstuff2: ;[ ] + in x2, USBIN ;[033] <-- bit 3 + breq unstuff2 ;[034] *** unstuff escape + eor r0, x2 ;[035] + or phase, r0 ;[036] + eor x1, x2 ;[037] + bst x1, USBMINUS ;[038] + in r0, USBIN ;[039] <-- phase + bld shift, 3 ;[040] + andi shift, 0xcf ;[041] +didUnstuff3: ;[ ] + breq unstuff3 ;[042] *** unstuff escape + nop ;[043] + in x1, USBIN ;[044] <-- bit 4 + eor x2, x1 ;[045] + bst x2, USBMINUS ;[046] + bld shift, 4 ;[047] +didUnstuff4: ;[ ] + eor r0, x1 ;[048] + or phase, r0 ;[049] + in r0, USBIN ;[050] <-- phase + andi shift, 0x9f ;[051] + breq unstuff4 ;[052] *** unstuff escape + rjmp continueWithBit5;[053] +; [---] ;[054] + +macro POP_STANDARD ; 16 cycles + pop cnt + pop x4 + pop x3 + pop x2 + pop x1 + pop shift + pop YH + pop r0 + endm +macro POP_RETI ; 5 cycles + pop YL + out SREG, YL + pop YL + endm + +#include "asmcommon.inc" + + +; USB spec says: +; idle = J +; J = (D+ = 0), (D- = 1) +; K = (D+ = 1), (D- = 0) +; Spec allows 7.5 bit times from EOP to SOP for replies + +bitstuff7: + eor x1, x4 ;[4] + ldi x2, 0 ;[5] + nop2 ;[6] C is zero (brcc) + rjmp didStuff7 ;[8] + +bitstuffN: + eor x1, x4 ;[5] + ldi x2, 0 ;[6] + lpm ;[7] 3 cycle NOP, modifies r0 + out USBOUT, x1 ;[10] <-- out + rjmp didStuffN ;[0] + +#define bitStatus x3 + +sendNakAndReti: + ldi cnt, USBPID_NAK ;[-19] + rjmp sendCntAndReti ;[-18] +sendAckAndReti: + ldi cnt, USBPID_ACK ;[-17] +sendCntAndReti: + mov r0, cnt ;[-16] + ldi YL, 0 ;[-15] R0 address is 0 + ldi YH, 0 ;[-14] + ldi cnt, 2 ;[-13] +; rjmp usbSendAndReti fallthrough + +;usbSend: +;pointer to data in 'Y' +;number of bytes in 'cnt' -- including sync byte [range 2 ... 12] +;uses: x1...x4, shift, cnt, Y +;Numbers in brackets are time since first bit of sync pattern is sent +usbSendAndReti: ; 12 cycles until SOP + in x2, USBDDR ;[-12] + ori x2, USBMASK ;[-11] + sbi USBOUT, USBMINUS;[-10] prepare idle state; D+ and D- must have been 0 (no pullups) + in x1, USBOUT ;[-8] port mirror for tx loop + out USBDDR, x2 ;[-7] <- acquire bus +; need not init x2 (bitstuff history) because sync starts with 0 + ldi x4, USBMASK ;[-6] exor mask + ldi shift, 0x80 ;[-5] sync byte is first byte sent + ldi bitStatus, 0xff ;[-4] init bit loop counter, works for up to 12 bytes +byteloop: +bitloop: + sbrs shift, 0 ;[8] [-3] + eor x1, x4 ;[9] [-2] + out USBOUT, x1 ;[10] [-1] <-- out + ror shift ;[0] + ror x2 ;[1] +didStuffN: + cpi x2, 0xfc ;[2] + brcc bitstuffN ;[3] + nop ;[4] + subi bitStatus, 37 ;[5] 256 / 7 ~=~ 37 + brcc bitloop ;[6] when we leave the loop, bitStatus has almost the initial value + sbrs shift, 0 ;[7] + eor x1, x4 ;[8] + ror shift ;[9] +didStuff7: + out USBOUT, x1 ;[10] <-- out + ror x2 ;[0] + cpi x2, 0xfc ;[1] + brcc bitstuff7 ;[2] + ld shift, y+ ;[3] + dec cnt ;[5] + brne byteloop ;[6] +;make SE0: + cbr x1, USBMASK ;[7] prepare SE0 [spec says EOP may be 21 to 25 cycles] + lds x2, usbNewDeviceAddr;[8] + lsl x2 ;[10] we compare with left shifted address + out USBOUT, x1 ;[11] <-- out SE0 -- from now 2 bits = 22 cycles until bus idle +;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: +;set address only after data packet was sent, not after handshake + subi YL, 2 ;[0] Only assign address on data packets, not ACK/NAK in r0 + sbci YH, 0 ;[1] + breq skipAddrAssign ;[2] + sts usbDeviceAddr, x2; if not skipped: SE0 is one cycle longer +skipAddrAssign: +;end of usbDeviceAddress transfer + ldi x2, 1< 12 cycles per bit +; Numbers in brackets are clocks counted from center of last sync bit +; when instruction starts +;register use in receive loop to receive the data bytes: +; shift assembles the byte currently being received +; x1 holds the D+ and D- line state +; x2 holds the previous line state +; cnt holds the number of bytes left in the receive buffer +; x3 holds the higher crc byte (see algorithm below) +; x4 is used as temporary register for the crc algorithm +; x5 is used for unstuffing: when unstuffing the last received bit is inverted in shift (to prevent further +; unstuffing calls. In the same time the corresponding bit in x5 is cleared to mark the bit as beening iverted +; zl lower crc value and crc table index +; zh used for crc table accesses + +;-------------------------------------------------------------------------------------------------------------- +; CRC mods: +; table driven crc checker, Z points to table in prog space +; ZL is the lower crc byte, x3 is the higher crc byte +; x4 is used as temp register to store different results +; the initialization of the crc register is not 0xFFFF but 0xFE54. This is because during the receipt of the +; first data byte an virtual zero data byte is added to the crc register, this results in the correct initial +; value of 0xFFFF at beginning of the second data byte before the first data byte is added to the crc. +; The magic number 0xFE54 results form the crc table: At tabH[0x54] = 0xFF = crcH (required) and +; tabL[0x54] = 0x01 -> crcL = 0x01 xor 0xFE = 0xFF +; bitcnt is renamed to x5 and is used for unstuffing purposes, the unstuffing works like in the 12MHz version +;-------------------------------------------------------------------------------------------------------------- +; CRC algorithm: +; The crc register is formed by x3 (higher byte) and ZL (lower byte). The algorithm uses a 'reversed' form +; i.e. that it takes the least significant bit first and shifts to the right. So in fact the highest order +; bit seen from the polynomial devision point of view is the lsb of ZL. (If this sounds strange to you i +; propose a research on CRC :-) ) +; Each data byte received is xored to ZL, the lower crc byte. This byte now builds the crc +; table index. Next the new high byte is loaded from the table and stored in x4 until we have space in x3 +; (its destination). +; Afterwards the lower table is loaded from the table and stored in ZL (the old index is overwritten as +; we don't need it anymore. In fact this is a right shift by 8 bits.) Now the old crc high value is xored +; to ZL, this is the second shift of the old crc value. Now x4 (the temp reg) is moved to x3 and the crc +; calculation is done. +; Prior to the first byte the two CRC register have to be initialized to 0xFFFF (as defined in usb spec) +; however the crc engine also runs during the receipt of the first byte, therefore x3 and zl are initialized +; to a magic number which results in a crc value of 0xFFFF after the first complete byte. +; +; This algorithm is split into the extra cycles of the different bits: +; bit7: XOR the received byte to ZL +; bit5: load the new high byte to x4 +; bit6: load the lower xor byte from the table, xor zl and x3, store result in zl (=the new crc low value) +; move x4 (the new high byte) to x3, the crc value is ready +; + + +macro POP_STANDARD ; 18 cycles + pop ZH + pop ZL + pop cnt + pop x5 + pop x3 + pop x2 + pop x1 + pop shift + pop x4 + endm +macro POP_RETI ; 7 cycles + pop YH + pop YL + out SREG, YL + pop YL + endm + +macro CRC_CLEANUP_AND_CHECK + ; the last byte has already been xored with the lower crc byte, we have to do the table lookup and xor + ; x3 is the higher crc byte, zl the lower one + ldi ZH, hi8(usbCrcTableHigh);[+1] get the new high byte from the table + lpm x2, Z ;[+2][+3][+4] + ldi ZH, hi8(usbCrcTableLow);[+5] get the new low xor byte from the table + lpm ZL, Z ;[+6][+7][+8] + eor ZL, x3 ;[+7] xor the old high byte with the value from the table, x2:ZL now holds the crc value + cpi ZL, 0x01 ;[+8] if the crc is ok we have a fixed remainder value of 0xb001 in x2:ZL (see usb spec) + brne ignorePacket ;[+9] detected a crc fault -> paket is ignored and retransmitted by the host + cpi x2, 0xb0 ;[+10] + brne ignorePacket ;[+11] detected a crc fault -> paket is ignored and retransmitted by the host + endm + + +USB_INTR_VECTOR: +;order of registers pushed: YL, SREG, YH, [sofError], x4, shift, x1, x2, x3, x5, cnt, ZL, ZH + push YL ;[-28] push only what is necessary to sync with edge ASAP + in YL, SREG ;[-26] + push YL ;[-25] + push YH ;[-23] +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +;---------------------------------------------------------------------------- +;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +;sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +waitForK: +;The following code results in a sampling window of < 1/4 bit which meets the spec. + sbis USBIN, USBMINUS ;[-17] + rjmp foundK ;[-16] + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError +foundK: ;[-15] +;{3, 5} after falling D- edge, average delay: 4 cycles +;bit0 should be at 30 (2.5 bits) for center sampling. Currently at 4 so 26 cylces till bit 0 sample +;use 1 bit time for setup purposes, then sample again. Numbers in brackets +;are cycles from center of first sync (double K) bit after the instruction + push x4 ;[-14] +; [---] ;[-13] + lds YL, usbInputBufOffset;[-12] used to toggle the two usb receive buffers +; [---] ;[-11] + clr YH ;[-10] + subi YL, lo8(-(usbRxBuf));[-9] [rx loop init] + sbci YH, hi8(-(usbRxBuf));[-8] [rx loop init] + push shift ;[-7] +; [---] ;[-6] + ldi shift, 0x80 ;[-5] the last bit is the end of byte marker for the pid receiver loop + clc ;[-4] the carry has to be clear for receipt of pid bit 0 + sbis USBIN, USBMINUS ;[-3] we want two bits K (sample 3 cycles too early) + rjmp haveTwoBitsK ;[-2] + pop shift ;[-1] undo the push from before + pop x4 ;[1] + rjmp waitForK ;[3] this was not the end of sync, retry +; The entire loop from waitForK until rjmp waitForK above must not exceed two +; bit times (= 24 cycles). + +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- +haveTwoBitsK: + push x1 ;[0] + push x2 ;[2] + push x3 ;[4] crc high byte + ldi x2, 1< jump back and store the byte + ori shift, 0x01 ;[11] invert the last received bit to prevent furhter unstuffing + in x2, USBIN ;[0] we have some free cycles so we could check for bit stuffing errors + andi x5, 0xFE ;[1] mark this bit as inverted (will be corrected before storing shift) + eor x1, x2 ;[2] x1 and x2 have to be different because the stuff bit is always a zero + andi x1, USBMASK ;[3] mask the interesting bits + breq stuffErr ;[4] if the stuff bit is a 1-bit something went wrong + mov x1, x2 ;[5] the next bit expects the last state to be in x1 + rjmp didunstuff0 ;[6] + ;[7] jump delay of rjmp didunstuffX + +unstuff1: ;[11] this is the jump delay of breq unstuffX + in x1, USBIN ;[0] we have some free cycles so we could check for bit stuffing errors + ori shift, 0x02 ;[1] invert the last received bit to prevent furhter unstuffing + andi x5, 0xFD ;[2] mark this bit as inverted (will be corrected before storing shift) + eor x2, x1 ;[3] x1 and x2 have to be different because the stuff bit is always a zero + andi x2, USBMASK ;[4] mask the interesting bits + breq stuffErr ;[5] if the stuff bit is a 1-bit something went wrong + mov x2, x1 ;[6] the next bit expects the last state to be in x2 + nop2 ;[7] + ;[8] + rjmp didunstuff1 ;[9] + ;[10] jump delay of rjmp didunstuffX + +unstuff2: ;[9] this is the jump delay of breq unstuffX + ori shift, 0x04 ;[10] invert the last received bit to prevent furhter unstuffing + andi x5, 0xFB ;[11] mark this bit as inverted (will be corrected before storing shift) + in x2, USBIN ;[0] we have some free cycles so we could check for bit stuffing errors + eor x1, x2 ;[1] x1 and x2 have to be different because the stuff bit is always a zero + andi x1, USBMASK ;[2] mask the interesting bits + breq stuffErr ;[3] if the stuff bit is a 1-bit something went wrong + mov x1, x2 ;[4] the next bit expects the last state to be in x1 + nop2 ;[5] + ;[6] + rjmp didunstuff2 ;[7] + ;[8] jump delay of rjmp didunstuffX + +unstuff3: ;[9] this is the jump delay of breq unstuffX + ori shift, 0x08 ;[10] invert the last received bit to prevent furhter unstuffing + andi x5, 0xF7 ;[11] mark this bit as inverted (will be corrected before storing shift) + in x1, USBIN ;[0] we have some free cycles so we could check for bit stuffing errors + eor x2, x1 ;[1] x1 and x2 have to be different because the stuff bit is always a zero + andi x2, USBMASK ;[2] mask the interesting bits + breq stuffErr ;[3] if the stuff bit is a 1-bit something went wrong + mov x2, x1 ;[4] the next bit expects the last state to be in x2 + nop2 ;[5] + ;[6] + rjmp didunstuff3 ;[7] + ;[8] jump delay of rjmp didunstuffX + + + +; the include has to be here due to branch distance restirctions +#define __USE_CRC__ +#include "asmcommon.inc" + + + +; USB spec says: +; idle = J +; J = (D+ = 0), (D- = 1) +; K = (D+ = 1), (D- = 0) +; Spec allows 7.5 bit times from EOP to SOP for replies +; 7.5 bit times is 90 cycles. ...there is plenty of time + + +sendNakAndReti: + ldi x3, USBPID_NAK ;[-18] + rjmp sendX3AndReti ;[-17] +sendAckAndReti: + ldi cnt, USBPID_ACK ;[-17] +sendCntAndReti: + mov x3, cnt ;[-16] +sendX3AndReti: + ldi YL, 20 ;[-15] x3==r20 address is 20 + ldi YH, 0 ;[-14] + ldi cnt, 2 ;[-13] +; rjmp usbSendAndReti fallthrough + +;usbSend: +;pointer to data in 'Y' +;number of bytes in 'cnt' -- including sync byte [range 2 ... 12] +;uses: x1...x4, btcnt, shift, cnt, Y +;Numbers in brackets are time since first bit of sync pattern is sent + +usbSendAndReti: ; 12 cycles until SOP + in x2, USBDDR ;[-12] + ori x2, USBMASK ;[-11] + sbi USBOUT, USBMINUS;[-10] prepare idle state; D+ and D- must have been 0 (no pullups) + in x1, USBOUT ;[-8] port mirror for tx loop + out USBDDR, x2 ;[-6] <- acquire bus + ldi x2, 0 ;[-6] init x2 (bitstuff history) because sync starts with 0 + ldi x4, USBMASK ;[-5] exor mask + ldi shift, 0x80 ;[-4] sync byte is first byte sent +txByteLoop: + ldi bitcnt, 0x40 ;[-3]=[9] binary 01000000 +txBitLoop: ; the loop sends the first 7 bits of the byte + sbrs shift, 0 ;[-2]=[10] if we have to send a 1 don't change the line state + eor x1, x4 ;[-1]=[11] + out USBOUT, x1 ;[0] + ror shift ;[1] + ror x2 ;[2] transfers the last sent bit to the stuffing history +didStuffN: + nop ;[3] + nop ;[4] + cpi x2, 0xfc ;[5] if we sent six consecutive ones + brcc bitstuffN ;[6] + lsr bitcnt ;[7] + brne txBitLoop ;[8] restart the loop while the 1 is still in the bitcount + +; transmit bit 7 + sbrs shift, 0 ;[9] + eor x1, x4 ;[10] +didStuff7: + ror shift ;[11] + out USBOUT, x1 ;[0] transfer bit 7 to the pins + ror x2 ;[1] move the bit into the stuffing history + cpi x2, 0xfc ;[2] + brcc bitstuff7 ;[3] + ld shift, y+ ;[4] get next byte to transmit + dec cnt ;[5] decrement byte counter + brne txByteLoop ;[7] if we have more bytes start next one + ;[8] branch delay + +;make SE0: + cbr x1, USBMASK ;[8] prepare SE0 [spec says EOP may be 25 to 30 cycles] + lds x2, usbNewDeviceAddr;[9] + lsl x2 ;[11] we compare with left shifted address + out USBOUT, x1 ;[0] <-- out SE0 -- from now 2 bits = 24 cycles until bus idle + subi YL, 20 + 2 ;[1] Only assign address on data packets, not ACK/NAK in x3 + sbci YH, 0 ;[2] +;2006-03-06: moved transfer of new address to usbDeviceAddr from C-Code to asm: +;set address only after data packet was sent, not after handshake + breq skipAddrAssign ;[3] + sts usbDeviceAddr, x2 ; if not skipped: SE0 is one cycle longer +skipAddrAssign: +;end of usbDeviceAddress transfer + ldi x2, 1< +int main (int argc, char **argv) +{ + int i, j; + for (i=0; i<512; i++){ + unsigned short crc = i & 0xff; + for(j=0; j<8; j++) crc = (crc >> 1) ^ ((crc & 1) ? 0xa001 : 0); + if((i & 7) == 0) printf("\n.byte "); + printf("0x%02x, ", (i > 0xff ? (crc >> 8) : crc) & 0xff); + if(i == 255) printf("\n"); + } + return 0; +} + +// Use the following algorithm to compute CRC values: +ushort computeCrc(uchar *msg, uchar msgLen) +{ + uchar i; + ushort crc = 0xffff; + for(i = 0; i < msgLen; i++) + crc = usbCrcTable16[lo8(crc) ^ msg[i]] ^ hi8(crc); + return crc; +} +*/ + +.balign 256 +usbCrcTableLow: +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41 +.byte 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 + +; .balign 256 +usbCrcTableHigh: +.byte 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2 +.byte 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04 +.byte 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E +.byte 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8 +.byte 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A +.byte 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC +.byte 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6 +.byte 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10 +.byte 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32 +.byte 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4 +.byte 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE +.byte 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38 +.byte 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA +.byte 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C +.byte 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26 +.byte 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0 +.byte 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62 +.byte 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4 +.byte 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE +.byte 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68 +.byte 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA +.byte 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C +.byte 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76 +.byte 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0 +.byte 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92 +.byte 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54 +.byte 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E +.byte 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98 +.byte 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A +.byte 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C +.byte 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86 +.byte 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 + diff --git a/firmware/fnordlicht-controller/usbdrv/usbdrvasm20.inc b/firmware/fnordlicht-controller/usbdrv/usbdrvasm20.inc new file mode 100644 index 0000000..303abaf --- /dev/null +++ b/firmware/fnordlicht-controller/usbdrv/usbdrvasm20.inc @@ -0,0 +1,360 @@ +/* Name: usbdrvasm20.inc + * Project: V-USB, virtual USB port for Atmel's(r) AVR(r) microcontrollers + * Author: Jeroen Benschop + * Based on usbdrvasm16.inc from Christian Starkjohann + * Creation Date: 2008-03-05 + * Tabsize: 4 + * Copyright: (c) 2008 by Jeroen Benschop and OBJECTIVE DEVELOPMENT Software GmbH + * License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) + * Revision: $Id: usbdrvasm20.inc 740 2009-04-13 18:23:31Z cs $ + */ + +/* Do not link this file! Link usbdrvasm.S instead, which includes the + * appropriate implementation! + */ + +/* +General Description: +This file is the 20 MHz version of the asssembler part of the USB driver. It +requires a 20 MHz crystal (not a ceramic resonator and not a calibrated RC +oscillator). + +See usbdrv.h for a description of the entire driver. + +Since almost all of this code is timing critical, don't change unless you +really know what you are doing! Many parts require not only a maximum number +of CPU cycles, but even an exact number of cycles! +*/ + +#define leap2 x3 +#ifdef __IAR_SYSTEMS_ASM__ +#define nextInst $+2 +#else +#define nextInst .+0 +#endif + +;max stack usage: [ret(2), YL, SREG, YH, bitcnt, shift, x1, x2, x3, x4, cnt] = 12 bytes +;nominal frequency: 20 MHz -> 13.333333 cycles per bit, 106.666667 cycles per byte +; Numbers in brackets are clocks counted from center of last sync bit +; when instruction starts +;register use in receive loop: +; shift assembles the byte currently being received +; x1 holds the D+ and D- line state +; x2 holds the previous line state +; x4 (leap) is used to add a leap cycle once every three bytes received +; X3 (leap2) is used to add a leap cycle once every three stuff bits received +; bitcnt is used to determine when a stuff bit is due +; cnt holds the number of bytes left in the receive buffer + +USB_INTR_VECTOR: +;order of registers pushed: YL, SREG YH, [sofError], bitcnt, shift, x1, x2, x3, x4, cnt + push YL ;[-28] push only what is necessary to sync with edge ASAP + in YL, SREG ;[-26] + push YL ;[-25] + push YH ;[-23] +;---------------------------------------------------------------------------- +; Synchronize with sync pattern: +;---------------------------------------------------------------------------- +;sync byte (D-) pattern LSb to MSb: 01010100 [1 = idle = J, 0 = K] +;sync up with J to K edge during sync pattern -- use fastest possible loops +;The first part waits at most 1 bit long since we must be in sync pattern. +;YL is guarenteed to be < 0x80 because I flag is clear. When we jump to +;waitForJ, ensure that this prerequisite is met. +waitForJ: + inc YL + sbis USBIN, USBMINUS + brne waitForJ ; just make sure we have ANY timeout +waitForK: +;The following code results in a sampling window of < 1/4 bit which meets the spec. + sbis USBIN, USBMINUS ;[-19] + rjmp foundK ;[-18] + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK + sbis USBIN, USBMINUS + rjmp foundK +#if USB_COUNT_SOF + lds YL, usbSofCount + inc YL + sts usbSofCount, YL +#endif /* USB_COUNT_SOF */ +#ifdef USB_SOF_HOOK + USB_SOF_HOOK +#endif + rjmp sofError +foundK: ;[-16] +;{3, 5} after falling D- edge, average delay: 4 cycles +;bit0 should be at 34 for center sampling. Currently at 4 so 30 cylces till bit 0 sample +;use 1 bit time for setup purposes, then sample again. Numbers in brackets +;are cycles from center of first sync (double K) bit after the instruction + push bitcnt ;[-16] +; [---] ;[-15] + lds YL, usbInputBufOffset;[-14] +; [---] ;[-13] + clr YH ;[-12] + subi YL, lo8(-(usbRxBuf));[-11] [rx loop init] + sbci YH, hi8(-(usbRxBuf));[-10] [rx loop init] + push shift ;[-9] +; [---] ;[-8] + ldi shift,0x40 ;[-7] set msb to "1" so processing bit7 can be detected + nop2 ;[-6] +; [---] ;[-5] + ldi bitcnt, 5 ;[-4] [rx loop init] + sbis USBIN, USBMINUS ;[-3] we want two bits K (sample 3 cycles too early) + rjmp haveTwoBitsK ;[-2] + pop shift ;[-1] undo the push from before + pop bitcnt ;[1] + rjmp waitForK ;[3] this was not the end of sync, retry +; The entire loop from waitForK until rjmp waitForK above must not exceed two +; bit times (= 27 cycles). + +;---------------------------------------------------------------------------- +; push more registers and initialize values while we sample the first bits: +;---------------------------------------------------------------------------- +haveTwoBitsK: + push x1 ;[0] + push x2 ;[2] + push x3 ;[4] (leap2) + ldi leap2, 0x55 ;[6] add leap cycle on 2nd,5th,8th,... stuff bit + push x4 ;[7] == leap + ldi leap, 0x55 ;[9] skip leap cycle on 2nd,5th,8th,... byte received + push cnt ;[10] + ldi cnt, USB_BUFSIZE ;[12] [rx loop init] + ldi x2, 1< +#ifndef __IAR_SYSTEMS_ASM__ +# include +#endif + +#define __attribute__(arg) /* not supported on IAR */ + +#ifdef __IAR_SYSTEMS_ASM__ +# define __ASSEMBLER__ /* IAR does not define standard macro for asm */ +#endif + +#ifdef __HAS_ELPM__ +# define PROGMEM __farflash +#else +# define PROGMEM __flash +#endif + +#define USB_READ_FLASH(addr) (*(PROGMEM char *)(addr)) + +/* The following definitions are not needed by the driver, but may be of some + * help if you port a gcc based project to IAR. + */ +#define cli() __disable_interrupt() +#define sei() __enable_interrupt() +#define wdt_reset() __watchdog_reset() +#define _BV(x) (1 << (x)) + +/* assembler compatibility macros */ +#define nop2 rjmp $+2 /* jump to next instruction */ +#define XL r26 +#define XH r27 +#define YL r28 +#define YH r29 +#define ZL r30 +#define ZH r31 +#define lo8(x) LOW(x) +#define hi8(x) (((x)>>8) & 0xff) /* not HIGH to allow XLINK to make a proper range check */ + +/* Depending on the device you use, you may get problems with the way usbdrv.h + * handles the differences between devices. Since IAR does not use #defines + * for MCU registers, we can't check for the existence of a particular + * register with an #ifdef. If the autodetection mechanism fails, include + * definitions for the required USB_INTR_* macros in your usbconfig.h. See + * usbconfig-prototype.h and usbdrv.h for details. + */ + +/* ------------------------------------------------------------------------- */ +#elif __CODEVISIONAVR__ /* check for CodeVision AVR */ +/* ------------------------------------------------------------------------- */ +/* This port is not working (yet) */ + +/* #define F_CPU _MCU_CLOCK_FREQUENCY_ seems to be defined automatically */ + +#include +#include + +#define __attribute__(arg) /* not supported on IAR */ + +#define PROGMEM __flash +#define USB_READ_FLASH(addr) (*(PROGMEM char *)(addr)) + +#ifndef __ASSEMBLER__ +static inline void cli(void) +{ + #asm("cli"); +} +static inline void sei(void) +{ + #asm("sei"); +} +#endif +#define _delay_ms(t) delay_ms(t) +#define _BV(x) (1 << (x)) +#define USB_CFG_USE_SWITCH_STATEMENT 1 /* macro for if() cascase fails for unknown reason */ + +#define macro .macro +#define endm .endmacro +#define nop2 rjmp .+0 /* jump to next instruction */ + +/* ------------------------------------------------------------------------- */ +#else /* default development environment is avr-gcc/avr-libc */ +/* ------------------------------------------------------------------------- */ + +#include +#ifdef __ASSEMBLER__ +# define _VECTOR(N) __vector_ ## N /* io.h does not define this for asm */ +#else +# include +#endif + +#define USB_READ_FLASH(addr) pgm_read_byte(addr) + +#define macro .macro +#define endm .endm +#define nop2 rjmp .+0 /* jump to next instruction */ + +#endif /* development environment */ + +/* for conveniecne, ensure that PRG_RDB exists */ +#ifndef PRG_RDB +# define PRG_RDB(addr) USB_READ_FLASH(addr) +#endif +#endif /* __usbportability_h_INCLUDED__ */ diff --git a/firmware/fnordlicht-firmware/COPYING b/firmware/fnordlicht-firmware/COPYING new file mode 100644 index 0000000..94a9ed0 --- /dev/null +++ b/firmware/fnordlicht-firmware/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If 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 convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state 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 program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/firmware/fnordlicht-firmware/Makefile b/firmware/fnordlicht-firmware/Makefile new file mode 100644 index 0000000..0fffc69 --- /dev/null +++ b/firmware/fnordlicht-firmware/Makefile @@ -0,0 +1,230 @@ +#################################################### +# fnordlicht-ng Makefile +#################################################### + +# ATTENTION: +# Any of these variables are overridden with the values from the file +# "config.mk", you can tune your settings there. The file "config.mk" is not +# under version control. +# Just run 'make' to create a "config.mk" with the default settings + +# hardware type, possible values (default is unset): +# ATTENTION: please configure in config.mk, this is just included here for reference! +# - fnordlicht -- original fnordlicht hardware +# - fnordlichtmini -- fnordlichtmini hardware +#HARDWARE = fnordlicht + +# controller +MCU = atmega8 + +# frequency +F_CPU = 16000000UL + +# main application name (without .hex) +# eg 'test' when the main function is defined in 'test.c' +TARGET = fnordlicht + +# c sourcecode files +# eg. 'test.c foo.c foobar/baz.c' +SRC = $(wildcard *.c) + +# asm sourcecode files +# eg. 'interrupts.S foobar/another.S' +ASRC = + +# headers which should be considered when recompiling +# eg. 'global.h foobar/important.h' +HEADERS = $(wildcard *.h) + +# include directories (used for both, c and asm) +# eg '. usbdrv/' +INCLUDES = + + +# use more debug-flags when compiling +DEBUG = 0 + +# default static color program +CONFIG_SCRIPT_DEFAULT = 1 +# include the script interpreter +CONFIG_SCRIPT = 1 +# include uart support +CONFIG_SERIAL = 1 +# include remote command support (needs uart) +CONFIG_REMOTE = 1 +# secondary pwm output +CONFIG_SECONDARY_PWM = 1 +# default baudrate +CONFIG_SERIAL_BAUDRATE = 19200 +# check jumper for master mode +CONFIG_MASTER_MODE = 1 + +# avrdude programmer protocol +PROG = usbasp +# avrdude programmer device +DEV = usb +# further flags for avrdude +AVRDUDE_FLAGS = + +CFLAGS += -DHARDWARE_$(HARDWARE)=1 -DCONFIG_SCRIPT_DEFAULT=$(CONFIG_SCRIPT_DEFAULT) +CFLAGS += -DCONFIG_SCRIPT=$(CONFIG_SCRIPT) -DCONFIG_SERIAL=$(CONFIG_SERIAL) +CFLAGS += -DCONFIG_REMOTE=$(CONFIG_REMOTE) -DCONFIG_SECONDARY_PWM=$(CONFIG_SECONDARY_PWM) +CFLAGS += -DCONFIG_SERIAL_BAUDRATE=$(CONFIG_SERIAL_BAUDRATE) +CFLAGS += -DCONFIG_MASTER_MODE=$(CONFIG_MASTER_MODE) + +#################################################### +# 'make' configuration +#################################################### +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AS = avr-as +SIZE = avr-size +CP = cp +RM = rm -f +RMDIR = rm -rf +MKDIR = mkdir +AVRDUDE = avrdude + +# flags for automatic dependency handling +DEPFLAGS = -MD -MP -MF .dep/$(@F).d + +# flags for the compiler (for .c files) +CFLAGS += -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -std=gnu99 -fshort-enums $(DEPFLAGS) +CFLAGS += $(addprefix -I,$(INCLUDES)) +# optimize for size +CFLAGS += -mcall-prologues +CFLAGS += --param inline-call-cost=2 -finline-limit=3 -fno-inline-small-functions +CFLAGS += -Wl,--relax +CFLAGS += -fno-split-wide-types + +# place each function in an own section and do garbage collection +CFLAGS += -ffunction-sections -fdata-sections +CFLAGS += -Wl,--gc-sections + +# flags for the compiler (for .S files) +ASFLAGS += -g -mmcu=$(MCU) -DF_CPU=$(F_CPU) -x assembler-with-cpp $(DEPFLAGS) +ASFLAGS += $(addprefix -I,$(INCLUDES)) + +# flags for the linker +LDFLAGS += -mmcu=$(MCU) + +# fill in object files +OBJECTS += $(SRC:.c=.o) +OBJECTS += $(ASRC:.S=.o) + +.PHONY: all + +# main make target (moved up here because of the config.mk target) +all: $(TARGET).hex $(TARGET).bin + +# create config.mk (if it does not exist yet) +$(CURDIR)/config.mk: + @$(CP) config.mk.template config.mk + @echo "====================================================" + @echo "created file $@" + @echo 'please tune your settings (especially $$HARDWARE)' + @echo "there, then run 'make' again" + @echo "====================================================" + @exit 1 + +# include config file +-include $(CURDIR)/config.mk + +# include more debug flags, if $(DEBUG) is 1 +ifeq ($(DEBUG),1) + CFLAGS += -Wall -W -Wchar-subscripts -Wmissing-prototypes + CFLAGS += -Wmissing-declarations -Wredundant-decls + CFLAGS += -Wstrict-prototypes -Wshadow -Wbad-function-cast + CFLAGS += -Winline -Wpointer-arith -Wsign-compare + CFLAGS += -Wunreachable-code -Wdisabled-optimization + CFLAGS += -Wcast-align -Wwrite-strings -Wnested-externs -Wundef + CFLAGS += -Wa,-adhlns=$(basename $@).lst + CFLAGS += -DCONFIG_DEBUG=1 +endif + +#################################################### +# avrdude configuration +#################################################### +ifeq ($(MCU),atmega8) + AVRDUDE_MCU=m8 +endif +ifeq ($(MCU),atmega48) + AVRDUDE_MCU=m48 +endif +ifeq ($(MCU),atmega88) + AVRDUDE_MCU=m88 +endif +ifeq ($(MCU),atmega168) + AVRDUDE_MCU=m168 +endif + +AVRDUDE_FLAGS += -p $(AVRDUDE_MCU) + +#################################################### +# make targets +#################################################### + +.PHONY: hardware-check clean distclean avrdude-terminal + +# check if HARDWARE is set +hardware-check: +ifeq ($(HARDWARE),) + @echo 'please edit config.mk and set $$HARDWARE' + @exit 1 +endif + +$(TARGET).elf: hardware-check $(OBJECTS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJECTS) + +# all objects (.o files) and config.mk +$(OBJECTS): $(HEADERS) config.mk + +# remove all compiled files +clean: + $(RM) $(foreach ext,elf hex eep.hex bin map,$(TARGET).$(ext)) \ + $(foreach file,$(patsubst %.o,%,$(OBJECTS)),$(foreach ext,o lst lss,$(file).$(ext))) + +# additionally remove the dependency makefile and config.mk +distclean: clean + $(RMDIR) .dep + $(RM) config.mk + +# avrdude-related targets +install program: program-$(TARGET) + +avrdude-terminal: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -t + +program-%: %.hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U flash:w:$< + +program-eeprom-%: %.eep.hex + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U eeprom:w:$< + +# special programming targets +%.hex: %.elf + $(OBJCOPY) -O ihex -R .eeprom $< $@ + @echo "====================================================" + @echo "$@ compiled for hardware: $(HARDWARE)" + @echo "using controller $(MCU)" + @echo -n "size for $< is " + @$(SIZE) -A $@ | grep '\.sec1' | tr -s ' ' | cut -d" " -f2 + @echo "====================================================" + +# special programming targets +%.bin: %.elf + $(OBJCOPY) -O binary -R .eeprom $< $@ + +%.eep.hex: %.elf + $(OBJCOPY) --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex -j .eeprom $< $@ + +%.lss: %.elf + $(OBJDUMP) -h -S $< > $@ + +.PHONY: fuses-atmega8-fnordlichtmini-without-bootloader + +fuses-atmega8-fnordlichtmini-without-bootloader: + $(AVRDUDE) $(AVRDUDE_FLAGS) -c $(PROG) -P $(DEV) -U lfuse:w:0x3f:m -U hfuse:w:0xd9:m + +-include $(shell $(MKDIR) .dep 2>/dev/null) $(wildcard .dep/*) diff --git a/firmware/fnordlicht-firmware/color.h b/firmware/fnordlicht-firmware/color.h new file mode 100644 index 0000000..6ab2533 --- /dev/null +++ b/firmware/fnordlicht-firmware/color.h @@ -0,0 +1,107 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +/* for PWM_CHANNELS */ +#include "globals.h" + +#ifndef COLOR_H +#define COLOR_H + +#if PWM_CHANNELS != 3 +#error "PWM_CHANNELS is not 3, this is unsupported!" +#endif + +struct rgb_color_t +{ + union { + struct { + uint8_t red; + uint8_t green; + uint8_t blue; + }; + uint8_t rgb[3]; + }; +}; + +struct rgb_color_offset_t { + union { + struct { + int8_t red; + int8_t green; + int8_t blue; + }; + int8_t rgb[3]; + }; +}; + +struct hsv_color_t +{ + union { + struct { + uint16_t hue; + uint8_t saturation; + uint8_t value; + }; + uint8_t hsv[4]; + }; +}; + +struct hsv_color_offset_t { + int16_t hue; + int8_t saturation; + int8_t value; +}; + +struct dual_color_t +{ + struct rgb_color_t rgb; + struct hsv_color_t hsv; +}; + +union color_t +{ + /* rgb */ + struct { + union { + struct { + uint8_t red; + uint8_t green; + uint8_t blue; + }; + uint8_t rgb[3]; + }; + /* marker, 0xff if rgb */ + uint8_t rgb_marker; + }; + + /* hsv */ + struct { + uint8_t saturation; + uint8_t value; + /* 0 <= hue <= 349, otherwise rgb might be assumed */ + uint16_t hue; + }; +}; + +#endif diff --git a/firmware/fnordlicht-firmware/config.h b/firmware/fnordlicht-firmware/config.h new file mode 100644 index 0000000..b708a48 --- /dev/null +++ b/firmware/fnordlicht-firmware/config.h @@ -0,0 +1,99 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/* + * ATTENTION: + * This file is under version control. Please use the file "config.mk" (which + * is not under version control) for configuring the fnordlicht firmware. + * A file with all the default values will be created by running 'make' the + * first time. + */ + +#ifndef _FNORDLICHT_CONFIG_H +#define _FNORDLICHT_CONFIG_H + +/* debug defines */ +#ifndef CONFIG_DEBUG +#define CONFIG_DEBUG 0 +#endif + +/* include the script interpreter per default */ +#ifndef CONFIG_SCRIPT +#define CONFIG_SCRIPT 1 +#endif + +/* define default startup script (0 is colorwheel, 1 is random) */ +#ifndef CONFIG_SCRIPT_DEFAULT +#define CONFIG_SCRIPT_DEFAULT 1 +#endif + +/* include uart support per default */ +#ifndef CONFIG_SERIAL +#define CONFIG_SERIAL 1 +#endif + +/* include remote command support per default */ +#ifndef CONFIG_REMOTE +#define CONFIG_REMOTE 1 +#endif + +/* secondary output pins */ +#ifndef CONFIG_SECONDARY_PWM +#define CONFIG_SECONDARY_PWM 1 +#endif + +/* set default baudrate */ +#ifndef CONFIG_SERIAL_BAUDRATE +#define CONFIG_SERIAL_BAUDRATE 19200 +#endif + +/* configure master mode: + * 0 disable master mode completely + * 1 check if master mode jumper is set (default) + * 2 always act as master + */ +#ifndef CONFIG_MASTER_MODE +#define CONFIG_MASTER_MODE 1 +#endif + +/* check if hardware is valid */ +#if defined(HARDWARE_fnordlicht) + /* specific settings for old fnordlicht hardware */ + #if !defined(PWM_INVERTED) + #define PWM_INVERTED + #endif + + /* disable secondary pwm for old fnordlicht hardware */ + #if defined(CONFIG_SECONDARY_PWM) && CONFIG_SECONDARY_PWM + #undef CONFIG_SECONDARY_PWM + #define CONFIG_SECONDARY_PWM 0 + #endif + +#elif defined(HARDWARE_fnordlichtmini) + /* specific settings for fnordlichtmini hardware */ + +#else +#error "unknown HARDWARE platform!" +#endif + +#endif /* _FNORDLICHT_CONFIG_H */ diff --git a/firmware/fnordlicht-firmware/config.mk.template b/firmware/fnordlicht-firmware/config.mk.template new file mode 100644 index 0000000..0801f07 --- /dev/null +++ b/firmware/fnordlicht-firmware/config.mk.template @@ -0,0 +1,74 @@ +# Put your own config here! +# +# This file is not under version control, and your settings +# will not be modified when upgrading the repository + + +################################################################### +# HARDWARE PLATFORM TYPE +# +# possible values: +# - fnordlicht -- original fnordlicht hardware +# - fnordlichtmini -- fnordlichtmini hardware +#HARDWARE = fnordlicht + +# default program +# +# possible values: +# - 0 -- colorwheel +# - 1 -- random colors (default) +#CONFIG_SCRIPT_DEFAULT = 1 + +################################################################### +# PROGRAMMER +# +# avrdude programmer protocol +#PROG = usbasp + +# avrdude programmer device +#DEV = usb + +# further flags for avrdude +#AVRDUDE_FLAGS = + + +################################################################### +# controller +# +# possible values: +# - atmega8 +# - atmega88 +# - atmega168 +# +#MCU = atmega8 + +# frequency +#F_CPU = 16000000UL + +# use more debug-flags when compiling +#DEBUG = 1 + +################################################################### +# FIRMWARE CONFIGURATION +# + +# include the script interpreter +#CONFIG_SCRIPT = 1 + +# include uart support +#CONFIG_SERIAL = 1 + +# include remote command support (needs uart) +#CONFIG_REMOTE = 1 + +# secondary pwm output +#CONFIG_SECONDARY_PWM = 1 + +# default baudrate +#CONFIG_SERIAL_BAUDRATE = 19200 + +# configure master mode: +# 0 disable master mode completely +# 1 check if master mode jumper is set (default) +# 2 always act as master +#CONFIG_MASTER_MODE = 1 diff --git a/firmware/fnordlicht-firmware/fifo.c b/firmware/fnordlicht-firmware/fifo.c new file mode 100644 index 0000000..e20696b --- /dev/null +++ b/firmware/fnordlicht-firmware/fifo.c @@ -0,0 +1,61 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "fifo.h" + +void fifo_init(fifo_t *f) +{ + f->read = 0; + f->write = 0; +} + +void fifo_enqueue(fifo_t *f, fifo_content_t data) +{ + f->buffer[f->write] = data; + f->write = (f->write + 1) % CONFIG_FIFO_SIZE; +} + +fifo_content_t fifo_dequeue(fifo_t *f) +{ + fifo_content_t data = f->buffer[f->read]; + f->read = (f->read + 1) % CONFIG_FIFO_SIZE; + return data; +} + +fifo_size_t fifo_fill(fifo_t *f) +{ + if (f->write >= f->read) + return f->write - f->read; + else + return CONFIG_FIFO_SIZE - (f->read - f->write); +} + +bool fifo_empty(fifo_t *f) +{ + return f->read == f->write; +} + +bool fifo_full(fifo_t *f) +{ + return fifo_fill(f) == CONFIG_FIFO_SIZE-1; +} diff --git a/firmware/fnordlicht-firmware/fifo.h b/firmware/fnordlicht-firmware/fifo.h new file mode 100644 index 0000000..c54035a --- /dev/null +++ b/firmware/fnordlicht-firmware/fifo.h @@ -0,0 +1,48 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __FIFO_H +#define __FIFO_H + +#include +#include +#include "globals.h" + +typedef uint8_t fifo_content_t; +typedef uint8_t fifo_size_t; + +typedef struct +{ + fifo_size_t read; + fifo_size_t write; + fifo_content_t buffer[CONFIG_FIFO_SIZE]; +} fifo_t; + +void fifo_init(fifo_t *f); +void fifo_enqueue(fifo_t *f, fifo_content_t data); +fifo_content_t fifo_dequeue(fifo_t *f); +fifo_size_t fifo_fill(fifo_t *f); +bool fifo_empty(fifo_t *f); +bool fifo_full(fifo_t *f); + +#endif diff --git a/firmware/fnordlicht-firmware/fnordlicht.c b/firmware/fnordlicht-firmware/fnordlicht.c new file mode 100644 index 0000000..b5e2ffe --- /dev/null +++ b/firmware/fnordlicht-firmware/fnordlicht.c @@ -0,0 +1,115 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/* includes */ +#include "globals.h" +#include "../common/io.h" + +#include +#include +#include +#include + +#include "../common/common.h" +#include "pwm.h" +#include "uart.h" +#include "remote.h" +#include "timer.h" +#include "script.h" +#include "storage.h" + +static void startup(void) +{ + /* if configuration is valid */ + if (storage_valid_config()) { + + /* read default mode from storage (do nothing if mode is invalid) */ + if (startup_config.params.mode == STARTUP_PROGRAM) { + /* start program */ + script_start(0, startup_config.params.program, (union program_params_t *)startup_config.params.program_parameters); + } + } else { + /* start default program */ + script_start_default(); + +#if !CONFIG_SCRIPT + /* or set some default color */ + global_pwm.target.red = 50; +#endif + } +} + +/* NEVER CALL DIRECTLY! */ +void disable_watchdog(void) \ + __attribute__((naked)) \ + __attribute__((section(".init3"))); +void disable_watchdog(void) +{ + MCUSR = 0; + wdt_disable(); +} + +/** main function + */ +int main(void) +{ + pwm_init(); + timer_init(); + uart_init(); + storage_init(); + remote_init(); + script_init(); + + /* do high-level startup configuration */ + startup(); + + /* enable interrupts globally */ + sei(); + + while (1) + { + /* update pwm */ + pwm_poll(); + + /* check for remote commands */ + remote_poll(); + + /* update pwm */ + pwm_poll(); + + /* call scripting */ + script_poll(); + + /* update pwm */ + pwm_poll(); + + /* update fading */ + pwm_poll_fading(); + + /* update pwm */ + pwm_poll(); + + /* process storage requests */ + storage_poll(); + } +} diff --git a/firmware/fnordlicht-firmware/globals.h b/firmware/fnordlicht-firmware/globals.h new file mode 100644 index 0000000..d105874 --- /dev/null +++ b/firmware/fnordlicht-firmware/globals.h @@ -0,0 +1,91 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _FNORDLICHT_GLOBALS_H +#define _FNORDLICHT_GLOBALS_H + +#include +#include "config.h" +#include "../common/common.h" + +/* check for avr-libc version */ +#if __AVR_LIBC_VERSION__ < 10600UL +#error "newer libc version (>= 1.6.0) needed!" +#endif + +/* check if cpu speed is defined */ +#ifndef F_CPU +#error "please define F_CPU! (see Makefile)" +#endif + +/* check if this cpu is supported */ +#if !(defined(__AVR_ATmega8__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__)) +#error "this cpu is not supported yet!" +#endif + +/* fifo size should be a power of 2 and below 128 */ +#define CONFIG_FIFO_SIZE 64 + +/* configure primary pwm pins (MUST be three successive pins in a port) */ +#define PWM_PORT B +#define PWM_CHANNELS 3 +#define PWM_CHANNEL_MASK (_BV(PB0) | _BV(PB1) | _BV(PB2)) +#define PWM_SHIFT 0 + +/* configure secondary pwm pins (MUST be three successive pins in a port) */ +#define PWM2_PORT D +#define PWM2_CHANNEL_MASK (_BV(PD5) | _BV(PD6) | _BV(PD7)) +#define PWM2_SHIFT 5 + +/* min brightness level */ +#define PWM_MIN_BRIGHTNESS 10 + +/* configure maximal static program parameter size */ +#define PROGRAM_PARAMETER_SIZE 10 + +/* number of color-configurations stored in EEPROM */ +#define CONFIG_EEPROM_COLORS 60 + +/* define default supported number of tasks */ +#define CONFIG_SCRIPT_TASKS 1 + +/* configure INT line for remote */ +#define REMOTE_INT_PORT D +#define REMOTE_INT_PIN PD2 + +/* configure jumper pins for remote master mode */ +#define REMOTE_MASTER_PORT C +#define REMOTE_MASTER_PIN1 PC2 +#define REMOTE_MASTER_PIN2 PC3 +/* configure master mode */ +/* use 1 as address for the second device */ +#define MASTER_MODE_FIRST_ADDRESS 1 +/* wait 200ms before sending sync sequence */ +#define MASTER_WAIT_BEFORE_SYNC 20 +/* wait 15 min (900s) between program changes */ +#define MASTER_MODE_SLEEP 900 + +/* configure storage bufer size */ +#define STORAGE_BUFFER_SIZE 13 + +#endif /* _FNORDLICHT_CONFIG_H */ diff --git a/firmware/fnordlicht-firmware/pwm.c b/firmware/fnordlicht-firmware/pwm.c new file mode 100644 index 0000000..241bca6 --- /dev/null +++ b/firmware/fnordlicht-firmware/pwm.c @@ -0,0 +1,686 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +/* includes */ +#include "globals.h" +#include "../common/io.h" + +#include +#include +#include +#include + +#include "../common/common.h" +#include "pwm.h" +#include "timer.h" +#include "remote.h" + +/* abbreviations for port, ddr and pin */ +#define P_PORT _OUTPORT(PWM_PORT) +#define P_DDR _DDRPORT(PWM_PORT) +#define P2_PORT _OUTPORT(PWM2_PORT) +#define P2_DDR _DDRPORT(PWM2_PORT) + +/* TYPES AND PROTOTYPES */ +#define PWM_MAX_TIMESLOTS (2*(PWM_CHANNELS+2)) + +/* encapsulates all pwm data including timeslot and output mask array */ +struct timeslot_t +{ + uint8_t mask; + uint16_t top; +}; + +struct timeslots_t +{ + /* store timslots in a queue */ + struct timeslot_t slot[PWM_MAX_TIMESLOTS]; + + uint8_t read; /* current read head in 'slot' array */ + uint8_t write; /* current write head in 'slot' array */ +}; + +/* internal data for the fading engine */ +struct fading_engine_t +{ + /* a timer for each channel */ + timer_t timer[PWM_CHANNELS]; + + /* and a bitmask, which stands for 'timer is running' */ + uint8_t running; +}; + +/* timer top values for 256 brightness levels (stored in flash) */ +static const uint16_t timeslot_table[] PROGMEM = +{ + 2, 8, 18, 31, 49, 71, 96, 126, + 159, 197, 238, 283, 333, 386, 443, 504, + 569, 638, 711, 787, 868, 953, 1041, 1134, + 1230, 1331, 1435, 1543, 1655, 1772, 1892, 2016, + 2144, 2276, 2411, 2551, 2695, 2842, 2994, 3150, + 3309, 3472, 3640, 3811, 3986, 4165, 4348, 4535, + 4726, 4921, 5120, 5323, 5529, 5740, 5955, 6173, + 6396, 6622, 6852, 7087, 7325, 7567, 7813, 8063, + 8317, 8575, 8836, 9102, 9372, 9646, 9923, 10205, + 10490, 10779, 11073, 11370, 11671, 11976, 12285, 12598, + 12915, 13236, 13561, 13890, 14222, 14559, 14899, 15244, + 15592, 15945, 16301, 16661, 17025, 17393, 17765, 18141, + 18521, 18905, 19293, 19685, 20080, 20480, 20884, 21291, + 21702, 22118, 22537, 22960, 23387, 23819, 24254, 24693, + 25135, 25582, 26033, 26488, 26946, 27409, 27876, 28346, + 28820, 29299, 29781, 30267, 30757, 31251, 31750, 32251, + 32757, 33267, 33781, 34299, 34820, 35346, 35875, 36409, + 36946, 37488, 38033, 38582, 39135, 39692, 40253, 40818, + 41387, 41960, 42537, 43117, 43702, 44291, 44883, 45480, + 46080, 46684, 47293, 47905, 48521, 49141, 49765, 50393, + 51025, 51661, 52300, 52944, 53592, 54243, 54899, 55558, + 56222, 56889, 57560, 58235, 58914, 59598, 60285, 60975, + 61670, 62369, 63072, 63779, 489, 1204, 1922, 2645, + 3371, 4101, 4836, 5574, 6316, 7062, 7812, 8566, + 9324, 10085, 10851, 11621, 12394, 13172, 13954, 14739, + 15528, 16322, 17119, 17920, 18725, 19534, 20347, 21164, + 21985, 22810, 23638, 24471, 25308, 26148, 26993, 27841, + 28693, 29550, 30410, 31274, 32142, 33014, 33890, 34770, + 35654, 36542, 37433, 38329, 39229, 40132, 41040, 41951, + 42866, 43786, 44709, 45636, 46567, 47502, 48441, 49384, + 50331, 51282, 52236, 53195, 54158, 55124, 56095, 57069, + 58047, 59030, 60016, 61006, 62000, 62998 }; + +/* GLOBAL VARIABLES */ +struct global_pwm_t global_pwm; +static struct timeslots_t timeslots; +static struct fading_engine_t fading; + +/* next output bitmask */ +static volatile uint8_t pwm_next_bitmask; + +/* FUNCTIONS AND INTERRUPTS */ +/* prototypes */ +void update_pwm_timeslots(struct rgb_color_t *target); +void update_rgb(uint8_t c); +void enqueue_timeslot(uint8_t mask, uint16_t top); +void dequeue_timeslot(struct timeslot_t *d); +void update_last_timeslot(uint8_t mask); +uint8_t timeslots_fill(void); + +/* initialize pwm hardware and structures */ +void pwm_init(void) +{ + /* init output pins */ + +#ifdef PWM_INVERTED + /* set all pins high -> leds off */ + P_PORT |= PWM_CHANNEL_MASK; +#else + /* set all pins low -> leds off */ + P_PORT &= ~(PWM_CHANNEL_MASK); +#endif + +#if CONFIG_SECONDARY_PWM +#ifdef PWM_INVERTED + /* set all pins high -> leds off */ + P2_PORT |= PWM2_CHANNEL_MASK; +#else + /* set all pins low -> leds off */ + P2_PORT &= ~(PWM2_CHANNEL_MASK); +#endif +#endif + + /* configure pins as outputs */ + P_DDR |= PWM_CHANNEL_MASK; + +#if CONFIG_SECONDARY_PWM + P2_DDR |= PWM2_CHANNEL_MASK; +#endif + + /* initialize timer 1 */ + + /* no prescaler, CTC mode */ + TCCR1B = _BV(CS10) | _BV(WGM12); + + /* enable timer1 overflow (=output compare 1a) + * and output compare 1b interrupt */ + _TIMSK_TIMER1 |= _BV(OCIE1A) | _BV(OCIE1B); + + /* set TOP for CTC mode */ + OCR1A = 64000; + + /* load initial delay, trigger an overflow */ + OCR1B = 65000; + + /* reset structures */ + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + global_pwm.fade_delay[i] = 1; + global_pwm.fade_step[i] = 1; + } + + /* calculate initial timeslots (2 times) */ + update_pwm_timeslots(&global_pwm.current); + update_pwm_timeslots(&global_pwm.current); + + /* set initial timeslot */ + struct timeslot_t t; + dequeue_timeslot(&t); + pwm_next_bitmask = t.mask; + + /* disable fading timers */ + fading.running = 0; +} + +/* prepare new timeslots */ +void pwm_poll(void) +{ + /* refill timeslots queue */ + while (timeslots_fill() < (PWM_MAX_TIMESLOTS-PWM_CHANNELS-2)) + update_pwm_timeslots(&global_pwm.current); +} + +/* update color values for current fading */ +void pwm_poll_fading(void) +{ + uint8_t mask = 1; + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + /* check running timers */ + if ( (fading.running & mask) && timer_expired(&fading.timer[i])) { + update_rgb(i); + fading.running &= ~mask; + } + + /* if timer is not running and current != target, start timer */ + if (!(fading.running & mask) + && global_pwm.current.rgb[i] != global_pwm.target.rgb.rgb[i] + && global_pwm.fade_delay[i] > 0) { + timer_set(&fading.timer[i], global_pwm.fade_delay[i]); + fading.running |= mask; + } + + /* shift mask */ + mask <<= 1; + } +} + +/** update pwm timeslot table (for target color) */ +void update_pwm_timeslots(struct rgb_color_t *target) +{ + static uint8_t sorted[PWM_CHANNELS] = {0, 1, 2}; + + /* sort channels according to the current brightness */ + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + for (uint8_t j = i+1; j < PWM_CHANNELS; j++) { + if (target->rgb[sorted[j]] < target->rgb[sorted[i]]) { + uint8_t temp; + + temp = sorted[i]; + sorted[i] = sorted[j]; + sorted[j] = temp; + } + } + } + + /* calculate initial bitmask */ + uint8_t initial_bitmask; +#ifdef PWM_INVERTED + initial_bitmask = PWM_CHANNEL_MASK; +#else + initial_bitmask = 0; +#endif + + uint8_t chanmask = 1; + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + if (target->rgb[i] > PWM_MIN_BRIGHTNESS) { +#ifdef PWM_INVERTED + initial_bitmask &= ~chanmask; +#else + initial_bitmask |= chanmask; +#endif + } + + chanmask <<= 1; + } + + /* insert first timeslot */ + enqueue_timeslot(initial_bitmask, 65000); + + /* calculate timeslots and masks */ + uint8_t mask = initial_bitmask; + uint8_t last_brightness = 0; + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + uint8_t brightness = target->rgb[sorted[i]]; + + /* if color is (nearly off) or max, process next color */ + if (brightness <= PWM_MIN_BRIGHTNESS || brightness == 255) + continue; + + /* check if current timeslot would happen after the middle interrupt */ + if (last_brightness < 181 && brightness >= 181) { + /* insert (normal) middle interrupt */ + enqueue_timeslot(mask, 65000); + } + + /* if brightness is new, allocate a new timeslot */ + if (brightness > last_brightness) { + + /* update mask and last_brightness */ + last_brightness = brightness; + #ifdef PWM_INVERTED + mask |= _BV(sorted[i]); + #else + mask &= ~_BV(sorted[i]); + #endif + + /* save (normal) timeslot */ + uint16_t top = pgm_read_word(×lot_table[target->rgb[sorted[i]] - 1 ]); + enqueue_timeslot(mask, top); + } else { + /* just update mask of last timeslot */ + #ifdef PWM_INVERTED + mask |= _BV(sorted[i]); + #else + mask &= ~_BV(sorted[i]); + #endif + + update_last_timeslot(mask); + } + } + + /* if all interrupts happen before the middle interrupt, insert it here */ + if (last_brightness < 181) + enqueue_timeslot(mask, 65000); +} + +/** fade any channels not already at their target brightness */ +void update_rgb(uint8_t c) +{ + /* return if target reached */ + if (global_pwm.current.rgb[c] == global_pwm.target.rgb.rgb[c]) + return; + + /* check direction */ + if (global_pwm.current.rgb[c] < global_pwm.target.rgb.rgb[c]) { + uint8_t diff = global_pwm.target.rgb.rgb[c] - global_pwm.current.rgb[c]; + + if (diff >= global_pwm.fade_step[c]) + global_pwm.current.rgb[c] += global_pwm.fade_step[c]; + else + global_pwm.current.rgb[c] += diff; + + } else { + uint8_t diff = global_pwm.current.rgb[c] - global_pwm.target.rgb.rgb[c]; + + if (diff >= global_pwm.fade_step[c]) + global_pwm.current.rgb[c] -= global_pwm.fade_step[c]; + else + global_pwm.current.rgb[c] -= diff; + } +} + +/* timeslot queue handling */ +void enqueue_timeslot(uint8_t mask, uint16_t top) +{ + struct timeslot_t *t = ×lots.slot[timeslots.write]; + t->mask = mask; + t->top = top; + timeslots.write = (timeslots.write + 1) % PWM_MAX_TIMESLOTS; +} + +void dequeue_timeslot(struct timeslot_t *d) +{ + struct timeslot_t *t = ×lots.slot[timeslots.read]; + d->mask = t->mask; + d->top = t->top; + timeslots.read = (timeslots.read + 1) % PWM_MAX_TIMESLOTS; +} + +void update_last_timeslot(uint8_t mask) +{ + uint8_t i; + if (timeslots.write > 0) + i = timeslots.write-1; + else + i = PWM_MAX_TIMESLOTS-1; + + timeslots.slot[i].mask = mask; +} + +uint8_t timeslots_fill(void) +{ + if (timeslots.write >= timeslots.read) + return timeslots.write - timeslots.read; + else + return PWM_MAX_TIMESLOTS - (timeslots.read - timeslots.write); +} + +/* convert hsv to rgb color + * (see http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_HSV_to_RGB ) + * and + * http://www.enide.net/webcms/uploads/file/projects/powerpicrgb-irda/hsvspace.pdf + */ +void pwm_hsv2rgb(struct dual_color_t *color) +{ + if (color->hsv.saturation == 0) { + for (uint8_t i = 0; i < PWM_CHANNELS; i++) + color->rgb.rgb[i] = color->hsv.value; + return; + } + + uint16_t h = color->hsv.hue % 360; + uint8_t s = color->hsv.saturation; + uint8_t v = color->hsv.value; + + uint16_t f = ((h % 60) * 255 + 30)/60; + uint16_t p = (v * (255-s)+128)/255; + uint16_t q = ((v * (255 - (s*f+128)/255))+128)/255; + uint16_t t = (v * (255 - ((s * (255 - f))/255)))/255; + + uint8_t i = h/60; + + switch (i) { + case 0: + color->rgb.rgb[0] = v; + color->rgb.rgb[1] = t; + color->rgb.rgb[2] = p; + break; + case 1: + color->rgb.rgb[0] = q; + color->rgb.rgb[1] = v; + color->rgb.rgb[2] = p; + break; + case 2: + color->rgb.rgb[0] = p; + color->rgb.rgb[1] = v; + color->rgb.rgb[2] = t; + break; + case 3: + color->rgb.rgb[0] = p; + color->rgb.rgb[1] = q; + color->rgb.rgb[2] = v; + break; + case 4: + color->rgb.rgb[0] = t; + color->rgb.rgb[1] = p; + color->rgb.rgb[2] = v; + break; + case 5: + color->rgb.rgb[0] = v; + color->rgb.rgb[1] = p; + color->rgb.rgb[2] = q; + break; + } +} + +/* convert rgb to hsv color + * (see http://en.wikipedia.org/wiki/HSL_and_HSV#Conversion_from_RGB_to_HSL_or_HSV ) + * and + * http://www.enide.net/webcms/uploads/file/projects/powerpicrgb-irda/hsvspace.pdf + */ +void pwm_rgb2hsv(struct dual_color_t *color) +{ + /* search min and max */ + uint8_t max = color->rgb.red; + uint8_t min = max; + + if (color->rgb.green > max) + max = color->rgb.green; + if (color->rgb.blue > max) + max = color->rgb.blue; + + if (color->rgb.green < min) + min = color->rgb.green; + if (color->rgb.blue < min) + min = color->rgb.blue; + + uint16_t hue = 0; + uint8_t diff = max - min; + uint8_t diffh = diff/2; + + /* compute value and saturation */ + color->hsv.value = max; + color->hsv.saturation = 0; + + if (max > 0) + color->hsv.saturation = ((255 * diff)+max/2)/max; + else { + color->hsv.saturation = 0; + color->hsv.hue = 0; /* undefined */ + return; + } + + if (max == min) { + hue = 0; + } else if (max == color->rgb.red) { + hue = (60 * (color->rgb.green - color->rgb.blue) + diffh)/diff + 360; + } else if (max == color->rgb.green) { + hue = (60 * (color->rgb.blue - color->rgb.red) + diffh)/diff + 120; + } else if (max == color->rgb.blue) { + hue = (60 * (color->rgb.red - color->rgb.green) + diffh)/diff + 240; + } + + hue = hue % 360; + + color->hsv.hue = hue; +} + +/* stop fading, hold current color */ +void pwm_stop_fading(void) +{ + memcpy(&global_pwm.target.rgb, &global_pwm.current.rgb, sizeof(struct rgb_color_t)); + + /* ignore all timers */ + fading.running = 0; +} + + +static uint8_t diff_abs(uint8_t a, uint8_t b) +{ + if (a > b) + return a-b; + else + return b-a; +} + +static void compute_speed(uint8_t step, uint8_t delay) +{ + /* search for max distance */ + uint8_t max = 0; + uint8_t dist = 0; + + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + uint8_t d = diff_abs(global_pwm.target.rgb.rgb[i], global_pwm.current.rgb[i]); + + if (d > dist) { + max = i; + dist = d; + } + } + + /* adjust fading speeds, relative to max distance */ + global_pwm.fade_step[max] = step; + global_pwm.fade_delay[max] = delay; + + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + if (i == max) + continue; + + uint8_t d = diff_abs(global_pwm.target.rgb.rgb[i], global_pwm.current.rgb[i]); + + uint8_t ratio = 1; + if (d > 0) + ratio = (dist+d/2)/d; + + if (ratio == 0) + ratio = 1; + + global_pwm.fade_delay[i] = ratio * delay; + global_pwm.fade_step[i] = step; + } +} + +void pwm_fade_rgb(struct rgb_color_t *color, uint8_t step, uint8_t delay) +{ + /* apply offsets for step and delay */ + step = remote_apply_offset(step, global_remote.offsets.step); + delay = remote_apply_offset(delay, global_remote.offsets.delay); + + /* set target color */ + for (uint8_t i = 0; i < PWM_CHANNELS; i++) + global_pwm.target.rgb.rgb[i] = color->rgb[i]; + + /* compute correct speed for all channels */ + if (delay == 0) + delay = 1; + compute_speed(step, delay); + + /* disable timer */ + fading.running = 0; +} + +void pwm_fade_hsv(struct hsv_color_t *color, uint8_t step, uint8_t delay) +{ + /* apply offsets for step and delay */ + step = remote_apply_offset(step, global_remote.offsets.step); + delay = remote_apply_offset(delay, global_remote.offsets.delay); + + /* convert color */ + memcpy(&global_pwm.target.hsv, color, sizeof(struct hsv_color_t)); + + /* apply offsets */ + remote_apply_hsv_offset(&global_pwm.target.hsv); + + /* update rgb color in target */ + pwm_hsv2rgb(&global_pwm.target); + + /* compute correct speed for all channels */ + compute_speed(step, delay); + + /* disable timer */ + fading.running = 0; +} + +bool pwm_target_reached(void) +{ + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + if (global_pwm.target.rgb.rgb[i] != global_pwm.current.rgb[i]) + return false; + } + + return true; +} + +/* modify current color */ +void pwm_modify_rgb(struct rgb_color_offset_t *color, uint8_t step, uint8_t delay) +{ + for (uint8_t i = 0; i < PWM_CHANNELS; i++) { + int16_t current = global_pwm.target.rgb.rgb[i]; + current += color->rgb[i]; + + if (current > 255) + current = 255; + if (current < 0) + current = 0; + + global_pwm.target.rgb.rgb[i] = LO8(current); + } + + compute_speed(step, delay); + + /* disable timer */ + fading.running = 0; +} + +void pwm_modify_hsv(struct hsv_color_offset_t *color, uint8_t step, uint8_t delay) +{ + /* convert current target color from rgb to hsv */ + pwm_rgb2hsv(&global_pwm.target); + + /* apply changes, hue */ + global_pwm.target.hsv.hue += color->hue; + + /* saturation */ + int16_t sat = global_pwm.target.hsv.saturation; + sat += color->saturation; + if (sat > 255) + sat = 255; + if (sat < 0) + sat = 0; + global_pwm.target.hsv.saturation = LO8(sat); + + /* value */ + int16_t val = global_pwm.target.hsv.value; + val += color->value; + if (val > 255) + val = 255; + if (val < 0) + val = 0; + global_pwm.target.hsv.value = LO8(val); + + /* re-convert to rgb */ + pwm_hsv2rgb(&global_pwm.target); + + /* compute correct speed for all channels */ + compute_speed(step, delay); + + /* disable timer */ + fading.running = 0; +} + + +/** interrupts*/ + +/** timer1 overflow (=output compare a) interrupt */ +ISR(TIMER1_COMPA_vect) +{ + /* read next_bitmask */ + uint8_t next_bitmask = pwm_next_bitmask; + /* output new values */ + P_PORT = (P_PORT & ~(PWM_CHANNEL_MASK)) | (next_bitmask << PWM_SHIFT); + #if CONFIG_SECONDARY_PWM + P2_PORT = (P2_PORT & ~(PWM2_CHANNEL_MASK)) | (next_bitmask << PWM2_SHIFT); + #endif + + /* prepare next interrupt */ + struct timeslot_t t; + dequeue_timeslot(&t); + + /* if next timeslot would happen too fast or has already happened, just spinlock */ + while (TCNT1 + 100 > t.top) + { + /* spin until timer interrupt is near enough */ + while (t.top > TCNT1); + + /* output new values */ + P_PORT = (P_PORT & ~(PWM_CHANNEL_MASK)) | (t.mask << PWM_SHIFT); + #if CONFIG_SECONDARY_PWM + P2_PORT = (P2_PORT & ~(PWM2_CHANNEL_MASK)) | (t.mask << PWM2_SHIFT); + #endif + + /* load next timeslot */ + dequeue_timeslot(&t); + } + + /* save values for next interrupt */ + OCR1B = t.top; + pwm_next_bitmask = t.mask; +} + +/** timer1 output compare b interrupt */ +ISR(TIMER1_COMPB_vect, ISR_ALIASOF(TIMER1_COMPA_vect)); diff --git a/firmware/fnordlicht-firmware/pwm.h b/firmware/fnordlicht-firmware/pwm.h new file mode 100644 index 0000000..29c5d72 --- /dev/null +++ b/firmware/fnordlicht-firmware/pwm.h @@ -0,0 +1,74 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef PWM_H +#define PWM_H + +#include "timer.h" +#include "color.h" + +#if PWM_CHANNELS != 3 +#error "PWM_CHANNELS is not 3, this is unsupported!" +#endif + +#define PWM_HSV_SIZE sizeof(struct hsv_color_t) + +struct global_pwm_t +{ + /* current color */ + struct rgb_color_t current; + + /* target for fading engine */ + struct dual_color_t target; + + /* delay and step for fading engine */ + uint8_t fade_delay[PWM_CHANNELS]; + uint8_t fade_step[PWM_CHANNELS]; +}; + +extern struct global_pwm_t global_pwm; + +/* prototypes */ +void pwm_init(void); +void pwm_poll(void); +void pwm_poll_fading(void); + +void pwm_fade_rgb(struct rgb_color_t *color, uint8_t step, uint8_t delay); +void pwm_fade_hsv(struct hsv_color_t *color, uint8_t step, uint8_t delay); + +/* return true if fading process is complete */ +bool pwm_target_reached(void); + +/* convert hsv to rgb color */ +void pwm_hsv2rgb(struct dual_color_t *color); +/* convert rgb to hsv color */ +void pwm_rgb2hsv(struct dual_color_t *color); + +/* stop fading, hold current color */ +void pwm_stop_fading(void); + +/* modify color */ +void pwm_modify_rgb(struct rgb_color_offset_t *color, uint8_t step, uint8_t delay); +void pwm_modify_hsv(struct hsv_color_offset_t *color, uint8_t step, uint8_t delay); + +#endif diff --git a/firmware/fnordlicht-firmware/remote-proto.h b/firmware/fnordlicht-firmware/remote-proto.h new file mode 100644 index 0000000..6035aec --- /dev/null +++ b/firmware/fnordlicht-firmware/remote-proto.h @@ -0,0 +1,154 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __REMOTE_PROTO +#define __REMOTE_PROTO 1 + +#include "../common/remote-proto.h" +#include "color.h" +#include "static_programs.h" + +struct remote_msg_fade_rgb_t +{ + uint8_t address; + uint8_t cmd; + uint8_t step; + uint8_t delay; + struct rgb_color_t color; +}; + +struct remote_msg_fade_hsv_t +{ + uint8_t address; + uint8_t cmd; + uint8_t step; + uint8_t delay; + struct hsv_color_t color; +}; + +struct remote_msg_save_rgb_t +{ + uint8_t address; + uint8_t cmd; + uint8_t slot; + uint8_t step; + uint8_t delay; + uint16_t pause; + struct rgb_color_t color; +}; + +struct remote_msg_save_hsv_t +{ + uint8_t address; + uint8_t cmd; + uint8_t slot; + uint8_t step; + uint8_t delay; + uint16_t pause; + struct hsv_color_t color; +}; + +struct remote_msg_save_current_t +{ + uint8_t address; + uint8_t cmd; + uint8_t slot; + uint8_t step; + uint8_t delay; + uint16_t pause; +}; + +struct remote_msg_config_offsets_t +{ + uint8_t address; + uint8_t cmd; + int8_t step; + int8_t delay; + int16_t hue; + uint8_t saturation; + uint8_t value; +}; + +struct remote_msg_start_program_t +{ + uint8_t address; + uint8_t cmd; + uint8_t script; + union program_params_t params; +}; + +struct remote_msg_stop_t +{ + uint8_t address; + uint8_t cmd; + uint8_t fade; +}; + +struct remote_msg_modify_current_t +{ + uint8_t address; + uint8_t cmd; + uint8_t step; + uint8_t delay; + struct rgb_color_offset_t rgb; + struct hsv_color_offset_t hsv; +}; + +struct remote_msg_pull_int_t +{ + uint8_t address; + uint8_t cmd; + uint8_t delay; +}; + +enum startup_mode_t +{ + STARTUP_NOTHING = 0, + STARTUP_PROGRAM = 1, +}; + +/* startup parameters including mode, size: 12 byte */ +struct startup_parameters_t +{ + enum startup_mode_t mode; + + union { + /* raw access to data, size: 11 byte */ + uint8_t raw[11]; + + /* structure for startup_mode == STARTUP_PROGRAM + * size: 11 byte */ + struct { + uint8_t program; + uint8_t program_parameters[PROGRAM_PARAMETER_SIZE]; + }; + }; +}; + +struct remote_msg_config_startup_t +{ + uint8_t address; + uint8_t cmd; + struct startup_parameters_t params; +}; + +#endif diff --git a/firmware/fnordlicht-firmware/remote.c b/firmware/fnordlicht-firmware/remote.c new file mode 100644 index 0000000..d6f54f7 --- /dev/null +++ b/firmware/fnordlicht-firmware/remote.c @@ -0,0 +1,606 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include "globals.h" +#include "remote.h" +#include "fifo.h" +#include "uart.h" +#include "pwm.h" +#include "../common/pt/pt.h" +#include "script.h" +#include "remote-proto.h" +#include "../common/common.h" +#include "storage.h" +#include "timer.h" + +/* abbreviations for port, ddr and pin */ +#define R_PORT _OUTPORT(REMOTE_INT_PORT) +#define R_DDR _DDRPORT(REMOTE_INT_PORT) +#define R_PIN _INPORT(REMOTE_INT_PORT) +#define INTPIN REMOTE_INT_PIN + +#define M_PORT _OUTPORT(REMOTE_MASTER_PORT) +#define M_DDR _DDRPORT(REMOTE_MASTER_PORT) +#define M_PIN _INPORT(REMOTE_MASTER_PORT) +#define M_PIN1 REMOTE_MASTER_PIN1 +#define M_PIN2 REMOTE_MASTER_PIN2 + +struct remote_state_t +{ + /* serial communication */ + union { + struct remote_msg_t msg; + uint8_t buf[REMOTE_MSG_LEN]; + }; + uint8_t buflen; + + uint8_t sync; + uint8_t synced; + struct pt thread; +#if CONFIG_MASTER_MODE + struct pt master_thread; +#endif + + /* int line */ + timer_t int_timer; + enum { + INT_IDLE = 0, + INT_PULLED = 1, + INT_PULLED_TIMER = 2, + } int_state; +}; + +struct remote_state_t remote; +struct global_remote_t global_remote; + +#if CONFIG_SERIAL && CONFIG_REMOTE + +/* static parsing routines */ +static void parse_fade_rgb(struct remote_msg_fade_rgb_t *msg); +static void parse_fade_hsv(struct remote_msg_fade_hsv_t *msg); +static void parse_save_rgb(struct remote_msg_save_rgb_t *msg); +static void parse_save_hsv(struct remote_msg_save_hsv_t *msg); +static void parse_save_current(struct remote_msg_save_current_t *msg); +static void parse_config_offsets(struct remote_msg_config_offsets_t *msg); +static void parse_start_program(struct remote_msg_start_program_t *msg); +static void parse_stop(struct remote_msg_stop_t *msg); +static void parse_modify_current(struct remote_msg_modify_current_t *msg); +static void parse_pull_int(struct remote_msg_pull_int_t *msg); +static void parse_config_startup(struct remote_msg_config_startup_t *msg); +static void parse_bootloader(struct remote_msg_bootloader_t *msg); +static void parse_powerdown(void); + +void remote_init(void) +{ + /* master mode check: check if PIN2 pulls down PIN1 */ + /* configure M_PIN1 as input with pullup */ + M_DDR &= ~_BV(M_PIN1); + M_PORT |= _BV(M_PIN1); + /* configure M_PIN2 as output, set low */ + M_DDR |= _BV(M_PIN2); + M_PORT &= ~_BV(M_PIN2); + + /* initialize offsets */ + global_remote.offsets.saturation = 255; + global_remote.offsets.value = 255; + + /* initialize int pin, tri-state */ + R_DDR &= ~_BV(INTPIN); + R_PORT &= ~_BV(INTPIN); +#ifdef INIT_ZERO + remote.int_state = INT_IDLE; + + PT_INIT(&remote.master_thread); +#endif + +#if CONFIG_MASTER_MODE == 1 + /* check master state: read value of M_PIN1 */ + if (!(M_PIN & _BV(M_PIN1))) + global_remote.master = true; +#elif CONFIG_MASTER_MODE == 2 + /* statically configure master mode */ + global_remote.master = true; +#endif +} + +static void remote_parse_msg(struct remote_msg_t *msg) +{ + /* verify address */ + if (msg->address != global_remote.address && msg->address != REMOTE_ADDR_BROADCAST) + return; + + /* parse command */ + switch (msg->cmd) { + case REMOTE_CMD_FADE_RGB: + parse_fade_rgb((struct remote_msg_fade_rgb_t *)msg); + break; + case REMOTE_CMD_FADE_HSV: + parse_fade_hsv((struct remote_msg_fade_hsv_t *)msg); + break; + case REMOTE_CMD_SAVE_RGB: + parse_save_rgb((struct remote_msg_save_rgb_t *)msg); + break; + case REMOTE_CMD_SAVE_HSV: + parse_save_hsv((struct remote_msg_save_hsv_t *)msg); + break; + case REMOTE_CMD_SAVE_CURRENT: + parse_save_current((struct remote_msg_save_current_t *)msg); + break; + case REMOTE_CMD_CONFIG_OFFSETS: + parse_config_offsets((struct remote_msg_config_offsets_t *)msg); + break; + case REMOTE_CMD_START_PROGRAM: + parse_start_program((struct remote_msg_start_program_t *)msg); + break; + case REMOTE_CMD_STOP: + parse_stop((struct remote_msg_stop_t *)msg); + break; + case REMOTE_CMD_MODIFY_CURRENT: + parse_modify_current((struct remote_msg_modify_current_t *)msg); + break; + case REMOTE_CMD_PULL_INT: + parse_pull_int((struct remote_msg_pull_int_t *)msg); + break; + case REMOTE_CMD_CONFIG_STARTUP: + parse_config_startup((struct remote_msg_config_startup_t *)msg); + break; + case REMOTE_CMD_BOOTLOADER: + parse_bootloader((struct remote_msg_bootloader_t *)msg); + break; + case REMOTE_CMD_POWERDOWN: + parse_powerdown(); + break; + + } +} + +static PT_THREAD(remote_thread(struct pt *thread)) +{ + PT_BEGIN(thread); + + while (1) { + PT_WAIT_UNTIL(thread, remote.buflen == REMOTE_MSG_LEN); + + remote_parse_msg(&remote.msg); + remote.buflen = 0; + } + + PT_END(thread); +} + +static __noinline void send_msg(struct remote_msg_t *msg) +{ + uint8_t *ptr = (uint8_t *)msg; + for (uint8_t i = 0; i < REMOTE_MSG_LEN; i++) + uart_putc(*ptr++); +} + + +static void send_resync(uint8_t addr) +{ + for (uint8_t i = 0; i < REMOTE_SYNC_LEN; i++) + uart_putc(REMOTE_CMD_RESYNC); + uart_putc(addr); +} + +#if CONFIG_MASTER_MODE +/* parameters for master mode script commands */ +#define MASTER_PROGRAMS 2 +static PROGMEM uint8_t master_parameters[] = { + /* first: colorwheel forward */ + 0, /* program index */ + 1, /* fade step */ + 2, /* fade delay */ + 0, /* fade sleep */ + 0, 0, /* hue start (little endian) */ + 20, 0, /* hue step (little endian) */ + -1, /* addr add */ + 255, /* saturation */ + 255, /* value */ + + /* first: colorwheel backward */ + 0, /* program index */ + 1, /* fade step */ + 2, /* fade delay */ + 0, /* fade sleep */ + 0, 0, /* hue start (little endian) */ + 20, 0, /* hue step (little endian) */ + 1, /* addr add */ + 255, /* saturation */ + 255, /* value */ +}; + +static PT_THREAD(remote_master_thread(struct pt *thread)) +{ + static struct remote_msg_start_program_t msg; + static timer_t timer; + static uint16_t sleep; + static uint8_t *ptr; + static uint8_t idx; + + PT_BEGIN(thread); + + /* wait */ + timer_set(&timer, MASTER_WAIT_BEFORE_SYNC); + while(!timer_expired(&timer)) + PT_YIELD(thread); + + /* start program on all devices */ + msg.address = 0xff; + msg.cmd = REMOTE_CMD_START_PROGRAM; + + while (1) { + ptr = &master_parameters[0]; + + for (idx = 0; idx < MASTER_PROGRAMS; idx++) { + /* stop current program and fading */ + script_stop(); + pwm_stop_fading(); + + /* start program colorwheel on all nodes */ + msg.script = pgm_read_byte(ptr++); + /* load parameters */ + for (uint8_t i = 0; i < sizeof(msg.params); i++) + msg.params.raw[i] = pgm_read_byte(ptr++); + + /* send */ + send_resync(MASTER_MODE_FIRST_ADDRESS); + PT_YIELD(thread); + send_msg((struct remote_msg_t *)&msg); + + /* start program locally */ + script_start(0, msg.script, &msg.params); + + /* sleep */ + sleep = MASTER_MODE_SLEEP; + while (sleep--) { + /* sleep 1s */ + timer_set(&timer, 100); + + while (!timer_expired(&timer)) + PT_YIELD(thread); + } + } + } + + PT_END(thread); +} +#endif + +void remote_poll(void) +{ + if (fifo_fill((fifo_t *)&global_uart.rx) > 0) { + uint8_t data = fifo_dequeue((fifo_t *)&global_uart.rx); + + /* check if sync sequence has been received before */ + if (remote.sync == REMOTE_SYNC_LEN) { + /* synced, safe address and send next address to following device */ + global_remote.address = data; + uart_putc(data+1); + + /* reset remote buffer */ + remote.buflen = 0; + + /* enable remote command thread */ + remote.synced = 1; + PT_INIT(&remote.thread); +#if CONFIG_MASTER_MODE + global_remote.master = false; +#endif + } else { + /* just pass through data */ + uart_putc(data); + + /* put data into remote buffer + * (just processed if remote.synced == 1) */ + if (remote.buflen < sizeof(remote.buf)) + remote.buf[remote.buflen++] = data; + } + + /* remember the number of sync bytes received so far */ + if (data == REMOTE_CMD_RESYNC) + remote.sync++; + else + remote.sync = 0; + } + + if (remote.synced) + PT_SCHEDULE(remote_thread(&remote.thread)); + + /* check int timer */ + if (remote.int_state == INT_PULLED_TIMER && timer_expired(&remote.int_timer)) { + remote_release_int(); + } + +#if CONFIG_MASTER_MODE + if (global_remote.master) + PT_SCHEDULE(remote_master_thread(&remote.master_thread)); +#endif +} + +void remote_pull_int(void) +{ + if (remote.int_state != INT_IDLE) + return; + + /* pull int pin to ground */ + R_DDR |= _BV(INTPIN); + R_PORT &= ~_BV(INTPIN); +} + +void remote_release_int(void) +{ + if (remote.int_state == INT_IDLE) + return; + + /* reset pin to tri-state */ + R_DDR &= ~_BV(INTPIN); + R_PORT &= ~_BV(INTPIN); + remote.int_state = INT_IDLE; +} + +/* offset helper functions */ +uint8_t remote_apply_offset(uint8_t value, int8_t offset) +{ + if (offset < 0) { + if (value > -offset) + value += offset; + else + value = 1; + } else if (offset > 0) { + uint16_t temp = value; + temp += offset; + if (temp > 255) + value = 255; + else + value = LO8(temp); + } + + return value; +} + +static uint8_t apply_scale(uint8_t value, uint8_t scale) +{ + uint16_t temp = value * scale; + temp /= 255; + return LO8(temp); +} + +void remote_apply_hsv_offset(struct hsv_color_t *color) +{ + color->hue += global_remote.offsets.hue; + color->saturation = apply_scale(color->saturation, global_remote.offsets.saturation); + color->value = apply_scale(color->value, global_remote.offsets.value); +} + +/* parser functions */ + +void parse_fade_rgb(struct remote_msg_fade_rgb_t *msg) +{ + pwm_fade_rgb(&msg->color, msg->step, msg->delay); +} + +void parse_fade_hsv(struct remote_msg_fade_hsv_t *msg) +{ + pwm_fade_hsv(&msg->color, msg->step, msg->delay); +} + +void parse_save_rgb(struct remote_msg_save_rgb_t *msg) +{ + if (msg->slot >= CONFIG_EEPROM_COLORS) + return; + + struct storage_color_t c; + c.step = msg->step; + c.delay = msg->delay; + c.pause = msg->pause; + + /* mark color as rgb */ + c.color.rgb_marker = 255; + + c.color.red = msg->color.red; + c.color.green = msg->color.green; + c.color.blue = msg->color.blue; + + storage_save_color(msg->slot, &c); +} + +void parse_save_hsv(struct remote_msg_save_hsv_t *msg) +{ + if (msg->slot >= CONFIG_EEPROM_COLORS) + return; + + struct storage_color_t c; + c.step = msg->step; + c.delay = msg->delay; + c.pause = msg->pause; + + c.color.hue = msg->color.hue; + c.color.saturation = msg->color.saturation; + c.color.value = msg->color.value; + + storage_save_color(msg->slot, &c); +} + +void parse_save_current(struct remote_msg_save_current_t *msg) +{ + if (msg->slot >= CONFIG_EEPROM_COLORS) + return; + + struct storage_color_t c; + c.step = msg->step; + c.delay = msg->delay; + c.pause = msg->pause; + + /* mark color as rgb */ + c.color.rgb_marker = 255; + + c.color.red = global_pwm.current.red; + c.color.green = global_pwm.current.green; + c.color.blue = global_pwm.current.blue; + + storage_save_color(msg->slot, &c); +} + +void parse_config_offsets(struct remote_msg_config_offsets_t *msg) +{ + global_remote.offsets.step = msg->step; + global_remote.offsets.delay = msg->delay; + global_remote.offsets.hue = msg->hue; + global_remote.offsets.saturation = msg->saturation; + global_remote.offsets.value = msg->value; +} + +void parse_start_program(struct remote_msg_start_program_t *msg) +{ + script_stop(); + pwm_stop_fading(); + script_start(0, msg->script, &msg->params); +} + +void parse_stop(struct remote_msg_stop_t *msg) +{ + script_stop(); + if (msg->fade) + pwm_stop_fading(); +} + +void parse_modify_current(struct remote_msg_modify_current_t *msg) +{ + /* return if a color change is in progress */ +#if CONFIG_SCRIPT + if (!global_script.disable || !pwm_target_reached()) + return; +#else + if (!pwm_target_reached()) + return; +#endif + + /* apply rgb and hsv offsets */ + pwm_modify_rgb(&msg->rgb, msg->step, msg->delay); + pwm_modify_hsv(&msg->hsv, msg->step, msg->delay); +} + +void parse_pull_int(struct remote_msg_pull_int_t *msg) +{ + /* pull pin to ground */ + remote_pull_int(); + + uint8_t delay = msg->delay; + + /* start timer, delay between 50ms and 2550ms */ + if (delay == 0) + delay = 5; + else if (delay <= 51) + delay *= 5; + else + delay = 255; + + timer_set(&remote.int_timer, delay); + + /* remember state */ + remote.int_state = INT_PULLED_TIMER; +} + +void parse_config_startup(struct remote_msg_config_startup_t *msg) +{ + /* set mode and copy data */ + memcpy(&startup_config.params, &msg->params, sizeof(struct startup_parameters_t)); + /* save config to eeprom */ + storage_save_config(); +} + +static void wait_for_uart(void) +{ + while (fifo_fill((fifo_t *)&global_uart.tx) != 0 || !uart_send_complete()); +} + +void parse_bootloader(struct remote_msg_bootloader_t *msg) +{ + /* check magic bytes */ + if (msg->magic[0] != BOOTLOADER_MAGIC_BYTE1 || + msg->magic[1] != BOOTLOADER_MAGIC_BYTE2 || + msg->magic[2] != BOOTLOADER_MAGIC_BYTE3 || + msg->magic[3] != BOOTLOADER_MAGIC_BYTE4) + return; + + /* wait until the tx fifo is empty, then start watchdog, but never kick it + * (bootloader and firmware both disable the watchdog) */ + wait_for_uart(); + wdt_enable(WDTO_120MS); +} + +void parse_powerdown(void) +{ + /* configure sleep mode */ + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + + /* wait until the tx fifo is empty before sleep */ + wait_for_uart(); + + /* save all output registers */ + uint8_t portb = PORTB; + uint8_t portc = PORTC; + uint8_t portd = PORTD; + uint8_t ddrb = DDRB; + uint8_t ddrc = DDRC; + uint8_t ddrd = DDRD; + PORTB = 0; + PORTC = 0; + PORTD = 0; + DDRB = 0; + DDRC = 0; + DDRD = 0; + + /* configure int pin as input (already done by setting the + * DDR register to zero) with pullup */ + R_PORT = _BV(INTPIN); + + /* enable int0 low level interrupt */ + _IFR_INT0 = _BV(INTF0); + _ICR_INT0 |= _BV(INT0); + /* enter sleep mode */ + sleep_mode(); + + /* wakeup, disable int0 */ + _ICR_INT0 &= ~_BV(INT0); + + /* restore output registers */ + PORTB = portb; + PORTC = portc; + PORTD = portd; + DDRB = ddrb; + DDRC = ddrc; + DDRD = ddrd; +} + +/* do nothing, just for wakeup after sleep */ +EMPTY_INTERRUPT(INT0_vect); + +#endif diff --git a/firmware/fnordlicht-firmware/remote.h b/firmware/fnordlicht-firmware/remote.h new file mode 100644 index 0000000..6535489 --- /dev/null +++ b/firmware/fnordlicht-firmware/remote.h @@ -0,0 +1,78 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __REMOTE_H +#define __REMOTE_H + +#include +#include +#include "color.h" + +struct remote_offsets_t +{ + int8_t step; + int8_t delay; + int16_t hue; + uint8_t saturation; + uint8_t value; +}; + +struct global_remote_t { + uint8_t address; + struct remote_offsets_t offsets; + +#if CONFIG_MASTER_MODE + /* master mode */ + bool master; +#endif +}; + +extern struct global_remote_t global_remote; + +/* we depend on serial uart */ +#if !CONFIG_SERIAL || !CONFIG_REMOTE + +#define remote_init(...) +#define remote_poll(...) +#define remote_address(...) 0 + +#define remote_apply_offset(x, ...) x +#define remote_apply_hsv_offset(x, ...) x + +#define remote_pull_int(...) +#define remote_release_int(...) + +#else + +void remote_init(void); +void remote_poll(void); +#define remote_address() (global_remote.address) + +void remote_pull_int(void); +void remote_release_int(void); + +/* offset helper functions */ +uint8_t remote_apply_offset(uint8_t value, int8_t offset); +void remote_apply_hsv_offset(struct hsv_color_t *color); + +#endif +#endif diff --git a/firmware/fnordlicht-firmware/script.c b/firmware/fnordlicht-firmware/script.c new file mode 100644 index 0000000..1fd8c46 --- /dev/null +++ b/firmware/fnordlicht-firmware/script.c @@ -0,0 +1,142 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include "globals.h" +#include "script.h" +#include "static_programs.h" +#include "pwm.h" + +#if CONFIG_SCRIPT + +/* global variables */ +struct global_script_t global_script; + +void script_init(void) +{ + /* initialize global structures */ +#ifdef INIT_ZERO + for (uint8_t i = 0; i < CONFIG_SCRIPT_TASKS; i++) { + global_script.tasks[i].enable = 0; + PT_INIT(&global_script.tasks[i].pt); + } +#endif + + /* initialize timer, delay before start is 200ms */ + timer_set(&global_script.timer, 20); +} + +void script_start_default(void) +{ +#ifdef CONFIG_SCRIPT_DEFAULT +#if CONFIG_SCRIPT_DEFAULT == 0 + /* enable colorwheel program */ + union program_params_t params; + params.colorwheel.hue_start = 0; + params.colorwheel.hue_step = 60; + params.colorwheel.add_addr = 0; + params.colorwheel.saturation = 255; + params.colorwheel.value = 255; + + params.colorwheel.fade_step = 1; + params.colorwheel.fade_delay = 2; + params.colorwheel.fade_sleep = 0; + + script_start(0, 0, ¶ms); +#elif CONFIG_SCRIPT_DEFAULT == 1 + /* enable colorwheel program */ + union program_params_t params; + params.random.seed = 23; + params.random.use_address = 0; + params.random.wait_for_fade = 1; + params.random.min_distance = 60; + + params.random.saturation = 255; + params.random.value = 255; + + params.random.fade_step = 1; + params.random.fade_delay = 3; + params.random.fade_sleep = 100; + + script_start(0, 1, ¶ms); +#else +#warning "CONFIG_SCRIPT_DEFAULT has unknown value! No default program is started." +#endif +#endif +} + +void script_poll(void) +{ + if (global_script.disable) + return; + + if (timer_expired(&global_script.timer)) { + for (uint8_t i = 0; i < CONFIG_SCRIPT_TASKS; i++) { + struct process_t *task = &global_script.tasks[i]; + if (task->enable) { + /* run task, disable if exited */ + if (PT_SCHEDULE(task->execute(task)) == 0) + task->enable = false; + } + } + + /* recall after 100ms */ + timer_set(&global_script.timer, 10); + } +} + +void script_stop(void) +{ + /* stop all tasks */ + for (uint8_t i = 0; i < CONFIG_SCRIPT_TASKS; i++) { + global_script.tasks[i].enable = false; + PT_INIT(&global_script.tasks[i].pt); + } + + /* disable global */ + global_script.disable = true; +} + +void script_start(uint8_t task, uint8_t index, union program_params_t *params) +{ + /* check for valid index */ + if (index >= STATIC_PROGRAMS_LEN) + return; + + /* enable global */ + global_script.disable = false; + + /* copy params from pointer to task structure */ + memcpy(&global_script.tasks[task].params, params, sizeof(union program_params_t)); + + /* load program handler */ + global_script.tasks[task].execute = (program_handler)pgm_read_word(&static_programs[index]); + + /* enable script */ + global_script.tasks[task].enable = true; + + /* reset timer */ + timer_set(&global_script.timer, 10); +} + +#endif diff --git a/firmware/fnordlicht-firmware/script.h b/firmware/fnordlicht-firmware/script.h new file mode 100644 index 0000000..22e680f --- /dev/null +++ b/firmware/fnordlicht-firmware/script.h @@ -0,0 +1,59 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __SCRIPT_H +#define __SCRIPT_H + +#include +#include +#include "globals.h" +#include "../common/pt/pt.h" +#include "timer.h" +#include "static_programs.h" + +#if !CONFIG_SCRIPT +#define script_init(...) +#define script_poll(...) +#define script_stop(...) +#define script_start(...) +#define script_start_default(...) + +#else + +struct global_script_t { + bool disable; + struct process_t tasks[CONFIG_SCRIPT_TASKS]; + timer_t timer; +}; + +/* global variables */ +extern struct global_script_t global_script; + +/* prototypes for scripting engine */ +void script_init(void); +void script_poll(void); +void script_stop(void); +void script_start(uint8_t task, uint8_t index, union program_params_t *params); +void script_start_default(void); + +#endif +#endif diff --git a/firmware/fnordlicht-firmware/static_programs.c b/firmware/fnordlicht-firmware/static_programs.c new file mode 100644 index 0000000..c437fe7 --- /dev/null +++ b/firmware/fnordlicht-firmware/static_programs.c @@ -0,0 +1,200 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include +#include +#include "static_programs.h" +#include "color.h" +#include "pwm.h" +#include "timer.h" +#include "remote.h" +#include "../common/common.h" +#include "storage.h" + +#if CONFIG_SCRIPT + +/* global list of programs */ +PROGMEM program_handler static_programs[] = { + program_colorwheel, + program_random, + program_replay, +}; + +PT_THREAD(program_colorwheel(struct process_t *process)) +{ + static uint16_t sleep; + static struct hsv_color_t c; + + PT_BEGIN(&process->pt); + + c.hue = process->params.colorwheel.hue_start; + c.value = process->params.colorwheel.value; + c.saturation = process->params.colorwheel.saturation; + + int8_t add = process->params.colorwheel.add_addr; + c.hue += remote_address() * add * process->params.colorwheel.hue_step; + + while (1) { + /* set new color */ + pwm_fade_hsv(&c, process->params.colorwheel.fade_step, process->params.colorwheel.fade_delay); + + c.hue += process->params.colorwheel.hue_step; + + /* wait until target reached */ + PT_WAIT_UNTIL(&process->pt, pwm_target_reached()); + + /* sleep (in seconds, remember: we are called every 100ms) */ + sleep = 10 * process->params.colorwheel.fade_sleep; + while (sleep--) + PT_YIELD(&process->pt); + } + + PT_END(&process->pt); +} + +PT_THREAD(program_random(struct process_t *process)) +{ + static uint16_t sleep; + static struct hsv_color_t c; + + PT_BEGIN(&process->pt); + + /* initialize random generator */ + uint16_t seed = process->params.random.seed; + if (process->params.random.use_address) + seed ^= remote_address(); + srandom(seed); + + c.value = process->params.random.value; + c.saturation = process->params.random.saturation; + + while (1) { + /* generate new color */ + union uint32_t_access rnd; + rnd.raw = random(); + + /* use lower word for hue */ + rnd.words[0] %= 360; + + /* check for minimal color distance, regenerate random if not */ + int16_t min_distance = process->params.random.min_distance; + if (min_distance) { + int16_t distance = c.hue - rnd.words[0]; + if (distance < 0) + distance = -distance; + + if (distance > 180) + distance = 360-distance; + + if (distance < min_distance) + continue; + } + + /* copy color to structure */ + c.hue = rnd.words[0]; + + /* fade to new color */ + pwm_fade_hsv(&c, process->params.random.fade_step, process->params.random.fade_delay); + + /* wait until target reached */ + if (process->params.random.wait_for_fade) + PT_WAIT_UNTIL(&process->pt, pwm_target_reached()); + + /* sleep (remember: we are called every 100ms) */ + if (process->params.random.fade_sleep > 0) { + sleep = process->params.random.fade_sleep; + + while (sleep--) + PT_YIELD(&process->pt); + } + } + + PT_END(&process->pt); +} + +PT_THREAD(program_replay(struct process_t *process)) +{ + static uint8_t pos; + static enum { + UP = 0, + DOWN = 1, + } direction; + static struct storage_color_t c; + + PT_BEGIN(&process->pt); + + /* reset variables */ + pos = process->params.replay.start; + direction = UP; + + while (1) { + /* load next color value */ + storage_load_color(pos, &c); + + if (c.color.rgb_marker == 0xff) { + struct rgb_color_t color; + color.red = c.color.red; + color.green = c.color.green; + color.blue = c.color.blue; + + pwm_fade_rgb(&color, c.step, c.delay); + } else { + struct hsv_color_t color; + color.hue = c.color.hue; + color.saturation = c.color.saturation; + color.value = c.color.value; + + pwm_fade_hsv(&color, c.step, c.delay); + } + + /* update pos */ + if (direction == UP) { + /* check for upper end */ + if (pos == process->params.replay.end) { + switch (process->params.replay.repeat) { + case REPEAT_NONE: PT_EXIT(&process->pt); + break; + case REPEAT_START: pos = process->params.replay.start; + break; + case REPEAT_REVERSE: direction = DOWN; + pos--; + break; + } + } else + pos++; + } else { + if (pos == process->params.replay.start) { + direction = UP; + pos++; + } else + pos--; + } + + /* wait */ + while (c.pause--) + PT_YIELD(&process->pt); + } + + PT_END(&process->pt); +} + +#endif diff --git a/firmware/fnordlicht-firmware/static_programs.h b/firmware/fnordlicht-firmware/static_programs.h new file mode 100644 index 0000000..b544db5 --- /dev/null +++ b/firmware/fnordlicht-firmware/static_programs.h @@ -0,0 +1,101 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __STATIC_PROGRAMS_H +#define __STATIC_PROGRAMS_H + +#include +#include +#include "globals.h" +#include "../common/pt/pt.h" + +struct process_t; +/* return value is char, see definition of PT_THREAD() */ +typedef char (*program_handler)(struct process_t *current); + +/* parameter structures (max 10 bytes) */ +struct colorwheel_params_t +{ + uint8_t fade_step; + uint8_t fade_delay; + uint8_t fade_sleep; + uint16_t hue_start; + int16_t hue_step; + int8_t add_addr; + uint8_t saturation; + uint8_t value; +}; + +struct random_params_t +{ + uint16_t seed; + uint8_t use_address:1; + uint8_t wait_for_fade:1; + uint8_t reserved:6; + uint8_t fade_step; + uint8_t fade_delay; + uint16_t fade_sleep; + uint8_t saturation; + uint8_t value; + uint8_t min_distance; +}; + +struct replay_params_t +{ + uint8_t start; + uint8_t end; + enum { + REPEAT_NONE = 0, + REPEAT_START = 1, + REPEAT_REVERSE = 2, + } repeat; +}; + +union program_params_t +{ + /* parameters for static programs */ + uint8_t raw[PROGRAM_PARAMETER_SIZE]; + struct colorwheel_params_t colorwheel; + struct random_params_t random; + struct replay_params_t replay; +}; + +/* global process struct */ +struct process_t { + program_handler execute; + struct pt pt; + bool enable; + union program_params_t params; +}; + +#if CONFIG_SCRIPT + +/* global list of programs */ +#define STATIC_PROGRAMS_LEN 3 +extern program_handler static_programs[]; + +PT_THREAD(program_colorwheel(struct process_t *process)); +PT_THREAD(program_random(struct process_t *process)); +PT_THREAD(program_replay(struct process_t *process)); + +#endif +#endif diff --git a/firmware/fnordlicht-firmware/storage.c b/firmware/fnordlicht-firmware/storage.c new file mode 100644 index 0000000..637ad69 --- /dev/null +++ b/firmware/fnordlicht-firmware/storage.c @@ -0,0 +1,175 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "storage.h" +#include "remote.h" +#include "../common/pt/pt.h" +#include +#include +#include + +/* global structures */ +struct storage_config_t startup_config; +EEMEM struct storage_t eeprom_storage; + +/* internal state */ +struct storage_internal_t +{ + bool eeprom_good; + + /* buffer for nonblocking read/write */ + uint8_t buf[STORAGE_BUFFER_SIZE]; + uint8_t index; + uint8_t *address; + uint8_t bytes_remaining; + struct pt thread; + + enum { + STORAGE_IDLE = 0, + STORAGE_WRITE = 1, + } state; +}; + +static struct storage_internal_t storage; + +static uint16_t eeprom_checksum(void) +{ + uint16_t checksum = 0; + uint8_t *start = NULL; + + for (uint16_t i = 0; i < sizeof(struct storage_t)-2; i++) + checksum = _crc16_update(checksum, eeprom_read_byte(start++)); + + return checksum; +} + +void storage_init(void) +{ + /* compare checksum and eeprom configuration content */ + uint16_t checksum1 = eeprom_checksum(); + uint16_t checksum2 = eeprom_read_word(&eeprom_storage.checksum); + storage.eeprom_good = (checksum1 == checksum2); + storage_load_config(); + +#ifdef INIT_ZERO + /* initialize state structure */ + storage.state = STORAGE_IDLE; + PT_INIT(&storage.thread); +#endif +} + +static PT_THREAD(storage_thread(struct pt *thread)) +{ + PT_BEGIN(thread); + + while (1) { + if (storage.state == STORAGE_WRITE) { + /* pull int line */ + remote_pull_int(); + + /* write data */ + while (storage.bytes_remaining--) { + eeprom_write_byte(storage.address++, storage.buf[storage.index++]); + + while (!eeprom_is_ready()) + PT_YIELD(thread); + } + + /* update checksum */ + eeprom_write_word(&eeprom_storage.checksum, eeprom_checksum()); + while (!eeprom_is_ready()) + PT_YIELD(thread); + + /* release int line */ + remote_release_int(); + + storage.state = STORAGE_IDLE; + } + + PT_YIELD(thread); + } + + PT_END(thread); +} + +void storage_poll(void) +{ + /* call storage thread */ + if (storage.state != STORAGE_IDLE) + PT_SCHEDULE(storage_thread(&storage.thread)); +} + +void storage_save_config(void) +{ + if (storage.state != STORAGE_IDLE) + return; + + /* set magic byte and save startup_config to EEPROM */ + startup_config.magic = EEPROM_MAGIC_BYTE; + + /* copy data to buffer */ + memcpy(&storage.buf[0], &startup_config, sizeof(struct storage_config_t)); + /* set address and bytes_remaining */ + storage.index = 0; + storage.bytes_remaining = sizeof(struct storage_config_t); + storage.address = (uint8_t *)&eeprom_storage.config; + /* start write */ + storage.state = STORAGE_WRITE; + + /* reset magic config and mark EEPROM as good */ + startup_config.magic = 0; + storage.eeprom_good = true; +} + +void storage_load_config(void) +{ + if (!storage.eeprom_good) + return; + + /* load config */ + eeprom_read_block(&startup_config, &eeprom_storage.config, sizeof(struct storage_config_t)); +} + +void storage_save_color(uint8_t position, struct storage_color_t *color) +{ + if (storage.state != STORAGE_IDLE) + return; + + /* copy data to buffer */ + memcpy(&storage.buf[0], color, sizeof(struct storage_color_t)); + /* set address and bytes_remaining */ + storage.index = 0; + storage.bytes_remaining = sizeof(struct storage_color_t); + storage.address = (uint8_t *)&eeprom_storage.color[position]; + /* start write */ + storage.state = STORAGE_WRITE; +} + +void storage_load_color(uint8_t position, struct storage_color_t *color) +{ + eeprom_read_block(color, &eeprom_storage.color[position], sizeof(struct storage_color_t)); +} + +bool storage_valid_config(void) +{ + return (storage.eeprom_good && startup_config.magic == EEPROM_MAGIC_BYTE); +} diff --git a/firmware/fnordlicht-firmware/storage.h b/firmware/fnordlicht-firmware/storage.h new file mode 100644 index 0000000..09e293d --- /dev/null +++ b/firmware/fnordlicht-firmware/storage.h @@ -0,0 +1,91 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef __STORAGE_H +#define __STORAGE_H + +#include "color.h" +#include "remote-proto.h" +#include +#include +#include + +/* store a color configuration in EEPROM + * size: 8 byte + * + * use union color_t here, check rgb_marker! */ +struct storage_color_t +{ + uint8_t step; + uint8_t delay; + uint16_t pause; + union color_t color; +}; + +#define EEPROM_MAGIC_BYTE 0x23 + +/* store the startup configuration in EEPROM + * size: 13 byte */ +struct storage_config_t +{ + /* magic byte, must match EEPROM_MAGIC_BYTE to mark a valid configuration */ + uint8_t magic; + /* startup parameters, defined in remote_proto.h, size: 12 byte */ + struct startup_parameters_t params; +}; + +/* storage structure for EEPROM + * size: 494 byte + * (means: 18 byte left) */ +struct storage_t +{ + /* startup configuration, size: 12 byte */ + struct storage_config_t config; + + /* color storage, size: 60*8 == 480 byte */ + struct storage_color_t color[CONFIG_EEPROM_COLORS]; + + /* crc16 checksum over config and color[], size: 2 byte */ + uint16_t checksum; +}; + +/* global structures */ +extern struct storage_config_t startup_config; +extern EEMEM struct storage_t eeprom_storage; + +/* initialize storage */ +void storage_init(void); +/* poll storage */ +void storage_poll(void); + +/* save storage config cfg to eeprom */ +void storage_save_config(void); +/* load storage config cfg from eeprom */ +void storage_load_config(void); + +void storage_save_color(uint8_t position, struct storage_color_t *color); +void storage_load_color(uint8_t position, struct storage_color_t *color); + +/* return true if configuration has been loaded and is valid */ +bool storage_valid_config(void); + +#endif diff --git a/firmware/fnordlicht-firmware/timer.c b/firmware/fnordlicht-firmware/timer.c new file mode 100644 index 0000000..8bd5263 --- /dev/null +++ b/firmware/fnordlicht-firmware/timer.c @@ -0,0 +1,78 @@ +/* + * unzap firmware + * + * (c) by Alexander Neumann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +#include +#include +#include "../common/io.h" +#include "timer.h" + +static volatile uint8_t internal_counter; + +void timer_init(void) +{ +#if defined(__AVR_ATmega8__) + /* initialize timer2, CTC at 10ms, prescaler 1024 */ + OCR2 = F_CPU/1024/100; + TCCR2 = _BV(WGM21) | _BV(CS22) | _BV(CS21) | _BV(CS20); + TIMSK |= _BV(OCIE2); +#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) + /* initialize timer2, CTC at 10ms, prescaler 1024 */ + OCR2A = F_CPU/1024/100; + TCCR2A = _BV(WGM21); + TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); + TIMSK2 = _BV(OCIE2A); +#else +#error "unknown controller, unable to initialize timer2" +#endif +} + +void timer_set(timer_t *t, uint8_t timeout) +{ + t->current = internal_counter; + t->timeout = timeout; +} + +bool timer_expired(timer_t *t) +{ + if (t->timeout == 0) + return true; + + /* attention: this is not correct, if internal_counter is incremented by more than one + * between two calls of timer_expired()! */ + if (t->current != internal_counter) { + t->timeout--; + t->current = internal_counter; + } + + return false; +} + +/* timer interrupt function */ +#if defined(__AVR_ATmega8__) +ISR(TIMER2_COMP_vect, ISR_NOBLOCK) { + internal_counter++; +} +#elif defined(__AVR_ATmega48__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) +ISR(TIMER2_COMPA_vect, ISR_NOBLOCK) { + internal_counter++; +} +#endif diff --git a/firmware/fnordlicht-firmware/timer.h b/firmware/fnordlicht-firmware/timer.h new file mode 100644 index 0000000..4bd8d43 --- /dev/null +++ b/firmware/fnordlicht-firmware/timer.h @@ -0,0 +1,42 @@ +/* + * unzap firmware + * + * (c) by Alexander Neumann + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * For more information on the GPL, please go to: + * http://www.gnu.org/copyleft/gpl.html + */ + +/* small timer library, uses timer2 */ + +#ifndef __TIMER_H +#define __TIMER_H + +#include +#include + +/* structures */ +typedef struct { + uint8_t current; + uint8_t timeout; +} timer_t; + +/* functions */ +void timer_init(void); +void timer_set(timer_t *t, uint8_t timeout); +bool timer_expired(timer_t *t); + +#endif diff --git a/firmware/fnordlicht-firmware/uart.c b/firmware/fnordlicht-firmware/uart.c new file mode 100644 index 0000000..a004bcf --- /dev/null +++ b/firmware/fnordlicht-firmware/uart.c @@ -0,0 +1,114 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "globals.h" + +#if CONFIG_SERIAL + +#include "../common/io.h" +#include + +#include "globals.h" +#include "../common/common.h" +#include "pwm.h" +#include "fifo.h" +#include "uart.h" + +/* define uart mode (8N1) */ +#if defined(__AVR_ATmega8__) +/* in atmega8, we need a special switching bit + * for addressing UCSRC */ +#define UART_UCSRC _BV(URSEL) | _BV(UCSZ0) | _BV(UCSZ1) + +#elif defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) +/* in atmega88, this isn't needed any more */ +#define UART_UCSRC _BV(_UCSZ0_UART0) | _BV(_UCSZ1_UART0) +#endif + +/* global variables */ +volatile struct global_uart_t global_uart; + +/** output one character */ +void uart_putc(uint8_t data) +{ + /* store data */ + fifo_enqueue((fifo_t *)&global_uart.tx, data); + + /* enable interrupt */ + _UCSRB_UART0 |= _BV(_UDRIE_UART0); +} + +/** init the hardware uart */ +void uart_init(void) +{ + #define BAUD CONFIG_SERIAL_BAUDRATE + #include + + /* set baud rate */ + _UBRRH_UART0 = UBRRH_VALUE; + _UBRRL_UART0 = UBRRL_VALUE; + + #if USE_2X + _UCSRA_UART0 |= (1 << _U2X_UART0); + #endif + + /* set mode */ + _UCSRC_UART0 = UART_UCSRC; + + /* enable transmitter, receiver and receiver complete interrupt */ + _UCSRB_UART0 = _BV(_TXEN_UART0) | _BV(_RXEN_UART0) | _BV(_RXCIE_UART0); + + /* init fifos */ + fifo_init((fifo_t *)&global_uart.rx); + fifo_init((fifo_t *)&global_uart.tx); +} + + +/** interrupts*/ + +/** uart receive interrupt */ +ISR(_SIG_UART_RECV_UART0) +{ + + /* store received data */ + fifo_enqueue((fifo_t *)&global_uart.rx, _UDR_UART0); + +} + +/** uart data register empty interrupt */ +ISR(_SIG_UART_DATA_UART0) +{ + + /* load next byte to transfer */ + _UDR_UART0 = fifo_dequeue((fifo_t *)&global_uart.tx); + + /* check if this interrupt is still needed */ + if ( fifo_fill((fifo_t *)&global_uart.tx) == 0) { + /* disable this interrupt */ + _UCSRB_UART0 &= ~_BV(_UDRIE_UART0); + } + +} + + +#endif diff --git a/firmware/fnordlicht-firmware/uart.h b/firmware/fnordlicht-firmware/uart.h new file mode 100644 index 0000000..ceeb4f0 --- /dev/null +++ b/firmware/fnordlicht-firmware/uart.h @@ -0,0 +1,58 @@ +/* vim:ts=4 sts=4 et tw=80 + * + * fnordlicht firmware + * + * for additional information please + * see http://lochraster.org/fnordlichtmini + * + * (c) by Alexander Neumann + * Lars Noschinski + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3 as published by + * the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef UART_H +#define UART_H + +#include "globals.h" +#include "fifo.h" +#include "../common/io.h" + +#if !CONFIG_SERIAL + +#define uart_init(...) +#define uart_putc(...) + +#else + +/* structs */ +struct global_uart_t { + fifo_t rx; + fifo_t tx; +}; + +/* global variables */ +extern volatile struct global_uart_t global_uart; + +/* prototypes */ +void uart_init(void); +void uart_putc(uint8_t data); + +static inline bool uart_send_complete(void) +{ + return _BV(_UDRE_UART0) & _UCSRA_UART0; +} + +#endif + +#endif diff --git a/firmware/fuses.txt b/firmware/fuses.txt new file mode 100644 index 0000000..e5c0414 --- /dev/null +++ b/firmware/fuses.txt @@ -0,0 +1,125 @@ +fnordlicht original hardware +=========================================================================== + +atmega8: + low fuse byte: + clock-source external xtal: CHKSEL = 0 + + -> default: 0xe1 + new: 0b11100000 = 0xe0 + + high fuse byte (with bootloader, leave at 0xd9 for use without bootloader): + 512 words bootloader size: BOOTSZ0 = 1 + BOOTSZ1 = 0 + + reset vector, jump to bootloader on reset: BOOTRST = 0 + + -> default: 0b11011001 = 0xd9 + new: 0b11011010 = 0xda + + lock byte: + + SPM is not allowed to write to the Boot Loader section BLB12 = 1 + BLB11 = 0 + -> default: 0x3f + new: 0x2f + + +atmega88/168: + low fuse byte: + clock-source external xtal: CHKSEL = 0 + + disable system clock divide by 8: CKDIV8 = 1 + + -> default: 0b01100010 = 0x62 + new: 0b11100000 = 0xe0 + + + high fuse byte: + no eeprom erase on chip erase: EESAVE = 0 + + -> default: 0b11011111 = 0xdf + new: 0b11010111 = 0xd7 + + + extended fuse byte: + 1024 words bootloader size: BOOTSZ0 = 0 + BOOTSZ1 = 0 + + reset vector, jump to bootloader on reset: BOOTRST = 0 + + -> default: 0x01 + new: 0x00 + + lock byte: + SPM is not allowed to write to the Boot Loader section BLB12 = 1 + BLB11 = 0 + + -> default: 0b111111 = 0x3f + new: 0b101111 = 0x2f + + +fnordlichtmini hardware +=========================================================================== + +atmega8: + low fuse byte: + clock-source external xtal: CHKSEL[1..3] = 111 + slowly rising power, 65ms startup timeout: CHKSEL[0] = 1, SUT[0..1] = 11 + enable brown-out detector: BODEN = 0 + require ~4v for safe operation: BODLEVEL = 0 + + -> default: 0xe1 + new: 0b00111111 = 0x3f + + high fuse byte (with bootloader, leave at 0xd9 for use without bootloader): + 512 words bootloader size: BOOTSZ0 = 1 + BOOTSZ1 = 0 + + reset vector, jump to bootloader on reset: BOOTRST = 0 + + -> default: 0b11011001 = 0xd9 + new: 0b11011010 = 0xda + + lock byte: + + SPM is not allowed to write to the Boot Loader section BLB12 = 1 + BLB11 = 0 + -> default: 0x3f + new: 0x2f + + +fnordlicht-controller +========================================================================== + +atmega168: + low fuse byte: + clock-source external xtal: CHKSEL = 0111 + + disable system clock divide by 8: CKDIV8 = 1 + + -> default: 0b01100010 = 0x62 + new: 0b11100111 = 0xe7 + + + high fuse byte: + + -> default: 0b11011111 = 0xdf + + + extended fuse byte: + 1024 words bootloader size: BOOTSZ0 = 0 + BOOTSZ1 = 0 + + reset vector, jump to bootloader on reset: BOOTRST = 0 + + -> default: 0b001 = 0x01 + new: 0b000 = 0x00 + + + lock byte: + SPM is not allowed to write to the Boot Loader section BLB12 = 1 + BLB11 = 0 + + -> default: 0b111111 = 0x3f + new: 0b101111 = 0x2f diff --git a/firmware/tests/change_current.rb b/firmware/tests/change_current.rb new file mode 100755 index 0000000..8aeaccd --- /dev/null +++ b/firmware/tests/change_current.rb @@ -0,0 +1,50 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) + +puts "fade to hsv color" +fade_hsv(255, 200, 100, 100, 2, 1) +sleep 2 + +puts "change current color - rgb" +modify_current(255, 1, 1, 50, 0, 0, 0, 0, 0) +sleep 2 +modify_current(255, 1, 1, 50, 0, 0, 0, 0, 0) +sleep 2 +modify_current(255, 1, 1, 50, 0, 0, 0, 0, 0) +sleep 2 +modify_current(255, 1, 1, -80, 0, 0, 0, 0, 0) +sleep 2 +modify_current(255, 1, 1, -100, 100, 127, 0, 0, 0) +sleep 2 + +puts "change current color - hsv" +fade_hsv(255, 60, 220, 120, 5, 1) +sleep 2 + +modify_current(255, 2, 1, 0, 0, 0, 235, 0, 0) +sleep 2 +modify_current(255, 2, 1, 0, 0, 0, 235, 0, 0) +sleep 2 +modify_current(255, 2, 1, 0, 0, 0, 235, 0, 0) +sleep 2 +modify_current(255, 2, 1, 0, 0, 0, 235, 0, 0) +sleep 2 +0.upto 2 do + modify_current(255, 5, 1, 0, 0, 0, 0, 0, -80) + sleep 1 + modify_current(255, 5, 1, 0, 0, 0, 0, 0, 80) + sleep 1 +end diff --git a/firmware/tests/config_startup_nothing.rb b/firmware/tests/config_startup_nothing.rb new file mode 100755 index 0000000..9718377 --- /dev/null +++ b/firmware/tests/config_startup_nothing.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +puts "configure startup: do nothing" +config_startup_nothing(255) diff --git a/firmware/tests/config_startup_program_colorwheel.rb b/firmware/tests/config_startup_program_colorwheel.rb new file mode 100755 index 0000000..cf09127 --- /dev/null +++ b/firmware/tests/config_startup_program_colorwheel.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +puts "configure startup: program colorwheel (fast, start with yellow)" +config_startup_program(255, 0, [8, 1, 10, 60, 0, 60, 0, 0, 255, 255]) diff --git a/firmware/tests/config_startup_program_random.rb b/firmware/tests/config_startup_program_random.rb new file mode 100755 index 0000000..1134224 --- /dev/null +++ b/firmware/tests/config_startup_program_random.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +puts "configure startup: program random (fast, no min_distance)" +config_startup_program(255, 1, [Time.now.usec & 0xff, Time.now.usec & 0xff, 1, 8, 1, 10, 0, 255, 255, 0]) diff --git a/firmware/tests/config_startup_program_replay.rb b/firmware/tests/config_startup_program_replay.rb new file mode 100755 index 0000000..a9cfedf --- /dev/null +++ b/firmware/tests/config_startup_program_replay.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +puts "configure startup: program replay 3-5 (repeat reverse)" +config_startup_program(255, 2, [3, 5, 2]) diff --git a/firmware/tests/enter_application.rb b/firmware/tests/enter_application.rb new file mode 100755 index 0000000..892ef21 --- /dev/null +++ b/firmware/tests/enter_application.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +if ARGV.length != 1 + $stderr.puts "USAGE: #{$0} " + exit 1 +end + +puts "sending sync sequence" +sync() + +puts "starting application" +boot_enter_application(ARGV.shift.to_i) diff --git a/firmware/tests/fade_hsv.rb b/firmware/tests/fade_hsv.rb new file mode 100755 index 0000000..61ae7a7 --- /dev/null +++ b/firmware/tests/fade_hsv.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) + +puts "fade to hsv color" +fade_hsv(255, 0, 255, 255, 2, 1) +sleep 2 +fade_hsv(255, 120, 255, 255, 2, 1) +sleep 2 +fade_hsv(255, 180, 255, 255, 2, 1) +sleep 2 +fade_hsv(255, 240, 255, 255, 2, 1) +sleep 2 + +fade_hsv(255, 200, 255, 150, 2, 1) +sleep 2 +fade_hsv(255, 200, 255, 20, 2, 1) +sleep 2 +fade_hsv(255, 200, 255, 100, 2, 1) +sleep 2 +fade_hsv(255, 200, 30, 100, 2, 1) +sleep 2 +fade_hsv(255, 200, 100, 100, 2, 1) +sleep 2 + diff --git a/firmware/tests/fade_rgb.rb b/firmware/tests/fade_rgb.rb new file mode 100755 index 0000000..e65d113 --- /dev/null +++ b/firmware/tests/fade_rgb.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) + +puts "fade to rgb color" +fade_rgb(255, 0, 0, 0, 255, 1) +fade_rgb(255, 255, 0, 0, 2, 1) +sleep 2 +fade_rgb(255, 0, 255, 0, 2, 1) +sleep 2 +fade_rgb(255, 0, 0, 255, 2, 1) diff --git a/firmware/tests/flash_file.rb b/firmware/tests/flash_file.rb new file mode 100755 index 0000000..e48acc3 --- /dev/null +++ b/firmware/tests/flash_file.rb @@ -0,0 +1,84 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +def crc16_update(crc, data) + return nil if crc < 0 || crc > 0xffff + return nil if data < 0 || data > 255 + + crc ^= data + 0.upto(7) do |i| + if crc % 2 == 1 + crc = (crc >> 1) ^ 0xA001 + else + crc = (crc >> 1) + end + end + + return crc +end + + +def compute_checksum(data) + checksum = 0xffff + + data.each_byte do |b| + checksum = crc16_update(checksum, b) + end + + return checksum +end + +if ARGV.length != 2 + $stderr.puts "usage: %s ADDRESS FILE" % $0 + exit 1 +end + +address = ARGV.shift.to_i +file = ARGV.shift + +puts "sending sync sequence" +sync() + +puts "flash" +open(file, 'r') do |f| + puts "configure bootloader" + boot_config(address, 0) + loop do + d = f.read(512) + break if d.nil? + puts "writing chunk (%s bytes)" % d.length + + boot_init(address); + d.split('').each_slice(13) do |s| + boot_data(address, s.join('')) + #sleep 0.05 + end + checksum = compute_checksum(d) + boot_crc_check(address, d.length, checksum, 5); + + puts "flashing..." + boot_flash(address) + sleep 0.3 + + break if d.length < 512 + end +end + +data = "" +open(file, 'r') do |f| + d = f.read() + break if d.nil? + data += d +end +checksum = compute_checksum(data) +puts "verifying checksum (%u bytes): 0x%04x" % [data.length, checksum] +boot_crc_flash(address, 0, data.length, checksum, 50) +puts "done" diff --git a/firmware/tests/lib/fnordlicht.rb b/firmware/tests/lib/fnordlicht.rb new file mode 100644 index 0000000..5a67c48 --- /dev/null +++ b/firmware/tests/lib/fnordlicht.rb @@ -0,0 +1,263 @@ +module Fnordlicht + def sync(addr = 0) + 1.upto(15) do + $dev.write "\e" + end + $dev.write addr.chr + $dev.flush + end + + def fade_rgb(addr, r, g, b, step, delay) + $dev.write addr.chr + $dev.write "\x01" + $dev.write step.chr + $dev.write delay.chr + $dev.write r.chr + $dev.write g.chr + $dev.write b.chr + $dev.write "\x00\x00\x00\x00\x00" + $dev.write "\x00\x00\x00" + $dev.flush + end + + def fade_hsv(addr, h, s, v, step, delay) + $dev.write addr.chr + $dev.write "\x02" + $dev.write step.chr + $dev.write delay.chr + $dev.write [h].pack('v') + $dev.write s.chr + $dev.write v.chr + $dev.write "\x00\x00\x00\x00\x00" + $dev.write "\x00\x00" + $dev.flush + end + + def save_rgb(addr, slot, r, g, b, step, delay, pause) + $dev.write addr.chr + $dev.write "\x03" + $dev.write slot.chr + $dev.write step.chr + $dev.write delay.chr + $dev.write [pause].pack('S') + $dev.write r.chr + $dev.write g.chr + $dev.write b.chr + $dev.write "\x00\x00\x00\x00\x00" + $dev.flush + end + + def save_hsv(addr, slot, h, s, v, step, delay, pause) + $dev.write addr.chr + $dev.write "\x04" + $dev.write slot.chr + $dev.write step.chr + $dev.write delay.chr + $dev.write [pause].pack('S') + $dev.write [h].pack('v') + $dev.write s.chr + $dev.write v.chr + $dev.write "\x00\x00\x00\x00" + $dev.flush + end + + def save_current(addr, slot, step, delay, pause) + $dev.write addr.chr + $dev.write "\x05" + $dev.write slot.chr + $dev.write step.chr + $dev.write delay.chr + $dev.write [pause].pack('S') + $dev.write "\x00\x00\x00\x00" + $dev.write "\x00\x00\x00\x00" + $dev.flush + end + + def config_offsets(addr, step, delay, h, s, v) + $dev.write addr.chr + $dev.write "\x06" + $dev.write [step].pack('c') + $dev.write [delay].pack('c') + $dev.write [h].pack('v') + $dev.write s.chr + $dev.write v.chr + $dev.write "\x00\x00\x00\x00\x00\x00\x00" + $dev.flush + end + + def start_program(addr, program, params) + $dev.write addr.chr + $dev.write "\x07" + $dev.write program.chr + rest = 12-params.length + puts "rest: %u" % rest if $verbose + params.each do |p| + $dev.write(p.chr) + end + 1.upto(rest) do + $dev.write("\x00") + end + $dev.flush + end + + def stop(addr, fading = 1) + $dev.write addr.chr + $dev.write "\x08" + $dev.write fading.chr + $dev.write "\x00\x00\x00\x00" + $dev.write "\x00\x00\x00\x00\x00" + $dev.write "\x00\x00\x00" + $dev.flush + end + + def modify_current(addr, step, delay, r, g, b, h, s, v) + $dev.write addr.chr + $dev.write "\x09" + $dev.write step.chr + $dev.write delay.chr + $dev.write [r].pack('c') + $dev.write [g].pack('c') + $dev.write [b].pack('c') + $dev.write [h].pack('v') + $dev.write [s].pack('c') + $dev.write [v].pack('c') + $dev.write "\x00\x00\x00\x00" + $dev.flush + end + + def pull_int(addr, delay) + $dev.write addr.chr + $dev.write "\x0A" + $dev.write delay.chr + + $dev.write "\x00\x00" + $dev.write "\x00\x00\x00\x00\x00" + $dev.write "\x00\x00\x00\x00\x00" + $dev.flush + end + + def config_startup_nothing(addr) + mode = 0 # do nothing + + $dev.write addr.chr + $dev.write "\x0B" + $dev.write mode.chr + + $dev.write "\x00\x00" + $dev.write "\x00\x00\x00\x00\x00" + $dev.write "\x00\x00\x00\x00\x00" + $dev.flush + end + + def config_startup_program(addr, program, params) + mode = 1 # start program + + $dev.write addr.chr + $dev.write "\x0B" + $dev.write mode.chr + $dev.write program.chr + + rest = 11-params.length + puts "rest: %u" % rest if $verbose + params.each do |p| + $dev.write(p.chr) + end + 1.upto(rest) do + $dev.write("\x00") + end + $dev.flush + end + + def powerdown(addr) + $dev.write(addr.chr) + $dev.write("\x0C") + $dev.write("\x00"*13) + $dev.flush + end + + # secondary functions + def fade_updown_rgb(addr, r, g, b, step, delay, sleep_time) + fade_rgb(addr, r, g, b, step, delay) + sleep(sleep_time) + fade_rgb(addr, 0, 0, 0, step, delay) + sleep(sleep_time) + end + + def fade_updown_hsv(addr, h, s, v, step, delay, sleep_time) + fade_hsv(addr, h, s, v, step, delay) + sleep(sleep_time) + fade_hsv(addr, h, s, 0, step, delay) + sleep(sleep_time) + end + + # bootloader functions + def start_bootloader(addr) + $dev.write(addr.chr) + $dev.write("\x80") + $dev.write("\x6b\x56\x27\xfc") + $dev.write("\x00\x00\x00\x00\x00\x00\x00\x00\x00") + $dev.flush + end + + def boot_config(addr, start_addr) + $dev.write(addr.chr) + $dev.write("\x81") + $dev.write [start_addr].pack('v') + $dev.write("\x00"*11) + $dev.flush + end + + def boot_init(addr) + $dev.write(addr.chr) + $dev.write("\x82") + $dev.write("\xff" * 13) + $dev.flush + end + + def boot_data(addr, data) + $dev.write(addr.chr) + $dev.write("\x83") + + # just write the first 13 bytes + data = data[0..12] + $dev.write(data) + + $dev.write("\xff" * (13-data.length)) + $dev.flush + end + + def boot_crc_check(addr, len, checksum, delay) + $dev.write(addr.chr) + $dev.write("\x84") + $dev.write [len].pack('v') + $dev.write [checksum].pack('v') + $dev.write delay.chr + $dev.write("\x00"*8) + $dev.flush + end + + def boot_crc_flash(addr, start, len, checksum, delay) + $dev.write(addr.chr) + $dev.write("\x85") + $dev.write [start].pack('v') + $dev.write [len].pack('v') + $dev.write [checksum].pack('v') + $dev.write delay.chr + $dev.write("\x00"*6) + $dev.flush + end + + def boot_flash(addr) + $dev.write(addr.chr) + $dev.write("\x86") + $dev.write("\x00"*13) + $dev.flush + end + + def boot_enter_application(addr) + $dev.write(addr.chr) + $dev.write("\x87") + $dev.write("\x00"*13) + $dev.flush + end +end diff --git a/firmware/tests/offset_delay.rb b/firmware/tests/offset_delay.rb new file mode 100755 index 0000000..92bb0a0 --- /dev/null +++ b/firmware/tests/offset_delay.rb @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) +fade_rgb(255, 0, 0, 0, 255, 0) +puts "reset offsets " +config_offsets(255, 0, 0, 0, 255, 255) + +puts "fade slow rgb (big steps)" +fade_updown_rgb(255, 255, 255, 0, 10, 10, 3) + +puts "fade slow hsv (big steps)" +fade_updown_hsv(255, 120, 255, 255, 10, 10, 3) + +puts "configuring delay offset" +config_offsets(255, 0, -5, 0, 255, 255) + +puts "fade slow rgb (big steps, but modified to fast)" +fade_updown_rgb(255, 255, 255, 0, 10, 10, 1) + +puts "fade slow hsv (big steps, but modified to fast)" +fade_updown_hsv(255, 120, 255, 255, 10, 10, 1) diff --git a/firmware/tests/offset_hsv.rb b/firmware/tests/offset_hsv.rb new file mode 100755 index 0000000..ec41712 --- /dev/null +++ b/firmware/tests/offset_hsv.rb @@ -0,0 +1,58 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) +fade_rgb(255, 0, 0, 0, 255, 0) +puts "reset offsets " +config_offsets(255, 0, 0, 0, 255, 255) + +puts "fade to blue" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + +puts "configuring saturation scale" +config_offsets(255, 10, 0, 0, 100, 255) + +puts "fade to blue (modified to light blue)" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + +puts "reset offsets " +config_offsets(255, 0, 0, 0, 255, 255) + +puts "fade to blue" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + +puts "configuring value scale" +config_offsets(255, 10, 0, 0, 255, 100) + +puts "fade to blue (modified to blue, low intensity)" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + +puts "reset offsets " +config_offsets(255, 0, 0, 0, 255, 255) + +puts "fade to blue" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + +puts "configuring hue offset" +config_offsets(255, 10, 0, 80, 255, 255) + +puts "fade to blue (modified to pink)" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + +puts "configuring negative hue offset" +config_offsets(255, 10, 0, -80, 255, 255) + +puts "fade to blue (modified to light green)" +fade_updown_hsv(255, 240, 255, 255, 10, 1, 1) + diff --git a/firmware/tests/offset_step.rb b/firmware/tests/offset_step.rb new file mode 100755 index 0000000..b8cceb3 --- /dev/null +++ b/firmware/tests/offset_step.rb @@ -0,0 +1,52 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) +fade_rgb(255, 0, 0, 0, 255, 0) +puts "reset offsets " +config_offsets(255, 0, 0, 0, 255, 255) + +puts "fade slow rgb" +fade_updown_rgb(255, 255, 255, 0, 1, 1, 3) + +puts "fade slow hsv" +fade_updown_hsv(255, 120, 255, 255, 1, 1, 3) + +puts "configuring positive step offset" +config_offsets(255, 10, 0, 0, 255, 255) + +puts "fade slow rgb (but modified to fast)" +fade_updown_rgb(255, 255, 255, 0, 1, 1, 1) + +puts "fade slow hsv (but modified to fast)" +fade_updown_hsv(255, 120, 255, 255, 1, 1, 1) + +puts "reset offsets " +config_offsets(255, 0, 0, 0, 255, 255) + +puts "fade fast rgb" +fade_updown_rgb(255, 255, 255, 0, 10, 1, 1) + +puts "fade fast hsv" +fade_updown_hsv(255, 120, 255, 255, 10, 1, 1) + +puts "configuring negative step offset" +config_offsets(255, -10, 0, 0, 255, 255) + +puts "fade fast rgb (but modified to slow)" +fade_updown_rgb(255, 255, 255, 0, 10, 1, 3) + +puts "fade fast hsv (but modified to slow)" +fade_updown_hsv(255, 120, 255, 255, 10, 1, 3) + diff --git a/firmware/tests/powerdown.rb b/firmware/tests/powerdown.rb new file mode 100755 index 0000000..dbd58dd --- /dev/null +++ b/firmware/tests/powerdown.rb @@ -0,0 +1,23 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +if ARGV.length != 1 + $stderr.puts "USAGE: #{$0} " + exit 1 +end + +addr = ARGV.shift.to_i + +puts "sending sync sequence" +sync() + +puts "powerdown" +powerdown(addr) diff --git a/firmware/tests/pull_int.rb b/firmware/tests/pull_int.rb new file mode 100755 index 0000000..fade01c --- /dev/null +++ b/firmware/tests/pull_int.rb @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "pull int 100ms" +pull_int(255, 2) diff --git a/firmware/tests/reset.rb b/firmware/tests/reset.rb new file mode 100755 index 0000000..353b77c --- /dev/null +++ b/firmware/tests/reset.rb @@ -0,0 +1,30 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +if ARGV.length != 1 + $stderr.puts "USAGE: #{$0} " + exit 1 +end + +addr = ARGV.shift.to_i + +puts "sending sync sequence" +sync() + +puts "starting bootloader" +start_bootloader(addr) + +sleep 0.2 +puts "sending sync sequence" +sync() + +puts "starting firmware" +boot_enter_application(addr) diff --git a/firmware/tests/save_current.rb b/firmware/tests/save_current.rb new file mode 100755 index 0000000..c6a89b4 --- /dev/null +++ b/firmware/tests/save_current.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +stop(255) + +puts "starting slow fade from red to green" +fade_rgb(255, 255, 0, 0, 255, 1) +sleep 0.1 + +puts "save start color" +save_current(255, 0, 8, 1, 10) + +fade_rgb(255, 0, 255, 0, 1, 2) + +sleep 1.8 +puts "save in the middle" +save_current(255, 1, 8, 1, 10) + +sleep 3 +puts "save at the end" +save_current(255, 2, 8, 1, 10) + +sleep 0.1 + +puts "start program replay, 0-2, repeat from start" +start_program(255, 2, [0, 2, 1]) diff --git a/firmware/tests/save_hsv.rb b/firmware/tests/save_hsv.rb new file mode 100755 index 0000000..60f27b5 --- /dev/null +++ b/firmware/tests/save_hsv.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +puts "save 11 random hue values (hsv) in eeprom" +0.upto 10 do |i| + hue = rand(360) + puts "hue: %u" % hue + save_hsv(255, i, hue, 255, 255, 10, 1, 10); + sleep 0.1 +end diff --git a/firmware/tests/save_rgb.rb b/firmware/tests/save_rgb.rb new file mode 100755 index 0000000..838c180 --- /dev/null +++ b/firmware/tests/save_rgb.rb @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() + +puts "save rgb: light orange (fast fading)" +save_rgb(255, 0, 255, 150, 0, 10, 1, 10); diff --git a/firmware/tests/start_bootloader.rb b/firmware/tests/start_bootloader.rb new file mode 100755 index 0000000..5553082 --- /dev/null +++ b/firmware/tests/start_bootloader.rb @@ -0,0 +1,21 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +if ARGV.length != 1 + $stderr.puts "USAGE: #{$0} " + exit 1 +end + +puts "sending sync sequence" +sync() + +puts "starting bootloader" +start_bootloader(ARGV.shift.to_i) diff --git a/firmware/tests/start_program_colorwheel.rb b/firmware/tests/start_program_colorwheel.rb new file mode 100755 index 0000000..fa6f35b --- /dev/null +++ b/firmware/tests/start_program_colorwheel.rb @@ -0,0 +1,22 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) + +puts "start program colorwheel (forward)" +start_program(255, 0, [5, 1, 1, 0, 0, 60, 0, 255, 255, 255]) +sleep 10 + +puts "start program colorwheel (reverse)" +start_program(255, 0, [5, 1, 1, 0, 0, 60, 0, 1, 255, 255]) diff --git a/firmware/tests/start_program_random.rb b/firmware/tests/start_program_random.rb new file mode 100755 index 0000000..b4d8cb8 --- /dev/null +++ b/firmware/tests/start_program_random.rb @@ -0,0 +1,18 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) + +puts "start program random" +start_program(255, 1, [Time.now.usec & 0xff, Time.now.usec & 0xff, 1, 5, 1, 10, 0, 255, 255, 40]) diff --git a/firmware/tests/start_program_replay.rb b/firmware/tests/start_program_replay.rb new file mode 100755 index 0000000..21255a1 --- /dev/null +++ b/firmware/tests/start_program_replay.rb @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255) + +puts "save 11 random hue values (hsv) in eeprom" +0.upto 10 do |i| + hue = rand(360) + puts "hue: %u" % hue + save_hsv(255, i, hue, 255, 255, 10, 1, 10); + sleep 0.1 +end + +puts "fade to blue" +fade_hsv(255, 240, 255, 255, 10, 1) + +sleep 2 + +puts "start program replay, 3-7 no repeat" +start_program(255, 2, [3, 7, 0]) + +sleep 10 + +puts "start program replay, 5-7 repeat from start" +start_program(255, 2, [5, 7, 1]) + +sleep 20 + +puts "start program replay, 5-7 repeat inverse" +start_program(255, 2, [5, 7, 2]) diff --git a/firmware/tests/stop.rb b/firmware/tests/stop.rb new file mode 100755 index 0000000..62d91ad --- /dev/null +++ b/firmware/tests/stop.rb @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby + +$:.unshift(File.dirname(__FILE__)+"/lib") + +require 'fnordlicht' +require 'serialport' + +include Fnordlicht + +$dev = SerialPort.new("/dev/ttyUSB0", 19200) + +puts "sending sync sequence" +sync() +puts "stop fading" +stop(255)