Skip to content

Commit

Permalink
Add ESP-NOW optional module
Browse files Browse the repository at this point in the history
This introduces basic support for ESP-NOW, which is a connectionless Wi-Fi
communication protocol by Espressif.

More information https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html
  • Loading branch information
nagy committed Sep 26, 2023
1 parent 564a8fc commit b726ffc
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 3 deletions.
20 changes: 18 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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)

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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 ----
Expand Down
11 changes: 10 additions & 1 deletion esp32/builtins.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) \
Expand Down
1 change: 1 addition & 0 deletions esp32/optional/README-optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions esp32/optional/espnow/espnow.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
\ 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]
base @ hex
\ 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
base !
[THEN]
forth definitions
43 changes: 43 additions & 0 deletions esp32/optional/espnow/espnow.h
Original file line number Diff line number Diff line change
@@ -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 <esp_now.h>

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}}
4 changes: 4 additions & 0 deletions esp32/optionals.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions esp32/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@
OPTIONAL_OLED_VOCABULARY \
OPTIONAL_RMT_VOCABULARY \
OPTIONAL_SPI_FLASH_VOCABULARY \
OPTIONAL_ESPNOW_VOCABULARY \
USER_VOCABULARIES
2 changes: 2 additions & 0 deletions esp32/print-builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
#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
#define OPTIONAL_INTERRUPTS_VOCABULARIES
#define OPTIONAL_OLED_VOCABULARY
#define OPTIONAL_RMT_VOCABULARY
#define OPTIONAL_SPI_FLASH_VOCABULARY
#define OPTIONAL_ESPNOW_VOCABULARY

#include "builtins.h"

Expand Down
1 change: 1 addition & 0 deletions esp32/sim_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
39 changes: 39 additions & 0 deletions examples/espnow.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#! /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

base @ hex
: 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
;
base !
30 changes: 30 additions & 0 deletions site/ESP32forth.html
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,36 @@ <h5 id="timers">Timers</h5>
TIMGn_Tx_INT_CLR_REG ( n -- a )
</pre>

<h5 id="espnow">ESPNOW</h5>
These words are inside the <code>espnow</code> vocabulary.
<p><b>
NOTE: The optional module espnow.h must be
placed next to ESP32forth.ino to include this capability.
</b></p>
<pre>

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
</pre>

<h3 id="adding_words">Adding Words</h3>

<p>
Expand Down

0 comments on commit b726ffc

Please sign in to comment.