flash.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """Compile and flash QMK Firmware
  2. You can compile a keymap already in the repo or using a QMK Configurator export.
  3. A bootloader must be specified.
  4. """
  5. from subprocess import DEVNULL
  6. import sys
  7. from argcomplete.completers import FilesCompleter
  8. from milc import cli
  9. import qmk.path
  10. from qmk.decorators import automagic_keyboard, automagic_keymap
  11. from qmk.commands import compile_configurator_json, create_make_command, parse_configurator_json
  12. from qmk.keyboard import keyboard_completer, keyboard_folder
  13. from qmk.flashers import flasher
  14. def print_bootloader_help():
  15. """Prints the available bootloaders listed in docs.qmk.fm.
  16. """
  17. cli.log.info('Here are the available bootloaders:')
  18. cli.echo('\tavrdude')
  19. cli.echo('\tbootloadhid')
  20. cli.echo('\tdfu')
  21. cli.echo('\tdfu-util')
  22. cli.echo('\tmdloader')
  23. cli.echo('\tst-flash')
  24. cli.echo('\tst-link-cli')
  25. cli.log.info('Enhanced variants for split keyboards:')
  26. cli.echo('\tavrdude-split-left')
  27. cli.echo('\tavrdude-split-right')
  28. cli.echo('\tdfu-ee')
  29. cli.echo('\tdfu-split-left')
  30. cli.echo('\tdfu-split-right')
  31. cli.echo('\tdfu-util-split-left')
  32. cli.echo('\tdfu-util-split-right')
  33. cli.echo('\tuf2-split-left')
  34. cli.echo('\tuf2-split-right')
  35. cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
  36. @cli.argument('filename', nargs='?', arg_only=True, type=qmk.path.FileType('r'), completer=FilesCompleter('.json'), help='A configurator export JSON to be compiled and flashed or a pre-compiled binary firmware file (bin/hex) to be flashed.')
  37. @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
  38. @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
  39. @cli.argument('-m', '--mcu', help='The MCU name. Required for HalfKay, HID, USBAspLoader and ISP flashing.')
  40. @cli.argument('-km', '--keymap', help='The keymap to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
  41. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Use this if you dont have a configurator file. Ignored when a configurator file is supplied.')
  42. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  43. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  44. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  45. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  46. @cli.subcommand('QMK Flash.')
  47. @automagic_keyboard
  48. @automagic_keymap
  49. def flash(cli):
  50. """Compile and or flash QMK Firmware or keyboard/layout
  51. If a binary firmware is supplied, try to flash that.
  52. If a Configurator JSON export is supplied this command will create a new keymap. Keymap and Keyboard arguments
  53. will be ignored.
  54. If no file is supplied, keymap and keyboard are expected.
  55. If bootloader is omitted the make system will use the configured bootloader for that keyboard.
  56. """
  57. if cli.args.filename and cli.args.filename.suffix in ['.bin', '.hex']:
  58. # Try to flash binary firmware
  59. cli.echo('Flashing binary firmware...\nPlease reset your keyboard into bootloader mode now!\nPress Ctrl-C to exit.\n')
  60. try:
  61. err, msg = flasher(cli.args.mcu, cli.args.filename)
  62. if err:
  63. cli.log.error(msg)
  64. return False
  65. except KeyboardInterrupt:
  66. cli.log.info('Ctrl-C was pressed, exiting...')
  67. sys.exit(0)
  68. else:
  69. if cli.args.clean and not cli.args.filename and not cli.args.dry_run:
  70. if cli.config.flash.keyboard and cli.config.flash.keymap:
  71. command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, 'clean')
  72. cli.run(command, capture_output=False, stdin=DEVNULL)
  73. # Build the environment vars
  74. envs = {}
  75. for env in cli.args.env:
  76. if '=' in env:
  77. key, value = env.split('=', 1)
  78. envs[key] = value
  79. else:
  80. cli.log.warning('Invalid environment variable: %s', env)
  81. # Determine the compile command
  82. command = ''
  83. if cli.args.bootloaders:
  84. # Provide usage and list bootloaders
  85. cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
  86. print_bootloader_help()
  87. return False
  88. if cli.args.filename:
  89. # Handle compiling a configurator JSON
  90. user_keymap = parse_configurator_json(cli.args.filename)
  91. keymap_path = qmk.path.keymap(user_keymap['keyboard'])
  92. command = compile_configurator_json(user_keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
  93. cli.log.info('Wrote keymap to {fg_cyan}%s/%s/keymap.c', keymap_path, user_keymap['keymap'])
  94. else:
  95. if cli.config.flash.keyboard and cli.config.flash.keymap:
  96. # Generate the make command for a specific keyboard/keymap.
  97. command = create_make_command(cli.config.flash.keyboard, cli.config.flash.keymap, cli.args.bootloader, parallel=cli.config.flash.parallel, **envs)
  98. elif not cli.config.flash.keyboard:
  99. cli.log.error('Could not determine keyboard!')
  100. elif not cli.config.flash.keymap:
  101. cli.log.error('Could not determine keymap!')
  102. # Compile the firmware, if we're able to
  103. if command:
  104. cli.log.info('Compiling keymap with {fg_cyan}%s', ' '.join(command))
  105. if not cli.args.dry_run:
  106. cli.echo('\n')
  107. compile = cli.run(command, capture_output=False, stdin=DEVNULL)
  108. return compile.returncode
  109. else:
  110. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  111. cli.echo('usage: qmk flash [-h] [-b] [-n] [-kb KEYBOARD] [-km KEYMAP] [-bl BOOTLOADER] [filename]')
  112. return False