{"id":41,"date":"2007-09-05T22:37:38","date_gmt":"2007-09-05T20:37:38","guid":{"rendered":"http:\/\/lallafa.de\/blog\/index.php\/2007\/09\/05\/dtvtrans-mac-dtv2ser\/"},"modified":"2007-09-11T22:21:13","modified_gmt":"2007-09-11T20:21:13","slug":"dtvtrans-mac-dtv2ser","status":"publish","type":"post","link":"https:\/\/lallafa.de\/blog\/2007\/09\/dtvtrans-mac-dtv2ser\/","title":{"rendered":"dtvtrans + mac -> dtv2ser"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2007\/09\/dtv2ser-tests1-thumb.jpg?resize=320%2C240\" height=\"240\" width=\"320\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"dtv2ser prototype\" title=\"dtv2ser prototype\" \/><br \/>\nMacs don&#8217;t have Parallel Ports. That&#8217;s no problem for using nowaday devices but its a real disadvantage if you like to hack hardware and need to access ports or like to work on toggle-bits level with any kind of homebrew hardware.<\/p>\n<p>I had a look at the cool DTV transfer tool called <a href=\"http:\/\/www.kahlin.net\/daniel\/dtv\/cable.php\">dtvtrans<\/a> written by TLR. There you build a parallel to joystick port cable and then you can transfer memory blocks directly from and to the DTV&#8217;s RAM or ROM. This is ultra handy for development and fast testing&#8230;<\/p>\n<p>But <em>without<\/em> a parport on my Mac, what to do?<br \/>\n<!--more--><\/p>\n<p>I remembered an older project I built that was presented in the german c&#8217;t magazine called <a href=\"http:\/\/www.heise.de\/ct\/projekte\/com2lan\/\">com2lan<\/a> where a serial port was connected to an AVR microcontroller (an ATmega8535) to do some simple tasks like measuring the temperature. Back in 2005, I already implemented a firmware called <a href=\"https:\/\/www.lallafa.de\/bp\/lancre.html\">lancre<\/a> for this little hardware.<\/p>\n<p>With an AVR you can implement your code in C on the host, cross compile it there and then transfer and flash it via an ISP to the device. The AVR has lots of I\/O pins which can be controlled directly in your program&#8230;<\/p>\n<p>So, the <strong>idea<\/strong> was: why not connect the DTV directly to the AVR, do the dtvtrans bit baning there and then use the serial port of the AVR to transfer the data to my Mac. With a standard USB-to-Serial Adapter (I use the well supported Keyspan <a href=\"http:\/\/www.keyspan.com\/products\/usa19hs\/homepage.spml\">USA-19HS<\/a>) you can easily connect any Mac (or other new PCs) to the AVR&#8217;s serial port.<\/p>\n<p>A new project was born, called <strong>dtv2ser!!<br \/>\n<\/strong><br \/>\nThe Dataflow of the project will look like this:<\/p>\n<pre>Mac ---[USB]--- USA-19HS ---[RS232]--- dtv2ser ---[dtvtrans]--- DTV\r\n<\/pre>\n<p>Since many other new PCs also lack a parport, I think this will be a nice add on for the DTV community and a real alternative to the initial dtvtrans parallel cable approach \ud83d\ude09<br \/>\n<span style=\"font-size:13pt;\"><strong><br \/>\nHardware<br \/>\n<\/strong><\/span><br \/>\nWith com2lan&#8217;s schematic at hand, I quickly found some free I\/O pins and soldered a connector cable with a DB9 joystick plug for the DTV<\/p>\n<p>Here you can see the wiring of the cable:<\/p>\n<pre>\r\nAVR Port Pint      dtvtrans     DB9 Pin DTV\r\nPA3                 RESET          9\r\nPA2                  ACK           6\r\nPC7                  CLK           4\r\nPC6                  D2            3\r\nPC5                  D1            2\r\nPC4                  D0            1\r\nGND                  GND           8\r\n<\/pre>\n<p>Note: You have to connect the DTV&#8217;s Reset Pin to DB9 Pin 9 which is a non-standard mapping on the joystick port. This will allow you to reset the DTV remotely from the host.<\/p>\n<p>Here is a snapshot of my experimental setup on my desk: On the left is my MBP with the Keyspan serial adapter connected to the com2lan Board (inside the silver housing with blue LCD top put back). From there the colorful wires connect to my DTV mod. I additionally connected the ISP (with USB) to the com2lan to do quick updates of the firmware while developing:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2007\/09\/dtv2ser-tests1.jpg?resize=320%2C240\" height=\"240\" width=\"320\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"dtv2ser experimental setup\" title=\"dtv2ser experimental setup\" \/><\/p>\n<p>And another close up of the com2lan box and the DTV:<\/p>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/lallafa.de\/blog\/wp-content\/uploads\/2007\/09\/dtv2ser-tests2.jpg?resize=320%2C240\" height=\"240\" width=\"320\" border=\"1\" hspace=\"4\" vspace=\"4\" alt=\"Dtv2Ser-Tests2\" \/><\/p>\n<p><span style=\"font-size:13pt;\"><strong>Software<\/strong><\/span><\/p>\n<p>Now the hardware was ready to go, the software needs to be done&#8230;<\/p>\n<p>I started with building an AVR toolchain, so all current versions of avr-gcc, avr-binutils and avrdude are available.<br \/>\nAs a starting point I used my <a href=\"https:\/\/www.lallafa.de\/bp\/lancre.html\">lancre<\/a> firmware and TLR&#8217;s source of <a href=\"http:\/\/www.kahlin.net\/daniel\/dtv\/cable.php\">dtvtrans-0.6<\/a> as a reference implementation of the dtvtrans protocol.<\/p>\n<p>The dtvtrans core was ported quickly to pin i\/O and I soon had some code running that sends a command from the AVR via dtvtrans to the DTV. Yohoo(!), a proof of concept was done and it was really a good feeling to see the flashing border on DTV while transferring data \ud83d\ude09<\/p>\n<p>The data now needs to be transferred to the Mac and I chose the serial port that is available on each AVR for that&#8230; Some digging in the specs revealed that my USB adapter supports up to 230 Kbps&#8230; so why not use it \ud83d\ude09 That would be a max of approx. 20 kbyte\/s for transfer and matches the 11-16 kbyte\/s reported for a parallel dtvtrans transfer. Some data transfer tests later (done with the old but still good <a href=\"http:\/\/homepage.mac.com\/dalverson\/zterm\/\">Zterm<\/a>) revealed that some sort of transfer control is needed to handle these data speeds. I chose the reliable hardware RTS\/CTS handshaking to control when to send or not and achieved up to 20 kbyte\/s with pure send and receives from the AVR to the Mac. Just add two more wires to the AVR and you can do handshaking from there. Fortunately the MAX232A level converter for RS232 already has 2 inputs and 2 outputs: TxD, RxD, CTS and, RTS.<\/p>\n<p>For the actual device I&#8217;d like it to be very similar to a good ol&#8217; modem. There are two modes involved: <em>command<\/em> and <em>transfer<\/em> mode. In command mode the device (here dtv2ser) waits for commands from the host (here the Mac). You then enter a short and human readable sequence of chars and finish them with a new line. The device processes the command and gives a result wether the command is reasonable. If everything is ok then the command performs some actions and optionally returns a result.<\/p>\n<p>If the command is a transfer command (e.g. read or write memory from the DTV) then the device enters transfer mode. Now there are only raw blocks of data that are transferred between the host and the device. In <strong>dtv2ser<\/strong>, a transfer command fully specifies the amount of data that will be transferred so both sides know when to enter command mode again. Error conditions (e.g. missing data or dtvtrans problems) will lead to missing transferred bytes that will trigger a receiption timeout on the host side.<\/p>\n<p>Here is an example:<\/p>\n<pre>\r\n# dtv2ser is in command mode\r\n\r\n# mac sends command: read memory, 00=ram, 000000=start address, 000100=length\r\nmac: r 00 000000 000100 +newline      \r\n\r\n# dtv2ser replies with 00=command ok\r\nd2s: 00                               \r\n\r\n# now dtv2ser enters transfer mode\r\nd2s: ... sends 0x100 bytes ...         # raw transfer of the bytes\r\nmac: ... reveives 0x100 bytes ...\r\n\r\n# dtv2ser automatically leaves transfer mode and reenters command mode\r\n\r\n# mac sends command: return transfer status\r\nmac: tr +newline\r\n# 00=transfer ok\r\nd2s: 00\r\n<\/pre>\n<p>First experiments with this approach worked really well&#8230; and thus I&#8217;ll keep this approach. With this basic mechanism working, I really needed a host tool on my Mac to control the dtv2ser device. Something similar to the dtvtrans program that is supplied to control the dtvtrans cable in TLR&#8217;s approach, but does talking with the dtv2ser device instead.<\/p>\n<p>First I thought about extending the dtvtrans C++ code but then I thought that (portable) serial handling in C++ is really too much to code for now. I want to start more quickly and looked at my favorite scripting language <a href=\"http:\/\/www.python.org\/\">Python<\/a> if serial support is availble. It is! Not out of box but on SF.net: <a href=\"http:\/\/pyserial.sourceforge.net\/\">PySerial<\/a>. Just download, unpack and install and volia: You have a serial class that manages the serial port including RTS\/CTS handling and timeouts! Considering that 20 kbyte\/s transfer are really slow, even compared to the execution time of scripting language, I think the Python code will do fine as a host tool.<\/p>\n<p>With serial commands in Python I really quickly hacked the first version of <strong>dtv2sertrans<\/strong> the host tool of the dtv2ser device. I added the basic methods of dtvtrans and also cloned its syntax:<\/p>\n<pre>\r\n# reset the DTV\r\ndtv2sertrans reset\r\n\r\n# read DTV RAM starting at 0x400 of size 0x200 and store a PRG file on disc\r\ndtv2sertrans read 0x400,0x200 dump.prg\r\n\r\n# write the local program file to the DTV RAM starting at the embedded address\r\ndtv2sertrans write myprog.prg\r\n<\/pre>\n<p>The handling of the host tool is now similar to the usage of dtvtrans and behaves like that (I believe, because I never had a chance to run the original one ;). The only difference is the back end&#8230;<\/p>\n<p><span style=\"font-size:13pt;\"><strong>Future<br \/>\n<\/strong><\/span><br \/>\nMy first experiments with the dtv2ser serial-to-dtvtrans bridge were very successful and really motivated me to bring this along in a full open-source project.<\/p>\n<p>Things that need to be done for the first release are:<\/p>\n<ul>\n<li>A dedicated HW board: Using an ATmega8 and a MAX232A (for serial level conversion), the board only needs a 14.547600 MHz quartz (for exact baud rate matching) and some LEDs for status. Should be a cheap and small design.<\/li>\n<li>A schematic in Eagle &#8211; good for publication. Currently, all my schematics are drawn on paper &#8211; this project is a good opportunity to learn <a href=\"http:\/\/www.cadsoft.de\/\">eagle<\/a> \ud83d\ude09<\/li>\n<li>Source Code clean up. Both the firmware and the host utility got quite ugly while experimenting and definitely need more structure for a release&#8230;<\/li>\n<li>Checking Error Conditions. There are many cases that something times out or errors&#8230; need to handle these&#8230;.<\/li>\n<li>Testing&#8230;. ok, it&#8217;s an OSS project, so you can take this one, too \ud83d\ude09<\/li>\n<\/ul>\n<p>I started with most points already, so the first release is not far away&#8230;. Just stay tuned and watch out for the new dtv2ser project page here on my blog!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Macs don&#8217;t have Parallel Ports. That&#8217;s no problem for using nowaday devices but its a real disadvantage if you like to hack hardware and need to access ports or like to work on toggle-bits level with any kind of homebrew &hellip; <a href=\"https:\/\/lallafa.de\/blog\/2007\/09\/dtvtrans-mac-dtv2ser\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[7,8,6],"tags":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-dtv64","category-hardware","category-mac-stuff"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/piBMF-F","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":0,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lallafa.de\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}