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.

263 lines
13 KiB

  1. # ISP Flashing Guide
  2. ISP flashing (also known as ICSP flashing) is the process of programming a microcontroller directly. This allows you to replace the bootloader, or change the "fuses" on the controller, which control a number of hardware- and software-related functions, such as the speed of the controller, how it boots, and other options.
  3. The main use of ISP flashing for QMK is flashing or replacing the bootloader on your AVR-based controller (Pro Micros, or V-USB chips).
  4. ?> This is only for programming AVR based boards, such as the Pro Micro or other ATmega controllers. It is not for Arm controllers, such as the Proton C.
  5. ## Dealing with Corrupted Bootloaders
  6. If you're having trouble flashing/erasing your board, and running into cryptic error messages like any of the following for a DFU based controller:
  7. libusb: warning [darwin_transfer_status] transfer error: timed out
  8. dfu.c:844: -ETIMEDOUT: Transfer timed out, NAK 0xffffffc4 (-60)
  9. atmel.c:1627: atmel_flash: flash data dfu_download failed.
  10. atmel.c:1629: Expected message length of 1072, got -60.
  11. atmel.c:1434: Error flashing the block: err -2.
  12. ERROR
  13. Memory write error, use debug for more info.
  14. commands.c:360: Error writing memory data. (err -4)
  15. dfu.c:844: -EPIPE: a) Babble detect or b) Endpoint stalled 0xffffffe0 (-32)
  16. Device is write protected.
  17. dfu.c:252: dfu_clear_status( 0x7fff4fc2ea80 )
  18. atmel.c:1434: Error flashing the block: err -2.
  19. ERROR
  20. Memory write error, use debug for more info.
  21. commands.c:360: Error writing memory data. (err -4)
  22. Or, if you see this sort of message for a Pro Micro based controller:
  23. avrdude: butterfly_recv(): programmer is not responding
  24. avrdude: butterfly_recv(): programmer is not responding
  25. avrdude: verification error, first mismatch at byte 0x002a
  26. 0x2b != 0x75
  27. avrdude: verification error; content mismatch
  28. avrdude: verification error; content mismatch
  29. You're likely going to need to ISP flash your board/device to get it working again.
  30. ## Hardware Needed
  31. You'll need one of the following to actually perform the ISP flashing (followed by the protocol they use):
  32. * [SparkFun PocketAVR](https://www.sparkfun.com/products/9825) - (USB Tiny)
  33. * [USBtinyISP AVR Programmer Kit](https://www.adafruit.com/product/46) - (USB Tiny)
  34. * [USBasp](https://www.fischl.de/usbasp/) - (usbasp)
  35. * [Teensy 2.0](https://www.pjrc.com/store/teensy.html) - (avrisp)
  36. * [Pro Micro](https://www.sparkfun.com/products/12640) - (avrisp)
  37. * [Bus Pirate](https://www.adafruit.com/product/237) - (buspirate)
  38. There are other devices that can be used to ISP flash, but these are the main ones. Also, all product links are to the official versions. You can source them elsewhere.
  39. You'll also need something to wire your "ISP Programmer" to the device that you're programming. Some PCBs may have ISP headers that you can use directly, but this often isn't the case, so you'll likely need to solder to the controller itself or to different switches or other components.
  40. ### The ISP Firmware
  41. The Teensy and Pro Micro controllers will need you to flash the ISP firmware to the controllers before you can use them as an ISP programmer. The rest of the hardware should come preprogrammed. So, for these controllers, download the correct hex file, and flash it first.
  42. * Teensy 2.0: [`util/teensy_2.0_ISP_B0.hex`](https://github.com/qmk/qmk_firmware/blob/master/util/teensy_2.0_ISP_B0.hex) (`B0`)
  43. * Pro Micro: [`util/pro_micro_ISP_B6_10.hex`](https://github.com/qmk/qmk_firmware/blob/master/util/pro_micro_ISP_B6_10.hex) (`10/B6`)
  44. Once you've flashed your controller, you won't need this hex file anymore.
  45. ## Software Needed
  46. The QMK Toolbox can be used for most (all) of this.
  47. However, you can grab the [Teensy Loader](https://www.pjrc.com/teensy/loader.html) to flash your Teensy 2.0 board, if you are using that. Or you can use `avrdude` (installed as part of `qmk_install.sh`), or [AVRDUDESS](https://blog.zakkemble.net/avrdudess-a-gui-for-avrdude/) (for Windows) to flash the Pro Micro, and the ISP flashing.
  48. ## Wiring
  49. This is pretty straight-forward - we'll be connecting like-things to like-things in the following manner.
  50. ### SparkFun Pocket AVR
  51. PocketAVR RST <-> Keyboard RESET
  52. PocketAVR SCLK <-> Keyboard B1 (SCLK)
  53. PocketAVR MOSI <-> Keyboard B2 (MOSI)
  54. PocketAVR MISO <-> Keyboard B3 (MISO)
  55. PocketAVR VCC <-> Keyboard VCC
  56. PocketAVR GND <-> Keyboard GND
  57. ### USBasp
  58. USBasp RST <-> Keyboard RESET
  59. USBasp SCLK <-> Keyboard B1 (SCLK)
  60. USBasp MOSI <-> Keyboard B2 (MOSI)
  61. USBasp MISO <-> Keyboard B3 (MISO)
  62. USBasp VCC <-> Keyboard VCC
  63. USBasp GND <-> Keyboard GND
  64. ### Teensy 2.0
  65. Teensy B0 <-> Keyboard RESET
  66. Teensy B1 <-> Keyboard B1 (SCLK)
  67. Teensy B2 <-> Keyboard B2 (MOSI)
  68. Teensy B3 <-> Keyboard B3 (MISO)
  69. Teensy VCC <-> Keyboard VCC
  70. Teensy GND <-> Keyboard GND
  71. !> Note that the B0 pin on the Teensy is wired to the RESET/RST pin on the keyboard's controller. ***DO NOT*** wire the RESET pin on the Teensy to the RESET on the keyboard.
  72. ### Pro Micro
  73. Pro Micro 10 (B6) <-> Keyboard RESET
  74. Pro Micro 15 (B1) <-> Keyboard B1 (SCLK)
  75. Pro Micro 16 (B2) <-> Keyboard B2 (MOSI)
  76. Pro Micro 14 (B3) <-> Keyboard B3 (MISO)
  77. Pro Micro VCC <-> Keyboard VCC
  78. Pro Micro GND <-> Keyboard GND
  79. !> Note that the 10/B6 pin on the Pro Micro is wired to the RESET/RST pin on the keyboard's controller. ***DO NOT*** wire the RESET pin on the Pro Micro to the RESET on the keyboard.
  80. ## Flashing Your Keyboard
  81. After you have your ISP programmer set up, and wired to your keyboard, it's time to flash your keyboard.
  82. ### The Bootloader File
  83. The simplest and quickest way to get things back to normal is to flash only a bootloader to the keyboard. Once this is done, you can connect the keyboard normally and flash the keyboard like you normally would.
  84. You can find the stock bootloaders in the [`util/` folder](https://github.com/qmk/qmk_firmware/tree/master/util). Be sure to flash the correct bootloader for your chip:
  85. * **Atmel DFU**
  86. * [ATmega16U4](https://github.com/qmk/qmk_firmware/blob/master/util/bootloader_atmega16u4_1.0.1.hex)
  87. * [ATmega32U4](https://github.com/qmk/qmk_firmware/blob/master/util/bootloader_atmega32u4_1.0.0.hex)
  88. * [AT90USB64](https://github.com/qmk/qmk_firmware/blob/master/util/bootloader_at90usb64_1.0.0.hex)
  89. * [AT90USB128](https://github.com/qmk/qmk_firmware/blob/master/util/bootloader_at90usb128_1.0.1.hex)
  90. * **Caterina**
  91. * [Pro Micro (5V/16MHz)](https://github.com/sparkfun/Arduino_Boards/blob/master/sparkfun/avr/bootloaders/caterina/Caterina-promicro16.hex)
  92. * [Pro Micro (3.3V/8MHz)](https://github.com/sparkfun/Arduino_Boards/blob/master/sparkfun/avr/bootloaders/caterina/Caterina-promicro8.hex)
  93. * **BootloadHID (PS2AVRGB)**
  94. * [ATmega32A](https://github.com/qmk/qmk_firmware/blob/master/util/bootloader_ps2avrgb_bootloadhid_1.0.1.hex)
  95. If you're not sure what your board uses, look in the `rules.mk` file for the keyboard in QMK. The `MCU` and `BOOTLOADER` lines will have the value you need. It may differ between different versions of the board.
  96. ### Production Techniques
  97. If you'd like to flash both the bootloader **and** the regular firmware at the same time, there are two options to do so. Manually, or with the `:production` target when compiling.
  98. To do this manually:
  99. 1. Open the original firmware .hex file in a text editor
  100. 2. Remove the last line (which should be `:00000001FF` - this is an EOF message)
  101. 3. Copy the entire bootloader's contents onto a new line (with no empty lines between) and paste it at the end of the original file
  102. 4. Save it as a new file by naming it `<keyboard>_<keymap>_production.hex`
  103. ?> It's possible to use other bootloaders here in the same way, but __you need a bootloader__, otherwise you'll have to use ISP again to write new firmware to your keyboard.
  104. #### Create QMK DFU Bootloader and Production images
  105. You can create the firmware, the QMK DFU Bootloader and the production firmware images for the board using the `:production` target when compiling. Once this is done, you'll see three files:
  106. * `<keyboard>_<keymap>.hex`
  107. * `<keyboard>_<keymap>_bootloader.hex`
  108. * `<keyboard>_<keymap>_production.hex`
  109. The QMK DFU bootloader has only really been tested on `atmega32u4` controllers (such as the AVR based Planck boards, and the Pro Micro), and hasn't been tested on other controllers. However, it will definitely not work on V-USB controllers, such as the `atmega32a` or `atmega328p`.
  110. You can flash either the bootloader or the production firmware file. The production firmware file will take a lot longer to flash, since it's flashing a lot more data.
  111. ?> Note: You should stay with the same bootloader. If you're using DFU already, switching to QMK DFU is fine. But flashing QMK DFU onto a Pro Micro, for instance, has additional steps needed.
  112. ## Flashing Your Bootloader/Production File
  113. Make sure your keyboard is unplugged from any device, and plug in your ISP Programmer.
  114. If you want to change bootloader types, You'll need to use the command line.
  115. ### QMK Toolbox
  116. 1. `AVRISP device connected` or `USB Tiny device connected` will show up in yellow
  117. 2. Select the correct bootloader/production .hex file with the `Open` dialog (spaces can't be in the path)
  118. 3. Be sure the correct `Microcontroller` option for the keyboard you're flashing (not the ISP programmer) is selected
  119. 4. Hit `Flash`
  120. 5. Wait, as nothing will output for a while, especially with production files
  121. If the verification and fuse checks are ok, you're done! Your board may restart automatically, otherwise, unplug your Teensy and plug in your keyboard - you can leave your Teensy wired to your keyboard while testing things, but it's recommended that you desolder it/remove the wiring once you're sure everything works.
  122. ### Command Line
  123. Open a terminal (`cmd` on Windows, for instance) and navigate to your where your modified .hex file is. We'll pretend this file is called `main.hex`, and that your Teensy 2.0 is on the `COM3` port - if you're unsure, you can open your Device Manager, and look for `Ports > USB Serial Device`. Use that COM port here. You can confirm it's the right port with:
  124. avrdude -c avrisp -P COM3 -p atmega32u4
  125. and you should get something like the following output:
  126. avrdude: AVR device initialized and ready to accept instructions
  127. Reading | ################################################## | 100% 0.02s
  128. avrdude: Device signature = 0x1e9587
  129. avrdude: safemode: Fuses OK
  130. avrdude done. Thank you.
  131. Since our keyboard uses an `atmega32u4` (common), that is the chip we'll specify. This is the full command:
  132. avrdude -c avrisp -P COM3 -p atmega32u4 -U flash:w:main.hex:i
  133. If your board uses an `atmega32a` (e.g. on a jj40), the command is this (the extra code at the end sets the fuses correctly):
  134. avrdude -c avrisp -P COM3 -p atmega32 -U flash:w:main.hex:i -U hfuse:w:0xD0:m -U lfuse:w:0x0F:m
  135. You should see a couple of progress bars, then you should see:
  136. avrdude: verifying ...
  137. avrdude: 32768 bytes of flash verified
  138. avrdude: safemode: Fuses OK
  139. avrdude done. Thank you.
  140. Which means everything should be ok! Your board may restart automatically, otherwise, unplug your Teensy and plug in your keyboard - you can leave your Teensy wired to your keyboard while testing things, but it's recommended that you desolder it/remove the wiring once you're sure everything works.
  141. If you're using a SparkFun PocketAVR Programmer, or another USB Tiny based ISP programmer, you will want to use something like this:
  142. avrdude -c usbtiny -P usb -p atmega32u4
  143. #### Advanced: Changing Fuses
  144. If you're switching bootloaders, such as flashing QMK DFU on a Pro Micro, you will need to change the fuses, in additional to flashing the bootloader hex file. This is because `caterina` (the Pro Micro bootloader) and `dfu` handle the startup routines differently, and that behavior is controlled by the fuses.
  145. !> This is one area that it is very important to be careful, as changing fuses is one of the ways that you can permanently brick your controller.
  146. For this, we are assuming the 5V 16MHz versions of the `atmega32u4` (such as the 5V Pro Micro).
  147. For DFU on the `atmega32u4`, these are the fuse settings that you want:
  148. | Fuse | Setting |
  149. |----------|------------------|
  150. | Low | `0x5E` |
  151. | High | `0xD9` or `0x99` |
  152. | Extended | `0xC3` |
  153. The High fuse can be 0xD9 or 0x99. The difference is that 0xD9 disables JTAG, which QMK Firmware disables via software as well, while 0x99 doesn't disable JTAG.
  154. To set this add `-U lfuse:w:0x5E:m -U hfuse:w:0xD9:m -U efuse:w:0xC3:m` to your command. So the final command should look something like:
  155. avrdude -c avrisp -P COM3 -p atmega32u4 -U flash:w:main.hex:i -U lfuse:w:0x5E:m -U hfuse:w:0xD9:m -U efuse:w:0xC3:m
  156. For Caterina on the `atmega32u4`, these are the fuse settings that you want:
  157. | Fuse | Setting|
  158. |----------|--------|
  159. | Low | `0xFF` |
  160. | High | `0xD8` |
  161. | Extended | `0xCB` |
  162. To set this add `-U lfuse:w:0xFF:m -U hfuse:w:0xD8:m -U efuse:w:0xCB:m` to your command. So the final command should look something like:
  163. avrdude -c avrisp -P COM3 -p atmega32u4 -U flash:w:main.hex:i -U lfuse:w:0xFF:m -U hfuse:w:0xD8:m -U efuse:w:0xCB:m
  164. If you are using a different controller or want different configuration, you can use [this AVR Fuse Calculator](https://www.engbedded.com/fusecalc/) to find a better value for you.
  165. ## Help
  166. If you have any questions/problems, feel free to [open an issue](https://github.com/qmk/qmk_firmware/issues/new)!