Skip to content

Commit 75f17d4

Browse files
M-ichae-lkjbracey
authored andcommitted
add-rtl8195am-feature-emac (#6904)
rtl8195am feature emac implementation.
1 parent 8dd512b commit 75f17d4

File tree

10 files changed

+399
-241
lines changed

10 files changed

+399
-241
lines changed

TESTS/network/emac/template_mbed_app.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"echo-server-trace": {
1616
"help": "Trace incoming messages on echo server",
1717
"value": 0
18-
},
18+
},
1919
"wifi-scan": {
2020
"help": "Scan and list access points",
2121
"value": 0

targets/TARGET_Realtek/TARGET_AMEBA/.mbedignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.cpp

Lines changed: 68 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919

2020
#include "RTWInterface.h"
2121
#include "mbed_interface.h"
22-
#include "rtw_emac.h"
2322

23+
#include "rtw_emac.h"
24+
#include "EMAC.h"
2425
#include "wifi_constants.h"
2526
#include "wifi_conf.h"
26-
#include "lwip_stack.h"
2727

28+
#include "OnboardNetworkStack.h"
29+
#include "EMACMemoryManager.h"
2830
#include "osdep_service.h"
2931

3032
typedef struct _wifi_scan_hdl {
@@ -42,14 +44,14 @@ static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_sca
4244
{
4345
wifi_scan_hdl *scan_handler = (wifi_scan_hdl *)malloced_scan_result->user_data;
4446
if (malloced_scan_result->scan_complete != RTW_TRUE) {
45-
if(scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num){
47+
if (scan_handler->ap_details && scan_handler->scan_num > scan_handler->ap_num) {
4648
nsapi_wifi_ap_t ap;
4749
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
4850
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
4951
memset((void*)&ap, 0x00, sizeof(nsapi_wifi_ap_t));
5052
memcpy(ap.ssid, record->SSID.val, record->SSID.len);
5153
memcpy(ap.bssid, record->BSSID.octet, 6);
52-
switch (record->security){
54+
switch (record->security) {
5355
case RTW_SECURITY_OPEN:
5456
ap.security = NSAPI_SECURITY_NONE;
5557
break;
@@ -78,41 +80,30 @@ static rtw_result_t scan_result_handler( rtw_scan_handler_result_t* malloced_sca
7880
scan_handler->ap_details[scan_handler->ap_num] = WiFiAccessPoint(ap);
7981
}
8082
scan_handler->ap_num++;
81-
} else{
83+
} else {
8284
// scan done
8385
rtw_up_sema(&scan_handler->scan_sema);
8486
}
8587
return RTW_SUCCESS;
8688
}
8789

88-
RTWInterface::RTWInterface(bool debug)
89-
: _dhcp(true), _ssid(), _pass(), _ip_address(), _netmask(), _gateway()
90+
RTWInterface::RTWInterface(RTW_EMAC &get_rtw_emac, OnboardNetworkStack &get_rtw_obn_stack) :
91+
rtw_emac(get_rtw_emac),
92+
rtw_obn_stack(get_rtw_obn_stack),
93+
rtw_interface(NULL),
94+
_dhcp(true),
95+
_ip_address(),
96+
_netmask(),
97+
_gateway(),
98+
_mac_address()
9099
{
91-
emac_interface_t *emac;
92-
int ret;
93-
extern u32 GlobalDebugEnable;
94-
95-
GlobalDebugEnable = debug?1:0;
96-
emac = wlan_emac_init_interface();
97-
if (!emac) {
98-
printf("Error init RTWInterface!\r\n");
99-
return;
100-
}
101-
emac->ops.power_up(emac);
102-
if (_inited == false) {
103-
ret = mbed_lwip_init(emac);
104-
if (ret != 0) {
105-
printf("Error init RTWInterface!(%d)\r\n", ret);
106-
return;
107-
}
108-
_inited = true;
109-
}
100+
rtw_emac.power_up();
110101
}
111102

112103
RTWInterface::~RTWInterface()
113104
{
114-
wlan_emac_link_change(false);
115-
mbed_lwip_bringdown();
105+
rtw_emac.wlan_emac_link_change(false);
106+
rtw_interface->bringdown();
116107
}
117108

118109
nsapi_error_t RTWInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
@@ -135,7 +126,7 @@ nsapi_error_t RTWInterface::set_dhcp(bool dhcp)
135126
*/
136127
nsapi_error_t RTWInterface::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
137128
{
138-
if(!ssid || (strlen(ssid) == 0)) {
129+
if (!ssid) {
139130
return NSAPI_ERROR_PARAMETER;
140131
}
141132

@@ -144,14 +135,11 @@ nsapi_error_t RTWInterface::set_credentials(const char *ssid, const char *pass,
144135
case NSAPI_SECURITY_WPA2:
145136
case NSAPI_SECURITY_WPA_WPA2:
146137
case NSAPI_SECURITY_WEP:
147-
if((strlen(pass) < 8) || (strlen(pass) > 63)) { // 802.11 password 8-63 characters
138+
if ((strlen(pass) < 8) || (strlen(pass) > 63)) { // 802.11 password 8-63 characters
148139
return NSAPI_ERROR_PARAMETER;
149140
}
150141
break;
151142
case NSAPI_SECURITY_NONE:
152-
if(pass && strlen(pass) > 0) {
153-
return NSAPI_ERROR_PARAMETER;
154-
}
155143
break;
156144
default:
157145
return NSAPI_ERROR_PARAMETER;
@@ -169,8 +157,7 @@ nsapi_error_t RTWInterface::connect()
169157
int ret;
170158
rtw_security_t sec;
171159

172-
if (!_ssid || (strlen(_ssid) == 0) ||
173-
(!_pass && _security != NSAPI_SECURITY_NONE)) {
160+
if (!_ssid || (!_pass && _security != NSAPI_SECURITY_NONE)) {
174161
printf("Invalid credentials\r\n");
175162
return NSAPI_ERROR_PARAMETER;
176163
}
@@ -191,85 +178,94 @@ nsapi_error_t RTWInterface::connect()
191178
return NSAPI_ERROR_PARAMETER;
192179
}
193180

181+
if (_channel > 0 && _channel < 14) {
182+
uint8_t pscan_config = PSCAN_ENABLE;
183+
wifi_set_pscan_chan(&_channel, &pscan_config, 1);
184+
}
185+
194186
ret = wifi_connect(_ssid, sec, _pass, strlen(_ssid), strlen(_pass), 0, (void *)NULL);
195187
if (ret != RTW_SUCCESS) {
196188
printf("failed: %d\r\n", ret);
197189
return NSAPI_ERROR_NO_CONNECTION;
198190
}
199191

200-
wlan_emac_link_change(true);
201-
return mbed_lwip_bringup(_dhcp,
202-
_ip_address[0] ? _ip_address : 0,
203-
_netmask[0] ? _netmask : 0,
204-
_gateway[0] ? _gateway : 0);
192+
rtw_emac.wlan_emac_link_change(true);
193+
if (!rtw_interface) {
194+
nsapi_error_t err = rtw_obn_stack.add_ethernet_interface(rtw_emac, true, &rtw_interface);
195+
if (err != NSAPI_ERROR_OK) {
196+
rtw_interface = NULL;
197+
return err;
198+
}
199+
}
200+
201+
int rtw_if_bringup = rtw_interface->bringup(_dhcp,
202+
_ip_address[0] ? _ip_address : 0,
203+
_netmask[0] ? _netmask : 0,
204+
_gateway[0] ? _gateway : 0,
205+
DEFAULT_STACK);
206+
return rtw_if_bringup;
205207
}
206208

207209
nsapi_error_t RTWInterface::scan(WiFiAccessPoint *res, unsigned count)
208210
{
209211
static wifi_scan_hdl scan_handler;
210212
scan_handler.ap_num = 0;
211-
if(!scan_handler.scan_sema)
213+
if (!scan_handler.scan_sema) {
212214
rtw_init_sema(&scan_handler.scan_sema, 0);
215+
}
213216
scan_handler.scan_num = count;
214217
scan_handler.ap_details = res;
215-
if(wifi_scan_networks(scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS){
218+
if (wifi_scan_networks(scan_result_handler, (void *)&scan_handler) != RTW_SUCCESS) {
216219
printf("wifi scan failed\n\r");
217220
return NSAPI_ERROR_DEVICE_ERROR;
218221
}
219-
if(rtw_down_timeout_sema( &scan_handler.scan_sema, MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
222+
if (rtw_down_timeout_sema( &scan_handler.scan_sema, MAX_SCAN_TIMEOUT ) == RTW_FALSE) {
220223
printf("wifi scan timeout\r\n");
221224
return NSAPI_ERROR_DEVICE_ERROR;
222225
}
223-
if(count <= 0 || count > scan_handler.ap_num)
226+
if (count <= 0 || count > scan_handler.ap_num) {
224227
count = scan_handler.ap_num;
225-
228+
}
226229
return count;
227230
}
228231

229232
nsapi_error_t RTWInterface::set_channel(uint8_t channel)
230233
{
231-
// Not supported for STA mode wifi driver
232-
if (channel != 0)
233-
return NSAPI_ERROR_UNSUPPORTED;
234-
234+
_channel = channel;
235235
return NSAPI_ERROR_OK;
236236
}
237237

238238
int8_t RTWInterface::get_rssi()
239239
{
240240
int rssi = 0;
241-
if(wifi_get_rssi(&rssi) == 0)
241+
if (wifi_get_rssi(&rssi) == 0) {
242242
return (int8_t)rssi;
243+
}
243244
return NSAPI_ERROR_OK;
244245
}
245246

246247
nsapi_error_t RTWInterface::connect(const char *ssid, const char *pass,
247248
nsapi_security_t security, uint8_t channel)
248249
{
249-
nsapi_error_t ret;
250-
251-
ret = set_credentials(ssid, pass, security);
252-
if(ret != NSAPI_ERROR_OK) return ret;
253-
254-
ret = set_channel(channel);
255-
if(ret != NSAPI_ERROR_OK) return ret;
256-
250+
set_credentials(ssid, pass, security);
251+
set_channel(channel);
257252
return connect();
258253
}
259254

260255
nsapi_error_t RTWInterface::disconnect()
261256
{
262257
char essid[33];
263258

264-
wlan_emac_link_change(false);
265-
mbed_lwip_bringdown();
266-
if(wifi_is_connected_to_ap() != RTW_SUCCESS)
259+
rtw_emac.wlan_emac_link_change(false);
260+
rtw_interface->bringdown();
261+
if (wifi_is_connected_to_ap() != RTW_SUCCESS) {
267262
return NSAPI_ERROR_NO_CONNECTION;
268-
if(wifi_disconnect()<0){
263+
}
264+
if (wifi_disconnect()<0) {
269265
return NSAPI_ERROR_DEVICE_ERROR;
270266
}
271-
while(1){
272-
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
267+
while(1) {
268+
if (wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0) {
273269
break;
274270
}
275271
}
@@ -278,40 +274,42 @@ nsapi_error_t RTWInterface::disconnect()
278274

279275
int RTWInterface::is_connected()
280276
{
281-
// wifi_is_connected_to_ap return 0 on connected
282277
return !wifi_is_connected_to_ap();
283278
}
284279

285280
const char *RTWInterface::get_mac_address()
286281
{
287-
return mbed_lwip_get_mac_address();
282+
if (rtw_interface->get_mac_address(_mac_address, sizeof _mac_address)) {
283+
return _mac_address;
284+
}
285+
return 0;
288286
}
289287

290288
const char *RTWInterface::get_ip_address()
291289
{
292-
if (mbed_lwip_get_ip_address(_ip_address, sizeof _ip_address)) {
290+
if (rtw_interface->get_ip_address(_ip_address, sizeof _ip_address)) {
293291
return _ip_address;
294292
}
295293
return 0;
296294
}
297295

298296
const char *RTWInterface::get_netmask()
299297
{
300-
if (mbed_lwip_get_netmask(_netmask, sizeof _netmask)) {
298+
if (rtw_interface->get_netmask(_netmask, sizeof _netmask)) {
301299
return _netmask;
302300
}
303301
return 0;
304302
}
305303

306304
const char *RTWInterface::get_gateway()
307305
{
308-
if (mbed_lwip_get_gateway(_gateway, sizeof _gateway)) {
306+
if (rtw_interface->get_gateway(_gateway, sizeof _gateway)) {
309307
return _gateway;
310308
}
311309
return 0;
312310
}
313311

314312
NetworkStack *RTWInterface::get_stack()
315313
{
316-
return nsapi_create_stack(&lwip_stack);
314+
return &rtw_obn_stack;
317315
}

targets/TARGET_Realtek/TARGET_AMEBA/RTWInterface.h

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121
#include "netsocket/WiFiInterface.h"
2222
#include "nsapi.h"
2323
#include "rtos.h"
24-
#include "lwip/netif.h"
24+
#include "netif.h"
25+
#include "rtw_emac.h"
26+
#include "OnboardNetworkStack.h"
27+
#include "LWIPStack.h"
2528

2629
// Forward declaration
2730
class NetworkStack;
@@ -34,7 +37,10 @@ class RTWInterface: public WiFiInterface
3437
public:
3538
/** RTWWlanInterface lifetime
3639
*/
37-
RTWInterface(bool debug=false);
40+
RTWInterface(
41+
RTW_EMAC &rtw_emac = RTW_EMAC::get_instance(),
42+
OnboardNetworkStack &rtw_lwip_stack = OnboardNetworkStack::get_default_instance());
43+
3844
~RTWInterface();
3945

4046
/** Set a static IP address
@@ -143,21 +149,27 @@ class RTWInterface: public WiFiInterface
143149
*/
144150
virtual const char *get_gateway();
145151

152+
RTW_EMAC &get_emac() const { return rtw_emac; }
153+
154+
virtual RTWInterface *rtwInterface() { return this; }
155+
146156
protected:
147157
/** Provide access to the underlying stack
148158
*
149159
* @return The underlying network stack
150160
*/
151161
virtual NetworkStack *get_stack();
152-
162+
RTW_EMAC &rtw_emac;
163+
OnboardNetworkStack &rtw_obn_stack;
164+
OnboardNetworkStack::Interface *rtw_interface;
153165
bool _dhcp;
154166
char _ssid[256];
155167
char _pass[256];
156168
nsapi_security_t _security;
157169
uint8_t _channel;
158170
char _ip_address[IPADDR_STRLEN_MAX];
159171
char _netmask[NSAPI_IPv4_SIZE];
160-
char _gateway[NSAPI_IPv4_SIZE];
172+
char _gateway[NSAPI_IPv4_SIZE];
173+
char _mac_address[NSAPI_MAC_SIZE];
161174
};
162-
163175
#endif

0 commit comments

Comments
 (0)