bytecodealliance / wasm-micro-runtime

WebAssembly Micro Runtime (WAMR)
Apache License 2.0
4.66k stars 576 forks source link

Support FreeRTOS #954

Open LinJian1982 opened 2 years ago

LinJian1982 commented 2 years ago

Does WAMR able to run FreeRTOS now? I see the OS abstraction layer do has the implementation for FreeRTOS, but it doesn't say the FreeRTOS is supported officially.

What else work need to do in order to support FreeRTOS?

wenyongh commented 2 years ago

Hi, FreeRTOS is supported, but as it isn't a complete platform, some APIs must be implemented, like printf and vprintf, it wasn't added to the support list yet.

Here are some guides to add a new platform based on freertos:

  1. add platform folder, suppose the platform name is some_os, then we need to create folder core/shared/platform/some_os

  2. add platform files needed under the platform folder: (1) platform_internal.h, like

    
    #ifndef _PLATFORM_INTERNAL_H
    #define _PLATFORM_INTERNAL_H

include libc header files needed

include

include

include

ifndef BH_PLATFORM_SOME_OS

define BH_PLATFORM_SOME_OS

endif

typedef TaskHandle_t korp_thread; typedef korp_thread korp_tid; typedef struct { bool is_recursive; SemaphoreHandle_t sem; } korp_mutex;

struct os_thread_wait_node; typedef struct os_thread_wait_node *os_thread_wait_list; typedef struct korp_cond { SemaphoreHandle_t wait_list_lock; os_thread_wait_list thread_wait_list; } korp_cond;

int os_printf(const char format, ...); int os_vprintf(const char format, va_list ap);


2. some_os_platform.c
```C
#include "platform_api_vmcore.h"
#include "platform_api_extension.h"

int
os_thread_sys_init();

void
os_thread_sys_destroy();

int
bh_platform_init()
{
    return os_thread_sys_init();
}

void
bh_platform_destroy()
{
    os_thread_sys_destroy();
}

int
os_printf(const char *format, ...)
{
    implement os_printf
}

int
os_vprintf(const char *format, va_list ap)
{
    implement os_vprintf
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
{
    implement if needed
}

void
os_munmap(void *addr, size_t size)
{
    implement if needed
}

int
os_mprotect(void *addr, size_t size, int prot)
{
    implement if needed
}

void
os_dcache_flush()
{
    implement if needed
}
  1. some_os_thread.s
    
    #include "platform_api_vmcore.h"
    #include "platform_api_extension.h"

uint8 * os_thread_get_stack_boundary() { implement if needed }

4. shared_platform.cmake
```cmake
set (PLATFORM_SHARED_DIR ${CMAKE_CURRENT_LIST_DIR})

add_definitions(-DBH_PLATFORM_SOME_OS)

include_directories(${PLATFORM_SHARED_DIR})
include_directories(${PLATFORM_SHARED_DIR}/../include)

include (${CMAKE_CURRENT_LIST_DIR}/../common/posix/platform_api_freertos.cmake)
include (${CMAKE_CURRENT_LIST_DIR}/../common/math/platform_api_math.cmake)

file (GLOB_RECURSE source_all ${PLATFORM_SHARED_DIR}/*.c)

set (PLATFORM_SHARED_SOURCE ${source_all}
                            ${PLATFORM_COMMON_MATH_SOURCE}
                            ${PLATFORM_COMMON_FREERTOS_SOURCE})

After implementing these files, you should be able to implement the product-mini files under product-mini/platform/some_os/ like other platform. Thanks.