Skip to content
Prev Previous commit
Next Next commit
define the function to go in bootloader and moved in a separate heade…
…r boot.h
  • Loading branch information
maidnl authored and facchinm committed May 30, 2024
commit 4ad21d4a8f24c93be8a4305f7e8a450775197f9f
12 changes: 12 additions & 0 deletions cores/arduino/boot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "boot.h"


/* call this function when you want to force the board to go in bootloader */
void goBootloader() {
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK;
BOOT_DOUBLE_TAP_DATA = DOUBLE_TAP_MAGIC;
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK;
((R_USB_FS0_Type*)R_USB_FS0_BASE)->SYSCFG_b.DPRPU = 0;
NVIC_SystemReset();
while (1); // WDT will fire here
}
21 changes: 21 additions & 0 deletions cores/arduino/boot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Arduino.h"

#ifndef BOOTLOADER_INCLUDE_H
#define BOOTLOADER_INCLUDE_H

/* Key code for writing PRCR register. */
#define BSP_PRV_PRCR_KEY (0xA500U)
#define BSP_PRV_PRCR_PRC1_UNLOCK ((BSP_PRV_PRCR_KEY) | 0x2U)
#define BSP_PRV_PRCR_LOCK ((BSP_PRV_PRCR_KEY) | 0x0U)

#ifdef NO_BACKUP_REGISTERS
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *)0x20007FF0))
#else
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *) &R_SYSTEM->VBTBKR[0]))
#endif
#define DOUBLE_TAP_MAGIC 0x07738135


/* call this function when you want to force the board to go in bootloader */
void goBootloader();
#endif
21 changes: 3 additions & 18 deletions cores/arduino/usb/SerialUSB.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include "SerialUSB.h"
#include "tusb.h"
#include "boot.h"

#ifndef DISABLE_USB_SERIAL
// Ensure we are installed in the USB chain
Expand Down Expand Up @@ -133,17 +133,7 @@ _SerialUSB::operator bool() {
return rv;
}

/* Key code for writing PRCR register. */
#define BSP_PRV_PRCR_KEY (0xA500U)
#define BSP_PRV_PRCR_PRC1_UNLOCK ((BSP_PRV_PRCR_KEY) | 0x2U)
#define BSP_PRV_PRCR_LOCK ((BSP_PRV_PRCR_KEY) | 0x0U)

#ifdef NO_BACKUP_REGISTERS
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *)0x20007FF0))
#else
#define BOOT_DOUBLE_TAP_DATA (*((volatile uint32_t *) &R_SYSTEM->VBTBKR[0]))
#endif
#define DOUBLE_TAP_MAGIC 0x07738135

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

if ((_SerialUSB::_bps == 1200) && (! _SerialUSB::_dtr)) {
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_PRC1_UNLOCK;
BOOT_DOUBLE_TAP_DATA = DOUBLE_TAP_MAGIC;
R_SYSTEM->PRCR = (uint16_t) BSP_PRV_PRCR_LOCK;
((R_USB_FS0_Type*)R_USB_FS0_BASE)->SYSCFG_b.DPRPU = 0;
NVIC_SystemReset();
while (1); // WDT will fire here
goBootloader();
}
}

Expand Down Expand Up @@ -223,4 +208,4 @@ void arduino::serialEventRun(void) {
if (serialEvent && SerialUSB.available()) {
serialEvent();
}
}
}