Skip to content

Commit 433b848

Browse files
committed
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 <[email protected]>
1 parent 5e6cffb commit 433b848

File tree

4 files changed

+275
-158
lines changed

4 files changed

+275
-158
lines changed

boot/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ endif()
5656
# Zephyr port-specific sources.
5757
zephyr_library_sources(
5858
main.c
59+
io.c
5960
flash_map_extended.c
6061
os.c
6162
keys.c

boot/zephyr/include/io/io.h

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2012-2014 Wind River Systems, Inc.
3+
* Copyright (c) 2020 Arm Limited
4+
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef H_IO_
20+
#define H_IO_
21+
22+
#include <stddef.h>
23+
24+
#ifdef CONFIG_SOC_FAMILY_NRF
25+
#include <helpers/nrfx_reset_reason.h>
26+
#endif
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/*
33+
* Initialises the configured LED.
34+
*/
35+
void io_led_init(void);
36+
37+
/*
38+
* Checks if GPIO is set in the required way to remain in serial recovery mode
39+
*
40+
* @retval false for normal boot, true for serial recovery boot
41+
*/
42+
bool io_detect_pin(void);
43+
44+
/*
45+
* Checks if board was reset using reset pin and if device should stay in
46+
* serial recovery mode
47+
*
48+
* @retval false for normal boot, true for serial recovery boot
49+
*/
50+
bool io_detect_pin_reset(void);
51+
52+
/*
53+
* Checks board boot mode via retention subsystem and if device should stay in
54+
* serial recovery mode
55+
*
56+
* @retval false for normal boot, true for serial recovery boot
57+
*/
58+
bool io_detect_boot_mode(void);
59+
60+
#ifdef CONFIG_SOC_FAMILY_NRF
61+
static inline bool io_boot_skip_serial_recovery()
62+
{
63+
uint32_t rr = nrfx_reset_reason_get();
64+
65+
return !(rr == 0 || (rr & NRFX_RESET_REASON_RESETPIN_MASK));
66+
}
67+
#else
68+
static inline bool io_boot_skip_serial_recovery()
69+
{
70+
return false;
71+
}
72+
#endif
73+
74+
#ifdef __cplusplus
75+
}
76+
#endif
77+
78+
#endif

boot/zephyr/io.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* Copyright (c) 2012-2014 Wind River Systems, Inc.
3+
* Copyright (c) 2020 Arm Limited
4+
* Copyright (c) 2021-2023 Nordic Semiconductor ASA
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include <assert.h>
20+
#include <zephyr/kernel.h>
21+
#include <zephyr/devicetree.h>
22+
#include <zephyr/drivers/gpio.h>
23+
#include <zephyr/sys/__assert.h>
24+
#include <zephyr/drivers/flash.h>
25+
#include <zephyr/drivers/timer/system_timer.h>
26+
#include <zephyr/usb/usb_device.h>
27+
#include <soc.h>
28+
#include <zephyr/linker/linker-defs.h>
29+
30+
#include "target.h"
31+
32+
#if defined(CONFIG_BOOT_SERIAL_PIN_RESET)
33+
#include <zephyr/drivers/hwinfo.h>
34+
#endif
35+
36+
#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE)
37+
#include <zephyr/retention/bootmode.h>
38+
#endif
39+
40+
/* Validate serial recovery configuration */
41+
#ifdef CONFIG_MCUBOOT_SERIAL
42+
#if !defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) && \
43+
!defined(CONFIG_BOOT_SERIAL_WAIT_FOR_DFU) && \
44+
!defined(CONFIG_BOOT_SERIAL_BOOT_MODE) && \
45+
!defined(CONFIG_BOOT_SERIAL_NO_APPLICATION) && \
46+
!defined(CONFIG_BOOT_SERIAL_PIN_RESET)
47+
#error "Serial recovery selected without an entrance mode set"
48+
#endif
49+
#endif
50+
51+
#ifdef CONFIG_MCUBOOT_INDICATION_LED
52+
53+
/*
54+
* The led0 devicetree alias is optional. If present, we'll use it
55+
* to turn on the LED whenever the button is pressed.
56+
*/
57+
#if DT_NODE_EXISTS(DT_ALIAS(mcuboot_led0))
58+
#define LED0_NODE DT_ALIAS(mcuboot_led0)
59+
#elif DT_NODE_EXISTS(DT_ALIAS(bootloader_led0))
60+
#warning "bootloader-led0 alias is deprecated; use mcuboot-led0 instead"
61+
#define LED0_NODE DT_ALIAS(bootloader_led0)
62+
#endif
63+
64+
#if DT_NODE_HAS_STATUS(LED0_NODE, okay) && DT_NODE_HAS_PROP(LED0_NODE, gpios)
65+
static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
66+
#else
67+
/* A build error here means your board isn't set up to drive an LED. */
68+
#error "Unsupported board: led0 devicetree alias is not defined"
69+
#endif
70+
71+
void io_led_init(void)
72+
{
73+
if (!device_is_ready(led0.port)) {
74+
BOOT_LOG_ERR("Didn't find LED device referred by the LED0_NODE\n");
75+
return;
76+
}
77+
78+
gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
79+
gpio_pin_set_dt(&led0, 0);
80+
}
81+
#endif /* CONFIG_MCUBOOT_INDICATION_LED */
82+
83+
#if defined(CONFIG_BOOT_SERIAL_ENTRANCE_GPIO) || defined(CONFIG_BOOT_USB_DFU_GPIO)
84+
85+
#if defined(CONFIG_MCUBOOT_SERIAL)
86+
#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_SERIAL_DETECT_DELAY
87+
#else
88+
#define BUTTON_0_DETECT_DELAY CONFIG_BOOT_USB_DFU_DETECT_DELAY
89+
#endif
90+
91+
#define BUTTON_0_NODE DT_ALIAS(mcuboot_button0)
92+
93+
#if DT_NODE_EXISTS(BUTTON_0_NODE) && DT_NODE_HAS_PROP(BUTTON_0_NODE, gpios)
94+
static const struct gpio_dt_spec button0 = GPIO_DT_SPEC_GET(BUTTON_0_NODE, gpios);
95+
#else
96+
#error "Serial recovery/USB DFU button must be declared in device tree as 'mcuboot_button0'"
97+
#endif
98+
99+
bool io_detect_pin(void)
100+
{
101+
int rc;
102+
int pin_active;
103+
104+
if (!device_is_ready(button0.port)) {
105+
__ASSERT(false, "GPIO device is not ready.\n");
106+
return false;
107+
}
108+
109+
rc = gpio_pin_configure_dt(&button0, GPIO_INPUT);
110+
__ASSERT(rc == 0, "Failed to initialize boot detect pin.\n");
111+
112+
rc = gpio_pin_get_dt(&button0);
113+
pin_active = rc;
114+
115+
__ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
116+
117+
if (pin_active) {
118+
if (BUTTON_0_DETECT_DELAY > 0) {
119+
#ifdef CONFIG_MULTITHREADING
120+
k_sleep(K_MSEC(50));
121+
#else
122+
k_busy_wait(50000);
123+
#endif
124+
125+
/* Get the uptime for debounce purposes. */
126+
int64_t timestamp = k_uptime_get();
127+
128+
for(;;) {
129+
rc = gpio_pin_get_dt(&button0);
130+
pin_active = rc;
131+
__ASSERT(rc >= 0, "Failed to read boot detect pin.\n");
132+
133+
/* Get delta from when this started */
134+
uint32_t delta = k_uptime_get() - timestamp;
135+
136+
/* If not pressed OR if pressed > debounce period, stop. */
137+
if (delta >= BUTTON_0_DETECT_DELAY || !pin_active) {
138+
break;
139+
}
140+
141+
/* Delay 1 ms */
142+
#ifdef CONFIG_MULTITHREADING
143+
k_sleep(K_MSEC(1));
144+
#else
145+
k_busy_wait(1000);
146+
#endif
147+
}
148+
}
149+
}
150+
151+
return (bool)pin_active;
152+
}
153+
#endif
154+
155+
#if defined(CONFIG_BOOT_SERIAL_PIN_RESET)
156+
bool io_detect_pin_reset(void)
157+
{
158+
uint32_t reset_cause;
159+
int rc;
160+
161+
rc = hwinfo_get_reset_cause(&reset_cause);
162+
163+
if (rc == 0 && reset_cause == RESET_PIN) {
164+
(void)hwinfo_clear_reset_cause();
165+
return true;
166+
}
167+
168+
return false;
169+
}
170+
#endif
171+
172+
#if defined(CONFIG_BOOT_SERIAL_BOOT_MODE)
173+
bool io_detect_boot_mode(void)
174+
{
175+
int32_t boot_mode;
176+
177+
boot_mode = bootmode_check(BOOT_MODE_TYPE_BOOTLOADER);
178+
179+
if (boot_mode == 1) {
180+
/* Boot mode to stay in bootloader, clear status and enter serial
181+
* recovery mode
182+
*/
183+
bootmode_clear();
184+
185+
return true;
186+
}
187+
188+
return false;
189+
}
190+
#endif

0 commit comments

Comments
 (0)