logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

flash.py (5225B)


  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 argcomplete.completers import FilesCompleter
  6. from pathlib import Path
  7. from milc import cli
  8. import qmk.path
  9. from qmk.decorators import automagic_keyboard, automagic_keymap
  10. from qmk.commands import build_environment
  11. from qmk.keyboard import keyboard_completer, keyboard_folder
  12. from qmk.keymap import keymap_completer, locate_keymap
  13. from qmk.flashers import flasher
  14. from qmk.build_targets import KeyboardKeymapBuildTarget, JsonKeymapBuildTarget
  15. def _list_bootloaders():
  16. """Prints the available bootloaders listed in docs.qmk.fm.
  17. """
  18. cli.print_help()
  19. cli.log.info('Here are the available bootloaders:')
  20. cli.echo('\tavrdude')
  21. cli.echo('\tbootloadhid')
  22. cli.echo('\tdfu')
  23. cli.echo('\tdfu-util')
  24. cli.echo('\tmdloader')
  25. cli.echo('\tst-flash')
  26. cli.echo('\tst-link-cli')
  27. cli.log.info('Enhanced variants for split keyboards:')
  28. cli.echo('\tavrdude-split-left')
  29. cli.echo('\tavrdude-split-right')
  30. cli.echo('\tdfu-ee')
  31. cli.echo('\tdfu-split-left')
  32. cli.echo('\tdfu-split-right')
  33. cli.echo('\tdfu-util-split-left')
  34. cli.echo('\tdfu-util-split-right')
  35. cli.echo('\tuf2-split-left')
  36. cli.echo('\tuf2-split-right')
  37. cli.echo('For more info, visit https://docs.qmk.fm/#/flashing')
  38. return False
  39. def _flash_binary(filename, mcu):
  40. """Try to flash binary firmware
  41. """
  42. cli.echo('Flashing binary firmware...\nPlease reset your keyboard into bootloader mode now!\nPress Ctrl-C to exit.\n')
  43. try:
  44. err, msg = flasher(mcu, filename)
  45. if err:
  46. cli.log.error(msg)
  47. return False
  48. except KeyboardInterrupt:
  49. cli.log.info('Ctrl-C was pressed, exiting...')
  50. return True
  51. @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.')
  52. @cli.argument('-b', '--bootloaders', action='store_true', help='List the available bootloaders.')
  53. @cli.argument('-bl', '--bootloader', default='flash', help='The flash command, corresponding to qmk\'s make options of bootloaders.')
  54. @cli.argument('-m', '--mcu', help='The MCU name. Required for HalfKay, HID, USBAspLoader and ISP flashing.')
  55. @cli.argument('-kb', '--keyboard', type=keyboard_folder, completer=keyboard_completer, help='The keyboard to build a firmware for. Ignored when a configurator export is supplied.')
  56. @cli.argument('-km', '--keymap', completer=keymap_completer, help='The keymap to build a firmware for. Ignored when a configurator export is supplied.')
  57. @cli.argument('-n', '--dry-run', arg_only=True, action='store_true', help="Don't actually build, just show the make command to be run.")
  58. @cli.argument('-j', '--parallel', type=int, default=1, help="Set the number of parallel make jobs; 0 means unlimited.")
  59. @cli.argument('-e', '--env', arg_only=True, action='append', default=[], help="Set a variable to be passed to make. May be passed multiple times.")
  60. @cli.argument('-c', '--clean', arg_only=True, action='store_true', help="Remove object files before compiling.")
  61. @cli.subcommand('QMK Flash.')
  62. @automagic_keyboard
  63. @automagic_keymap
  64. def flash(cli):
  65. """Compile and or flash QMK Firmware or keyboard/layout
  66. If a binary firmware is supplied, try to flash that.
  67. If a Configurator export is supplied this command will create a new keymap, overwriting an existing keymap if one exists.
  68. If a keyboard and keymap are provided this command will build a firmware based on that.
  69. If bootloader is omitted the make system will use the configured bootloader for that keyboard.
  70. """
  71. if cli.args.filename and isinstance(cli.args.filename, Path) and cli.args.filename.suffix in ['.bin', '.hex', '.uf2']:
  72. return _flash_binary(cli.args.filename, cli.args.mcu)
  73. if cli.args.bootloaders:
  74. return _list_bootloaders()
  75. # Build the environment vars
  76. envs = build_environment(cli.args.env)
  77. # Handler for the build target
  78. target = None
  79. if cli.args.filename:
  80. # if we were given a filename, assume we have a json build target
  81. target = JsonKeymapBuildTarget(cli.args.filename)
  82. elif cli.config.flash.keyboard and cli.config.flash.keymap:
  83. # if we got a keyboard and keymap, attempt to find it
  84. if not locate_keymap(cli.config.flash.keyboard, cli.config.flash.keymap):
  85. cli.log.error('Invalid keymap argument.')
  86. cli.print_help()
  87. return False
  88. # If we got here, then we have a valid keyboard and keymap for a build target
  89. target = KeyboardKeymapBuildTarget(cli.config.flash.keyboard, cli.config.flash.keymap)
  90. if not target:
  91. cli.log.error('You must supply a configurator export, both `--keyboard` and `--keymap`, or be in a directory for a keyboard or keymap.')
  92. cli.print_help()
  93. return False
  94. target.configure(parallel=cli.config.flash.parallel, clean=cli.args.clean)
  95. return target.compile(cli.args.bootloader, dry_run=cli.args.dry_run, **envs)