8000 use freertos queue for esp now · micropython/micropython@1854d05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1854d05

Browse files
nickzoicshawwwn
authored andcommitted
use freertos queue for esp now
(cherry picked from commit 0918f0516f6906d167c3deed101cff9c993db2a8)
1 parent 5926660 commit 1854d05

File tree

3 files changed

+37
-28
lines changed

3 files changed

+37
-28
lines changed

ports/esp32/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ ESPTOOL ?= $(ESPCOMP)/esptool_py/esptool/esptool.py
3939

4040
# verify the ESP IDF version
4141
<<<<<<< HEAD
42+
<<<<<<< HEAD
4243
=======
4344
ESPIDF_SUPHASH := e6afe28bafe5db5ab79fae213f2e8e1ccd9f937c
4445
>>>>>>> 00b709d2c... Add protocol to network.WLAN.config and LR constant.
46+
=======
47+
ESPIDF_SUPHASH := 2c95a77cf93781f296883d5dbafcdc18e4389656
48+
>>>>>>> 0918f0516... use freertos queue for esp now
4549
ESPIDF_CURHASH := $(shell git -C $(ESPIDF) show -s --pretty=format:'%H')
4650
ifneq ($(ESPIDF_CURHASH),$(ESPIDF_SUPHASH))
4751
$(info ** WARNING **)

ports/esp32/esp_espnow.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "esp_now.h"
3434
#include "esp_wifi.h"
3535

36+
#include "freertos/queue.h"
37+
3638
#include "py/runtime.h"
3739
#include "py/mphal.h"
3840
#include "py/nlr.h"
@@ -79,37 +81,44 @@ static inline void _get_bytes(mp_obj_t str, size_t len, uint8_t *dst) {
7981

8082
// this is crap of course but lets try it
8183

82-
static int recv_buf_len = 0;
83-
uint8_t recv_mac[6];
84-
static uint8_t recv_buffer[250];
84+
typedef struct {
85+
uint8_t macaddr[ESP_NOW_ETH_ALEN];
86+
uint16_t len;
87+
uint8_t data[ESP_NOW_MAX_DATA_LEN];
88+
} esp_now_queue_t;
89+
90+
QueueHandle_t esp_now_queue;
8591

8692
STATIC mp_obj_t espnow_recv() {
87-
if (recv_buf_len < 1) return mp_const_none;
93+
static esp_now_queue_t queue_item = { 0 };
94+
int r = xQueueReceive(esp_now_queue, &queue_item, 0);
95+
if (r != pdTRUE) return mp_const_none;
8896
mp_obj_tuple_t *msg = mp_obj_new_tuple(2, NULL);
89-
msg->items[0] = mp_obj_new_bytes(recv_mac, sizeof(recv_mac));
90-
msg->items[1] = mp_obj_new_bytes(recv_buffer, recv_buf_len);
91-
recv_buf_len = 0;
97+
msg->items[0] = mp_obj_new_bytes(queue_item.macaddr, ESP_NOW_ETH_ALEN);
98+
msg->items[1] = mp_obj_new_bytes(queue_item.data, queue_item.len);
9299
return msg;
93100
}
94101

95102
MP_DEFINE_CONST_FUN_OBJ_0(espnow_recv_obj, espnow_recv);
96103

97-
void simple_cb(const uint8_t *macaddr, const uint8_t *data, int len)
104+
void recv_cb(const uint8_t *macaddr, const uint8_t *data, int len)
98105
{
99-
if (len < sizeof(recv_buffer)) {
100-
memcpy(recv_buffer, data, len);
101-
memcpy(recv_mac, macaddr, 6);
102-
recv_buf_len = len;
103-
}
106+
// this is double copying, perhaps I should be just queueing the pointers
107+
static esp_now_queue_t queue_item = { 0 };
108+
queue_item.len = len;
109+
memcpy(queue_item.macaddr, macaddr, ESP_NOW_ETH_ALEN);
110+
memcpy(queue_item.data, data, len);
111+
xQueueSend(esp_now_queue, &queue_item, 0);
104112
}
105113

106114
static int initialized = 0;
107115

108116
STATIC mp_obj_t espnow_init() {
109117
if (!initialized) {
110118
esp_now_init();
119+
esp_now_queue = xQueueCreate(5, sizeof(esp_now_queue_t));
111120
initialized = 1;
112-
esp_now_register_recv_cb(simple_cb);
121+
esp_now_register_recv_cb(recv_cb);
113122
}
114123
return mp_const_none;
115124
}
@@ -118,31 +127,34 @@ MP_DEFINE_CONST_FUN_OBJ_0(espnow_init_obj, espnow_init);
118127
STATIC mp_obj_t espnow_deinit() {
119128
if (initialized) {
120129
esp_now_deinit();
130+
vQueueDelete(esp_now_queue);
121131
initialized = 0;
122132
}
123133
return mp_const_none;
124134
}
125135
MP_DEFINE_CONST_FUN_OBJ_0(espnow_deinit_obj, espnow_deinit);
126136

127137
STATIC mp_obj_t espnow_set_pmk(mp_obj_t pmk) {
128-
uint8_t buf[ESP_NOW_ETH_ALEN];
129-
_get_bytes(pmk, ESP_NOW_ETH_ALEN, buf);
138+
uint8_t buf[ESP_NOW_KEY_LEN];
139+
_get_bytes(pmk, ESP_NOW_KEY_LEN, buf);
130140
esp_espnow_exceptions(esp_now_set_pmk(buf));
131141
return mp_const_none;
132142
}
133143
MP_DEFINE_CONST_FUN_OBJ_1(espnow_set_pmk_obj, espnow_set_pmk);
134144

135145
STATIC mp_obj_t espnow_add_peer(size_t n_args, const mp_obj_t *args) {
136146
esp_now_peer_info_t peer = {0};
147+
// leaving channel as 0 for autodetect
137148
peer.ifidx = ((wlan_if_obj_t *)MP_OBJ_TO_PTR(args[0]))->if_id;
138149
_get_bytes(args[1], ESP_NOW_ETH_ALEN, peer.peer_addr);
139-
_get_bytes(args[2], ESP_NOW_KEY_LEN, peer.lmk);
140-
peer.encrypt = (n_args > 3 && mp_obj_is_true(args[3])) ? 1 : 0;
141-
// leaving channel as 0 for autodetect
150+
if (n_args > 2) {
151+
_get_bytes(args[2], ESP_NOW_KEY_LEN, peer.lmk);
152+
peer.encrypt = 1;
153+
}
142154
esp_espnow_exceptions(esp_now_add_peer(&peer));
143155
return mp_const_none;
144156
}
145-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_add_peer_obj, 3, 4, espnow_add_peer);
157+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow_add_peer_obj, 2, 3, espnow_add_peer);
146158

147159
STATIC mp_obj_t espnow_send(mp_obj_t addr, mp_obj_t msg) {
148160
mp_uint_t len1;

ports/esp32/modnetwork.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -499,18 +499,11 @@ STATIC mp_obj_t esp_ifconfig(size_t n_args, const mp_obj_t *args) {
499499
netutils_parse_ipv4_addr(items[2], (void*)&info.gw, NETUTILS_BIG);
500500
netutils_parse_ipv4_addr(items[3], (void*)&dns_info.ip, NETUTILS_BIG);
501501
// To set a static IP we have to disable DHCP first
502-
<<<<<<< HEAD
503-
if (self->if_id == WIFI_IF_STA || self->if_id == ESP_IF_ETH) {
504-
esp_err_t e = tcpip_adapter_dhcpc_stop(self->if_id);
505-
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_exceptions(e);
506-
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(self->if_id, &info));
507-
ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
508-
=======
509502
if (self->if_id == WIFI_IF_STA) {
510503
esp_err_t e = tcpip_adapter_dhcpc_stop(WIFI_IF_STA);
511504
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_network_exceptions(e);
512505
ESP_EXCEPTIONS(tcpip_adapter_set_ip_info(WIFI_IF_STA, &info));
513-
>>>>>>> ... start on espnow
506+
ESP_EXCEPTIONS(tcpip_adapter_set_dns_info(self->if_id, TCPIP_ADAPTER_DNS_MAIN, &dns_info));
514507
} else if (self->if_id == WIFI_IF_AP) {
515508
esp_err_t e = tcpip_adapter_dhcps_stop(WIFI_IF_AP);
516509
if (e != ESP_OK && e != ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPPED) _esp_network_exceptions(e);

0 commit comments

Comments
 (0)
0