Skip to content

Commit 9dc0c37

Browse files
authored
Merge pull request #11888 from fredlee12001/master
Pelion enable for UNO_91H
2 parents 48f38a4 + 72c5c78 commit 9dc0c37

File tree

8 files changed

+180
-16
lines changed

8 files changed

+180
-16
lines changed

features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.cpp

Lines changed: 149 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,45 @@
2121
#include "wland_types.h"
2222
#include "rda_sys_wrapper.h"
2323

24+
typedef enum {
25+
WIFI_CONNECTED,
26+
WIFI_DISCONNECTED,
27+
}WIFI_STATE;
28+
29+
static WIFI_STATE wifi_state = WIFI_DISCONNECTED;
30+
31+
void daemon(void *para)
32+
{
33+
void *main_msgQ = NULL;
34+
rda_msg msg;
35+
int ret;
36+
RDAWiFiInterface *wifi = (RDAWiFiInterface *)para;
37+
main_msgQ = rda_mail_create(10, sizeof(unsigned int)*4);
38+
wifi->set_msg_queue(main_msgQ);
39+
while(1){
40+
rda_mail_get(main_msgQ, (void*)&msg, osWaitForever);
41+
switch(msg.type)
42+
{
43+
case MAIN_RECONNECT:
44+
printf("wifi disconnect!\r\n");
45+
ret = wifi->disconnect();
46+
if(ret != 0){
47+
printf("disconnect failed!\r\n");
48+
break;
49+
}
50+
ret = wifi->reconnect();
51+
while(ret != 0){
52+
osDelay(5*1000);
53+
ret = wifi->reconnect();
54+
};
55+
break;
56+
default:
57+
printf("unknown msg\r\n");
58+
break;
59+
}
60+
}
61+
}
62+
2463
nsapi_error_t RDAWiFiInterface::set_channel(uint8_t channel)
2564
{
2665
int ret= 0;
@@ -52,14 +91,15 @@ nsapi_error_t RDAWiFiInterface::init()
5291
{
5392
if (!_interface) {
5493
if (!_emac.power_up()) {
55-
LWIP_DEBUGF(NETIF_DEBUG,"power up failed!\n");
94+
LWIP_DEBUGF(NETIF_DEBUG,("power up failed!\n"));
5695
}
5796
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
5897
if (err != NSAPI_ERROR_OK) {
5998
_interface = NULL;
6099
return err;
61100
}
62101
_interface->attach(_connection_status_cb);
102+
//rda_thread_new("daemon", daemon, this, DEFAULT_THREAD_STACKSIZE*4, osPriorityNormal);
63103
}
64104
return NSAPI_ERROR_OK;
65105
}
@@ -93,12 +133,22 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
93133
rda5981_scan_result bss;
94134
int ret = 0;
95135

136+
if(wifi_state == WIFI_CONNECTED) {
137+
return NSAPI_ERROR_IS_CONNECTED;
138+
}
139+
96140
if (ssid == NULL || ssid[0] == 0) {
97141
return NSAPI_ERROR_PARAMETER;
98142
}
99-
143+
144+
set_credentials(ssid, pass, security);
145+
set_channel(channel);
146+
100147
init();
101148

149+
//reset all scan result to avoid any previous stored SSID/PW/CHANNEL
150+
rda5981_del_scan_all_result();
151+
rda5981_scan(NULL,0,0);
102152
if(rda5981_check_scan_result_name(ssid) != 0) {
103153
for (i = 0; i< 5; i++) {
104154
rda5981_scan(NULL, 0, 0);
@@ -112,13 +162,13 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
112162
}
113163

114164
if (find == false) {
115-
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
116-
return NSAPI_ERROR_CONNECTION_TIMEOUT;
165+
LWIP_DEBUGF(NETIF_DEBUG,("can not find the ap.\r\n"));
166+
return NSAPI_ERROR_NO_SSID;
117167
}
118168
bss.channel = 15;
119169
rda5981_get_scan_result_name(&bss, ssid);
120170
if ((channel !=0) && (bss.channel != channel)) {
121-
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
171+
LWIP_DEBUGF(NETIF_DEBUG, ("invalid channel\r\n"));
122172
return NSAPI_ERROR_CONNECTION_TIMEOUT;
123173
}
124174

@@ -135,13 +185,23 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
135185
if (ret) {
136186
return NSAPI_ERROR_CONNECTION_TIMEOUT;
137187
}
188+
189+
wifi_state = WIFI_CONNECTED;
138190

139191
ret = _interface->bringup(_dhcp,
140192
_ip_address[0] ? _ip_address : 0,
141193
_netmask[0] ? _netmask : 0,
142194
_gateway[0] ? _gateway : 0,
143195
DEFAULT_STACK,
144196
_blocking);
197+
LWIP_DEBUGF(NETIF_DEBUG,("Interface bringup up status:%d\r\n",ret));
198+
199+
if( ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IS_CONNECTED ) {
200+
ret = NSAPI_ERROR_OK;
201+
}
202+
else if( ret == NSAPI_ERROR_DHCP_FAILURE) {
203+
disconnect();
204+
}
145205

146206
return ret;
147207
}
@@ -156,9 +216,10 @@ nsapi_error_t RDAWiFiInterface::disconnect()
156216
{
157217
rda_msg msg;
158218

159-
if(sta_state < 2) {
219+
if(wifi_state == WIFI_DISCONNECTED) {
160220
return NSAPI_ERROR_NO_CONNECTION;
161221
}
222+
wifi_state = WIFI_DISCONNECTED;
162223
void* wifi_disconnect_sem = rda_sem_create(0);
163224
msg.type = WLAND_DISCONNECT;
164225
msg.arg1 = (unsigned int)wifi_disconnect_sem;
@@ -172,6 +233,81 @@ nsapi_error_t RDAWiFiInterface::disconnect()
172233
return NSAPI_ERROR_NO_CONNECTION;
173234
}
174235

236+
nsapi_error_t RDAWiFiInterface::reconnect()
237+
{
238+
rda_msg msg;
239+
bool find = false;
240+
int i = 0;
241+
rda5981_scan_result bss;
242+
int ret = 0;
243+
244+
if (_ssid == NULL || _ssid[0] == 0) {
245+
return NSAPI_ERROR_PARAMETER;
246+
}
247+
248+
rda5981_del_scan_all_result();
249+
if(rda5981_check_scan_result_name(_ssid) != 0) {
250+
for (i = 0; i< 5; i++) {
251+
rda5981_scan(NULL, 0, 0);
252+
if(rda5981_check_scan_result_name(_ssid) == 0) {
253+
find = true;
254+
break;
255+
}
256+
}
257+
} else {
258+
find = true;
259+
}
260+
261+
if (find == false) {
262+
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
263+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
264+
}
265+
bss.channel = 15;
266+
rda5981_get_scan_result_name(&bss, _ssid);
267+
if ((_channel !=0) && (bss.channel != _channel)) {
268+
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
269+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
270+
}
271+
272+
memcpy(gssid, _ssid, strlen(_ssid));
273+
if (_pass[0] != 0) {
274+
memcpy(gpass, _pass, strlen(_pass));
275+
}
276+
memset(gbssid, 0, NSAPI_MAC_BYTES);
277+
gssid[strlen(_ssid)] = gpass[strlen(_pass)] = '\0';
278+
279+
msg.type = WLAND_CONNECT;
280+
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
281+
ret = rda_sem_wait(wifi_auth_sem, 10000);
282+
if (ret) {
283+
return NSAPI_ERROR_CONNECTION_TIMEOUT;
284+
}
285+
286+
if(_dhcp) {
287+
memset(_ip_address, 0, sizeof(_ip_address));
288+
memset(_netmask, 0, sizeof(_netmask));
289+
memset(_gateway, 0, sizeof(_gateway));
290+
}
291+
292+
ret = _interface->bringup(_dhcp,
293+
_ip_address[0] ? _ip_address : 0,
294+
_netmask[0] ? _netmask : 0,
295+
_gateway[0] ? _gateway : 0,
296+
DEFAULT_STACK,
297+
_blocking);
298+
LWIP_DEBUGF(NETIF_DEBUG,("Interface bringup up status:%d\r\n",ret));
299+
300+
if( ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IS_CONNECTED ) {
301+
ret = NSAPI_ERROR_OK;
302+
wifi_state = WIFI_CONNECTED;
303+
}
304+
else if( ret == NSAPI_ERROR_DHCP_FAILURE) {
305+
disconnect();
306+
}
307+
return ret;
308+
}
309+
310+
175311
nsapi_size_or_error_t RDAWiFiInterface::scan(WiFiAccessPoint *res, nsapi_size_t count)
176312
{
177313
int bss_num = 0, i;
@@ -219,3 +355,10 @@ WiFiInterface *WiFiInterface::get_default_instance() {
219355
static RDAWiFiInterface wifinet;
220356
return &wifinet;
221357
}
358+
359+
nsapi_size_or_error_t RDAWiFiInterface::set_msg_queue(void *queue)
360+
{
361+
//TO_DO: No need for 1st stage since application already control the logic.
362+
//rda5981_set_main_queue(queue);
363+
return 0;
364+
}

features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ class RDAWiFiInterface : public EMACInterface, public WiFiInterface
107107
*/
108108
virtual nsapi_error_t disconnect();
109109

110+
/** Restart the interface
111+
*
112+
* Attempts to reconnect to a WiFi network. Ssid and passphrase has been stored.
113+
*
114+
* @return 0 on success, negative error code on failure
115+
*/
116+
virtual nsapi_error_t reconnect();
117+
110118
/** Scan for available networks
111119
*
112120
* This function will block. If the @a count is 0, function will only return count of available networks, so that
@@ -121,6 +129,9 @@ class RDAWiFiInterface : public EMACInterface, public WiFiInterface
121129
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);
122130

123131
virtual nsapi_size_or_error_t init();
132+
133+
virtual nsapi_size_or_error_t set_msg_queue(void *queue);
134+
124135
private:
125136
char _ssid[33];
126137
char _pass[65];

targets/TARGET_RDA/TARGET_UNO_91H/device/RDA5981_nvic_virtual.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
//#include "cmsis.h"
2727
//#include <stdint.h>
28+
#include "rda_ccfg_api.h"
2829

29-
extern void rda_ccfg_ckrst(void);
3030
extern void rda_wdt_softreset(void);
3131
#ifdef __cplusplus
3232
extern "C" {
@@ -38,7 +38,7 @@ extern "C" {
3838
*/
3939
__NO_RETURN __STATIC_INLINE void __RDA5981_SystemReset(void)
4040
{
41-
41+
rda_ccfg_perrst();
4242
rda_ccfg_ckrst () ;
4343
__DSB(); /* Ensure all outstanding memory accesses included
4444
buffered write are completed before reset */

targets/TARGET_RDA/TARGET_UNO_91H/device/system_RDA5991H.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ void SystemInit (void)
173173
#if ((__FPU_PRESENT == 1) && (__FPU_USED == 1))
174174
SCB->CPACR |= ((3UL << 10*2) | (3UL << 11*2)); /* set CP10, CP11 Full Access */
175175
#endif /* ((__FPU_PRESENT == 1) && (__FPU_USED == 1)) */
176-
SCB->VTOR = RDA_CODE_BASE; /* vector table in flash */
176+
#ifndef APPLICATION_ADDR
177+
#define APPLICATION_ADDR RDA_CODE_BASE /* vector table in flash, add support for bootloader jump */
178+
#endif
179+
SCB->VTOR = APPLICATION_ADDR;
177180
NVIC_SetPriorityGrouping(0x06); /* 1 bit for pre-emption pri */
178181

179182
__enable_irq();

targets/TARGET_RDA/TARGET_UNO_91H/flash_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define MBED_FLASH_SIZE 0x100000
2323
#else
2424
//there is 4K BOOTROM at beginning of the flash
25-
#define MBED_FLASH_SIZE (MBED_ROM_SIZE+0x1000)
25+
#define MBED_FLASH_SIZE MBED_ROM_SIZE
2626
#endif
2727

2828

@@ -75,7 +75,7 @@ static const sector_info_t sectors_info[] = {
7575

7676
static const flash_target_config_t flash_target_config = {
7777
.page_size = 0x100,
78-
.flash_start = 0x18000000,
78+
.flash_start = 0x18001000,
7979
.flash_size = MBED_FLASH_SIZE,
8080
.sectors = sectors_info,
8181
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)

targets/TARGET_RDA/TARGET_UNO_91H/rda_ccfg_api.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ static inline void rda_ccfg_ck(void)
7878

7979
cfg = (RDA_SCU->CORECFG >> 11) & 0x07U;
8080
rd_rf_usb_reg(0xA4, &val, 0);
81+
82+
// If i2c_wakeup_en is already set, do nothing and return
83+
if((val & 0x01U) == 0x00U) {
84+
return;
85+
}
86+
8187
#if ((SYS_CPU_CLK == CLK_FREQ_160M) && (AHB_BUS_CLK == CLK_FREQ_80M))
8288
/* HCLK inv */
8389
if(((CLK_FREQ_40M << 1) | CLK_FREQ_40M) == cfg) {
@@ -227,6 +233,8 @@ void rda_ccfg_ckrst(void)
227233
/* Config BUS clock */
228234
val &= ~(0x01U << 9);
229235
val |= (0x00U << 9); /* 1'b0:40M, 1'b1:80M */
236+
237+
val |= (0x01U); /* clear i2c_wakeup_en */
230238
wr_rf_usb_reg(0xA4, val, 0);
231239
}
232240

targets/TARGET_RDA/TARGET_UNO_91H/serial_api.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ int serial_getc(serial_t *obj)
321321

322322
void serial_putc(serial_t *obj, int c)
323323
{
324-
while (serial_writable(obj));
324+
while (!serial_writable(obj));
325325
obj->uart->THR = c;
326326
}
327327

@@ -334,12 +334,12 @@ int serial_writable(serial_t *obj)
334334
{
335335
int isWritable = 1;
336336
if (obj->index == 0) {
337-
return (obj->uart->FSR & TXFIFO_FULL_MASK); // uart0 not have flow control
337+
return !(obj->uart->FSR & TXFIFO_FULL_MASK); // uart0 not have flow control
338338
} else {
339339
if (((obj->uart->MCR & AFCE_MASK) == 0x00UL) && (NC != uart_data[obj->index].sw_cts.pin)) //If flow control: writable if CTS low + UART done
340-
isWritable = (gpio_read(&uart_data[obj->index].sw_cts) == 0) && (obj->uart->FSR & TXFIFO_FULL_MASK);
340+
isWritable = (gpio_read(&uart_data[obj->index].sw_cts) == 0) && !(obj->uart->FSR & TXFIFO_FULL_MASK);
341341
else
342-
isWritable = (obj->uart->FSR & TXFIFO_FULL_MASK);
342+
isWritable = !(obj->uart->FSR & TXFIFO_FULL_MASK);
343343
return isWritable;
344344
}
345345
}

targets/TARGET_RDA/TARGET_UNO_91H/us_ticker.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ extern void rda_timer_irq_set(void);
4141
void us_ticker_init(void)
4242
{
4343
if (us_ticker_inited) {
44-
us_ticker_disable_interrupt();
4544
return;
4645
}
4746

0 commit comments

Comments
 (0)