Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freertos port #4817

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RANLIB ?=ranlib
CABAL :=cabal
# IDRIS_ENABLE_STATS should not be set in final release
# Any flags defined here which alter the RTS API must also be added to src/IRTS/CodegenC.hs
CFLAGS :=-O2 -Wall -std=c99 -pipe -fdata-sections -ffunction-sections -D_POSIX_C_SOURCE=200809L -DHAS_PTHREAD -DIDRIS_ENABLE_STATS $(CFLAGS)
CFLAGS :=-O2 -Wall -std=c99 -pipe -fdata-sections -ffunction-sections -D_POSIX_C_SOURCE=200809L -DIS_THREADED -DHAS_PTHREAD -DIDRIS_ENABLE_STATS $(CFLAGS)

# CABALFLAGS :=
CABALFLAGS += --enable-tests
Expand Down
11 changes: 11 additions & 0 deletions rts/FreeRTOS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Idris RTS on FreeRTOS
The Idris RTS compiled without libc ontop of FreeRTOS tasks, using FreeRTOS queues as IPC. This
enables using Idris when programming microcontrollers and smaller systems, but it is also a step
towards the goal of building an Idris Unikernel.

FreeRTOS was chosen as abstraction layer towards the hardware since it's easy to port to new
architectures, since it's small, and since there already are a lot of port available.

## How to run Idris on FreeRTOS
See the instructions in the repository,
[mokshasoft/FreeRTOS-community-ports](https://github.com/mokshasoft/FreeRTOS-community-ports/tree/master/Demo/ARM926ejs-GCC-Idris).
41 changes: 41 additions & 0 deletions rts/FreeRTOS/idris_FreeRTOS.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2020, Mokshasoft AB (mokshasoft.com)
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include "idris_FreeRTOS.h"
#include "idris_rts.h"

void wrapper_vTaskDelay(int delay_ms)
{
vTaskDelay(delay_ms/portTICK_RATE_MS);
}

QueueHandle_t wrapper_xQueueCreate(UBaseType_t uxQueueLength)
{
return xQueueCreate(uxQueueLength, sizeof(void*));
}

void wrapper_vQueueDelete(QueueHandle_t xQueue)
{
vQueueDelete(xQueue);
}
37 changes: 37 additions & 0 deletions rts/FreeRTOS/idris_FreeRTOS.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020, Mokshasoft AB (mokshasoft.com)
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <FreeRTOS.h>
#include <queue.h>

/*
* Wrappers for FreeRTOS Task Control
*/
void wrapper_vTaskDelay(int delay_ms);

/*
* Wrappers for FreeRTOS Queue
*/
QueueHandle_t wrapper_xQueueCreate(UBaseType_t uxQueueLength);
void wrapper_vQueueDelete(QueueHandle_t xQueue);
79 changes: 79 additions & 0 deletions rts/FreeRTOS/idris_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright 2020, Mokshasoft AB (mokshasoft.com)
*
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

/* The UART part of this code depends on the print driver of the versatilepb, remove or port
* this part of the code if compiling for another platform.
*/

#include "idris_gmp.h"
#include "idris_opts.h"
#include "idris_rts.h"
#include "idris_stats.h"
#include "print.h"
#include <FreeRTOS.h>
#include <task.h>

void _idris__123_runMain_95_0_125_(VM* vm, VAL* oldbase);

RTSOpts opts = {
.init_heap_size = 10000,
.max_stack_size = 5000,
.show_summary = 0
};

static void uartLog(const char* msg) {
vDirectPrintMsg(msg);
}

static void halt() {
for (; ;);
}

void vRootThread(void* pvParameters) {
VM* vm = init_vm(opts.max_stack_size, opts.init_heap_size, 1);
init_gmpalloc();
init_nullaries();
_idris__123_runMain_95_0_125_(vm, NULL);
}

int main() {
// Init print
if (pdFAIL == printInit(0)) {
uartLog("Init print failed\r\n");
halt();
}

// Create root thread
if (pdPASS != xTaskCreate(vRootThread, "root", 2000, NULL, 0, NULL)) {
uartLog("Failed to create root thread\r\n");
halt();
}

// Start the FreeRTOS scheduler
vTaskStartScheduler();

// Will not get here unless there is insufficient RAM
uartLog("will not get here\r\n");
return EXIT_SUCCESS;
}
11 changes: 9 additions & 2 deletions rts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ include ../config.mk

OBJS = idris_rts.o idris_heap.o idris_gc.o idris_gmp.o idris_bitstring.o \
idris_opts.o idris_stats.o idris_utf8.o idris_stdfgn.o \
idris_buffer.o getline.o idris_net.o
idris_buffer.o getline.o idris_net.o itoa.o
HDRS = idris_rts.h idris_heap.h idris_gc.h idris_gmp.h idris_bitstring.h \
idris_opts.h idris_stats.h idris_stdfgn.h idris_net.h \
idris_buffer.h idris_utf8.h getline.h
idris_buffer.h idris_utf8.h getline.h itoa.h
CFLAGS := $(CFLAGS)
CFLAGS += $(GMP_INCLUDE_DIR) $(GMP) -DIDRIS_TARGET_OS="\"$(OS)\""
CFLAGS += -DIDRIS_TARGET_TRIPLE="\"$(MACHINE)\""
Expand All @@ -16,6 +16,13 @@ else
CFLAGS += -fPIC
endif

ifdef BARE_METAL
# Using this flag changes the precision of float casts compared to snprintf.
# e.g. 2345.345 get converted to "2345.344970703125" instead of "234.345"
OBJS += ftoa.o
HDRS += ftoa.h
endif

ifndef IDRIS_GMP
OBJS += mini-gmp.o
HDRS += mini-gmp.h
Expand Down
41 changes: 41 additions & 0 deletions rts/bare-metal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.7.2)

project(idris-rts-bare-metal C)

file(GLOB static
rts/idris_rts.c
rts/idris_heap.c
rts/idris_gc.c
rts/idris_gmp.c
rts/idris_bitstring.c
rts/idris_utf8.c
rts/mini-gmp.c
rts/itoa.c
rts/ftoa.c
rts/bare-metal/idris_main.c
)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W -Wall")
# Ignore some warnings
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-parameter")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-sign-compare")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-variable")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-empty-body")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-maybe-uninitialized")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-but-set-variable")
# Don't use the host machines' environment when cross compiling
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DIDRIS_TARGET_OS=\"\\\"bare-metal\\\"\"")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DBARE_METAL")
execute_process(
COMMAND ${CMAKE_C_COMPILER} -dumpmachine
OUTPUT_VARIABLE MACHINE
OUTPUT_STRIP_TRAILING_WHITESPACE
)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DIDRIS_TARGET_TRIPLE=\"\\\"${MACHINE}\\\"\"")

add_library(idris-rts-bare-metal EXCLUDE_FROM_ALL ${static})
target_include_directories(idris-rts-bare-metal PUBLIC rts)
target_link_libraries(
idris-rts-bare-metal
c
)
5 changes: 5 additions & 0 deletions rts/bare-metal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# idris-rts-bare-metal - Idris RTS bare-metal library
The Idris RTS compiled without libc to allow executing Idris apps on bare-metal.

## How to use this library?
See the instructions in the project repository, [mokshasoft/bare-metal-idris-manifest](https://github.com/mokshasoft/bare-metal-idris-manifest).
31 changes: 31 additions & 0 deletions rts/bare-metal/idris_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "idris_gmp.h"
#include "idris_opts.h"
#include "idris_rts.h"
#include "idris_stats.h"

void _idris__123_runMain_95_0_125_(VM* vm, VAL* oldbase);

RTSOpts opts = {
.init_heap_size = 10000,
.max_stack_size = 5000,
.show_summary = 0
};

int main() {
VM* vm = init_vm(opts.max_stack_size, opts.init_heap_size, 1);
init_gmpalloc();
init_nullaries();

_idris__123_runMain_95_0_125_(vm, NULL);

#ifdef IDRIS_DEBUG
if (opts.show_summary) {
idris_gcInfo(vm, 1);
}
#endif

// Remove call to terminate since it crashes the application during a free
//Stats stats = terminate(vm);

return EXIT_SUCCESS;
}
Loading