├── .gitignore ├── CMakeLists.txt ├── LICENSE.txt ├── README.md ├── crc.c ├── crc.h ├── lightning_rx.pio ├── lightning_tx.pio ├── main.c ├── media ├── lightning.png ├── pinout.png └── tamarin-logo-300.png ├── pico_sdk_import.cmake ├── probe.pio ├── tamarin_config.h ├── tamarin_probe.c ├── tamarin_probe.h ├── tusb_config.h ├── usb_descriptors.c ├── util.c └── util.h /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /build 3 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | # initialize the SDK based on PICO_SDK_PATH 3 | # note: this must happen before project() 4 | include(pico_sdk_import.cmake) 5 | project(tamarin_firmware) 6 | 7 | # set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb") 8 | 9 | pico_sdk_init() 10 | 11 | add_executable(tamarin_firmware) 12 | 13 | # target_compile_definitions(tamarin_firmware 14 | # PUBLIC PICO_UART_ENABLE_CRLF_SUPPORT=0 15 | # PUBLIC PICO_STDIO_ENABLE_CRLF_SUPPORT=0 16 | # PUBLIC PICO_STDIO_DEFAULT_CRLF=0 17 | # ) 18 | 19 | target_compile_options(tamarin_firmware INTERFACE -DPICO_ENTER_USB_BOOT_ON_EXIT=1) 20 | 21 | 22 | pico_generate_pio_header(tamarin_firmware ${CMAKE_CURRENT_LIST_DIR}/lightning_tx.pio) 23 | pico_generate_pio_header(tamarin_firmware ${CMAKE_CURRENT_LIST_DIR}/lightning_rx.pio) 24 | pico_generate_pio_header(tamarin_firmware ${CMAKE_CURRENT_LIST_DIR}/probe.pio) 25 | 26 | # Required so the build finds tusb_config.h 27 | target_include_directories(tamarin_firmware PRIVATE ${CMAKE_CURRENT_LIST_DIR}) 28 | 29 | target_sources(tamarin_firmware PRIVATE 30 | tamarin_probe.c 31 | main.c 32 | usb_descriptors.c 33 | util.c 34 | crc.c 35 | ) 36 | 37 | target_link_libraries(tamarin_firmware PRIVATE pico_stdlib tinyusb_device tinyusb_board pico_stdio hardware_i2c pico_multicore pico_time pico_sync hardware_pio) 38 | pico_add_extra_outputs(tamarin_firmware) 39 | 40 | # enable usb output, disable uart output 41 | #pico_enable_stdio_usb(tamarin_firmware 1) 42 | #pico_enable_stdio_uart(tamarin_firmware 0) 43 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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 | . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tamarin Firmware 2 | ![Tamarin Logo](https://github.com/stacksmashing/tamarin-firmware/blob/main/media/tamarin-logo-300.png?raw=true) 3 | 4 | ## PICO SDK NOTE 5 | 6 | Please build with the Pico-SDK Version 4fe995d0ec984833a7ea9c33bac5c67a53c04178 7 | 8 | Newer versions have some USB incompatibility. 9 | 10 | ## Build 11 | 12 | ``` 13 | mkdir build 14 | cd build 15 | cmake .. 16 | make 17 | ``` 18 | 19 | ## Hooking it up 20 | 21 | ![Pinout diagram](https://github.com/stacksmashing/tamarin-firmware/blob/main/media/pinout.png?raw=true) 22 | 23 | With this cable, connect: 24 | 25 | - [GP0] L1n (Purple) 26 | - [GP1] L1p (Orange) 27 | - [GND] GND (Black) 28 | - [GP2] ID1 (Blue) 29 | - [GP3] ID0 (Yellow) 30 | - [VBUS] 5V (Red) 31 | 32 | Note: The colors might be different for your cable. I recommend checking the pinout using a voltmeter. 33 | 34 | ![Lightning](https://github.com/stacksmashing/tamarin-firmware/blob/main/media/lightning.png?raw=true) 35 | 36 | Another cable was observed to have the following pinout: 37 | 38 | - [GP0] L1n (Green) 39 | - [GP1] L1p (White) 40 | - [GND] GND (Black) 41 | - [GP2] ID1 (Orange) 42 | - [GP3] ID0 (Red) 43 | - [VBUS] 5V (Yellow) 44 | 45 | If you would like to connect to your device over USB, cut a USB cable and connect its wires like this: 46 | - USB cable D+ (green) -> L0p (color depends on your cable) 47 | - USB cable D- (white) -> L0n (color depends on your cable) 48 | - USB cable GND (black) -> GND 49 | 50 | ## Usage 51 | 52 | Tamarin Cable provides three USB endpoints, of which two are serial ports. 53 | 54 | Serial port 1 is the control serial port, use it to configure DCSD/JTAG mode. 55 | 56 | Serial port 2 is the DCSD port, when Tamarin Cable is in DCSD mode the serial output will be provided here. 57 | 58 | ### OpenOCD 59 | 60 | To use Tamarin as a JTAG adapter you need to use our [OpenOCD fork](https://github.com/stacksmashing/openocd) that includes support for the Tamarin probe. 61 | 62 | To enable JTAG on production iPhones they need to be demoted. For checkm8 vulnerable iPhones this can be done using [ipwndfu](https://github.com/axi0mX/ipwndfu). 63 | 64 | Once the phone is successfully demoted the [bonobo configs](https://github.com/lambdaconcept/bonobo-configs/blob/master/t8015.cfg) can be used to connect to the iPhone like so: 65 | 66 | ``` 67 | openocd -f interface/tamarin.cfg -f t8015.cfg 68 | ``` 69 | 70 | ## Known issues 71 | 72 | 1. Commands are unavailable in JTAG mode. Workaround: Enter the desired command and then reconnect the device. To reset the device you can also use JTAG. 73 | 2. JTAG is not re-enabled after manual device reset. Workaround: Run the JTAG command again, then reconnect the device (or the Tamarin cable). 74 | -------------------------------------------------------------------------------- /crc.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * Functions and types for CRC checks. 4 | * 5 | * Generated on Mon May 1 14:15:47 2023 6 | * by pycrc v0.10.0, https://pycrc.org 7 | * using the configuration: 8 | * - Width = 8 9 | * - Poly = 0x31 10 | * - XorIn = 0xff 11 | * - ReflectIn = True 12 | * - XorOut = 0x00 13 | * - ReflectOut = True 14 | * - Algorithm = table-driven 15 | */ 16 | #include "crc.h" /* include the header file generated with pycrc */ 17 | #include 18 | #include 19 | 20 | 21 | 22 | /** 23 | * Static table used for the table_driven implementation. 24 | */ 25 | static const crc_t crc_table[256] = { 26 | 0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83, 0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41, 27 | 0x9d, 0xc3, 0x21, 0x7f, 0xfc, 0xa2, 0x40, 0x1e, 0x5f, 0x01, 0xe3, 0xbd, 0x3e, 0x60, 0x82, 0xdc, 28 | 0x23, 0x7d, 0x9f, 0xc1, 0x42, 0x1c, 0xfe, 0xa0, 0xe1, 0xbf, 0x5d, 0x03, 0x80, 0xde, 0x3c, 0x62, 29 | 0xbe, 0xe0, 0x02, 0x5c, 0xdf, 0x81, 0x63, 0x3d, 0x7c, 0x22, 0xc0, 0x9e, 0x1d, 0x43, 0xa1, 0xff, 30 | 0x46, 0x18, 0xfa, 0xa4, 0x27, 0x79, 0x9b, 0xc5, 0x84, 0xda, 0x38, 0x66, 0xe5, 0xbb, 0x59, 0x07, 31 | 0xdb, 0x85, 0x67, 0x39, 0xba, 0xe4, 0x06, 0x58, 0x19, 0x47, 0xa5, 0xfb, 0x78, 0x26, 0xc4, 0x9a, 32 | 0x65, 0x3b, 0xd9, 0x87, 0x04, 0x5a, 0xb8, 0xe6, 0xa7, 0xf9, 0x1b, 0x45, 0xc6, 0x98, 0x7a, 0x24, 33 | 0xf8, 0xa6, 0x44, 0x1a, 0x99, 0xc7, 0x25, 0x7b, 0x3a, 0x64, 0x86, 0xd8, 0x5b, 0x05, 0xe7, 0xb9, 34 | 0x8c, 0xd2, 0x30, 0x6e, 0xed, 0xb3, 0x51, 0x0f, 0x4e, 0x10, 0xf2, 0xac, 0x2f, 0x71, 0x93, 0xcd, 35 | 0x11, 0x4f, 0xad, 0xf3, 0x70, 0x2e, 0xcc, 0x92, 0xd3, 0x8d, 0x6f, 0x31, 0xb2, 0xec, 0x0e, 0x50, 36 | 0xaf, 0xf1, 0x13, 0x4d, 0xce, 0x90, 0x72, 0x2c, 0x6d, 0x33, 0xd1, 0x8f, 0x0c, 0x52, 0xb0, 0xee, 37 | 0x32, 0x6c, 0x8e, 0xd0, 0x53, 0x0d, 0xef, 0xb1, 0xf0, 0xae, 0x4c, 0x12, 0x91, 0xcf, 0x2d, 0x73, 38 | 0xca, 0x94, 0x76, 0x28, 0xab, 0xf5, 0x17, 0x49, 0x08, 0x56, 0xb4, 0xea, 0x69, 0x37, 0xd5, 0x8b, 39 | 0x57, 0x09, 0xeb, 0xb5, 0x36, 0x68, 0x8a, 0xd4, 0x95, 0xcb, 0x29, 0x77, 0xf4, 0xaa, 0x48, 0x16, 40 | 0xe9, 0xb7, 0x55, 0x0b, 0x88, 0xd6, 0x34, 0x6a, 0x2b, 0x75, 0x97, 0xc9, 0x4a, 0x14, 0xf6, 0xa8, 41 | 0x74, 0x2a, 0xc8, 0x96, 0x15, 0x4b, 0xa9, 0xf7, 0xb6, 0xe8, 0x0a, 0x54, 0xd7, 0x89, 0x6b, 0x35 42 | }; 43 | 44 | 45 | crc_t crc_reflect(crc_t data, size_t data_len) 46 | { 47 | unsigned int i; 48 | crc_t ret; 49 | 50 | ret = data & 0x01; 51 | for (i = 1; i < data_len; i++) { 52 | data >>= 1; 53 | ret = (ret << 1) | (data & 0x01); 54 | } 55 | return ret; 56 | } 57 | 58 | 59 | crc_t crc_update(crc_t crc, const void *data, size_t data_len) 60 | { 61 | const unsigned char *d = (const unsigned char *)data; 62 | unsigned int tbl_idx; 63 | 64 | while (data_len--) { 65 | tbl_idx = crc ^ *d; 66 | crc = (crc_table[tbl_idx] ^ (crc >> 8)) & 0xff; 67 | d++; 68 | } 69 | return crc & 0xff; 70 | } 71 | -------------------------------------------------------------------------------- /crc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * Functions and types for CRC checks. 4 | * 5 | * Generated on Mon May 1 14:15:50 2023 6 | * by pycrc v0.10.0, https://pycrc.org 7 | * using the configuration: 8 | * - Width = 8 9 | * - Poly = 0x31 10 | * - XorIn = 0xff 11 | * - ReflectIn = True 12 | * - XorOut = 0x00 13 | * - ReflectOut = True 14 | * - Algorithm = table-driven 15 | * 16 | * This file defines the functions crc_init(), crc_update() and crc_finalize(). 17 | * 18 | * The crc_init() function returns the initial \c crc value and must be called 19 | * before the first call to crc_update(). 20 | * Similarly, the crc_finalize() function must be called after the last call 21 | * to crc_update(), before the \c crc is being used. 22 | * is being used. 23 | * 24 | * The crc_update() function can be called any number of times (including zero 25 | * times) in between the crc_init() and crc_finalize() calls. 26 | * 27 | * This pseudo-code shows an example usage of the API: 28 | * \code{.c} 29 | * crc_t crc; 30 | * unsigned char data[MAX_DATA_LEN]; 31 | * size_t data_len; 32 | * 33 | * crc = crc_init(); 34 | * while ((data_len = read_data(data, MAX_DATA_LEN)) > 0) { 35 | * crc = crc_update(crc, data, data_len); 36 | * } 37 | * crc = crc_finalize(crc); 38 | * \endcode 39 | */ 40 | #ifndef CRC_H 41 | #define CRC_H 42 | 43 | #include 44 | #include 45 | 46 | #ifdef __cplusplus 47 | extern "C" { 48 | #endif 49 | 50 | 51 | /** 52 | * The definition of the used algorithm. 53 | * 54 | * This is not used anywhere in the generated code, but it may be used by the 55 | * application code to call algorithm-specific code, if desired. 56 | */ 57 | #define CRC_ALGO_TABLE_DRIVEN 1 58 | 59 | 60 | /** 61 | * The type of the CRC values. 62 | * 63 | * This type must be big enough to contain at least 8 bits. 64 | */ 65 | typedef uint_fast8_t crc_t; 66 | 67 | 68 | /** 69 | * Reflect all bits of a \a data word of \a data_len bytes. 70 | * 71 | * \param[in] data The data word to be reflected. 72 | * \param[in] data_len The width of \a data expressed in number of bits. 73 | * \return The reflected data. 74 | */ 75 | crc_t crc_reflect(crc_t data, size_t data_len); 76 | 77 | 78 | /** 79 | * Calculate the initial crc value. 80 | * 81 | * \return The initial crc value. 82 | */ 83 | static inline crc_t crc_init(void) 84 | { 85 | return 0xff; 86 | } 87 | 88 | 89 | /** 90 | * Update the crc value with new data. 91 | * 92 | * \param[in] crc The current crc value. 93 | * \param[in] data Pointer to a buffer of \a data_len bytes. 94 | * \param[in] data_len Number of bytes in the \a data buffer. 95 | * \return The updated crc value. 96 | */ 97 | crc_t crc_update(crc_t crc, const void *data, size_t data_len); 98 | 99 | 100 | /** 101 | * Calculate the final crc value. 102 | * 103 | * \param[in] crc The current crc value. 104 | * \return The final crc value. 105 | */ 106 | static inline crc_t crc_finalize(crc_t crc) 107 | { 108 | return crc; 109 | } 110 | 111 | 112 | #ifdef __cplusplus 113 | } /* closing brace for extern "C" */ 114 | #endif 115 | 116 | #endif /* CRC_H */ 117 | -------------------------------------------------------------------------------- /lightning_rx.pio: -------------------------------------------------------------------------------- 1 | .program lightning_rx 2 | 3 | ; 1 NOP is 0.5us 4 | 5 | .side_set 1 opt 6 | receive: 7 | 8 | ; Set pin to input 9 | SET PINDIRS 0 side 0 10 | MOV ISR NULL 11 | 12 | receive_next_byte: 13 | MOV ISR NULL 14 | ; Set data counter 15 | SET X 7 16 | 17 | data_loop: 18 | ; Now we wait for the falling flank at the start of the bit 19 | ; Then we wait 3us 20 | WAIT 0 PIN 0 side 1 [5] 21 | ; Read (inverted!!) bit 22 | ; We wait 4us so we are at the position of either the finishing flank OR the stop bit 23 | IN PINS 1 side 0 [7] 24 | NOP 25 | ; if this is NOT a stop bit we continue receive 26 | JMP PIN continue_receive 27 | ; if it IS a stop bit, we restart! 28 | WAIT 1 PIN 0 side 1 29 | NOP 30 | JMP receive_next_byte side 0 31 | continue_receive: 32 | WAIT 1 PIN 0 side 1 33 | JMP X-- data_loop side 0 34 | 35 | ; if we get here we have received 8 bits, time to push! 36 | PUSH NOBLOCK 37 | JMP receive_next_byte 38 | 39 | % c-sdk { 40 | static inline pio_sm_config lightning_rx_program_init(PIO pio, uint sm, uint offset, uint pin, float clkdiv) { 41 | 42 | 43 | //pio_sm_set_pins_with_mask(pio, sm, 1u << pin_rx, 1u << pin_rx); 44 | pio_sm_set_pindirs_with_mask(pio, sm, 1u << 19, 1u << 19); 45 | pio_sm_set_pindirs_with_mask(pio, sm, 1u << 19, 0u << pin); 46 | pio_gpio_init(pio, 19); 47 | 48 | 49 | pio_sm_config c = lightning_rx_program_get_default_config(offset); 50 | 51 | // Map the state machine's OUT pin group to one pin, namely the `pin` 52 | // parameter to this function. 53 | //sm_config_set_out_pins(&c, pin, 1); 54 | sm_config_set_set_pins(&c, pin, 1); 55 | sm_config_set_in_pins(&c, pin); 56 | sm_config_set_jmp_pin(&c, pin); 57 | //pio_sm_set_sideset_pins(pio, sm, 19); 58 | sm_config_set_sideset_pins(&c, 19); 59 | sm_config_set_in_shift(&c, false, false, 32); 60 | sm_config_set_out_shift(&c, true, false, 32); 61 | 62 | // Set this pin's GPIO function (connect PIO to the pad) 63 | pio_gpio_init(pio, pin); 64 | pio_gpio_init(pio, 19); 65 | // Set the pin direction to output at the PIO 66 | //pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, false); 67 | sm_config_set_clkdiv(&c, clkdiv); 68 | 69 | 70 | // Load our configuration, and jump to the start of the program 71 | pio_sm_init(pio, sm, offset, &c); 72 | // Set the state machine running 73 | pio_sm_set_enabled(pio, sm, true); 74 | return c; 75 | } 76 | %} 77 | -------------------------------------------------------------------------------- /lightning_tx.pio: -------------------------------------------------------------------------------- 1 | .program lightning_tx 2 | 3 | ; 1 NOP is 0.5us 4 | 5 | SET PINS 1 6 | SET PINDIRS 1 7 | send: 8 | ; Pull in one byte from OSR 9 | PULL 10 | ; Output counter (reg X) 11 | SET X, 7 12 | 13 | continue_send: 14 | ; Pull 1 bit from OSR 15 | OUT Y 1 16 | 17 | ; if Y is zero we send a (you guessed it) zero 18 | JMP !Y send_zero 19 | 20 | ; otherwise we send a 1 21 | send_one: 22 | SET PINS 0 [3] 23 | SET PINS 1 [11] 24 | JMP send_done 25 | send_zero: 26 | SET PINS 0 [13] 27 | SET PINS 1 [2] 28 | send_done: 29 | JMP X-- continue_send 30 | NOP [14] 31 | PUSH [15] 32 | 33 | 34 | % c-sdk { 35 | static inline pio_sm_config lightning_tx_program_init(PIO pio, uint sm, uint offset, uint pin, float clkdiv) { 36 | 37 | 38 | pio_sm_set_pins_with_mask(pio, sm, 1u << pin, 1u << pin); 39 | pio_sm_set_pindirs_with_mask(pio, sm, 1u << pin, 1u << pin); 40 | //pio_sm_set_pindirs_with_mask(pio, sm, 1u << 19, 0u << pin); 41 | pio_gpio_init(pio, 19); 42 | 43 | 44 | pio_sm_config c = lightning_tx_program_get_default_config(offset); 45 | 46 | // Map the state machine's OUT pin group to one pin, namely the `pin` 47 | // parameter to this function. 48 | sm_config_set_out_pins(&c, pin, 1); 49 | sm_config_set_set_pins(&c, pin, 1); 50 | sm_config_set_in_pins(&c, pin); 51 | sm_config_set_jmp_pin(&c, pin); 52 | //pio_sm_set_sideset_pins(pio, sm, 19); 53 | //sm_config_set_sideset_pins(&c, 19); 54 | 55 | // shift_right = false, auto_push = false 56 | sm_config_set_in_shift(&c, false, false, 32); 57 | // shift_right = true, auto_pull = false 58 | sm_config_set_out_shift(&c, true, false, 32); 59 | 60 | // Set this pin's GPIO function (connect PIO to the pad) 61 | pio_gpio_init(pio, pin); 62 | pio_gpio_init(pio, 19); 63 | // Set the pin direction to output at the PIO 64 | //pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, false); 65 | sm_config_set_clkdiv(&c, clkdiv); 66 | 67 | 68 | // Load our configuration, and jump to the start of the program 69 | pio_sm_init(pio, sm, offset, &c); 70 | // Set the state machine running 71 | pio_sm_set_enabled(pio, sm, true); 72 | return c; 73 | } 74 | %} 75 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include "pico/stdio.h" 10 | #include "pico/multicore.h" 11 | #include "pico/bootrom.h" 12 | 13 | #include "hardware/gpio.h" 14 | #include "hardware/uart.h" 15 | #include "hardware/timer.h" 16 | #include "hardware/watchdog.h" 17 | 18 | #include "lightning_rx.pio.h" 19 | #include "lightning_tx.pio.h" 20 | 21 | #include "bsp/board.h" 22 | #include "tusb.h" 23 | 24 | #include "tamarin_probe.h" 25 | #include "util.h" 26 | #include "crc.h" 27 | 28 | #define PIN_SDQ 3 29 | 30 | volatile bool serialEnabled = false; 31 | volatile bool jtagInited = false; 32 | volatile bool jtagEnabled = false; 33 | volatile bool wantLeaveJtag = false; 34 | 35 | void configure_rx(PIO pio, uint sm) { 36 | pio_sm_set_enabled(pio, sm, false); 37 | pio_clear_instruction_memory(pio); 38 | uint offset = pio_add_program(pio, &lightning_rx_program); 39 | pio_sm_config c = lightning_rx_program_init(pio, sm, offset, PIN_SDQ, 125.0/2.0); 40 | } 41 | 42 | void leave_dcsd() { 43 | serialEnabled = false; 44 | 45 | uart_deinit(uart0); 46 | } 47 | 48 | void leave_jtag() { 49 | jtagEnabled = false; 50 | jtagInited = false; 51 | 52 | tamarin_probe_deinit(); 53 | wantLeaveJtag = false; 54 | } 55 | 56 | void lightning_send_wake() { 57 | gpio_init(PIN_SDQ); 58 | gpio_set_dir(PIN_SDQ, GPIO_OUT); 59 | gpio_put(PIN_SDQ, 0); 60 | 61 | sleep_us(200); 62 | 63 | gpio_set_dir(PIN_SDQ, GPIO_IN); 64 | 65 | sleep_us(50); 66 | } 67 | 68 | void tamarin_reset_tristar(PIO pio, uint sm) { 69 | leave_dcsd(); 70 | wantLeaveJtag = true; 71 | while (wantLeaveJtag); 72 | 73 | lightning_send_wake(); 74 | 75 | configure_rx(pio, sm); 76 | } 77 | 78 | unsigned char reverse_byte(unsigned char b) { 79 | b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; 80 | b = (b & 0xCC) >> 2 | (b & 0x33) << 2; 81 | b = (b & 0xAA) >> 1 | (b & 0x55) << 1; 82 | return b; 83 | } 84 | 85 | enum state { 86 | RESTART_ENUMERATION, 87 | WAITING_FOR_INIT, 88 | READING_TRISTAR_REQUEST, 89 | HANDLE_TRISTAR_REQUEST, 90 | READING_POWER_REQUEST, 91 | HANDLE_POWER_REQUEST, 92 | READING_TRISTAR_UNKNOWN_76, 93 | HANDLE_TRISTAR_UNKNOWN_76, 94 | HANDLE_JTAG, 95 | // Force JTAG mode if the cable is already in JTAG mode 96 | // (e.g. after the tamarin cable was reset but not the device) 97 | FORCE_JTAG, 98 | FORCE_SPAM 99 | }; 100 | 101 | volatile enum state gState = RESTART_ENUMERATION; 102 | 103 | enum command { 104 | CMD_DEFAULT, 105 | CMD_RESET, 106 | CMD_AUTO_DFU, 107 | // Used as 'second stage' for the DFU command. Saves us a state machine. 108 | CMD_INTERNAL_AUTO_DFU_2, 109 | CMD_MAX, 110 | }; 111 | 112 | enum default_command { 113 | DEFAULT_CMD_DCSD = 0, 114 | DEFAULT_CMD_JTAG, 115 | DEFAULT_CMD_SPAM, 116 | DEFAULT_CMD_CHARGING 117 | }; 118 | 119 | // Command that should be sent automatically (on reboot, plugin, etc.) 120 | // Automatically changed when selecting DCSD/JTAG 121 | enum default_command gDefaultCommand = DEFAULT_CMD_DCSD; 122 | 123 | // Next command to send 124 | enum command gCommand = CMD_DEFAULT; 125 | 126 | enum responses { 127 | // JTAG mode 128 | RSP_USB_UART_JTAG, 129 | // JTAG mode 130 | RSP_USB_SPAM_JTAG, 131 | // DCSD mode 132 | RSP_USB_UART, 133 | // Reset 134 | RSP_RESET, 135 | // DFU 136 | RSP_DFU, 137 | RSP_USB_A_CHARGING_CABLE, 138 | RSP_MAX 139 | }; 140 | 141 | enum TRISTAR_REQUESTS { 142 | TRISTAR_POLL = 0x74, 143 | TRISTAR_POWER = 0x70, 144 | TRISTAR_UNKNOWN_76 = 0x76, 145 | }; 146 | 147 | // To generate CRCs: 148 | // hex(pwnlib.util.crc.generic_crc(b"\x75\x00\x00\x02\x00\x00\x00", 0x31, 8, 0xff, True, True, False)) 149 | const uint8_t bootloader_response[RSP_MAX][7] = { 150 | [RSP_USB_UART_JTAG] = {0x75, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00}, 151 | [RSP_USB_SPAM_JTAG] = {0x75, 0xa0, 0x08, 0x10, 0x00, 0x00, 0x00}, 152 | [RSP_USB_UART] = {0x75, 0x20, 0x00, 0x10, 0x00, 0x00, 0x00}, 153 | [RSP_RESET] = {0x75, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00}, 154 | [RSP_DFU] = {0x75, 0x20, 0x00, 0x02, 0x00, 0x00, 0x00}, 155 | [RSP_USB_A_CHARGING_CABLE] = {0x75, 0x10, 0x0c, 0x00, 0x00, 0x00, 0x00}, 156 | }; 157 | 158 | void set_idbus_high_impedance() { 159 | gpio_pull_up (PIN_SDQ); 160 | gpio_init(PIN_SDQ); 161 | gpio_set_dir(PIN_SDQ, GPIO_IN); 162 | } 163 | 164 | #define DCSD_UART uart0 165 | #define DCSD_TX_PIN 0 166 | #define DCSD_RX_PIN 1 167 | 168 | void dcsd_mode(PIO pio, uint sm) { 169 | uart_init(uart0, 115200); 170 | gpio_set_function(DCSD_TX_PIN, GPIO_FUNC_UART); 171 | gpio_set_function(DCSD_RX_PIN, GPIO_FUNC_UART); 172 | 173 | serprint("DCSD mode active.\r\n"); 174 | serprint("Connect to the second serial port of the\r\n"); 175 | serprint("Tamarin Cable to access the monitor.\r\n"); 176 | 177 | serialEnabled = true; 178 | } 179 | 180 | void jtag_mode(PIO pio, uint sm) { 181 | set_idbus_high_impedance(); 182 | pio_sm_set_enabled(pio, sm, false); 183 | serprint("JTAG mode active, ID pin in Hi-Z.\r\n"); 184 | serprint("You can now connect with an SWD debugger.\r\n"); 185 | 186 | jtagInited = false; 187 | jtagEnabled = true; 188 | } 189 | 190 | uint8_t crc_data(const uint8_t *data, size_t len) { 191 | crc_t crc = crc_init(); 192 | crc = crc_update(crc, data, len); 193 | crc = crc_finalize(crc); 194 | return crc; 195 | } 196 | 197 | void respond_lightning(PIO pio, uint sm, const uint8_t *data, size_t data_length) { 198 | // Static buffer 199 | static uint8_t response_buffer[64]; 200 | if(data_length > 63) { 201 | serprint("Lightning response too large, not sending.\r\n"); 202 | return; 203 | } 204 | memcpy(response_buffer, data, data_length); 205 | response_buffer[data_length] = crc_data(data, data_length); 206 | 207 | pio_sm_set_enabled(pio, sm, false); 208 | pio_clear_instruction_memory(pio); 209 | uint offset = pio_add_program(pio, &lightning_tx_program); 210 | pio_sm_config c = lightning_tx_program_init(pio, sm, offset, PIN_SDQ, 125.0/2.0); 211 | // We fill in 1 byte first, then the second byte, only then do we start to do 212 | // "get_blocking" to avoid the state machine ever running empty. 213 | pio_sm_put_blocking(pio, sm, response_buffer[0]); 214 | for(size_t i=1; i < data_length+1; i++) { 215 | pio_sm_put_blocking(pio, sm, response_buffer[i]); 216 | pio_sm_get_blocking(pio, sm); 217 | } 218 | pio_sm_get_blocking(pio, sm); 219 | } 220 | 221 | void output_state_machine() { 222 | char DFU[8]; 223 | PIO pio = pio1; 224 | uint sm = pio_claim_unused_sm(pio, true); 225 | 226 | uint8_t i = 0; 227 | uint8_t buf[4]; 228 | 229 | uint32_t value, value_b; 230 | while(1) { 231 | switch(gState) { 232 | case RESTART_ENUMERATION: 233 | serprint("Restarting enumeration!\r\n"); 234 | tamarin_reset_tristar(pio, sm); 235 | serprint("Done restarting enumeration!\r\n"); 236 | gState = WAITING_FOR_INIT; 237 | break; 238 | 239 | case WAITING_FOR_INIT: 240 | if (pio_sm_is_rx_fifo_empty(pio, sm)) break; 241 | value = pio_sm_get_blocking(pio, sm); 242 | value_b = reverse_byte(value & 0xFF); 243 | 244 | if(value_b == TRISTAR_POLL) { 245 | leave_dcsd(); 246 | gState = READING_TRISTAR_REQUEST; 247 | buf[0] = value_b; 248 | i = 1; 249 | } else if(value_b == TRISTAR_POWER) { 250 | gState = READING_POWER_REQUEST; 251 | buf[0] = value_b; 252 | i = 1; 253 | } else if(value_b == TRISTAR_UNKNOWN_76) { 254 | gState = READING_TRISTAR_UNKNOWN_76; 255 | buf[0] = value_b; 256 | i = 1; 257 | } else { 258 | serprint("Tristar >> 0x%x (unknown, ignoring)\r\n", value_b); 259 | } 260 | sleep_us(100); // Breaks without this... 261 | break; 262 | case READING_TRISTAR_REQUEST: 263 | if (pio_sm_is_rx_fifo_empty(pio, sm)) break; 264 | value = pio_sm_get_blocking(pio, sm); 265 | value_b = reverse_byte(value & 0xFF); 266 | 267 | buf[i++] = value_b; 268 | if(i == 4) { 269 | gState = HANDLE_TRISTAR_REQUEST; 270 | i = 0; 271 | } 272 | sleep_us(10); // Breaks without this... 273 | break; 274 | case READING_TRISTAR_UNKNOWN_76: 275 | if (pio_sm_is_rx_fifo_empty(pio, sm)) break; 276 | value = pio_sm_get_blocking(pio, sm); 277 | value_b = reverse_byte(value & 0xFF); 278 | 279 | buf[i++] = value_b; 280 | if(i == 2) { 281 | gState = HANDLE_TRISTAR_UNKNOWN_76; 282 | i = 0; 283 | } 284 | sleep_us(10); // Breaks without this... 285 | break; 286 | case HANDLE_TRISTAR_UNKNOWN_76: 287 | respond_lightning(pio, sm, "\x77\x02\x01\x02\x80\x60\x01\x39\x3a\x44\x3e\xc9", 11); 288 | serprint("76 request received: %02X %02X\r\n", buf[0], buf[1]); 289 | gState = WAITING_FOR_INIT; 290 | break; 291 | case READING_POWER_REQUEST: 292 | if (pio_sm_is_rx_fifo_empty(pio, sm)) break; 293 | value = pio_sm_get_blocking(pio, sm); 294 | value_b = reverse_byte(value & 0xFF); 295 | 296 | buf[i++] = value_b; 297 | if(i == 4) { 298 | gState = HANDLE_POWER_REQUEST; 299 | i = 0; 300 | } 301 | 302 | sleep_us(10); // Breaks without this... 303 | 304 | break; 305 | case HANDLE_POWER_REQUEST: 306 | respond_lightning(pio, sm, "\x71\x93", 1); 307 | serprint("Power request received: %02X %02X %02X %02X\r\n", buf[0], buf[1], buf[2], buf[3]); 308 | gState = WAITING_FOR_INIT; 309 | break; 310 | case HANDLE_TRISTAR_REQUEST: 311 | serprint("Tristar request received: %02X %02X %02X %02X\r\n", buf[0], buf[1], buf[2], buf[3]); 312 | switch(gCommand) { 313 | case CMD_DEFAULT: 314 | switch (gDefaultCommand) { 315 | case DEFAULT_CMD_DCSD: 316 | respond_lightning(pio, sm, bootloader_response[RSP_USB_UART], 7); 317 | dcsd_mode(pio, sm); 318 | break; 319 | 320 | case DEFAULT_CMD_JTAG: 321 | respond_lightning(pio, sm, bootloader_response[RSP_USB_UART_JTAG], 7); 322 | gState = FORCE_JTAG; 323 | continue; 324 | 325 | case DEFAULT_CMD_SPAM: 326 | respond_lightning(pio, sm, bootloader_response[RSP_USB_SPAM_JTAG], 7); 327 | gState = FORCE_SPAM; 328 | continue; 329 | 330 | case DEFAULT_CMD_CHARGING: 331 | respond_lightning(pio, sm, bootloader_response[RSP_USB_A_CHARGING_CABLE], 7); 332 | serprint("Sent charging\r\n"); 333 | gState = WAITING_FOR_INIT; 334 | break; 335 | } 336 | break; 337 | case CMD_RESET: 338 | respond_lightning(pio, sm, bootloader_response[RSP_RESET], 7); 339 | sleep_us(1000); 340 | gCommand = CMD_DEFAULT; 341 | break; 342 | case CMD_AUTO_DFU: 343 | respond_lightning(pio, sm, bootloader_response[RSP_RESET], 7); 344 | gCommand = CMD_INTERNAL_AUTO_DFU_2; 345 | break; 346 | case CMD_INTERNAL_AUTO_DFU_2: 347 | respond_lightning(pio, sm, bootloader_response[RSP_DFU], 7); 348 | serprint("Device should now be in DFU mode.\r\n"); 349 | gCommand = CMD_DEFAULT; 350 | break; 351 | default: 352 | serprint("UNKNOWN MODE. Send help. Locking up.\r\n"); 353 | while(1) {} 354 | break; 355 | } 356 | 357 | gState = WAITING_FOR_INIT; 358 | configure_rx(pio, sm); 359 | break; 360 | 361 | case HANDLE_JTAG: 362 | // Nothing to do 363 | break; 364 | 365 | case FORCE_JTAG: 366 | dcsd_mode(pio, sm); // Also init serial 367 | case FORCE_SPAM: 368 | jtag_mode(pio, sm); 369 | gState = HANDLE_JTAG; 370 | break; 371 | } 372 | } 373 | } 374 | 375 | void print_menu() { 376 | serprint("Good morning!\r\n\r\n"); 377 | serprint("1: JTAG mode\r\n"); 378 | serprint("2: DCSD mode\r\n"); 379 | serprint("3: Reset device\r\n"); 380 | serprint("4: Reset and enter DFU mode (iPhone X and up only)\r\n"); 381 | serprint("5: Reenumerate\r\n\r\n"); 382 | serprint("F: Force JTAG mode without sending command\r\n"); 383 | serprint("J: Force SPAM-JTAG mode without sending command\r\n"); 384 | serprint("R: Reset Tamarin cable\r\n"); 385 | serprint("S: SPAM mode (Apple Watch UART)\r\n"); 386 | serprint("U: Go into firmware update mode\r\n"); 387 | serprint("> "); 388 | } 389 | 390 | void shell_task() { 391 | if (tud_cdc_n_available(ITF_CONSOLE)) { 392 | char c = tud_cdc_n_read_char(ITF_CONSOLE); 393 | tud_cdc_n_write_char(ITF_CONSOLE, c); 394 | switch(c) { 395 | case '1': 396 | serprint("\r\nEnabling JTAG mode.\r\n"); 397 | gCommand = CMD_DEFAULT; 398 | gDefaultCommand = DEFAULT_CMD_JTAG; 399 | gState = RESTART_ENUMERATION; 400 | break; 401 | case '2': 402 | serprint("\r\nEnabling DCSD mode.\r\n"); 403 | gCommand = CMD_DEFAULT; 404 | gDefaultCommand = DEFAULT_CMD_DCSD; 405 | gState = RESTART_ENUMERATION; 406 | break; 407 | case '3': 408 | serprint("\r\nResetting.\r\n"); 409 | gCommand = CMD_RESET; 410 | gState = RESTART_ENUMERATION; 411 | break; 412 | case '4': 413 | serprint("\r\nEnabling DFU mode.\r\n"); 414 | gCommand = CMD_AUTO_DFU; 415 | gState = RESTART_ENUMERATION; 416 | break; 417 | case '5': 418 | serprint("\r\nReenumerate\r\n"); 419 | gCommand = CMD_DEFAULT; 420 | gState = RESTART_ENUMERATION; 421 | break; 422 | case 'f': 423 | case 'F': 424 | serprint("\r\nForcing JTAG mode.\r\n"); 425 | gDefaultCommand = DEFAULT_CMD_JTAG; 426 | gState = FORCE_JTAG; 427 | break; 428 | case 'j': 429 | case 'J': 430 | serprint("\r\nForcing SPAM mode.\r\n"); 431 | gDefaultCommand = DEFAULT_CMD_SPAM; 432 | gState = FORCE_SPAM; 433 | break; 434 | case 'R': 435 | case 'r': 436 | watchdog_enable(100, 1); 437 | break; 438 | case 's': 439 | case 'S': 440 | serprint("\r\nEnabling SPAM mode.\r\n"); 441 | gCommand = CMD_DEFAULT; 442 | gDefaultCommand = DEFAULT_CMD_SPAM; 443 | gState = RESTART_ENUMERATION; 444 | break; 445 | case 'U': 446 | case 'u': 447 | reset_usb_boot(0, 0); 448 | default: 449 | print_menu(); 450 | break; 451 | } 452 | } 453 | } 454 | 455 | int main() { 456 | // Power enable for Tamarin Cable. Not required on regular Pico, 457 | // but also doesn't hurt. 458 | gpio_init(19); 459 | gpio_set_dir(19, GPIO_OUT); 460 | gpio_put(19, 1); 461 | 462 | board_init(); 463 | tusb_init(); 464 | 465 | multicore_launch_core1(output_state_machine); 466 | 467 | while(1) { 468 | tud_task(); 469 | shell_task(); 470 | 471 | // Handle serial 472 | if (serialEnabled) { 473 | if (uart_is_readable(uart0)) { 474 | tud_cdc_n_write_char(ITF_DCSD, uart_getc(uart0)); 475 | tud_cdc_n_write_flush(ITF_DCSD); 476 | } 477 | 478 | if (tud_cdc_n_available(ITF_DCSD)) { 479 | uart_putc_raw(uart0, tud_cdc_n_read_char(ITF_DCSD)); 480 | } 481 | } 482 | 483 | tud_task(); 484 | tud_vendor_flush(); 485 | tud_task(); 486 | if (jtagEnabled) { 487 | if (!jtagInited) { 488 | tamarin_probe_init(); 489 | tud_task(); 490 | jtagInited = true; 491 | } 492 | 493 | tamarin_probe_task(!serialEnabled); 494 | } 495 | tud_task(); 496 | tud_vendor_flush(); 497 | tud_task(); 498 | if (wantLeaveJtag){ 499 | leave_jtag(); 500 | } 501 | } 502 | } 503 | -------------------------------------------------------------------------------- /media/lightning.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stacksmashing/tamarin-firmware/5d4a559a6bd34781ab21a38bf5dc151e61749f37/media/lightning.png -------------------------------------------------------------------------------- /media/pinout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stacksmashing/tamarin-firmware/5d4a559a6bd34781ab21a38bf5dc151e61749f37/media/pinout.png -------------------------------------------------------------------------------- /media/tamarin-logo-300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stacksmashing/tamarin-firmware/5d4a559a6bd34781ab21a38bf5dc151e61749f37/media/tamarin-logo-300.png -------------------------------------------------------------------------------- /pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading Raspberry Pi Pico SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | -------------------------------------------------------------------------------- /probe.pio: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | .program probe 27 | .side_set 1 opt 28 | 29 | public out_negedge: 30 | set pindirs, 1 side 0x0 ; Init OE clock 0 31 | pull ; Pull number of bits to shift -1 from tx fifo and put into output shift register 32 | mov x, osr ; mov bits to shift -1 from output shift register into x 33 | pull ; Pull data to shift out 34 | out_negedge_bitloop: 35 | out pins, 1 side 0x0 ; clock data out on falling edge 36 | jmp x-- out_negedge_bitloop side 0x1 ; data is present for posedge 37 | set pins, 1 side 0x0 ; Drive data low 38 | push ; Push to rx fifo just so processor knows when done 39 | jmp out_negedge ; Wait for next transaction 40 | 41 | public in_posedge: 42 | set pindirs, 0 side 0x0 ; INIT IE clock 0 43 | pull ; Pull number of bits to shift -1 from tx fifo and put into output shift register 44 | mov x, osr ; mov bits to shift -1 from output shift register into x into x 45 | in_posedge_bitloop: 46 | in pins, 1 side 0x1 ; Generate posedge and read data 47 | jmp x-- in_posedge_bitloop side 0x0 ; 48 | push ; Push to rx fifo when done 49 | jmp in_posedge ; Jump back to start 50 | -------------------------------------------------------------------------------- /tamarin_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under GNU Public License v3 3 | * Copyright (c) 2022 Thomas Roth 4 | * Based on Picoprobe by: 5 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 6 | * 7 | */ 8 | 9 | #ifndef PICOPROBE_H_ 10 | #define PICOPROBE_H_ 11 | 12 | #if true 13 | #define tamarin_info(format,args...) serprint(format, ## args) 14 | #else 15 | #define picoprobe_info(format,...) ((void)0) 16 | #endif 17 | 18 | 19 | #if true 20 | #define tamarin_debug(format,args...) serprint(format, ## args) 21 | #else 22 | #define tamarin_debug(format,...) ((void)0) 23 | #endif 24 | 25 | #if true 26 | #define tamarin_dump(format,args...) serprint(format, ## args) 27 | #else 28 | #define tamarin_dump(format,...) ((void)0) 29 | #endif 30 | 31 | 32 | // PIO config 33 | #define PROBE_PIO pio0 34 | #define PROBE_SM 0 35 | #define PROBE_PIN_OFFSET 2 36 | #define PROBE_PIN_SWCLK PROBE_PIN_OFFSET + 0 // 2 37 | #define PROBE_PIN_SWDIO PROBE_PIN_OFFSET + 1 // 3 38 | 39 | // Target reset config 40 | // TODO: We do not support reset. 41 | #define PROBE_PIN_RESET 6 42 | 43 | // LED config 44 | #ifndef PICOPROBE_LED 45 | 46 | #ifndef PICO_DEFAULT_LED_PIN 47 | #error PICO_DEFAULT_LED_PIN is not defined, run PICOPROBE_LED= cmake 48 | #elif PICO_DEFAULT_LED_PIN == -1 49 | #error PICO_DEFAULT_LED_PIN is defined as -1, run PICOPROBE_LED= cmake 50 | #else 51 | #define PICOPROBE_LED PICO_DEFAULT_LED_PIN 52 | #endif 53 | 54 | #endif 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /tamarin_probe.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under GNU Public License v3 3 | * Copyright (c) 2022 Thomas Roth 4 | * Based on Picoprobe by: 5 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 6 | * 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | #include "tamarin_config.h" 17 | #include "probe.pio.h" 18 | #include "tusb.h" 19 | #include "util.h" 20 | 21 | // Disable all prints for now 22 | #define serprint(...) 23 | 24 | enum TAMARIN_CMDS { 25 | TAMARIN_INVALID = 0, // Invalid command 26 | TAMARIN_READ = 1, 27 | TAMARIN_WRITE = 2, 28 | TAMARIN_LINE_RESET = 3, 29 | TAMARIN_SET_FREQ = 4 30 | }; 31 | 32 | #define SWD_RSP_OK 0b001 33 | #define SWD_RSP_WAIT 0b010 34 | #define SWD_RSP_FAULT 0b100 35 | 36 | static int gDPIDR = 0; 37 | static int gOrigSEL = 0; 38 | 39 | #define BITS_ALWYS 0x81 40 | 41 | #define BITS_AP (1<<1) 42 | #define BITS_DP (0<<1) 43 | 44 | #define BITS_RD (1<<2) 45 | #define BITS_WR (0<<2) 46 | 47 | #define PARITY(i) (i<<5) 48 | 49 | #define BITS_DP_IDCODE (0b00 << 3) 50 | #define BITS_DP_ABORT (0b00 << 3) 51 | #define BITS_DP_CTRL (0b01 << 3) 52 | #define BITS_DP_RESEND (0b10 << 3) 53 | #define BITS_DP_SELECT (0b10 << 3) 54 | #define BITS_DP_RDBUFF (0b11 << 3) 55 | 56 | #define BITS_AP_CSW (0b00 << 3) 57 | #define BITS_AP_TAR (0b01 << 3) 58 | #define BITS_AP_DRW (0b11 << 3) 59 | 60 | #define BITS_DP_READ(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(1) : PARITY(0)) | addr | BITS_RD | BITS_DP | BITS_ALWYS 61 | #define BITS_DP_WRITE(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(0) : PARITY(1)) | addr | BITS_WR | BITS_DP | BITS_ALWYS 62 | 63 | #define BITS_AP_READ(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(0) : PARITY(1)) | addr | BITS_RD | BITS_AP | BITS_ALWYS 64 | #define BITS_AP_WRITE(addr) (((addr) == 0 || ((addr)>>3) == 3) ? PARITY(1) : PARITY(0)) | addr | BITS_WR | BITS_AP | BITS_ALWYS 65 | 66 | #define SWD_DP_read_IDCODE(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_IDCODE),val) 67 | #define SWD_DP_read_CTRL(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_CTRL),val) 68 | #define SWD_DP_read_RESEND(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_RESEND),val) 69 | #define SWD_DP_read_RDBUFF(val) tamarin_tx_read_bare(BITS_DP_READ(BITS_DP_RDBUFF),val) 70 | 71 | #define SWD_DP_write_ABORT(val) tamarin_tx_write_bare(BITS_DP_WRITE(BITS_DP_ABORT),val) 72 | #define SWD_DP_write_CTRL(val) tamarin_tx_write_bare(BITS_DP_WRITE(BITS_DP_CTRL),val) 73 | #define SWD_DP_write_SELECT(val) tamarin_tx_write_bare(BITS_DP_WRITE(BITS_DP_SELECT),val) 74 | 75 | #define SWD_AP_read_CSW(val) tamarin_tx_read_bare(BITS_AP_READ(BITS_AP_CSW),val) 76 | #define SWD_AP_read_TAR(val) tamarin_tx_read_bare(BITS_AP_READ(BITS_AP_TAR),val) 77 | #define SWD_AP_read_DRW(val) tamarin_tx_read_bare(BITS_AP_READ(BITS_AP_DRW),val) 78 | 79 | #define SWD_AP_write_CSW(val) tamarin_tx_write_bare(BITS_AP_WRITE(BITS_AP_CSW),val) 80 | #define SWD_AP_write_TAR(val) tamarin_tx_write_bare(BITS_AP_WRITE(BITS_AP_TAR),val) 81 | #define SWD_AP_write_DRW(val) tamarin_tx_write_bare(BITS_AP_WRITE(BITS_AP_DRW),val) 82 | 83 | 84 | #define SWD_DP_clear_error() SWD_DP_write_ABORT(0x1e) 85 | 86 | // This struct is the direct struct that is sent to 87 | // the probe. 88 | struct __attribute__((__packed__)) tamarin_cmd_hdr { 89 | // Currently unused 90 | uint8_t id; 91 | // One of TAMARIN_CMDS 92 | uint8_t cmd; 93 | // The full (incl. start/stop/parity) SWD command 94 | // Unused for LINE_RESET and SET_FREQ. 95 | uint8_t request; 96 | // The data for writes (unused otherwise) 97 | uint32_t data; 98 | // Number of (8 bit) idle cycles to perform after this op 99 | uint8_t idle_cycles; 100 | }; 101 | 102 | // This is the struture returned by the probe for each command 103 | struct __attribute__((__packed__)) tamarin_res_hdr { 104 | // Unused 105 | uint8_t id; 106 | // The (3 bit) result: OK/WAIT/FAULT 107 | uint8_t res; 108 | // The data for reads (undefined otherwise) 109 | uint32_t data; 110 | }; 111 | 112 | #define PROBE_BUF_SIZE 8192 113 | struct _probe { 114 | // Data received from computer 115 | struct tamarin_cmd_hdr probe_cmd; 116 | 117 | // Data that will be sent back to the computer 118 | struct tamarin_res_hdr probe_res; 119 | 120 | // PIO offset 121 | uint offset; 122 | }; 123 | 124 | static struct _probe probe; 125 | 126 | void probe_set_swclk_freq(uint freq_khz) { 127 | tamarin_info("Setting SWD frequency to %d kHz\r\n", freq_khz); 128 | uint clk_sys_freq_khz = clock_get_hz(clk_sys) / 1000; 129 | // Worked out with saleae 130 | uint32_t divider = clk_sys_freq_khz / freq_khz / 2; 131 | pio_sm_set_clkdiv_int_frac(pio0, PROBE_SM, divider, 0); 132 | } 133 | 134 | static inline void probe_write_bits(uint bit_count, uint32_t data_byte) { 135 | 136 | serprint(">> %X (%d)\r\n", data_byte, bit_count); 137 | pio_sm_put_blocking(pio0, PROBE_SM, bit_count - 1); 138 | pio_sm_put_blocking(pio0, PROBE_SM, data_byte); 139 | pio_sm_get_blocking(pio0, PROBE_SM); 140 | } 141 | 142 | static inline uint32_t probe_read_bits(uint bit_count) { 143 | pio_sm_put_blocking(pio0, PROBE_SM, bit_count - 1); 144 | uint32_t data = pio_sm_get_blocking(pio0, PROBE_SM); 145 | data = data >> (32-bit_count); 146 | serprint("<< %X (%d)\r\n", data, bit_count); 147 | return data; 148 | } 149 | 150 | static void probe_read_mode(void) { 151 | pio_sm_exec(pio0, PROBE_SM, pio_encode_jmp(probe.offset + probe_offset_in_posedge)); 152 | while(pio0->dbg_padoe & (1 << PROBE_PIN_SWDIO)); 153 | } 154 | 155 | static void probe_write_mode(void) { 156 | pio_sm_exec(pio0, PROBE_SM, pio_encode_jmp(probe.offset + probe_offset_out_negedge)); 157 | while(!(pio0->dbg_padoe & (1 << PROBE_PIN_SWDIO))); 158 | } 159 | 160 | void tamarin_start_probe() { 161 | // set to output 162 | pio_sm_set_consecutive_pindirs(pio0, PROBE_SM, PROBE_PIN_OFFSET, 2, true); 163 | // Enable SM 164 | pio_sm_set_enabled(pio0, PROBE_SM, 1); 165 | 166 | // Jump to write program 167 | // probe_write_mode(); 168 | probe_read_mode(); 169 | // probe_enabled = true; 170 | } 171 | 172 | void tamarin_probe_init() { 173 | // Funcsel pins 174 | pio_gpio_init(pio0, PROBE_PIN_SWCLK); 175 | pio_gpio_init(pio0, PROBE_PIN_SWDIO); 176 | // Make sure SWDIO has a pullup on it. Idle state is high 177 | gpio_pull_up(PROBE_PIN_SWDIO); 178 | 179 | // Target reset pin: pull up, input to emulate open drain pin 180 | gpio_pull_up(PROBE_PIN_RESET); 181 | // gpio_init will leave the pin cleared and set as input 182 | gpio_init(PROBE_PIN_RESET); 183 | 184 | uint offset = pio_add_program(pio0, &probe_program); 185 | probe.offset = offset; 186 | 187 | pio_sm_config sm_config = probe_program_get_default_config(offset); 188 | 189 | // Set SWCLK as a sideset pin 190 | sm_config_set_sideset_pins(&sm_config, PROBE_PIN_SWCLK); 191 | 192 | // Set SWDIO offset 193 | sm_config_set_out_pins(&sm_config, PROBE_PIN_SWDIO, 1); 194 | sm_config_set_set_pins(&sm_config, PROBE_PIN_SWDIO, 1); 195 | sm_config_set_in_pins(&sm_config, PROBE_PIN_SWDIO); 196 | 197 | // Set pins to input to ensure we don't pull the line low unnecessarily 198 | pio_sm_set_consecutive_pindirs(pio0, PROBE_SM, PROBE_PIN_OFFSET, 2, false); 199 | 200 | // shift output right, autopull off, autopull threshold 201 | sm_config_set_out_shift(&sm_config, true, false, 0); 202 | // shift input right as swd data is lsb first, autopush off 203 | sm_config_set_in_shift(&sm_config, true, false, 0); 204 | 205 | // Init SM with config 206 | pio_sm_init(pio0, PROBE_SM, offset, &sm_config); 207 | 208 | // Set up divisor 209 | probe_set_swclk_freq(1000); 210 | 211 | gDPIDR = 0; 212 | gOrigSEL = -1; 213 | tamarin_start_probe(); 214 | } 215 | 216 | void tamarin_probe_deinit() { 217 | gDPIDR = 0; 218 | pio_sm_set_enabled(pio0, PROBE_SM, false); 219 | pio_clear_instruction_memory(pio0); 220 | gpio_disable_pulls(PROBE_PIN_SWDIO); 221 | } 222 | 223 | int __not_in_flash_func(tamarin_tx_read_bare)(uint8_t request, uint32_t *value); 224 | 225 | void tamarin_line_reset() { 226 | probe_write_mode(); 227 | probe_write_bits(32, 0xFFFFFFFF); 228 | probe_write_bits(32, 0xFFFFFFFF); 229 | probe_write_bits(16, 0xe79e); 230 | probe_write_bits(32, 0xFFFFFFFF); 231 | probe_write_bits(32, 0xFFFFFFFF); 232 | // probe_write_bits(8, 0x0); 233 | uint32_t foo; 234 | tamarin_tx_read_bare(0xa5, &foo); 235 | probe_read_mode(); 236 | } 237 | 238 | int __not_in_flash_func(tamarin_tx_read_bare)(uint8_t request, uint32_t *value) { 239 | 240 | // uint8_t data_reversed = reverse(request); 241 | uint32_t result = request; 242 | for(int i=0; i < 5; i++) { 243 | serprint("Read loop"); 244 | probe_write_mode(); 245 | 246 | // Idle cycles just in case 247 | probe_write_bits(32, 0x0); 248 | 249 | // probe_write_bits(32, 0x0); 250 | probe_write_bits(32, 0x0); 251 | probe_write_bits(32, 0x0); 252 | probe_write_bits(32, 0x0); 253 | 254 | probe_write_bits(8, request); 255 | // Read parity bit 256 | probe_write_bits(1, 1); 257 | probe_read_mode(); 258 | result = probe_read_bits(3); 259 | if(result == 2) { 260 | serprint("Read - Probe received wait. Try: %d\r\n", i); 261 | // because we probably have overrun on we need to perform a 262 | // data phase. 263 | uint32_t read_data = probe_read_bits(32); 264 | uint32_t result_parity = probe_read_bits(1); 265 | continue; 266 | } 267 | if(result != 1) { 268 | return result; 269 | } 270 | break; 271 | } 272 | 273 | 274 | uint32_t read_data = probe_read_bits(32); 275 | uint32_t result_parity = probe_read_bits(1); 276 | *value = read_data; 277 | serprint("READ - Result: %d Data: %08X (%d)\r\n", result, read_data, result_parity); 278 | return result; 279 | } 280 | 281 | 282 | int __not_in_flash_func(tamarin_tx_write_bare)(uint8_t request, uint32_t value) { 283 | uint32_t value_parity = __builtin_parity(value); 284 | uint32_t result = request; 285 | for(int i=0; i < 5; i++) { 286 | serprint("Write loop"); 287 | probe_write_mode(); 288 | 289 | // Idle cycles... These can be reduced 290 | probe_write_bits(32, 0x0); 291 | probe_write_bits(32, 0x0); 292 | probe_write_bits(32, 0x0); 293 | probe_write_bits(32, 0x0); 294 | 295 | probe_write_bits(8, request); 296 | 297 | // Read parity bit 298 | probe_write_bits(1, 1); 299 | 300 | probe_read_mode(); 301 | result = probe_read_bits(3); 302 | if(result == 2) { 303 | serprint("Write - Probe received wait. Try: %d\r\n", i); 304 | // With overrun detection we still have to do the datapahse 305 | probe_write_mode(); 306 | // turn 307 | probe_write_bits(1, 0x1); 308 | probe_write_bits(32, 0x0); 309 | probe_write_bits(1, 0x1); 310 | // uint32_t result_parity = probe_read_bits(1); 311 | continue; 312 | } 313 | if(result != 1) { 314 | return result; 315 | } 316 | break; 317 | } 318 | 319 | // Another turn! 320 | probe_write_mode(); 321 | probe_write_bits(1, 1); 322 | // Write the actual data 323 | probe_write_bits(32, value); 324 | // Write parity bit 325 | probe_write_bits(1, value_parity); 326 | 327 | probe_read_mode(); 328 | serprint("READ - Result: %d\r\n", result); 329 | return result; 330 | } 331 | 332 | bool probe_enabled = false; 333 | void probe_handle_pkt(void) { 334 | struct tamarin_cmd_hdr *cmd = &probe.probe_cmd; 335 | 336 | tamarin_debug("Processing packet: ID: %u Command: %u Request: 0x%02X Data: 0x%08X Idle: %d\r\n", cmd->id, cmd->cmd, cmd->request, cmd->data, cmd->idle_cycles); 337 | int result = 0; 338 | uint32_t data = 0; 339 | switch(cmd->cmd) { 340 | case TAMARIN_READ: 341 | tamarin_debug("Executing read\r\n"); 342 | 343 | result = tamarin_tx_read_bare(cmd->request, &data); 344 | tamarin_debug("Read: %d 0x%08X", result, data); 345 | break; 346 | case TAMARIN_WRITE: 347 | tamarin_debug("Executing write\r\n"); 348 | if (cmd->request == BITS_DP_WRITE(BITS_DP_SELECT)){ 349 | gOrigSEL = cmd->data; 350 | } 351 | result = tamarin_tx_write_bare(cmd->request, cmd->data); 352 | tamarin_debug("Wrrite: %d 0x%08X", result, cmd->data); 353 | break; 354 | case TAMARIN_LINE_RESET: 355 | tamarin_debug("Executing line reset\r\n"); 356 | tamarin_line_reset(); 357 | break; 358 | case TAMARIN_SET_FREQ: 359 | tamarin_debug("Executing set frequency\r\n"); 360 | probe_set_swclk_freq(cmd->data); 361 | break; 362 | default: 363 | tamarin_debug("UNKNOWN COMMAND!\r\n"); 364 | break; 365 | } 366 | 367 | // For now we just reply with a default command 368 | struct tamarin_res_hdr res; 369 | res.id = 33; 370 | res.data = data; 371 | res.res = result; 372 | 373 | tud_vendor_write((char*)&res, sizeof(res)); 374 | } 375 | 376 | int SWD_readmem(uint32_t addr, uint32_t *data){ 377 | int result = 0; 378 | result = SWD_AP_write_TAR(addr); 379 | if (result != SWD_RSP_OK) goto cleanup; 380 | 381 | result = SWD_AP_read_DRW(data); 382 | if (result != SWD_RSP_OK) goto cleanup; 383 | 384 | result = SWD_DP_read_RDBUFF(data); 385 | if (result != SWD_RSP_OK) goto cleanup; 386 | cleanup:; 387 | return result; 388 | } 389 | 390 | void handle_SPAM(void){ 391 | int result = 0; 392 | uint32_t data = 0; 393 | int hasdata = 0; 394 | if (!gDPIDR){ 395 | probe_set_swclk_freq(1000); 396 | tamarin_line_reset(); 397 | SWD_DP_clear_error(); 398 | result = SWD_DP_read_IDCODE(&data); 399 | if (data != 0 && result == SWD_RSP_OK){ 400 | gDPIDR = data; 401 | result = SWD_DP_write_CTRL(0x50000000); 402 | } 403 | }else{ 404 | uint32_t uart_ctrl_reg = 0; 405 | if (gDPIDR == 0x5ba02477){ 406 | //s7002 407 | uart_ctrl_reg = 0xc6e00004; 408 | }else if (gDPIDR == 0x4ba02477){ 409 | //s8002 410 | uart_ctrl_reg = 0xC83B401C; 411 | } 412 | 413 | if (uart_ctrl_reg){ 414 | result = SWD_DP_clear_error(); 415 | if (result != SWD_RSP_OK) goto cleanup; 416 | 417 | if (gOrigSEL != 0x01000000){ 418 | result = SWD_DP_write_SELECT(0x01000000); 419 | if (result != SWD_RSP_OK) goto cleanup; 420 | if (gOrigSEL == -1) gOrigSEL = 0x01000000; 421 | } 422 | 423 | result = SWD_AP_write_CSW(0xA2000012); 424 | if (result != SWD_RSP_OK) goto cleanup; 425 | 426 | 427 | int cnt = 1; 428 | uint32_t payload = 0; 429 | 430 | while (cnt > 0){ 431 | result = SWD_readmem(uart_ctrl_reg + 0x00,&data); 432 | if (result != SWD_RSP_OK) goto cleanup; 433 | cnt = data & 0x7f; 434 | if (!cnt) break; 435 | hasdata = 1; 436 | tud_cdc_n_write_char(ITF_DCSD, data >> 8); 437 | cnt--; 438 | result = SWD_readmem(uart_ctrl_reg + 0x0C,&payload); 439 | if (result != SWD_RSP_OK) goto cleanup; 440 | int sendSize = cnt > sizeof(payload) ? sizeof(payload) : cnt; 441 | tud_cdc_n_write(ITF_DCSD, &payload, sendSize); 442 | } 443 | 444 | cleanup:; 445 | if (result == SWD_RSP_FAULT){ 446 | data = 0; 447 | result = SWD_DP_read_CTRL(&data); 448 | if (result == 99) return; 449 | } 450 | if (gOrigSEL != 0x01000000){ 451 | result = SWD_RSP_WAIT; 452 | for (int i=0; i<100 && result == SWD_RSP_WAIT; i++){ 453 | result = SWD_DP_write_SELECT(gOrigSEL); 454 | } 455 | } 456 | } 457 | } 458 | if (hasdata) tud_cdc_n_write_flush(ITF_DCSD); 459 | return; 460 | } 461 | 462 | // USB bits 463 | void tamarin_probe_task(int doSPAM) { 464 | if ( tud_vendor_available() ) { 465 | char tmp_buf[64]; 466 | uint count = tud_vendor_read(tmp_buf, 64); 467 | if (count == 0) { 468 | return; 469 | } 470 | 471 | memcpy(&probe.probe_cmd, tmp_buf, sizeof(struct tamarin_cmd_hdr)); 472 | probe_handle_pkt(); 473 | } 474 | if (doSPAM) handle_SPAM(); 475 | } 476 | -------------------------------------------------------------------------------- /tamarin_probe.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2022 Thomas Roth - code@stacksmashing.net 5 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | * 25 | */ 26 | 27 | #ifndef PROBE_H_ 28 | #define PROBE_H_ 29 | 30 | void tamarin_probe_task(int doSPAM); 31 | void tamarin_probe_init(void); 32 | void tamarin_probe_deinit(void); 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /tusb_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | #ifndef _TUSB_CONFIG_H_ 27 | #define _TUSB_CONFIG_H_ 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | //-------------------------------------------------------------------- 34 | // COMMON CONFIGURATION 35 | //-------------------------------------------------------------------- 36 | 37 | // defined by board.mk 38 | #ifndef CFG_TUSB_MCU 39 | #error CFG_TUSB_MCU must be defined 40 | #endif 41 | 42 | // RHPort number used for device can be defined by board.mk, default to port 0 43 | #ifndef BOARD_DEVICE_RHPORT_NUM 44 | #define BOARD_DEVICE_RHPORT_NUM 0 45 | #endif 46 | 47 | // RHPort max operational speed can defined by board.mk 48 | // Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed 49 | #ifndef BOARD_DEVICE_RHPORT_SPEED 50 | #if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \ 51 | CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56 || CFG_TUSB_MCU == OPT_MCU_SAMX7X) 52 | #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED 53 | #else 54 | #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED 55 | #endif 56 | #endif 57 | 58 | // Device mode with rhport and speed defined by board.mk 59 | #if BOARD_DEVICE_RHPORT_NUM == 0 60 | #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) 61 | #elif BOARD_DEVICE_RHPORT_NUM == 1 62 | #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) 63 | #else 64 | #error "Incorrect RHPort configuration" 65 | #endif 66 | 67 | #ifndef CFG_TUSB_OS 68 | #define CFG_TUSB_OS OPT_OS_PICO 69 | #endif 70 | 71 | // CFG_TUSB_DEBUG is defined by compiler in DEBUG build 72 | // #define CFG_TUSB_DEBUG 0 73 | 74 | /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. 75 | * Tinyusb use follows macros to declare transferring memory so that they can be put 76 | * into those specific section. 77 | * e.g 78 | * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) 79 | * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) 80 | */ 81 | #ifndef CFG_TUSB_MEM_SECTION 82 | #define CFG_TUSB_MEM_SECTION 83 | #endif 84 | 85 | #ifndef CFG_TUSB_MEM_ALIGN 86 | #define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) 87 | #endif 88 | 89 | //-------------------------------------------------------------------- 90 | // DEVICE CONFIGURATION 91 | //-------------------------------------------------------------------- 92 | 93 | #ifndef CFG_TUD_ENDPOINT0_SIZE 94 | #define CFG_TUD_ENDPOINT0_SIZE 64 95 | #endif 96 | 97 | //------------- CLASS -------------// 98 | #define CFG_TUD_CDC 2 99 | #define CFG_TUD_MSC 0 100 | #define CFG_TUD_HID 0 101 | #define CFG_TUD_MIDI 0 102 | #define CFG_TUD_VENDOR 1 103 | 104 | // CDC FIFO size of TX and RX 105 | #define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 1024) 106 | #define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 1024) 107 | 108 | // CDC Endpoint transfer buffer size, more is faster 109 | #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 1024) 110 | 111 | 112 | 113 | #ifndef CFG_TUD_ENDPOINT0_SIZE 114 | #error ey 115 | #define CFG_TUD_ENDPOINT0_SIZE 64 116 | #endif 117 | 118 | // Vendor transfer bufsize 119 | #define CFG_TUD_VENDOR_RX_BUFSIZE 8192 120 | #define CFG_TUD_VENDOR_TX_BUFSIZE 8192 121 | 122 | 123 | #ifdef __cplusplus 124 | } 125 | #endif 126 | 127 | #endif /* _TUSB_CONFIG_H_ */ 128 | -------------------------------------------------------------------------------- /usb_descriptors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed under GNU Public License v3 3 | * Copyright (c) 2022 Thomas Roth 4 | * Based on: 5 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 6 | * 7 | */ 8 | 9 | #include "tusb.h" 10 | 11 | /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. 12 | * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. 13 | * 14 | * Auto ProductID layout's Bitmap: 15 | * [MSB] MIDI | HID | MSC | CDC [LSB] 16 | */ 17 | #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) 18 | #define USB_PID 0x0004 19 | // #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ 20 | // _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) 21 | 22 | #define USB_VID 0x2B3E 23 | #define USB_BCD 0x2342 24 | 25 | //--------------------------------------------------------------------+ 26 | // Device Descriptors 27 | //--------------------------------------------------------------------+ 28 | tusb_desc_device_t const desc_device = 29 | { 30 | .bLength = sizeof(tusb_desc_device_t), 31 | .bDescriptorType = TUSB_DESC_DEVICE, 32 | .bcdUSB = USB_BCD, 33 | 34 | // Use Interface Association Descriptor (IAD) for CDC 35 | // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) 36 | .bDeviceClass = TUSB_CLASS_MISC, 37 | .bDeviceSubClass = MISC_SUBCLASS_COMMON, 38 | .bDeviceProtocol = MISC_PROTOCOL_IAD, 39 | .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, 40 | 41 | .idVendor = USB_VID, 42 | .idProduct = USB_PID, 43 | .bcdDevice = 0x0100, 44 | 45 | .iManufacturer = 0x01, 46 | .iProduct = 0x02, 47 | .iSerialNumber = 0x03, 48 | 49 | .bNumConfigurations = 0x01 50 | }; 51 | 52 | // Invoked when received GET DEVICE DESCRIPTOR 53 | // Application return pointer to descriptor 54 | uint8_t const * tud_descriptor_device_cb(void) 55 | { 56 | return (uint8_t const *) &desc_device; 57 | } 58 | 59 | //--------------------------------------------------------------------+ 60 | // Configuration Descriptor 61 | //--------------------------------------------------------------------+ 62 | enum 63 | { 64 | ITF_NUM_CDC_0 = 0, 65 | ITF_NUM_CDC_0_DATA, 66 | 67 | ITF_NUM_PROBE, 68 | ITF_NUM_CDC_1, 69 | ITF_NUM_CDC_1_DATA, 70 | // ITF_NUM_CDC_2_DATA, 71 | ITF_NUM_TOTAL 72 | }; 73 | 74 | #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + (CFG_TUD_CDC * TUD_CDC_DESC_LEN) + TUD_VENDOR_DESC_LEN) 75 | 76 | #define EPNUM_CDC_0_NOTIF 0x81 77 | #define EPNUM_CDC_0_OUT 0x02 78 | #define EPNUM_CDC_0_IN 0x83 79 | 80 | #define EPNUM_CDC_1_NOTIF 0x86 81 | #define EPNUM_CDC_1_OUT 0x07 82 | #define EPNUM_CDC_1_IN 0x88 83 | 84 | #define EPNUM_PROBE_OUT 0x04 85 | #define EPNUM_PROBE_IN 0x85 86 | // #define EPNUM_CDC_2_NOTIF 0x85 87 | // #define EPNUM_CDC_2_OUT 0x06 88 | // #define EPNUM_CDC_2_IN 0x86 89 | 90 | uint8_t const desc_fs_configuration[] = 91 | { 92 | // Config number, interface count, string index, total length, attribute, power in mA 93 | TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), 94 | 95 | // 1st CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size. 96 | TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, 4, EPNUM_CDC_0_NOTIF, 8, EPNUM_CDC_0_OUT, EPNUM_CDC_0_IN, 64), 97 | 98 | // 2nd CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size. 99 | // TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, 4, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64), 100 | // 3rd CDC: Interface number, string index, EP notification address and size, EP data address (out, in) and size. 101 | // TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_2, 4, EPNUM_CDC_2_NOTIF, 8, EPNUM_CDC_2_OUT, EPNUM_CDC_2_IN, 64), 102 | // 4th Vendor: Picoprobe interface 103 | TUD_VENDOR_DESCRIPTOR(ITF_NUM_PROBE, 0, EPNUM_PROBE_OUT, EPNUM_PROBE_IN, 64), 104 | TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, 4, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64), 105 | }; 106 | 107 | 108 | // Invoked when received GET CONFIGURATION DESCRIPTOR 109 | // Application return pointer to descriptor 110 | // Descriptor contents must exist long enough for transfer to complete 111 | uint8_t const * tud_descriptor_configuration_cb(uint8_t index) 112 | { 113 | (void) index; // for multiple configurations 114 | 115 | return desc_fs_configuration; 116 | } 117 | 118 | //--------------------------------------------------------------------+ 119 | // String Descriptors 120 | //--------------------------------------------------------------------+ 121 | 122 | // array of pointer to string descriptors 123 | char const* string_desc_arr [] = 124 | { 125 | (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) 126 | "stacksmashing", // 1: Manufacturer 127 | "Tamarin Cable", // 2: Product 128 | "31337", // 3: Serials, should use chip ID 129 | // "Tamarin CDC", // 4: CDC Interface 130 | }; 131 | 132 | static uint16_t _desc_str[32]; 133 | 134 | // Invoked when received GET STRING DESCRIPTOR request 135 | // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete 136 | uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) 137 | { 138 | (void) langid; 139 | 140 | uint8_t chr_count; 141 | 142 | if ( index == 0) 143 | { 144 | memcpy(&_desc_str[1], string_desc_arr[0], 2); 145 | chr_count = 1; 146 | }else 147 | { 148 | // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. 149 | // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors 150 | 151 | if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; 152 | 153 | const char* str = string_desc_arr[index]; 154 | 155 | // Cap at max char 156 | chr_count = strlen(str); 157 | if ( chr_count > 31 ) chr_count = 31; 158 | 159 | // Convert ASCII string into UTF-16 160 | for(uint8_t i=0; i 3 | #include 4 | #include 5 | 6 | #include "pico/stdio.h" 7 | #include "pico/multicore.h" 8 | 9 | #include "hardware/gpio.h" 10 | #include "hardware/uart.h" 11 | #include "hardware/timer.h" 12 | 13 | #include "bsp/board.h" 14 | #include "tusb.h" 15 | 16 | void serprint(const char* format, ...) { 17 | char buf[128]; 18 | va_list args; 19 | va_start (args, format); 20 | vsnprintf(buf, 128, format, args); 21 | va_end (args); 22 | tud_cdc_n_write_str(ITF_CONSOLE, buf); 23 | tud_cdc_n_write_flush(ITF_CONSOLE); 24 | tud_task(); 25 | } 26 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #define ITF_CONSOLE 0 5 | #define ITF_DCSD 1 6 | #define ITF_JTAG 2 7 | 8 | void serprint(const char* format, ...); 9 | --------------------------------------------------------------------------------