LPCXpresso55S69_MRuby/src/main.c
Yilin Sun 861159b916
Added modified version of LineNoise, fixed _read.
Signed-off-by: Yilin Sun <imi415@imi.moe>
2023-03-07 09:37:00 +08:00

118 lines
2.6 KiB
C

/* Board */
#include "board.h"
#include "clock_config.h"
#include "peripherals.h"
#include "pin_mux.h"
/* mRuby */
#include "mruby.h"
#include "mruby/compile.h"
/* FreeRTOS */
#include "FreeRTOS.h"
#include "task.h"
/* LineNoise */
#include "linenoise.h"
/* App */
#include "app_syscalls.h"
#ifndef APP_LOG_SUCCESSFUL_ALLOC
#define APP_LOG_SUCCESSFUL_ALLOC false
#endif
static void app_mrb_runtime_task(void *parameters);
int main(void) {
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
if (app_syscalls_init() != 0) {
goto dead_loop;
}
if (xTaskCreate(app_mrb_runtime_task, "MRB_RT", 1536, NULL, 2, NULL) != pdPASS) {
goto dead_loop;
}
vTaskStartScheduler();
dead_loop:
for (;;) {
__WFI();
}
}
static void app_mrb_alloc_fail_hook(void) {
for (;;) {
__WFI();
}
}
static void *app_mrb_allocf(mrb_state *mrb, void *ptr, size_t len, void *user) {
void *res = realloc(ptr, len);
if (ptr == NULL) {
if (res == NULL) {
printf("[ALLOC] malloc() failed, size: %d\n", len);
app_mrb_alloc_fail_hook();
} else {
#if APP_LOG_SUCCESSFUL_ALLOC
printf("[ALLOC] malloced %d bytes to ptr %p\n", len, res);
#endif
}
} else {
if (len == 0) {
#if APP_LOG_SUCCESSFUL_ALLOC
printf("[ALLOC] freed ptr %p\n", ptr);
#endif
} else {
if (res == NULL) {
printf("[ALLOC] malloc-copy-free failed, orig ptr: %p, new size: %d\n", ptr, len);
app_mrb_alloc_fail_hook();
} else {
#if APP_LOG_SUCCESSFUL_ALLOC
printf("[ALLOC] malloc-copy-free done, orig ptr: %p, new size: %d, new ptr: %p\n", ptr, len, res);
#endif
}
}
}
return res;
}
static void app_mrb_runtime_task(void *parameters) {
mrb_state *mrb = mrb_open_allocf(app_mrb_allocf, NULL);
mrbc_context *cxt = mrbc_context_new(mrb);
mrb_show_version(mrb);
mrb_show_copyright(mrb);
char *line;
while ((line = linenoise("hello> ")) != NULL) {
linenoiseHistoryAdd(line);
printf("You entered: %s\n", line);
free(line);
}
char mrb_code[] = "name = 'LPCXpresso55S69'\nputs \"Greetings #{name}, from MRuby #{RUBY_VERSION}.\"";
struct mrb_parser_state *parser_state = mrb_parse_string(mrb, mrb_code, cxt);
mrb_load_exec(mrb, parser_state, cxt);
for (;;) {
/* Wait for REPL commands */
vTaskDelay(pdMS_TO_TICKS(500));
}
mrbc_cleanup_local_variables(mrb, cxt);
mrbc_context_free(mrb, cxt);
mrb_close(mrb);
vTaskDelete(NULL);
}