In-system programming tool for LPC microcontrollers
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

__init__.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # Copyright 2017 Clayton G. Hobbs
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """In-system programming tool for LPC microcontrollers"""
  15. __author__ = "Clayton G. Hobbs"
  16. __version__ = "0.2.0"
  17. import argparse
  18. import sys
  19. import intelhex
  20. from alpaca_isp.chips import chips
  21. from alpaca_isp.exceptions import *
  22. from alpaca_isp.lpc import *
  23. def create_lpc(tty, baudrate=DEFAULT_BAUDRATE, clock=DEFAULT_CLOCK,
  24. timeout=DEFAULT_TIMEOUT, control=False, try_sync=None,
  25. verbose=False):
  26. """Create an object of the appropriate LPC subclass for the chip"""
  27. # Open the LPC
  28. lpc = LPC(tty, baudrate=baudrate, clock=clock, timeout=timeout)
  29. lpc.open()
  30. # Enter ISP mode if we've been asked to
  31. if control:
  32. lpc.enter_isp()
  33. # Synchronize with the microcontroller
  34. if verbose:
  35. print("Synchronizing", end="", flush=True)
  36. try:
  37. lpc.synchronize(max_tries=try_sync, verbose=verbose)
  38. except RecvTimeout:
  39. lpc.close()
  40. if verbose:
  41. print(" failed")
  42. raise
  43. except KeyboardInterrupt:
  44. lpc.close()
  45. if verbose:
  46. print()
  47. raise
  48. if verbose:
  49. print()
  50. # Turn the LPC object into the right type
  51. chip = chips[lpc.part_id]
  52. return chip.family(lpc, chip)
  53. def main():
  54. """Entry point for Alpaca ISP command line tool"""
  55. # Make the argument parser
  56. parser = argparse.ArgumentParser(
  57. description="Flash an LPC microcontroller")
  58. parser.add_argument("file", metavar="file", nargs="+", type=str,
  59. help="Intel HEX file to flash to the microcontroller")
  60. parser.add_argument("tty", metavar="tty", type=str,
  61. help="the tty to which the microcontroller is attached")
  62. parser.add_argument("-b", "--baudrate", type=int,
  63. default=DEFAULT_BAUDRATE,
  64. help="baud rate used for communication (default: %(default)s)")
  65. parser.add_argument("-c", "--clock-khz", type=int, default=DEFAULT_CLOCK,
  66. help="microcontroller's clock frequency in kHz "
  67. "(default: %(default)s)")
  68. parser.add_argument("-t", "--timeout", type=float,
  69. default=DEFAULT_TIMEOUT,
  70. help="timeout for reading data from the microcontroller in "
  71. "seconds (default: %(default)s)")
  72. parser.add_argument("-e", "--erase", action="store_true",
  73. help="erase all the microcontroller's flash before flashing")
  74. parser.add_argument("--no-start", action="store_true",
  75. help="do not start the microcontroller after flashing")
  76. parser.add_argument("--try-sync", type=int,
  77. help="maximum number of tries to synchronize with the "
  78. "microcontroller")
  79. parser.add_argument("-n", "--control", action="store_true",
  80. help="control RS232 lines to enter ISP mode (/RST = DTR, /ISP = "
  81. "RTS)")
  82. parser.add_argument("-r", "--verify", action="store_true",
  83. help="verify that the data were written correctly after flashing")
  84. # Parse arguments
  85. args = parser.parse_args()
  86. # Open the LPC
  87. try:
  88. lpc = create_lpc(args.tty, baudrate=args.baudrate,
  89. clock=args.clock_khz, timeout=args.timeout,
  90. control=args.control, try_sync=args.try_sync, verbose=True)
  91. except (RecvTimeout, KeyboardInterrupt):
  92. sys.exit(1)
  93. # Unlock the chip
  94. lpc.unlock()
  95. # Erase the flash if we've been asked to
  96. if args.erase:
  97. print("Erasing all flash sectors")
  98. lpc.prepare_write()
  99. lpc.erase()
  100. # Load the files
  101. ih = intelhex.IntelHex()
  102. for f in args.file:
  103. ih.fromfile(f, format="hex")
  104. # Write the files to flash
  105. try:
  106. lpc.flash_hex(ih, verbose=True)
  107. except ISPError as e:
  108. if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
  109. print("Error: code read protection is enabled")
  110. print("Unable to flash, exiting")
  111. sys.exit(2)
  112. # Verify that the flash has been written correctly if we've been asked to
  113. if args.verify:
  114. try:
  115. lpc.verify(ih, verbose=True)
  116. except ISPError as e:
  117. if e.args[0] == ReturnCode.CODE_READ_PROTECTION_ENABLED:
  118. print("Error: code read protection is enabled")
  119. print("Unable to verify, continuing")
  120. else:
  121. print("Flash verification failed, exiting")
  122. sys.exit(3)
  123. # Start the program if we haven't been asked not to
  124. if not args.no_start:
  125. print("Starting program")
  126. lpc.go()
  127. lpc.close()