2022-01-13 05:50:19 -08:00
|
|
|
/*
|
|
|
|
Copyright 2022 Benjamin Vedder benjamin@vedder.se
|
|
|
|
Copyright 2022 Joel Svensson svenssonjoel@yahoo.se
|
|
|
|
|
|
|
|
This file is part of the VESC firmware.
|
|
|
|
|
|
|
|
The VESC firmware is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
The VESC firmware is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lispif.h"
|
|
|
|
#include "commands.h"
|
|
|
|
#include "terminal.h"
|
|
|
|
#include "flash_helper.h"
|
2022-01-28 18:36:34 -08:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "timeout.h"
|
2022-01-13 05:50:19 -08:00
|
|
|
#include "lispbm.h"
|
|
|
|
|
2022-03-31 14:22:26 -07:00
|
|
|
#define HEAP_SIZE 2048
|
2022-05-11 13:10:42 -07:00
|
|
|
#define LISP_MEM_SIZE LBM_MEMORY_SIZE_14K
|
|
|
|
#define LISP_MEM_BITMAP_SIZE LBM_MEMORY_BITMAP_SIZE_14K
|
2022-02-13 06:18:22 -08:00
|
|
|
#define GC_STACK_SIZE 160
|
|
|
|
#define PRINT_STACK_SIZE 128
|
2022-05-05 10:50:22 -07:00
|
|
|
#define EXTENSION_STORAGE_SIZE 200
|
|
|
|
#define VARIABLE_STORAGE_SIZE 64
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-01-21 01:52:56 -08:00
|
|
|
__attribute__((section(".ram4"))) static lbm_cons_t heap[HEAP_SIZE] __attribute__ ((aligned (8)));
|
2022-01-18 11:31:24 -08:00
|
|
|
static uint32_t memory_array[LISP_MEM_SIZE];
|
2022-05-05 10:50:22 -07:00
|
|
|
__attribute__((section(".ram4"))) static uint32_t bitmap_array[LISP_MEM_BITMAP_SIZE];
|
|
|
|
__attribute__((section(".ram4"))) static uint32_t gc_stack_storage[GC_STACK_SIZE];
|
|
|
|
__attribute__((section(".ram4"))) static uint32_t print_stack_storage[PRINT_STACK_SIZE];
|
|
|
|
__attribute__((section(".ram4"))) static extension_fptr extension_storage[EXTENSION_STORAGE_SIZE];
|
|
|
|
__attribute__((section(".ram4"))) static lbm_value variable_storage[VARIABLE_STORAGE_SIZE];
|
2022-02-13 06:18:22 -08:00
|
|
|
|
2022-01-21 01:52:56 -08:00
|
|
|
static lbm_tokenizer_string_state_t string_tok_state;
|
|
|
|
static lbm_tokenizer_char_stream_t string_tok;
|
2022-01-13 05:50:19 -08:00
|
|
|
|
|
|
|
static thread_t *eval_tp = 0;
|
2022-01-28 18:36:34 -08:00
|
|
|
static THD_FUNCTION(eval_thread, arg);
|
2022-01-13 08:50:27 -08:00
|
|
|
static THD_WORKING_AREA(eval_thread_wa, 2048);
|
2022-01-13 05:50:19 -08:00
|
|
|
static bool lisp_thd_running = false;
|
|
|
|
|
2022-03-01 03:07:27 -08:00
|
|
|
static int repl_cid = -1;
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
// Private functions
|
2022-03-01 04:08:25 -08:00
|
|
|
static bool start_lisp(bool print, bool load_code);
|
2022-01-28 18:36:34 -08:00
|
|
|
static uint32_t timestamp_callback(void);
|
|
|
|
static void sleep_callback(uint32_t us);
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
void lispif_init(void) {
|
|
|
|
// Do not attempt to start lisp after a watchdog reset, in case lisp
|
|
|
|
// was the cause of it.
|
|
|
|
// TODO: Anything else to check?
|
|
|
|
if (!timeout_had_IWDG_reset() && terminal_get_first_fault() != FAULT_CODE_BOOTING_FROM_WATCHDOG_RESET) {
|
2022-03-01 04:08:25 -08:00
|
|
|
start_lisp(false, true);
|
2022-01-28 18:36:34 -08:00
|
|
|
}
|
2022-01-13 05:50:19 -08:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:50:01 -08:00
|
|
|
static void ctx_cb(eval_context_t *ctx, void *arg1, void *arg2) {
|
2022-01-29 12:26:15 -08:00
|
|
|
if (arg2 != NULL) {
|
|
|
|
lbm_print_value((char*)arg2, 40, ctx->r);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arg1 != NULL) {
|
|
|
|
float *res = (float*)arg1;
|
|
|
|
*res = 100.0 * (float)ctx->K.max_sp / 256.0;
|
|
|
|
}
|
2022-01-28 18:36:34 -08:00
|
|
|
}
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-03-01 15:50:01 -08:00
|
|
|
static void print_ctx_info(eval_context_t *ctx, void *arg1, void *arg2) {
|
|
|
|
(void) arg1;
|
|
|
|
(void) arg2;
|
|
|
|
|
|
|
|
char output[128];
|
|
|
|
|
|
|
|
int print_ret = lbm_print_value(output, sizeof(output), ctx->r);
|
|
|
|
|
|
|
|
commands_printf_lisp("--------------------------------");
|
|
|
|
commands_printf_lisp("ContextID: %u", ctx->id);
|
|
|
|
commands_printf_lisp("Stack SP: %u", ctx->K.sp);
|
|
|
|
commands_printf_lisp("Stack SP max: %u", ctx->K.max_sp);
|
|
|
|
if (print_ret) {
|
|
|
|
commands_printf_lisp("Value: %s", output);
|
|
|
|
} else {
|
|
|
|
commands_printf_lisp("Error: %s", output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sym_it(const char *str) {
|
|
|
|
commands_printf_lisp("%s", str);
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
void lispif_process_cmd(unsigned char *data, unsigned int len,
|
|
|
|
void(*reply_func)(unsigned char *data, unsigned int len)) {
|
|
|
|
COMM_PACKET_ID packet_id;
|
|
|
|
|
|
|
|
packet_id = data[0];
|
|
|
|
data++;
|
|
|
|
len--;
|
|
|
|
|
|
|
|
switch (packet_id) {
|
|
|
|
case COMM_LISP_SET_RUNNING: {
|
|
|
|
bool ok = false;
|
|
|
|
bool running = data[0];
|
2022-02-03 08:31:02 -08:00
|
|
|
lispif_disable_all_events();
|
2022-01-28 18:36:34 -08:00
|
|
|
|
|
|
|
if (!running) {
|
|
|
|
int timeout_cnt = 20;
|
|
|
|
lbm_pause_eval();
|
|
|
|
while (lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED && timeout_cnt > 0) {
|
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
timeout_cnt--;
|
|
|
|
}
|
|
|
|
ok = timeout_cnt > 0;
|
|
|
|
} else {
|
2022-03-01 04:08:25 -08:00
|
|
|
ok = start_lisp(true, true);
|
2022-01-13 05:50:19 -08:00
|
|
|
}
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
int32_t ind = 0;
|
|
|
|
uint8_t send_buffer[50];
|
|
|
|
send_buffer[ind++] = packet_id;
|
|
|
|
send_buffer[ind++] = ok;
|
|
|
|
reply_func(send_buffer, ind);
|
|
|
|
} break;
|
|
|
|
|
|
|
|
|
|
|
|
case COMM_LISP_GET_STATS: {
|
|
|
|
|
|
|
|
float cpu_use = 0.0;
|
|
|
|
float heap_use = 0.0;
|
|
|
|
float mem_use = 0.0;
|
|
|
|
float stack_use = 0.0;
|
|
|
|
|
|
|
|
static systime_t time_last = 0;
|
|
|
|
if (eval_tp) {
|
|
|
|
cpu_use = 100.0 * (float)eval_tp->p_time / (float)(chVTGetSystemTimeX() - time_last);
|
|
|
|
time_last = chVTGetSystemTimeX();
|
|
|
|
eval_tp->p_time = 0;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-04-12 03:49:29 -07:00
|
|
|
if (lbm_heap_state.gc_num > 0) {
|
|
|
|
heap_use = 100.0 * (float)(HEAP_SIZE - lbm_heap_state.gc_last_free) / (float)HEAP_SIZE;
|
2022-01-13 05:50:19 -08:00
|
|
|
}
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
mem_use = 100.0 * (float)(lbm_memory_num_words() - lbm_memory_num_free()) / (float)lbm_memory_num_words();
|
|
|
|
lbm_running_iterator(ctx_cb, &stack_use, NULL);
|
|
|
|
|
|
|
|
chMtxLock(&send_buffer_mutex);
|
|
|
|
int32_t ind = 0;
|
|
|
|
|
|
|
|
send_buffer_global[ind++] = packet_id;
|
|
|
|
buffer_append_float16(send_buffer_global, cpu_use, 1e2, &ind);
|
|
|
|
buffer_append_float16(send_buffer_global, heap_use, 1e2, &ind);
|
|
|
|
buffer_append_float16(send_buffer_global, mem_use, 1e2, &ind);
|
|
|
|
buffer_append_float16(send_buffer_global, stack_use, 1e2, &ind);
|
|
|
|
|
2022-01-29 12:26:15 -08:00
|
|
|
char r_buf[40];
|
|
|
|
r_buf[0] = '\0'; lbm_done_iterator(ctx_cb, NULL, r_buf); r_buf[39] = '\0';
|
|
|
|
strcpy((char*)(send_buffer_global + ind), r_buf); ind += strlen(r_buf) + 1;
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
lbm_value curr = *lbm_get_env_ptr();
|
2022-03-25 09:11:43 -07:00
|
|
|
while (lbm_type_of(curr) == LBM_TYPE_CONS) {
|
2022-01-28 18:36:34 -08:00
|
|
|
lbm_value key_val = lbm_car(curr);
|
2022-03-25 09:11:43 -07:00
|
|
|
if (lbm_type_of(lbm_car(key_val)) == LBM_TYPE_SYMBOL && lbm_is_number(lbm_cdr(key_val))) {
|
2022-01-28 18:36:34 -08:00
|
|
|
const char *name = lbm_get_name_by_symbol(lbm_dec_sym(lbm_car(key_val)));
|
|
|
|
strcpy((char*)(send_buffer_global + ind), name);
|
|
|
|
ind += strlen(name) + 1;
|
2022-03-25 09:11:43 -07:00
|
|
|
buffer_append_float32_auto(send_buffer_global, lbm_dec_as_float(lbm_cdr(key_val)), &ind);
|
2022-01-28 18:36:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ind > 300) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
curr = lbm_cdr(curr);
|
|
|
|
}
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-02-16 15:49:11 -08:00
|
|
|
for (int i = 0; i < lbm_get_num_variables(); i ++) {
|
|
|
|
const char *name = lbm_get_variable_name_by_index(i);
|
|
|
|
const lbm_value var = lbm_get_variable_by_index(i);
|
|
|
|
if (lbm_is_number(var) && name) {
|
|
|
|
strcpy((char*)(send_buffer_global + ind), name);
|
|
|
|
ind += strlen(name) + 1;
|
2022-03-25 09:11:43 -07:00
|
|
|
buffer_append_float32_auto(send_buffer_global, lbm_dec_as_float(var), &ind);
|
2022-03-07 09:58:17 -08:00
|
|
|
|
|
|
|
if (ind > 300) {
|
|
|
|
break;
|
|
|
|
}
|
2022-02-16 15:49:11 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
reply_func(send_buffer_global, ind);
|
|
|
|
chMtxUnlock(&send_buffer_mutex);
|
|
|
|
} break;
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-03-01 03:07:27 -08:00
|
|
|
case COMM_LISP_REPL_CMD: {
|
2022-03-01 04:08:25 -08:00
|
|
|
if (!lisp_thd_running) {
|
|
|
|
start_lisp(true, false);
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:07:27 -08:00
|
|
|
if (lisp_thd_running) {
|
2022-03-01 15:50:01 -08:00
|
|
|
char *str = (char*)data;
|
|
|
|
|
|
|
|
if (len <= 1) {
|
2022-03-03 10:38:51 -08:00
|
|
|
commands_printf_lisp(">");
|
2022-03-01 15:50:01 -08:00
|
|
|
} else if (len >= 5 && strncmp(str, ":help", 5) == 0) {
|
|
|
|
commands_printf_lisp("== Special Commands ==");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":help\n"
|
|
|
|
" Print this help text");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":info\n"
|
|
|
|
" Print info about memory usage, allocated arrays and garbage collection");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":env\n"
|
|
|
|
" Print current environment and variables");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":ctxs\n"
|
|
|
|
" Print context (threads) info");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":symbols\n"
|
|
|
|
" Print symbol names");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":reset\n"
|
|
|
|
" Reset LBM");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":pause\n"
|
|
|
|
" Pause LBM");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":continue\n"
|
|
|
|
" Continue running LBM");
|
|
|
|
commands_printf_lisp(
|
2022-04-03 03:40:27 -07:00
|
|
|
":step <num_steps>\n"
|
|
|
|
" Run num_steps LBM steps");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":undef <symbol_name>\n"
|
|
|
|
" Undefine symbol");
|
|
|
|
commands_printf_lisp(
|
|
|
|
":verb\n"
|
|
|
|
" Toggle verbose error messages");
|
2022-03-01 15:50:01 -08:00
|
|
|
commands_printf_lisp(" ");
|
|
|
|
commands_printf_lisp("Anything else will be evaluated as an expression in LBM.");
|
|
|
|
commands_printf_lisp(" ");
|
|
|
|
} else if (len >= 5 && strncmp(str, ":info", 5) == 0) {
|
|
|
|
commands_printf_lisp("--(LISP HEAP)-----------------------------------------------\n");
|
|
|
|
commands_printf_lisp("Heap size: %u Bytes\n", HEAP_SIZE * 8);
|
|
|
|
commands_printf_lisp("Used cons cells: %d\n", HEAP_SIZE - lbm_heap_num_free());
|
|
|
|
commands_printf_lisp("Free cons cells: %d\n", lbm_heap_num_free());
|
2022-04-12 03:49:29 -07:00
|
|
|
commands_printf_lisp("GC counter: %d\n", lbm_heap_state.gc_num);
|
|
|
|
commands_printf_lisp("Recovered: %d\n", lbm_heap_state.gc_recovered);
|
|
|
|
commands_printf_lisp("Recovered arrays: %u\n", lbm_heap_state.gc_recovered_arrays);
|
|
|
|
commands_printf_lisp("Marked: %d\n", lbm_heap_state.gc_marked);
|
2022-03-01 15:50:01 -08:00
|
|
|
commands_printf_lisp("--(Symbol and Array memory)---------------------------------\n");
|
|
|
|
commands_printf_lisp("Memory size: %u Words\n", lbm_memory_num_words());
|
|
|
|
commands_printf_lisp("Memory free: %u Words\n", lbm_memory_num_free());
|
2022-04-12 03:49:29 -07:00
|
|
|
commands_printf_lisp("Allocated arrays: %u\n", lbm_heap_state.num_alloc_arrays);
|
2022-03-01 15:50:01 -08:00
|
|
|
commands_printf_lisp("Symbol table size: %u Bytes\n", lbm_get_symbol_table_size());
|
|
|
|
} else if (strncmp(str, ":env", 4) == 0) {
|
|
|
|
lbm_value curr = *lbm_get_env_ptr();
|
|
|
|
char output[128];
|
|
|
|
|
|
|
|
commands_printf_lisp("Environment:\n");
|
2022-03-25 09:11:43 -07:00
|
|
|
while (lbm_type_of(curr) == LBM_TYPE_CONS) {
|
2022-03-01 15:50:01 -08:00
|
|
|
lbm_print_value(output, sizeof(output), lbm_car(curr));
|
|
|
|
curr = lbm_cdr(curr);
|
|
|
|
commands_printf_lisp(" %s",output);
|
|
|
|
}
|
|
|
|
commands_printf_lisp("Variables:");
|
|
|
|
for (int i = 0; i < lbm_get_num_variables(); i ++) {
|
|
|
|
const char *name = lbm_get_variable_name_by_index(i);
|
|
|
|
lbm_print_value(output,1024, lbm_get_variable_by_index(i));
|
|
|
|
commands_printf_lisp(" %s = %s", name ? name : "error", output);
|
|
|
|
}
|
|
|
|
} else if (strncmp(str, ":ctxs", 5) == 0) {
|
|
|
|
commands_printf_lisp("****** Running contexts ******");
|
|
|
|
lbm_running_iterator(print_ctx_info, NULL, NULL);
|
|
|
|
commands_printf_lisp("****** Blocked contexts ******");
|
|
|
|
lbm_blocked_iterator(print_ctx_info, NULL, NULL);
|
|
|
|
commands_printf_lisp("****** Done contexts ******");
|
|
|
|
lbm_done_iterator(print_ctx_info, NULL, NULL);
|
|
|
|
} else if (strncmp(str, ":symbols", 8) == 0) {
|
|
|
|
lbm_symrepr_name_iterator(sym_it);
|
|
|
|
commands_printf_lisp(" ");
|
|
|
|
} else if (strncmp(str, ":reset", 6) == 0) {
|
|
|
|
commands_printf_lisp(start_lisp(false, flash_helper_code_size(CODE_IND_LISP) > 0) ?
|
|
|
|
"Reset OK\n\n" : "Reset Failed\n\n");
|
|
|
|
} else if (strncmp(str, ":pause", 6) == 0) {
|
|
|
|
lbm_pause_eval_with_gc(30);
|
|
|
|
while(lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
|
|
|
|
sleep_callback(10);
|
|
|
|
}
|
|
|
|
commands_printf_lisp("Evaluator paused\n");
|
|
|
|
} else if (strncmp(str, ":continue", 9) == 0) {
|
|
|
|
lbm_continue_eval();
|
|
|
|
} else if (strncmp(str, ":step", 5) == 0) {
|
|
|
|
int num = atoi(str + 5);
|
|
|
|
lbm_step_n_eval((uint32_t)num);
|
|
|
|
} else if (strncmp(str, ":undef", 6) == 0) {
|
|
|
|
lbm_pause_eval();
|
|
|
|
while(lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
|
|
|
|
sleep_callback(10);
|
|
|
|
}
|
|
|
|
char *sym = str + 7;
|
|
|
|
commands_printf_lisp("undefining: %s", sym);
|
|
|
|
commands_printf_lisp("%s", lbm_undefine(sym) ? "Cleared bindings" : "No definition found");
|
2022-03-01 03:07:27 -08:00
|
|
|
lbm_continue_eval();
|
2022-04-03 03:40:27 -07:00
|
|
|
} else if (strncmp(str, ":verb", 5) == 0) {
|
|
|
|
static bool verbose_now = false;
|
|
|
|
verbose_now = !verbose_now;
|
|
|
|
lbm_set_verbose(verbose_now);
|
|
|
|
commands_printf_lisp("Verbose errors %s", verbose_now ? "Enabled" : "Disabled");
|
2022-03-01 03:07:27 -08:00
|
|
|
} else {
|
2022-03-01 15:50:01 -08:00
|
|
|
bool ok = true;
|
|
|
|
int timeout_cnt = 1000;
|
|
|
|
lbm_pause_eval_with_gc(30);
|
|
|
|
while (lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED && timeout_cnt > 0) {
|
|
|
|
chThdSleep(5);
|
|
|
|
timeout_cnt--;
|
|
|
|
}
|
|
|
|
ok = timeout_cnt > 0;
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
lbm_create_char_stream_from_string(&string_tok_state, &string_tok, (char*)data);
|
|
|
|
repl_cid = lbm_load_and_eval_expression(&string_tok);
|
|
|
|
lbm_continue_eval();
|
|
|
|
lbm_wait_ctx(repl_cid, 500);
|
|
|
|
repl_cid = -1;
|
|
|
|
} else {
|
|
|
|
commands_printf_lisp("Could not pause");
|
|
|
|
}
|
2022-03-01 03:07:27 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
commands_printf_lisp("LispBM is not running");
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
default:
|
|
|
|
break;
|
2022-01-13 05:50:19 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 03:07:27 -08:00
|
|
|
static void done_callback(eval_context_t *ctx) {
|
|
|
|
lbm_cid cid = ctx->id;
|
|
|
|
lbm_value t = ctx->r;
|
|
|
|
|
|
|
|
if (cid == repl_cid) {
|
2022-03-02 10:07:42 -08:00
|
|
|
char output[128];
|
|
|
|
lbm_print_value(output, sizeof(output), t);
|
2022-03-01 03:07:27 -08:00
|
|
|
commands_printf_lisp("> %s", output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-01 04:08:25 -08:00
|
|
|
static bool start_lisp(bool print, bool load_code) {
|
2022-01-28 18:36:34 -08:00
|
|
|
bool res = false;
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-05-11 13:10:42 -07:00
|
|
|
lispif_stop_lib();
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
char *code_data = (char*)flash_helper_code_data(CODE_IND_LISP);
|
|
|
|
int32_t code_len = flash_helper_code_size(CODE_IND_LISP);
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-03-01 04:08:25 -08:00
|
|
|
if (!load_code || (code_data != 0 && code_len > 0)) {
|
2022-01-28 18:36:34 -08:00
|
|
|
if (!lisp_thd_running) {
|
2022-02-13 06:18:22 -08:00
|
|
|
lbm_init(heap, HEAP_SIZE,
|
|
|
|
gc_stack_storage, GC_STACK_SIZE,
|
2022-05-05 10:50:22 -07:00
|
|
|
memory_array, LISP_MEM_SIZE,
|
|
|
|
bitmap_array, LISP_MEM_BITMAP_SIZE,
|
2022-02-16 15:49:11 -08:00
|
|
|
print_stack_storage, PRINT_STACK_SIZE,
|
|
|
|
extension_storage, EXTENSION_STORAGE_SIZE);
|
|
|
|
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
lbm_set_timestamp_us_callback(timestamp_callback);
|
|
|
|
lbm_set_usleep_callback(sleep_callback);
|
2022-02-28 05:29:59 -08:00
|
|
|
lbm_set_printf_callback(commands_printf_lisp);
|
2022-03-01 03:07:27 -08:00
|
|
|
lbm_set_ctx_done_callback(done_callback);
|
2022-04-06 10:49:09 -07:00
|
|
|
chThdCreateStatic(eval_thread_wa, sizeof(eval_thread_wa), NORMALPRIO - 1, eval_thread, NULL);
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
lisp_thd_running = true;
|
|
|
|
} else {
|
|
|
|
lbm_pause_eval();
|
|
|
|
while (lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
|
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
}
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-02-13 06:18:22 -08:00
|
|
|
lbm_init(heap, HEAP_SIZE,
|
|
|
|
gc_stack_storage, GC_STACK_SIZE,
|
2022-05-05 10:50:22 -07:00
|
|
|
memory_array, LISP_MEM_SIZE,
|
|
|
|
bitmap_array, LISP_MEM_BITMAP_SIZE,
|
2022-02-16 15:49:11 -08:00
|
|
|
print_stack_storage, PRINT_STACK_SIZE,
|
|
|
|
extension_storage, EXTENSION_STORAGE_SIZE);
|
|
|
|
lbm_variables_init(variable_storage, VARIABLE_STORAGE_SIZE);
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
lbm_pause_eval();
|
|
|
|
while (lbm_get_eval_state() != EVAL_CPS_STATE_PAUSED) {
|
|
|
|
chThdSleepMilliseconds(100);
|
|
|
|
}
|
2022-01-21 01:52:56 -08:00
|
|
|
}
|
2022-01-28 18:36:34 -08:00
|
|
|
|
|
|
|
lispif_load_vesc_extensions();
|
2022-02-23 13:56:30 -08:00
|
|
|
lbm_set_dynamic_load_callback(lispif_vesc_dynamic_loader);
|
2022-01-28 18:36:34 -08:00
|
|
|
|
2022-03-01 04:08:25 -08:00
|
|
|
if (load_code) {
|
|
|
|
if (print) {
|
|
|
|
commands_printf_lisp("Parsing %d characters", strlen(code_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
lbm_create_char_stream_from_string(&string_tok_state, &string_tok, code_data);
|
|
|
|
lbm_load_and_eval_program(&string_tok);
|
2022-01-29 12:26:15 -08:00
|
|
|
}
|
2022-01-28 18:36:34 -08:00
|
|
|
|
|
|
|
lbm_continue_eval();
|
|
|
|
|
|
|
|
res = true;
|
2022-01-21 01:52:56 -08:00
|
|
|
}
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
return res;
|
|
|
|
}
|
2022-01-13 05:50:19 -08:00
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
static uint32_t timestamp_callback(void) {
|
|
|
|
systime_t t = chVTGetSystemTime();
|
|
|
|
return (uint32_t) ((1000000 / CH_CFG_ST_FREQUENCY) * t);
|
2022-01-13 05:50:19 -08:00
|
|
|
}
|
|
|
|
|
2022-01-28 18:36:34 -08:00
|
|
|
static void sleep_callback(uint32_t us) {
|
|
|
|
chThdSleepMicroseconds(us);
|
|
|
|
}
|
|
|
|
|
|
|
|
static THD_FUNCTION(eval_thread, arg) {
|
|
|
|
(void)arg;
|
|
|
|
eval_tp = chThdGetSelfX();
|
|
|
|
chRegSetThreadName("Lisp Eval");
|
|
|
|
lbm_run_eval();
|
2022-01-13 05:50:19 -08:00
|
|
|
}
|