From a56457b6737b48206b00ee554159d6999398d75e Mon Sep 17 00:00:00 2001 From: Justin Morton Date: Wed, 23 Oct 2024 13:05:29 -0700 Subject: [PATCH] samples: cellular: nrf_cloud_rest_fota: add smp fota for nrf9160dk Add the option to enable SMP FOTA on the nRF9160DK. IRIS-9481 Signed-off-by: Justin Morton --- .../nrf_cloud_rest_fota/CMakeLists.txt | 4 ++ .../cellular/nrf_cloud_rest_fota/README.rst | 13 ++++ .../nrf9160dk_mcumgr_client_uart2.overlay | 46 ++++++++++++++ .../nrf_cloud_rest_fota/overlay_smp_fota.conf | 12 ++++ .../cellular/nrf_cloud_rest_fota/src/main.c | 9 ++- .../nrf_cloud_rest_fota/src/smp_reset.c | 62 +++++++++++++++++++ .../nrf_cloud_rest_fota/src/smp_reset.h | 20 ++++++ 7 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 samples/cellular/nrf_cloud_rest_fota/nrf9160dk_mcumgr_client_uart2.overlay create mode 100644 samples/cellular/nrf_cloud_rest_fota/overlay_smp_fota.conf create mode 100644 samples/cellular/nrf_cloud_rest_fota/src/smp_reset.c create mode 100644 samples/cellular/nrf_cloud_rest_fota/src/smp_reset.h diff --git a/samples/cellular/nrf_cloud_rest_fota/CMakeLists.txt b/samples/cellular/nrf_cloud_rest_fota/CMakeLists.txt index 3469216e476..035ad1e382d 100644 --- a/samples/cellular/nrf_cloud_rest_fota/CMakeLists.txt +++ b/samples/cellular/nrf_cloud_rest_fota/CMakeLists.txt @@ -12,4 +12,8 @@ zephyr_compile_definitions(PROJECT_NAME=${PROJECT_NAME}) # NORDIC SDK APP START target_sources(app PRIVATE src/main.c) + +if(CONFIG_NRF_CLOUD_FOTA_SMP AND CONFIG_BOARD_NRF9160DK_NRF9160_NS) + target_sources(app PRIVATE src/smp_reset.c) +endif() # NORDIC SDK APP END diff --git a/samples/cellular/nrf_cloud_rest_fota/README.rst b/samples/cellular/nrf_cloud_rest_fota/README.rst index 3ccbf00bb8a..355390c2ced 100644 --- a/samples/cellular/nrf_cloud_rest_fota/README.rst +++ b/samples/cellular/nrf_cloud_rest_fota/README.rst @@ -115,6 +115,19 @@ To enable full modem FOTA, add the following parameter to your build command: Also, if you are using an nRF9160 DK, specify your development kit version by appending it to the board name. For example, if you are using version 1.0.1, use the board name ``nrf9160dk@1.0.1/nrf9160/ns`` in your build command. +To enable SMP FOTA (nRF9160 DK only), add the following parameters to your build command: + +* ``-DEXTRA_CONF_FILE=overlay_smp_fota.conf`` +* ``-DEXTRA_DTC_OVERLAY_FILE=nrf9160dk_mcumgr_client_uart2.overlay`` + +The nRF52840 device on your DK must be running an SMP compatible firmware, such as the :ref:`smp_svr` sample. +Build the :ref:`smp_svr` sample for the ``nrf9160dk/nrf52840`` board with the following parameters: + +* ``-DEXTRA_CONF_FILE=overlay_smp_fota.conf`` +* ``-DEXTRA_DTC_OVERLAY_FILE=nrf9160dk_mcumgr_client_uart2.overlay`` + +To change :ref:`smp_svr` sample's application version, set the :kconfig:option:`CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION` Kconfig option. + Dependencies ************ diff --git a/samples/cellular/nrf_cloud_rest_fota/nrf9160dk_mcumgr_client_uart2.overlay b/samples/cellular/nrf_cloud_rest_fota/nrf9160dk_mcumgr_client_uart2.overlay new file mode 100644 index 00000000000..3cba07b8d98 --- /dev/null +++ b/samples/cellular/nrf_cloud_rest_fota/nrf9160dk_mcumgr_client_uart2.overlay @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/* Generic PIN definition for UART2 or UART1 to nRF52840 */ +&pinctrl { + mcumgr_uart_default: mcumgr_uart_default { + group1 { + psels = , + ; + }; + group2 { + psels = , + ; + bias-pull-up; + }; + }; + + mcumgr_uart_sleep: mcumgr_uart_sleep { + group1 { + psels = , + , + , + ; + low-power-enable; + }; + }; +}; + +&uart2 { + compatible = "nordic,nrf-uarte"; + current-speed = <1000000>; + status = "okay"; + pinctrl-0 = <&mcumgr_uart_default>; + pinctrl-1 = <&mcumgr_uart_sleep>; + pinctrl-names = "default", "sleep"; + hw-flow-control; +}; + +/ { + chosen { + zephyr,uart-mcumgr = &uart2; + }; +}; diff --git a/samples/cellular/nrf_cloud_rest_fota/overlay_smp_fota.conf b/samples/cellular/nrf_cloud_rest_fota/overlay_smp_fota.conf new file mode 100644 index 00000000000..9e385ee7e05 --- /dev/null +++ b/samples/cellular/nrf_cloud_rest_fota/overlay_smp_fota.conf @@ -0,0 +1,12 @@ +# +# Copyright (c) 2024 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# +CONFIG_NRF_CLOUD_FOTA_SMP=y +CONFIG_NRF_MCUMGR_SMP_CLIENT=y +CONFIG_MCUMGR_GRP_IMG=y +CONFIG_MCUMGR_GRP_OS=y +CONFIG_MCUMGR_TRANSPORT_UART=y +CONFIG_UART_MCUMGR_RX_BUF_COUNT=4 +CONFIG_BOOT_IMAGE_ACCESS_HOOKS=y diff --git a/samples/cellular/nrf_cloud_rest_fota/src/main.c b/samples/cellular/nrf_cloud_rest_fota/src/main.c index 8645cfd349e..722ab9d9a0e 100644 --- a/samples/cellular/nrf_cloud_rest_fota/src/main.c +++ b/samples/cellular/nrf_cloud_rest_fota/src/main.c @@ -18,6 +18,7 @@ #include #include #include +#include "smp_reset.h" LOG_MODULE_REGISTER(nrf_cloud_rest_fota, CONFIG_NRF_CLOUD_REST_FOTA_SAMPLE_LOG_LEVEL); @@ -81,7 +82,10 @@ static void sample_reboot(enum nrf_cloud_fota_reboot_status status); static struct nrf_cloud_fota_poll_ctx fota_ctx = { .rest_ctx = &rest_ctx, .device_id = device_id, - .reboot_fn = sample_reboot + .reboot_fn = sample_reboot, +#if SMP_FOTA_ENABLED + .smp_reset_cb = nrf52840_reset_api +#endif }; #if defined(CONFIG_REST_FOTA_DO_JITP) @@ -219,7 +223,8 @@ static void send_device_status(void) .bootloader = 1, .modem = 1, .application = 1, - .modem_full = fota_ctx.full_modem_fota_supported + .modem_full = fota_ctx.full_modem_fota_supported, + .smp = SMP_FOTA_ENABLED }; struct nrf_cloud_svc_info svc_inf = { diff --git a/samples/cellular/nrf_cloud_rest_fota/src/smp_reset.c b/samples/cellular/nrf_cloud_rest_fota/src/smp_reset.c new file mode 100644 index 00000000000..304ac95df47 --- /dev/null +++ b/samples/cellular/nrf_cloud_rest_fota/src/smp_reset.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(nrf_cloud_rest_fota_smp, CONFIG_NRF_CLOUD_REST_FOTA_SAMPLE_LOG_LEVEL); + +#define RESET_NODE DT_NODELABEL(nrf52840_reset) +#define HAS_RECOVERY_MODE (DT_NODE_HAS_STATUS(RESET_NODE, okay)) + +int nrf52840_reset_api(void) +{ + int err; + const struct gpio_dt_spec reset_pin_spec = GPIO_DT_SPEC_GET(RESET_NODE, gpios); + + if (!device_is_ready(reset_pin_spec.port)) { + LOG_ERR("Reset device not ready"); + return -EIO; + } + + /* Configure pin as output and initialize it to inactive state. */ + err = gpio_pin_configure_dt(&reset_pin_spec, GPIO_OUTPUT_INACTIVE); + if (err) { + LOG_ERR("Pin configure err: %d", err); + return err; + } + + /* Reset the nRF52840 and let it wait until the pin is inactive again + * before running to main to ensure that it won't send any data until + * the H4 device is setup and ready to receive. + */ + err = gpio_pin_set_dt(&reset_pin_spec, 1); + if (err) { + LOG_ERR("GPIO Pin set to 1 err: %d", err); + return err; + } + + /* Wait for the nRF52840 peripheral to stop sending data. + * + * It is critical (!) to wait here, so that all bytes + * on the lines are received and drained correctly. + */ + k_sleep(K_MSEC(10)); + + /* We are ready, let the nRF52840 run to main */ + err = gpio_pin_set_dt(&reset_pin_spec, 0); + if (err) { + LOG_ERR("GPIO Pin set to 0 err: %d", err); + return err; + } + + LOG_DBG("Reset Pin %d", reset_pin_spec.pin); + + return 0; +} diff --git a/samples/cellular/nrf_cloud_rest_fota/src/smp_reset.h b/samples/cellular/nrf_cloud_rest_fota/src/smp_reset.h new file mode 100644 index 00000000000..2d3893fe5ed --- /dev/null +++ b/samples/cellular/nrf_cloud_rest_fota/src/smp_reset.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2024 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#ifndef _SMP_RESET_H_ +#define _SMP_RESET_H_ + +#if defined(CONFIG_NRF_CLOUD_FOTA_SMP) && defined(CONFIG_BOARD_NRF9160DK_NRF9160_NS) +#define SMP_FOTA_ENABLED 1 +#else +#define SMP_FOTA_ENABLED 0 +#endif + +/** + * @brief Reset the nRF52840 for SMP FOTA. + */ +int nrf52840_reset_api(void); + +#endif /* _SMP_RESET_H_ */