You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2023/06/14 11:59:18 UTC
[mynewt-core] 02/03: mcu/nrf52: Auto enable debugger in UICR
This is an automated email from the ASF dual-hosted git repository.
janc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit a50ba12fdc166c23cd73e6eabaa15ac937ab1ccb
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Mon Jun 12 15:08:05 2023 +0200
mcu/nrf52: Auto enable debugger in UICR
Recent NRF52 chips changed how default UICR value for APPROTECT (0xFF) is treated.
It used to mean that debugger is enabled.
Now it means that debugger is disabled (access port protection is on).
While it increase security in a final product it may be difficult to
work with mynewt project that usually have bootloader present.
When protection kicks in device needs to be recovered (erase of flash and UICR)
to use debugger or event to flash application via SWD.
This adds setting MCU_ACCESS_PORT_PROTECTION that allows to write UICR value
automatically. This new setting is set so it does not change MCU behaviour
(debugger enabled for older chips, debugger disabled for newer).
For convenience debugger is enabled by default in Nordic DK boards that
are known to have new chips.
Signed-off-by: Jerzy Kasenberg <je...@codecoup.pl>
---
hw/bsp/nordic_pca10056/syscfg.yml | 2 ++
hw/mcu/nordic/nrf52xxx/src/system_nrf52.c | 13 +++++++++++++
hw/mcu/nordic/nrf52xxx/syscfg.yml | 10 ++++++++++
3 files changed, 25 insertions(+)
diff --git a/hw/bsp/nordic_pca10056/syscfg.yml b/hw/bsp/nordic_pca10056/syscfg.yml
index 501f42ef9..aab9c4d8f 100644
--- a/hw/bsp/nordic_pca10056/syscfg.yml
+++ b/hw/bsp/nordic_pca10056/syscfg.yml
@@ -28,6 +28,8 @@ syscfg.vals:
# Enable nRF52840 MCU and common startup code
MCU_TARGET: nRF52840
MCU_COMMON_STARTUP: 1
+ # Disable access port, enable debugger
+ MCU_ACCESS_PORT_PROTECTION: disable
# Set default pins for peripherals
UART_0_PIN_TX: 6
diff --git a/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c b/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c
index 05c490a2b..2ff437c14 100644
--- a/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c
+++ b/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c
@@ -50,6 +50,7 @@
#endif
#include "system_nrf52_approtect.h"
+#include "syscfg/syscfg.h"
/*lint ++flb "Enter library region" */
@@ -489,6 +490,18 @@ void SystemInit(void)
NRF_P1->PIN_CNF[9] = (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
#endif
#endif
+ if (MYNEWT_VAL_CHOICE(MCU_ACCESS_PORT_PROTECTION, disable)) {
+ NRF_APPROTECT->DISABLE = 0x5A;
+ NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Msk;
+ NRF_UICR->APPROTECT = 0x5A;
+ while (!NRF_NVMC->READY) ;
+ NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
+ } else if (MYNEWT_VAL_CHOICE(MCU_ACCESS_PORT_PROTECTION, enable) && NRF_UICR->APPROTECT != 0) {
+ NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Msk;
+ NRF_UICR->APPROTECT = 0;
+ while (!NRF_NVMC->READY) ;
+ NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
+ }
nrf52_handle_approtect();
SystemCoreClockUpdate();
diff --git a/hw/mcu/nordic/nrf52xxx/syscfg.yml b/hw/mcu/nordic/nrf52xxx/syscfg.yml
index 5ad6c62b1..138159a58 100644
--- a/hw/mcu/nordic/nrf52xxx/syscfg.yml
+++ b/hw/mcu/nordic/nrf52xxx/syscfg.yml
@@ -102,6 +102,16 @@ syscfg.defs:
Enables generic startup code. This allows BSP to not include own
startup code.
value: 0
+ MCU_ACCESS_PORT_PROTECTION:
+ description: >
+ If set to enable, writes 0x00 to APPROTECT register at startup disabling debugger.
+ If set to disable, writes 0x5A to APPROTECT register at startup enabling debugger.
+ (It can only be enabled if it was not already disabled before). If it was disabled
+ MCU has to be erased (UICR and FLASH) to enable debugger again.
+ If left to default, does not write APPROTECT. Debugger may be enabled if APPROTECT
+ has already 0x5A value or for older revisions 0xFF.
+ choices: enable,disable,default
+ value: default
# MCU peripherals definitions
I2C_0: