Skip to content

Pelion enable for UNO_91H #11888

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

Merged
merged 12 commits into from
Nov 20, 2019
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
155 changes: 149 additions & 6 deletions features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,45 @@
#include "wland_types.h"
#include "rda_sys_wrapper.h"

typedef enum {
WIFI_CONNECTED,
WIFI_DISCONNECTED,
}WIFI_STATE;

static WIFI_STATE wifi_state = WIFI_DISCONNECTED;

void daemon(void *para)
{
void *main_msgQ = NULL;
rda_msg msg;
int ret;
RDAWiFiInterface *wifi = (RDAWiFiInterface *)para;
main_msgQ = rda_mail_create(10, sizeof(unsigned int)*4);
wifi->set_msg_queue(main_msgQ);
while(1){
rda_mail_get(main_msgQ, (void*)&msg, osWaitForever);
switch(msg.type)
{
case MAIN_RECONNECT:
printf("wifi disconnect!\r\n");
ret = wifi->disconnect();
if(ret != 0){
printf("disconnect failed!\r\n");
break;
}
ret = wifi->reconnect();
while(ret != 0){
osDelay(5*1000);
ret = wifi->reconnect();
};
break;
default:
printf("unknown msg\r\n");
break;
}
}
}

nsapi_error_t RDAWiFiInterface::set_channel(uint8_t channel)
{
int ret= 0;
Expand Down Expand Up @@ -52,14 +91,15 @@ nsapi_error_t RDAWiFiInterface::init()
{
if (!_interface) {
if (!_emac.power_up()) {
LWIP_DEBUGF(NETIF_DEBUG,"power up failed!\n");
LWIP_DEBUGF(NETIF_DEBUG,("power up failed!\n"));
}
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
if (err != NSAPI_ERROR_OK) {
_interface = NULL;
return err;
}
_interface->attach(_connection_status_cb);
//rda_thread_new("daemon", daemon, this, DEFAULT_THREAD_STACKSIZE*4, osPriorityNormal);
}
return NSAPI_ERROR_OK;
}
Expand Down Expand Up @@ -93,12 +133,22 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
rda5981_scan_result bss;
int ret = 0;

if(wifi_state == WIFI_CONNECTED) {
return NSAPI_ERROR_IS_CONNECTED;
}

if (ssid == NULL || ssid[0] == 0) {
return NSAPI_ERROR_PARAMETER;
}


set_credentials(ssid, pass, security);
set_channel(channel);

init();

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

if (find == false) {
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
return NSAPI_ERROR_CONNECTION_TIMEOUT;
LWIP_DEBUGF(NETIF_DEBUG,("can not find the ap.\r\n"));
return NSAPI_ERROR_NO_SSID;
}
bss.channel = 15;
rda5981_get_scan_result_name(&bss, ssid);
if ((channel !=0) && (bss.channel != channel)) {
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
LWIP_DEBUGF(NETIF_DEBUG, ("invalid channel\r\n"));
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}

Expand All @@ -135,13 +185,23 @@ nsapi_error_t RDAWiFiInterface::connect(const char *ssid, const char *pass,
if (ret) {
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}

wifi_state = WIFI_CONNECTED;

ret = _interface->bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0,
DEFAULT_STACK,
_blocking);
LWIP_DEBUGF(NETIF_DEBUG,("Interface bringup up status:%d\r\n",ret));

if( ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IS_CONNECTED ) {
ret = NSAPI_ERROR_OK;
}
else if( ret == NSAPI_ERROR_DHCP_FAILURE) {
disconnect();
}

return ret;
}
Expand All @@ -156,9 +216,10 @@ nsapi_error_t RDAWiFiInterface::disconnect()
{
rda_msg msg;

if(sta_state < 2) {
if(wifi_state == WIFI_DISCONNECTED) {
return NSAPI_ERROR_NO_CONNECTION;
}
wifi_state = WIFI_DISCONNECTED;
void* wifi_disconnect_sem = rda_sem_create(0);
msg.type = WLAND_DISCONNECT;
msg.arg1 = (unsigned int)wifi_disconnect_sem;
Expand All @@ -172,6 +233,81 @@ nsapi_error_t RDAWiFiInterface::disconnect()
return NSAPI_ERROR_NO_CONNECTION;
}

nsapi_error_t RDAWiFiInterface::reconnect()
{
rda_msg msg;
bool find = false;
int i = 0;
rda5981_scan_result bss;
int ret = 0;

if (_ssid == NULL || _ssid[0] == 0) {
return NSAPI_ERROR_PARAMETER;
}

rda5981_del_scan_all_result();
if(rda5981_check_scan_result_name(_ssid) != 0) {
for (i = 0; i< 5; i++) {
rda5981_scan(NULL, 0, 0);
if(rda5981_check_scan_result_name(_ssid) == 0) {
find = true;
break;
}
}
} else {
find = true;
}

if (find == false) {
LWIP_DEBUGF(NETIF_DEBUG,"can not find the ap.\r\n");
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}
bss.channel = 15;
rda5981_get_scan_result_name(&bss, _ssid);
if ((_channel !=0) && (bss.channel != _channel)) {
LWIP_DEBUGF(NETIF_DEBUG, "invalid channel\r\n");
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}

memcpy(gssid, _ssid, strlen(_ssid));
if (_pass[0] != 0) {
memcpy(gpass, _pass, strlen(_pass));
}
memset(gbssid, 0, NSAPI_MAC_BYTES);
gssid[strlen(_ssid)] = gpass[strlen(_pass)] = '\0';

msg.type = WLAND_CONNECT;
rda_mail_put(wland_msgQ, (void*)&msg, osWaitForever);
ret = rda_sem_wait(wifi_auth_sem, 10000);
if (ret) {
return NSAPI_ERROR_CONNECTION_TIMEOUT;
}

if(_dhcp) {
memset(_ip_address, 0, sizeof(_ip_address));
memset(_netmask, 0, sizeof(_netmask));
memset(_gateway, 0, sizeof(_gateway));
}

ret = _interface->bringup(_dhcp,
_ip_address[0] ? _ip_address : 0,
_netmask[0] ? _netmask : 0,
_gateway[0] ? _gateway : 0,
DEFAULT_STACK,
_blocking);
LWIP_DEBUGF(NETIF_DEBUG,("Interface bringup up status:%d\r\n",ret));

if( ret == NSAPI_ERROR_OK || ret == NSAPI_ERROR_IS_CONNECTED ) {
ret = NSAPI_ERROR_OK;
wifi_state = WIFI_CONNECTED;
}
else if( ret == NSAPI_ERROR_DHCP_FAILURE) {
disconnect();
}
return ret;
}


nsapi_size_or_error_t RDAWiFiInterface::scan(WiFiAccessPoint *res, nsapi_size_t count)
{
int bss_num = 0, i;
Expand Down Expand Up @@ -219,3 +355,10 @@ WiFiInterface *WiFiInterface::get_default_instance() {
static RDAWiFiInterface wifinet;
return &wifinet;
}

nsapi_size_or_error_t RDAWiFiInterface::set_msg_queue(void *queue)
{
//TO_DO: No need for 1st stage since application already control the logic.
//rda5981_set_main_queue(queue);
return 0;
}
11 changes: 11 additions & 0 deletions features/netsocket/emac-drivers/TARGET_RDA_EMAC/RdaWiFiInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ class RDAWiFiInterface : public EMACInterface, public WiFiInterface
*/
virtual nsapi_error_t disconnect();

/** Restart the interface
*
* Attempts to reconnect to a WiFi network. Ssid and passphrase has been stored.
*
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t reconnect();

/** Scan for available networks
*
* This function will block. If the @a count is 0, function will only return count of available networks, so that
Expand All @@ -121,6 +129,9 @@ class RDAWiFiInterface : public EMACInterface, public WiFiInterface
virtual nsapi_size_or_error_t scan(WiFiAccessPoint *res, nsapi_size_t count);

virtual nsapi_size_or_error_t init();

virtual nsapi_size_or_error_t set_msg_queue(void *queue);

private:
char _ssid[33];
char _pass[65];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

//#include "cmsis.h"
//#include <stdint.h>
#include "rda_ccfg_api.h"

extern void rda_ccfg_ckrst(void);
extern void rda_wdt_softreset(void);
#ifdef __cplusplus
extern "C" {
Expand All @@ -38,7 +38,7 @@ extern "C" {
*/
__NO_RETURN __STATIC_INLINE void __RDA5981_SystemReset(void)
{
rda_ccfg_perrst();
rda_ccfg_ckrst () ;
__DSB(); /* Ensure all outstanding memory accesses included
buffered write are completed before reset */
Expand Down
5 changes: 4 additions & 1 deletion targets/TARGET_RDA/TARGET_UNO_91H/device/system_RDA5991H.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ void SystemInit (void)
#if ((__FPU_PRESENT == 1) && (__FPU_USED == 1))
SCB->CPACR |= ((3UL << 10*2) | (3UL << 11*2)); /* set CP10, CP11 Full Access */
#endif /* ((__FPU_PRESENT == 1) && (__FPU_USED == 1)) */
SCB->VTOR = RDA_CODE_BASE; /* vector table in flash */
#ifndef APPLICATION_ADDR
#define APPLICATION_ADDR RDA_CODE_BASE /* vector table in flash, add support for bootloader jump */
#endif
SCB->VTOR = APPLICATION_ADDR;
NVIC_SetPriorityGrouping(0x06); /* 1 bit for pre-emption pri */

__enable_irq();
Expand Down
4 changes: 2 additions & 2 deletions targets/TARGET_RDA/TARGET_UNO_91H/flash_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define MBED_FLASH_SIZE 0x100000
#else
//there is 4K BOOTROM at beginning of the flash
#define MBED_FLASH_SIZE (MBED_ROM_SIZE+0x1000)
#define MBED_FLASH_SIZE MBED_ROM_SIZE
#endif


Expand Down Expand Up @@ -75,7 +75,7 @@ static const sector_info_t sectors_info[] = {

static const flash_target_config_t flash_target_config = {
.page_size = 0x100,
.flash_start = 0x18000000,
.flash_start = 0x18001000,
.flash_size = MBED_FLASH_SIZE,
.sectors = sectors_info,
.sector_info_count = sizeof(sectors_info) / sizeof(sector_info_t)
Expand Down
8 changes: 8 additions & 0 deletions targets/TARGET_RDA/TARGET_UNO_91H/rda_ccfg_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ static inline void rda_ccfg_ck(void)

cfg = (RDA_SCU->CORECFG >> 11) & 0x07U;
rd_rf_usb_reg(0xA4, &val, 0);

// If i2c_wakeup_en is already set, do nothing and return
if((val & 0x01U) == 0x00U) {
return;
}

#if ((SYS_CPU_CLK == CLK_FREQ_160M) && (AHB_BUS_CLK == CLK_FREQ_80M))
/* HCLK inv */
if(((CLK_FREQ_40M << 1) | CLK_FREQ_40M) == cfg) {
Expand Down Expand Up @@ -227,6 +233,8 @@ void rda_ccfg_ckrst(void)
/* Config BUS clock */
val &= ~(0x01U << 9);
val |= (0x00U << 9); /* 1'b0:40M, 1'b1:80M */

val |= (0x01U); /* clear i2c_wakeup_en */
wr_rf_usb_reg(0xA4, val, 0);
}

Expand Down
8 changes: 4 additions & 4 deletions targets/TARGET_RDA/TARGET_UNO_91H/serial_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ int serial_getc(serial_t *obj)

void serial_putc(serial_t *obj, int c)
{
while (serial_writable(obj));
while (!serial_writable(obj));
obj->uart->THR = c;
}

Expand All @@ -334,12 +334,12 @@ int serial_writable(serial_t *obj)
{
int isWritable = 1;
if (obj->index == 0) {
return (obj->uart->FSR & TXFIFO_FULL_MASK); // uart0 not have flow control
return !(obj->uart->FSR & TXFIFO_FULL_MASK); // uart0 not have flow control
} else {
if (((obj->uart->MCR & AFCE_MASK) == 0x00UL) && (NC != uart_data[obj->index].sw_cts.pin)) //If flow control: writable if CTS low + UART done
isWritable = (gpio_read(&uart_data[obj->index].sw_cts) == 0) && (obj->uart->FSR & TXFIFO_FULL_MASK);
isWritable = (gpio_read(&uart_data[obj->index].sw_cts) == 0) && !(obj->uart->FSR & TXFIFO_FULL_MASK);
else
isWritable = (obj->uart->FSR & TXFIFO_FULL_MASK);
isWritable = !(obj->uart->FSR & TXFIFO_FULL_MASK);
return isWritable;
}
}
Expand Down
1 change: 0 additions & 1 deletion targets/TARGET_RDA/TARGET_UNO_91H/us_ticker.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ extern void rda_timer_irq_set(void);
void us_ticker_init(void)
{
if (us_ticker_inited) {
us_ticker_disable_interrupt();
return;
}

Expand Down