Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions ESP32/ESP32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

using namespace mbed;
using namespace rtos;
using namespace std::chrono;
using std::milli;

ESP32 * ESP32::instESP32 = NULL;

Expand Down Expand Up @@ -191,7 +193,7 @@ void ESP32::_startup_common()
}
if (_p_wifi_en != NULL) {
_p_wifi_en->write(0);
ThisThread::sleep_for(10);
ThisThread::sleep_for(10ms);
_p_wifi_en->write(1);
_parser.recv("ready");
} else {
Expand Down Expand Up @@ -351,7 +353,7 @@ bool ESP32::accept(int * p_id)
}
_smutex.unlock();
if (!ret) {
ThisThread::sleep_for(5);
ThisThread::sleep_for(5ms);
}
}

Expand Down Expand Up @@ -392,7 +394,7 @@ bool ESP32::reset(void)
#endif
}

ThisThread::sleep_for(5);
ThisThread::sleep_for(5ms);

uint8_t wk_ver[4+1]; /* It needs 1 byte extra. */

Expand Down Expand Up @@ -654,7 +656,7 @@ int ESP32::scan(WiFiAccessPoint *res, unsigned limit)
_smutex.lock();
_startup_wifi();
_smutex.unlock();
ThisThread::sleep_for(1500);
ThisThread::sleep_for(1500ms);
}

_smutex.lock();
Expand Down Expand Up @@ -1109,6 +1111,14 @@ int8_t ESP32::get_wifi_status() const
return _wifi_status;
}

void ESP32::flush()
{
_smutex.lock();
_parser.flush();
_smutex.unlock();
}


#if defined(TARGET_ESP32AT_BLE)
bool ESP32::ble_set_role(int role)
{
Expand Down
27 changes: 23 additions & 4 deletions ESP32/ESP32.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@
#include <stdlib.h>
#include "drivers/DigitalOut.h"
#include "drivers/SerialBase.h"
#include "drivers/BufferedSerial.h"
#include "features/netsocket/nsapi_types.h"
#include "features/netsocket/WiFiAccessPoint.h"
#include "PinNames.h"
#include "platform/ATCmdParser.h"
#include "platform/Callback.h"
#include "platform/mbed_error.h"
#include "platform/mbed_version.h"
#include "rtos/Mutex.h"
#include "rtos/ThisThread.h"

#if (MBED_MAJOR_VERSION < 6)
#include "drivers/UARTSerial.h"
#else
#include "drivers/BufferedSerial.h"
#endif

#ifndef ESP32_CONNECT_TIMEOUT
#define ESP32_CONNECT_TIMEOUT 15000
#endif
Expand Down Expand Up @@ -246,6 +252,15 @@ class ESP32
*/
int8_t get_wifi_status() const;

/**
* Flush the serial port input buffers.
*
* If you do HW reset for ESP module, you should
* flush the input buffers from existing responses
* from the device.
*/
void flush();

static const int8_t WIFIMODE_STATION = 1;
static const int8_t WIFIMODE_SOFTAP = 2;
static const int8_t WIFIMODE_STATION_SOFTAP = 3;
Expand All @@ -260,7 +275,11 @@ class ESP32
mbed::DigitalOut * _p_wifi_io0;
bool _init_end_common;
bool _init_end_wifi;
#if (MBED_MAJOR_VERSION < 6)
mbed::UARTSerial _serial;
#else
mbed::BufferedSerial _serial;
#endif
mbed::ATCmdParser _parser;
struct packet {
struct packet *next;
Expand Down Expand Up @@ -352,11 +371,11 @@ class ESP32
typedef struct {
uint16_t adv_int_min; /**< minimum value of advertising interval; range: 0x0020 ~ 0x4000 */
uint16_t adv_int_max; /**< maximum value of advertising interval; range: 0x0020 ~ 0x4000 */
uint8_t adv_type; /**< 0FADV_TYPE_IND, 2FADV_TYPE_SCAN_IND, 3FADV_TYPE_NONCONN_IND */
uint8_t own_addr_type; /**< own BLE address type; 0FBLE_ADDR_TYPE_PUBLIC, 1FBLE_ADDR_TYPE_RANDOM */
uint8_t adv_type; /**< 0:FADV_TYPE_IND, 2:FADV_TYPE_SCAN_IND, 3:FADV_TYPE_NONCONN_IND */
uint8_t own_addr_type; /**< own BLE address type; 0:FBLE_ADDR_TYPE_PUBLIC, 1:FBLE_ADDR_TYPE_RANDOM */
uint8_t channel_map; /**< channel of advertising; ADV_CHNL_~ */
uint8_t adv_filter_policy; /**< filter policy of advertising; ADV_FILTER_ALLOW_SCAN_~ */
uint8_t peer_addr_type; /**< remote BLE address type; 0FPUBLIC, 1FRANDOM */
uint8_t peer_addr_type; /**< remote BLE address type; 0:FPUBLIC, 1:FRANDOM */
uint8_t peer_addr[6]; /**< remote BLE address */
} advertising_param_t;

Expand Down
63 changes: 63 additions & 0 deletions ESP32Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
ESP32Interface::ESP32Interface() :
ESP32Stack(MBED_CONF_ESP32_WIFI_EN, MBED_CONF_ESP32_WIFI_IO0, MBED_CONF_ESP32_WIFI_TX, MBED_CONF_ESP32_WIFI_RX, MBED_CONF_ESP32_WIFI_DEBUG,
MBED_CONF_ESP32_WIFI_RTS, MBED_CONF_ESP32_WIFI_CTS, MBED_CONF_ESP32_WIFI_BAUDRATE, 0),
_rst_pin(MBED_CONF_ESP32_WIFI_EN),
_initialized(false),
_dhcp(true),
_ap_ssid(),
_ap_pass(),
Expand All @@ -38,6 +40,8 @@ ESP32Interface::ESP32Interface() :
ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
PinName rts, PinName cts, int baudrate) :
ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate, 0),
_rst_pin(en),
_initialized(false),
_dhcp(true),
_ap_ssid(),
_ap_pass(),
Expand All @@ -54,6 +58,8 @@ ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx,

ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) :
ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400, 0),
_rst_pin(MBED_CONF_ESP32_WIFI_EN),
_initialized(false),
_dhcp(true),
_ap_ssid(),
_ap_pass(),
Expand Down Expand Up @@ -108,6 +114,8 @@ int ESP32Interface::connect(const char *ssid, const char *pass, nsapi_security_t
if (ret != NSAPI_ERROR_OK) {
return ret;
}

_init();
return connect();
}

Expand Down Expand Up @@ -201,6 +209,7 @@ int ESP32Interface::set_channel(uint8_t channel)

int ESP32Interface::disconnect()
{
_initialized = false;
if (_connection_status == NSAPI_STATUS_DISCONNECTED) {
return NSAPI_ERROR_NO_CONNECTION;
}
Expand Down Expand Up @@ -263,6 +272,7 @@ int8_t ESP32Interface::get_rssi()

int ESP32Interface::scan(WiFiAccessPoint *res, unsigned count)
{
_init();
return _esp->scan(res, count);
}

Expand Down Expand Up @@ -311,3 +321,56 @@ WiFiInterface *WiFiInterface::get_default_instance() {

#endif

ESP32Interface::~ESP32Interface()
{
// Power down the modem
_rst_pin.rst_assert();
}

ESP32Interface::ResetPin::ResetPin(PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1))
{
}

void ESP32Interface::ResetPin::rst_assert()
{
if (_rst_pin.is_connected()) {
_rst_pin = 0;
//tr_debug("rst_assert(): HW reset asserted.");
}
}

void ESP32Interface::ResetPin::rst_deassert()
{
if (_rst_pin.is_connected()) {
_rst_pin = 1;
//tr_debug("rst_deassert(): HW reset deasserted.");
}
}

bool ESP32Interface::ResetPin::is_connected()
{
return _rst_pin.is_connected();
}

nsapi_error_t ESP32Interface::_init(void)
{
if (!_initialized) {
if (_reset() != NSAPI_ERROR_OK) {
return NSAPI_ERROR_DEVICE_ERROR;
}
_initialized = true;
}
return NSAPI_ERROR_OK;
}

nsapi_error_t ESP32Interface::_reset(void)
{
if (_rst_pin.is_connected()) {
_rst_pin.rst_assert();
rtos::ThisThread::sleep_for(2ms);
_esp->flush();
_rst_pin.rst_deassert();
}

return NSAPI_ERROR_OK;
}
22 changes: 21 additions & 1 deletion ESP32Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false,
PinName rts = NC, PinName cts = NC, int baudrate = 230400);

/**
* @brief ESP32Interface default destructor
*/
virtual ~ESP32Interface();

/** ESP32Interface lifetime
* @param tx TX pin
* @param rx RX pin
Expand Down Expand Up @@ -241,6 +246,19 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
}

private:

// HW reset pin
class ResetPin {
public:
ResetPin(PinName rst_pin);
void rst_assert();
void rst_deassert();
bool is_connected();
private:
mbed::DigitalOut _rst_pin;
} _rst_pin;

int _initialized;
bool _dhcp;
char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
char _ap_pass[64]; /* The longest allowed passphrase */
Expand All @@ -249,10 +267,12 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface
SocketAddress _netmask;
SocketAddress _gateway;
nsapi_connection_status_t _connection_status;
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;

void set_connection_status(nsapi_connection_status_t connection_status);
void wifi_status_cb(int8_t wifi_status);
nsapi_error_t _init(void);
nsapi_error_t _reset(void);
};

#endif