diff --git a/Makefile b/Makefile index a48699c..6c8597b 100644 --- a/Makefile +++ b/Makefile @@ -343,6 +343,11 @@ $(GEN)/esp32_spi-flash.h: \ $< spi_flash_source $(VERSION) $(REVISION) \ esp32/optional/spi-flash/spi-flash.fs >$@ +$(GEN)/esp32_espnow.h: \ + tools/source_to_string.js esp32/optional/espnow/espnow.fs | $(GEN) + $< espnow_source $(VERSION) $(REVISION) \ + esp32/optional/espnow/espnow.fs >$@ + $(GEN)/esp32_serial-bluetooth.h: \ tools/source_to_string.js \ esp32/optional/serial-bluetooth/bterm.fs \ @@ -358,7 +363,8 @@ OPTIONAL_MODULES = \ $(ESP32)/ESP32forth/interrupts.h \ $(ESP32)/ESP32forth/rmt.h \ $(ESP32)/ESP32forth/serial-bluetooth.h \ - $(ESP32)/ESP32forth/spi-flash.h + $(ESP32)/ESP32forth/spi-flash.h \ + $(ESP32)/ESP32forth/espnow.h add-optional: $(OPTIONAL_MODULES) @@ -702,6 +708,15 @@ $(ESP32)/ESP32forth/optional/spi-flash.h: \ spi_flash=@$(GEN)/esp32_spi-flash.h \ >$@ +$(ESP32)/ESP32forth/optional/espnow.h: \ + esp32/optional/espnow/espnow.h \ + $(GEN)/esp32_espnow.h | $(ESP32)/ESP32forth/optional + cat esp32/optional/espnow/espnow.h | tools/replace.js \ + VERSION=$(VERSION) \ + REVISION=$(REVISION) \ + espnow=@$(GEN)/esp32_espnow.h \ + >$@ + # ---- ESP32 ARDUINO BUILD AND FLASH ---- ARDUINO_BUILDER="/mnt/c/Program Files (x86)/Arduino/arduino-builder.exe" @@ -805,7 +820,8 @@ $(ESP32)/ESP32forth.zip: \ $(ESP32)/ESP32forth/optional/interrupts.h \ $(ESP32)/ESP32forth/optional/rmt.h \ $(ESP32)/ESP32forth/optional/serial-bluetooth.h \ - $(ESP32)/ESP32forth/optional/spi-flash.h + $(ESP32)/ESP32forth/optional/spi-flash.h \ + $(ESP32)/ESP32forth/optional/espnow.h cd $(ESP32) && rm -f ESP32forth.zip && zip -r ESP32forth.zip ESP32forth # ---- Publish to Archive ---- diff --git a/esp32/builtins.h b/esp32/builtins.h index 74b37a6..663f2e7 100644 --- a/esp32/builtins.h +++ b/esp32/builtins.h @@ -84,6 +84,14 @@ # define OPTIONAL_SPI_FLASH_SUPPORT # endif +// Hook to pull in optional ESPNOW support. +# if __has_include("espnow.h") +# include "espnow.h" +# else +# define OPTIONAL_ESPNOW_VOCABULARY +# define OPTIONAL_ESPNOW_SUPPORT +# endif + static cell_t ResizeFile(cell_t fd, cell_t size); #endif @@ -119,7 +127,8 @@ static cell_t ResizeFile(cell_t fd, cell_t size); OPTIONAL_OLED_SUPPORT \ OPTIONAL_RMT_SUPPORT \ OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \ - OPTIONAL_SPI_FLASH_SUPPORT + OPTIONAL_SPI_FLASH_SUPPORT \ + OPTIONAL_ESPNOW_SUPPORT #define REQUIRED_MEMORY_SUPPORT \ YV(internals, MALLOC, SET malloc(n0)) \ diff --git a/esp32/optional/README-optional.txt b/esp32/optional/README-optional.txt index b8fee01..00c2962 100644 --- a/esp32/optional/README-optional.txt +++ b/esp32/optional/README-optional.txt @@ -16,6 +16,7 @@ These are the current optional modules: * serial-bluetooth.h - Support for Bluetooth serial and bterm a Bluetooth serial redirector for the terminal * spi-flash.h - Support for low level SPI Flash partition access + * espnow.h - Support for ESP NOW Initially ESP32forth focused on a minimal C kernel, with most functionality built in Forth code loaded at boot. Eventually, as support for more capabilities diff --git a/esp32/optional/espnow/espnow.fs b/esp32/optional/espnow/espnow.fs new file mode 100644 index 0000000..8449699 --- /dev/null +++ b/esp32/optional/espnow/espnow.fs @@ -0,0 +1,40 @@ +\ Copyright 2023 Daniel Nagy +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +vocabulary espnow espnow definitions +transfer espnow-builtins +DEFINED? ESPNOW_init [IF] +\ error codes from https://github.com/espressif/esp-idf/blob/master/components/esp_common/include/esp_err.h +0 constant ESP_OK +-1 constant ESP_FAIL +$101 constant ESP_ERR_NO_MEM \ Out of memory +$102 constant ESP_ERR_INVALID_ARG \ Invalid argument +$103 constant ESP_ERR_INVALID_STATE \ Invalid state +$104 constant ESP_ERR_INVALID_SIZE \ Invalid size +$105 constant ESP_ERR_NOT_FOUND \ Requested resource not found +$106 constant ESP_ERR_NOT_SUPPORTED \ Operation or feature not supported +$107 constant ESP_ERR_TIMEOUT \ Operation timed out +$108 constant ESP_ERR_INVALID_RESPONSE \ Received response was invalid +$109 constant ESP_ERR_INVALID_CRC \ CRC or checksum was invalid +$10A constant ESP_ERR_INVALID_VERSION \ Version was invalid +$10B constant ESP_ERR_INVALID_MAC \ MAC address was invalid +$10C constant ESP_ERR_NOT_FINISHED \ Operation has not fully completed + +$3000 constant ESP_ERR_WIFI_BASE \ Starting number of WiFi error codes +$3000 constant ESP_ERR_MESH_BASE \ Starting number of MESH error codes +$6000 constant ESP_ERR_FLASH_BASE \ Starting number of flash error codes +$c000 constant ESP_ERR_HW_CRYPTO_BASE \ Starting number of HW cryptography module error codes +$d000 constant ESP_ERR_MEMPROT_BASE \ Starting number of Memory Protection API error codes +[THEN] +forth definitions diff --git a/esp32/optional/espnow/espnow.h b/esp32/optional/espnow/espnow.h new file mode 100644 index 0000000..da6535e --- /dev/null +++ b/esp32/optional/espnow/espnow.h @@ -0,0 +1,43 @@ +// Copyright 2023 Daniel Nagy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * ESP32forth ESPNOW v{{VERSION}} + * Revision: {{REVISION}} + */ + +// More documentation +// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html + +#include + +static esp_err_t ESPNOW_add_peer (const uint8_t *broadcastAddress) { + esp_now_peer_info_t peerInfo = {0}; + memcpy(peerInfo.peer_addr, broadcastAddress, 6); + peerInfo.channel = 0; // use current wifi channel + peerInfo.encrypt = false; + return esp_now_add_peer(&peerInfo); +} + +#define OPTIONAL_ESPNOW_VOCABULARY V(espnow) +#define OPTIONAL_ESPNOW_SUPPORT \ + XV(internals, "espnow-source", ESPNOW_SOURCE, \ + PUSH espnow_source; PUSH sizeof(espnow_source) - 1) \ + YV(espnow, espnow_init , PUSH esp_now_init()) \ + YV(espnow, espnow_deinit , PUSH esp_now_deinit()) \ + YV(espnow, espnow_add_peer, n0 = ESPNOW_add_peer(b0)) \ + YV(espnow, espnow_send , n0 = esp_now_send(b2, b1, n0); NIPn(2)) \ + + +{{espnow}} diff --git a/esp32/optionals.fs b/esp32/optionals.fs index 9f5d7df..583985b 100644 --- a/esp32/optionals.fs +++ b/esp32/optionals.fs @@ -47,3 +47,7 @@ internals DEFINED? serial-bluetooth-source [IF] internals DEFINED? spi-flash-source [IF] spi-flash-source evaluate [THEN] forth + +internals DEFINED? espnow-source [IF] + espnow-source evaluate +[THEN] forth diff --git a/esp32/options.h b/esp32/options.h index 6250c6e..97074a8 100644 --- a/esp32/options.h +++ b/esp32/options.h @@ -95,4 +95,5 @@ OPTIONAL_OLED_VOCABULARY \ OPTIONAL_RMT_VOCABULARY \ OPTIONAL_SPI_FLASH_VOCABULARY \ + OPTIONAL_ESPNOW_VOCABULARY \ USER_VOCABULARIES diff --git a/esp32/print-builtins.cpp b/esp32/print-builtins.cpp index c5e1037..b8cb522 100644 --- a/esp32/print-builtins.cpp +++ b/esp32/print-builtins.cpp @@ -31,6 +31,7 @@ #define OPTIONAL_RMT_SUPPORT #define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT #define OPTIONAL_SPI_FLASH_SUPPORT +#define OPTIONAL_ESPNOW_SUPPORT #define OPTIONAL_BLUETOOTH_VOCABULARY #define OPTIONAL_CAMERA_VOCABULARY @@ -38,6 +39,7 @@ #define OPTIONAL_OLED_VOCABULARY #define OPTIONAL_RMT_VOCABULARY #define OPTIONAL_SPI_FLASH_VOCABULARY +#define OPTIONAL_ESPNOW_VOCABULARY #include "builtins.h" diff --git a/esp32/sim_main.cpp b/esp32/sim_main.cpp index 7ca335e..4eafee3 100644 --- a/esp32/sim_main.cpp +++ b/esp32/sim_main.cpp @@ -28,6 +28,7 @@ #define OPTIONAL_OLED_VOCABULARY #define OPTIONAL_RMT_VOCABULARY #define OPTIONAL_SPI_FLASH_VOCABULARY +#define OPTIONAL_ESPNOW_VOCABULARY static cell_t *simulated(cell_t *sp, const char *op); diff --git a/examples/espnow.fs b/examples/espnow.fs new file mode 100644 index 0000000..166173b --- /dev/null +++ b/examples/espnow.fs @@ -0,0 +1,37 @@ +#! /usr/bin/env ueforth + +\ Copyright 2023 Daniel Nagy +\ +\ Licensed under the Apache License, Version 2.0 (the "License"); +\ you may not use this file except in compliance with the License. +\ You may obtain a copy of the License at +\ +\ http://www.apache.org/licenses/LICENSE-2.0 +\ +\ Unless required by applicable law or agreed to in writing, software +\ distributed under the License is distributed on an "AS IS" BASIS, +\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +\ See the License for the specific language governing permissions and +\ limitations under the License. + +also WiFi +also espnow + +\ start wifi +WIFI_MODE_STA WiFi.mode +\ initialize espnow and check result +espnow_init ESP_OK <> throw + +: espnow_register_example + \ register the mac 12:34:56:78:9a:bc as a peer + $12 c, $34 c, $56 c, $78 c, $9a c, $bc c, + here 6 - + ESPNOW_add_peer ESP_OK <> throw + -6 allot +; +: espnow_send_some + \ NULL a.k.a. send to peerlist + \ send 10 bytes of data space pointer + 0 here 10 - 10 + espnow_send ESP_OK <> throw +; diff --git a/site/ESP32forth.html b/site/ESP32forth.html index b973183..b79415c 100644 --- a/site/ESP32forth.html +++ b/site/ESP32forth.html @@ -1055,6 +1055,36 @@
Timers
TIMGn_Tx_INT_CLR_REG ( n -- a ) +
ESPNOW
+These words are inside the espnow vocabulary. +

+ NOTE: The optional module espnow.h must be + placed next to ESP32forth.ino to include this capability. +

+
+
+espnow_init ( -- err )
+espnow_deinit ( -- err )
+espnow_add_peer ( a -- err )
+espnow_send ( b2 b1 n0 -- err )
+
+( Constants )
+ESP_OK
+ESP_FAIL
+ESP_ERR_NO_MEM              \ Out of memory
+ESP_ERR_INVALID_ARG         \ Invalid argument
+ESP_ERR_INVALID_STATE       \ Invalid state
+ESP_ERR_INVALID_SIZE        \ Invalid size
+ESP_ERR_NOT_FOUND           \ Requested resource not found
+ESP_ERR_NOT_SUPPORTED       \ Operation or feature not supported
+ESP_ERR_TIMEOUT             \ Operation timed out
+ESP_ERR_INVALID_RESPONSE    \ Received response was invalid
+ESP_ERR_INVALID_CRC         \ CRC or checksum was invalid
+ESP_ERR_INVALID_VERSION     \ Version was invalid
+ESP_ERR_INVALID_MAC         \ MAC address was invalid
+ESP_ERR_NOT_FINISHED        \ Operation has not fully completed
+
+

Adding Words