|
| 1 | +// +build nrf52840,nrf52840_reset_uf2 |
| 2 | + |
| 3 | +package machine |
| 4 | + |
| 5 | +import ( |
| 6 | + "device/arm" |
| 7 | + "device/nrf" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + DFU_MAGIC_SERIAL_ONLY_RESET = 0x4e |
| 12 | + DFU_MAGIC_UF2_RESET = 0x57 |
| 13 | + DFU_MAGIC_OTA_RESET = 0xA8 |
| 14 | +) |
| 15 | + |
| 16 | +// checkShouldReset is called by the USB-CDC implementation to check whether to |
| 17 | +// reset into the bootloader/OTA and if so, resets the chip appropriately. |
| 18 | +func checkShouldReset() { |
| 19 | + if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 { |
| 20 | + EnterUF2Bootloader() |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// EnterSerialBootloader resets the chip into the serial bootloader. After |
| 25 | +// reset, it can be flashed using serial/nrfutil. |
| 26 | +func EnterSerialBootloader() { |
| 27 | + arm.DisableInterrupts() |
| 28 | + nrf.POWER.GPREGRET.Set(DFU_MAGIC_SERIAL_ONLY_RESET) |
| 29 | + arm.SystemReset() |
| 30 | +} |
| 31 | + |
| 32 | +// EnterUF2Bootloader resets the chip into the UF2 bootloader. After reset, it |
| 33 | +// can be flashed via nrfutil or by copying a UF2 file to the mass storage device |
| 34 | +func EnterUF2Bootloader() { |
| 35 | + arm.DisableInterrupts() |
| 36 | + nrf.POWER.GPREGRET.Set(DFU_MAGIC_UF2_RESET) |
| 37 | + arm.SystemReset() |
| 38 | +} |
| 39 | + |
| 40 | +// EnterOTABootloader resets the chip into the bootloader so that it can be |
| 41 | +// flashed via an OTA update |
| 42 | +func EnterOTABootloader() { |
| 43 | + arm.DisableInterrupts() |
| 44 | + nrf.POWER.GPREGRET.Set(DFU_MAGIC_OTA_RESET) |
| 45 | + arm.SystemReset() |
| 46 | +} |
0 commit comments