zephyr: Move IO functions out of main to separate file

Moves IO functions into a separate file to allow reuse

Signed-off-by: Jamie McCrae <jamie.mccrae@nordicsemi.no>
diff --git a/boot/zephyr/CMakeLists.txt b/boot/zephyr/CMakeLists.txt
index ad4c823..3ce4235 100644
--- a/boot/zephyr/CMakeLists.txt
+++ b/boot/zephyr/CMakeLists.txt
@@ -56,6 +56,7 @@
 # Zephyr port-specific sources.
 zephyr_library_sources(
   main.c
+  io.c
   flash_map_extended.c
   os.c
   keys.c
diff --git a/boot/zephyr/include/io/io.h b/boot/zephyr/include/io/io.h
new file mode 100644
index 0000000..332eefb
--- /dev/null
+++ b/boot/zephyr/include/io/io.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2012-2014 Wind River Systems, Inc.
+ * Copyright (c) 2020 Arm Limited
+ * Copyright (c) 2021-2023 Nordic Semiconductor ASA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef H_IO_
+#define H_IO_
+
+#include <stddef.h>
+
+#ifdef CONFIG_SOC_FAMILY_NRF
+#include <helpers/nrfx_reset_reason.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+ * Initialises the configured LED.
+ */
+void io_led_init(void);
+
+/*
+ * Checks if GPIO is set in the required way to remain in serial recovery mode
+ *
+ * @retval	false for normal boot, true for serial recovery boot
+ */
+bool io_detect_pin(void);
+
+/*
+ * Checks if board was reset using reset pin and if device should stay in
+ * serial recovery mode
+ *
+ * @retval	false for normal boot, true for serial recovery boot
+ */
+bool io_detect_pin_reset(void);
+
+/*
+ * Checks board boot mode via retention subsystem and if device should stay in
+ * serial recovery mode
+ *
+ * @retval	false for normal boot, true for serial recovery boot
+ */
+bool io_detect_boot_mode(void);
+
+#ifdef CONFIG_SOC_FAMILY_NRF
+static inline bool io_boot_skip_serial_recovery()
+{
+    uint32_t rr = nrfx_reset_reason_get();
+
+    return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
+}
+#else
+static inline bool io_boot_skip_serial_recovery()
+{
+    return false;
+}
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/boot/zephyr/io.c b/boot/zephyr/io.c
new file mode 100644
index 0000000..6d3b01e
--- /dev/null
+++ b/boot/zephyr/io.c
@@ -0,0 +1,190 @@
+/*
+ * Copyright (c) 2012-2014 Wind River Systems, Inc.
+ * Copyright (c) 2020 Arm Limited
+ * Copyright (c) 2021-2023 Nordic Semiconductor ASA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <assert.h>
+#include <zephyr/kernel.h>
+#include <zephyr/devicetree.h>
+#include <zephyr/drivers/gpio.h>
+#include <zephyr/sys/__assert.h>
+#include <zephyr/drivers/flash.h>
+#include <zephyr/drivers/timer/system_timer.h>
+#include <zephyr/usb/usb_device.h>
+#include <soc.h>
+#include <zephyr/linker/linker-defs.h>
+
+#include "target.h"
+
+#if defined(CONFIG_BOOT_SERIAL_PIN_RESET)
+#include <zephyr/drivers/hwinfo.h>
+#endif
+
+#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE)
+#include <zephyr/retention/bootmode.h>
+#endif
+
+/* Validate serial recovery configuration */
+#ifdef CONFIG_MCUBOOT_SERIAL
+#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
+    !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
+    !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
+    !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \
+    !defined(CONFIG_BOOT_SERIAL_PIN_RESET)
+#error "Serial recovery selected without an entrance mode set"
+#endif
+#endif
+
+#ifdef CONFIG_MCUBOOT_INDICATION_LED
+
+/*
+ * The led0 devicetree alias is optional. If present, we'll use it
+ * to turn on the LED whenever the button is pressed.
+ */
+#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
+#define LED0_NODE DT_ALIAS(mcuboot_led0)
+#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
+#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
+#define LED0_NODE DT_ALIAS(bootloader_led0)
+#endif
+
+#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
+static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
+#else
+/* A build error here means your board isn't set up to drive an LED. */
+#error "Unsupported board: led0 devicetree alias is not defined"
+#endif
+
+void io_led_init(void)
+{
+    if (!device_is_ready(led0.port)) {
+        BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
+        return;
+    }
+
+    gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
+    gpio_pin_set_dt(&led0, 0);
+}
+#endif /* CONFIG_MCUBOOT_INDICATION_LED */
+
+#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO)
+
+#if defined(CONFIG_MCUBOOT_SERIAL)
+#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
+#else
+#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
+#endif
+
+#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
+
+#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
+static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
+#else
+#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
+#endif
+
+bool io_detect_pin(void)
+{
+    int rc;
+    int pin_active;
+
+    if (!device_is_ready(button0.port)) {
+        __ASSERT(false, "GPIO device is not ready.\n");
+        return false;
+    }
+
+    rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
+    __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
+
+    rc = gpio_pin_get_dt(&button0);
+    pin_active = rc;
+
+    __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
+
+    if (pin_active) {
+        if (BUTTON_0_DETECT_DELAY > 0) {
+#ifdef CONFIG_MULTITHREADING
+            k_sleep(K_MSEC(50));
+#else
+            k_busy_wait(50000);
+#endif
+
+            /* Get the uptime for debounce purposes. */
+            int64_t timestamp = k_uptime_get();
+
+            for(;;) {
+                rc = gpio_pin_get_dt(&button0);
+                pin_active = rc;
+                __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
+
+                /* Get delta from when this started */
+                uint32_t delta = k_uptime_get() -  timestamp;
+
+                /* If not pressed OR if pressed > debounce period, stop. */
+                if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
+                    break;
+                }
+
+                /* Delay 1 ms */
+#ifdef CONFIG_MULTITHREADING
+                k_sleep(K_MSEC(1));
+#else
+                k_busy_wait(1000);
+#endif
+            }
+        }
+    }
+
+    return (bool)pin_active;
+}
+#endif
+
+#if defined(CONFIG_BOOT_SERIAL_PIN_RESET)
+bool io_detect_pin_reset(void)
+{
+    uint32_t reset_cause;
+    int rc;
+
+    rc = hwinfo_get_reset_cause(&reset_cause);
+
+    if (rc == 0 && reset_cause == RESET_PIN) {
+        (void)hwinfo_clear_reset_cause();
+        return true;
+    }
+
+    return false;
+}
+#endif
+
+#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE)
+bool io_detect_boot_mode(void)
+{
+    int32_t boot_mode;
+
+    boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
+
+    if (boot_mode == 1) {
+        /* Boot mode to stay in bootloader, clear status and enter serial
+         * recovery mode
+         */
+        bootmode_clear();
+
+        return true;
+    }
+
+    return false;
+}
+#endif
diff --git a/boot/zephyr/main.c b/boot/zephyr/main.c
index 7b37024..abd2fe6 100644
--- a/boot/zephyr/main.c
+++ b/boot/zephyr/main.c
@@ -31,6 +31,7 @@
 #include <cmsis_core.h>
 #endif
 
+#include "io/io.h"
 #include "target.h"
 
 #include "bootutil/bootutil_log.h"
@@ -74,10 +75,6 @@
 };
 #endif
 
-#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
-#include <zephyr/retention/bootmode.h>
-#endif
-
 #if defined(CONFIG_BOOT_USB_DFU_WAIT) || defined(CONFIG_BOOT_USB_DFU_GPIO)
 #include <zephyr/usb/class/usb_dfu.h>
 #endif
@@ -86,10 +83,6 @@
 #include <arm_cleanup.h>
 #endif
 
-#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
-#include <zephyr/drivers/hwinfo.h>
-#endif
-
 /* CONFIG_LOG_MINIMAL is the legacy Kconfig property,
  * replaced by CONFIG_LOG_MODE_MINIMAL.
  */
@@ -132,67 +125,8 @@
         * !defined(ZEPHYR_LOG_MODE_MINIMAL)
 	*/
 
-#ifdef CONFIG_SOC_FAMILY_NRF
-#include <helpers/nrfx_reset_reason.h>
-
-static inline bool boot_skip_serial_recovery()
-{
-    uint32_t rr = nrfx_reset_reason_get();
-
-    return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
-}
-#else
-static inline bool boot_skip_serial_recovery()
-{
-    return false;
-}
-#endif
-
 BOOT_LOG_MODULE_REGISTER(mcuboot);
 
-/* Validate serial recovery configuration */
-#ifdef CONFIG_MCUBOOT_SERIAL
-#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
-    !defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
-    !defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
-    !defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \
-    !defined(CONFIG_BOOT_SERIAL_PIN_RESET)
-#error "Serial recovery selected without an entrance mode set"
-#endif
-#endif
-
-#ifdef CONFIG_MCUBOOT_INDICATION_LED
-
-/*
- * The led0 devicetree alias is optional. If present, we'll use it
- * to turn on the LED whenever the button is pressed.
- */
-#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
-#define LED0_NODE DT_ALIAS(mcuboot_led0)
-#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
-#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
-#define LED0_NODE DT_ALIAS(bootloader_led0)
-#endif
-
-#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
-static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
-#else
-/* A build error here means your board isn't set up to drive an LED. */
-#error "Unsupported board: led0 devicetree alias is not defined"
-#endif
-
-void led_init(void)
-{
-    if (!device_is_ready(led0.port)) {
-        BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
-        return;
-    }
-
-    gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
-    gpio_pin_set_dt(&led0, 0);
-}
-#endif /* CONFIG_MCUBOOT_INDICATION_LED */
-
 void os_heap_init(void);
 
 #if defined(CONFIG_ARM)
@@ -437,78 +371,6 @@
         * !defined(CONFIG_LOG_PROCESS_THREAD) && !defined(ZEPHYR_LOG_MODE_MINIMAL)
         */
 
-#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO)
-
-#ifdef CONFIG_MCUBOOT_SERIAL
-#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
-#else
-#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
-#endif
-
-#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
-
-#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
-static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
-#else
-#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
-#endif
-
-static bool detect_pin(void)
-{
-    int rc;
-    int pin_active;
-
-    if (!device_is_ready(button0.port)) {
-        __ASSERT(false, "GPIO device is not ready.\n");
-        return false;
-    }
-
-    rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
-    __ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
-
-    rc = gpio_pin_get_dt(&button0);
-    pin_active = rc;
-
-    __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
-
-    if (pin_active) {
-        if (BUTTON_0_DETECT_DELAY > 0) {
-#ifdef CONFIG_MULTITHREADING
-            k_sleep(K_MSEC(50));
-#else
-            k_busy_wait(50000);
-#endif
-
-            /* Get the uptime for debounce purposes. */
-            int64_t timestamp = k_uptime_get();
-
-            for(;;) {
-                rc = gpio_pin_get_dt(&button0);
-                pin_active = rc;
-                __ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
-
-                /* Get delta from when this started */
-                uint32_t delta = k_uptime_get() -  timestamp;
-
-                /* If not pressed OR if pressed > debounce period, stop. */
-                if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
-                    break;
-                }
-
-                /* Delay 1 ms */
-#ifdef CONFIG_MULTITHREADING
-                k_sleep(K_MSEC(1));
-#else
-                k_busy_wait(1000);
-#endif
-            }
-        }
-    }
-
-    return (bool)pin_active;
-}
-#endif
-
 #ifdef CONFIG_MCUBOOT_SERIAL
 static void boot_serial_enter()
 {
@@ -534,14 +396,6 @@
     int rc;
     FIH_DECLARE(fih_rc, FIH_FAILURE);
 
-#ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
-    int32_t boot_mode;
-#endif
-
-#ifdef CONFIG_BOOT_SERIAL_PIN_RESET
-    uint32_t reset_cause;
-#endif
-
     MCUBOOT_WATCHDOG_SETUP();
     MCUBOOT_WATCHDOG_FEED();
 
@@ -565,23 +419,20 @@
     mcuboot_status_change(MCUBOOT_STATUS_STARTUP);
 
 #ifdef CONFIG_BOOT_SERIAL_ENTRANCE_GPIO
-    if (detect_pin() &&
-            !boot_skip_serial_recovery()) {
+    if (io_detect_pin() &&
+            !io_boot_skip_serial_recovery()) {
         boot_serial_enter();
     }
 #endif
 
 #ifdef CONFIG_BOOT_SERIAL_PIN_RESET
-    rc = hwinfo_get_reset_cause(&reset_cause);
-
-    if (rc == 0 && reset_cause == RESET_PIN) {
-        (void)hwinfo_clear_reset_cause();
+    if (io_detect_pin_reset()) {
         boot_serial_enter();
     }
 #endif
 
 #if defined(CONFIG_BOOT_USB_DFU_GPIO)
-    if (detect_pin()) {
+    if (io_detect_pin()) {
 #ifdef CONFIG_MCUBOOT_INDICATION_LED
         gpio_pin_set_dt(&led0, 1);
 #endif
@@ -631,13 +482,10 @@
     FIH_CALL(boot_go, fih_rc, &rsp);
 
 #ifdef CONFIG_BOOT_SERIAL_BOOT_MODE
-    boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
-
-    if (boot_mode == 1) {
+    if (io_detect_boot_mode()) {
         /* Boot mode to stay in bootloader, clear status and enter serial
          * recovery mode
          */
-        bootmode_clear();
         boot_serial_enter();
     }
 #endif