This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new fd7698c66 mcu/nrf52: Limit auto enable debugger to new chips
fd7698c66 is described below

commit fd7698c662bd081e8332077519d521340eccf5de
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Fri Jun 16 09:37:13 2023 +0200

    mcu/nrf52: Limit auto enable debugger to new chips
    
    Disabling approtect by writing 0x5A to UICR is valid
    for newer chips only.
    Writing 0x5A to approtect for older revisions results in
    enabling protection.
    
    This applies auto debugger enable feature for revisions
    that support this.
    
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 hw/mcu/nordic/nrf52xxx/src/system_nrf52.c | 33 ++++++++++++++++++++++++++++---
 1 file changed, 30 insertions(+), 3 deletions(-)

diff --git a/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c 
b/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c
index 2ff437c14..c236e0b73 100644
--- a/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c
+++ b/hw/mcu/nordic/nrf52xxx/src/system_nrf52.c
@@ -101,6 +101,33 @@ static bool errata_136(void);
     uint32_t SystemCoreClock __attribute__((used)) = __SYSTEM_CLOCK_64M;
 #endif
 
+#if MYNEWT_VAL_CHOICE(MCU_ACCESS_PORT_PROTECTION, disable)
+/*
+ * When target requests debugger to be enabled automatically additional check
+ * has to be berformed to make sure that approtect wants 0xFF or 0x5A to
+ * enable debugger. Following lines define AUTO_PROTECT_DISABLE which evaluates
+ * to true if 0x5A is applicable for current chip.
+ */
+#define NRF_MINIMUM_VARIANT(a, b) ((uint16_t)NRF_FICR->INFO.VARIANT >= (((a) 
<< 8) | b))
+#if defined (NRF52805_XXAA)
+#define AUTO_APPROTECT_DISABLE NRF_MINIMUM_VARIANT('B', '0')
+#elif defined(NRF52810_XXAA)
+#define AUTO_APPROTECT_DISABLE NRF_MINIMUM_VARIANT('E', '0')
+#elif defined(NRF52811_XXAA)
+#define AUTO_APPROTECT_DISABLE NRF_MINIMUM_VARIANT('B', '0')
+#elif defined(NRF52832_XXAA) || defined(NRF52832_XXAB)
+#define AUTO_APPROTECT_DISABLE NRF_MINIMUM_VARIANT('G', '0')
+#elif defined (NRF52833_XXAA)
+#define AUTO_APPROTECT_DISABLE NRF_MINIMUM_VARIANT('B', '0')
+#elif defined(NRF52840_XXAA)
+#define AUTO_APPROTECT_DISABLE NRF_MINIMUM_VARIANT('F', '0')
+#else
+#define AUTO_APPROTECT_DISABLE 0
+#endif
+#else
+#define AUTO_APPROTECT_DISABLE 0
+#endif
+
 void SystemCoreClockUpdate(void)
 {
     SystemCoreClock = __SYSTEM_CLOCK_64M;
@@ -490,10 +517,10 @@ 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;
+    if (MYNEWT_VAL_CHOICE(MCU_ACCESS_PORT_PROTECTION, disable) && 
AUTO_APPROTECT_DISABLE) {
+        NRF_APPROTECT->DISABLE = APPROTECT_DISABLE_DISABLE_SwDisable;
         NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Msk;
-        NRF_UICR->APPROTECT = 0x5A;
+        NRF_UICR->APPROTECT = APPROTECT_DISABLE_DISABLE_SwDisable;
         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) {

Reply via email to