{"id":44,"date":"2013-07-27T01:25:00","date_gmt":"2013-07-27T05:25:00","guid":{"rendered":"http:\/\/www.fitzsim.org\/blog\/?p=44"},"modified":"2013-07-30T18:14:28","modified_gmt":"2013-07-30T22:14:28","slug":"advice-serial-term-and-u-boot","status":"publish","type":"post","link":"https:\/\/www.fitzsim.org\/blog\/?p=44","title":{"rendered":"Advice, serial-term and U-Boot"},"content":{"rendered":"\n<p>At work I make heavy use of the GNU Emacs mode <i>serial-term<\/i> to interface with various target boards.  The boards all run U-Boot with a 5 second autoboot timeout.  I often want to power-cycle a group of boards, then stop them all at the U-Boot prompt before they autoboot.  It&#8217;s a race to <i>C-x b<\/i> to all the different buffers in time.  I used to frequently miss some buffers; the boards would start booting, and I would have to wait to issue a software reset.  It became annoying enough that I decided to create an autostop extension. <\/p>\n<p> Luckily, <i>serial-term<\/i> is written in Emacs Lisp, so I can extend it to do exactly what I want.  In this case I want a global &#8220;U-Boot autoboot-stop&#8221; mode. Here&#8217;s what I came up with: <\/p>\n<p> First, a variable to to keep track of whether the mode is enabled or not. <\/p>\n<pre class=\"src src-emacs-lisp\"><span style=\"color: #73d216;\">;; <\/span><span style=\"color: #73d216;\">Detect U-Boot autoboot prompt and stop it.<\/span>\r\n(<span style=\"color: #b4fa70;\">defvar<\/span> <span style=\"color: #fcaf3e;\">u-boot-stop-autoboot-mode<\/span> nil\r\n  <span style=\"color: #e9b96e;\">\"Non-nil if term-mode should prevent U-Boot from autobooting.<\/span>\r\n<span style=\"color: #e9b96e;\">Use the function u-boot-toggle-stop-autoboot-mode to toggle.\"<\/span>)\r\n<\/pre>\n<p> Then the advice itself.  It jacks into the <i>term-emulate-terminal<\/i> process filter to detect the U-Boot autoboot prompt, then sends a newline in response. <\/p>\n<pre class=\"src src-emacs-lisp\">(<span style=\"color: #b4fa70;\">defadvice<\/span> <span style=\"color: #fce94f;\">term-emulate-terminal<\/span>\r\n  (before u-boot-maybe-stop-autoboot activate disable)\r\n  (<span style=\"color: #b4fa70;\">with-current-buffer<\/span> (process-buffer proc)\r\n    (<span style=\"color: #b4fa70;\">when<\/span> (string-match <span style=\"color: #e9b96e;\">\"Hit any key to stop autoboot:\"<\/span> str)\r\n      (message <span style=\"color: #e9b96e;\">\"U-Boot autoboot stopped in buffer %s\"<\/span>\r\n               (buffer-name (current-buffer)))\r\n      (term-send-raw-string <span style=\"color: #e9b96e;\">\"\\n\"<\/span>))))\r\n<\/pre>\n<p> And finally a function to toggle the advice on and off. <\/p>\n<pre class=\"src src-emacs-lisp\">(<span style=\"color: #b4fa70;\">defun<\/span> <span style=\"color: #fce94f;\">u-boot-toggle-stop-autoboot-mode<\/span> ()\r\n  <span style=\"color: #e9b96e;\">\"Toggle whether or not term-mode should interrupt U-Boot autoboot.\"<\/span>\r\n  (interactive)\r\n  (<span style=\"color: #b4fa70;\">if<\/span> u-boot-stop-autoboot-mode\r\n      (<span style=\"color: #b4fa70;\">progn<\/span>\r\n        (ad-disable-advice\r\n         'term-emulate-terminal 'before 'u-boot-maybe-stop-autoboot)\r\n        (ad-update 'term-emulate-terminal)\r\n        (setq u-boot-stop-autoboot-mode nil)\r\n        (message <span style=\"color: #e9b96e;\">\"U-Boot autoboot will not be interrupted\"<\/span>))\r\n    (<span style=\"color: #b4fa70;\">progn<\/span>\r\n      (ad-enable-advice\r\n       'term-emulate-terminal 'before 'u-boot-maybe-stop-autoboot)\r\n      (ad-activate 'term-emulate-terminal)\r\n      (setq u-boot-stop-autoboot-mode t)\r\n      (message <span style=\"color: #e9b96e;\">\"U-Boot autoboot will be interrupted\"<\/span>))))\r\n<\/pre>\n<p> Now I can enable the mode globally with <\/p>\n<p> <i>M-x u-boot-toggle-stop-autoboot-mode<\/i> <\/p>\n<p> and be sure that all the boards will be interrupted in time. <\/p>\n<p> It&#8217;s worth noting that developing this feature was relatively straightforward. I never left my Emacs session; I developed the code right in <code>~\/.emacs.d\/init.el<\/code>, tested it quickly in a <i>terminal<\/i> buffer, and confirmed its operation in my existing <i>serial-term<\/i> buffers.  Most of the development time was spent reading the <i>Info<\/i> documentation for Advice, to figure out how to enable and disable a specific piece of advice. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>At work I make heavy use of the GNU Emacs mode serial-term to interface with various target boards. The boards all run U-Boot with a 5 second autoboot timeout. I often want to power-cycle a group of boards, then stop them all at the U-Boot prompt before they autoboot. It&#8217;s a race to C-x b &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.fitzsim.org\/blog\/?p=44\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Advice, serial-term and U-Boot&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-44","post","type-post","status-publish","format-standard","hentry","category-emacs","entry"],"_links":{"self":[{"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=44"}],"version-history":[{"count":81,"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":127,"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/44\/revisions\/127"}],"wp:attachment":[{"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fitzsim.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}