该如何使用psram

使用lvgl想创建画布,这种时候就需要一个大的buff,不知道思澈的psram该如何使用,在sdk的某个readme里看见了如下使用psram的方法

// Initial PSRAM hardware before using it
rt_psram_init(); 

// Define PSRAM base address for memory access, it can not be changed
#define PSRAM_BASE_ADDR             PSRAM_BASE

int *buf = (int *)PSRAM_BASE_ADDR;
int i;

// Write psram memory
for(i=0; i<1000; i++)
    buf[i] = i*6543;

// Read psram
int value = *buf;

// Read and Write
int *src = (int *)PSRAM_BASE_ADDR;
int *dst = (int *)(PSRAM_BASE_ADDR + 0x100000);
memcpy(dst, src, 1000);

是只能使用原始的方法吗,有没有内存管理函数,rtt我看自身应该就带内存管理,另外就是如果我屏幕也在外部申请,这个起始位置是多少,会不会还有别的程序在使用psram,需要注意什么

不建议在正常的应用中这么做,这样无法去管理某块内存的生命周期,容易出问题。
正常在PSRAM中定义一个数组的方法为:

static uint8_t opus_heap_pool[1024 * 1024] L2_RET_BSS_SECT(opus_heap_pool);

如果想alloc到PSRAM中,需要使用到rt_memheap*函数,需要先

static struct rt_memheap opus_memheap;
int opus_heap_init(void)
{
    rt_memheap_init(&opus_memheap, "opus_memheap", (void *)opus_heap_pool,
                    sizeof(opus_heap_pool));
    return 0;
}

之后使用rt_memheap_alloc/free即可

void *opus_heap_malloc(uint32_t size)
{
    return rt_memheap_alloc(&opus_memheap, size);
}

void opus_heap_free(void *p)
{
    rt_memheap_free(p);
}
2 个赞