Skip to content

Commit 0c0b9e5

Browse files
unicornxRbb666
authored andcommitted
bsp: k230: use configuration instead of immediate value
The original code used immediate values directly when it came to memory layout, which was not a good programming habit. Now we use macros and try to be compatible with SDK configuration values in the SDK compilation environment. The main changes are to clean up board.h, and extract some memory layout constants to define a new header file mem_layout.h. Also update KERNEL_VADDR_START to the default value for Smart, so we can use it as base to calculate other mapping address. Signed-off-by: Chen Wang <unicorn_wang@outlook.com>
1 parent 2a00bd4 commit 0c0b9e5

File tree

6 files changed

+109
-33
lines changed

6 files changed

+109
-33
lines changed

bsp/k230/.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ CONFIG_RT_BACKTRACE_LEVEL_MAX_NR=32
201201
CONFIG_ARCH_CPU_64BIT=y
202202
CONFIG_RT_USING_CACHE=y
203203
CONFIG_ARCH_MM_MMU=y
204-
CONFIG_KERNEL_VADDR_START=0xffffffc000020000
204+
CONFIG_KERNEL_VADDR_START=0xffffffc000000000
205205
CONFIG_ARCH_RISCV=y
206206
CONFIG_ARCH_RISCV_FPU=y
207207
CONFIG_ARCH_RISCV_FPU_D=y

bsp/k230/board/board.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,30 @@
2222
#ifdef RT_USING_SMART
2323
#include <mmu.h>
2424
#include "page.h"
25+
#endif
26+
27+
extern unsigned int __sram_size;
28+
extern unsigned int __sram_base;
29+
extern unsigned int __sram_end;
30+
#define RAM_END (rt_size_t)((void *)&__sram_end)
31+
32+
extern unsigned int __bss_start;
33+
extern unsigned int __bss_end;
2534

26-
/* respect to boot loader, must be 0xFFFFFFC000200000 */
27-
RT_STATIC_ASSERT(kmem_region, KERNEL_VADDR_START == 0xffffffc000020000);
35+
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
36+
#define RT_HW_HEAP_END ((void *)(((rt_size_t)RT_HW_HEAP_BEGIN) + CONFIG_MEM_RTSMART_HEAP_SIZE))
37+
38+
#define RT_HW_PAGE_START ((void *)((rt_size_t)RT_HW_HEAP_END + sizeof(rt_size_t)))
39+
#define RT_HW_PAGE_END ((void *)(RAM_END))
40+
41+
#ifdef RT_USING_SMART
2842

2943
rt_region_t init_page_region = {(rt_size_t)RT_HW_PAGE_START, (rt_size_t)RT_HW_PAGE_END};
3044

3145
extern size_t MMUTable[];
3246

3347
struct mem_desc platform_mem_desc[] = {
34-
{KERNEL_VADDR_START, (rt_size_t)0xFFFFFFC020000000 - 1, (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
48+
{KERNEL_VADDR_START, (rt_size_t)(KERNEL_VADDR_START + CONFIG_MEM_MMZ_BASE + CONFIG_MEM_MMZ_SIZE - 1), (rt_size_t)ARCH_MAP_FAILED, NORMAL_MEM},
3549
};
3650

3751
#define NUM_MEM_DESC (sizeof(platform_mem_desc) / sizeof(platform_mem_desc[0]))

bsp/k230/board/board.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,14 @@
1111
#ifndef BOARD_H__
1212
#define BOARD_H__
1313

14-
#include <rtconfig.h>
14+
#include "rtconfig.h"
15+
#include "mem_layout.h"
1516

16-
extern unsigned int __sram_size;
17-
extern unsigned int __sram_base;
18-
extern unsigned int __sram_end;
19-
#define RAM_END (rt_size_t)((void *)&__sram_end)
20-
21-
extern unsigned int __bss_start;
22-
extern unsigned int __bss_end;
23-
24-
#define RT_HW_HEAP_BEGIN ((void *)&__bss_end)
25-
#define RT_HW_HEAP_END ((void *)(((rt_size_t)RT_HW_HEAP_BEGIN) + 0x2000000 ))
26-
27-
#define RT_HW_PAGE_START ((void *)((rt_size_t)RT_HW_HEAP_END + sizeof(rt_size_t)))
28-
#define RT_HW_PAGE_END ((void *)(RAM_END))
29-
30-
void rt_hw_board_init(void);
31-
void rt_init_user_mem(struct rt_thread *thread, const char *name, unsigned long *entry);
32-
33-
#define TIMER_CLK_FREQ (27000000)
34-
35-
/* From K230 Technical Reference Manual, chapter 1.5 Address Space mapping */
17+
/*
18+
* K230 Memory Map
19+
*
20+
* See K230 Technical Reference Manual, chapter 1.5 Address Space mapping
21+
*/
3622
#define SRAM_BASE_ADDR (0x80200000UL)
3723
#define SRAM_IO_SIZE (0x00200000UL)
3824

@@ -200,4 +186,7 @@ void rt_init_user_mem(struct rt_thread *thread, const char *name, unsigned long
200186
#define SPI_XIP_FLASH_IO_SIZE (0x08000000UL)
201187

202188
#define IO_SPACE_BASE_ADDR (KPU_BASE_ADDR)
203-
#endif
189+
190+
#define TIMER_CLK_FREQ (27000000)
191+
192+
#endif // BOARD_H__

bsp/k230/board/mem_layout.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2006-2021, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#ifndef MEMORY_LAYOUT_H__
7+
#define MEMORY_LAYOUT_H__
8+
9+
#include "../rtconfig.h"
10+
11+
/*
12+
* Physical Memory layout:
13+
*
14+
* +---------+ <- CONFIG_MEM_TOTAL_SIZE
15+
* | ...... | maybe zero
16+
* +---------+ <- CONFIG_MEM_MMZ_BASE + CONFIG_MEM_MMZ_SIZE
17+
* | |
18+
* | |
19+
* | |
20+
* | |
21+
* | |
22+
* | |
23+
* | |
24+
* | |
25+
* |---------| <- CONFIG_MEM_MMZ_BASE
26+
* | ...... | maybe zero
27+
* +---------+ <- CONFIG_MEM_RTSMART_SIZE
28+
* | guard | MEM_GUARD_SIZE
29+
* +---------+ <- End of Kerenl
30+
* | |
31+
* | |
32+
* +---------+
33+
* | heap | CONFIG_MEM_RTSMART_HEAP_SIZE
34+
* +---------+
35+
* | |
36+
* +---------+ <- Beginning of Kernel <- mapping to KERNEL_VADDR_START + MEM_OPENSBI_SIZE
37+
* | opensbi | MEM_OPENSBI_SIZE
38+
* +---------+ <- Beginning of Physical Memory (0) <- mapping to KERNEL_VADDR_START
39+
*/
40+
41+
#define MEM_OPENSBI_SIZE 0x20000 // 128K
42+
#define MEM_GUARD_SIZE 0x1000 // 4K
43+
44+
/*
45+
* The value of CONFIG_XXX may come from rtconfig.h. This is mainly for
46+
* compatibility with compiling RT-Thread in the SDK environment and using
47+
* the SDK configuration.
48+
*
49+
* If CONFIG_XXX is defined, it means that the value comes from the SDK
50+
* configuration, otherwise the default configuration of bsp/k230 in RT-Thead
51+
* is used. The default configuration of bsp/k230 is for the 01Studio CanMV
52+
* development board that supports 512MB of memory.
53+
*/
54+
#ifndef CONFIG_MEM_TOTAL_SIZE
55+
#define CONFIG_MEM_TOTAL_SIZE 0x20000000 // 512M
56+
#endif
57+
58+
#ifndef CONFIG_MEM_RTSMART_SIZE
59+
#define CONFIG_MEM_RTSMART_SIZE 0x10000000 // 256M
60+
#endif
61+
62+
#ifndef CONFIG_MEM_RTSMART_HEAP_SIZE
63+
#define CONFIG_MEM_RTSMART_HEAP_SIZE 0x2000000 // 32M
64+
#endif
65+
66+
#ifndef CONFIG_MEM_MMZ_BASE
67+
#define CONFIG_MEM_MMZ_BASE 0x10000000 // 512M
68+
#endif
69+
70+
#ifndef CONFIG_MEM_MMZ_SIZE
71+
#define CONFIG_MEM_MMZ_SIZE 0x10000000 // 256M
72+
#endif
73+
74+
#define MEM_KERNEL_SIZE (CONFIG_MEM_RTSMART_SIZE - MEM_OPENSBI_SIZE - MEM_GUARD_SIZE)
75+
76+
#endif // MEMORY_LAYOUT_H__

bsp/k230/link.lds

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@
88
* 2020/12/12 bernard The first version
99
*/
1010

11-
#include "rtconfig.h"
11+
#include "board/mem_layout.h"
1212

1313
OUTPUT_ARCH( "riscv" )
1414

1515
/*
1616
* Memory layout:
17-
* 2M ==> +128K: Bootloader(sbi)
18-
* 2M+128K ==> +sizeof(rtthread.bin): Kernel
19-
* ~ ==> 32M: Heap
20-
* 32M - 66M: Page
17+
* See mem_layout.h
2118
*/
2219

2320
MEMORY
2421
{
25-
SRAM : ORIGIN = KERNEL_VADDR_START, LENGTH = 262012K
22+
SRAM : ORIGIN = KERNEL_VADDR_START + MEM_OPENSBI_SIZE, LENGTH = MEM_KERNEL_SIZE
2623
}
2724

2825
ENTRY(_start)

bsp/k230/rtconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
#define ARCH_CPU_64BIT
128128
#define RT_USING_CACHE
129129
#define ARCH_MM_MMU
130-
#define KERNEL_VADDR_START 0xffffffc000020000
130+
#define KERNEL_VADDR_START 0xffffffc000000000
131131
#define ARCH_RISCV
132132
#define ARCH_RISCV_FPU
133133
#define ARCH_RISCV_FPU_D

0 commit comments

Comments
 (0)