From f9dc5250111e414ab76c5800ee2f7acd4e270b55 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Tue, 28 Apr 2020 13:44:19 -0700 Subject: [PATCH] Improve WiFi performance by 20X Polling for the async response was ruinning WiFi and UDP performance. This fix removes that polling while not breaking anything. It has been stress tested while streaming over an hour of wifi video data using the new RPC scripts. --- src/omv/py/py_winc.c | 7 ++++++- src/winc1500/src/winc.c | 40 +++++++++++++++++++++------------------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/omv/py/py_winc.c b/src/omv/py/py_winc.c index 8e24e39f8..1f70c39b2 100644 --- a/src/omv/py/py_winc.c +++ b/src/omv/py/py_winc.c @@ -502,7 +502,12 @@ static mp_uint_t py_winc_socket_sendto(mod_network_socket_obj_t *socket, { MAKE_SOCKADDR(addr, ip, port) int ret = winc_socket_sendto(socket->fd, buf, len, &addr, socket->timeout); - if (ret < 0) { + if (ret == SOCK_ERR_TIMEOUT) { + // The socket is Not closed on timeout when calling + // WINC1500 functions that actually accept a timeout. + *_errno = MP_ETIMEDOUT; + return 0; + } else if (ret < 0) { *_errno = ret; py_winc_socket_close(socket); return -1; diff --git a/src/winc1500/src/winc.c b/src/winc1500/src/winc.c index 828250cdb..91e842d3e 100644 --- a/src/winc1500/src/winc.c +++ b/src/winc1500/src/winc.c @@ -410,6 +410,7 @@ static int winc_async_request(uint8_t msg_type, void *ret, uint32_t timeout) // Wait for async request to finish. while (async_request_done == false) { + __WFI(); // Handle pending events from network controller. m2m_wifi_handle_events(NULL); // timeout == 0 in blocking mode. @@ -537,6 +538,7 @@ int winc_connect(const char *ssid, uint8_t security, const char *key, uint16_t c async_request_done = false; while (async_request_done == false) { + __WFI(); // Handle pending events from network controller. m2m_wifi_handle_events(NULL); } @@ -602,6 +604,7 @@ int winc_wait_for_sta(uint32_t *sta_ip, uint32_t timeout) { uint32_t tick_start = HAL_GetTick(); while (connected_sta_ip == 0) { + __WFI(); // Handle pending events from network controller. m2m_wifi_handle_events(NULL); if (timeout && ((HAL_GetTick() - tick_start) >= timeout)) { @@ -637,6 +640,7 @@ int winc_netinfo(winc_netinfo_t *netinfo) m2m_wifi_get_connection_info(); while (async_request_done == false) { + __WFI(); // Handle pending events from network controller. m2m_wifi_handle_events(NULL); } @@ -654,6 +658,7 @@ int winc_scan(winc_scan_callback_t cb, void *arg) m2m_wifi_request_scan(M2M_WIFI_CH_ALL); while (async_request_done == false) { + __WFI(); // Handle pending events from network controller. m2m_wifi_handle_events(NULL); } @@ -671,6 +676,7 @@ int winc_get_rssi() m2m_wifi_req_curr_rssi(); while (async_request_done == false) { + __WFI(); // Handle pending events from network controller. m2m_wifi_handle_events(NULL); } @@ -827,21 +833,19 @@ int winc_socket_send(int fd, const uint8_t *buf, uint32_t len, uint32_t timeout) int bytes = 0; while (bytes < len) { + // Do async request (clean out any messages - but ignore them). + int async_ret; + async_request_data = &async_ret; + async_request_done = false; + async_request_type = SOCKET_MSG_SEND; + m2m_wifi_handle_events(NULL); + // Split the packet into smaller ones. int n = MIN((len - bytes), SOCKET_BUFFER_MAX_LENGTH); int ret = WINC1500_EXPORT(send)(fd, (uint8_t*)buf + bytes, n, 0); if (ret == SOCK_ERR_NO_ERROR) { - // Do async request - int async_ret; - ret = winc_async_request(SOCKET_MSG_SEND, &async_ret, timeout); - - // Check sent bytes returned from async request. - if (ret != SOCK_ERR_NO_ERROR || async_ret <= 0) { - return (ret != SOCK_ERR_NO_ERROR) ? ret : async_ret; - } - - bytes += async_ret; + bytes += n; } else if (ret == SOCK_ERR_BUFFER_FULL) { // timeout == 0 in blocking mode. if (timeout && ((HAL_GetTick() - tick_start) >= timeout)) { @@ -891,20 +895,18 @@ int winc_socket_sendto(int fd, const uint8_t *buf, uint32_t len, sockaddr *addr, int bytes = 0; while (bytes < len) { + // Do async request (clean out any messages - but ignore them). + int async_ret; + async_request_data = &async_ret; + async_request_done = false; + async_request_type = SOCKET_MSG_SENDTO; + m2m_wifi_handle_events(NULL); + // Split the packet into smaller ones. int n = MIN((len - bytes), SOCKET_BUFFER_MAX_LENGTH); int ret = WINC1500_EXPORT(sendto)(fd, (uint8_t*)buf + bytes, n, 0, addr, sizeof(*addr)); if (ret == SOCK_ERR_NO_ERROR) { - // Do async request - int async_ret; // sendto always returns 0. - ret = winc_async_request(SOCKET_MSG_SENDTO, &async_ret, timeout); - - // Check sent bytes returned from async request. - if (ret != SOCK_ERR_NO_ERROR || async_ret < 0) { - return (ret != SOCK_ERR_NO_ERROR) ? ret : async_ret; - } - bytes += n; } else if (ret == SOCK_ERR_BUFFER_FULL) { // timeout == 0 in blocking mode.