#!/usr/bin/env python3 # tools/lwl/ocdconsole.py # # Copyright (C) 2019 Dave Marples. All rights reserved. # Author: Dave Marples # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in # the documentation and/or other materials provided with the # distribution. # 3. Neither the name NuttX nor the names of its contributors may be # used to endorse or promote products derived from this software # without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # # # Usage # ===== # # No special python modules are needed, it should be possible to run the # application simply as shown below; # # ------------------------------------------ # $ ./ocdconsole.py # ==Link Activated # # nsh> # nsh> help # help usage: help [-v] [] # # ? echo exit hexdump ls mh sleep xd # cat exec help kill mb mw usleep # nsh> # ------------------------------------------ # # This code is designed to be 'hardy' and will survive a shutdown and # restart of the openocd process. When your target application # changes then the location of the upword and downword may change, # so they are re-searched for again. To speed up the start process # consider putting those words at fixed locations (e.g. via the # linker file) and referencing them directly. # LWL_ACTIVESHIFT = 31 LWL_DNSENSESHIFT = 30 LWL_UPSENSESHIFT = 29 LWL_OCTVALSHIFT = 27 LWL_PORTSHIFT = 24 LWL_PORTMASK = (7<=length): print("ERROR: Cannot find signature\r") exit(1) # We have the base address, so get the variables themselves # ========================================================= downwordaddr=baseaddr+downwordaddr+4 upwordaddr=downwordaddr+4 downword=LWL_ACTIVE # Now wake up the link...keep on trying if it goes down # ===================================================== while True: ocd.writeVariable(downwordaddr, downword) upword = ocd.readVariable(upwordaddr) if (upword&LWL_ACTIVE!=0): print("==Link Activated\r") break except (BrokenPipeError, ConnectionRefusedError, ConnectionResetError) as e: raise e # Now run the comms loop until something fails # ============================================ try: while True: ocd.writeVariable(downwordaddr, downword) upword = ocd.readVariable(upwordaddr) if (upword&LWL_ACTIVE==0): print("\r==Link Deactivated\r") break if kbhit(): charin = sys.stdin.read(1) if (ord(charin)==3): sys.exit(0) if (downword&LWL_DNSENSEBIT): downword=(downword&LWL_UPSENSEBIT) else: downword=(downword&LWL_UPSENSEBIT)|LWL_DNSENSEBIT downword|=(LWL_PORT_CONSOLE<>LWL_PORTSHIFT if (incomingPort==LWL_PORT_CONSOLE): incomingBytes=(upword&LWL_OCTVALMASK)>>LWL_OCTVALSHIFT if (incomingBytes>=1): dooutput(upword&255); if (incomingBytes>=2): dooutput((upword>>8)&255); if (incomingBytes==3): dooutput((upword>>16)&255); if (downword&LWL_UPSENSEBIT): downword = downword&~LWL_UPSENSEBIT else: downword = downword|LWL_UPSENSEBIT except (ConnectionResetError, ConnectionResetError, BrokenPipeError) as e: print("\r==Link Lost\r") raise e except (BrokenPipeError, ConnectionRefusedError, ConnectionResetError) as e: time.sleep(1) continue finally: termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)