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/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