Skip to content

Commit

Permalink
Add linker script for PSRAM. Add PSRAM allocation code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylwester committed Oct 13, 2024
1 parent 3240820 commit 327df27
Show file tree
Hide file tree
Showing 5 changed files with 719 additions and 16 deletions.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ add_executable(main
src/picoadk_hw.cpp
src/midi_input_usb.cpp
src/get_serial.c
src/psram.cpp
lib/FreeRTOS-Kernel/portable/MemMang/heap_3.c
)

Expand Down Expand Up @@ -138,12 +139,24 @@ target_compile_definitions(main PRIVATE
PICO_USE_SW_SPIN_LOCKS=1
USE_USB_MIDI_HOST=0
SAMPLE_RATE=${DSP_SAMPLE_RATE}
RP2350_PSRAM_CS=0
)

target_compile_definitions(Audio PRIVATE
SAMPLE_RATE=${DSP_SAMPLE_RATE}
)

# Configure the linker script with actual values
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/default.ld.in # Input template file
${CMAKE_CURRENT_BINARY_DIR}/default.ld # Output linker script file
@ONLY # Use only @var@ style placeholders
)

# Set the linker script to use the custom default.ld
pico_set_linker_script(main ${CMAKE_CURRENT_BINARY_DIR}/default.ld)


target_link_options(main PRIVATE
-Xlinker --print-memory-usage
)
Expand Down
227 changes: 227 additions & 0 deletions default.ld.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
MEMORY
{
FLASH(rx) : ORIGIN = 0x10000000, LENGTH = @FLASH_LENGTH@
PSRAM(rwx) : ORIGIN = 0x11000000, LENGTH = @PSRAM_LENGTH@
RAM(rwx) : ORIGIN = 0x20000000, LENGTH = @RAM_LENGTH@
SCRATCH_X(rwx) : ORIGIN = 0x20080000, LENGTH = 4k
SCRATCH_Y(rwx) : ORIGIN = 0x20081000, LENGTH = 4k
}

PROVIDE ( _EEPROM_start = @EEPROM_START@ );
PROVIDE ( _FS_start = @FS_START@ );
PROVIDE ( _FS_end = @FS_END@ );

ENTRY(_entry_point)

SECTIONS
{
.flash_begin : {
__flash_binary_start = .;
} > FLASH

.ota : {
KEEP (*(.OTA))
*ota.o
} > FLASH

.partition : {
. = __flash_binary_start + 0x2ff0;
LONG(@FS_START@)
LONG(@FS_END@)
LONG(@EEPROM_START@)
LONG(@FLASH_LENGTH@)
} > FLASH

.text : {
KEEP (*(.vectors))
__logical_binary_start = .;
KEEP (*(.binary_info_header))
__binary_info_header_end = .;
KEEP (*(.embedded_block))
__embedded_block_end = .;
KEEP (*(.reset))
*(.init)
*libgcc.a:cmse_nonsecure_call.o
*(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*)
*(.fini)
*crtbegin.o(.ctors)
*crtbegin?.o(.ctors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors)
*(SORT(.ctors.*))
*(.ctors)
*crtbegin.o(.dtors)
*crtbegin?.o(.dtors)
*(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors)
*(SORT(.dtors.*))
*(.dtors)
. = ALIGN(4);
PROVIDE_HIDDEN (__preinit_array_start = .);
KEEP(*(SORT(.preinit_array.*)))
KEEP(*(.preinit_array))
PROVIDE_HIDDEN (__preinit_array_end = .);
. = ALIGN(4);
PROVIDE_HIDDEN (__init_array_start = .);
KEEP(*(SORT(.init_array.*)))
KEEP(*(.init_array))
PROVIDE_HIDDEN (__init_array_end = .);
. = ALIGN(4);
PROVIDE_HIDDEN (__fini_array_start = .);
*(SORT(.fini_array.*))
*(.fini_array)
PROVIDE_HIDDEN (__fini_array_end = .);
*(.eh_frame*)
. = ALIGN(4);
} > FLASH

.rodata : {
*(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .rodata*)
*(.srodata*)
. = ALIGN(4);
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.flashdata*)))
. = ALIGN(4);
} > FLASH

.ARM.extab : {
*(.ARM.extab* .gnu.linkonce.armextab.*)
} > FLASH

__exidx_start = .;
.ARM.exidx : {
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
} > FLASH
__exidx_end = .;

. = ALIGN(4);
__binary_info_start = .;
.binary_info : {
KEEP(*(.binary_info.keep.*))
*(.binary_info.*)
} > FLASH
__binary_info_end = .;
. = ALIGN(4);

.ram_vector_table (NOLOAD) : {
*(.ram_vector_table)
} > RAM

.uninitialized_data (NOLOAD) : {
. = ALIGN(4);
*(.uninitialized_data*)
} > RAM

.data : {
__data_start__ = .;
*(vtable)
*(.time_critical*)
*(.text*)
. = ALIGN(4);
*(.rodata*)
. = ALIGN(4);
*(.data*)
*(.sdata*)
. = ALIGN(4);
*(.after_data.*)
. = ALIGN(4);
PROVIDE_HIDDEN (__mutex_array_start = .);
KEEP(*(SORT(.mutex_array.*)))
KEEP(*(.mutex_array))
PROVIDE_HIDDEN (__mutex_array_end = .);
*(.jcr)
. = ALIGN(4);
} > RAM AT> FLASH

.tdata : {
. = ALIGN(4);
*(.tdata .tdata.* .gnu.linkonce.td.*)
__tdata_end = .;
} > RAM AT> FLASH
PROVIDE(__data_end__ = .);
__etext = LOADADDR(.data);

.tbss (NOLOAD) : {
. = ALIGN(4);
__bss_start__ = .;
__tls_base = .;
*(.tbss .tbss.* .gnu.linkonce.tb.*)
*(.tcommon)
__tls_end = .;
} > RAM

.bss (NOLOAD) : {
. = ALIGN(4);
__tbss_end = .;
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.bss*)))
*(COMMON)
PROVIDE(__global_pointer$ = . + 2K);
*(.sbss*)
. = ALIGN(4);
__bss_end__ = .;
} > RAM

.heap (NOLOAD) : {
__end__ = .;
end = __end__;
KEEP(*(.heap*))
. = ALIGN(4);
__HeapLimit = .;
} > RAM

.scratch_x : {
__scratch_x_start__ = .;
*(.scratch_x.*)
. = ALIGN(4);
__scratch_x_end__ = .;
} > SCRATCH_X AT > FLASH
__scratch_x_source__ = LOADADDR(.scratch_x);

.scratch_y : {
__scratch_y_start__ = .;
*(.scratch_y.*)
. = ALIGN(4);
__scratch_y_end__ = .;
} > SCRATCH_Y AT > FLASH
__scratch_y_source__ = LOADADDR(.scratch_y);

.stack1_dummy (NOLOAD) : {
*(.stack1*)
} > SCRATCH_X

.stack_dummy (NOLOAD) : {
KEEP(*(.stack*))
} > SCRATCH_Y

.flash_end : {
KEEP(*(.embedded_end_block*))
PROVIDE(__flash_binary_end = .);
} > FLASH = 0xaa

.psram (NOLOAD) : {
__psram_start__ = .;
*(.psram*)
. = ALIGN(4096);
__psram_heap_start__ = .;
} > PSRAM

__StackLimit = ORIGIN(RAM) + LENGTH(RAM);
__StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X);
__StackTop = ORIGIN(SCRATCH_Y) + LENGTH(SCRATCH_Y);
__StackOneBottom = __StackOneTop - SIZEOF(.stack1_dummy);
__StackBottom = __StackTop - SIZEOF(.stack_dummy);
PROVIDE(__stack = __StackTop);

PROVIDE (__heap_start = __end__);
PROVIDE (__heap_end = __HeapLimit);
PROVIDE( __tls_align = MAX(ALIGNOF(.tdata), ALIGNOF(.tbss)) );
PROVIDE( __tls_size_align = (__tls_size + __tls_align - 1) & ~(__tls_align - 1));
PROVIDE( __arm32_tls_tcb_offset = MAX(8, __tls_align) );

PROVIDE (__psram_start = __psram_start__);
PROVIDE (__psram_heap_start = __psram_heap_start__);

PROVIDE (_end = __end__);
PROVIDE (__llvm_libc_heap_limit = __HeapLimit);

ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed")
ASSERT( __binary_info_header_end - __logical_binary_start <= 1024, "Binary info must be in first 1024 bytes of the binary")
ASSERT( __embedded_block_end - __logical_binary_start <= 4096, "Embedded block must be in first 4096 bytes of the binary")
}
39 changes: 39 additions & 0 deletions include/psram.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
@file sfe_psram.c
@brief This file contains a function that is used to detect and initialize PSRAM on
SparkFun rp2350 boards.
*/

/*
The MIT License (MIT)
Copyright (c) 2024 SparkFun Electronics
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions: The
above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#pragma once

void psram_reinit_timing();
void *__psram_malloc(size_t size);
void __psram_free(void *ptr);
void *__psram_realloc(void *ptr, size_t size);
void *__psram_calloc(size_t num, size_t size);
size_t __psram_largest_free_block();
size_t __psram_total_space();
size_t __psram_total_used();
19 changes: 3 additions & 16 deletions src/picoadk_hw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ void picoadk_init()

// Initialize TinyUSB
board_init();
tusb_init(0, TUSB_ROLE_DEVICE);

tusb_init();

stdio_init_all();

// set gpio 25 (soft mute) to output and set to 1 (unmute)
Expand Down Expand Up @@ -43,21 +45,6 @@ void picoadk_init()
}

srand(rand_seed);

gpio_set_function(10, GPIO_FUNC_SPI);
gpio_set_function(11, GPIO_FUNC_SPI);
gpio_set_function(12, GPIO_FUNC_SPI);
gpio_init(13);
gpio_set_dir(13, GPIO_OUT);
gpio_put(13, 1);

spi_init(spi1, 16000000);
spi_set_format(spi1, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
spi_set_slave(spi1, false);

// Set up PSRAM
gpio_set_function(47, GPIO_FUNC_XIP_CS1); // CS for PSRAM
xip_ctrl_hw->ctrl|=XIP_CTRL_WRITABLE_M1_BITS;
}

int adc128_read(uint8_t chan)
Expand Down
Loading

0 comments on commit 327df27

Please sign in to comment.