├── .gitignore ├── LICENSE ├── README.md ├── altmode_friend.step ├── captures ├── dec.txt ├── pinecil.txt ├── pinecil_1.pvs ├── pinecil_1.sr ├── pinecil_commented.txt └── trace.py ├── fusb302.py ├── machine.py ├── pdstacc.py ├── simplest_sink_example.py ├── sink_example.py ├── sniffer.py └── source_example.py /.gitignore: -------------------------------------------------------------------------------- 1 | pics/ 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Altmode Friend 2 | 3 | Find example code and 3D STEP in this repo. 4 | 5 | Code examples: 6 | 7 | - `simplest_sink_example.py`: A sink example code, that just picks 5V by default. Currently not tested after refactoring. It also might summon DisplayPort out of a DP source, but that's just an accident. 8 | - `sink_example.py`: A sink example code, with two different strategies for picking a PD profile. Currently not tested after refactoring. It also can summon DisplayPort out of a DP source - but the PoC is not fully tested yet. 9 | - `source_example`: Firmware for a bespoke board that uses the stack to create a PD PSU. Currently not tested after refactoring. 10 | - `sniffer.py`: PD sniffer code. Change `replay` to False to run it and adjust the CC pin. Live capture not currently tested after refactoring. 11 | 12 | Required libraries: 13 | 14 | - `fusb302.py`: FUSB302 low-level code 15 | - `pdstacc.py`: PD stack code 16 | 17 | [Find PCB sources here.](https://github.com/CRImier/MyKiCad/tree/master/Peripherals/altmode_friend) 18 | 19 | `machine.py` file is a mock you can use to run and test parts of this code on your compooter - in particular, the replay mode of `sniffer.py`. 20 | It is distributed under some other license, because it's been taken [from here](https://github.com/djantzen/pico_book/blob/master/machine.py) 21 | -------------------------------------------------------------------------------- /captures/pinecil.txt: -------------------------------------------------------------------------------- 1 | ----------------------- 2 | 0x22 0x01 ( DEV_ID) rd 0x91 (0b10010001) 3 | 0x22 0x0c ( RESET) wr 0x01 (0b00000001) 4 | 0x22 0x01 ( DEV_ID) rd 0x91 (0b10010001) 5 | 0x22 0x0b ( POWER) wr 0x0f (0b00001111) 6 | 0x22 0x0a ( MASK) wr 0x00 (0b00000000) 7 | 0x22 0x0e ( MASKA) wr 0x00 (0b00000000) 8 | 0x22 0x0f ( MASKB) wr 0x00 (0b00000000) 9 | 0x22 0x06 ( CTRL0) wr 0x0c (0b00001100) 10 | 0x22 0x09 ( CTRL3) wr 0x07 (0b00000111) 11 | 0x22 0x08 ( CTRL2) wr 0x00 (0b00000000) 12 | 0x22 0x07 ( CTRL1) wr 0x04 (0b00000100) 13 | 0x22 0x02 ( SWITCH0) wr 0x07 (0b00000111) 14 | 0x22 0x40 ( STATUS0) rd 0x03 (0b00000011) 15 | 0x22 0x02 ( SWITCH0) wr 0x0b (0b00001011) 16 | 0x22 0x40 ( STATUS0) rd 0x00 (0b00000000) 17 | 0x22 0x03 ( SWITCH1) wr 0x25 (0b00100101) 18 | 0x22 0x02 ( SWITCH0) wr 0x07 (0b00000111) 19 | 0x22 0x06 ( CTRL0) wr 0x44 (0b01000100) 20 | 0x22 0x07 ( CTRL1) wr 0x04 (0b00000100) 21 | 0x22 0x0c ( RESET) wr 0x02 (0b00000010) 22 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x28 0x41 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00101000 0b01000001) 23 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x41 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000001 0b00001000 0b00000001) 24 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 25 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 26 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 27 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x13 0x08 0x51 (0b00001100 0b00000001 0b00000000 0b00000000 0b00010011 0b00001000 0b01010001) 28 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b00000001) 29 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x51 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010001 0b00001000 0b01000001) 30 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x01 0x13 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000001 0b00010011 0b00001000 0b00000001) 31 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 32 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 33 | 0x22 0x43 ( FIFO) rd 0xa1 0x61 (0b10100001 0b01100001) 34 | 0x22 0x43 ( FIFO) rd 0x2c 0x91 0x01 0x08 0x2c 0xd1 0x02 0x00 0x13 0xc1 0x03 0x00 0xdc 0xb0 0x04 0x00 0xa5 0x40 0x06 0x00 0x3c 0x21 0xdc 0xc0 (0b00101100 0b10010001 0b00000001 0b00001000 0b00101100 0b11010001 0b00000010 0b00000000 0b00010011 0b11000001 0b00000011 0b00000000 0b11011100 0b10110000 0b00000100 0b00000000 0b10100101 0b01000000 0b00000110 0b00000000 0b00111100 0b00100001 0b11011100 0b11000000) 35 | 0x22 0x43 ( FIFO) rd 0x48 0xc6 0xe7 0xc6 (0b01001000 0b11000110 0b11100111 0b11000110) 36 | 0x22 0x41 ( STATUS1) rd 0x28 (0b00101000) 37 | 0x22 0x43 ( FIFO) wr 0x12 0x12 0x12 0x13 0x86 (0b00010010 0b00010010 0b00010010 0b00010011 0b10000110) 38 | 0x22 0x43 ( FIFO) wr 0x82 0x10 0xdc 0x70 0x03 0x41 (0b10000010 0b00010000 0b11011100 0b01110000 0b00000011 0b01000001) 39 | 0x22 0x43 ( FIFO) wr 0xff 0x14 0xfe 0xa1 (0b11111111 0b00010100 0b11111110 0b10100001) 40 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x20 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00100000 0b01000001) 41 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x51 0x20 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010001 0b00100000 0b00000001) 42 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x13 0x28 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b00010011 0b00101000 0b01000001) 43 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x28 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00101000 0b01000001) 44 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 45 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x04 0x00 0x13 0x08 0x51 (0b00001100 0b00000001 0b00000100 0b00000000 0b00010011 0b00001000 0b01010001) 46 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x13 0x08 0x00 (0b00001100 0b00000001 0b00000000 0b00000000 0b00010011 0b00001000 0b00000000) 47 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b01000001) 48 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x41 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01000001 0b00001000 0b00000001) 49 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x51 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b01010001) 50 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x51 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010001 0b00001000 0b00000001) 51 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x01 0x13 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000001 0b00010011 0b00001000 0b01000001) 52 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 53 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 54 | 0x22 0x43 ( FIFO) rd 0x61 0x01 (0b01100001 0b00000001) 55 | 0x22 0x43 ( FIFO) rd 0x8f 0x78 0x38 0x4a (0b10001111 0b01111000 0b00111000 0b01001010) 56 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 57 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 58 | 0x22 0x43 ( FIFO) rd 0xa3 0x03 (0b10100011 0b00000011) 59 | 0x22 0x43 ( FIFO) rd 0x6f 0xac 0xfa 0x5d (0b01101111 0b10101100 0b11111010 0b01011101) 60 | 0x22 0x41 ( STATUS1) rd 0x28 (0b00101000) 61 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x28 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00101000 0b01000001) 62 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x41 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01000001 0b00001000 0b00000001) 63 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x51 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b01010001) 64 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b00000001) 65 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x01 0x13 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000001 0b00010011 0b00001000 0b01000001) 66 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 67 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 68 | 0x22 0x43 ( FIFO) rd 0xa6 0x05 (0b10100110 0b00000101) 69 | 0x22 0x43 ( FIFO) rd 0x1f 0xfd 0xee 0xc9 (0b00011111 0b11111101 0b11101110 0b11001001) 70 | 0x22 0x41 ( STATUS1) rd 0x28 (0b00101000) 71 | -------------------------------------------------------------------------------- /captures/pinecil_1.pvs: -------------------------------------------------------------------------------- 1 | [General] 2 | decode_signals=2 3 | generated_signals=0 4 | meta_objs=1 5 | views=1 6 | 7 | [D0] 8 | color=4279638298 9 | conv_options=0 10 | conversion_type=0 11 | enabled=true 12 | name=D0 13 | 14 | [D1] 15 | color=4287582722 16 | conv_options=0 17 | conversion_type=0 18 | enabled=true 19 | name=D1 20 | 21 | [D2] 22 | color=4291559424 23 | conv_options=0 24 | conversion_type=0 25 | enabled=false 26 | name=D2 27 | 28 | [D3] 29 | color=4294277376 30 | conv_options=0 31 | conversion_type=0 32 | enabled=false 33 | name=D3 34 | 35 | [D4] 36 | color=4293776384 37 | conv_options=0 38 | conversion_type=0 39 | enabled=false 40 | name=D4 41 | 42 | [D5] 43 | color=4285780502 44 | conv_options=0 45 | conversion_type=0 46 | enabled=false 47 | name=D5 48 | 49 | [D6] 50 | color=4281623972 51 | conv_options=0 52 | conversion_type=0 53 | enabled=false 54 | name=D6 55 | 56 | [D7] 57 | color=4285878395 58 | conv_options=0 59 | conversion_type=0 60 | enabled=false 61 | name=D7 62 | 63 | [decode_signal0] 64 | channel0\assigned_signal_name=D0 65 | channel0\initial_pin_state=2 66 | channel0\name=SCL 67 | channel1\assigned_signal_name=D1 68 | channel1\initial_pin_state=2 69 | channel1\name=SDA 70 | channels=2 71 | color=4281510450 72 | conv_options=0 73 | conversion_type=0 74 | decoder0\ann_class0\visible=true 75 | decoder0\ann_class1\visible=true 76 | decoder0\ann_class10\visible=true 77 | decoder0\ann_class2\visible=true 78 | decoder0\ann_class3\visible=true 79 | decoder0\ann_class4\visible=true 80 | decoder0\ann_class5\visible=true 81 | decoder0\ann_class6\visible=true 82 | decoder0\ann_class7\visible=true 83 | decoder0\ann_class8\visible=true 84 | decoder0\ann_class9\visible=true 85 | decoder0\id=i2c 86 | decoder0\options=0 87 | decoder0\row0\visible=true 88 | decoder0\row1\visible=true 89 | decoder0\row2\visible=true 90 | decoder0\visible=true 91 | decoders=1 92 | enabled=true 93 | name=I\xb2\x43 94 | 95 | [decode_signal1] 96 | channel0\assigned_signal_name=D0 97 | channel0\initial_pin_state=2 98 | channel0\name=SCL 99 | channel1\assigned_signal_name=D1 100 | channel1\initial_pin_state=2 101 | channel1\name=SDA 102 | channels=2 103 | color=4286722730 104 | conv_options=0 105 | conversion_type=0 106 | decoder0\ann_class0\visible=true 107 | decoder0\ann_class1\visible=true 108 | decoder0\ann_class10\visible=true 109 | decoder0\ann_class2\visible=true 110 | decoder0\ann_class3\visible=true 111 | decoder0\ann_class4\visible=true 112 | decoder0\ann_class5\visible=true 113 | decoder0\ann_class6\visible=true 114 | decoder0\ann_class7\visible=true 115 | decoder0\ann_class8\visible=true 116 | decoder0\ann_class9\visible=true 117 | decoder0\id=i2c 118 | decoder0\options=0 119 | decoder0\row0\visible=true 120 | decoder0\row1\visible=true 121 | decoder0\row2\visible=true 122 | decoder0\visible=true 123 | decoder1\id=i2cfilter 124 | decoder1\option0\name=address 125 | decoder1\option0\type=x 126 | decoder1\option0\value=@ByteArray(<\0\0\0\0\0\0\0) 127 | decoder1\options=1 128 | decoder1\row0\visible=true 129 | decoder1\visible=true 130 | decoders=2 131 | enabled=true 132 | name=I\xb2\x43 filter 133 | 134 | [meta_obj0] 135 | assoc_view=0 136 | end_time=22 serialization::archive 17 0 0 0 0 3 33900800 1 98070893 2048552 78134345 0 0 0 6 137 | start_time=22 serialization::archive 17 0 0 0 0 79343928 78966510 81037660 72856607 16952035 58293680 -8 0 0 6 138 | type=selection 139 | 140 | [view0] 141 | D0\trace_height=36 142 | D1\trace_height=36 143 | D2\trace_height=36 144 | D3\trace_height=36 145 | D4\trace_height=36 146 | D5\trace_height=36 147 | D6\trace_height=36 148 | D7\trace_height=36 149 | offset=22 serialization::archive 17 0 0 0 0 84065148 90517168 74900031 65195510 39527723 40106391 -8 0 0 6 150 | scale=1.0200983911783723e-07 151 | segment_display_mode=3 152 | splitter_state=@ByteArray(\0\0\0\xff\0\0\0\x1\0\0\0\x2\0\0\0S\0\0\x3\xf3\x1\0\0\0\x1\x1\0\0\0\x1\0) 153 | v_offset=-323 154 | zero_offset=22 serialization::archive 17 0 0 0 0 0 0 0 0 0 0 0 0 0 6 155 | -------------------------------------------------------------------------------- /captures/pinecil_1.sr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CRImier/AltmodeFriend/1fa44c282e6d5b7796f51fc36662d464c36e99c6/captures/pinecil_1.sr -------------------------------------------------------------------------------- /captures/pinecil_commented.txt: -------------------------------------------------------------------------------- 1 | ----------------------- 2 | 0x22 0x01 ( DEV_ID) rd 0x91 (0b10010001) # read ID 3 | 0x22 0x0c ( RESET) wr 0x01 (0b00000001) # reset FUSB 4 | 0x22 0x01 ( DEV_ID) rd 0x91 (0b10010001) # read ID 5 | 0x22 0x0b ( POWER) wr 0x0f (0b00001111) # power everything up 6 | 0x22 0x0a ( MASK) wr 0x00 (0b00000000) # unmask everything 7 | 0x22 0x0e ( MASKA) wr 0x00 (0b00000000) # .. 8 | 0x22 0x0f ( MASKB) wr 0x00 (0b00000000) # .. 9 | 0x22 0x06 ( CTRL0) wr 0x0c (0b00001100) # unmask all interrupts; don't start TX automatically; host current to 11 == 3A? o_o 10 | 0x22 0x09 ( CTRL3) wr 0x07 (0b00000111) # enable automatic packet retries 11 | 0x22 0x08 ( CTRL2) wr 0x00 (0b00000000) # disable DRP toggle... wait wtf 0b00x is Do Not Use o_o 12 | 0x22 0x07 ( CTRL1) wr 0x04 (0b00000100) # flush receive 13 | 14 | # measure start 15 | 0x22 0x02 ( SWITCH0) wr 0x07 (0b00000111) # enable measurement on CC1 16 | 0x22 0x40 ( STATUS0) rd 0x03 (0b00000011) # check current level - 0b11 17 | 0x22 0x02 ( SWITCH0) wr 0x0b (0b00001011) # enable measurement on CC2 18 | 0x22 0x40 ( STATUS0) rd 0x00 (0b00000000) # check current level - 0b00 19 | # compared 20 | 0x22 0x03 ( SWITCH1) wr 0x25 (0b00100101) # pd revision 2, autocrc, TX on CC1 21 | 0x22 0x02 ( SWITCH0) wr 0x07 (0b00000111) # enable measurement on CC1 22 | # measure stop 23 | 24 | 0x22 0x06 ( CTRL0) wr 0x44 (0b01000100) # flush TX; host current to Default? o_o 25 | 0x22 0x07 ( CTRL1) wr 0x04 (0b00000100) # flush receive 26 | 0x22 0x0c ( RESET) wr 0x02 (0b00000010) # reset PD 27 | 28 | # monitor status 29 | 30 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x28 0x41 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00101000 0b01000001) 31 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x41 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000001 0b00001000 0b00000001) 32 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 33 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 34 | 0x22 0x3c (STATUS0A) rd 0x0c 0x00 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000000 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 35 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x13 0x08 0x51 (0b00001100 0b00000001 0b00000000 0b00000000 0b00010011 0b00001000 0b01010001) 36 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b00000001) 37 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x51 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010001 0b00001000 0b01000001) 38 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x01 0x13 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000001 0b00010011 0b00001000 0b00000001) 39 | 40 | # data RXed 41 | 42 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 43 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 44 | 0x22 0x43 ( FIFO) rd 0xa1 0x61 (0b10100001 0b01100001) 45 | 0x22 0x43 ( FIFO) rd 46 | 47 | #PDOs 48 | 49 | 0 0x2c 0x91 0x01 0x08 50 | 1 0x2c 0xd1 0x02 0x00 51 | 2 0x13 0xc1 0x03 0x00 52 | 3 0xdc 0xb0 0x04 0x00 53 | 4 0xa5 0x40 0x06 0x00 54 | 5 0x3c 0x21 0xdc 0xc0 ( 55 | 0 0b00101100 0b10010001 0b00000001 0b00001000 56 | 1 0b00101100 0b11010001 0b00000010 0b00000000 57 | 2 0b00010011 0b11000001 0b00000011 0b00000000 58 | 3 0b11011100 0b10110000 0b00000100 0b00000000 59 | 4 0b10100101 0b01000000 0b00000110 0b00000000 60 | 5 0b00111100 0b00100001 0b11011100 0b11000000) 61 | 62 | # CRC 63 | 0x22 0x43 ( FIFO) rd 0x48 0xc6 0xe7 0xc6 (0b01001000 0b11000110 0b11100111 0b11000110) 64 | 65 | 66 | 67 | 0x22 0x41 ( STATUS1) rd 0x28 (0b00101000) 68 | 69 | 0b0100 70 | 71 | 0x22 0x43 ( FIFO) wr 0x12 0x12 0x12 0x13 0x86 (0b00010010 0b00010010 0b00010010 0b00010011 0b10000110) 72 | 0x22 0x43 ( FIFO) wr 0x82 0x10 0xdc 0x70 0x03 0x41 (0b10000010 0b00010000 0b11011100 0b01110000 0b00000011 0b01000001) 73 | 0x22 0x43 ( FIFO) wr 0xff 0x14 0xfe 0xa1 (0b11111111 0b00010100 0b11111110 0b10100001) 74 | 75 | 76 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x20 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00100000 0b01000001) 77 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x51 0x20 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010001 0b00100000 0b00000001) 78 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x13 0x28 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b00010011 0b00101000 0b01000001) 79 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x28 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00101000 0b01000001) 80 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x42 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01000010 0b00001000 0b00000001) 81 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x04 0x00 0x13 0x08 0x51 (0b00001100 0b00000001 0b00000100 0b00000000 0b00010011 0b00001000 0b01010001) 82 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x13 0x08 0x00 (0b00001100 0b00000001 0b00000000 0b00000000 0b00010011 0b00001000 0b00000000) 83 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b01000001) 84 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x41 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01000001 0b00001000 0b00000001) 85 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x51 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b01010001) 86 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x51 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010001 0b00001000 0b00000001) 87 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x01 0x13 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000001 0b00010011 0b00001000 0b01000001) 88 | 89 | 90 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 91 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 92 | 0x22 0x43 ( FIFO) rd 0x61 0x01 (0b01100001 0b00000001) 93 | 0x22 0x43 ( FIFO) rd 0x8f 0x78 0x38 0x4a (0b10001111 0b01111000 0b00111000 0b01001010) 94 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 95 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 96 | 0x22 0x43 ( FIFO) rd 0xa3 0x03 (0b10100011 0b00000011) 97 | 0x22 0x43 ( FIFO) rd 0x6f 0xac 0xfa 0x5d (0b01101111 0b10101100 0b11111010 0b01011101) 98 | 0x22 0x41 ( STATUS1) rd 0x28 (0b00101000) 99 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x28 0x41 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00101000 0b01000001) 100 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x41 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01000001 0b00001000 0b00000001) 101 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x51 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b01010001) 102 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x00 0x52 0x08 0x01 (0b00001100 0b00000001 0b00000000 0b00000000 0b01010010 0b00001000 0b00000001) 103 | 0x22 0x3c (STATUS0A) rd 0x0c 0x01 0x00 0x01 0x13 0x08 0x41 (0b00001100 0b00000001 0b00000000 0b00000001 0b00010011 0b00001000 0b01000001) 104 | 0x22 0x41 ( STATUS1) rd 0x08 (0b00001000) 105 | 0x22 0x43 ( FIFO) rd 0xe0 (0b11100000) 106 | 0x22 0x43 ( FIFO) rd 0xa6 0x05 (0b10100110 0b00000101) 107 | 0x22 0x43 ( FIFO) rd 0x1f 0xfd 0xee 0xc9 (0b00011111 0b11111101 0b11101110 0b11001001) 108 | 0x22 0x41 ( STATUS1) rd 0x28 (0b00101000) 109 | -------------------------------------------------------------------------------- /captures/trace.py: -------------------------------------------------------------------------------- 1 | 2 | # transaction list 3 | tr = [] 4 | 5 | # current transaction 6 | t = [] 7 | 8 | # filter address 9 | addr = 0x22 10 | 11 | # read/write keyword shortening 12 | tts = {"write:":'wr', "read:":'rd'} 13 | 14 | with open('dec.txt', 'r') as f: 15 | line = f.readline() 16 | while line: 17 | line = line.strip() 18 | if not line: 19 | break 20 | tss, _, _, d = line.split(' ', 3) 21 | if d == "Start": 22 | # new transaction 23 | # saving/discarding the current one 24 | if t: 25 | if t[1] != addr: 26 | #print("Filtering transaction for", hex(t[0]), ":", t) 27 | t = ["start"] # discarding the entire transaction 28 | else: 29 | tr.append(t) 30 | t = ["start"] 31 | elif d.startswith("Address "): 32 | _, rw, a = d.split(' ', 2) 33 | t.append(int(a, 16)) 34 | t.append(tts[rw]) 35 | elif d.startswith("Data"): 36 | _, rw, dt = d.split(' ', 2) 37 | t.append(tts[rw]) 38 | t.append(int(dt, 16)) 39 | elif d in ['ACK', 'NACK', 'Stop', 'Start repeat']: 40 | t.append(d.lower()) 41 | #new line 42 | line = f.readline() 43 | 44 | 45 | print("-----------------------") 46 | 47 | def myhex(i): 48 | return '0x'+hex(i)[2:].zfill(2) 49 | 50 | def mybin(i): 51 | return '0b'+bin(i)[2:].zfill(8) 52 | 53 | # second parsing 54 | 55 | regs = { 56 | 0x00: "CTRL3", 57 | 0x01: "DEV_ID", 58 | 0x02: "SWITCH0", 59 | 0x03: "SWITCH1", 60 | 0x04: "MEASURE", 61 | 0x05: "SLICE", 62 | 0x06: "CTRL0", 63 | 0x07: "CTRL1", 64 | 0x08: "CTRL2", 65 | 0x09: "CTRL3", 66 | 0x0A: "MASK", 67 | 0x0B: "POWER", 68 | 0x0C: "RESET", 69 | 0x0D: "OCPREG", 70 | 0x0E: "MASKA", 71 | 0x0F: "MASKB", 72 | 0x43: "FIFO", 73 | 0x3C: "STATUS0A", 74 | 0x3D: "STATUS1A", 75 | 0x3E: "INTRPT_A", 76 | 0x3F: "INTRPT_A", 77 | 0x40: "STATUS0", 78 | 0x41: "STATUS1", 79 | 0x42: "INTRPT_A", 80 | 0x43: "FIFO", 81 | } 82 | 83 | longest_regn = max(map(len, regs.values())) 84 | 85 | for t in tr: 86 | addr = t[1] 87 | reg = t[5] 88 | t = t[7:] 89 | if t[0] == 'start repeat': 90 | t = t[4:] 91 | op = t[0] 92 | d = [t[1]] 93 | t = t[2:] 94 | for el in t: 95 | if el in ['ack', 'nack', 'stop', 'rd', 'wr']: 96 | continue 97 | d.append(el) 98 | data = " ".join(list(map(myhex, d))) 99 | data += ' ({})'.format(" ".join(list(map(mybin, d)))) 100 | reg_str = "{} ({})".format(myhex(reg), regs.get(reg, " ").rjust(longest_regn, ' ')) 101 | print(myhex(addr), reg_str, op, data) 102 | 103 | 104 | -------------------------------------------------------------------------------- /fusb302.py: -------------------------------------------------------------------------------- 1 | from time import sleep 2 | 3 | ######################## 4 | # 5 | # FUSB-specific code 6 | # 7 | ######################## 8 | 9 | class FUSB302(): 10 | #FUSB302_I2C_SLAVE_ADDR = 0x22 11 | REG_DEVICE_ID = 0x01 12 | REG_SWITCHES0 = 0x02 13 | REG_SWITCHES1 = 0x03 14 | REG_MEASURE = 0x04 15 | REG_CONTROL0 = 0x06 16 | REG_CONTROL1 = 0x07 17 | REG_CONTROL2 = 0x08 18 | REG_CONTROL3 = 0x09 19 | REG_MASK = 0x0A 20 | REG_POWER = 0x0B 21 | REG_RESET = 0x0C 22 | REG_MASKA = 0x0E 23 | REG_MASKB = 0x0F 24 | REG_STATUS0A = 0x3C 25 | REG_STATUS1A = 0x3D 26 | REG_INTERRUPTA = 0x3E 27 | REG_INTERRUPTB = 0x3F 28 | REG_STATUS0 = 0x40 29 | REG_STATUS1 = 0x41 30 | REG_INTERRUPT = 0x42 31 | REG_FIFOS = 0x43 32 | 33 | def __init__(self, bus, addr=0x22, int_p=None): 34 | self.bus = bus 35 | self.addr = addr 36 | self.int_p = int_p 37 | 38 | def reset(self): 39 | # reset the entire FUSB 40 | self.bus.writeto_mem(self.addr, self.REG_RESET, bytes([0b1])) 41 | 42 | def reset_pd(self): 43 | # resets the FUSB PD logic 44 | self.bus.writeto_mem(self.addr, self.REG_RESET, bytes([0b10])) 45 | 46 | def unmask_all(self): 47 | # unmasks all interrupts 48 | self.bus.writeto_mem(self.addr, self.REG_MASK, bytes([0b0])) 49 | self.bus.writeto_mem(self.addr, self.REG_MASKA, bytes([0b0])) 50 | self.bus.writeto_mem(self.addr, self.REG_MASKB, bytes([0b0])) 51 | 52 | def cc_current(self): 53 | # show measured CC level interpreted as USB-C current levels 54 | return self.bus.readfrom_mem(self.addr, self.REG_STATUS0, 1)[0] & 0b11 55 | 56 | def read_cc(self, cc): 57 | # enable a CC pin for reading 58 | assert(cc in [0, 1, 2]) 59 | x = self.bus.readfrom_mem(self.addr, self.REG_SWITCHES0, 1)[0] 60 | x1 = x 61 | clear_mask = ~0b1100 & 0xFF 62 | x &= clear_mask 63 | mask = [0b0, 0b100, 0b1000][cc] 64 | x |= mask 65 | #print('self.REG_SWITCHES0: ', bin(x1), bin(x), cc) 66 | self.bus.writeto_mem(self.addr, self.REG_SWITCHES0, bytes((x,)) ) 67 | 68 | def enable_pullups(self): 69 | # enable host pullups on CC pins, disable pulldowns 70 | x = self.bus.readfrom_mem(0x22, 0x02, 1)[0] 71 | x |= 0b11000000 72 | self.bus.writeto_mem(0x22, 0x02, bytes((x,)) ) 73 | 74 | def set_mdac(self, value): 75 | x = self.bus.readfrom_mem(0x22, 0x04, 1)[0] 76 | x &= 0b11000000 77 | x |= value 78 | self.bus.writeto_mem(0x22, 0x04, bytes((x,)) ) 79 | 80 | def enable_sop(self): 81 | # enable reception of SOP'/SOP" messages 82 | x = self.bus.readfrom_mem(self.addr, self.REG_CONTROL1, 1)[0] 83 | mask = 0b1100011 84 | x |= mask 85 | self.bus.writeto_mem(self.addr, self.REG_CONTROL1, bytes((x,)) ) 86 | 87 | def disable_pulldowns(self): 88 | x = self.bus.readfrom_mem(self.addr, self.REG_SWITCHES0, 1)[0] 89 | clear_mask = ~0b11 & 0xFF 90 | x &= clear_mask 91 | self.bus.writeto_mem(self.addr, self.REG_SWITCHES0, bytes((x,)) ) 92 | 93 | def enable_pulldowns(self): 94 | x = self.bus.readfrom_mem(self.addr, self.REG_SWITCHES0, 1)[0] 95 | x |= 0b11 96 | self.bus.writeto_mem(self.addr, self.REG_SWITCHES0, bytes((x,)) ) 97 | 98 | def measure_sink(self, debug=False): 99 | # read CC pins and see which one senses the pullup 100 | self.read_cc(1) 101 | sleep(0.001) 102 | cc1_c = self.cc_current() 103 | self.read_cc(2) 104 | sleep(0.001) 105 | cc2_c = self.cc_current() 106 | # picking the CC pin depending on which pin can detect a pullup 107 | cc = [1, 2][cc1_c < cc2_c] 108 | if debug: print('m', bin(cc1_c), bin(cc2_c), cc) 109 | if cc1_c == cc2_c: 110 | return 0 111 | return cc 112 | 113 | def measure_source(self, debug=False): 114 | # read CC pins and see which one senses the correct host current 115 | self.read_cc(1) 116 | sleep(0.001) 117 | cc1_c = self.cc_current() 118 | self.read_cc(2) 119 | sleep(0.001) 120 | cc2_c = self.cc_current() 121 | if cc1_c == self.host_current: 122 | cc = 1 123 | elif cc2_c == self.host_current: 124 | cc = 2 125 | else: 126 | cc = 0 127 | if debug: print('m', bin(cc1_c), bin(cc2_c), cc) 128 | return cc 129 | 130 | def set_controls_sink(self): 131 | # boot: 0b00100100 132 | ctrl0 = 0b00000000 # unmask all interrupts; don't autostart TX.. disable pullup current 133 | self.bus.writeto_mem(self.addr, self.REG_CONTROL0, bytes((ctrl0,)) ) 134 | # boot: 0b00000110 135 | ctrl3 = 0b00000111 # enable automatic packet retries 136 | self.bus.writeto_mem(self.addr, self.REG_CONTROL3, bytes((ctrl3,)) ) 137 | 138 | host_current=0b10 139 | 140 | def set_controls_source(self): 141 | # boot: 0b00100100 142 | ctrl0 = 0b00000000 # unmask all interrupts; don't autostart TX 143 | ctrl0 |= self.host_current << 2 # set host current advertisement pullups 144 | self.bus.writeto_mem(self.addr, self.REG_CONTROL0, bytes((ctrl0,)) ) 145 | self.bus.writeto_mem(0x22, 0x06, bytes((ctrl0,)) ) 146 | # boot: 0b00000110 147 | ctrl3 = 0b00000110 # no automatic packet retries 148 | self.bus.writeto_mem(self.addr, self.REG_CONTROL3, bytes((ctrl3,)) ) 149 | # boot: 0b00000010 150 | #ctrl2 = 0b00000000 # disable DRP toggle. setting it to Do Not Use o_o ??? 151 | #self.bus.writeto_mem(self.addr, self.REG_CONTROL2, bytes((ctrl2,)) ) 152 | 153 | def set_wake(self, state): 154 | # boot: 0b00000010 155 | ctrl2 = self.bus.readfrom_mem(0x22, 0x08, 1)[0] 156 | clear_mask = ~(1 << 3) & 0xFF 157 | ctrl2 &= clear_mask 158 | if state: 159 | ctrl2 | (1 << 3) 160 | self.bus.writeto_mem(0x22, 0x08, bytes((ctrl2,)) ) 161 | 162 | def flush_receive(self): 163 | x = self.bus.readfrom_mem(self.addr, self.REG_CONTROL1, 1)[0] 164 | mask = 0b100 # flush receive 165 | x |= mask 166 | self.bus.writeto_mem(self.addr, self.REG_CONTROL1, bytes((x,)) ) 167 | 168 | def flush_transmit(self): 169 | x = self.bus.readfrom_mem(self.addr, self.REG_CONTROL0, 1)[0] 170 | mask = 0b01000000 # flush transmit 171 | x |= mask 172 | self.bus.writeto_mem(self.addr, self.REG_CONTROL0, bytes((x,)) ) 173 | 174 | def enable_tx(self, cc): 175 | # enables switch on either CC1 or CC2 176 | x = self.bus.readfrom_mem(self.addr, self.REG_SWITCHES1, 1)[0] 177 | x1 = x 178 | mask = 0b10 if cc == 2 else 0b1 179 | x &= 0b10011100 # clearing both TX bits and revision bits 180 | x |= mask 181 | x |= 0b100 182 | x |= 0b10 << 5 # revision 3.0 183 | #print('et', bin(x1), bin(x), cc) 184 | self.bus.writeto_mem(self.addr, self.REG_SWITCHES1, bytes((x,)) ) 185 | 186 | def set_roles(self, power_role = 0, data_role = 0): 187 | x = self.bus.readfrom_mem(0x22, 0x03, 1)[0] 188 | x &= 0b01101111 # clearing both role bits 189 | x |= power_role << 7 190 | x |= data_role << 7 191 | self.bus.writeto_mem(0x22, 0x03, bytes((x,)) ) 192 | 193 | def power(self): 194 | # enables all power circuits 195 | x = self.bus.readfrom_mem(self.addr, self.REG_POWER, 1)[0] 196 | mask = 0b1111 197 | x |= mask 198 | self.bus.writeto_mem(self.addr, self.REG_POWER, bytes((x,)) ) 199 | 200 | def polarity(self): 201 | # reads polarity and role bits from STATUS1A 202 | return (self.bus.readfrom_mem(self.addr, self.REG_STATUS1A, 1)[0] >> 3) & 0b111 203 | #'0b110001' 204 | 205 | def interrupts(self): 206 | # return all interrupt registers 207 | return self.bus.readfrom_mem(self.addr, self.REG_INTERRUPTA, 2)+self.bus.readfrom_mem(self.addr, self.REG_INTERRUPT, 1) 208 | 209 | # interrupts are cleared just by reading them, it seems 210 | #def clear_interrupts(self): 211 | # # clear interrupt 212 | # self.bus.writeto_mem(self.addr, self.REG_INTERRUPTA, bytes([0])) 213 | # self.bus.writeto_mem(self.addr, self.REG_INTERRUPT, bytes([0])) 214 | 215 | # this is a way better way to do things than the following function - 216 | # the read loop should be ported to this function, and the next ome deleted 217 | def rxb_state(self): 218 | # get read buffer interrupt states - (rx buffer empty, rx buffer full) 219 | st = self.bus.readfrom_mem(self.addr, self.REG_STATUS1, 1)[0] 220 | return ((st & 0b100000) >> 5, (st & 0b10000) >> 4) 221 | 222 | # TODO: yeet 223 | def rxb_state(self): 224 | st = self.bus.readfrom_mem(self.addr, self.REG_STATUS1, 1)[0] 225 | return ((st & 0b110000) >> 4, (st & 0b11000000) >> 6) 226 | 227 | def get_rxb(self, l=80): 228 | # read from FIFO 229 | return self.bus.readfrom_mem(self.addr, self.REG_FIFOS, l) 230 | 231 | def hard_reset(self): 232 | self.bus.writeto_mem(self.addr, self.REG_CONTROL3, bytes([0b1000000])) 233 | return self.bus.readfrom_mem(self.addr, self.REG_CONTROL3, 1) 234 | 235 | def find_cc(self, fn="measure_sink", debug=False): 236 | if isinstance(fn, str): 237 | fn = getattr(self, fn) 238 | cc = fn(debug=debug) 239 | self.flush_receive() 240 | self.enable_tx(cc) 241 | self.read_cc(cc) 242 | self.flush_transmit() 243 | self.flush_receive() 244 | #import gc; gc.collect() 245 | self.reset_pd() 246 | return cc 247 | 248 | # FUSB toggle logic shorthands 249 | # currently unused 250 | 251 | polarity_values = ( 252 | (0, 0), # 000: logic still running 253 | (1, 0), # 001: cc1, src 254 | (2, 0), # 010: cc2, src 255 | (-1, -1), # 011: unknown 256 | (-1, -1), # 100: unknown 257 | (1, 1), # 101: cc1, snk 258 | (2, 1), # 110: cc2, snk 259 | (0, 2), # 111: audio accessory 260 | ) 261 | 262 | current_values = ( 263 | "Ra/low", 264 | "Rd-Default", 265 | "Rd-1.5", 266 | "Rd-3.0" 267 | ) 268 | 269 | def p_pol(self): 270 | return polarity_values[self.polarity()] 271 | 272 | def p_int(self, a=None): 273 | if a is None: 274 | a = self.interrupts() 275 | return [bin(x) for x in a] 276 | 277 | def p_cur(self): 278 | return current_values[self.cc_current()] 279 | 280 | def send(self, message): 281 | sop_seq = [0x12, 0x12, 0x12, 0x13, 0x80] 282 | eop_seq = [0xff, 0x14, 0xfe, 0xa1] 283 | 284 | sop_seq[4] |= len(message) 285 | 286 | self.bus.writeto_mem(self.addr, self.REG_FIFOS, bytes(sop_seq) ) 287 | self.bus.writeto_mem(self.addr, self.REG_FIFOS, bytes(message) ) 288 | self.bus.writeto_mem(self.addr, self.REG_FIFOS, bytes(eop_seq) ) 289 | -------------------------------------------------------------------------------- /machine.py: -------------------------------------------------------------------------------- 1 | """ 2 | Classes to assist with testing. PinEvent is a simple container of old and new values. StateTrackable is a 3 | base class that records a series of state changes described as PinEvents. Pin, PWM and ADC are mocks that 4 | extend StateTrackable. 5 | """ 6 | 7 | from mock import Mock 8 | import time 9 | time.ticks_us = Mock() 10 | time.ticks_diff = Mock() 11 | 12 | class PinEvent: 13 | def __init__(self, old_value, new_value): 14 | self.event_id = None 15 | self.old_value = old_value 16 | self.new_value = new_value 17 | 18 | def set_id(self, event_id): 19 | self.event_id = event_id 20 | 21 | def __str__(self): 22 | return "Event {} with old value {}, new value {}".format(self.event_id, self.old_value, self.new_value) 23 | 24 | 25 | class StateTrackable: 26 | def __init__(self): 27 | self.events = [] 28 | self.event_id = 1 29 | 30 | def record_event(self, event: PinEvent): 31 | event.set_id(self.event_id) 32 | self.events.append(event) 33 | self.event_id += 1 34 | 35 | def get_event(self, event_id: int) -> PinEvent: 36 | for e in self.events: 37 | if e.event_id == event_id: 38 | return e 39 | 40 | def __str__(self): 41 | return "{}".format(self.events) 42 | 43 | 44 | class Pin(StateTrackable): 45 | 46 | OPEN_DRAIN = 2 47 | IRQ_FALLING = 4 48 | IRQ_RISING = 8 49 | IN = 0 50 | OUT = 1 51 | PULL_UP = 1 52 | PULL_DOWN = 2 53 | 54 | def __init__(self, id, mode=IN, value=None): 55 | super().__init__() 56 | self.id = id 57 | self.mock_value = None 58 | self.mode = mode 59 | self.pull = None 60 | self.irq_falling_handler = None 61 | self.irq_rising_handler = None 62 | 63 | def init(self, mode=IN, pull=None): 64 | self.mock_value = None 65 | self.mode = mode 66 | self.pull = pull 67 | 68 | def value(self, value=None): 69 | if value is None: 70 | return self.mock_value 71 | event = PinEvent(self.mock_value, value) 72 | self.record_event(event) 73 | self.mock_value = event.new_value 74 | if self.irq_rising_handler is not None: 75 | if event.old_value is None or event.new_value > event.old_value: 76 | self.irq_rising_handler(self) 77 | if self.irq_falling_handler is not None: 78 | if event.new_value is not None and event.new_value < event.old_value: 79 | self.irq_falling_handler(self) 80 | 81 | def irq(self, handler, trigger: int = (IRQ_FALLING | IRQ_RISING | OPEN_DRAIN), priority: int = 1, 82 | wake: int = None, hard: bool = False): 83 | if trigger & self.IRQ_FALLING: 84 | self.irq_falling_handler = handler 85 | if trigger & self.IRQ_RISING: 86 | self.irq_rising_handler = handler 87 | 88 | def on(self): 89 | self.value(1) 90 | 91 | def off(self): 92 | self.value(0) 93 | 94 | def toggle(self): 95 | self.value(self.value() - 1) * -1 96 | 97 | def __str__(self): 98 | # Do not change the first 7 characters or it will break code to retrieve pin id 99 | return "Pin({}, mode=ALT, pull=PULL_DOWN, alt=31)".format(self.id) 100 | 101 | 102 | class BusMessage: 103 | def __init__(self, payload): 104 | self.message_id = None 105 | self.payload = payload 106 | 107 | def set_message_id(self, message_id): 108 | self.message_id = message_id 109 | 110 | def __str__(self): 111 | return self.payload 112 | 113 | 114 | class BusMessageGenerator: 115 | 116 | def __init__(self): 117 | self._messages = {} 118 | 119 | def add(self, message: bytes, addr: int = 0x00): 120 | if addr not in self._messages: 121 | self._messages[addr] = list() 122 | bus_message = BusMessage(payload=message) 123 | bus_message.set_message_id(len(self._messages[addr]) + 1) 124 | self._messages[addr].append(bus_message) 125 | 126 | def next(self, addr: int = 0x00) -> bytes: 127 | # in lieu of shift()... 128 | self._messages[addr].reverse() 129 | first = self._messages[addr].pop() 130 | self._messages[addr].reverse() 131 | return first 132 | 133 | def has_next(self, addr: int = 0x00) -> bool: 134 | return len(self._messages[addr]) > 0 135 | 136 | 137 | class Bus: 138 | 139 | def __init__(self): 140 | self._generator = BusMessageGenerator() 141 | self._messages = {} 142 | 143 | @property 144 | def generator(self) -> BusMessageGenerator: 145 | return self._generator 146 | 147 | def get_current_message(self, addr: int = 0x00) -> BusMessage: 148 | if addr not in self._messages: 149 | raise Exception("No messages yet for {}", addr) 150 | max_id = len(self._messages[addr]) 151 | return self.get_message(addr, max_id) 152 | 153 | def record_message(self, message, addr:int = 0x00) -> None: 154 | if addr not in self._messages: 155 | self._messages[addr] = list() 156 | max_id = len(self._messages[addr]) 157 | bus_message = BusMessage(message) 158 | bus_message.set_message_id(max_id + 1) 159 | self._messages[addr].append(bus_message) 160 | 161 | def get_message(self, message_id: int, addr: int = 0x00): 162 | for message in self._messages[addr]: 163 | if message.message_id == message_id: 164 | return message 165 | 166 | 167 | class SPI(Bus): 168 | 169 | CONTROLLER: int = None 170 | LSB: int = 0 171 | MSB: int = 1 172 | 173 | def __init__(self, id: int, baudrate: int = 1_000_000, *, polarity: int = 0, phase: int = 0, bits: int = 8, 174 | firstbit: int = MSB, sck: Pin = None, mosi: Pin = None, miso: Pin = None): 175 | super().__init__() 176 | self._id = id 177 | self._baudrate = baudrate 178 | self._polarity = polarity 179 | self._phase = phase 180 | self._bits = bits 181 | self._firstbit = firstbit 182 | self._sck = sck 183 | self._mosi = mosi 184 | self._miso = miso 185 | 186 | def deinit(self) -> None: 187 | ... 188 | 189 | """Read a number of bytes specified by nbytes while continuously writing the single byte given by write. 190 | Returns a bytes object with the data that was read.""" 191 | def read(self, nbytes: int, write: int = 0x00) -> bytes: 192 | if nbytes is None or nbytes == 0: 193 | raise ValueError("Nbytes invalid {}").format(nbytes) 194 | if self._generator.has_next(): 195 | return self._generator.next().payload[0:nbytes] 196 | else: 197 | return None 198 | 199 | """ Read into the buffer specified by buf while continuously writing the single byte given by write. Returns None. 200 | Note: on WiPy this function returns the number of bytes read.""" 201 | def readinto(self, buf, write: int = 0x00): 202 | if buf is None or len(buf) == 0: 203 | raise ValueError("Buffer invalid {}").format(buf) 204 | reading = self.read(nbytes=len(buf)) 205 | for i in range(len(buf)): 206 | buf[i] = reading[i] 207 | 208 | """ Write the bytes contained in buf. Returns None. 209 | Note: on WiPy this function returns the number of bytes written.""" 210 | def write(self, buf: bytes): 211 | if buf is None or len(buf) == 0: 212 | raise ValueError("Nbytes invalid {}").format(buf) 213 | if buf.__class__ not in (bytearray, bytes, str): 214 | raise ValueError("Buf must be bytearray, bytes or string but is {}".format(buf.__class__)) 215 | clone = bytearray(len(buf)) 216 | clone[:] = buf[:] 217 | self.record_message(message=clone) 218 | 219 | """ Write the bytes from write_buf while reading into read_buf. The buffers can be the same or different, 220 | but both buffers must have the same length. Returns None. 221 | Note: on WiPy this function returns the number of bytes written.""" 222 | def write_readinto(self, write_buf, read_buf): 223 | ... 224 | 225 | 226 | class I2C(Bus): 227 | 228 | def __init__(self, id, scl, sda, freq=400000): 229 | super().__init__() 230 | self.id = id 231 | self.scl = scl 232 | self.sda = sda 233 | self.freq = freq 234 | 235 | def scan(self): 236 | return [] 237 | 238 | '''Read nbytes from the peripheral specified by addr. If stop is true then a STOP condition is generated at the end of the transfer. 239 | Returns a bytes object with the data read.''' 240 | def readfrom(self, addr, nbytes, stop=True) -> bytes: 241 | if nbytes is None or nbytes < 0: 242 | raise ValueError("Nbytes invalid {}").format(nbytes) 243 | if self._generator.has_next(addr=addr): 244 | return self._generator.next(addr=addr).payload[0:nbytes] 245 | else: 246 | return self.get_current_message(addr=addr).payload[0:nbytes] 247 | 248 | """Read into buf from the peripheral specified by addr. The number of bytes read will be the length of buf. 249 | If stop is true then a STOP condition is generated at the end of the transfer. The method returns None.""" 250 | def readfrom_into(self, addr, buf, stop=True): 251 | reading = self.readfrom(addr, buf, stop) 252 | for i in range(len(buf)): 253 | buf[i] = reading[i] 254 | 255 | """Write the bytes from buf to the peripheral specified by addr. If a NACK is received following the write of a byte 256 | from buf then the remaining bytes are not sent. If stop is true then a STOP condition is generated at the end of 257 | the transfer, even if a NACK is received. The function returns the number of ACKs that were received.""" 258 | def writeto(self, addr, buf, stop=True) -> int: 259 | if buf is None or len(buf) == 0: 260 | raise ValueError("Nbytes invalid {}").format(buf) 261 | if buf.__class__ not in (bytearray, bytes, str): 262 | raise ValueError("Buf must be bytearray, bytes or string") 263 | message = None 264 | ack_count = 0 265 | if buf.__class__ in (bytearray, bytes): 266 | message = bytearray(len(buf)) 267 | for i in range(len(buf)): 268 | message[i] = buf[i] 269 | ack_count += 1 270 | if buf.__class__ is str: 271 | message = buf 272 | ack_count = 1 273 | self.record_message(addr=addr, message=message) 274 | return ack_count 275 | 276 | """Write the bytes contained in vector to the peripheral specified by addr. vector should be a tuple or list of 277 | objects with the buffer protocol. The addr is sent once and then the bytes from each object in vector are written 278 | out sequentially. The objects in vector may be zero bytes in length in which case they don’t contribute to the output. 279 | If a NACK is received following the write of a byte from one of the objects in vector then the remaining bytes, and 280 | any remaining objects, are not sent. If stop is true then a STOP condition is generated at the end of the transfer, 281 | even if a NACK is received. The function returns the number of ACKs that were received.""" 282 | def writevto(self, addr, vector, stop=True): 283 | raise NotImplementedError('writevto') 284 | 285 | """Read nbytes from the peripheral specified by addr starting from the memory address specified by memaddr. 286 | The argument addrsize specifies the address size in bits. Returns a bytes object with the data read.""" 287 | def readfrom_mem(self, addr, memaddr, nbytes, *, addrsize=8): 288 | return bytearray(100) 289 | raise NotImplementedError('readfrom_mem') 290 | 291 | """Read into buf from the peripheral specified by addr starting from the memory address specified by memaddr. 292 | The number of bytes read is the length of buf. The argument addrsize specifies the address size in bits 293 | (on ESP8266 this argument is not recognised and the address size is always 8 bits). 294 | The method returns None.""" 295 | def readfrom_mem_into(self, addr, memaddr, buf, *, addrsize=8): 296 | raise NotImplementedError('readfrom_mem_into') 297 | 298 | """Write buf to the peripheral specified by addr starting from the memory address specified by memaddr. 299 | The argument addrsize specifies the address size in bits (on ESP8266 this argument is not recognised and the address size is always 8 bits). 300 | The method returns None.""" 301 | def writeto_mem(self, addr, memaddr, buf, *, addrsize=8): 302 | return 303 | raise NotImplementedError('writeto_mem') 304 | 305 | 306 | class Signal: 307 | 308 | def __init__(self, pin: Pin, invert: bool = False): 309 | self.pin = pin 310 | self.invert = invert 311 | 312 | def value(self, x: int = None): 313 | if self.invert: 314 | x = (x - 1) * -1 315 | self.pin.value(x) 316 | 317 | def on(self): 318 | self.pin.on() 319 | 320 | def off(self): 321 | self.pin.off() 322 | 323 | def __str__(self): 324 | return self.pin.__str__() 325 | 326 | 327 | class ADC(StateTrackable): 328 | def __init__(self, pin: Pin): 329 | super().__init__() 330 | self.pin = pin 331 | self.u16_value = 0 332 | 333 | def write_u16(self, u16_value: int): 334 | event = PinEvent(old_value=self.u16_value, new_value=u16_value) 335 | self.record_event(event) 336 | self.u16_value = u16_value 337 | 338 | def read_u16(self) -> int: 339 | return self.u16_value 340 | 341 | 342 | class PWM(StateTrackable): 343 | def __init__(self, pin: Pin): 344 | super().__init__() 345 | self.pin = pin 346 | self.duty_ns_value = None 347 | self.duty_u16_value = None 348 | self.freq_value = None 349 | 350 | def duty_ns(self, duty_ns_value=None): 351 | if duty_ns_value is None: 352 | return self.duty_ns_value 353 | else: 354 | event = PinEvent(old_value=self.duty_ns_value, new_value=duty_ns_value) 355 | self.record_event(event) 356 | self.duty_ns_value = duty_ns_value 357 | 358 | def duty_u16(self, duty_u16_value=None): 359 | if duty_u16_value is None: 360 | return self.duty_u16_value 361 | else: 362 | event = PinEvent(old_value=self.duty_u16_value, new_value=duty_u16_value) 363 | self.record_event(event) 364 | self.duty_u16_value = duty_u16_value 365 | 366 | def freq(self, freq_value): 367 | if freq_value is None: 368 | return self.freq_value 369 | else: 370 | self.freq_value = freq_value 371 | 372 | -------------------------------------------------------------------------------- /pdstacc.py: -------------------------------------------------------------------------------- 1 | from time import sleep 2 | import sys 3 | 4 | ######################## 5 | # 6 | # Specification data 7 | # 8 | ######################## 9 | 10 | control_message_types = [ 11 | "Reserved", 12 | "GoodCRC", 13 | "GotoMin", 14 | "Accept", 15 | "Reject", 16 | "Ping", 17 | "PS_RDY", 18 | "Get_Source_Cap", 19 | "Get_Sink_Cap", 20 | "DR_Swap", 21 | "PR_Swap", 22 | "VCONN_Swap", 23 | "Wait", 24 | "Soft_Reset", 25 | "Data_Reset", 26 | "Data_Reset_Complete", 27 | "Not_Supported", 28 | "Get_Source_Cap_Extended", 29 | "Get_Status", 30 | "FR_Swap", 31 | "Get_PPS_Status", 32 | "Get_Country_Codes", 33 | "Get_Sink_Cap_Extended", 34 | "Get_Source_Info", 35 | "Get_Revision", 36 | ] 37 | 38 | data_message_types = [ 39 | "Reserved", 40 | "Source_Capabilities", 41 | "Request", 42 | "BIST", 43 | "Sink_Capabilities", 44 | "Battery_Status", 45 | "Alert", 46 | "Get_Country_Info", 47 | "Enter_USB", 48 | "EPR_Request", 49 | "EPR_Mode", 50 | "Source_Info", 51 | "Revision", 52 | "Reserved", 53 | "Reserved", 54 | "Vendor_Defined", 55 | ] 56 | 57 | ######################## 58 | # 59 | # USB-C stacc code 60 | # 61 | ######################## 62 | 63 | class PDStacc(): 64 | pdo_requested = False 65 | pdos = [] 66 | 67 | sent_messages = [] 68 | 69 | # set to -1 because it's incremented before each command is sent out 70 | msg_id = -1 71 | 72 | def __init__(self, fusb): 73 | self.fusb = fusb 74 | 75 | def init_fusb(self): 76 | self.fusb.reset() 77 | self.fusb.power() 78 | self.fusb.unmask_all() 79 | 80 | def setup_sink(self): 81 | #self.fusb.enable_pulldowns() 82 | self.fusb.set_controls_sink() 83 | self.fusb.set_roles() 84 | self.fusb.set_wake(True) 85 | self.fusb.set_mdac(0b100) 86 | cc = 0 87 | cc = self.fusb.find_cc(fn="measure_sink", debug=True) 88 | while cc == 0: 89 | cc = self.fusb.find_cc(fn="measure_sink") 90 | cc = self.fusb.find_cc(fn="measure_sink", debug=True) 91 | self.cc = cc 92 | 93 | def setup_listen(self, cc): 94 | self.fusb.flush_receive() 95 | self.fusb.disable_pulldowns() 96 | sleep(0.2) 97 | self.fusb.read_cc(cc) 98 | self.fusb.enable_sop() 99 | self.fusb.flush_transmit() 100 | self.fusb.flush_receive() 101 | self.fusb.reset_pd() 102 | 103 | def setup_source(self): 104 | self.fusb.set_controls_source() 105 | self.fusb.set_roles(power_role=1) 106 | self.fusb.disable_pulldowns() 107 | self.fusb.set_wake(True) 108 | self.fusb.enable_pullups() 109 | self.fusb.set_mdac(0b111111) 110 | cc = self.fusb.find_cc(fn="measure_source", debug=True) 111 | while cc == 0: 112 | cc = self.fusb.find_cc(fn="measure_source") 113 | cc = self.fusb.find_cc(fn="measure_source", debug=True) 114 | self.cc = cc 115 | self.set_5v_power_rail_cb() 116 | 117 | def increment_msg_id(self): 118 | self.msg_id += 1 119 | if self.msg_id == 8: msg_id = 0 120 | return self.msg_id 121 | 122 | def reset_msg_id(self): 123 | self.msg_id = -1 124 | 125 | packets = [] 126 | 127 | def flow_source(self, psu_advertisement): 128 | counter = 0 129 | advertisement_counter = 1 130 | self.reset_msg_id() 131 | sleep(0.3) 132 | print("sending advertisement") 133 | self.send_advertisement(psu_advertisement) 134 | self.profile_selected = False 135 | try: 136 | timeout = 0.00001 137 | while True: 138 | if self.fusb.rxb_state()[0] == 0: # buffer non-empty 139 | d = get_message() 140 | self.packets.append(d) 141 | msg_types = control_message_types if d["c"] else data_message_types 142 | msg_name = msg_types[d["t"]] 143 | # now we do things depending on the message type that we received 144 | if msg_name == "GoodCRC": # example 145 | print("GoodCRC") 146 | elif msg_name == "Request": 147 | self.profile_selected = True 148 | self.process_psu_request(psu_advertisement, d) 149 | self.show_msg(d) 150 | for message in self.sent_messages: 151 | sys.stdout.write('> ') 152 | sys.stdout.write(myhex(message)) 153 | sys.stdout.write('\n') 154 | self.sent_messages = [] 155 | sleep(timeout) # so that ctrlc works 156 | counter += 1 157 | if counter == 10000: 158 | counter = 0 159 | if not self.profile_selected and advertisement_counter < 30: 160 | print("sending advertisement") 161 | self.send_advertisement(psu_advertisement) 162 | advertisement_counter += 1 163 | if self.fusb.int_p.value() == 0: 164 | i = self.fusb.interrupts() 165 | print(i) 166 | i_reg = i[2] 167 | if i_reg & 0x80: # I_VBUSOK 168 | print("I_VBUSOK") 169 | #pass # just a side effect of vbus being attached 170 | if i_reg & 0x40: # I_ACTIVITY 171 | print("I_ACTIVITY") 172 | pass # just a side effect of CC comms I think? 173 | if i_reg & 0x20: # I_COMP_CHNG 174 | print("I_COMP_CHNG") 175 | # this is where detach can occur, let's check 176 | cc = self.fusb.find_cc(fn="measure_source") 177 | if cc == 0: 178 | print("Disconnect detected!") 179 | return # we exiting this 180 | if i_reg & 0x10: # I_CRC_CHK 181 | pass # new CRC, just a side effect of CC comms 182 | if i_reg & 0x8: # I_ALERT 183 | print("I_ALERT") 184 | x = self.fusb.bus.readfrom_mem(0x22, 0x41, 1)[0] # TODO export 185 | print(bin(x)) 186 | if i_reg & 0x4: # I_WAKE 187 | print("I_WAKE") 188 | if i_reg & 0x2: # I_COLLISION 189 | print("I_COLLISION") 190 | if i_reg & 0x1: # I_BC_LVL 191 | print("I_BC_LVL") 192 | except KeyboardInterrupt: 193 | print("CtrlC") 194 | sleep(1) 195 | raise 196 | 197 | def flow_sink(self): 198 | self.pdo_requested = False # not sure this needs to be here 199 | self.reset_msg_id() 200 | try: 201 | timeout = 0.00001 202 | while True: 203 | if self.fusb.rxb_state()[0] == 0: # buffer non-empty 204 | d = self.get_message() 205 | self.packets.append(d) 206 | msg_types = control_message_types if d["c"] else data_message_types 207 | msg_name = msg_types[d["t"]] 208 | # now we do things depending on the message type that we received 209 | if msg_name == "GoodCRC": # example 210 | pass # print("GoodCRC") 211 | elif msg_name == "Source_Capabilities": 212 | # need to request a PDO! 213 | self.pdos = self.get_pdos(d) 214 | pdo_i, current = self.select_pdo(self.pdos) 215 | # sending a message, need to increment message id 216 | self.request_fixed_pdo(pdo_i, current, current) 217 | # print("PDO requested!") 218 | self.pdo_requested = True 219 | sys.stdout.write(str(self.pdos)) 220 | sys.stdout.write('\n') 221 | elif msg_name in ["Accept", "PS_RDY"]: 222 | self.process_accept_cb(d) 223 | elif msg_name == "Vendor_Defined": 224 | self.parse_vdm(d) 225 | self.react_vdm(d) 226 | self.show_msg(d) 227 | for message in self.sent_messages: 228 | sys.stdout.write('> ') 229 | sys.stdout.write(myhex(message)) 230 | sys.stdout.write('\n') 231 | self.sent_messages = [] 232 | sleep(timeout) # so that ctrlc works 233 | if self.fusb.int_p.value() == 0: 234 | # needs sink detach processing here lmao 235 | i = self.fusb.interrupts() 236 | print(i) 237 | i_reg = i[2] 238 | if i_reg & 0x80: # I_VBUSOK 239 | pass # just a side effect of vbus being attached 240 | if i_reg & 0x40: # I_ACTIVITY 241 | print("I_ACTIVITY") 242 | pass # just a side effect of CC comms I think? 243 | if i_reg & 0x20: # I_COMP_CHNG 244 | print("I_COMP_CHNG") 245 | cc = self.fusb.find_cc(fn="measure_sink") 246 | if cc == 0: 247 | print("Disconnect detected!") 248 | return # we exiting this 249 | if i_reg & 0x10: # I_CRC_CHK 250 | pass # new CRC, just a side effect of CC comms 251 | if i_reg & 0x8: # I_ALERT 252 | print("I_ALERT") 253 | if i_reg & 0x4: # I_WAKE 254 | print("I_WAKE") 255 | if i_reg & 0x2: # I_COLLISION 256 | print("I_COLLISION") 257 | if i_reg & 0x1: # I_BC_LVL 258 | print("I_BC_LVL") 259 | except KeyboardInterrupt: 260 | print("CtrlC") 261 | sleep(1) 262 | raise 263 | 264 | ######################## 265 | # 266 | # Packet reception 267 | # and parsing code 268 | # 269 | ######################## 270 | 271 | header_starts = [0xe0, 0xc0] 272 | 273 | def get_message(self, get_rxb="get_rxb"): 274 | if isinstance(get_rxb, str): 275 | get_rxb = getattr(self.fusb, get_rxb) 276 | header = 0 277 | d = {} 278 | # we might have to get through some message data! 279 | while header not in self.header_starts: 280 | header = get_rxb(1)[0] 281 | if header == 0: 282 | return 283 | if header not in self.header_starts: 284 | # this will be printed, eventually. 285 | # the aim is that it doesn't delay code in the way that print() seems to 286 | sys.stdout.write("disc {}\n".format(hex(header))) 287 | d["o"] = False # incoming message 288 | d["h"] = header 289 | b1, b0 = get_rxb(2) 290 | d["b0"] = b0 291 | d["b1"] = b1 292 | sop = 1 if header == 0xe0 else 0 293 | d["st"] = sop 294 | # parsing the packet header 295 | prole = b0 & 1 296 | d["pr"] = prole 297 | drole = b1 >> 5 & 1 298 | d["dr"] = drole 299 | msg_type = b1 & 0b11111 300 | pdo_count = (b0 >> 4) & 0b111 301 | d["dc"] = pdo_count 302 | d["t"] = msg_type 303 | d["c"] = pdo_count == 0 # control if True else data 304 | msg_index = int((b0 >> 1) & 0b111) 305 | d["i"] = msg_index 306 | if pdo_count: 307 | read_len = pdo_count*4 308 | pdos = get_rxb(read_len) 309 | d["d"] = pdos 310 | _ = get_rxb(4) # crc 311 | rev = b1 >> 6 312 | d["r"] = rev 313 | is_ext = b0 >> 7 # extended 314 | d["e"] = is_ext 315 | msg_types = control_message_types if pdo_count == 0 else data_message_types 316 | msg_name = msg_types[d["t"]] 317 | d["tn"] = msg_name 318 | if msg_name == "Vendor_Defined": 319 | self.parse_vdm(d) 320 | return d 321 | 322 | def show_msg(self, d): 323 | ## d["h"] = header 324 | ## sop = 1 if header == 0xe0 else 0 325 | ## d["st"] = sop 326 | sop_str = "" if d["st"] else "'" 327 | # parsing the packet header 328 | ## d["pr"] = prole 329 | prole_str = "NC"[d["pr"]] if d["st"] else "R" 330 | drole_str = "UD"[d["dr"]] if d["st"] else "R" 331 | ## d["dc"] = pdo_count 332 | ## d["t"] = msg_type 333 | ## d["c"] = pdo_count == 0 # control if True else data 334 | message_types = control_message_types if d["c"] else data_message_types 335 | ## d["i"] = msg_index 336 | msg_type_str = message_types[d["t"]] if d["t"] < len(message_types) else "Reserved" 337 | ## if pdo_count: 338 | ## d["d"] = pdos 339 | ## d["r"] = rev 340 | rev_str = "123"[d["r"]] 341 | ## d["e"] = is_ext 342 | ext_str = ["std", "ext"][d["e"]] 343 | # msg direction 344 | dir_str = ">" if d["o"] else "<" 345 | if d["dc"]: 346 | # converting "41 80 00 FF A4 25 00 2C" to "FF008041 2C0025A4" 347 | pdo_strs = [] 348 | pdo_data = myhex(d["d"]).split(' ') 349 | for i in range(len(pdo_data)//4): 350 | pdo_strs.append(''.join(reversed(pdo_data[(i*4):][:4]))) 351 | pdo_str = " ".join(pdo_strs) 352 | else: 353 | pdo_str = "" 354 | sys.stdout.write("{} {}{}: {}; p{} d{} r{}, {}, p{}, {} {}\n".format(dir_str, d["i"], sop_str, msg_type_str, prole_str, drole_str, rev_str, ext_str, d["dc"], myhex((d["b0"], d["b1"])).replace(' ', ''), pdo_str)) 355 | # extra parsing where possible 356 | if msg_type_str == "Vendor_Defined": 357 | self.print_vdm(d) 358 | #sys.stdout.write(str(d["d"])) 359 | #sys.stdout.write('\n') 360 | elif msg_type_str == "Source_Capabilities": 361 | sys.stdout.write(str(self.get_pdos(d))) 362 | sys.stdout.write('\n') 363 | return d 364 | 365 | ######################## 366 | # 367 | # PDO parsing code 368 | # 369 | ######################## 370 | 371 | pdo_types = ['fixed', 'batt', 'var', 'pps'] 372 | pps_types = ['spr', 'epr', 'res', 'res'] 373 | 374 | def parse_capability_pdo(self, pdo): 375 | pdo_t = self.pdo_types[pdo[3] >> 6] 376 | if pdo_t == 'fixed': 377 | current_h = pdo[1] & 0b11 378 | current_b = ( current_h << 8 ) | pdo[0] 379 | current = current_b * 10 380 | voltage_h = pdo[2] & 0b1111 381 | voltage_b = ( voltage_h << 6 ) | (pdo[1] >> 2) 382 | voltage = voltage_b * 50 383 | peak_current = (pdo[2] >> 4) & 0b11 384 | return (pdo_t, voltage, current, peak_current, pdo[3]) 385 | elif pdo_t == 'batt': 386 | # TODO 387 | return ('batt', pdo) 388 | elif pdo_t == 'var': 389 | current_h = pdo[1] & 0b11 390 | current = ( current_h << 8 ) | pdo[0]*10 391 | # TODO 392 | return ('var', current, pdo) 393 | elif pdo_t == 'pps': 394 | t = (pdo[3] >> 4) & 0b11 395 | limited = (pdo[3] >> 5) & 0b1 396 | max_voltage_h = pdo[3] & 0b1 397 | max_voltage_b = (max_voltage_h << 7) | pdo[2] >> 1 398 | max_voltage = max_voltage_b * 100 399 | min_voltage = pdo[1] * 100 400 | max_current_b = pdo[0] & 0b1111111 401 | max_current = max_current_b * 50 402 | return ('pps', self.pps_types[t], max_voltage, min_voltage, max_current, limited) 403 | 404 | def create_pdo(self, pdo_t, *args): 405 | print(pdo_t, *args) 406 | assert(pdo_t in self.pdo_types) 407 | pdo = [0 for i in range(4)] 408 | if pdo_t == 'fixed': 409 | voltage, current, peak_current, pdo3 = args 410 | current_v = current // 10 411 | current_h = (current_v >> 8) & 0b11 412 | current_l = current_v & 0xFF 413 | pdo[1] = current_h 414 | pdo[0] = current_l 415 | """ 416 | current_h = pdo[1] & 0b11 417 | current_b = ( current_h << 8 ) | pdo[0] 418 | current = current_b * 10 419 | """ 420 | voltage_v = voltage // 50 421 | pdo[2] = (voltage_v >> 6) & 0b1111 422 | pdo[1] |= (voltage_v & 0b111111) << 2 423 | """ 424 | voltage_h = pdo[2] & 0b1111 425 | voltage_b = ( voltage_h << 6 ) | (pdo[1] >> 2) 426 | voltage = voltage_b * 50 427 | """ 428 | pdo[2] |= (peak_current & 0b11) << 4 429 | peak_current = (pdo[2] >> 4) & 0b11 430 | pdo[3] = pdo3 431 | pdo[3] |= self.pdo_types.index(pdo_t) << 6 432 | elif pdo_t == 'batt': 433 | raise Exception("Batt PDO formation not implemented yet!") 434 | elif pdo_t == 'var': 435 | raise Exception("Variable PDO formation not implemented yet!") 436 | elif pdo_t == 'pps': 437 | """t = (pdo[3] >> 4) & 0b11 438 | limited = (pdo[3] >> 5) & 0b1 439 | max_voltage_h = pdo[3] & 0b1 440 | max_voltage_b = (max_voltage_h << 7) | pdo[2] >> 1 441 | max_voltage = max_voltage_b * 100 442 | min_voltage = pdo[1] * 100 443 | max_current_b = pdo[0] & 0b1111111 444 | max_current = max_current_b * 50 445 | return ('pps', pps_types[t], max_voltage, min_voltage, max_current, limited)""" 446 | raise Exception("PPS PDO formation not implemented yet!") 447 | print(self.parse_capability_pdo(bytes(pdo))) 448 | return pdo 449 | 450 | def get_pdos(self, d): 451 | pdo_list = [] 452 | pdos = d["d"] 453 | for pdo_i in range(d["dc"]): 454 | pdo_bytes = pdos[(pdo_i*4):][:4] 455 | #print(myhex(pdo_bytes)) 456 | parsed_pdo = self.parse_capability_pdo(pdo_bytes) 457 | pdo_list.append(parsed_pdo) 458 | return pdo_list 459 | 460 | ######################## 461 | # 462 | # Command sending code 463 | # and simple commands 464 | # 465 | ######################## 466 | 467 | def send_command(self, command, data, msg_id=None, rev=0b10, power_role=0, data_role=0): 468 | p = [command, data, msg_id, rev, power_role, data_role] 469 | self.packets.append(p) 470 | msg_id = self.increment_msg_id() if msg_id is None else msg_id 471 | obj_count = len(data) // 4 472 | 473 | header = [0, 0] # hoot hoot ! 474 | 475 | header[0] |= rev << 6 # PD revision 476 | header[0] |= (data_role & 0b1) << 5 # PD revision 477 | header[0] |= (command & 0b11111) 478 | 479 | header[1] = power_role & 0b1 480 | header[1] |= (msg_id & 0b111) << 1 # message ID 481 | header[1] |= obj_count << 4 482 | 483 | message = header+data 484 | 485 | self.fusb.send(message) 486 | 487 | self.sent_messages.append(message) 488 | 489 | def soft_reset(self): 490 | self.send_command(0b01101, []) 491 | self.reset_msg_id() 492 | 493 | ######################## 494 | # 495 | # PSU request processing code 496 | # 497 | ######################## 498 | 499 | def send_advertisement(self, psu_advertisement): 500 | #data = [bytes(a) for a in psu_advertisement] 501 | data = psu_advertisement 502 | self.send_command(0b1, data, power_role=1, data_role=1) 503 | 504 | def process_psu_request(self, d): 505 | print(d) 506 | profile = ((d["d"][3] >> 4)&0b111)-1 507 | print("Selected profile", profile) 508 | if not self.validate_profile_cb(profile, d): 509 | print("Profile", profile, "not handled!") 510 | return False 511 | #TODO respose that profile request is not valid!!! 512 | self.send_command(0b11, [], power_role=1, data_role=1) # Accept 513 | # external callback 514 | self.switch_to_profile_cb(profile, d) 515 | sleep(0.1) 516 | self.send_command(0b110, [], power_role=1, data_role=1) # PS_RDY 517 | 518 | ######################## 519 | # 520 | # PDO request code 521 | # 522 | ######################## 523 | 524 | def request_fixed_pdo(self, num, current, max_current): 525 | pdo = [0 for i in range(4)] 526 | 527 | max_current_b = max_current // 10 528 | max_current_l = max_current_b & 0xff 529 | max_current_h = max_current_b >> 8 530 | pdo[0] = max_current_l 531 | pdo[1] |= max_current_h 532 | 533 | current_b = current // 10 534 | current_l = current_b & 0x3f 535 | current_h = current_b >> 6 536 | pdo[1] |= current_l << 2 537 | pdo[2] |= current_h 538 | 539 | pdo[3] |= (num+1) << 4 # object position 540 | pdo[3] |= 0b1 # no suspend 541 | 542 | self.send_command(0b00010, pdo) 543 | 544 | def request_pps_pdo(self, num, voltage, current): 545 | pdo = [0 for i in range(4)] 546 | 547 | current = current // 50 548 | pdo[0] = current & 0x7f 549 | 550 | voltage = voltage // 20 551 | voltage_l = (voltage & 0x7f) 552 | voltage_h = (voltage >> 7) & 0x1f 553 | pdo[1] |= voltage_l << 1 554 | pdo[2] = voltage_h 555 | 556 | pdo[3] |= (num+1) << 4 # object position 557 | pdo[3] |= 0b1 # no suspend 558 | 559 | self.send_command(0b00010, pdo) 560 | 561 | def flow_record(self, packets): 562 | while True: 563 | if self.fusb.rxb_state()[0] == 0: 564 | print(self.get_buffer_fast(packets)) 565 | #print(self.fusb.get_rxb(80)) 566 | #print(get_message()) 567 | sleep(0.0001) 568 | 569 | def get_buffer_fast(self, packets): 570 | packet = [] 571 | while self.fusb.rxb_state()[0] == 0: 572 | packet.append(self.fusb.get_rxb(1)[0]) 573 | packets.append(packet) 574 | return packet 575 | 576 | ######################## 577 | # 578 | # VDM parsing and response code 579 | # 580 | ######################## 581 | 582 | vdm_commands = [ 583 | "Reserved", 584 | "Discover Identity", 585 | "Discover SVIDs", 586 | "Discover Modes", 587 | "Enter Mode", 588 | "Exit Mode", 589 | "Attention"] 590 | 591 | svids = { 592 | 0xff00: 'SID', 593 | 0xff01: 'DisplayPort', 594 | } 595 | 596 | dp_commands = { 597 | 0x10: "DP Status Update", 598 | 0x11: "DP Configure"} 599 | 600 | vdm_cmd_types = ["REQ", "ACK", "NAK", "BUSY"] 601 | 602 | # reply-with-hardcoded code 603 | 604 | def react_vdm(self, d): 605 | if d["vdm_s"]: 606 | cmd_type = d["vdm_ct"] 607 | command_name = d["vdm_cn"] 608 | # response vdm params 609 | rd = {} 610 | # all same params as the incoming message, save for the command type 611 | for key in ["vdm_s", "vdm_sv", "vdm_c", "vdm_v", "vdm_o"]: 612 | rd[key] = d[key] 613 | # command type is ACK and not REQ for all command replies 614 | rd["vdm_ct"] = 1 # ACK 615 | if command_name == "Discover Identity": 616 | # discover identity response with "we are an altmode adapter yesyes" 617 | data = list(b'A\xA0\x00\xff\xa4%\x00,\x00\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x11') 618 | r = self.create_vdm_data(rd, data[4:]) 619 | print(r) 620 | print(data) 621 | self.send_command(d["t"], r) 622 | #sys.stdout.write("a") # debug stuff 623 | elif command_name == "Discover SVIDs": 624 | data = list(b'B\xA0\x00\xff\x00\x00\x01\xff') 625 | r = self.create_vdm_data(rd, data[4:]) 626 | print(r) 627 | print(data) 628 | self.send_command(d["t"], r) 629 | #sys.stdout.write("b") 630 | elif command_name == "Discover Modes": 631 | #data = list(b'C\xA0\x01\xff\x45\x04\x00\x00') 632 | data = list(b'C\xA0\x01\xff\x05\x0c\x00\x00') 633 | r = self.create_vdm_data(rd, data[4:]) 634 | print(r) 635 | print(data) 636 | self.send_command(d["t"], r) 637 | #sys.stdout.write("c") 638 | elif command_name == "Enter Mode": 639 | data = list(b'D\xA1\x01\xff') 640 | r = self.create_vdm_data(rd, []) 641 | print(r) 642 | print(data) 643 | self.send_command(d["t"], r) 644 | #sys.stdout.write("d") 645 | elif command_name == "DP Status Update": 646 | #data = list(b'P\xA1\x01\xff\x1a\x00\x00\x00') 647 | data = list(b'P\xA1\x01\xff\x9a\x00\x00\x00') 648 | r = self.create_vdm_data(rd, data[4:]) 649 | print(r) 650 | print(data) 651 | self.send_command(d["t"], r) 652 | #sys.stdout.write("e") 653 | elif command_name == "DP Configure": 654 | data = list(b'Q\xA1\x01\xff') 655 | r = self.create_vdm_data(rd, []) 656 | print(r) 657 | print(data) 658 | self.send_command(d["t"], r) 659 | #sys.stdout.write("f") 660 | # no unstructured vdm processing at this time 661 | 662 | def create_vdm_data(self, d, data): 663 | """ 664 | Creates the VDM header (PDO) from a dict with pre-supplied data and an additional data list. 665 | """ 666 | l = 4 + len(data) 667 | vdm = bytearray(l) 668 | for i in data: 669 | vdm[i+4] = i 670 | # most basic vdm flags 671 | vdm_s = d["vdm_s"] 672 | vdm[1] |= vdm_s << 7 673 | vdm_sv = d["vdm_sv"] 674 | vdm[2] = vdm_sv & 0xff 675 | vdm[3] = vdm_sv >> 8 676 | # can't build unstructured vdms yet 677 | if vdm_s: 678 | # building structured vdm 679 | # vdm command 680 | vdm_c = d["vdm_c"] 681 | vdm[0] |= (vdm_c & 0b11111) 682 | # vdm command type 683 | vdm_ct = d["vdm_ct"] 684 | vdm[0] |= (vdm_ct & 0b11) << 6 685 | # default version codes set to 0b01; 0b00 686 | vdm_v = d.get("vdm_v", 0b0100) 687 | vdm[1] |= (vdm_v & 0b1111) << 3 688 | # object position 689 | vdm_o = d.get("vdm_o", 0) 690 | vdm[1] |= vdm_o & 0b111 691 | else: 692 | raise NotImplementedError 693 | return bytes(vdm) 694 | 695 | def parse_vdm(self, d): 696 | data = d['d'] 697 | is_structured = data[1] >> 7 698 | d["vdm_s"] = is_structured 699 | svid = (data[3] << 8) + data[2] 700 | d["vdm_sv"] = svid 701 | svid_name = self.svids.get(svid, "Unknown ({})".format(hex(svid))) 702 | d["vdm_svn"] = svid_name 703 | if is_structured: 704 | # version: major and minor 705 | version_bin = (data[1] >> 3) & 0xf 706 | d["vdm_v"] = version_bin 707 | obj_pos = data[1] & 0b111 708 | d["vdm_o"] = obj_pos 709 | cmd_type = data[0]>>6 710 | d["vdm_ct"] = cmd_type 711 | command = data[0] & 0b11111 712 | d["vdm_c"] = command 713 | if command > 15: 714 | command_name = "SVID specific {}".format(bin(command)) 715 | if svid_name == "DisplayPort": 716 | command_name = self.dp_commands.get(command, command_name) 717 | else: 718 | command_name = self.vdm_commands[command] if command < 7 else "Reserved" 719 | d["vdm_cn"] = command_name 720 | #if svid_name == "DisplayPort": 721 | # parse_dp_command(version_str()) 722 | else: 723 | vdmd = [data[1] & 0x7f, data[0]] 724 | d["vdm_d"] = vdmd 725 | #print(d) 726 | 727 | vdm_dp_pin_assg = { 728 | 0b1:"A", 729 | 0b10:"B", 730 | 0b100:"C", 731 | 0b1000:"D", 732 | 0b10000:"E", 733 | 0b100000:"F", 734 | } 735 | 736 | vdm_dp_port_cap = [ 737 | "RES", 738 | "UFP", 739 | "DFP", 740 | "UFP&DFP" 741 | ] 742 | 743 | vdm_dp_port_conn = [ 744 | "NC", 745 | "UFP", 746 | "DFP", 747 | "UFP&DFP" 748 | ] 749 | 750 | vdm_dp_port_conf = [ 751 | "USB", 752 | "DFP", 753 | "DFP", 754 | "RES" 755 | ] 756 | 757 | vdm_dp_sgn = { 758 | 0b1:"DP", 759 | 0b10:"USBg2", 760 | 0b100:"RES1", 761 | 0b1000:"RES2" 762 | } 763 | 764 | def print_vdm(self, d): 765 | if d["vdm_s"]: 766 | svid_name = d["vdm_svn"] 767 | version_str = mybin([d["vdm_v"]])[4:] 768 | objpos_str = mybin([d["vdm_o"]])[5:] 769 | cmd_type_name = self.vdm_cmd_types[d["vdm_ct"]] 770 | cmd_name = d["vdm_cn"] 771 | sys.stdout.write("VDM: str, m{} v{} o{}, ct{}: {}\n".format(svid_name, version_str, objpos_str, cmd_type_name, cmd_name)) 772 | if svid_name == "DisplayPort": 773 | if cmd_name == "Discover Modes" and cmd_type_name == "ACK": 774 | msg = d['d'][4:] 775 | # port capability (bits 0:1) 776 | port_cap = msg[0] & 0b11 777 | vdm_dp_port_cap_s = self.vdm_dp_port_cap[port_cap] 778 | # signaling (bits 5:2) 779 | sgn = (msg[0] >> 2) & 0b1111 780 | sgn_s = [] 781 | for p in self.vdm_dp_sgn.keys(): 782 | if sgn & p: 783 | sgn_s.append(self.vdm_dp_sgn[p]) 784 | sgn_s = ",".join(sgn_s) 785 | # receptacle indication (bit 6) 786 | r_i = (msg[0] >> 6) & 0b1 787 | r_s = "re" if r_i else "pl" 788 | # usb2 signaling (bit 7) 789 | u2_i = (msg[0] >> 7) & 0b1 790 | u2_s = "n" if u2_i else "y" 791 | # dfp pin assignments (bits 15:8) 792 | dfp_assy_n = msg[1] 793 | dfp_assy_s = "" 794 | for p in self.vdm_dp_pin_assg.keys(): 795 | if dfp_assy_n & p: 796 | dfp_assy_s += self.vdm_dp_pin_assg[p] 797 | # dfp pin assignments (bits 23:16) 798 | ufp_assy_n = msg[2] 799 | ufp_assy_s = "" 800 | for p in self.vdm_dp_pin_assg.keys(): 801 | if ufp_assy_n & p: 802 | ufp_assy_s += self.vdm_dp_pin_assg[p] 803 | #res_byte = msg[3] # (bites 31:24, has to be 0) 804 | sys.stdout.write("\tModes: p_cap:{} sgn:{} ri:{} u2:{} d_ass:{} u_ass:{}\n".format(vdm_dp_port_cap_s, sgn_s, r_s, u2_s, dfp_assy_s, ufp_assy_s)) 805 | elif cmd_name == "DP Status Update": 806 | msg = d['d'][4:] 807 | # dfp/ufp connected (bits 0:1) 808 | conn = msg[0] & 0b11 809 | conn_s = self.vdm_dp_port_conn[conn] 810 | # power (bit 2) 811 | pwr = (msg[0] >> 2) & 0b1 812 | pwr_s = "d" if pwr else "n" 813 | # enabled (bit 3) 814 | en = (msg[0] >> 3) & 0b1 815 | en_s = "y" if en else "n" 816 | # multi-function (bit 4) 817 | mf = (msg[0] >> 4) & 0b1 818 | mf_s = "p" if mf else "n" 819 | # usb switch req (bit 5) 820 | usw = (msg[0] >> 5) & 0b1 821 | usw_s = "r" if usw else "n" 822 | # dp exit req (bit 6) 823 | dpe = (msg[0] >> 6) & 0b1 824 | dpe_s = "r" if dpe else "n" 825 | # HPD state (bit 7) 826 | hpd = (msg[0] >> 7) & 0b1 827 | hpd_s = "h" if hpd else "l" 828 | # IRQ state (bit 8) 829 | irq = msg[1] & 0b1 830 | irq_s = str(irq) 831 | sys.stdout.write("\tStatus: conn:{} pwr:{} en:{} mf:{} usw:{} dpe:{} hpd:{} irq:{}\n".format(conn_s, pwr_s, en_s, mf_s, usw_s, dpe_s, hpd_s, irq_s)) 832 | if cmd_name == "DP Configure" and cmd_type_name == "REQ": 833 | msg = d['d'][4:] 834 | # select configuration (bits 0:1) 835 | conf = msg[0] & 0b11 836 | conf_s = self.vdm_dp_port_conf[conf] 837 | # signaling (bits 5:2) 838 | sgn = (msg[0] >> 2) & 0b1111 839 | sgn_s = [] 840 | for p in self.vdm_dp_sgn.keys(): 841 | if sgn & p: 842 | sgn_s.append(self.vdm_dp_sgn[p]) 843 | sgn_s = ",".join(sgn_s) 844 | if not sgn_s: 845 | sgn_s = "UNSP" 846 | # reserved (bits 7:6) 847 | # ufp pin assignments (bits 15:8) 848 | ufp_assy_n = msg[1] 849 | ufp_assy_s = "" 850 | for p in self.vdm_dp_pin_assg.keys(): 851 | if ufp_assy_n & p: 852 | ufp_assy_s += self.vdm_dp_pin_assg[p] 853 | #res_bytes = msg[2:] # (bytes 31:24, has to be 0) 854 | sys.stdout.write("\tConfigure: conf:{} sgn:{} p_ass:{}\n".format(conf_s, sgn_s, ufp_assy_s)) 855 | #di = d 856 | #breakpoint() 857 | else: 858 | sys.stdout.write("VDM: unstr, m{}, d{}".format(svid_name, myhex(d["vdm_d"]))) 859 | 860 | ######################## 861 | # 862 | # Helper functions 863 | # 864 | ######################## 865 | 866 | def myhex(b, j=" "): 867 | l = [] 868 | for e in b: 869 | e = hex(e)[2:].upper() 870 | if len(e) < 2: 871 | e = ("0"*(2-len(e)))+e 872 | l.append(e) 873 | return j.join(l) 874 | 875 | def mybin(b, j=" "): 876 | l = [] 877 | for e in b: 878 | e = bin(e)[2:].upper() 879 | if len(e) < 8: 880 | e = ("0"*(8-len(e)))+e 881 | l.append(e) 882 | return j.join(l) 883 | 884 | -------------------------------------------------------------------------------- /simplest_sink_example.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C, ADC 2 | from time import sleep 3 | 4 | from fusb302 import FUSB302 5 | from pdstacc import PDStacc 6 | 7 | ######################## 8 | # 9 | # Hardware Stuff 10 | # 11 | ######################## 12 | 13 | i2c = I2C(sda=Pin(18), scl=Pin(19), id=1, freq=400000) 14 | print(i2c.scan()) 15 | int_p = Pin(20, Pin.IN, Pin.PULL_UP) 16 | 17 | a = ADC(Pin(28)) 18 | print(a.read_u16()) 19 | 20 | fusb = FUSB302(i2c, int_p=int_p) 21 | stacc = PDStacc(fusb) 22 | 23 | def get_adc_vbus(): 24 | return (3.3*11*a.read_u16())/65536 25 | 26 | print(get_adc_vbus(), "V") 27 | 28 | def process_accept(d): 29 | # callback from sink loop, letting you take note when your profile is accepted 30 | print(get_adc_vbus(), "V") 31 | 32 | stacc.process_accept_cb = process_accept 33 | 34 | ################################### 35 | # 36 | # Power profile selection examples 37 | # 38 | ################################### 39 | 40 | """ 41 | Here's the summary of what you need to care about. 42 | You must pick a PD profile within 500ms (or so) 43 | after a PSU sends you the Source_Capabilities 44 | message. As a result, the responder function has to be quick. 45 | You cannot do user input in this function, or any long-winded processing. 46 | 47 | If you don't pick a profile within the timeout, the PSU will shut off the default 5V power. 48 | This is why the select_pdo functions are short and sweet. 49 | 50 | Remember - after you request any profile, you can re-request profiles arbitrarily. 51 | So, if you need to pick user-chosen profiles, you can initially pick the 5V profile, 52 | and then take your time to think about it, sending a new Request as soon as you want to. 53 | 54 | The last received PDOs are always available as `stacc.pdos`. 55 | """ 56 | 57 | # simple example - pick a fixed PDO with voltage of 5V, and request maximum current available 58 | 59 | expected_voltage = 20 60 | #expected_current = 1000 61 | 62 | def select_pdo_for_voltage(pdos, voltage=5): 63 | """ 64 | request the 5V profile and that's it 65 | requests the maximum current available 66 | """ 67 | for i, pdo in enumerate(pdos): 68 | if pdo[0] != 'fixed': # skipping variable PDOs 69 | continue 70 | t, pdo_voltage, max_current, oc, flags = pdo 71 | if pdo_voltage//1000 == voltage: 72 | return i, max_current 73 | 74 | # setting our callback to be used 75 | stacc.select_pdo = select_pdo_for_voltage 76 | 77 | ######################## 78 | # 79 | # it gets real here 80 | # 81 | ######################## 82 | 83 | stacc.init_fusb() 84 | while True: 85 | try: 86 | stacc.setup_sink() 87 | stacc.flow_sink() 88 | # after flow_sink exits, setup_sink runs again 89 | # this handles stack reset after unplug 90 | # this would best work as an actual explicit state machine 91 | except KeyboardInterrupt: 92 | # lets you exit the loop on ctrlc 93 | print("CtrlC again to exit") 94 | sleep(1) 95 | -------------------------------------------------------------------------------- /sink_example.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C, ADC 2 | from time import sleep 3 | 4 | from fusb302 import FUSB302 5 | from pdstacc import PDStacc 6 | 7 | ######################## 8 | # 9 | # Hardware Stuff 10 | # 11 | ######################## 12 | 13 | #vl_pol = Pin(2, Pin.OUT, value=0) 14 | #vl_amsel = Pin(3, Pin.OUT, value=1) 15 | #vl_en = Pin(4, Pin.OUT, value=1) 16 | 17 | #bl_en = Pin(10, Pin.OUT, value=1) 18 | #bl_pwm = Pin(15, Pin.OUT, value=1) 19 | 20 | i2c = I2C(sda=Pin(18), scl=Pin(19), id=1, freq=400000) 21 | print(i2c.scan()) 22 | int_p = Pin(20, Pin.IN, Pin.PULL_UP) 23 | 24 | a = ADC(Pin(28)) 25 | print(a.read_u16()) 26 | 27 | fusb = FUSB302(i2c, int_p=int_p) 28 | stacc = PDStacc(fusb) 29 | 30 | def get_adc_vbus(): 31 | return (3.3*11*a.read_u16())/65536 32 | 33 | print(get_adc_vbus(), "V") 34 | 35 | def process_accept(d): 36 | # callback from sink loop, letting you take note when your profile is accepted 37 | print(get_adc_vbus(), "V") 38 | 39 | stacc.process_accept_cb = process_accept 40 | 41 | ################################### 42 | # 43 | # Power profile selection examples 44 | # 45 | ################################### 46 | 47 | """ 48 | Here's the summary of what you need to care about. 49 | You must pick a PD profile within 500ms (or so) 50 | after a PSU sends you the Source_Capabilities 51 | message. As a result, the responder function has to be quick. 52 | You cannot do user input in this function, or any long-winded processing. 53 | 54 | If you don't pick a profile within the timeout, the PSU will shut off the default 5V power. 55 | This is why the select_pdo functions are short and sweet. 56 | 57 | Remember - after you request any profile, you can re-request profiles arbitrarily. 58 | So, if you need to pick user-chosen profiles, you can initially pick the 5V profile, 59 | and then take your time to think about it, sending a new Request as soon as you want to. 60 | 61 | The last received PDOs are always available as `stacc.pdos`. 62 | """ 63 | 64 | 65 | # simple example - pick a fixed PDO with voltage of 20, and request maximum current available 66 | 67 | expected_voltage = 20 68 | #expected_current = 1000 69 | 70 | def select_pdo_for_voltage(pdos, voltage=None, current=None): 71 | if voltage is None: voltage = expected_voltage 72 | #if current is None: current = expected_current 73 | for i, pdo in enumerate(pdos): 74 | if pdo[0] != 'fixed': # skipping variable PDOs 75 | continue 76 | t, pdo_voltage, max_current, oc, flags = pdo 77 | if pdo_voltage//1000 == voltage: 78 | current = current if current else max_current 79 | return i, current 80 | 81 | # Another example 82 | 83 | expected_resistance = 8 84 | 85 | def select_pdo_for_resistance(pdos, resistance = None): 86 | # finding a PDO with maximum extractable power 87 | # for a given static resistance, 88 | # while making sure that we don't overcurrent the PSU 89 | # calculation storage lists 90 | if resistance is None: resistance = expected_resistance 91 | power_levels = [] 92 | currents = [] 93 | for pdo in pdos: 94 | if pdo[0] != 'fixed': # skipping variable PDOs for now 95 | # keeping indices in sync 96 | power_levels.append(0); currents.append(0) 97 | continue 98 | t, voltage, max_current, oc, flags = pdo 99 | voltage = voltage / 1000 100 | max_current = max_current / 1000 101 | # calculating the power needed 102 | current = voltage / resistance 103 | current = current * 1.10 # adding 10% leeway 104 | if current > max_current: # current too high, skipping 105 | # keeping indices in sync 106 | power_levels.append(0); currents.append(0) 107 | continue 108 | power = voltage * current 109 | power_levels.append(power) 110 | currents.append(int(current*1000)) 111 | # finding the maximum power level 112 | i = power_levels.index(max(power_levels)) 113 | # returning the PDO index + current we'd need 114 | return i, currents[i] 115 | 116 | # setting our callback to be used 117 | stacc.select_pdo = select_pdo_for_voltage 118 | 119 | ######################## 120 | # 121 | # it gets real here 122 | # 123 | ######################## 124 | 125 | stacc.init_fusb() 126 | while True: 127 | try: 128 | stacc.setup_sink() 129 | stacc.flow_sink() 130 | # after flow_sink exits, setup_sink runs again 131 | # this handles stack reset after unplug 132 | # this would best work as an actual explicit state machine 133 | except KeyboardInterrupt: 134 | # lets you exit the loop on ctrlc 135 | print("CtrlC again to exit") 136 | sleep(1) 137 | -------------------------------------------------------------------------------- /sniffer.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C, ADC 2 | from time import sleep 3 | 4 | from fusb302 import FUSB302 5 | from pdstacc import PDStacc 6 | 7 | ######################## 8 | # 9 | # Hardware Stuff 10 | # 11 | ######################## 12 | 13 | replay = True 14 | 15 | i2c = I2C(sda=Pin(18), scl=Pin(19), id=1, freq=400000) 16 | print(i2c.scan()) 17 | int_p = Pin(20, Pin.IN, Pin.PULL_UP) 18 | 19 | fusb = FUSB302(i2c, int_p=int_p) 20 | 21 | stacc = PDStacc(fusb) 22 | 23 | ######################## 24 | # 25 | # Packet capture code 26 | # 27 | ######################## 28 | 29 | packets = [] 30 | # my usb-c dock with usb3 and hdmi i think? 31 | packets1 = [[192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 65, 0, 187, 108, 187, 168, 224, 66, 16, 44, 177, 4, 18, 171, 173, 31, 42, 224, 97, 1, 143, 120, 56, 74, 224, 99, 3, 33, 123, 0, 150, 224, 65, 2, 151, 13, 181, 70], [224, 102, 5, 81, 42, 20, 2, 224, 65, 4, 162, 168, 214, 175], [192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212], [192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107, 192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107, 192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107, 192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107], [192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17, 192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17, 192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17, 192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17], [192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158, 192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158, 192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158, 192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158], [192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228, 192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228, 192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228, 192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228], [192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91, 192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91, 192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91, 192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91], [192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33, 192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33, 192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33, 192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33], [192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174, 192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174, 192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174, 192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174], [192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212], [192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107, 192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107, 192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107, 192, 79, 24, 1, 128, 0, 255, 49, 86, 215, 107], [192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17, 192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17, 192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17, 192, 79, 26, 1, 128, 0, 255, 81, 5, 23, 17], [192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158, 192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158, 192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158, 192, 79, 28, 1, 128, 0, 255, 241, 240, 87, 158], [192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228, 192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228, 192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228, 192, 79, 30, 1, 128, 0, 255, 145, 163, 151, 228], [192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91, 192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91, 192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91, 192, 79, 16, 1, 128, 0, 255, 240, 29, 167, 91], [192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33, 192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33, 192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33, 192, 79, 18, 1, 128, 0, 255, 144, 78, 103, 33], [192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174, 192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174, 192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174, 192, 79, 20, 1, 128, 0, 255, 48, 187, 39, 174], [192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212, 192, 79, 22, 1, 128, 0, 255, 80, 232, 231, 212], [224, 111, 23, 1, 128, 0, 255, 214, 196, 43, 238, 224, 65, 6, 142, 201, 216, 65, 224, 79, 82, 65, 128, 0, 255, 164, 37, 0, 44, 0, 0, 0, 0, 1, 0, 0, 0, 11, 0, 0, 17, 49, 174, 102, 75, 224, 97, 3, 163, 25, 54, 164, 224, 111, 25, 2, 128, 0, 255, 89, 213, 174, 67, 224, 65, 8, 137, 228, 96, 166, 224, 79, 52, 66, 128, 0, 255, 164, 37, 1, 255, 0, 0, 0, 0, 166, 70, 26, 81, 224, 97, 5, 150, 188, 85, 77, 224, 111, 27, 3, 128, 1, 255, 29, 208, 201, 152, 224, 65, 10, 165, 133, 110, 72, 224, 79, 38, 67, 128, 1, 255, 5, 12, 0, 0, 241, 253, 40, 109, 224, 97, 7, 186, 221, 91, 163, 224, 111, 29, 4, 129, 1, 255, 51, 119, 156, 139, 224, 65, 12, 144, 32, 13, 161, 224, 79, 24, 68, 129, 1, 255, 72, 165, 196, 223, 224, 97, 9, 189, 240, 227, 68, 224, 111, 47, 16, 129, 1, 255, 1, 0, 0, 0, 216, 217, 112, 117, 224, 65, 14, 188, 65, 3, 79, 224, 79, 42, 80, 129, 1, 255, 26, 0, 0, 0, 52, 141, 63, 222, 224, 97, 11, 145, 145, 237, 170, 224, 111, 33, 17, 129, 1, 255, 6, 8, 0, 0, 213, 107, 220, 226, 224, 65, 0, 187, 108, 187, 168], [224, 79, 28, 81, 129, 1, 255, 37, 164, 131, 77, 224, 97, 13, 164, 52, 142, 67]] 32 | packets2 = [[224, 33, 1, 138, 55, 65, 186, 224, 163, 3, 111, 172, 250, 93, 224, 166, 5, 31, 253, 238, 201, 0, 224, 0, 33, 1, 1, 138, 55, 0, 65, 186]] 33 | # framework card 34 | packets3 = [[192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 1, 0, 190, 35, 194, 88, 224, 130, 16, 41, 164, 128, 18, 249, 127, 43, 73, 224, 97, 1, 143, 120, 56, 74, 224, 163, 3, 111, 172, 250, 93, 224, 1, 2, 146, 66, 204, 182], [224, 166, 5, 31, 253, 238, 201, 224, 1, 4, 167, 231, 175, 95], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [224, 175, 23, 1, 160, 0, 255, 130, 156, 142, 199, 224, 1, 6, 139, 134, 161, 177, 224, 143, 82, 65, 160, 0, 255, 172, 50, 0, 108, 0, 0, 0, 0, 0, 0, 3, 0, 24, 0, 0, 0, 47, 142, 158, 155, 224, 97, 3, 163, 25, 54, 164, 224, 175, 25, 2, 160, 0, 255, 13, 141, 11, 106, 224, 1, 8, 140, 171, 25, 86, 224, 143, 36, 66, 160, 0, 255, 0, 0, 1, 255, 235, 230, 247, 249, 224, 97, 5, 150, 188, 85, 77, 224, 175, 27, 3, 160, 1, 255, 73, 136, 108, 177, 224, 1, 10, 160, 202, 23, 184, 224, 143, 38, 67, 160, 1, 255, 5, 16, 0, 0, 216, 144, 22, 207, 224, 97, 7, 186, 221, 91, 163, 224, 175, 29, 4, 161, 1, 255, 103, 47, 57, 162, 224, 1, 12, 149, 111, 116, 81, 224, 143, 24, 68, 161, 1, 255, 28, 253, 97, 246, 224, 175, 47, 16, 161, 1, 255, 1, 0, 0, 0, 229, 238, 114, 194, 224, 1, 14, 185, 14, 122, 191]] 35 | packets4 = [[192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 1, 0, 190, 35, 194, 88, 224, 130, 16, 41, 164, 128, 18, 249, 127, 43, 73, 224, 97, 1, 143, 120, 56, 74, 224, 163, 3, 111, 172, 250, 93, 224, 1, 2, 146, 66, 204, 182], [224, 166, 5, 31, 253, 238, 201, 224, 1, 4, 167, 231, 175, 95], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [224, 175, 23, 1, 160, 0, 255, 130, 156, 142, 199, 224, 1, 6, 139, 134, 161, 177, 224, 143, 82, 65, 160, 0, 255, 172, 50, 0, 108, 0, 0, 0, 0, 0, 0, 3, 0, 24, 0, 0, 0, 47, 142, 158, 155, 224, 97, 3, 163, 25, 54, 164, 224, 175, 25, 2, 160, 0, 255, 13, 141, 11, 106, 224, 1, 8, 140, 171, 25, 86, 224, 143, 36, 66, 160, 0, 255, 0, 0, 1, 255, 235, 230, 247, 249], [224, 97, 5, 150, 188, 85, 77, 224, 175, 27, 3, 160, 1, 255, 73, 136, 108, 177, 224, 1, 10, 160, 202, 23, 184, 224, 143, 38, 67, 160, 1, 255, 5, 16, 0, 0, 216, 144, 22, 207, 224, 97, 7, 186, 221, 91, 163, 224, 175, 29, 4, 161, 1, 255, 103, 47, 57, 162, 224, 1, 12, 149, 111, 116, 81, 224, 143, 24, 68, 161, 1, 255, 28, 253, 97, 246, 224, 1, 14, 185, 14, 122, 191, 224, 97, 11, 145, 145, 237, 170], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 1, 0, 190, 35, 194, 88, 224, 130, 16, 41, 164, 128, 18, 249, 127, 43, 73, 224, 97, 1, 143, 120, 56, 74, 224, 163, 3, 111, 172, 250, 93, 224, 1, 2, 146, 66, 204, 182], [224, 166, 5, 31, 253, 238, 201, 224, 1, 4, 167, 231, 175, 95], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [224, 175, 23, 1, 160, 0, 255, 130, 156, 142, 199, 224, 1, 6, 139, 134, 161, 177, 224, 143, 82, 65, 160, 0, 255, 172, 50, 0, 108, 0, 0, 0, 0, 0, 0, 3, 0, 24, 0, 0, 0, 47, 142, 158, 155, 224, 97, 3, 163, 25, 54, 164, 224, 175, 25, 2, 160, 0, 255, 13, 141, 11, 106, 224, 1, 8, 140, 171, 25, 86, 224, 143, 36, 66, 160, 0, 255, 0, 0, 1, 255, 235, 230, 247, 249, 224, 97, 5, 150, 188, 85, 77, 224, 175, 27, 3, 160, 1, 255, 73, 136, 108, 177, 224, 1, 10, 160, 202, 23, 184, 224, 143, 38, 67, 160, 1, 255, 5, 16, 0, 0, 216, 144, 22, 207, 224, 97, 7, 186, 221, 91, 163, 224, 175, 29, 4, 161, 1, 255, 103, 47, 57, 162, 224, 1, 12, 149, 111, 116, 81, 224, 143, 24, 68, 161, 1, 255, 28, 253, 97, 246, 224, 175, 47, 16, 161, 1, 255, 1, 0, 0, 0, 229, 238, 114, 194, 224, 1, 14, 185, 14, 122, 191]] 36 | # larger usb hub connected through separate cable 37 | packets5 = [[192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 65, 1, 45, 92, 188, 223, 192, 143, 81, 65, 160, 0, 255, 34, 5, 96, 28, 67, 9, 0, 0, 144, 1, 23, 10, 67, 38, 10, 17, 144, 140, 83, 42, 192, 65, 0, 187, 108, 187, 168, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 65, 0, 187, 108, 187, 168, 224, 130, 16, 44, 177, 4, 19, 137, 131, 240, 76, 224, 97, 1, 143, 120, 56, 74, 224, 163, 3, 111, 172, 250, 93, 224, 65, 2, 151, 13, 181, 70], [224, 166, 5, 31, 253, 238, 201, 224, 65, 4, 162, 168, 214, 175], [224, 175, 23, 1, 160, 0, 255, 130, 156, 142, 199, 224, 65, 6, 142, 201, 216, 65, 224, 143, 82, 65, 160, 0, 255, 92, 29, 128, 109, 70, 66, 15, 0, 1, 7, 2, 113, 217, 0, 0, 17, 78, 127, 126, 152, 224, 97, 3, 163, 25, 54, 164, 224, 175, 25, 2, 160, 0, 255, 13, 141, 11, 106, 224, 65, 8, 137, 228, 96, 166, 224, 143, 36, 66, 160, 0, 255, 0, 0, 1, 255, 235, 230, 247, 249, 224, 97, 5, 150, 188, 85, 77], [192, 143, 18, 2, 160, 0, 255, 42, 185, 119, 26, 192, 65, 3, 1, 61, 178, 49, 192, 143, 49, 66, 160, 0, 255, 135, 128, 180, 4, 0, 0, 0, 0, 135, 24, 234, 31, 192, 65, 0, 187, 108, 187, 168, 224, 175, 27, 3, 160, 1, 255, 73, 136, 108, 177, 224, 65, 10, 165, 133, 110, 72, 224, 143, 38, 67, 160, 1, 255, 69, 0, 12, 0, 153, 36, 145, 228, 224, 97, 7, 186, 221, 91, 163, 224, 175, 29, 4, 161, 1, 255, 103, 47, 57, 162, 224, 65, 12, 144, 32, 13, 161, 224, 143, 24, 68, 161, 1, 255, 28, 253, 97, 246, 224, 97, 9, 189, 240, 227, 68, 224, 175, 47, 16, 161, 1, 255, 1, 0, 0, 0, 229, 238, 114, 194, 224, 65, 14, 188, 65, 3, 79, 224, 143, 42, 80, 161, 1, 255, 26, 0, 0, 0, 9, 186, 61, 105, 224, 97, 11, 145, 145, 237, 170], [224, 175, 33, 17, 161, 1, 255, 6, 8, 0, 0, 232, 92, 222, 85, 224, 65, 0, 187, 108, 187, 168, 224, 143, 28, 81, 161, 1, 255, 113, 252, 38, 100, 224, 97, 13, 164, 52, 142, 67], [224, 143, 46, 6, 161, 1, 255, 154, 0, 0, 0, 127, 167, 193, 74, 224, 97, 15, 136, 85, 128, 173], [224, 139, 0, 127, 83, 174, 153, 224, 97, 1, 143, 120, 56, 74, 224, 163, 3, 111, 172, 250, 93, 224, 65, 2, 151, 13, 181, 70, 224, 134, 2, 30, 76, 14, 194, 224, 97, 3, 163, 25, 54, 164], [224, 138, 4, 39, 166, 216, 135, 224, 97, 5, 150, 188, 85, 77, 224, 163, 5, 90, 9, 153, 180, 224, 65, 4, 162, 168, 214, 175], [224, 166, 6, 165, 172, 231, 80, 224, 65, 6, 142, 201, 216, 65], [224, 134, 7, 145, 184, 100, 178, 224, 97, 6, 44, 237, 92, 212, 192, 141, 0, 249, 244, 244, 207, 192, 65, 1, 45, 92, 188, 223, 192, 131, 1, 225, 233, 112, 38, 192, 65, 0, 187, 108, 187, 168], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 65, 3, 1, 61, 178, 49, 192, 143, 83, 65, 160, 0, 255, 34, 5, 96, 28, 67, 9, 0, 0, 144, 1, 23, 10, 67, 38, 10, 17, 237, 141, 151, 206, 192, 65, 2, 151, 13, 181, 70], [224, 129, 81, 44, 145, 1, 46, 44, 209, 2, 0, 44, 193, 3, 0, 44, 177, 4, 0, 244, 65, 6, 0, 42, 247, 108, 228, 224, 97, 0, 25, 72, 63, 61, 224, 162, 16, 244, 209, 135, 82, 103, 102, 30, 241, 224, 65, 1, 45, 92, 188, 223, 224, 131, 3, 205, 136, 126, 200, 224, 97, 2, 53, 41, 49, 211], [224, 134, 5, 189, 217, 106, 92, 224, 97, 4, 0, 140, 82, 58, 224, 168, 2, 50, 69, 9, 201, 224, 65, 3, 1, 61, 178, 49, 224, 132, 23, 44, 145, 1, 46, 48, 66, 219, 53, 224, 97, 6, 44, 237, 92, 212, 224, 171, 4, 196, 179, 71, 11, 224, 65, 5, 52, 152, 209, 216, 224, 131, 9, 211, 97, 171, 40, 224, 97, 8, 43, 192, 228, 51, 224, 166, 6, 165, 172, 231, 80, 224, 65, 7, 24, 249, 223, 54], [224, 143, 43, 6, 161, 1, 255, 154, 1, 0, 0, 7, 136, 148, 1, 224, 97, 10, 7, 161, 234, 221, 224, 143, 45, 6, 161, 1, 255, 154, 1, 0, 0, 141, 241, 142, 114, 224, 97, 12, 50, 4, 137, 52, 224, 143, 47, 6, 161, 1, 255, 154, 1, 0, 0, 11, 217, 120, 92, 224, 97, 14, 30, 101, 135, 218, 224, 143, 33, 6, 161, 1, 255, 154, 1, 0, 0, 153, 2, 186, 148, 224, 97, 0, 25, 72, 63, 61, 224, 143, 35, 6, 161, 1, 255, 154, 1, 0, 0, 31, 42, 76, 186, 224, 97, 2, 53, 41, 49, 211], [224, 143, 37, 6, 161, 1, 255, 154, 1, 0, 0, 149, 83, 86, 201, 224, 97, 4, 0, 140, 82, 58, 224, 143, 39, 6, 161, 1, 255, 154, 1, 0, 0, 19, 123, 160, 231, 224, 97, 6, 44, 237, 92, 212, 224, 143, 41, 6, 161, 1, 255, 154, 1, 0, 0, 129, 160, 98, 47, 224, 97, 8, 43, 192, 228, 51, 224, 143, 43, 6, 161, 1, 255, 154, 1, 0, 0, 7, 136, 148, 1, 224, 97, 10, 7, 161, 234, 221], [224, 143, 45, 6, 161, 1, 255, 154, 1, 0, 0, 141, 241, 142, 114, 224, 97, 12, 50, 4, 137, 52], [224, 143, 47, 6, 161, 1, 255, 154, 1, 0, 0, 11, 217, 120, 92, 224, 97, 14, 30, 101, 135, 218], [224, 143, 33, 6, 161, 1, 255, 154, 1, 0, 0, 153, 2, 186, 148, 224, 97, 0, 25, 72, 63, 61, 224, 143, 35, 6, 161, 1, 255, 154, 1, 0, 0, 31, 42, 76, 186, 224, 97, 2, 53, 41, 49, 211, 224, 143, 37, 6, 161, 1, 255, 154, 1, 0, 0, 149, 83, 86, 201, 224, 97, 4, 0, 140, 82, 58, 224, 143, 39, 6, 161, 1, 255, 154]] 38 | # framework hdmi card 39 | packets6 = [[192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [224, 161, 17, 44, 145, 1, 39, 177, 155, 38, 148, 224, 1, 0, 190, 35, 194, 88, 224, 130, 16, 68, 16, 129, 18, 170, 55, 43, 91, 224, 97, 1, 143, 120, 56, 74, 224, 163, 3, 111, 172, 250, 93, 224, 1, 2, 146, 66, 204, 182], [224, 166, 5, 31, 253, 238, 201, 224, 1, 4, 167, 231, 175, 95], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66, 192, 143, 24, 1, 160, 0, 255, 101, 14, 114, 66], [192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56, 192, 143, 26, 1, 160, 0, 255, 5, 93, 178, 56], [192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183, 192, 143, 28, 1, 160, 0, 255, 165, 168, 242, 183], [192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205, 192, 143, 30, 1, 160, 0, 255, 197, 251, 50, 205], [192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114, 192, 143, 16, 1, 160, 0, 255, 164, 69, 2, 114], [192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8, 192, 143, 18, 1, 160, 0, 255, 196, 22, 194, 8], [192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135, 192, 143, 20, 1, 160, 0, 255, 100, 227, 130, 135], [192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253, 192, 143, 22, 1, 160, 0, 255, 4, 176, 66, 253], [224, 175, 23, 1, 160, 0, 255, 130, 156, 142, 199, 224, 1, 6, 139, 134, 161, 177, 224, 143, 82, 65, 160, 0, 255, 172, 50, 0, 108, 0, 0, 0, 0, 0, 0, 2, 0, 24, 0, 0, 0, 138, 93, 194, 80, 224, 97, 3, 163, 25, 54, 164, 224, 175, 25, 2, 160, 0, 255, 13, 141, 11, 106, 224, 1, 8, 140, 171, 25, 86, 224, 143, 36, 66, 160, 0, 255, 0, 0, 1, 255, 235, 230, 247, 249, 224, 97, 5, 150, 188, 85, 77, 224, 175, 27, 3, 160, 1, 255, 73, 136, 108, 177, 224, 1, 10, 160, 202, 23, 184, 224, 143, 38, 67, 160, 1, 255, 5, 4, 0, 0, 116, 155, 57, 212, 224, 97, 7, 186, 221, 91, 163, 224, 175, 29, 4, 161, 1, 255, 103, 47, 57, 162, 224, 1, 12, 149, 111, 116, 81, 224, 143, 24, 68, 161, 1, 255, 28, 253, 97, 246, 224, 175, 47, 16, 161, 1, 255, 1, 0, 0, 0, 229, 238, 114, 194, 224, 1, 14, 185, 14, 122, 191, 224, 175, 33, 17, 161, 1, 255, 6, 4, 0, 0, 140, 165, 196, 92, 224, 1, 0, 190, 35, 194, 88, 224, 143, 28, 81, 161, 1, 255, 113, 252, 38, 100, 224, 97, 13, 164, 52, 142, 67]] 40 | 41 | packets_pos = [ 0,0 ] # hoot-hoot,,, hewwo ;-P 42 | 43 | def gb(): 44 | fun = postfactum_readout if replay else fusb.get_rxb 45 | return stacc.show_msg(stacc.get_message(fun)) 46 | 47 | def gba(): 48 | # not quite working well atm, sowwy (also, mood) 49 | while True: 50 | try: 51 | gb(); print() 52 | except IndexError: 53 | return 54 | except Exception as e: 55 | raise e 56 | break 57 | 58 | def postfactum_readout(length=80): 59 | # A function that helps read data out of our own capture buffer instead of using the FUSB's internal buffer 60 | # so, it pretends to be the FUSB FIFO read function, for parsing packets that are recorded into `packets` 61 | err = 0 62 | response = [] 63 | while len(response) < length: 64 | # ran out of data? this ends here 65 | if packets_pos[0] == len(packets)-1 and packets_pos[1] == len(packets[packets_pos[0]])-1: 66 | # buffer underflow, returning the unfinished buffer with zeroes in the end, just like the FUSB does 67 | response = [0]*(length-len(response)) 68 | return bytes(response) 69 | # we still got data to add! 70 | # is the current buffer enough? 71 | remainder = length - len(response) 72 | current_packet = packets[packets_pos[0]] 73 | current_packet_end = current_packet[packets_pos[1]:] 74 | while remainder > 0: 75 | chunk_len = min(len(current_packet_end), remainder) 76 | response += current_packet_end[:chunk_len] 77 | remainder -= chunk_len 78 | packets_pos[1] += chunk_len 79 | # now, checking for overflow 80 | if packets_pos[1] >= len(current_packet)-1: 81 | # sanity check - this should not happen 82 | if packets_pos[1] > len(current_packet): 83 | print("Alert, overcount!", packets_pos, len(current_packet_end), chunk_len) 84 | # do we need to go to the next packet? 85 | if len(packets)-1 <= packets_pos[0]: 86 | # next packet doesn't exist lol 87 | print("We ran out of packet") 88 | err += 1 # malformed packets cause this function to glitch and loop infinitely, hence the error counter 89 | if err == 4: 90 | return bytes(response) # lol gave up here 91 | else: 92 | err = 0 93 | packets_pos[0] += 1 94 | packets_pos[1] = 0 95 | current_packet = packets[packets_pos[0]] 96 | current_packet_end = current_packet[packets_pos[1]:] 97 | return bytes(response) 98 | 99 | ######################## 100 | # 101 | # it gets real here 102 | # 103 | ######################## 104 | 105 | listen_cc = 2 106 | 107 | stacc.init_fusb() 108 | stacc.setup_listen(listen_cc) 109 | 110 | if not replay: 111 | stacc.flow_record(packets=packets) 112 | else: 113 | packets = packets1; gba() 114 | -------------------------------------------------------------------------------- /source_example.py: -------------------------------------------------------------------------------- 1 | from machine import Pin, I2C, ADC 2 | from time import sleep 3 | 4 | from fusb302 import FUSB302 5 | from pdstacc import PDStacc 6 | 7 | ######################## 8 | # 9 | # Hardware Stuff 10 | # 11 | ######################## 12 | 13 | # non-documented hardware stuff for a specific board I got going on 14 | # glad I've gotten to that point ^~^ 15 | p_5_a = Pin(2, Pin.OUT, value=0) 16 | p_5_m = Pin(7, Pin.OUT, value=0) 17 | p_vin = Pin(3, Pin.OUT, value=0) 18 | p_discharge = Pin(6, Pin.OUT, value=0) 19 | p_5_m.off() 20 | p_5_a.off() 21 | p_vin.off() 22 | p_discharge.on() 23 | p_led_1 = Pin(9, Pin.OUT, value=0) 24 | p_led_2 = Pin(15, Pin.OUT, value=0) 25 | 26 | i2c = I2C(sda=Pin(18), scl=Pin(19), id=1, freq=400000) 27 | print(i2c.scan()) 28 | 29 | a = ADC(Pin(28)) 30 | print(a.read_u16()) 31 | 32 | int_p = Pin(20, Pin.IN, Pin.PULL_UP) 33 | 34 | fusb = FUSB302(i2c, int_p=int_p) 35 | stacc = PDStacc(fusb) 36 | 37 | def get_adc_vbus(): 38 | return (3.3*11*a.read_u16())/65536 39 | 40 | print(get_adc_vbus(), "V") 41 | 42 | def source_sanity_check(): 43 | # sanity check 44 | # currently, both 5V and VIN pins are supposed to be shut off 45 | vbus_v = get_adc_vbus() 46 | if vbus_v > 1: 47 | # enable discharge FET and wait for VBUS to discharge 48 | print("VBUS is at {}, has to be discharged".format(vbus_v)) 49 | p_discharge.on() 50 | sleep(0.3) 51 | vbus_v = get_adc_vbus() 52 | if vbus_v > 1: 53 | # blink and enter error state 54 | # maybe a FET is borked, maybe something else 55 | ## remove all pullups and pulldowns? TODO 56 | print("VBUS is still at {}, can't be discharged".format(vbus_v)) 57 | while True: 58 | p_led_1.toggle() 59 | sleep(0.3) 60 | # infinite loop; TODO: add checks in case of longer discharge 61 | else: 62 | print("VBUS is at {}".format(vbus_v)) 63 | 64 | source_sanity_check() 65 | 66 | def set_power_rail(rail): 67 | rail = rail.lower() 68 | p_led_1.off(); p_led_2.off() 69 | if rail == "off": 70 | p_5_m.off() 71 | p_5_a.off() 72 | p_vin.off() 73 | p_discharge.on() 74 | elif rail == "5v": 75 | p_vin.off() 76 | p_discharge.on() 77 | p_5_a.on() 78 | p_5_m.on() 79 | p_discharge.off() 80 | elif rail == "vin": 81 | p_5_m.off() 82 | p_discharge.off() 83 | p_5_a.on() 84 | p_vin.on() 85 | p_5_a.off() 86 | p_led_1.on(); p_led_2.off() 87 | else: 88 | # catch-all: 89 | p_5_m.off() 90 | p_5_a.off() 91 | p_vin.off() 92 | p_discharge.on() 93 | raise Exception("rail has to be one of 'off', '5v' or 'vin', was '{}'".format(rail)) 94 | 95 | # callbacks for main loop 96 | 97 | def validate_profile(profile, d): 98 | if profile not in range(len(psu_advertisement)): 99 | set_power_rail('off') 100 | else: 101 | return True 102 | 103 | def switch_to_profile(profile, d): 104 | if profile == 0: 105 | set_power_rail('5V') 106 | elif profile == 1: 107 | set_power_rail('VIN') 108 | 109 | def en_5v_power_rail(): 110 | set_power_rail('5V') 111 | 112 | stacc.validate_profile_cb = validate_profile 113 | stacc.switch_to_profile_cb = switch_to_profile 114 | stacc.en_5v_power_rail_cb = en_5v_power_rail 115 | 116 | psu_advertisement = stacc.create_pdo('fixed', 5000, 1500, 0, 8) + \ 117 | stacc.create_pdo('fixed', 19000, 5000, 0, 0) 118 | 119 | ######################## 120 | # 121 | # it gets real here 122 | # 123 | ######################## 124 | 125 | stacc.init_fusb() 126 | while True: 127 | try: 128 | set_power_rail('off') 129 | stacc.setup_source() 130 | stacc.flow_source() 131 | # after flow_source exits, setup_source runs again 132 | # this would best work as an actual explicit state machine 133 | except KeyboardInterrupt: 134 | # lets you exit the loop on ctrlc 135 | print("CtrlC again to exit") 136 | sleep(1) 137 | --------------------------------------------------------------------------------