Skip to content

Commit 5560186

Browse files
committed
Add ESP-NOW optional module
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
1 parent 564a8fc commit 5560186

File tree

11 files changed

+174
-3
lines changed

11 files changed

+174
-3
lines changed

Makefile

+18-2
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ $(GEN)/esp32_spi-flash.h: \
343343
$< spi_flash_source $(VERSION) $(REVISION) \
344344
esp32/optional/spi-flash/spi-flash.fs >$@
345345

346+
$(GEN)/esp32_espnow.h: \
347+
tools/source_to_string.js esp32/optional/espnow/espnow.fs | $(GEN)
348+
$< espnow_source $(VERSION) $(REVISION) \
349+
esp32/optional/espnow/espnow.fs >$@
350+
346351
$(GEN)/esp32_serial-bluetooth.h: \
347352
tools/source_to_string.js \
348353
esp32/optional/serial-bluetooth/bterm.fs \
@@ -358,7 +363,8 @@ OPTIONAL_MODULES = \
358363
$(ESP32)/ESP32forth/interrupts.h \
359364
$(ESP32)/ESP32forth/rmt.h \
360365
$(ESP32)/ESP32forth/serial-bluetooth.h \
361-
$(ESP32)/ESP32forth/spi-flash.h
366+
$(ESP32)/ESP32forth/spi-flash.h \
367+
$(ESP32)/ESP32forth/espnow.h
362368

363369
add-optional: $(OPTIONAL_MODULES)
364370

@@ -702,6 +708,15 @@ $(ESP32)/ESP32forth/optional/spi-flash.h: \
702708
spi_flash=@$(GEN)/esp32_spi-flash.h \
703709
>$@
704710

711+
$(ESP32)/ESP32forth/optional/espnow.h: \
712+
esp32/optional/espnow/espnow.h \
713+
$(GEN)/esp32_espnow.h | $(ESP32)/ESP32forth/optional
714+
cat esp32/optional/espnow/espnow.h | tools/replace.js \
715+
VERSION=$(VERSION) \
716+
REVISION=$(REVISION) \
717+
espnow=@$(GEN)/esp32_espnow.h \
718+
>$@
719+
705720
# ---- ESP32 ARDUINO BUILD AND FLASH ----
706721

707722
ARDUINO_BUILDER="/mnt/c/Program Files (x86)/Arduino/arduino-builder.exe"
@@ -805,7 +820,8 @@ $(ESP32)/ESP32forth.zip: \
805820
$(ESP32)/ESP32forth/optional/interrupts.h \
806821
$(ESP32)/ESP32forth/optional/rmt.h \
807822
$(ESP32)/ESP32forth/optional/serial-bluetooth.h \
808-
$(ESP32)/ESP32forth/optional/spi-flash.h
823+
$(ESP32)/ESP32forth/optional/spi-flash.h \
824+
$(ESP32)/ESP32forth/optional/espnow.h
809825
cd $(ESP32) && rm -f ESP32forth.zip && zip -r ESP32forth.zip ESP32forth
810826

811827
# ---- Publish to Archive ----

esp32/builtins.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@
8484
# define OPTIONAL_SPI_FLASH_SUPPORT
8585
# endif
8686

87+
// Hook to pull in optional ESPNOW support.
88+
# if __has_include("espnow.h")
89+
# include "espnow.h"
90+
# else
91+
# define OPTIONAL_ESPNOW_VOCABULARY
92+
# define OPTIONAL_ESPNOW_SUPPORT
93+
# endif
94+
8795
static cell_t ResizeFile(cell_t fd, cell_t size);
8896

8997
#endif
@@ -119,7 +127,8 @@ static cell_t ResizeFile(cell_t fd, cell_t size);
119127
OPTIONAL_OLED_SUPPORT \
120128
OPTIONAL_RMT_SUPPORT \
121129
OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \
122-
OPTIONAL_SPI_FLASH_SUPPORT
130+
OPTIONAL_SPI_FLASH_SUPPORT \
131+
OPTIONAL_ESPNOW_SUPPORT
123132

124133
#define REQUIRED_MEMORY_SUPPORT \
125134
YV(internals, MALLOC, SET malloc(n0)) \

esp32/optional/README-optional.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ These are the current optional modules:
1616
* serial-bluetooth.h - Support for Bluetooth serial and
1717
bterm a Bluetooth serial redirector for the terminal
1818
* spi-flash.h - Support for low level SPI Flash partition access
19+
* espnow.h - Support for ESP NOW
1920

2021
Initially ESP32forth focused on a minimal C kernel, with most functionality
2122
built in Forth code loaded at boot. Eventually, as support for more capabilities

esp32/optional/espnow/espnow.fs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
\ Copyright 2023 Daniel Nagy
2+
\
3+
\ Licensed under the Apache License, Version 2.0 (the "License");
4+
\ you may not use this file except in compliance with the License.
5+
\ You may obtain a copy of the License at
6+
\
7+
\ http://www.apache.org/licenses/LICENSE-2.0
8+
\
9+
\ Unless required by applicable law or agreed to in writing, software
10+
\ distributed under the License is distributed on an "AS IS" BASIS,
11+
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
\ See the License for the specific language governing permissions and
13+
\ limitations under the License.
14+
15+
vocabulary espnow espnow definitions
16+
transfer espnow-builtins
17+
DEFINED? ESPNOW_init [IF]
18+
base @ hex
19+
\ error codes from https://github.com/espressif/esp-idf/blob/master/components/esp_common/include/esp_err.h
20+
0 constant ESP_OK
21+
-1 constant ESP_FAIL
22+
101 constant ESP_ERR_NO_MEM \ Out of memory
23+
102 constant ESP_ERR_INVALID_ARG \ Invalid argument
24+
103 constant ESP_ERR_INVALID_STATE \ Invalid state
25+
104 constant ESP_ERR_INVALID_SIZE \ Invalid size
26+
105 constant ESP_ERR_NOT_FOUND \ Requested resource not found
27+
106 constant ESP_ERR_NOT_SUPPORTED \ Operation or feature not supported
28+
107 constant ESP_ERR_TIMEOUT \ Operation timed out
29+
108 constant ESP_ERR_INVALID_RESPONSE \ Received response was invalid
30+
109 constant ESP_ERR_INVALID_CRC \ CRC or checksum was invalid
31+
10A constant ESP_ERR_INVALID_VERSION \ Version was invalid
32+
10B constant ESP_ERR_INVALID_MAC \ MAC address was invalid
33+
10C constant ESP_ERR_NOT_FINISHED \ Operation has not fully completed
34+
35+
3000 constant ESP_ERR_WIFI_BASE \ Starting number of WiFi error codes
36+
3000 constant ESP_ERR_MESH_BASE \ Starting number of MESH error codes
37+
6000 constant ESP_ERR_FLASH_BASE \ Starting number of flash error codes
38+
c000 constant ESP_ERR_HW_CRYPTO_BASE \ Starting number of HW cryptography module error codes
39+
d000 constant ESP_ERR_MEMPROT_BASE \ Starting number of Memory Protection API error codes
40+
base !
41+
[THEN]
42+
forth definitions

esp32/optional/espnow/espnow.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <WiFi.h>
2+
#include <esp_wifi.h>
3+
#include <esp_now.h>
4+
5+
// More documentation
6+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html
7+
8+
static esp_err_t ESPNOW_add_peer (const uint8_t *broadcastAddress) {
9+
esp_now_peer_info_t peerInfo = {0};
10+
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
11+
peerInfo.channel = 0; // use current wifi channel
12+
peerInfo.encrypt = false;
13+
return esp_now_add_peer(&peerInfo);
14+
}
15+
16+
#define OPTIONAL_ESPNOW_VOCABULARY V(espnow)
17+
#define OPTIONAL_ESPNOW_SUPPORT \
18+
XV(internals, "espnow-source", ESPNOW_SOURCE, \
19+
PUSH espnow_source; PUSH sizeof(espnow_source) - 1) \
20+
YV(espnow, espnow_init , PUSH esp_now_init()) \
21+
YV(espnow, espnow_deinit , PUSH esp_now_deinit()) \
22+
YV(espnow, espnow_add_peer, n0 = ESPNOW_add_peer(b0)) \
23+
YV(espnow, espnow_send , n0 = esp_now_send(b2, b1, n0); NIPn(2)) \
24+
25+
26+
{{espnow}}

esp32/optionals.fs

+4
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,7 @@ internals DEFINED? serial-bluetooth-source [IF]
4747
internals DEFINED? spi-flash-source [IF]
4848
spi-flash-source evaluate
4949
[THEN] forth
50+
51+
internals DEFINED? espnow-source [IF]
52+
espnow-source evaluate
53+
[THEN] forth

esp32/options.h

+1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@
9595
OPTIONAL_OLED_VOCABULARY \
9696
OPTIONAL_RMT_VOCABULARY \
9797
OPTIONAL_SPI_FLASH_VOCABULARY \
98+
OPTIONAL_ESPNOW_VOCABULARY \
9899
USER_VOCABULARIES

esp32/print-builtins.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@
3131
#define OPTIONAL_RMT_SUPPORT
3232
#define OPTIONAL_SERIAL_BLUETOOTH_SUPPORT
3333
#define OPTIONAL_SPI_FLASH_SUPPORT
34+
#define OPTIONAL_ESPNOW_SUPPORT
3435

3536
#define OPTIONAL_BLUETOOTH_VOCABULARY
3637
#define OPTIONAL_CAMERA_VOCABULARY
3738
#define OPTIONAL_INTERRUPTS_VOCABULARIES
3839
#define OPTIONAL_OLED_VOCABULARY
3940
#define OPTIONAL_RMT_VOCABULARY
4041
#define OPTIONAL_SPI_FLASH_VOCABULARY
42+
#define OPTIONAL_ESPNOW_VOCABULARY
4143

4244
#include "builtins.h"
4345

esp32/sim_main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define OPTIONAL_OLED_VOCABULARY
2929
#define OPTIONAL_RMT_VOCABULARY
3030
#define OPTIONAL_SPI_FLASH_VOCABULARY
31+
#define OPTIONAL_ESPNOW_VOCABULARY
3132

3233
static cell_t *simulated(cell_t *sp, const char *op);
3334

examples/espnow.fs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#! /usr/bin/env ueforth
2+
3+
\ Copyright 2023 Daniel Nagy
4+
\
5+
\ Licensed under the Apache License, Version 2.0 (the "License");
6+
\ you may not use this file except in compliance with the License.
7+
\ You may obtain a copy of the License at
8+
\
9+
\ http://www.apache.org/licenses/LICENSE-2.0
10+
\
11+
\ Unless required by applicable law or agreed to in writing, software
12+
\ distributed under the License is distributed on an "AS IS" BASIS,
13+
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
\ See the License for the specific language governing permissions and
15+
\ limitations under the License.
16+
17+
also WiFi
18+
also espnow
19+
20+
\ start wifi
21+
WIFI_MODE_STA WiFi.mode
22+
\ initialize espnow and check result
23+
espnow_init ESP_OK <> throw
24+
25+
base @ hex
26+
: espnow_register_example
27+
\ register the mac 12:34:56:78:9a:bc as a peer
28+
12 c, 34 c, 56 c, 78 c, 9a c, bc c,
29+
here 6 -
30+
ESPNOW_add_peer ESP_OK <> throw
31+
-6 allot
32+
;
33+
: espnow_send_some
34+
\ NULL a.k.a. send to peerlist
35+
\ send 10 bytes of data space pointer
36+
0 here 10 - 10
37+
espnow_send ESP_OK <> throw
38+
;
39+
base !

site/ESP32forth.html

+30
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,36 @@ <h5 id="timers">Timers</h5>
10551055
TIMGn_Tx_INT_CLR_REG ( n -- a )
10561056
</pre>
10571057

1058+
<h5 id="espnow">ESPNOW</h5>
1059+
These words are inside the <code>espnow</code> vocabulary.
1060+
<p><b>
1061+
NOTE: The optional module espnow.h must be
1062+
placed next to ESP32forth.ino to include this capability.
1063+
</b></p>
1064+
<pre>
1065+
1066+
espnow_init ( -- err )
1067+
espnow_deinit ( -- err )
1068+
espnow_add_peer ( a -- err )
1069+
espnow_send ( b2 b1 n0 -- err )
1070+
1071+
( Constants )
1072+
ESP_OK
1073+
ESP_FAIL
1074+
ESP_ERR_NO_MEM \ Out of memory
1075+
ESP_ERR_INVALID_ARG \ Invalid argument
1076+
ESP_ERR_INVALID_STATE \ Invalid state
1077+
ESP_ERR_INVALID_SIZE \ Invalid size
1078+
ESP_ERR_NOT_FOUND \ Requested resource not found
1079+
ESP_ERR_NOT_SUPPORTED \ Operation or feature not supported
1080+
ESP_ERR_TIMEOUT \ Operation timed out
1081+
ESP_ERR_INVALID_RESPONSE \ Received response was invalid
1082+
ESP_ERR_INVALID_CRC \ CRC or checksum was invalid
1083+
ESP_ERR_INVALID_VERSION \ Version was invalid
1084+
ESP_ERR_INVALID_MAC \ MAC address was invalid
1085+
ESP_ERR_NOT_FINISHED \ Operation has not fully completed
1086+
</pre>
1087+
10581088
<h3 id="adding_words">Adding Words</h3>
10591089

10601090
<p>

0 commit comments

Comments
 (0)