From c8f6776511616410ff9cbe9e352eb607555ffe77 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Thu, 7 Jan 2021 13:58:46 -0800 Subject: [PATCH] More arduino working. --- ueforth/arduino/arduino.fs | 16 +++++++++- ueforth/arduino/arduino.template.ino | 48 ++++++++++++++++++++++++++-- ueforth/common/boot.fs | 1 + ueforth/common/core.h | 1 + 4 files changed, 62 insertions(+), 4 deletions(-) diff --git a/ueforth/arduino/arduino.fs b/ueforth/arduino/arduino.fs index b544ffb..42f0c4c 100644 --- a/ueforth/arduino/arduino.fs +++ b/ueforth/arduino/arduino.fs @@ -1,6 +1,20 @@ +( Set up Basic I/O ) +: arduino-bye 0 terminate ; +' arduino-bye is bye +: arduino-type ( a n -- ) Serial.write drop ; +' arduino-type is type +: key? ( -- n ) Serial.available ; +: arduino-key ( -- n ) + begin Serial.available until 0 >r rp@ 1 Serial.readBytes drop r> ; +' arduino-key is key + ( Map Arduino / ESP32 things to shorter names. ) : pin ( n n -- ) swap digitalWrite ; : adc ( n -- n ) analogRead ; -: duty ( n n -- ) 255 ledcAnalogWrite ; +: duty ( n n -- ) 255 min 8191 255 */ ledcWrite ; : freq ( n n -- ) 1000 * 13 ledcSetup drop ; : tone ( n n -- ) 1000 * ledcWriteTone drop ; + +( Startup Serial ) +115200 Serial.begin +100 ms diff --git a/ueforth/arduino/arduino.template.ino b/ueforth/arduino/arduino.template.ino index fe73791..3c213d4 100644 --- a/ueforth/arduino/arduino.template.ino +++ b/ueforth/arduino/arduino.template.ino @@ -1,5 +1,9 @@ {{opcodes}} +#include "SPIFFS.h" +#include +#include + #include #include #include @@ -19,6 +23,13 @@ #endif #define PLATFORM_OPCODE_LIST \ + /* Serial */ \ + X("Serial.begin", SERIAL_BEGIN, Serial.begin(tos); DROP) \ + X("Serial.end", SERIAL_END, Serial.end()) \ + X("Serial.available", SERIAL_AVAILABLE, DUP; tos = Serial.available()) \ + X("Serial.readBytes", SERIAL_READ_BYTES, tos = Serial.readBytes((uint8_t *) *sp, tos); --sp) \ + X("Serial.write", SERIAL_WRITE, tos = Serial.write((const uint8_t *) *sp, tos); --sp) \ + /* Pins and PWM */ \ X("pinMode", PIN_MODE, pinMode(*sp, tos); --sp; DROP) \ X("digitalWrite", DIGITAL_WRITE, digitalWrite(*sp, tos); --sp; DROP) \ X("analogRead", ANALOG_READ, tos = (cell_t) analogRead(tos)) \ @@ -33,7 +44,7 @@ tos = (cell_t) (1000000 * ledcWriteTone(*sp, tos / 1000.0)); --sp) \ X("ledcWriteNote", LEDC_WRITE_NOTE, \ tos = (cell_t) (1000000 * ledcWriteNote(sp[-1], (note_t) *sp, tos)); sp -=2) \ - X("MS", MS, mspause(tos); DROP) \ + X("MS", MS, delay(tos); DROP) \ X("TERMINATE", TERMINATE, exit(tos)) \ /* File words */ \ X("R/O", R_O, *++sp = O_RDONLY) \ @@ -60,6 +71,28 @@ tos = (cell_t) lseek(fd, tos, SEEK_SET); tos = tos < 0 ? errno : 0) \ X("FILE-SIZE", FILE_SIZE, struct stat st; w = fstat(tos, &st); \ tos = (cell_t) st.st_size; PUSH w < 0 ? errno : 0) \ + /* WiFi */ \ + X("WiFi.config", WIFI_CONFIG, \ + WiFi.config(ToIP(sp[-1]), ToIP(*sp), ToIP(tos)); sp -= 2; DROP) \ + X("WiFi.begin", WIFI_BEGIN, \ + WiFi.begin((const char *) *sp, (const char *) tos); --sp; DROP) \ + X("WiFi.disconnect", WIFI_DISCONNECT, WiFi.disconnect()) \ + X("WiFi.status", WIFI_STATUS, DUP; tos = WiFi.status()) \ + X("WiFi.macAddress", WIFI_MAC_ADDRESS, WiFi.macAddress((uint8_t *) tos); DROP) \ + X("WiFi.localIP", WIFI_LOCAL_IPS, DUP; tos = FromIP(WiFi.localIP())) \ + /* SPIFFS */ \ + X("SPIFFS.begin", SPIFFS_BEGIN, tos = SPIFFS.begin(tos)) \ + X("SPIFFS.end", SPIFFS_END, SPIFFS.end()) \ + X("SPIFFS.format", SPIFFS_FORMAT, DUP; tos = SPIFFS.format()) \ + X("SPIFFS.totalBytes", SPIFFS_TOTAL_BYTES, DUP; tos = SPIFFS.totalBytes()) \ + X("SPIFFS.usedBytes", SPIFFS_USED_BYTES, DUP; tos = SPIFFS.usedBytes()) \ + /* WebServer */ \ + X("WebServer.new", WEBSERVER_NEW, DUP; tos = (cell_t) new WebServer(tos)) \ + X("WebServer.delete", WEBSERVER_DELETE, delete (WebServer *) tos; DROP) \ + X("WebServer.begin", WEBSERVER_BEGIN, \ + WebServer *ws = (WebServer *) tos; DROP; ws->begin(tos); DROP) \ + X("WebServer.stop", WEBSERVER_STOP, \ + WebServer *ws = (WebServer *) tos; DROP; ws->stop()) \ // TODO: Why doesn't ftruncate exist? // X("RESIZE-FILE", RESIZE_FILE, cell_t fd = tos; DROP; \ @@ -70,8 +103,17 @@ static char filename[PATH_MAX]; {{core}} {{boot}} -static void mspause(cell_t ms) { - vTaskDelay(ms / portTICK_PERIOD_MS); +static IPAddress ToIP(cell_t ip) { + return IPAddress(ip & 0xff, ((ip >> 8) & 0xff), ((ip >> 16) & 0xff), ((ip >> 24) & 0xff)); +} + +static cell_t FromIP(IPAddress ip) { + cell_t ret = 0; + ret = (ret << 8) | ip[3]; + ret = (ret << 8) | ip[2]; + ret = (ret << 8) | ip[1]; + ret = (ret << 8) | ip[0]; + return ret; } void setup() { diff --git a/ueforth/common/boot.fs b/ueforth/common/boot.fs index 11ebd89..514d618 100644 --- a/ueforth/common/boot.fs +++ b/ueforth/common/boot.fs @@ -161,6 +161,7 @@ variable hld else dup here swap >r >r $place r> r> then ; immediate : ." postpone s" state @ if postpone type else type then ; immediate : z" postpone s" state @ if postpone drop else drop then ; immediate +: r" parse-quote state @ if swap aliteral aliteral then ; immediate ( Better Errors ) : notfound ( a n n -- ) diff --git a/ueforth/common/core.h b/ueforth/common/core.h index fddc1e6..bb8d613 100644 --- a/ueforth/common/core.h +++ b/ueforth/common/core.h @@ -119,6 +119,7 @@ static cell_t *evaluate1(cell_t *sp) { static void ueforth(int argc, char *argv[], void *heap, const char *src, cell_t src_len) { + memset(&g_sys, 0, sizeof(g_sys)); g_sys.heap = (cell_t *) heap; register cell_t *sp = g_sys.heap; g_sys.heap += STACK_SIZE; register cell_t *rp = g_sys.heap; g_sys.heap += STACK_SIZE;