Skip to content

Commit ce1a09d

Browse files
author
Veijo Pesonen
committed
Switches from OOB thread to global event queue
1 parent 40e4ee1 commit ce1a09d

File tree

3 files changed

+55
-56
lines changed

3 files changed

+55
-56
lines changed

ESP8266Interface.cpp

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
*/
1616

1717
#include <cstring>
18-
#include "Callback.h"
18+
#include "events/EventQueue.h"
19+
#include "events/mbed_shared_queues.h"
20+
#include "platform/Callback.h"
1921
#include "ESP8266.h"
2022
#include "ESP8266Interface.h"
2123
#include "mbed_debug.h"
@@ -51,8 +53,8 @@ ESP8266Interface::ESP8266Interface()
5153
_started(false),
5254
_conn_stat(NSAPI_STATUS_DISCONNECTED),
5355
_conn_stat_cb(NULL),
54-
_oob_thr(NULL),
55-
_oob_thr_sta(NULL)
56+
_geq(NULL),
57+
_oob_event_id(0)
5658
{
5759
memset(_cbs, 0, sizeof(_cbs));
5860
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -77,9 +79,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
7779
_started(false),
7880
_conn_stat(NSAPI_STATUS_DISCONNECTED),
7981
_conn_stat_cb(NULL),
80-
_oob_thr(NULL),
81-
_oob_thr_sta(NULL),
82-
_oob_thr_run(false)
82+
_geq(NULL),
83+
_oob_event_id(0)
8384
{
8485
memset(_cbs, 0, sizeof(_cbs));
8586
memset(ap_ssid, 0, sizeof(ap_ssid));
@@ -97,14 +98,8 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
9798

9899
ESP8266Interface::~ESP8266Interface()
99100
{
100-
_oob_thr_run = false;
101-
102-
if (_oob_thr) {
103-
_oob_thr->join();
104-
delete _oob_thr;
105-
}
106-
if (_oob_thr_sta) {
107-
free (_oob_thr_sta);
101+
if (_oob_event_id) {
102+
_geq->cancel(_oob_event_id);
108103
}
109104
}
110105

@@ -123,36 +118,14 @@ int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security
123118
return connect();
124119
}
125120

126-
void ESP8266Interface::start_bg_oob()
121+
void ESP8266Interface::_oob2geq()
127122
{
128-
static const uint32_t _stack_sz = 1536;
129-
130-
if (!_oob_thr_sta) {
131-
_oob_thr_sta = (unsigned char*) (malloc(_stack_sz));
132-
}
133-
if (!_oob_thr_sta) {
134-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_ENOMEM), \
135-
"ESP8266::start_bg_oob: no memory");
136-
}
137-
138-
if (!_oob_thr) {
139-
_oob_thr = new Thread(osPriorityNormal,
140-
_stack_sz,
141-
_oob_thr_sta,
142-
"esp8266_oob");
143-
}
144-
if (!_oob_thr) {
145-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
146-
"ESP8266::start_bg_oob: thread creation failed");
147-
}
123+
_geq = mbed_event_queue();
124+
_oob_event_id = _geq->call_every(ESP8266_RECV_TIMEOUT, callback(this, &ESP8266Interface::proc_oob_evnt));
148125

149-
if (_oob_thr->get_state() == rtos::Thread::Deleted) {
150-
_oob_thr_run = true;
151-
osStatus status = _oob_thr->start(callback(this, &ESP8266Interface::thr_process_oob));
152-
if (status != osOK) {
153-
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_THREAD_CREATE_FAILED), \
154-
"ESP8266::start_bg_oob: thread start failed");
155-
}
126+
if (!_oob_event_id) {
127+
MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
128+
"ESP8266::_oob2geq: unable to allocate OOB event");
156129
}
157130
}
158131

@@ -175,7 +148,9 @@ int ESP8266Interface::connect()
175148
return status;
176149
}
177150

178-
start_bg_oob();
151+
if (!_oob_event_id) {
152+
_oob2geq();
153+
}
179154

180155
if(get_ip_address()) {
181156
return NSAPI_ERROR_IS_CONNECTED;
@@ -680,6 +655,7 @@ void ESP8266Interface::update_conn_state_cb()
680655
default:
681656
_started = false;
682657
_initialized = false;
658+
_geq->cancel(_oob_event_id);
683659
_conn_stat = NSAPI_STATUS_DISCONNECTED;
684660
}
685661

@@ -689,12 +665,9 @@ void ESP8266Interface::update_conn_state_cb()
689665
}
690666
}
691667

692-
void ESP8266Interface::thr_process_oob()
668+
void ESP8266Interface::proc_oob_evnt()
693669
{
694-
while (_oob_thr_run) {
695-
if (_initialized) {
696-
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
697-
}
698-
wait_ms(ESP8266_RECV_TIMEOUT);
670+
if (_initialized) {
671+
_esp.bg_process_oob(ESP8266_RECV_TIMEOUT, true);
699672
}
700673
}

ESP8266Interface.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919

2020
#include "mbed.h"
2121

22-
#include "Thread.h"
23-
#include "Callback.h"
22+
#include "events/EventQueue.h"
23+
#include "events/mbed_shared_queues.h"
24+
#include "rtos/Thread.h"
25+
#include "platform/Callback.h"
2426
#include "netsocket/nsapi_types.h"
2527
#include "netsocket/NetworkInterface.h"
2628
#include "netsocket/NetworkStack.h"
@@ -369,11 +371,11 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface
369371
Callback<void(nsapi_event_t, intptr_t)> _conn_stat_cb;
370372

371373
// Background OOB processing
372-
rtos::Thread *_oob_thr;
373-
unsigned char *_oob_thr_sta;
374-
bool _oob_thr_run;
375-
void thr_process_oob();
376-
void start_bg_oob();
374+
// Use global EventQueue
375+
events::EventQueue *_geq;
376+
int _oob_event_id;
377+
void proc_oob_evnt();
378+
void _oob2geq();
377379
};
378380

379381
#endif

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ Least one is expected to check are the following configuration parameters
5252
}
5353
```
5454

55+
To enable network status updates from the driver one must take Global EventQueue into use via app config file. Otherwise
56+
status updates are only processes when calling socket send() or recv(). Consider setting the stack size for the queue at
57+
least to 1.5KB and increase it in case of stack overflow
58+
59+
```javascript
60+
"target_overrides": {
61+
"*": {
62+
"nsapi.default-wifi-ssid": "\"WIFI_SSID\"",
63+
"nsapi.default-wifi-password": "\"WIFI_PASSWORD\"",
64+
"nsapi.default-wifi-security": "WIFI_SECURITY",
65+
"esp8266.provide-default": true,
66+
"esp8266.socket-bufsize": 153600,
67+
"esp8266.rts": "NC",
68+
"esp8266.cts": "NC"
69+
"esp8266.rx": "D0",
70+
"esp8266.tx": "D1",
71+
// Adds dispatch_forever() to your main-function
72+
"events.shared-dispatch-from-application": true,
73+
// Adjusts the stack size of the global event queue
74+
"events.shared-stacksize": 1536
75+
}
76+
}
77+
```
78+
5579
## UART HW flow control
5680

5781
UART HW flow control requires you to additionally wire the CTS and RTS flow control pins between your board and your

0 commit comments

Comments
 (0)