Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rtl872x] hal: wifi stack stopping to work issue #2649

Merged
merged 7 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
31 changes: 30 additions & 1 deletion hal/network/ncp_client/realtek/rtl_ncp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ LOG_SOURCE_CATEGORY("ncp.rltk.client");
#include "gpio_hal.h"
#include "timer_hal.h"
#include "delay_hal.h"
#include "ble_hal.h"

#include "check.h"

Expand All @@ -46,6 +47,7 @@ extern "C" {

#include "spark_wiring_vector.h"
#include "rtl_system_error.h"
#include "rtl_sdk_support.h"

#undef IFNAMSIZ // AMBD SDK and LWIP both define this symbol...
#include "wlan_hal.h"
Expand Down Expand Up @@ -429,6 +431,10 @@ int RealtekNcpClient::scan(WifiScanCallback callback, void* data) {
}
return RTW_SUCCESS;
}, (void*)&ctx);
if (rtlError) {
LOG(WARN,"wifi_scan_networks err %d", rtlError);
scott-brust marked this conversation as resolved.
Show resolved Hide resolved
ctx.done = true;
}
while (!ctx.done) {
HAL_Delay_Milliseconds(100);
}
Expand All @@ -438,7 +444,30 @@ int RealtekNcpClient::scan(WifiScanCallback callback, void* data) {
if (ctx.results.size() == 0) {
// Workaround for a weird state we might enter where the wifi driver
// is not returning any results
CHECK(off());
hal_ble_lock(nullptr);
bool advertising = hal_ble_gap_is_advertising(nullptr) ||
hal_ble_gap_is_connecting(nullptr, nullptr) ||
hal_ble_gap_is_connected(nullptr, nullptr);
hal_ble_stack_deinit(nullptr);
rtwCoexPreventCleanup(0);

HAL_Delay_Milliseconds(100);

wifi_off();

RCC_PeriphClockCmd(APBPeriph_WL, APBPeriph_WL_CLOCK, DISABLE);
RCC_PeriphClockCmd(APBPeriph_WL, APBPeriph_WL_CLOCK, ENABLE);
rtwCoexCleanup(0);

SPARK_ASSERT(wifi_on(RTW_MODE_STA) == 0);

if (hal_ble_stack_init(nullptr) == SYSTEM_ERROR_NONE) {
if (advertising) {
hal_ble_gap_start_advertising(nullptr);
}
}

hal_ble_unlock(nullptr);
}
return rtl_error_to_system(rtlError);
}
Expand Down
38 changes: 6 additions & 32 deletions hal/src/rtl872x/ble_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ extern "C" {
#include "network/ncp/wifi/ncp.h"
#include "network/ncp/wifi/wifi_network_manager.h"
#include "network/ncp/wifi/wifi_ncp_client.h"
#include "rtl_sdk_support.h"

using spark::Vector;
using namespace particle;
Expand Down Expand Up @@ -155,33 +156,6 @@ constexpr system_tick_t BLE_ENQUEUE_TIMEOUT_MS = 5000;

StaticRecursiveMutex s_bleMutex;

void rtwCoexRunDisable() {
os_thread_scheduling(false, nullptr);
auto p = pcoex[0];
if (p && (p->state & 0x000000ff) != 0x00) {
p->state &= 0xffffff00;
}
os_thread_scheduling(true, nullptr);
}

void rtwCoexRunEnable() {
os_thread_scheduling(false, nullptr);
auto p = pcoex[0];
if (p) {
p->state |= 0x01;
}
os_thread_scheduling(true, nullptr);
}

void rtwCoexCleanup() {
auto p = pcoex[0];
if (p && p->mutex) {
auto m = p->mutex;
p->mutex = nullptr;
rtw_mutex_free(m);
}
}

bool isUuidEqual(const hal_ble_uuid_t* uuid1, const hal_ble_uuid_t* uuid2) {
if (uuid1->type != uuid2->type) {
return false;
Expand Down Expand Up @@ -1086,7 +1060,7 @@ int BleGap::init() {
gap_config_max_le_link_num(BLE_MAX_LINK_COUNT);
gap_config_max_le_paired_device(BLE_MAX_LINK_COUNT);

rtwCoexRunDisable();
rtwCoexRunDisable(0);

CHECK_TRUE(bte_init(), SYSTEM_ERROR_INTERNAL);
bt_coex_init();
Expand Down Expand Up @@ -1189,14 +1163,14 @@ int BleGap::stop() {
// but still allows rtw_coex_bt_enable to deinitialize BT coexistence.
// Subsequently we restore the state with rtwCoexRunEnable() and rtw_coex_wifi_enable
// will call into rtw_coex_run_enable(123, false) which will finally cleanup the mutex
rtwCoexRunDisable();
rtwCoexRunDisable(0);
rtw_coex_bt_enable(*(void**)rltk_wlan_info[0].dev->priv, 0);
HAL_Delay_Milliseconds(100);
rtwCoexRunEnable();
rtwCoexRunEnable(0);
rtw_coex_wifi_enable(*(void**)rltk_wlan_info[0].dev->priv, 0);
rtwCoexRunDisable();
rtwCoexRunDisable(0);
rtw_coex_wifi_enable(*(void**)rltk_wlan_info[0].dev->priv, 1);
rtwCoexCleanup();
rtwCoexCleanupMutex(0);
}
}
bte_deinit();
Expand Down
2 changes: 1 addition & 1 deletion hal/src/rtl872x/hal_platform_rtl8721x_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
// No SOF support :(
#define HAL_PLATFORM_USB_SOF (0)

#define HAL_PLATFORM_SYSTEM_POOL_SIZE 1024
#define HAL_PLATFORM_SYSTEM_POOL_SIZE 8192

#define HAL_PLATFORM_MODULE_SUFFIX_EXTENSIONS (1)

Expand Down
72 changes: 72 additions & 0 deletions hal/src/rtl872x/rtl_sdk_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,78 @@ int _rtl_sscanf(const char *buf, const char *fmt, ...);

}

struct pcoex_reveng {
uint32_t state;
uint32_t unknown;
_mutex* mutex;
};

extern "C" pcoex_reveng* pcoex[4];

extern "C" int rtw_coex_wifi_enable(void* priv, uint32_t state);
extern "C" int rtw_coex_bt_enable(void* priv, uint32_t state);

void rtwCoexRunDisable(int idx) {
os_thread_scheduling(false, nullptr);
auto p = pcoex[idx];
if (p && (p->state & 0x000000ff) != 0x00) {
p->state &= 0xffffff00;
}
os_thread_scheduling(true, nullptr);
}

void rtwCoexPreventCleanup(int idx) {
os_thread_scheduling(false, nullptr);
auto p = pcoex[idx];
if (p) {
p->state = 0xff00ff00;
}
os_thread_scheduling(true, nullptr);
}

void rtwCoexRunEnable(int idx) {
os_thread_scheduling(false, nullptr);
auto p = pcoex[idx];
if (p) {
p->state |= 0x01;
}
os_thread_scheduling(true, nullptr);
}

void rtwCoexCleanupMutex(int idx) {
int start = idx;
int end = idx;
if (idx < 0) {
start = 0;
end = sizeof(pcoex)/sizeof(pcoex[0]) - 1;
}
for (int i = start; i <= end; i++) {
auto p = pcoex[i];
if (p && p->mutex) {
auto m = p->mutex;
p->mutex = nullptr;
rtw_mutex_free(m);
}
}
}

void rtwCoexCleanup(int idx) {
rtwCoexCleanupMutex(idx);
int start = idx;
int end = idx;
if (idx < 0) {
start = 0;
end = sizeof(pcoex)/sizeof(pcoex[0]) - 1;
}
for (int i = start; i <= end; i++) {
if (pcoex[i]) {
free(pcoex[i]);
pcoex[i] = nullptr;
}
}
}


extern "C" u32 DiagPrintf(const char *fmt, ...);
extern "C" int DiagVSprintf(char *buf, const char *fmt, const int *dp);
extern u32 ConfigDebugBuffer;
Expand Down
6 changes: 6 additions & 0 deletions hal/src/rtl872x/rtl_sdk_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ void ipc_table_init(void);
void ipc_send_message(uint8_t channel, uint32_t message);
uint32_t ipc_get_message(uint8_t channel);

void rtwCoexRunDisable(int idx);
void rtwCoexPreventCleanup(int idx);
void rtwCoexRunEnable(int idx);
void rtwCoexCleanup(int idx);
void rtwCoexCleanupMutex(int idx);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion third_party/freertos/freertos