From bb2b137e2cb25ba604c75d54cbd55d2ce65b5211 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Sat, 26 Nov 2022 23:27:14 -0500 Subject: [PATCH] WIP: Functional pico REPL (broke native) --- include/pico/config_autogen.h | 2 + include/tusb_config.h | 18 +++++++ platformio.ini | 13 +++++- src/main.c | 88 +++++++++++++++++++++++++++-------- src/native-plat.c | 7 +++ src/native-plat.h | 4 ++ src/pico-plat.c | 18 +++++++ src/pico-plat.h | 7 +++ src/plat.h | 15 ++++++ 9 files changed, 150 insertions(+), 22 deletions(-) create mode 100644 include/pico/config_autogen.h create mode 100644 include/tusb_config.h create mode 100644 src/native-plat.c create mode 100644 src/native-plat.h create mode 100644 src/pico-plat.c create mode 100644 src/pico-plat.h create mode 100644 src/plat.h diff --git a/include/pico/config_autogen.h b/include/pico/config_autogen.h new file mode 100644 index 0000000..157ef8a --- /dev/null +++ b/include/pico/config_autogen.h @@ -0,0 +1,2 @@ +/* SELECT OTHER BOARD */ +#include "boards/pico.h" diff --git a/include/tusb_config.h b/include/tusb_config.h new file mode 100644 index 0000000..53453b4 --- /dev/null +++ b/include/tusb_config.h @@ -0,0 +1,18 @@ +/* + default config for printf + */ + +#ifndef _PICO_STDIO_USB_TUSB_CONFIG_H +#define _PICO_STDIO_USB_TUSB_CONFIG_H + +#include "pico/stdio_usb.h" + +#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE) + +#define CFG_TUD_CDC (1) +#define CFG_TUD_CDC_RX_BUFSIZE (256) +#define CFG_TUD_CDC_TX_BUFSIZE (256) + +// We use a vendor specific interface but with our own driver +#define CFG_TUD_VENDOR (0) +#endif diff --git a/platformio.ini b/platformio.ini index 02998b5..7470bc4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,7 +25,8 @@ release_build_flags = debug_build_flags = -ggdb -O0 - +src_filter = + + [env:pico-debug] platform = https://github.com/Wiz-IO/wizio-pico.git @@ -37,6 +38,9 @@ build_flags = -D PLATFORM_PICO_SDK -D LIB_PICO_STDIO_USB -D PICO_USB +build_src_filter = + ${common.src_filter} + + upload_protocol = uf2 upload_port = /run/media/max/RPI-RP2 @@ -48,6 +52,9 @@ build_flags = ${common.build_flags} ${common.debug_build_flags} -D PLATFORM_NATIVE +build_src_filter = + ${common.src_filter} + + [env:native-release] platform = native @@ -55,7 +62,9 @@ build_flags = ${common.build_flags} ${common.release_build_flags} -D PLATFORM_NATIVE - +build_src_filter = + ${common.src_filter} + + ;monitor_port = SELECT SERIAL PORT ;monitor_speed = 115200 diff --git a/src/main.c b/src/main.c index cd6b649..1a92bae 100644 --- a/src/main.c +++ b/src/main.c @@ -3,11 +3,13 @@ #include "uclisp.h" #include "types.h" +#include "plat.h" -#ifdef PLATFORM_PICO_SDK +// TODO:remove #include "pico/stdio.h" #include "pico/stdlib.h" -#endif + +#define UNUSED __attribute__((unused)) struct ucl *ucl; @@ -15,30 +17,48 @@ struct ucl *ucl; struct ucl_object *ucl_car(struct ucl*, struct ucl_object *); void ucl_print_obj(struct ucl_object *); +// TODO: remove +char buffer[1024] = {0}; + +void pico_getline() { + int chr; + char *end = &buffer[0]; + bool done = false; + while (!done) { + while((chr = getchar_timeout_us(0)) != PICO_ERROR_TIMEOUT) + { + if (chr == '\n' || chr == '\r') { + done = true; + break; + } + + chr = chr & 0xFF; + *end = chr; + end++; + + printf("%c", chr); + fflush(stdout); + } + } + *end = '\0'; + printf("\n"); + fflush(stdout); +} + int main() { -#ifdef PLATFORM_PICO_SDK - stdio_usb_init(); - gpio_init(PICO_DEFAULT_LED_PIN); - gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); - bool gpio_state = false; -#endif - + platform_init(); ucl = ucl_create(); while(1) { -#ifdef PLATFORM_PICO_SDK - gpio_put(PICO_DEFAULT_LED_PIN, gpio_state); - gpio_state = !gpio_state; -#endif + platform_repl_complete(); - // TODO: Work out input for pico - size_t line_len = 512; - char *line = NULL; printf("> "); - getline(&line, &line_len, stdin); + fflush(stdout); - struct ucl_object *sexp = ucl_parse(ucl, line); + pico_getline(); + + struct ucl_object *sexp = ucl_parse(ucl, buffer); struct ucl_object *result = NULL; if (sexp->type == UCL_TYPE_ERROR) { result = sexp; @@ -46,10 +66,38 @@ int main() { result = ucl_evaluate(ucl, ucl_car(ucl, sexp)); } ucl_print_obj(result); - printf("\n"); + ucl_gc(ucl); - free(line); + printf("\n"); } return 0; } + +/* int main() { */ + +/* platform_init(); */ +/* ucl = ucl_create(); */ + +/* while(1) { */ +/* platform_repl_complete(); */ + +/* size_t line_len = 512; */ +/* char *line = NULL; */ +/* printf("> "); */ +/* getline(&line, &line_len, stdin); */ + +/* struct ucl_object *sexp = ucl_parse(ucl, line); */ +/* struct ucl_object *result = NULL; */ +/* if (sexp->type == UCL_TYPE_ERROR) { */ +/* result = sexp; */ +/* } else { */ +/* result = ucl_evaluate(ucl, ucl_car(ucl, sexp)); */ +/* } */ +/* ucl_print_obj(result); */ +/* printf("\n"); */ +/* ucl_gc(ucl); */ +/* } */ + +/* return 0; */ +/* } */ diff --git a/src/native-plat.c b/src/native-plat.c new file mode 100644 index 0000000..232a7d1 --- /dev/null +++ b/src/native-plat.c @@ -0,0 +1,7 @@ +#include "plat.h" + +void platform_init() { +} + +void platform_repl_complete() { +} diff --git a/src/native-plat.h b/src/native-plat.h new file mode 100644 index 0000000..de2bb70 --- /dev/null +++ b/src/native-plat.h @@ -0,0 +1,4 @@ +#ifndef _NATIVE_ENV_H_ +#define _NATIVE_ENV_H_ + +#endif diff --git a/src/pico-plat.c b/src/pico-plat.c new file mode 100644 index 0000000..3275d96 --- /dev/null +++ b/src/pico-plat.c @@ -0,0 +1,18 @@ +#include "plat.h" + +#include +#include "pico/stdio.h" +#include "pico/stdlib.h" + +static bool gpio_state = false; + +void platform_init() { + stdio_init_all(); + gpio_init(PICO_DEFAULT_LED_PIN); + gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); +} + +void platform_repl_complete() { + gpio_put(PICO_DEFAULT_LED_PIN, gpio_state); + gpio_state = !gpio_state; +} diff --git a/src/pico-plat.h b/src/pico-plat.h new file mode 100644 index 0000000..021ccf7 --- /dev/null +++ b/src/pico-plat.h @@ -0,0 +1,7 @@ +#ifndef _PICO_PLAT_H_ +#define _PICO_PLAT_H_ + +// Not sure why this isn't defined on Wizio Pico-SDK (is it defined in the standard SDK?) +#define getline __getline + +#endif diff --git a/src/plat.h b/src/plat.h new file mode 100644 index 0000000..78ae69b --- /dev/null +++ b/src/plat.h @@ -0,0 +1,15 @@ +#ifndef _PLAT_H_ +#define _PLAT_H_ + +#ifdef PLATFORM_PICO_SDK +#include "pico-plat.h" +#endif + +#ifdef PLATFORM_NATIVE +#include "native-plat.h" +#endif + +void platform_init(); +void platform_repl_complete(); + +#endif