Skip to content

Commit 05405d4

Browse files
nvlsianpude-nordic
authored andcommitted
[nrf noup] do_boot: clean peripherals state before boot
Do some cleanup of nRF peripherals. This is necessary since Zephyr doesn't have any driver deinitialization functionality, and we'd like to leave peripherals in a more predictable state before booting the Zephyr image. This should be re-worked when the zephyr driver model allows us to deinitialize devices cleanly before jumping to the chain-loaded image. Signed-off-by: Andrzej Puzdrowski <[email protected]> Signed-off-by: Robert Lubos <[email protected]> Signed-off-by: Torsten Rasmussen <[email protected]> Signed-off-by: Øyvind Rønningstad <[email protected]> Signed-off-by: Martí Bolívar <[email protected]> Signed-off-by: Håkon Øye Amundsen <[email protected]> Signed-off-by: Ioannis Glaropoulos <[email protected]> Signed-off-by: Johann Fischer <[email protected]> Signed-off-by: Trond Einar Snekvik <[email protected]> Signed-off-by: Torsten Rasmussen <[email protected]> Signed-off-by: Jamie McCrae <[email protected]> Signed-off-by: Dominik Ermel <[email protected]> (cherry picked from commit 0a4da3a) (cherry picked from commit e56136a)
1 parent e52481a commit 05405d4

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,9 @@ zephyr_library_sources(
343343
${BOOT_DIR}/zephyr/arm_cleanup.c
344344
)
345345
endif()
346+
347+
if(CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL)
348+
zephyr_library_sources(
349+
${BOOT_DIR}/zephyr/nrf_cleanup.c
350+
)
351+
endif()

boot/zephyr/include/nrf_cleanup.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2020 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#ifndef H_NRF_CLEANUP_
8+
#define H_NRF_CLEANUP_
9+
10+
/**
11+
* Perform cleanup on some peripheral resources used by MCUBoot prior chainload
12+
* the application.
13+
*
14+
* This function disables all RTC instances and UARTE instances.
15+
* It Disables their interrupts signals as well.
16+
*/
17+
void nrf_cleanup_peripheral(void);
18+
19+
#endif

boot/zephyr/main.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ K_SEM_DEFINE(boot_log_sem, 1, 1);
123123
#include <pm_config.h>
124124
#endif
125125

126+
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
127+
#include <nrf_cleanup.h>
128+
#endif
129+
126130
#ifdef CONFIG_SOC_FAMILY_NRF
127131
#include <helpers/nrfx_reset_reason.h>
128132

@@ -242,7 +246,9 @@ static void do_boot(struct boot_rsp *rsp)
242246
}
243247
#endif
244248
#endif
245-
249+
#if CONFIG_MCUBOOT_NRF_CLEANUP_PERIPHERAL
250+
nrf_cleanup_peripheral();
251+
#endif
246252
#if CONFIG_MCUBOOT_CLEANUP_ARM_CORE
247253
cleanup_arm_nvic(); /* cleanup NVIC registers */
248254

boot/zephyr/nrf_cleanup.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2020 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <hal/nrf_clock.h>
8+
#if defined(NRF_UARTE0) || defined(NRF_UARTE1)
9+
#include <hal/nrf_uarte.h>
10+
#endif
11+
#if defined(NRF_RTC0) || defined(NRF_RTC1) || defined(NRF_RTC2)
12+
#include <hal/nrf_rtc.h>
13+
#endif
14+
#if defined(NRF_PPI)
15+
#include <hal/nrf_ppi.h>
16+
#endif
17+
#if defined(NRF_DPPIC)
18+
#include <hal/nrf_dppi.h>
19+
#endif
20+
21+
#include <string.h>
22+
23+
#define NRF_UARTE_SUBSCRIBE_CONF_OFFS offsetof(NRF_UARTE_Type, SUBSCRIBE_STARTRX)
24+
#define NRF_UARTE_SUBSCRIBE_CONF_SIZE (offsetof(NRF_UARTE_Type, EVENTS_CTS) -\
25+
NRF_UARTE_SUBSCRIBE_CONF_OFFS)
26+
27+
#define NRF_UARTE_PUBLISH_CONF_OFFS offsetof(NRF_UARTE_Type, PUBLISH_CTS)
28+
#define NRF_UARTE_PUBLISH_CONF_SIZE (offsetof(NRF_UARTE_Type, SHORTS) -\
29+
NRF_UARTE_PUBLISH_CONF_OFFS)
30+
31+
#if defined(NRF_RTC0) || defined(NRF_RTC1) || defined(NRF_RTC2)
32+
static inline void nrf_cleanup_rtc(NRF_RTC_Type * rtc_reg)
33+
{
34+
nrf_rtc_task_trigger(rtc_reg, NRF_RTC_TASK_STOP);
35+
nrf_rtc_event_disable(rtc_reg, 0xFFFFFFFF);
36+
nrf_rtc_int_disable(rtc_reg, 0xFFFFFFFF);
37+
}
38+
#endif
39+
40+
static void nrf_cleanup_clock(void)
41+
{
42+
nrf_clock_int_disable(NRF_CLOCK, 0xFFFFFFFF);
43+
}
44+
45+
void nrf_cleanup_peripheral(void)
46+
{
47+
#if defined(NRF_RTC0)
48+
nrf_cleanup_rtc(NRF_RTC0);
49+
#endif
50+
#if defined(NRF_RTC1)
51+
nrf_cleanup_rtc(NRF_RTC1);
52+
#endif
53+
#if defined(NRF_RTC2)
54+
nrf_cleanup_rtc(NRF_RTC2);
55+
#endif
56+
#if defined(NRF_UARTE0)
57+
nrf_uarte_disable(NRF_UARTE0);
58+
nrf_uarte_int_disable(NRF_UARTE0, 0xFFFFFFFF);
59+
#if defined(NRF_DPPIC)
60+
/* Clear all SUBSCRIBE configurations. */
61+
memset((uint8_t *)NRF_UARTE0 + NRF_UARTE_SUBSCRIBE_CONF_OFFS, 0, NRF_UARTE_SUBSCRIBE_CONF_SIZE);
62+
/* Clear all PUBLISH configurations. */
63+
memset((uint8_t *)NRF_UARTE0 + NRF_UARTE_PUBLISH_CONF_OFFS, 0, NRF_UARTE_PUBLISH_CONF_SIZE);
64+
#endif
65+
#endif
66+
#if defined(NRF_UARTE1)
67+
nrf_uarte_disable(NRF_UARTE1);
68+
nrf_uarte_int_disable(NRF_UARTE1, 0xFFFFFFFF);
69+
#if defined(NRF_DPPIC)
70+
/* Clear all SUBSCRIBE configurations. */
71+
memset((uint8_t *)NRF_UARTE1 + NRF_UARTE_SUBSCRIBE_CONF_OFFS, 0, NRF_UARTE_SUBSCRIBE_CONF_SIZE);
72+
/* Clear all PUBLISH configurations. */
73+
memset((uint8_t *)NRF_UARTE1 + NRF_UARTE_PUBLISH_CONF_OFFS, 0, NRF_UARTE_PUBLISH_CONF_SIZE);
74+
#endif
75+
#endif
76+
#if defined(NRF_PPI)
77+
nrf_ppi_channels_disable_all(NRF_PPI);
78+
#endif
79+
#if defined(NRF_DPPIC)
80+
nrf_dppi_channels_disable_all(NRF_DPPIC);
81+
#endif
82+
nrf_cleanup_clock();
83+
}

0 commit comments

Comments
 (0)