In-system programming tool for LPC microcontrollers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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"
  17. import argparse
  18. import sys
  19. from alpaca_isp.chips import chips
  20. from alpaca_isp.exceptions import *
  21. from alpaca_isp.lpc import *
  22. def create_lpc(tty, baudrate=DEFAULT_BAUDRATE, clock=DEFAULT_CLOCK,
  23. timeout=DEFAULT_TIMEOUT, control=False, try_sync=None,
  24. verbose=False):
  25. """Create an object of the appropriate LPC subclass for the chip"""
  26. # Open the LPC
  27. lpc = LPC(tty, baudrate=baudrate, clock=clock, timeout=timeout)
  28. lpc.open()
  29. # Enter ISP mode if we've been asked to
  30. if control:
  31. lpc.enter_isp()
  32. # Synchronize with the microcontroller
  33. if verbose:
  34. print("Synchronizing", end="", flush=True)
  35. try:
  36. lpc.synchronize(max_tries=try_sync, verbose=verbose)
  37. except RecvTimeout:
  38. lpc.close()
  39. if verbose:
  40. print(" failed")
  41. sys.exit(1)
  42. if verbose:
  43. print()
  44. # Turn the LPC object into the right type
  45. chip = chips[lpc.part_id]
  46. return chip.family(lpc, chip)
  47. def main():
  48. """Entry point for Alpaca ISP command line tool"""
  49. # Make the argument parser
  50. parser = argparse.ArgumentParser(
  51. description="Flash an LPC microcontroller")
  52. parser.add_argument("file", metavar="file", nargs="+", type=str,
  53. help="Intel HEX file to flash to the microcontroller")
  54. parser.add_argument("tty", metavar="tty", type=str,
  55. help="the tty to which the microcontroller is attached")
  56. parser.add_argument("-b", "--baudrate", type=int,
  57. default=DEFAULT_BAUDRATE,
  58. help="baud rate used for communication (default: %(default)s)")
  59. parser.add_argument("-c", "--clock-khz", type=int, default=DEFAULT_CLOCK,
  60. help="microcontroller's clock frequency in kHz "
  61. "(default: %(default)s)")
  62. parser.add_argument("-t", "--timeout", type=float,
  63. default=DEFAULT_TIMEOUT,
  64. help="timeout for reading data from the microcontroller in "
  65. "seconds (default: %(default)s)")
  66. parser.add_argument("-e", "--erase", action="store_true",
  67. help="erase all the microcontroller's flash before flashing")
  68. parser.add_argument("--no-start", action="store_true",
  69. help="do not start the microcontroller after flashing")
  70. parser.add_argument("--try-sync", type=int,
  71. help="maximum number of tries to synchronize with the "
  72. "microcontroller")
  73. parser.add_argument("--control", action="store_true",
  74. help="control RS232 lines to enter ISP mode (/RST = DTR, /ISP = "
  75. "RTS)")
  76. parser.add_argument("--verify", action="store_true",
  77. help="verify that the data were written correctly after flashing")
  78. # Parse arguments
  79. args = parser.parse_args()
  80. # Open the LPC
  81. lpc = create_lpc(args.tty, baudrate=args.baudrate, clock=args.clock_khz,
  82. timeout=args.timeout, control=args.control, try_sync=args.try_sync,
  83. verbose=True)
  84. # Unlock the chip
  85. lpc.unlock()
  86. # Erase the flash if we've been asked to
  87. if args.erase:
  88. lpc.prepare_write()
  89. lpc.erase()
  90. # TODO: write the file to flash
  91. # Start the program if we haven't been asked not to
  92. if not args.no_start:
  93. lpc.go()
  94. lpc.close()