Skip to content

Commit 2477914

Browse files
sumpfralleacassis
authored andcommitted
board/arm/rp2040: implement "board_boot_image" for bootloader support
This function is required by the bootloaders (nxboot and mcuboot). Signed-off-by: Lars Kruse <[email protected]>
1 parent b92e0b6 commit 2477914

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

boards/arm/rp2040/common/src/Make.defs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ ifeq ($(CONFIG_BOARDCTL_RESET),y)
2929
CSRCS += rp2040_reset.c
3030
endif
3131

32+
ifeq ($(CONFIG_BOARDCTL_BOOT_IMAGE),y)
33+
CSRCS += rp2040_boot_image.c
34+
endif
35+
3236
ifeq ($(CONFIG_SPI),y)
3337
CSRCS += rp2040_spi.c
3438
endif
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/****************************************************************************
2+
* boards/arm/rp2040/common/src/rp2040_boot_image.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <nuttx/config.h>
28+
29+
#include <debug.h>
30+
#include <stdio.h>
31+
#include <fcntl.h>
32+
33+
#include <sys/boardctl.h>
34+
#include <nuttx/irq.h>
35+
#include <arch/barriers.h>
36+
37+
#include "nvic.h"
38+
#include "arm_internal.h"
39+
40+
/****************************************************************************
41+
* Pre-processor Definitions
42+
****************************************************************************/
43+
44+
/****************************************************************************
45+
* Private Types
46+
****************************************************************************/
47+
48+
/* This structure represents the first two entries on NVIC vector table */
49+
50+
struct arm_vector_table
51+
{
52+
uint32_t spr; /* Stack pointer on reset */
53+
uint32_t reset; /* Pointer to reset exception handler */
54+
};
55+
56+
/****************************************************************************
57+
* Private Function Prototypes
58+
****************************************************************************/
59+
60+
static void cleanup_arm_nvic(void);
61+
static void systick_disable(void);
62+
63+
/****************************************************************************
64+
* Private Functions
65+
****************************************************************************/
66+
67+
/****************************************************************************
68+
* Name: cleanup_arm_nvic
69+
*
70+
* Description:
71+
* Acknowledge and disable all interrupts in NVIC
72+
*
73+
* Input Parameters:
74+
* None
75+
*
76+
* Returned Value:
77+
* None
78+
*
79+
****************************************************************************/
80+
81+
static void cleanup_arm_nvic(void)
82+
{
83+
/* Allow any pending interrupts to be recognized */
84+
85+
UP_ISB();
86+
up_irq_disable();
87+
88+
/* Disable all interrupts */
89+
90+
putreg32(0xffffffff, ARMV6M_NVIC_ICER);
91+
92+
/* Clear all pending interrupts */
93+
94+
putreg32(0xffffffff, ARMV6M_NVIC_ICPR);
95+
}
96+
97+
/****************************************************************************
98+
* Name: systick_disable
99+
*
100+
* Description:
101+
* Disable the SysTick system timer
102+
*
103+
* Input Parameters:
104+
* None
105+
*
106+
* Returned Value:
107+
* None
108+
*
109+
****************************************************************************/
110+
111+
static void systick_disable(void)
112+
{
113+
putreg32(0, ARMV6M_SYSTICK_CSR);
114+
putreg32(SYSTICK_RVR_MASK, ARMV6M_SYSTICK_RVR);
115+
putreg32(0, ARMV6M_SYSTICK_CVR);
116+
}
117+
118+
/****************************************************************************
119+
* Public Functions
120+
****************************************************************************/
121+
122+
/****************************************************************************
123+
* Name: board_boot_image
124+
*
125+
* Description:
126+
* This entry point is called by bootloader to jump to application image.
127+
*
128+
****************************************************************************/
129+
130+
int board_boot_image(const char *path, uint32_t hdr_size)
131+
{
132+
static struct arm_vector_table vt;
133+
struct file file;
134+
ssize_t bytes;
135+
int ret;
136+
137+
ret = file_open(&file, path, O_RDONLY | O_CLOEXEC);
138+
if (ret < 0)
139+
{
140+
syslog(LOG_ERR, "Failed to open %s with: %d", path, ret);
141+
return ret;
142+
}
143+
144+
bytes = file_pread(&file, &vt, sizeof(vt), hdr_size);
145+
if (bytes != sizeof(vt))
146+
{
147+
syslog(LOG_ERR, "Failed to read ARM vector table: %d", bytes);
148+
return bytes < 0 ? bytes : -1;
149+
}
150+
151+
systick_disable();
152+
153+
cleanup_arm_nvic();
154+
155+
/* Set main and process stack pointers */
156+
157+
__asm__ __volatile__(
158+
"\tmsr msp, %0\n"
159+
"\tmsr control, %1\n"
160+
"\tisb\n"
161+
"\tmov pc, %2\n"
162+
:
163+
: "r" (vt.spr), "r" (0), "r" (vt.reset));
164+
165+
return 0;
166+
}

0 commit comments

Comments
 (0)