配置sct文件
LR_IROM1 0x08000000 0x00200000 { ; load region size_region
ER_IROM1 0x08000000 0x00200000 { ; load address = execution address
*.o (RESET, +First)
*(InRoot$$Sections)
.ANY (+RO)
}
; RW data - 128KB DTCM
RW_IRAM1 0x20000000 0x00020000 {
.ANY (+RW +ZI)
}
; RW data - 512KB AXI SRAM
RW_IRAM2 0x24000000 0x00080000 {
*(.RAM_D1)
}
; RW data - 128KB SRAM1(0x30000000) + 128KB SRAM2(0x3002 0000) + 32KB SRAM3(0x30040000)
RW_IRAM3 0x30000000 0x00048000 {
*(.RAM_D2)
}
; RW data - 64KB SRAM4(0x38000000)
RW_IRAM4 0x38000000 0x00010000 {
*(.RAM_D3)
}
}
第 1 – 2 行,LR_IROM1 是 Load Region 加载域,ER_IROM1 是 Execution Region 执行域。首地
址都是 0x0800 0000,大小都是 0x0020 0000,即 STM32H7 的 Flash 地址和对应大小。
RW 区也是要存储到 ROM/Flash 里面的,在执行映像之前,必须将已初始化的 RW 数据从 ROM 中复制到 RAM 中的执行地址并创建 ZI Section(初始化为 0 的变量区)。
第 3 行的*.o (RESET, +First)
在启动文件 startup_stm32h743xx.s 有个段名为 RESET 的代码段,主要存储了中断向量表。这里是将其存放在 Flash 的首地址。
第 4 行的*(InRoot$$Sections)
这里是将 MDK 的一些库文件全部放在根域,比如__main.o, _scatter*.o, _dc*.o。
第 5 行.ANY (+RO)
将目标文件中所有具有 RO 只读属性的数据放在这里,即 ER_IROM1。
第 9-11 行,RW_IRAM1 是执行域,配置的是 DTCM,首地址 0x2000 0000,大小 128KB。
将目标文件中所有具有 RW 和 ZI 数据放在这里。
第 14-16 行,RW_IRAM2 是执行域,配置的是 AXI SRAM,首地址 0x24000000,大小 512KB。给这个域专门配了一个名字 .RAM_D1。这样就可以通过__attribute__((section(“name”)))将其分配到这个 RAM 域。
第 19-21 行,RW_IRAM3 是执行域,配置的是 D2 域的 SRAM1,SRAM2 和 SRAM3,首地址0x30000000,共计大小 288KB。给这个域专门配了一个名字 .RAM_D2。这样就可以通过__attribute__((section(“name”)))将其分配到这个 RAM 域。
第 24-26 行,RW_IRAM3 是执行域,配置的是 D3 域的 SRAM4,首地址 0x38000000,共计大小64KB。给这个域专门配了一个名字 .RAM_D3。这样就可以通过__attribute__((section(“name”)))将其分配到这个 RAM 域。

内存使用
/* 定义在 512KB AXI SRAM 里面的变量 */
__attribute__((section (".RAM_D1"))) uint32_t AXISRAMBuf[10];
__attribute__((section (".RAM_D1"))) uint16_t AXISRAMCount;
/* 定义在 128KB SRAM1(0x30000000) + 128KB SRAM2(0x30020000) + 32KB SRAM3(0x30040000)里面的变量 */
__attribute__((section (".RAM_D2"))) uint32_t D2SRAMBuf[10];
__attribute__((section (".RAM_D2"))) uint16_t D2SRAMount;
/* 定义在 64KB SRAM4(0x38000000)里面的变量 */
__attribute__((section (".RAM_D3"))) uint32_t D3SRAMBuf[10];
__attribute__((section (".RAM_D3"))) uint16_t D3SRAMCount;
AXISRAMBuf[0] = AXISRAMCount++;
AXISRAMBuf[5] = AXISRAMCount++;
AXISRAMBuf[9] = AXISRAMCount++;
D2SRAMBuf[0] = D2SRAMount++;
D2SRAMBuf[5] = D2SRAMount++;
D2SRAMBuf[9] = D2SRAMount++;
D3SRAMBuf[0] = D3SRAMCount++;
D3SRAMBuf[5] = D3SRAMCount++;
D3SRAMBuf[9] = D3SRAMCount++;
动态内存分配
rtx_lib.h
#ifndef RTX_LIB_H_
#define RTX_LIB_H_
#include <string.h>
#include <stdint.h>
#include "stm32h7xx.h"
// Memory Pool Header structure
typedef struct {
uint32_t size; // Memory Pool size
uint32_t used; // Used Memory
} mem_head_t;
// Memory Block Header structure
typedef struct mem_block_s {
struct mem_block_s *next; // Next Memory Block in list
uint32_t info; // Block Info or max used Memory (in last block)
} mem_block_t;
// Memory Block Info: Length = <31:2>:'00', Type = <1:0>
#define MB_INFO_LEN_MASK 0xFFFFFFFCU // Length mask
#define MB_INFO_TYPE_MASK 0x00000003U // Type mask
// Memory Head Pointer
__STATIC_INLINE mem_head_t *MemHeadPtr (void *mem) {
//lint -e{9079} -e{9087} "conversion from pointer to void to pointer to other type" [MISRA Note 6]
return ((mem_head_t *)mem);
}
// Memory Block Pointer
__STATIC_INLINE mem_block_t *MemBlockPtr (void *mem, uint32_t offset) {
uint32_t addr;
mem_block_t *ptr;
//lint --e{923} --e{9078} "cast between pointer and unsigned int" [MISRA Note 8]
addr = (uint32_t)mem + offset;
ptr = (mem_block_t *)addr;
return ptr;
}
// ==== Library functions ====
// Memory Heap Library functions
extern uint32_t osRtxMemoryInit (void *mem, uint32_t size);
extern void *osRtxMemoryAlloc(void *mem, uint32_t size, uint32_t type);
extern uint32_t osRtxMemoryFree (void *mem, void *block);
#endif // RTX_LIB_H_
rtx_memory.c
#include "rtx_lib.h"
// ==== Library functions ====
/// Initialize Memory Pool with variable block size.
/// \param[in] mem pointer to memory pool.
/// \param[in] size size of a memory pool in bytes.
/// \return 1 - success, 0 - failure.
__WEAK uint32_t osRtxMemoryInit (void *mem, uint32_t size) {
mem_head_t *head;
mem_block_t *ptr;
// Check parameters
//lint -e{923} "cast from pointer to unsigned int" [MISRA Note 7]
if ((mem == NULL) || (((uint32_t)mem & 7U) != 0U) || ((size & 7U) != 0U) ||
(size < (sizeof(mem_head_t) + (2U*sizeof(mem_block_t))))) {
//EvrRtxMemoryInit(mem, size, 0U);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return 0U;
}
// Initialize memory pool header
head = MemHeadPtr(mem);
head->size = size;
head->used = sizeof(mem_head_t) + sizeof(mem_block_t);
// Initialize first and last block header
ptr = MemBlockPtr(mem, sizeof(mem_head_t));
ptr->next = MemBlockPtr(mem, size - sizeof(mem_block_t));
ptr->next->next = NULL;
ptr->next->info = sizeof(mem_head_t) + sizeof(mem_block_t);
ptr->info = 0U;
//EvrRtxMemoryInit(mem, size, 1U);
return 1U;
}
/// Allocate a memory block from a Memory Pool.
/// \param[in] mem pointer to memory pool.
/// \param[in] size size of a memory block in bytes.
/// \param[in] type memory block type: 0 - generic, 1 - control block
/// \return allocated memory block or NULL in case of no memory is available.
__WEAK void *osRtxMemoryAlloc (void *mem, uint32_t size, uint32_t type) {
mem_block_t *ptr;
mem_block_t *p, *p_new;
uint32_t block_size;
uint32_t hole_size;
// Check parameters
if ((mem == NULL) || (size == 0U) || ((type & ~MB_INFO_TYPE_MASK) != 0U)) {
//EvrRtxMemoryAlloc(mem, size, type, NULL);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
// Add block header to size
block_size = size + sizeof(mem_block_t);
// Make sure that block is 8-byte aligned
block_size = (block_size + 7U) & ~((uint32_t)7U);
// Search for hole big enough
p = MemBlockPtr(mem, sizeof(mem_head_t));
for (;;) {
//lint -e{923} -e{9078} "cast from pointer to unsigned int"
hole_size = (uint32_t)p->next - (uint32_t)p;
hole_size -= p->info & MB_INFO_LEN_MASK;
if (hole_size >= block_size) {
// Hole found
break;
}
p = p->next;
if (p->next == NULL) {
// Failed (end of list)
//EvrRtxMemoryAlloc(mem, size, type, NULL);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return NULL;
}
}
// Update used memory
(MemHeadPtr(mem))->used += block_size;
// Update max used memory
p_new = MemBlockPtr(mem, (MemHeadPtr(mem))->size - sizeof(mem_block_t));
if (p_new->info < (MemHeadPtr(mem))->used) {
p_new->info = (MemHeadPtr(mem))->used;
}
// Allocate block
if (p->info == 0U) {
// No block allocated, set info of first element
p->info = block_size | type;
ptr = MemBlockPtr(p, sizeof(mem_block_t));
} else {
// Insert new element into the list
p_new = MemBlockPtr(p, p->info & MB_INFO_LEN_MASK);
p_new->next = p->next;
p_new->info = block_size | type;
p->next = p_new;
ptr = MemBlockPtr(p_new, sizeof(mem_block_t));
}
//EvrRtxMemoryAlloc(mem, size, type, ptr);
return ptr;
}
/// Return an allocated memory block back to a Memory Pool.
/// \param[in] mem pointer to memory pool.
/// \param[in] block memory block to be returned to the memory pool.
/// \return 1 - success, 0 - failure.
__WEAK uint32_t osRtxMemoryFree (void *mem, void *block) {
const mem_block_t *ptr;
mem_block_t *p, *p_prev;
// Check parameters
if ((mem == NULL) || (block == NULL)) {
//EvrRtxMemoryFree(mem, block, 0U);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return 0U;
}
// Memory block header
ptr = MemBlockPtr(block, 0U);
ptr--;
// Search for block header
p_prev = NULL;
p = MemBlockPtr(mem, sizeof(mem_head_t));
while (p != ptr) {
p_prev = p;
p = p->next;
if (p == NULL) {
// Not found
//EvrRtxMemoryFree(mem, block, 0U);
//lint -e{904} "Return statement before end of function" [MISRA Note 1]
return 0U;
}
}
// Update used memory
(MemHeadPtr(mem))->used -= p->info & MB_INFO_LEN_MASK;
// Free block
if (p_prev == NULL) {
// Release first block, only set info to 0
p->info = 0U;
} else {
// Discard block from chained list
p_prev->next = p->next;
}
//EvrRtxMemoryFree(mem, block, 1U);
return 1U;
}
main.c
/* D1, AXI SRAM, 512KB */
mem_head_t *AXISRAMUsed;
uint64_t AppMallocAXISRAM[512*1024/8]__attribute__((at(0x24000000)));
/* D2, 128KB SRAM1(0x30000000) + 128KB SRAM2(0x30020000) + 32KB SRAM3(0x30040000) */
mem_head_t *SRAM1Used;
uint64_t AppMallocSRAM1[288*1024/8]__attribute__((at(0x30000000)));
/* D3, SRAM4, 64KB */
mem_head_t *SRAM4Used;
uint64_t AppMallocSRAM4[64*1024/8]__attribute__((at(0x38000000)));
uint32_t *DTCM_Addres0, *AXISRAM_Addres0, *SRAM1_Addres0, *SRAM4_Addres0;
uint16_t *DTCM_Addres1, *AXISRAM_Addres1, *SRAM1_Addres1, *SRAM4_Addres1;
uint8_t *DTCM_Addres2, *AXISRAM_Addres2, *SRAM1_Addres2, *SRAM4_Addres2;
bsp_Init();
osRtxMemoryInit(AppMallocDTCM, sizeof(AppMallocDTCM));
osRtxMemoryInit(AppMallocAXISRAM, sizeof(AppMallocAXISRAM));
osRtxMemoryInit(AppMallocSRAM1, sizeof(AppMallocSRAM1));
osRtxMemoryInit(AppMallocSRAM4, sizeof(AppMallocSRAM4));
/* 从DTCM申请280字节空间,使用指针变量DTCM_Addres0操作这些空间时不要超过280字节大小 */
printf("=========================================================\r\n");
DTCM_Addres0 = osRtxMemoryAlloc(AppMallocDTCM, 280, 0);
DTCMUsed = MemHeadPtr(AppMallocDTCM);
printf("DTCM 总大小 = %d 字节,申请大小 = 0280 字节,当前共使用大小 = %d 字节\r\n",
DTCMUsed->size, DTCMUsed->used);
/* 从 DTCM 申请 64 字节空间,使用指针变量 DTCM_Addres1 操作这些空间时不要超过 64 字节大小 */
DTCM_Addres1 = osRtxMemoryAlloc(AppMallocDTCM, 64, 0);
DTCMUsed = MemHeadPtr(AppMallocDTCM);
printf("DTCM 总大小 = %d 字节,申请大小 = 0064 字节,当前共使用大小 = %d 字节\r\n",
DTCMUsed->size, DTCMUsed->used);
/* 从 DTCM 申请 6111 字节空间,使用指针变量 DTCM_Addres2 操作这些空间时不要超过 6111 字节大小 */
DTCM_Addres2 = osRtxMemoryAlloc(AppMallocDTCM, 6111, 0);
DTCMUsed = MemHeadPtr(AppMallocDTCM);
printf("DTCM 总大小 = %d 字节,申请大小 = 6111 字节,当前共使用大小 = %d 字节\r\n",
DTCMUsed->size, DTCMUsed->used);
break;
1772




被折叠的 条评论
为什么被折叠?



