Skip to content

Commit 4ad21d4

Browse files
maidnlfacchinm
authored andcommitted
define the function to go in bootloader and moved in a separate header boot.h
1 parent de91b64 commit 4ad21d4

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

cores/arduino/boot.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "boot.h"
2+
3+
4+
/* call this function when you want to force the board to go in bootloader */
5+
void goBootloader() {
6+
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK;
7+
BOOT_DOUBLE_TAP_DATA = DOUBLE_TAP_MAGIC;
8+
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK;
9+
((R_USB_FS0_Type*)R_USB_FS0_BASE)->SYSCFG_b.DPRPU = 0;
10+
NVIC_SystemReset();
11+
while (1); // WDT will fire here
12+
}

cores/arduino/boot.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "Arduino.h"
2+
3+
#ifndef BOOTLOADER_INCLUDE_H
4+
#define BOOTLOADER_INCLUDE_H
5+
6+
/* Key code for writing PRCR register. */
7+
#define BSP_PRV_PRCR_KEY (0xA500U)
8+
#define BSP_PRV_PRCR_PRC1_UNLOCK ((BSP_PRV_PRCR_KEY) | 0x2U)
9+
#define BSP_PRV_PRCR_LOCK ((BSP_PRV_PRCR_KEY) | 0x0U)
10+
11+
#ifdef NO_BACKUP_REGISTERS
12+
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *)0x20007FF0))
13+
#else
14+
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *) &R_SYSTEM->VBTBKR[0]))
15+
#endif
16+
#define DOUBLE_TAP_MAGIC 0x07738135
17+
18+
19+
/* call this function when you want to force the board to go in bootloader */
20+
void goBootloader();
21+
#endif

cores/arduino/usb/SerialUSB.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
#include "SerialUSB.h"
32
#include "tusb.h"
3+
#include "boot.h"
44

55
#ifndef DISABLE_USB_SERIAL
66
// Ensure we are installed in the USB chain
@@ -133,17 +133,7 @@ _SerialUSB::operator bool() {
133133
return rv;
134134
}
135135

136-
/* Key code for writing PRCR register. */
137-
#define BSP_PRV_PRCR_KEY (0xA500U)
138-
#define BSP_PRV_PRCR_PRC1_UNLOCK ((BSP_PRV_PRCR_KEY) | 0x2U)
139-
#define BSP_PRV_PRCR_LOCK ((BSP_PRV_PRCR_KEY) | 0x0U)
140136

141-
#ifdef NO_BACKUP_REGISTERS
142-
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *)0x20007FF0))
143-
#else
144-
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *) &R_SYSTEM->VBTBKR[0]))
145-
#endif
146-
#define DOUBLE_TAP_MAGIC 0x07738135
147137

148138
int _SerialUSB::_bps, _SerialUSB::_bits, _SerialUSB::_parity, _SerialUSB::_stop;
149139
bool _SerialUSB::_dtr, _SerialUSB::_rts;
@@ -154,12 +144,7 @@ static void CheckSerialReset() {
154144
#endif
155145

156146
if ((_SerialUSB::_bps == 1200) && (! _SerialUSB::_dtr)) {
157-
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK;
158-
BOOT_DOUBLE_TAP_DATA = DOUBLE_TAP_MAGIC;
159-
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK;
160-
((R_USB_FS0_Type*)R_USB_FS0_BASE)->SYSCFG_b.DPRPU = 0;
161-
NVIC_SystemReset();
162-
while (1); // WDT will fire here
147+
goBootloader();
163148
}
164149
}
165150

@@ -223,4 +208,4 @@ void arduino::serialEventRun(void) {
223208
if (serialEvent && SerialUSB.available()) {
224209
serialEvent();
225210
}
226-
}
211+
}

0 commit comments

Comments
 (0)