Skip to content

Commit c49d27e

Browse files
committed
changed to macros for lpc1788 extra memories
1 parent e763c9d commit c49d27e

5 files changed

Lines changed: 205 additions & 55 deletions

File tree

klib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# set the sources
22
set(SOURCES
33
${CMAKE_CURRENT_SOURCE_DIR}/entry/entry.c
4+
${CMAKE_CURRENT_SOURCE_DIR}/entry/secondary.cpp
45
)
56

67
set(HEADERS_PRIVATE

klib/entry/secondary.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <stdint.h>
2+
3+
#pragma pack(push, 1)
4+
5+
/**
6+
* @brief Struct for memory segments that are loaded after the startup code is run
7+
*
8+
*/
9+
struct data_memory_segment_t {
10+
const uint32_t *rom_start;
11+
uint32_t *start;
12+
uint32_t *end;
13+
};
14+
15+
/**
16+
* @brief Struct for bss memory segments that are cleared after the startup code is run
17+
*
18+
*/
19+
struct bss_memory_segment_t {
20+
uint32_t *start;
21+
uint32_t *end;
22+
};
23+
24+
#pragma pack(pop)
25+
26+
extern "C" {
27+
// multisection data segment symbol
28+
extern const data_memory_segment_t __multisection_data_start;
29+
30+
// multisection bss segment symbol
31+
extern const bss_memory_segment_t __multisection_bss_start;
32+
}
33+
34+
namespace klib::entry {
35+
void secondary_memory_loader() {
36+
// loop over all data segments
37+
for (const data_memory_segment_t *segment = &__multisection_data_start; ; segment++) {
38+
// check if we have reached the end of the segments
39+
if (!segment->start && !segment->end && !segment->rom_start) {
40+
// invalid segment
41+
break;
42+
}
43+
44+
// get the length of the segment
45+
const uint32_t length = ((segment->end - segment->start) + (sizeof(uint32_t) - 1)) / sizeof(uint32_t);
46+
47+
// check if we have any length to copy
48+
if (!length) {
49+
continue;
50+
}
51+
52+
// copy rom to ram
53+
for (uint32_t i = 0; i < length; i++) {
54+
((volatile uint32_t*)segment->start)[i] = segment->rom_start[i];
55+
}
56+
}
57+
58+
// loop over all bss segments
59+
for (const bss_memory_segment_t *segment = &__multisection_bss_start; ; segment++) {
60+
// check if we have reached the end of the segments
61+
if (!segment->start && !segment->end) {
62+
// invalid segment
63+
break;
64+
}
65+
66+
// get the length of the segment
67+
const uint32_t length = ((segment->end - segment->start) + (sizeof(uint32_t) - 1)) / sizeof(uint32_t);
68+
69+
// check if we have any length to copy
70+
if (!length) {
71+
continue;
72+
}
73+
74+
// clear the bss segment
75+
for (uint32_t i = 0; i < length; i++) {
76+
((volatile uint32_t*)segment->start)[i] = 0x00;
77+
}
78+
}
79+
}
80+
}

klib/entry/secondary.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef KLIB_SECONDARY_HPP
2+
#define KLIB_SECONDARY_HPP
3+
4+
#include <cstdint>
5+
6+
namespace klib::entry {
7+
/**
8+
* @brief A secondary memory loader. This loader should be
9+
* called afer the memories are initialized by the user
10+
* code. Constructor(102) is reserved for this function.
11+
*
12+
*/
13+
void secondary_memory_loader();
14+
}
15+
16+
#endif

targets/arm/linkerscript/linkerscript.ld

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,69 @@
7373
KEEP(*(SORT(.name))) \
7474
PROVIDE(COMBINE_STR3(__,name,_end) = .); \
7575
}
76+
77+
/* macro to define a data section. Note: this section is not automaticly loaded.
78+
The secondary loaded needs to be enabled to load this at startup after the
79+
startup code */
80+
#define DATA_SECTION(name, region, storage) \
81+
COMBINE_STR2(.name,_data) : \
82+
{ \
83+
. = ALIGN(4); \
84+
PROVIDE(COMBINE_STR3(__,name,_data_rom_start) = LOADADDR(COMBINE_STR2(.name,_data))); \
85+
PROVIDE(COMBINE_STR3(__,name,_data_start) = .); \
86+
*(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_data))); \
87+
*(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_data.*))); \
88+
. = ALIGN(4); \
89+
PROVIDE(COMBINE_STR3(__,name,_data_end) = .); \
90+
} > region AT > storage
91+
92+
/* data section entry for the multi section table */
93+
#define DATA_SECTION_ENTRY(name) \
94+
LONG(COMBINE_STR3(__,name,_data_rom_start)); \
95+
LONG(COMBINE_STR3(__,name,_data_start)); \
96+
LONG(COMBINE_STR3(__,name,_data_end));
97+
98+
/* end marker for the multi section table */
99+
#define DATA_SECTION_ENTRY_END() \
100+
LONG(0); \
101+
LONG(0); \
102+
LONG(0);
103+
104+
/* multisection table to automaticly load the data into the section location */
105+
#define DATA_MULTISECTION_TABLE(entries) \
106+
ALLIGNED_READONLY_SECTION( \
107+
multisection_data, 4, \
108+
PROVIDE(__multisection_data_start = .); \
109+
entries \
110+
PROVIDE(__multisection_data_end = .); \
111+
)
112+
113+
#define BSS_SECTION(name, region) \
114+
COMBINE_STR2(.name,_bss) (NOLOAD) : \
115+
{ \
116+
. = ALIGN(4); \
117+
PROVIDE(COMBINE_STR3(__,name,_bss_start) = .); \
118+
*(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_bss))); \
119+
*(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_bss.*))); \
120+
. = ALIGN(4); \
121+
PROVIDE(COMBINE_STR3(__,name,_bss_end) = .); \
122+
} > region
123+
124+
/* bss section entry for the multi section table */
125+
#define BSS_SECTION_ENTRY(name) \
126+
LONG(COMBINE_STR3(__,name,_bss_start)); \
127+
LONG(COMBINE_STR3(__,name,_bss_end));
128+
129+
/* end marker for the multi section table */
130+
#define BSS_SECTION_ENTRY_END() \
131+
LONG(0); \
132+
LONG(0);
133+
134+
/* multisection table to automaticly load all the bss sections to zero */
135+
#define BSS_MULTISECTION_TABLE(entries) \
136+
ALLIGNED_READONLY_SECTION( \
137+
multisection_bss, 4, \
138+
PROVIDE(__multisection_bss_start = .); \
139+
entries \
140+
PROVIDE(__multisection_bss_end = .); \
141+
)

targets/chip/lpc1788/linkerscript.ld

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -132,61 +132,48 @@ SECTIONS
132132
} > ram
133133

134134
/* additional ram segment of the lpc1788 */
135-
.ram1 :
136-
{
137-
*(SORT_BY_ALIGNMENT(.ram1))
138-
*(SORT_BY_ALIGNMENT(.ram1.*))
139-
} > ram1
140-
141-
/* static memory, note: not loaded by default. No startup code loads the segments */
142-
.static0 (NOLOAD) :
143-
{
144-
*(SORT_BY_ALIGNMENT(.static0));
145-
*(SORT_BY_ALIGNMENT(.static0.*));
146-
} > static0
147-
148-
.static1 (NOLOAD) :
149-
{
150-
*(SORT_BY_ALIGNMENT(.static1));
151-
*(SORT_BY_ALIGNMENT(.static1.*));
152-
} > static1
153-
154-
.static2 (NOLOAD) :
155-
{
156-
*(SORT_BY_ALIGNMENT(.static2));
157-
*(SORT_BY_ALIGNMENT(.static2.*));
158-
} > static2
159-
160-
.static3 (NOLOAD) :
161-
{
162-
*(SORT_BY_ALIGNMENT(.static3));
163-
*(SORT_BY_ALIGNMENT(.static3.*));
164-
} > static3
165-
166-
/* dynamic memory, note: not loaded by default. No startup code loads the segments */
167-
.dynamic0 (NOLOAD) :
168-
{
169-
*(SORT_BY_ALIGNMENT(.dynamic0));
170-
*(SORT_BY_ALIGNMENT(.dynamic0.*));
171-
} > dynamic0
172-
173-
.dynamic1 (NOLOAD) :
174-
{
175-
*(SORT_BY_ALIGNMENT(.dynamic1));
176-
*(SORT_BY_ALIGNMENT(.dynamic1.*));
177-
} > dynamic1
178-
179-
.dynamic2 (NOLOAD) :
180-
{
181-
*(SORT_BY_ALIGNMENT(.dynamic2));
182-
*(SORT_BY_ALIGNMENT(.dynamic2.*));
183-
} > dynamic2
184-
185-
.dynamic3 (NOLOAD) :
186-
{
187-
*(SORT_BY_ALIGNMENT(.dynamic3));
188-
*(SORT_BY_ALIGNMENT(.dynamic3.*));
189-
} > dynamic3
135+
DATA_SECTION(ram1, ram1, rom)
136+
BSS_SECTION(ram1, ram1)
137+
138+
/* static memory */
139+
DATA_SECTION(static0, static0, rom)
140+
DATA_SECTION(static1, static1, rom)
141+
DATA_SECTION(static2, static2, rom)
142+
DATA_SECTION(static3, static3, rom)
143+
BSS_SECTION(static0, static0)
144+
BSS_SECTION(static1, static1)
145+
BSS_SECTION(static2, static2)
146+
BSS_SECTION(static3, static3)
147+
148+
/* dynamic memory */
149+
DATA_SECTION(dynamic0, dynamic0, rom)
150+
DATA_SECTION(dynamic1, dynamic1, rom)
151+
DATA_SECTION(dynamic2, dynamic2, rom)
152+
DATA_SECTION(dynamic3, dynamic3, rom)
153+
BSS_SECTION(dynamic0, dynamic0)
154+
BSS_SECTION(dynamic1, dynamic1)
155+
BSS_SECTION(dynamic2, dynamic2)
156+
BSS_SECTION(dynamic3, dynamic3)
157+
158+
/* create a table to initialize the data sections on a secondary memory */
159+
DATA_MULTISECTION_TABLE(
160+
DATA_SECTION_ENTRY(ram1)
161+
DATA_SECTION_ENTRY(dynamic0)
162+
DATA_SECTION_ENTRY(dynamic1)
163+
DATA_SECTION_ENTRY(dynamic2)
164+
DATA_SECTION_ENTRY(dynamic3)
165+
DATA_SECTION_ENTRY_END()
166+
) > rom
167+
168+
/* create a table to initialize the bss sections on a secondary memory */
169+
BSS_MULTISECTION_TABLE(
170+
BSS_SECTION_ENTRY(ram1)
171+
BSS_SECTION_ENTRY(dynamic0)
172+
BSS_SECTION_ENTRY(dynamic1)
173+
BSS_SECTION_ENTRY(dynamic2)
174+
BSS_SECTION_ENTRY(dynamic3)
175+
BSS_SECTION_ENTRY_END()
176+
) > rom
190177

191178
/* Heap segment */
192179
.heap (NOLOAD) :

0 commit comments

Comments
 (0)