Skip to content

Commit cc532b1

Browse files
authored
Extended support for External Memory (#97)
Initial commit to support larger containers Replace custom functions with zephyr ones Use input container as a name for containers Rename base container Cleanup psram support Fixed container naming Signed-off-by: Krisztian Szilvasi <34309983+kr-t@users.noreply.github.com>
1 parent f9e4f4b commit cc532b1

7 files changed

Lines changed: 101 additions & 24 deletions

File tree

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,15 @@ add_custom_target(generate_messages DEPENDS ${MSG_GENERATED_FILE})
5757

5858
if(NOT "${OCRE_INPUT_FILE}" STREQUAL "")
5959
message("Using input file: ${OCRE_INPUT_FILE}")
60+
61+
# Extract filename without extension for use in software
62+
get_filename_component(OCRE_INPUT_FILE_NAME ${OCRE_INPUT_FILE} NAME_WE)
63+
message("Input file name (without extension): ${OCRE_INPUT_FILE_NAME}")
64+
add_definitions(-DOCRE_INPUT_FILE_NAME="${OCRE_INPUT_FILE_NAME}")
65+
6066
add_custom_command(
6167
OUTPUT ${CMAKE_CURRENT_LIST_DIR}/src/ocre/ocre_input_file.g
62-
COMMAND xxd -n wasm_binary -i ${OCRE_INPUT_FILE} > ${CMAKE_CURRENT_LIST_DIR}/src/ocre/ocre_input_file.g
68+
COMMAND xxd -i ${OCRE_INPUT_FILE} | sed 's/unsigned char .*\\[/static const unsigned char wasm_binary[/' | sed 's/unsigned int .*_len/static const unsigned int wasm_binary_len/' > ${CMAKE_CURRENT_LIST_DIR}/src/ocre/ocre_input_file.g
6369
DEPENDS ${OCRE_INPUT_FILE}
6470
COMMENT "Generating C header from ${OCRE_INPUT_FILE}"
6571
)

Kconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ config OCRE_WAMR_HEAP_BUFFER_SIZE
3939
help
4040
A static memory allocation for WAMR to use as a heap.
4141

42+
config OCRE_STORAGE_HEAP_BUFFER_SIZE
43+
int "Storage heap buffer size in bytes"
44+
default 500000
45+
depends on MEMC
46+
help
47+
A static memory allocation for container storage to use as a heap.
48+
4249
config OCRE_CONTAINER_DEFAULT_HEAP_SIZE
4350
int "Default value for the container heap size"
4451
default 4096

boards/b_u585i_iot02a.conf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CONFIG_MEMC=y
88

99
# Container defaults
1010
CONFIG_MAX_CONTAINERS=5
11-
CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=8388608
11+
CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE=6388608
12+
CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE=2000000
1213

1314
# Bus interfaces
1415
CONFIG_GPIO=y

boards/b_u585i_iot02a.overlay

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <zephyr/dt-bindings/pinctrl/stm32-pinctrl.h>
22

33
/ {
4+
chosen {
5+
zephyr,code-partition = &slot0_partition;
6+
};
7+
48
aliases {
59
rng0 = &rng_device;
610
led0 = &green_led_1;
@@ -82,12 +86,37 @@
8286
};
8387
};
8488

85-
/* Optional: flash partitions */
89+
/* Flash partitions - 2MB total, no MCUboot */
8690
&flash0 {
91+
/delete-node/ partitions;
92+
93+
partitions {
94+
compatible = "fixed-partitions";
95+
#address-cells = <1>;
96+
#size-cells = <1>;
97+
98+
/* Application partition - 1900KB */
99+
slot0_partition: partition@0 {
100+
label = "image-0";
101+
reg = <0x00000000 DT_SIZE_K(2000)>;
102+
};
103+
/* Dummy slot1 partition for MCUboot compatibility (unused) */
104+
slot1_partition: partition@1F5000 {
105+
label = "image-1";
106+
reg = <0x001F5000 DT_SIZE_K(44)>;
107+
};
108+
};
109+
};
110+
111+
// 64MB external flash
112+
&mx25lm51245 {
87113
partitions {
88-
user_data_partition: partition@100000 {
114+
/delete-node/ partition;
115+
116+
/* Use the whole flash for the filesystem. */
117+
user_data_partition: storage_partition: partition@0 {
89118
label = "user_data";
90-
reg = <0x00100000 DT_SIZE_K(256)>;
119+
reg = <0x00000000 DT_SIZE_M(64)>;
91120
};
92121
};
93122
};

src/ocre/components/container_supervisor/cs_sm_impl.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,15 @@ LOG_MODULE_DECLARE(ocre_cs_component, OCRE_LOG_LEVEL);
3434
#include "cs_sm.h"
3535
#include "cs_sm_impl.h"
3636

37-
// External RAM support for WAMR heap on boards that have it
37+
#include "ocre_psram.h"
38+
39+
// WAMR heap buffer - uses PSRAM when available
3840
#if defined(CONFIG_MEMC)
39-
#if defined(CONFIG_BOARD_ARDUINO_PORTENTA_H7)
40-
__attribute__((section("SDRAM1"), aligned(32)))
41-
#elif defined(CONFIG_BOARD_B_U585I_IOT02A)
42-
__attribute__((section(".stm32_psram"), aligned(32)))
43-
#elif defined(CONFIG_BOARD_MIMXRT1064_EVK)
44-
__attribute__((section("SDRAM"), aligned(32)))
45-
#endif // defined (<board>)
46-
#endif // defined(CONFIG_MEMC)
41+
PSRAM_SECTION_ATTR
42+
#endif
4743
static char wamr_heap_buf[CONFIG_OCRE_WAMR_HEAP_BUFFER_SIZE] = {0};
4844

45+
4946
// Thread pool for container execution
5047
#define CONTAINER_THREAD_POOL_SIZE 4
5148
static core_thread_t container_threads[CONTAINER_THREAD_POOL_SIZE];
@@ -178,6 +175,7 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument
178175
size_t file_size = 0;
179176
void *file_handle = NULL;
180177
char filepath[FILE_PATH_MAX];
178+
181179

182180
ret = core_construct_filepath(filepath, sizeof(filepath), container_data->sha256);
183181
if (ret < 0) {
@@ -191,9 +189,9 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument
191189
}
192190

193191
container_arguments->size = file_size;
194-
container_arguments->buffer = malloc(file_size);
192+
container_arguments->buffer = storage_heap_alloc(file_size);
195193
if (!container_arguments->buffer) {
196-
LOG_ERR("Failed to allocate memory for container binary.");
194+
LOG_ERR("Failed to allocate memory for container binary from PSRAM.");
197195
return -ENOMEM;
198196
}
199197

@@ -202,22 +200,22 @@ static int load_binary_to_buffer_fs(ocre_runtime_arguments_t *container_argument
202200
ret = core_fileopen(filepath, &file_handle);
203201
if (ret < 0) {
204202
LOG_ERR("Failed to open file %s: %d", filepath, ret);
205-
free(container_arguments->buffer);
203+
storage_heap_free(container_arguments->buffer);
206204
return ret;
207205
}
208206

209207
ret = core_fileread(file_handle, container_arguments->buffer, file_size);
210208
if (ret < 0) {
211209
LOG_ERR("Failed to read file %s: %d", filepath, ret);
212210
core_fileclose(file_handle);
213-
free(container_arguments->buffer);
211+
storage_heap_free(container_arguments->buffer);
214212
return ret;
215213
}
216214

217215
ret = core_fileclose(file_handle);
218216
if (ret < 0) {
219217
LOG_ERR("Failed to close file %s: %d", filepath, ret);
220-
free(container_arguments->buffer);
218+
storage_heap_free(container_arguments->buffer);
221219
return ret;
222220
}
223221
return 0;
@@ -268,6 +266,7 @@ ocre_container_runtime_status_t CS_runtime_init(ocre_cs_ctx *ctx, ocre_container
268266
#ifdef CONFIG_OCRE_CONTAINER_MESSAGING
269267
ocre_messaging_init();
270268
#endif
269+
storage_heap_init();
271270
return RUNTIME_STATUS_INITIALIZED;
272271
}
273272

@@ -322,7 +321,7 @@ ocre_container_status_t CS_create_container(ocre_container_t *container) {
322321
curr_container_arguments->error_buf, sizeof(curr_container_arguments->error_buf));
323322
if (!curr_container_arguments->module) {
324323
LOG_ERR("Failed to load WASM module: %s", curr_container_arguments->error_buf);
325-
free(curr_container_arguments->buffer);
324+
storage_heap_free(curr_container_arguments->buffer);
326325
return CONTAINER_STATUS_ERROR;
327326
}
328327

@@ -381,7 +380,7 @@ ocre_container_status_t CS_run_container(ocre_container_t *container) {
381380
LOG_ERR("Failed to instantiate WASM module: %s, for containerID= %d", curr_container_arguments->error_buf,
382381
curr_container_ID);
383382
wasm_runtime_unload(curr_container_arguments->module);
384-
free(curr_container_arguments->buffer);
383+
storage_heap_free(curr_container_arguments->buffer);
385384
return CONTAINER_STATUS_ERROR;
386385
}
387386
#if defined(CONFIG_OCRE_TIMER) || defined(CONFIG_OCRE_GPIO) || defined(CONFIG_OCRE_SENSORS) || \
@@ -514,7 +513,7 @@ ocre_container_status_t CS_destroy_container(ocre_container_t *container, ocre_c
514513
}
515514

516515
if (container->ocre_runtime_arguments.buffer) {
517-
free(container->ocre_runtime_arguments.buffer);
516+
storage_heap_free(container->ocre_runtime_arguments.buffer);
518517
container->ocre_runtime_arguments.buffer = NULL;
519518
}
520519

src/samples-mini/zephyr/main.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ int ocre_network_init();
2626
int main(int argc, char *argv[]) {
2727
ocre_cs_ctx ctx;
2828
ocre_container_init_arguments_t args;
29-
char *container_filename = "hello";
29+
#ifdef OCRE_INPUT_FILE_NAME
30+
const char *container_filename = OCRE_INPUT_FILE_NAME;
31+
#else
32+
const char *container_filename = "hello-from-ocre";
33+
#endif
3034

3135
#ifdef CONFIG_OCRE_NETWORKING
3236
int net_status = ocre_network_init();
@@ -52,7 +56,7 @@ int main(int argc, char *argv[]) {
5256
int container_ID;
5357

5458
ocre_container_data.heap_size = 0;
55-
snprintf(ocre_container_data.name, sizeof(ocre_container_data.name), "Hello World");
59+
snprintf(ocre_container_data.name, sizeof(ocre_container_data.name), "%s", container_filename);
5660
snprintf(ocre_container_data.sha256, sizeof(ocre_container_data.sha256), "%s", container_filename);
5761
ocre_container_data.timers = 0;
5862
ocre_container_runtime_create_container(&ctx, &ocre_container_data, &container_ID, NULL);

src/shared/platform/ocre_psram.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef OCRE_PSRAM
2+
#define OCRE_PSRAM
3+
4+
// PSRAM configuration - centralized for different platforms
5+
#if defined(CONFIG_MEMC)
6+
// Board-specific PSRAM section attributes
7+
#if defined(CONFIG_BOARD_ARDUINO_PORTENTA_H7)
8+
#define PSRAM_SECTION_ATTR __attribute__((section("SDRAM1"), aligned(32)))
9+
#elif defined(CONFIG_BOARD_B_U585I_IOT02A)
10+
#define PSRAM_SECTION_ATTR __attribute__((section(".stm32_psram"), aligned(32)))
11+
#elif defined(CONFIG_BOARD_MIMXRT1064_EVK)
12+
#define PSRAM_SECTION_ATTR __attribute__((section("SDRAM"), aligned(32)))
13+
#else
14+
#define PSRAM_SECTION_ATTR __attribute__((aligned(32)))
15+
#endif
16+
17+
PSRAM_SECTION_ATTR
18+
static char storage_heap_buf[CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE] = {0};
19+
20+
static struct k_heap storage_heap;
21+
#define storage_heap_init() k_heap_init(&storage_heap, storage_heap_buf, CONFIG_OCRE_STORAGE_HEAP_BUFFER_SIZE)
22+
#define storage_heap_alloc(size) k_heap_alloc(&storage_heap, (size), K_SECONDS(1))
23+
#define storage_heap_free(buffer) k_heap_free(&storage_heap, (void*)buffer)
24+
#else
25+
// No PSRAM - use system malloc
26+
#define storage_heap_init() /* No initialization needed */
27+
#define storage_heap_alloc(size) malloc(size)
28+
#define storage_heap_free(buffer) free(buffer)
29+
#endif
30+
31+
#endif /* OCRE_PSRAM*/

0 commit comments

Comments
 (0)