Skip to content

Commit 96b1ed2

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 96b1ed2

File tree

11 files changed

+187
-3
lines changed

11 files changed

+187
-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

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
\ error codes from https://github.com/espressif/esp-idf/blob/master/components/esp_common/include/esp_err.h
19+
0 constant ESP_OK
20+
-1 constant ESP_FAIL
21+
$101 constant ESP_ERR_NO_MEM \ Out of memory
22+
$102 constant ESP_ERR_INVALID_ARG \ Invalid argument
23+
$103 constant ESP_ERR_INVALID_STATE \ Invalid state
24+
$104 constant ESP_ERR_INVALID_SIZE \ Invalid size
25+
$105 constant ESP_ERR_NOT_FOUND \ Requested resource not found
26+
$106 constant ESP_ERR_NOT_SUPPORTED \ Operation or feature not supported
27+
$107 constant ESP_ERR_TIMEOUT \ Operation timed out
28+
$108 constant ESP_ERR_INVALID_RESPONSE \ Received response was invalid
29+
$109 constant ESP_ERR_INVALID_CRC \ CRC or checksum was invalid
30+
$10A constant ESP_ERR_INVALID_VERSION \ Version was invalid
31+
$10B constant ESP_ERR_INVALID_MAC \ MAC address was invalid
32+
$10C constant ESP_ERR_NOT_FINISHED \ Operation has not fully completed
33+
34+
$3000 constant ESP_ERR_WIFI_BASE \ Starting number of WiFi error codes
35+
$3000 constant ESP_ERR_MESH_BASE \ Starting number of MESH error codes
36+
$6000 constant ESP_ERR_FLASH_BASE \ Starting number of flash error codes
37+
$c000 constant ESP_ERR_HW_CRYPTO_BASE \ Starting number of HW cryptography module error codes
38+
$d000 constant ESP_ERR_MEMPROT_BASE \ Starting number of Memory Protection API error codes
39+
[THEN]
40+
forth definitions

esp32/optional/espnow/espnow.h

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
/*
16+
* ESP32forth ESPNOW v{{VERSION}}
17+
* Revision: {{REVISION}}
18+
*/
19+
20+
// More documentation
21+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/network/esp_now.html
22+
23+
#include <esp_now.h>
24+
25+
static esp_err_t ESPNOW_add_peer (const uint8_t *broadcastAddress) {
26+
esp_now_peer_info_t peerInfo = {0};
27+
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
28+
peerInfo.channel = 0; // use current wifi channel
29+
peerInfo.encrypt = false;
30+
return esp_now_add_peer(&peerInfo);
31+
}
32+
33+
#define OPTIONAL_ESPNOW_VOCABULARY V(espnow)
34+
#define OPTIONAL_ESPNOW_SUPPORT \
35+
XV(internals, "espnow-source", ESPNOW_SOURCE, \
36+
PUSH espnow_source; PUSH sizeof(espnow_source) - 1) \
37+
YV(espnow, espnow_init , PUSH esp_now_init()) \
38+
YV(espnow, espnow_deinit , PUSH esp_now_deinit()) \
39+
YV(espnow, espnow_add_peer, n0 = ESPNOW_add_peer(b0)) \
40+
YV(espnow, espnow_send , n0 = esp_now_send(b2, b1, n0); NIPn(2)) \
41+
42+
43+
{{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

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
: espnow_register_example
26+
\ register the mac 12:34:56:78:9a:bc as a peer
27+
$12 c, $34 c, $56 c, $78 c, $9a c, $bc c,
28+
here 6 -
29+
ESPNOW_add_peer ESP_OK <> throw
30+
-6 allot
31+
;
32+
: espnow_send_some
33+
\ NULL a.k.a. send to peerlist
34+
\ send 10 bytes of data space pointer
35+
0 here 10 - 10
36+
espnow_send ESP_OK <> throw
37+
;

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)