Skip to content

Commit 0655086

Browse files
authored
machine/nrf: implement auto-reset over USB for nrf52840
1 parent bf57ae0 commit 0655086

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

src/machine/usb_nrf52840.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,6 @@ func cdcSetup(setup usbSetup) bool {
384384
return false
385385
}
386386

387-
func checkShouldReset() {
388-
if usbLineInfo.dwDTERate == 1200 && usbLineInfo.lineState&usb_CDC_LINESTATE_DTR == 0 {
389-
// TODO: reset here
390-
}
391-
}
392-
393387
func sendUSBPacket(ep uint32, data []byte) {
394388
count := len(data)
395389
copy(udd_ep_in_cache_buffer[ep][:], data)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// +build nrf52840,!nrf52840_reset_uf2
2+
3+
package machine
4+
5+
func checkShouldReset() {
6+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

targets/circuitplay-bluefruit.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"inherits": ["nrf52840"],
3-
"build-tags": ["circuitplay_bluefruit"],
3+
"build-tags": ["circuitplay_bluefruit","nrf52840_reset_uf2"],
4+
"flash-1200-bps-reset": "true",
45
"flash-method": "msd",
56
"msd-volume-name": "CPLAYBTBOOT",
67
"msd-firmware-name": "firmware.uf2",

targets/clue_alpha.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"inherits": ["nrf52840"],
3-
"build-tags": ["clue_alpha"],
3+
"build-tags": ["clue_alpha","nrf52840_reset_uf2"],
4+
"flash-1200-bps-reset": "true",
45
"flash-method": "msd",
56
"msd-volume-name": "FTHR840BOOT",
67
"msd-firmware-name": "firmware.uf2",

0 commit comments

Comments
 (0)