PSRAM User Guide

[中文]

Overview

PSRAM (Pseudo Static RAM) is a type of memory that combines the simple interface of SRAM with the capacity advantages of DRAM. It uses DRAM technology internally to achieve larger capacity, while providing an SRAM-like interface to the host controller through integrated refresh control circuitry.

PSRAM Application Scenarios

PSRAM is particularly suitable for the following applications:

  • Image caching (camera frame buffers, image preprocessing)

  • Audio data processing (voice recognition, voice buffers)

  • Network application caching (TCP/IP buffers, OTA data)

  • Heap memory expansion (task creation, runtime memory allocation)

Our PSRAM Support (BK7258)

  • The BK7258 chip integrates built-in PSRAM (packaged inside the chip)

  • Automatic model and capacity detection: supports 4MB / 8MB / 16MB models. The driver automatically detects the PSRAM ID and configures initialization parameters during startup.

  • After initialization, the system automatically maps valid address space for developer use.

  • Note: Although the driver can automatically detect capacity, users must ensure that memory accesses do not exceed the valid size.

PSRAM Usage Instructions

  • PSRAM can be accessed by both AP and CP cores, but access should be coordinated to avoid resource conflicts and ensure proper mutex management.

  • Dynamic memory allocation is supported using psram_malloc() / psram_free() for multimedia caching, task creation, etc.

  • Part of the PSRAM can be used to expand heap memory, improving system memory flexibility.

PSRAM Types

BK7258 supports three types of PSRAM with the following configurations:

Name

Size (Mbyte)

W955D8MKY_5J APS6408L_O APS128XXO_OB9

4 8 16

PSRAM Partitioning

PSRAM is mainly divided into two parts: one part is used as multimedia data memory, and the other part is used as heap space for different CPU cores. The following macros define the size and usage of each part:

Macro

Value

Description

CONFIG_PSRAM CONFIG_PSRAM_AS_SYS_MEMORY CONFIG_USE_PSRAM_HEAP_AT_SRAM_OOM CONFIG_PSRAM_HEAP_BASE CONFIG_PSRAM_HEAP_SIZE CONFIG_PSRAM_HEAP_CPU0_BASE_ADDER

Y Y Y 0x60700000 0x80000 0x60700000

Enable PSRAM module Enable PSRAM as heap Allocate from PSRAM when SRAM is exhausted PSRAM heap base address for current CPU PSRAM heap size for current CPU Common PSRAM heap base address

Note

The above values differ across CPU cores. CONFIG_PSRAM_HEAP_CPU0_BASE_ADDER represents the base address for heap usage. The default PSRAM address range is from 0x60000000 up to CONFIG_PSRAM_HEAP_CPU0_BASE_ADDER for multimedia use, and the remaining space is used for heap.

CONFIG_PSRAM_HEAP_BASE defines the heap base address for the current core, and CONFIG_PSRAM_HEAP_SIZE defines its size. The values vary by core.

Whether PSRAM is used as heap on a specific core is controlled by CONFIG_PSRAM_AS_SYS_MEMORY.

Using PSRAM as Heap

  • Configure the following macros to set PSRAM heap base and size:

    CONFIG_PSRAM_HEAP_BASE // PSRAM heap base address
    CONFIG_PSRAM_HEAP_SIZE // PSRAM heap size
    
  • Memory allocation and release APIs:

    void *psram_malloc(size_t size);
    void psram_free(void *p);
    
  • On the BK7258 chip, PSRAM will be powered down in low power mode. If allocated PSRAM memory is not freed, it may prevent the system from entering low power mode. Use the following API to check current PSRAM memory usage:

    void bk_psram_heap_get_used_state(void);
    
  • Create a task on PSRAM:

    bk_err_t rtos_create_psram_thread(beken_thread_t *thread, uint8_t priority, const char *name, beken_thread_function_t function, uint32_t stack_size, beken_thread_arg_t arg);
    bk_err_t rtos_delete_thread(beken_thread_t *thread); // Task deletion API remains unchanged
    

PSRAM Partitioning for Multimedia

PSRAM used for multimedia is divided into four modules according to function, defined in psram_heap_type_t (located in bk_idk/include/driver/psram_types.h).

Each module’s memory is defined by psram_mem_slab_mapping in bk_avdk_main/components/media_utils/Kconfig. The size of each module is controlled by the following macros:

Macro

Value (bytes)

Description

CONFIG_PSRAM_MEM_SLAB_USER_SIZE CONFIG_PSRAM_MEM_SLAB_AUDIO_SIZE CONFIG_PSRAM_MEM_SLAB_ENCODE_SIZE CONFIG_PSRAM_MEM_SLAB_DISPLAY_SIZE

102400 102400 1433600 5701632

Size for user usage Size for audio usage Size for encoding (JPEG/H264) usage Size for display usage

Note

These macros define the default values and can be adjusted based on application needs. The total allocated size should not exceed CONFIG_PSRAM_HEAP_CPU0_BASE_ADDER to avoid conflicts.

Adjusting Memory Allocation for Each Module

PSRAM is divided into four modules for different data types:

  • USER: User-defined usage, size controlled by CONFIG_PSRAM_MEM_SLAB_USER_SIZE.

  • AUDIO: Audio data, size controlled by CONFIG_PSRAM_MEM_SLAB_AUDIO_SIZE.

  • ENCODE: Encoding data (JPEG/H264), size controlled by CONFIG_PSRAM_MEM_SLAB_ENCODE_SIZE.

  • DISPLAY: Display data (YUV, RGB565, RGB888), size controlled by CONFIG_PSRAM_MEM_SLAB_DISPLAY_SIZE.

For example:

  • ENCODE can store multiple JPEG or H264 frames. Frame size macros are defined in ./bk_idk/middleware/driver/camera/Kconfig:

Macro

Value (bytes)

Description

Range

CONFIG_JPEG_FRAME_SIZE CONFIG_H264_FRAME_SIZE

153600 65536

Size of one JPEG frame | [0, 204800] Size of one H264 frame | [0, 204800]

  • For example, for DISPLAY module with RGB565 format and resolution 800x480: One frame size = 800 * 480 * 2 = 768000 bytes. Max frames = CONFIG_PSRAM_MEM_SLAB_DISPLAY_SIZE / 768000 = 7 frames.

  • Similarly, ENCODE module max JPEG frames = CONFIG_PSRAM_MEM_SLAB_ENCODE_SIZE / CONFIG_JPEG_FRAME_SIZE = 9 frames.

  • The maximum frame count is defined in bk_avdk_main/components/multimedia/comm/frame_buffer.c:

    uint8_t fb_count[FB_INDEX_MAX] = {5, 4, H264_GOP_FRAME_CNT * 2};
    
    Meaning 5 DISPLAY frames, 4 JPEG frames, and H264_GOP_FRAME_CNT * 2 H264 frames.
    
    The number of frames can be adjusted as long as it does not exceed each module's allocated size.
    

Using PSRAM for Multimedia

Since multimedia functions run on CPU1, PSRAM usage for multimedia is directly controlled on CPU1. After CPU1 startup, the system automatically initializes PSRAM for multimedia use; no manual calls are required.

When CPU1 powers down, multimedia no longer uses PSRAM, and no explicit deallocation is needed.

  • Memory initialization API:

    bk_psram_frame_buffer_init
    
  • Memory allocation and release APIs:

    void *bk_psram_frame_buffer_malloc(psram_heap_type_t type, uint32_t size);
    void bk_psram_frame_buffer_free(void* mem_ptr);
    

Note

It is recommended to use the system APIs psram_malloc / psram_free for PSRAM memory management. Avoid using the above multimedia-specific APIs directly.

Configuring Cache for PSRAM

If the application layer wants to enable cache for PSRAM to improve performance, the following approach can be used.

Default PSRAM configuration:

{ ARM_MPU_RBAR(0x60000000UL, ARM_MPU_SH_NON, 0, 1, 0),
  ARM_MPU_RLAR(0x63FFFFE0UL, 1) },

To enable part of PSRAM as cacheable, modify the MPU configuration in mpu_cfg.c as follows:

{ ARM_MPU_RBAR(0x60000000UL, ARM_MPU_SH_INNER, 0, 1, 0),
  ARM_MPU_RLAR(0x62FFFFE0UL, 1) },  // non-cacheable

{ ARM_MPU_RBAR(0x62FFFFE0UL, ARM_MPU_SH_NON, 0, 1, 0),
  ARM_MPU_RLAR(0x63FFFFE0UL, 0) },  // cacheable

Note

  1. Do not configure the entire PSRAM as cacheable; use segmented configuration. The system reserves some regions as non-cacheable.

  2. The application must ensure data consistency when using cacheable PSRAM regions.