Skip to content

Commit

Permalink
flashstub: Finished getting the RP2040 Flash stub integrated into the…
Browse files Browse the repository at this point in the history
… Meson build system
  • Loading branch information
dragonmux committed Oct 22, 2024
1 parent 090d010 commit e357716
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 34 deletions.
9 changes: 5 additions & 4 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ endif
probe_host = disabler()
probe_bootloader = disabler()

# Deal with the print memory usage option before recursing so the Flash stubs don't screw things up
if is_firmware_build and get_option('print_memory_usage')
add_project_link_arguments('-Wl,--print-memory-usage', language: 'c')
endif

subdir('src')

## Black Magic Firmware (BMF) targets
Expand All @@ -171,10 +176,6 @@ If you did not touch the build system this is not your fault, please report it
''',
)

if get_option('print_memory_usage')
add_project_link_arguments('-Wl,--print-memory-usage', language: 'c')
endif

# System binary utilities
size = find_program('size')
objcopy = find_program('objcopy')
Expand Down
82 changes: 57 additions & 25 deletions src/target/flashstub/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

hexdump = find_program('hexdump', native: true, required: false)
# Default definitions of the resulting stubs for all this that are harmless for, eg, BMDA
rp2040_stub = []

write_stub = generator(
hexdump,
arguments: [
'-v',
'-e', '/2 "0x%04X, "',
'@INPUT@'
],
ouptut: '@CURRENT_SOURCE_DIR@/@BASENAME@.stub'
capture: true,
)
# If we're doing a firmware build, type to find hexdump
if is_firmware_build
hexdump = find_program('hexdump', required: false)
else
hexdump = disabler()
endif
# If we cannot or we're not a firmware build, we're done
if not hexdump.found()
subdir_done()
endif

# We found hexdump, so build the stubs and their conversions to text files containing suitably formatted
# hexadecimal of their code, ready to go onto a target.

# Default options for all stub builds
stub_build_args = [
'-mthumb',
'-nostartfiles',
Expand All @@ -49,26 +54,53 @@ stub_build_args = [
'-ffreestanding',
]

lmi_stub_elf = executable(
'lmi_stub',
'lmi.c',
c_args: [
'-mcpu=cortexm-m4',
stub_build_args,
]
)
# Flash stub for Stellaris/Tiva-C parts
# lmi_stub_elf = executable(
# 'lmi_stub',
# 'lmi.c',
# c_args: [
# '-mcpu=cortex-m4',
# stub_build_args,
# ]
# )

efm32_stub_elf = executable(
'efm32_stub'
'efm32.c',
c_args: stub_build_args,
)
# Flash stub for EFM32 parts
# efm32_stub_elf = executable(
# 'efm32_stub',
# 'efm32.c',
# c_args: [
# '-mcpu=cortex-m3',
# stub_build_args,
# ]
# )

# Flash stub for the RP2040
rp2040_stub_elf = executable(
'rp_stub',
'rp.c',
c_args: [
'-mcpu=cortexm-m0plus',
'-mcpu=cortex-m0plus',
stub_build_args
],
link_args: [
'-mcpu=cortex-m0plus',
stub_build_args,
'-T', '@0@/rp.ld'.format(meson.current_source_dir()),
],
link_depends: files('rp.ld'),
pie: false,
install: false,
)

rp2040_stub = custom_target(
'rp_stub-hex',
command: [
hexdump,
'-v',
'-e', '/2 "0x%04X, "',
'@INPUT@'
],
input: rp2040_stub_elf,
output: 'rp.stub',
capture: true,
)
7 changes: 3 additions & 4 deletions src/target/flashstub/rp.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ static gpio_qspi_s *const gpio_qspi = (gpio_qspi_s *)RP_GPIO_QSPI_BASE_ADDR;

#define MIN(x, y) (((x) < (y)) ? (x) : (y))

void __attribute__((naked, used, section(".entry")))
rp_flash_write_stub(const uint16_t command, const uint32_t dest, const uint8_t *const src, const uint32_t length)
void __attribute__((naked, used, section(".entry"))) rp_flash_write_stub()
{
/* Create a stack for our own sanity */
__asm__("ldr r4, =#0x20042000\n"
Expand Down Expand Up @@ -150,8 +149,8 @@ static void rp_spi_write(const uint32_t address, const uint8_t *const src, const
rp_spi_flash_deselect();
}

static void __attribute__((used, section(".entry")))
rp_flash_write(const uint32_t dest, const uint8_t *const src, const size_t length, const uint32_t page_size)
static void __attribute__((used, section(".entry"))) rp_flash_write(
const uint32_t dest, const uint8_t *const src, const size_t length, const uint32_t page_size)
{
for (size_t offset = 0; offset < length; offset += page_size) {
/* Try to write-enable the Flash */
Expand Down
4 changes: 3 additions & 1 deletion src/target/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

target_common_includes = include_directories('.')

subdir('flashstub')

target_common_sources = files(
'adi.c',
'adiv5.c',
Expand Down Expand Up @@ -241,7 +243,7 @@ target_rp = declare_dependency(
sources: files(
'rp2040.c',
'rp2350.c',
),
) + rp2040_stub,
dependencies: target_cortexm,
)

Expand Down

0 comments on commit e357716

Please sign in to comment.