提交 646cb6ca 编写于 作者: 还_没_想_好's avatar 还_没_想_好 提交者: 还_没_想_好

[bsp][amebaz] BSP工程更新,适配新的WLAN框架

上级 1699a12b
......@@ -13,15 +13,24 @@ CONFIG_RT_THREAD_PRIORITY_32=y
# CONFIG_RT_THREAD_PRIORITY_256 is not set
CONFIG_RT_THREAD_PRIORITY_MAX=32
CONFIG_RT_TICK_PER_SECOND=1000
CONFIG_RT_DEBUG=y
CONFIG_RT_USING_OVERFLOW_CHECK=y
CONFIG_RT_DEBUG_INIT=0
CONFIG_RT_DEBUG_THREAD=0
CONFIG_RT_USING_HOOK=y
CONFIG_RT_IDEL_HOOK_LIST_SIZE=4
CONFIG_IDLE_THREAD_STACK_SIZE=256
CONFIG_RT_USING_TIMER_SOFT=y
CONFIG_RT_TIMER_THREAD_PRIO=4
CONFIG_RT_TIMER_THREAD_STACK_SIZE=512
CONFIG_RT_DEBUG=y
# CONFIG_RT_DEBUG_INIT_CONFIG is not set
# CONFIG_RT_DEBUG_THREAD_CONFIG is not set
# CONFIG_RT_DEBUG_SCHEDULER_CONFIG is not set
# CONFIG_RT_DEBUG_IPC_CONFIG is not set
# CONFIG_RT_DEBUG_TIMER_CONFIG is not set
# CONFIG_RT_DEBUG_IRQ_CONFIG is not set
# CONFIG_RT_DEBUG_MEM_CONFIG is not set
# CONFIG_RT_DEBUG_SLAB_CONFIG is not set
# CONFIG_RT_DEBUG_MEMHEAP_CONFIG is not set
# CONFIG_RT_DEBUG_MODULE_CONFIG is not set
#
# Inter-Thread communication
......@@ -53,13 +62,14 @@ CONFIG_RT_USING_DEVICE=y
CONFIG_RT_USING_CONSOLE=y
CONFIG_RT_CONSOLEBUF_SIZE=128
CONFIG_RT_CONSOLE_DEVICE_NAME="dbg"
# CONFIG_RT_USING_MODULE is not set
#
# RT-Thread Components
#
CONFIG_RT_USING_COMPONENTS_INIT=y
# CONFIG_RT_USING_USER_MAIN is not set
CONFIG_RT_USING_USER_MAIN=y
CONFIG_RT_MAIN_THREAD_STACK_SIZE=2048
CONFIG_RT_MAIN_THREAD_PRIORITY=10
#
# C++ features
......@@ -82,7 +92,7 @@ CONFIG_FINSH_CMD_SIZE=80
# CONFIG_FINSH_USING_AUTH is not set
CONFIG_FINSH_USING_MSH=y
CONFIG_FINSH_USING_MSH_DEFAULT=y
# CONFIG_FINSH_USING_MSH_ONLY is not set
CONFIG_FINSH_USING_MSH_ONLY=y
CONFIG_FINSH_ARG_MAX=10
#
......@@ -94,26 +104,49 @@ CONFIG_FINSH_ARG_MAX=10
# Device Drivers
#
CONFIG_RT_USING_DEVICE_IPC=y
CONFIG_RT_PIPE_BUFSZ=512
CONFIG_RT_USING_SERIAL=y
# CONFIG_RT_USING_CAN is not set
# CONFIG_RT_USING_HWTIMER is not set
# CONFIG_RT_USING_CPUTIME is not set
# CONFIG_RT_USING_I2C is not set
CONFIG_RT_USING_PIN=y
# CONFIG_RT_USING_PIN is not set
# CONFIG_RT_USING_PWM is not set
# CONFIG_RT_USING_MTD_NOR is not set
# CONFIG_RT_USING_MTD_NAND is not set
# CONFIG_RT_USING_MTD is not set
# CONFIG_RT_USING_RTC is not set
# CONFIG_RT_USING_SDIO is not set
# CONFIG_RT_USING_SPI is not set
# CONFIG_RT_USING_WDT is not set
CONFIG_RT_USING_WIFI=y
CONFIG_RT_USING_WLAN_STA=y
CONFIG_RT_USING_WLAN_AP=y
CONFIG_WIFI_DEVICE_STA_NAME="w0"
CONFIG_WIFI_DEVICE_AP_NAME="ap"
# CONFIG_RT_USING_AUDIO is not set
#
# Using WiFi
#
CONFIG_RT_USING_WIFI=y
CONFIG_RT_WLAN_DEVICE_STA_NAME="wlan0"
CONFIG_RT_WLAN_DEVICE_AP_NAME="wlan1"
CONFIG_RT_WLAN_DEFAULT_PROT="lwip"
CONFIG_RT_WLAN_SCAN_WAIT_MS=10000
CONFIG_RT_WLAN_CONNECT_WAIT_MS=10000
CONFIG_RT_WLAN_SSID_MAX_LENGTH=32
CONFIG_RT_WLAN_PASSWORD_MAX_LENGTH=32
CONFIG_RT_WLAN_SCAN_SORT=y
CONFIG_RT_WLAN_CFG_INFO_MAX=3
CONFIG_RT_WLAN_WORKQUEUE_THREAD_NAME="wlan_job"
CONFIG_RT_WLAN_WORKQUEUE_THREAD_SIZE=2048
CONFIG_RT_WLAN_WORKQUEUE_THREAD_PRIO=22
CONFIG_RT_WLAN_DEV_EVENT_NUM=2
# CONFIG_RT_WLAN_PROT_LWIP_PBUF_FORCE is not set
CONFIG_RT_WLAN_DEBUG=y
CONFIG_RT_WLAN_CMD_DEBUG=y
# CONFIG_RT_WLAN_MGNT_DEBUG is not set
# CONFIG_RT_WLAN_DEV_DEBUG is not set
# CONFIG_RT_WLAN_PROT_DEBUG is not set
# CONFIG_RT_WLAN_CFG_DEBUG is not set
# CONFIG_RT_WLAN_LWIP_DEBUG is not set
#
# Using USB
#
......@@ -127,15 +160,21 @@ CONFIG_RT_USING_LIBC=y
# CONFIG_RT_USING_PTHREADS is not set
#
# Network stack
# Network
#
#
# Socket abstraction layer
#
# CONFIG_RT_USING_SAL is not set
#
# light weight TCP/IP stack
#
CONFIG_RT_USING_LWIP=y
CONFIG_RT_USING_LWIP141=y
# CONFIG_RT_USING_LWIP202 is not set
# CONFIG_RT_USING_LWIP141 is not set
CONFIG_RT_USING_LWIP202=y
# CONFIG_RT_USING_LWIP_IPV6 is not set
CONFIG_RT_LWIP_IGMP=y
CONFIG_RT_LWIP_ICMP=y
# CONFIG_RT_LWIP_SNMP is not set
......@@ -178,11 +217,18 @@ CONFIG_LWIP_SO_SNDTIMEO=1
CONFIG_LWIP_SO_RCVBUF=1
# CONFIG_RT_LWIP_NETIF_LOOPBACK is not set
CONFIG_LWIP_NETIF_LOOPBACK=0
# CONFIG_RT_LWIP_STATS is not set
# CONFIG_RT_LWIP_DEBUG is not set
#
# Modbus master and slave stack
#
# CONFIG_RT_USING_MODBUS is not set
#
# AT commands
#
# CONFIG_RT_USING_AT is not set
# CONFIG_LWIP_USING_DHCPD is not set
#
......@@ -212,7 +258,6 @@ CONFIG_LWIP_NETIF_LOOPBACK=0
# CONFIG_PKG_USING_LJSON is not set
# CONFIG_PKG_USING_EZXML is not set
# CONFIG_PKG_USING_NANOPB is not set
# CONFIG_PKG_USING_GAGENT_CLOUD is not set
#
# Wi-Fi
......@@ -230,7 +275,16 @@ CONFIG_LWIP_NETIF_LOOPBACK=0
# CONFIG_PKG_USING_COAP is not set
# CONFIG_PKG_USING_NOPOLL is not set
# CONFIG_PKG_USING_NETUTILS is not set
# CONFIG_PKG_USING_AT_DEVICE is not set
# CONFIG_PKG_USING_WIZNET is not set
#
# IoT Cloud
#
# CONFIG_PKG_USING_ONENET is not set
# CONFIG_PKG_USING_GAGENT_CLOUD is not set
# CONFIG_PKG_USING_ALI_IOTKIT is not set
# CONFIG_PKG_USING_AZURE is not set
#
# security packages
......@@ -242,6 +296,7 @@ CONFIG_LWIP_NETIF_LOOPBACK=0
#
# language packages
#
# CONFIG_PKG_USING_LUA is not set
# CONFIG_PKG_USING_JERRYSCRIPT is not set
# CONFIG_PKG_USING_MICROPYTHON is not set
......@@ -258,6 +313,7 @@ CONFIG_LWIP_NETIF_LOOPBACK=0
# CONFIG_PKG_USING_EASYFLASH is not set
# CONFIG_PKG_USING_EASYLOGGER is not set
# CONFIG_PKG_USING_SYSTEMVIEW is not set
# CONFIG_PKG_USING_RDB is not set
#
# system packages
......@@ -271,22 +327,29 @@ CONFIG_LWIP_NETIF_LOOPBACK=0
# CONFIG_PKG_USING_SQLITE is not set
# CONFIG_PKG_USING_RTI is not set
# CONFIG_PKG_USING_LITTLEVGL2RTT is not set
# CONFIG_PKG_USING_CMSIS is not set
# CONFIG_PKG_USING_DFS_YAFFS is not set
#
# peripheral libraries and drivers
#
# CONFIG_PKG_USING_STM32F4_HAL is not set
# CONFIG_PKG_USING_STM32F4_DRIVERS is not set
CONFIG_PKG_USING_REALTEK_AMEBA=y
CONFIG_PKG_REALTEK_AMEBA_PATH="/packages/peripherals/realtek_ameba"
CONFIG_PKG_USING_REALTEK_AMEBA_LATEST_VERSION=y
# CONFIG_PKG_USING_REALTEK_AMEBA_V100 is not set
CONFIG_PKG_REALTEK_AMEBA_VER="latest"
# CONFIG_PKG_USING_CC3200_SDK is not set
# CONFIG_PKG_USING_SHT2X is not set
# CONFIG_PKG_USING_AHT10 is not set
# CONFIG_PKG_USING_AP3216C is not set
# CONFIG_PKG_USING_STM32_SDIO is not set
# CONFIG_PKG_USING_ICM20608 is not set
# CONFIG_PKG_USING_U8G2 is not set
#
# miscellaneous packages
#
# CONFIG_PKG_USING_LIBCSV is not set
# CONFIG_PKG_USING_OPTPARSE is not set
# CONFIG_PKG_USING_FASTLZ is not set
# CONFIG_PKG_USING_MINILZO is not set
# CONFIG_PKG_USING_QUICKLZ is not set
......@@ -298,17 +361,52 @@ CONFIG_PKG_REALTEK_AMEBA_VER="latest"
#
# sample package
#
# CONFIG_PKG_USING_SAMPLES is not set
#
# samples: kernel and components samples
#
# CONFIG_PKG_USING_KERNEL_SAMPLES is not set
# CONFIG_PKG_USING_FILESYSTEM_SAMPLES is not set
# CONFIG_PKG_USING_NETWORK_SAMPLES is not set
# CONFIG_PKG_USING_PERIPHERAL_SAMPLES is not set
#
# example package: hello
#
# CONFIG_PKG_USING_HELLO is not set
#
# Privated Packages of RealThread
#
# CONFIG_PKG_USING_CODEC is not set
# CONFIG_PKG_USING_PLAYER is not set
# CONFIG_PKG_USING_PERSIMMON_SRC is not set
#
# Network Utilities
#
# CONFIG_PKG_USING_MDNS is not set
# CONFIG_PKG_USING_UPNP is not set
# CONFIG_PKG_USING_WICED is not set
# CONFIG_PKG_USING_CLOUDSDK is not set
# CONFIG_PKG_USING_COREMARK is not set
# CONFIG_PKG_USING_POWER_MANAGER is not set
# CONFIG_PKG_USING_RT_OTA is not set
# CONFIG_PKG_USING_RDBD_SRC is not set
# CONFIG_PKG_USING_RTINSIGHT is not set
# CONFIG_PKG_USING_SMARTCONFIG is not set
#
# Test Packages of RealThread
#
# CONFIG_PKG_USING_JERRYSCRIPT_WIN32 is not set
# CONFIG_PKG_USING_JS_PERSIMMON is not set
#
# Env config
#
# CONFIG_SYS_AUTO_UPDATE_PKGS is not set
# CONFIG_SYS_CREATE_MDK_IAR_PROJECT is not set
# CONFIG_SYS_PKGS_DOWNLOAD_ACCELERATE is not set
CONFIG_RT_USING_UART0=y
# CONFIG_BSP_USING_UART0 is not set
CONFIG_BSP_USING_WIFI=y
......@@ -26,10 +26,6 @@ config $ENV_DIR
source "$RTT_DIR/Kconfig"
source "$PKGS_DIR/Kconfig"
source "$ENV_DIR/tools/scripts/cmds/Kconfig"
config RT_USING_UART0
bool "Using RT_USING_UART0"
default y
source "$BSP_DIR/drivers/Kconfig"
select PKG_USING_REALTEK_AMEBA
......@@ -17,8 +17,8 @@ amebaz 是由Realtek推出的Cortex-M4内核的WiFi SOC芯片rtl8710b系列
| 环境 | 说明 |
| ------------ | ------------------------------------------------------------ |
| PC操作系统 | Linux/MacOS/Windows |
| 编译器 | arm-none-eabi-gcc version 6.3.1 20170620 (release)/iar |
| 构建工具 | scons/iar |
| 编译器 | arm-none-eabi-gcc version 6.3.1 20170620 (release) |
| 构建工具 | scons |
| 依赖软件环境 | Env工具/(IAR或arm-none-eabi-gcc)/git/调试器驱动 |
1) 下载源码
......@@ -40,9 +40,9 @@ amebaz 是由Realtek推出的Cortex-M4内核的WiFi SOC芯片rtl8710b系列
(Windows)
>在[RT-Thread官网][1]下载ENV工具包
>在[RT-Thread官网][1]下载ENV工具包,并参考官网给出的env使用教程
3) 配置芯片型号
3) 配置工程(可选)
(Linux/Mac)
......@@ -56,8 +56,6 @@ amebaz 是由Realtek推出的Cortex-M4内核的WiFi SOC芯片rtl8710b系列
menuconfig
```
在menuconfig页面配置并选择对应的芯片型号,若开发环境为IAR,则需要生成工程
4) 下载package
amebaz配套的驱动库以package形式提供,故需先下载对应的package(realtek-ameba),请使用env下载package
......@@ -66,28 +64,11 @@ amebaz配套的驱动库以package形式提供,故需先下载对应的package
pkgs --update
```
5) 生成工程(Mac/Linux下请跳过此步骤)
(Windows IAR)
```bash
SET RTT_CC=iar
scons --target=iar -s
```
*该板级支持包不支持生成mdk的工程
*该板级支持包不支持生成mdk的工程及iar工程,后续会支持iar工程
5) 编译
6) 编译
使用IAR请参见对应教程
(Windows arm-none-eabi-gcc)
使用以下指令设置gcc路径
```bash
SET RTT_EXEC_PATH=[GCC路径]
```
Windows可以使用env编译工程,无需设置GCC工具链路径,直接在bsp/amebaz工程目录下编译即可。
(Linux/Mac arm-none-eabi-gcc)
使用以下指令设置gcc路径
......@@ -148,15 +129,103 @@ msh />
*默认串口
## 4. WIFI简单使用
### 4.1 wifi扫描命令
wifi 扫描命令格式如下
```unknown
wifi scan
```
命令说明
| 字段 | 描述 |
| ---- | ------------------------ |
| wifi | 有关wifi命令都以wifi开头 |
| scan | wifi执行扫描动作 |
在调试工具中输入该命令,即可进行 wifi 命令扫描,调试信息如下
```unknown
## 4. 驱动支持情况及计划
scan ap down
SSID MAC security rssi chn Mbps
------------------------------- ----------------- -------------- ---- --- ----
NEO-shanghai-ap2 58:66:ba:a1:ee:71 WPA2_TKIP_PSK -74 11 0
WQ1 88:25:93:94:51:54 WPA2_AES_PSK -76 13 0
shyc1 a0:40:a0:a3:e8:c9 WPA2_AES_PSK -77 13 0
KVIP 70:65:82:3b:71:43 WPA2_AES_PSK -83 11 0
YST2016 88:25:93:c6:67:d1 WPA2_TKIP_PSK -84 4 0
```
> 注:
>
> 测试命令有关 wifi,均以wifi开头。
### 4.2 wifi 接入
接入 wifi 之前,先介绍一下其接入的命令 ,如下
```unknown
wifi join ssid 123456789
```
命令说明
| 字段 | 描述 |
| --------- | ---------------------------------- |
| wifi | 有关wifi命令都以wifi开头 |
| join | wifi执行连接动作 |
| ssid | 热点的名字 |
| 123456789 | 热点的密码,没有密码可不输入这一项 |
了解上述命令,并且成功完成前面步骤,在串口中输入 `wifi w0 join realthread_VIP 123456789` ,如下日志表示连接成功
```unknown
WIFI wlan0 Setting:
==============================
MODE => STATION
SSID => realthread_VIP
CHANNEL => 6
SECURITY => AES
PASSWORD => 123456789
[I/WLAN.mgnt] wifi connect success ssid:realthread_VIP
```
另外,提供一个简单的测试使用命令,查询设备IP地址命令:ifconfig。
```unknown
network interface: w0 (Default)
MTU: 1500
MAC: 00 e0 4c d5 ac 46
FLAGS: UP LINK_UP ETHARP BROADCAST
ip address: 172.16.200.110
gw address: 172.16.200.1
net mask : 255.255.255.0
dns server #0: 172.16.200.1
dns server #1: 223.5.5.5
```
## 5. 驱动支持情况及计划
| 驱动 | 支持情况 | 备注 |
| ---------- | :------: | :--------------------------: |
| UART | 支持 | UART0 |
| UART | 支持 | UART0 |
| WLAN | 部分支持 | 仅支持STA模式 |
## 6. 已知问题
wlan仅支持STA模式,连接WIFI输入错误密码时,会宕机。
## 5. 联系人信息
## 7. 联系人信息
维护人:
[flyingcys][4] < [flyingcys@163.com][5] >
......
......@@ -15,19 +15,12 @@
#include <rthw.h>
#include <rtthread.h>
/**
* @addtogroup ameba
*/
/*@{*/
#include <rtdevice.h>
int main(void)
{
rt_kprintf("build time: %s %s\n", __DATE__, __TIME__);
rt_kprintf("Hello RT-Thread!\n");
rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
return 0;
}
/*@}*/
config BSP_USING_UART0
bool "Using UART0"
select RT_USING_SERIAL
default y
config BSP_USING_WIFI
bool "Using WIFI"
select RT_USING_WIFI
default y
......@@ -5,14 +5,9 @@ list = os.listdir(cwd)
objs = []
src = Split('''
board.c
drv_uart.c
''')
if GetDepend(['RT_USING_LWIP']):
src += Split('''
drv_wifi.c
''')
board.c
drv_uart.c
''')
CPPPATH = [cwd]
......
......@@ -37,7 +37,7 @@ struct device_uart
rt_uint32_t irqno;
};
#ifdef RT_USING_UART0
#ifdef BSP_USING_UART0
static struct rt_serial_device serial0;
static struct device_uart uart0;
#endif
......@@ -230,16 +230,16 @@ int rt_hw_uart_init(void)
struct rt_serial_device *serial;
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
#ifdef RT_USING_UART0
#ifdef BSP_USING_UART0
{
struct device_uart *uart;
serial = &serial0;
uart = &uart0;
/* Init UART Hardware */
serial_init(&uart->serial, UART_TX, UART_RX);
serial->ops = &_uart_ops;
serial->config = config;
serial->config.baud_rate = 115200;
......
/*
* File : drv_wifi.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-5-30 Bernard the first version
* 2018-5-30 flyingcys add amebaz wifi driver
*/
#include <rtthread.h>
#include <wlan_dev.h>
#include <skbuff.h>
#include "amebaz_wlan.h"
//#define ETH_RX_DUMP
//#define ETH_TX_DUMP
//#define MINI_DUMP
struct sk_buff * rltk_wlan_get_recv_skb(int idx);
struct sk_buff * rltk_wlan_alloc_skb(unsigned int total_len);
#define MAX_ADDR_LEN 6
struct ameba_wifi
{
struct rt_wlan_device parent;
rt_uint8_t dev_addr[MAX_ADDR_LEN];
int idx;
int connected;
};
#ifdef RT_USING_WLAN_STA
static struct ameba_wifi wifi_sta;
#endif
#ifdef RT_USING_WLAN_AP
static struct ameba_wifi wifi_ap;
#endif
#if defined(ETH_RX_DUMP) || defined(ETH_TX_DUMP)
static void packet_dump(const char *msg, const struct pbuf *p)
{
const struct pbuf *q;
rt_uint32_t i, j;
rt_uint8_t *ptr;
rt_kprintf("%s %d byte\n", msg, p->tot_len);
#ifdef MINI_DUMP
return;
#endif
i = 0;
for (q = p; q != RT_NULL; q = q->next)
{
ptr = q->payload;
for (j = 0; j < q->len; j++)
{
if ((i % 8) == 0)
{
rt_kprintf(" ");
}
if ((i % 16) == 0)
{
rt_kprintf("\r\n");
}
rt_kprintf("%02x ", *ptr);
i++;
ptr++;
}
}
rt_kprintf("\n\n");
}
#endif /* dump */
#define netifapi_netif_set_link_up(n) netifapi_netif_common(n, netif_set_link_up, NULL)
#define netifapi_netif_set_link_down(n) netifapi_netif_common(n, netif_set_link_down, NULL)
void netif_set_connected(int connected)
{
wifi_sta.connected = connected;
if (connected)
{
netifapi_netif_set_link_up(wifi_sta.parent.parent.netif);
}
else
{
netifapi_netif_set_link_down(wifi_sta.parent.parent.netif);
}
}
void rltk_wlan_set_netif_info(int idx_wlan, void * dev, unsigned char * dev_addr)
{
struct ameba_wifi *wifi;
if(idx_wlan == 0)
wifi = &wifi_sta;
rtw_memcpy(wifi->dev_addr, dev_addr, 6);
rt_hw_wifi_init();
}
void netif_rx(int idx, unsigned int len)
{
struct ameba_wifi *wifi;
struct pbuf *p, *q;
int sg_len = 0;
struct sk_buff *skb = RT_NULL;
if(idx == 0)
wifi = &wifi_sta;
#if CONFIG_WLAN
if(!wifi->connected || !rltk_wlan_running(idx))
return;
#endif
skb = rltk_wlan_get_recv_skb(idx);
if(!skb)
{
rt_kprintf("netif_rx rltk_wlan_get_recv_skb NULL.\n");
return;
}
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
if (p != RT_NULL)
{
pbuf_take(p, skb->data, len);
skb_pull(skb, len);
#ifdef ETH_RX_DUMP
packet_dump("RX dump", p);
#endif /* ETH_RX_DUMP */
if(wifi->parent.parent.netif->input(p, wifi->parent.parent.netif) != ERR_OK)
{
pbuf_free(p);
}
}
else
{
rt_kprintf("pbuf_alloc NULL for wifi RX.\n");
}
}
int netif_is_valid_IP(int idx, unsigned char *ip_dest)
{
struct netif * pnetif;
struct ip_addr addr = { 0 };
u32_t *ip_dest_addr = (u32_t*)ip_dest;
if(idx == 0)
pnetif = wifi_sta.parent.parent.netif;
addr.addr = *ip_dest_addr;
if(pnetif == RT_NULL)
return 0;
if(pnetif->ip_addr.addr == 0)
return 1;
if(ip_addr_ismulticast(&addr) || ip_addr_isbroadcast(&addr,pnetif))
{
return 1;
}
if(ip_addr_cmp(&(pnetif->ip_addr), &addr))
return 1;
return 0;
}
void netif_post_sleep_processing(void)
{
}
void netif_pre_sleep_processing(void)
{
}
unsigned char *rltk_wlan_get_ip(int idx)
{
struct ameba_wifi *wifi;
if(idx == 0)
wifi = &wifi_sta;
return (unsigned char *)&(wifi->parent.parent.netif->ip_addr);
}
struct netif *rltk_wlan_get_netif(int idx)
{
struct netif *netif;
if(idx == 0)
netif = wifi_sta.parent.parent.netif;
else if(idx == 1)
netif = wifi_ap.parent.parent.netif;
return netif;
}
rt_err_t rt_ameba_wifi_init(rt_device_t dev)
{
return RT_EOK;
}
rt_err_t rt_ameba_wifi_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
rt_err_t rt_ameba_wifi_close(rt_device_t dev)
{
return RT_EOK;
}
rt_size_t rt_ameba_wifi_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
rt_set_errno(-RT_ENOSYS);
return 0;
}
rt_size_t rt_ameba_wifi_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
rt_set_errno(-RT_ENOSYS);
return 0;
}
rt_err_t rt_ameba_wifi_control(rt_device_t dev, int cmd, void *args)
{
switch(cmd)
{
case NIOCTL_GADDR:
{
struct ameba_wifi *wifi = (struct ameba_wifi *)dev;
if(args)
memcpy(args, wifi->dev_addr, MAX_ADDR_LEN);
else
return -RT_ERROR;
}
break;
case WIFI_INIT:
{
rt_wlan_mode_t mode = *(rt_wlan_mode_t *)args;
rt_kprintf("mode:%d\n", mode);
}
break;
case WIFI_SCAN:
{
struct rt_wlan_scan_result *dst = RT_NULL;
dst = (struct rt_wlan_scan_result *)rt_malloc(sizeof(struct rt_wlan_scan_result));
if(dst == RT_NULL)
{
rt_kprintf("rt_malloc for scan result failed!\n");
return -RT_ENOMEM;
}
memset(dst, 0, sizeof(struct rt_wlan_scan_result));
if(amebaz_wifi_scan(dst) != RT_EOK)
{
rt_kprintf("amebaz_wifi_scan failed...\n");
return -RT_ERROR;
}
*(struct rt_wlan_scan_result **)args = dst;
}
break;
case WIFI_JOIN:
break;
case WIFI_EASYJOIN:
{
struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
if(amebaz_wifi_connect(wlan->info->ssid, (char *)args) != RT_EOK)
{
rt_kprintf("amebaz_wifi_connect failed...\n");
return -RT_ERROR;
}
}
break;
case WIFI_SOFTAP:
{
struct rt_wlan_device *wlan = (struct rt_wlan_device *)dev;
if(amebaz_wifi_ap_start(wlan->info->ssid, (char *)args, wlan->info->channel) != RT_EOK)
{
rt_kprintf("amebaz_wifi_ap_start failed...\n");
return -RT_ERROR;
}
}
break;
case WIFI_DISCONNECT:
if(amebaz_wifi_disconnect() != RT_EOK)
{
rt_kprintf("amebaz_wifi_disconnect failed...\n");
return -RT_ERROR;
}
break;
case WIFI_GET_RSSI:
{
int *rssi = (int *)args;
*rssi = amebaz_wifi_get_rssi();
}
break;
case WIFI_ENTER_POWERSAVE:
break;
case WIFI_CFG_MONITOR:
break;
case WIFI_SET_CHANNEL:
{
int channel = *(int *)args;
amebaz_wifi_set_channel(channel);
}
break;
case WIFI_GET_CHANNEL:
{
int *channel = (int *)args;
*channel = amebaz_wifi_get_channel();
}
break;
case WIFI_SET_MONITOR_CALLBACK:
break;
}
return RT_EOK;
}
rt_err_t rt_ameba_wifi_tx(rt_device_t dev, struct pbuf* p)
{
rt_err_t result = RT_EOK;
struct ameba_wifi *wifi = (struct ameba_wifi *)dev;
int idx;
rt_base_t level;
struct sk_buff *skb = RT_NULL;
idx = wifi->idx;
level = rt_hw_interrupt_disable();
if(wifi->connected && rltk_wlan_check_isup(idx))
rltk_wlan_tx_inc(idx);
else
{
rt_hw_interrupt_enable(level);
// rt_kprintf("is not: connected && rltk_wlan_check_isup(idx)\n");
result = -RT_ERROR;
goto _exit;
}
rt_hw_interrupt_enable(level);
#ifdef ETH_TX_DUMP
packet_dump("TX dump", p);
#endif /* ETH_TX_DUMP */
skb = rltk_wlan_alloc_skb(p->tot_len);
if(skb != RT_NULL)
{
/* copy pbuf to a whole ETH frame */
pbuf_copy_partial(p, skb->tail, p->tot_len, 0);
skb_put(skb, p->tot_len);
rltk_wlan_send_skb(idx, skb);
}
else
{
rt_kprintf("rltk_wlan_alloc_skb NULL for WIFI TX.\n");
result = -RT_ENOMEM;
}
_exit:
level = rt_hw_interrupt_disable();
rltk_wlan_tx_dec(idx);
rt_hw_interrupt_enable(level);
return result;
}
int rt_hw_wifi_init(void)
{
rt_kprintf("%s %d\n", __FUNCTION__, __LINE__);
#ifdef RT_USING_WLAN_STA
wifi_sta.parent.parent.parent.init = rt_ameba_wifi_init;
wifi_sta.parent.parent.parent.open = rt_ameba_wifi_open;
wifi_sta.parent.parent.parent.close = rt_ameba_wifi_close;
wifi_sta.parent.parent.parent.read = rt_ameba_wifi_read;
wifi_sta.parent.parent.parent.write = rt_ameba_wifi_write;
wifi_sta.parent.parent.parent.control = rt_ameba_wifi_control;
wifi_sta.parent.parent.parent.user_data = RT_NULL;
//
wifi_sta.idx = 0;
//
wifi_sta.parent.parent.eth_rx = RT_NULL;
wifi_sta.parent.parent.eth_tx = rt_ameba_wifi_tx;
/* register wifi device */
eth_device_init(&wifi_sta.parent.parent, WIFI_DEVICE_STA_NAME);
#endif
#ifdef RT_USING_WLAN_AP
#endif
return RT_EOK;
}
......@@ -6,10 +6,11 @@ CPPPATH = [cwd, str(Dir('#'))]
src = []
if GetDepend(['RT_USING_LWIP']):
src += Split('''
amebaz_wlan.c
''')
src += Split('''
drv_wifi.c
drv_wlan.c
''')
group = DefineGroup('amebaz_wlan', src, depend = [''], CPPPATH = CPPPATH)
group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH)
Return('group')
/*
* File : amebaz_wlan.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-5-30 Bernard the first version
* 2018-6-12 flyingcys add amebaz wlan interface
*/
#include <rtthread.h>
#include <netif/ethernetif.h>
#include "wifi_structures.h"
#include "wifi_constants.h"
#include <wifi/wifi_util.h>
#include <wifi/wifi_conf.h>
#define PASSWD_LEN 65
#define SCAN_WAIT_TIME 10000
typedef enum
{
WIFI_NONE,
WIFI_STATION,
WIFI_AP,
} rt_wlan_mode_t;
struct rt_wlan_info
{
rt_wlan_mode_t mode; /* wifi mode */
rtw_security_t security;
char *ssid;
uint8_t bssid[6];
/* maximal data rate */
uint32_t datarate;
/* radio channel */
uint16_t channel;
/* signal strength */
int16_t rssi;
};
typedef struct rt_wlan_scan_result
{
char ap_num;
struct rt_wlan_info *ap_table;
} rt_wlan_scan_result_t;
static rtw_network_info_t wifi_info = {0};
static rtw_ap_info_t ap_info = {0};
static unsigned char wifi_password[65] = {0};
static unsigned char ap_password[PASSWD_LEN] = {0};
static rt_sem_t scan_done_sem = RT_NULL;
static char *scan_buf = RT_NULL;
static int ApNum = 0;
extern struct netif *rltk_wlan_get_netif(int idx);
static void LwIP_ReleaseIP(uint8_t idx)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
struct netif *pnetif = rltk_wlan_get_netif(idx);
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 0, 0, 0, 0);
netif_set_addr(pnetif, &ipaddr , &netmask, &gw);
}
static rtw_result_t amebaz_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result )
{
if (malloced_scan_result->scan_complete != RTW_TRUE) {
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
++ ApNum;
if(malloced_scan_result->user_data)
memcpy((void *)((char *)malloced_scan_result->user_data+(ApNum-1)*sizeof(rtw_scan_result_t)), (char *)record, sizeof(rtw_scan_result_t));
}
else
{
rt_kprintf("ap num:%d\n", ApNum);
if(scan_done_sem)
{
rt_sem_release(scan_done_sem);
}
}
return RTW_SUCCESS;
}
static int amebaz_wifi_do_scan(void)
{
int ret = RTW_SUCCESS;
rt_kprintf("wifi scan start...\n");
scan_buf = malloc(65*sizeof(rtw_scan_result_t));
if(scan_buf == NULL){
ret = RTW_BUFFER_UNAVAILABLE_TEMPORARY;
return -RT_ENOMEM;
}
memset(scan_buf, 0, 65 * sizeof(rtw_scan_result_t));
if((ret = wifi_scan_networks(amebaz_scan_result_handler, scan_buf)) != RTW_SUCCESS){
rt_kprintf("ERROR: wifi scan failed\n\r");
return -RT_ERROR;
}
return RT_EOK;
}
int amebaz_wifi_scan(struct rt_wlan_scan_result *dst)
{
rt_uint32_t i;
rt_uint32_t j = 0;
scan_done_sem = rt_sem_create("scandone", 0, RT_IPC_FLAG_FIFO);
if(scan_done_sem == RT_NULL)
return -RT_ENOMEM;
if(amebaz_wifi_do_scan() != RT_EOK)
{
rt_kprintf("amebaz_wifi_do_scan failed...\n");
return -RT_ERROR;
}
if(rt_sem_take(scan_done_sem, rt_tick_from_millisecond(SCAN_WAIT_TIME)) != RT_EOK)
{
rt_kprintf("scan wait timeout...\n");
return -RT_ETIMEOUT;
}
if(scan_done_sem)
{
rt_sem_delete(scan_done_sem);
scan_done_sem = RT_NULL;
}
rtw_scan_result_t *ptr = (rtw_scan_result_t *)scan_buf;
dst->ap_num = ApNum;
ApNum = 0;
dst->ap_table = (struct rt_wlan_info *)rt_malloc(sizeof(struct rt_wlan_info) * dst->ap_num);
if(dst->ap_table == RT_NULL)
{
rt_kprintf("rt_malloc for ap table failed...\n");
goto __exit;
}
for(i = 0; i < dst->ap_num; i ++)
{
dst->ap_table[i].mode = 1;
dst->ap_table[i].security = ptr->security;
dst->ap_table[i].ssid = (char *)rt_malloc(33);
if(dst->ap_table[i].ssid == RT_NULL)
{
rt_kprintf("rt_malloc for ssid Failed! times:%d,total:%d\n",i,ApNum);
j = i;
goto __exit;
}
memset(dst->ap_table[i].ssid, 0, 33);
memcpy(dst->ap_table[i].ssid, ptr->SSID.val, ptr->SSID.len);
memcpy(dst->ap_table[i].bssid, ptr->BSSID.octet, 6);
dst->ap_table[i].datarate = 0;
dst->ap_table[i].channel = ptr->channel;
dst->ap_table[i].rssi = ptr->signal_strength;
ptr ++;
}
if(scan_buf != RT_NULL)
{
rt_free(scan_buf);
scan_buf = RT_NULL;
}
return RT_EOK;
__exit:
if(scan_buf != RT_NULL)
{
rt_free(scan_buf);
scan_buf = RT_NULL;
}
if(dst->ap_table)
{
for(i = 0; i < j; i ++)
rt_free(dst->ap_table[i].ssid);
rt_free(dst->ap_table);
dst->ap_table = RT_NULL;
dst->ap_num = 0;
}
return -RT_ERROR;
}
void amebaz_wifi_info_init(void)
{
memset(wifi_info.ssid.val, 0, sizeof(wifi_info.ssid.val));
memset(wifi_info.bssid.octet, 0, 6);
memset(wifi_password, 0, sizeof(wifi_password));
wifi_info.ssid.len = 0;
wifi_info.password = NULL;
wifi_info.password_len = 0;
wifi_info.key_id = -1;
memset(ap_info.ssid.val, 0, sizeof(ap_info.ssid.val));
ap_info.ssid.len = 0;
ap_info.security_type = RTW_SECURITY_UNKNOWN;
ap_info.password = NULL;
ap_info.password_len = 0;
ap_info.channel = 1;
}
static int amebaz_wifi_set_sta_info(char *ssid, char *passwd)
{
if(ssid == RT_NULL || strlen(ssid) > 32)
{
rt_kprintf("Invalid argument...\n");
return -RT_EINVAL;
}
strcpy(wifi_info.ssid.val, ssid);
wifi_info.ssid.len = strlen(ssid);
if(passwd != NULL)
{
if(strlen(passwd) > 64)
{
rt_kprintf("Invalid argument...\n");
return -RT_EINVAL;
}
strcpy(wifi_password, passwd);
wifi_info.password = wifi_password;
wifi_info.password_len = strlen(passwd);
}
else
wifi_info.password = RT_NULL;
return RT_EOK;
}
static int amebaz_wifi_set_ap_info(char *ssid, char *passwd, int channel)
{
if(ssid == RT_NULL || strlen(ssid) > 32)
{
rt_kprintf("Invalid argument...\n");
return -RT_EINVAL;
}
strcpy(ap_info.ssid.val, ssid);
ap_info.ssid.len = strlen(ssid);
if(passwd != NULL)
{
if(strlen(passwd) > 64)
{
rt_kprintf("Invalid argument...\n");
return -RT_EINVAL;
}
strcpy(ap_password, passwd);
ap_info.password = ap_password;
ap_info.password_len = strlen(passwd);
}
else
ap_info.password = RT_NULL;
ap_info.channel = channel;
return RT_EOK;
}
static int amebaz_wifi_do_connect(void)
{
int mode, ret;
char empty_bssid[6] = {0};
char assoc_by_bssid = 0;
rt_kprintf("amebaz wifi do connect start...\n");
if(memcmp (wifi_info.bssid.octet, empty_bssid, 6))
{
assoc_by_bssid = 1;
}
else if(wifi_info.ssid.val[0] == 0)
{
ret = RTW_BADARG;
return -RT_ERROR;
}
if(wifi_info.password != RT_NULL)
{
if((wifi_info.key_id >= 0) && (wifi_info.key_id <= 3))
{
wifi_info.security_type = RTW_SECURITY_WEP_PSK;
}
else
{
wifi_info.security_type = RTW_SECURITY_WPA2_AES_PSK;
}
}
else
{
wifi_info.security_type = RTW_SECURITY_OPEN;
}
//Check if in AP mode
wext_get_mode(WLAN0_NAME, &mode);
if(mode == IW_MODE_MASTER)
{
#if 0
#if CONFIG_LWIP_LAYER
dhcps_deinit();
#endif
wifi_off();
vTaskDelay(20);
if (wifi_on(RTW_MODE_STA) < 0){
printf("\n\rERROR: Wifi on failed!");
ret = RTW_ERROR;
goto EXIT;
}
#endif
}
if(assoc_by_bssid)
{
rt_kprintf("Joining BSS by BSSID \"MAC_FMT\" ...\n", MAC_ARG(wifi_info.bssid.octet));
ret = wifi_connect_bssid(wifi_info.bssid.octet, (char*)wifi_info.ssid.val, wifi_info.security_type, (char*)wifi_info.password,
ETH_ALEN, wifi_info.ssid.len, wifi_info.password_len, wifi_info.key_id, NULL);
}
else
{
rt_kprintf("\n\rJoining BSS by SSID %s...\n\r", (char*)wifi_info.ssid.val);
ret = wifi_connect((char*)wifi_info.ssid.val, wifi_info.security_type,
(char*)wifi_info.password, wifi_info.ssid.len,
wifi_info.password_len, wifi_info.key_id, NULL);
}
if(ret!= RTW_SUCCESS)
{
if(ret == RTW_INVALID_KEY)
rt_kprintf("ERROR:Invalid Key\n");
rt_kprintf("ERROR: Can't connect to AP\n");
return -RT_ERROR;
}
rt_kprintf("now start dhcp...\n");
netif_set_connected(1);
dhcp_start(netif_default);
rt_kprintf("dhcp success...\n");
return RT_EOK;
}
int amebaz_wifi_connect(char *ssid, char *passwd)
{
int ret;
ret = amebaz_wifi_set_sta_info(ssid, passwd);
if(ret != RT_EOK)
{
amebaz_wifi_info_init();
return ret;
}
if(amebaz_wifi_do_connect() != RT_EOK)
{
amebaz_wifi_info_init();
rt_kprintf("amebaz_wifi_do_connect failed...\n");
return -RT_ERROR;
}
amebaz_wifi_info_init();
return RT_EOK;
}
static int amebaz_wifi_do_disconnect(void)
{
int timeout = 20;
char essid[33];
int ret = RTW_SUCCESS;
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0)
{
rt_kprintf("\nWIFI disconnected!\n");
return -RT_ERROR;
}
if((ret = wifi_disconnect()) < 0)
{
return -RT_ERROR;
}
while(1)
{
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) < 0)
{
rt_kprintf("\nWIFI disconnected!\n");
break;
}
if(timeout == 0)
{
rt_kprintf("ERROR: Deassoc timeout!\n\r");
ret = RTW_TIMEOUT;
break;
}
vTaskDelay(10);
timeout --;
}
LwIP_ReleaseIP(WLAN0_IDX);
if(ret != RTW_SUCCESS)
return -RT_ERROR;
rt_kprintf("amebaz wifi do disconnect success...\n");
return RT_EOK;
}
int amebaz_wifi_disconnect(void)
{
int ret = RT_EOK;
ret = amebaz_wifi_do_disconnect();
if(ret != RT_EOK)
rt_kprintf("amebaz_wifi_do_disconnect failed...\n");
amebaz_wifi_info_init();
return ret;
}
static int amebaz_wifi_do_ap_start(void)
{
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gw;
struct netif *pnetif = rltk_wlan_get_netif(1);
int timeout = 20;
int ret = RTW_SUCCESS;
if(ap_info.ssid.val[0] == 0){
rt_kprintf("ERROR: SSID can't be empty\n\r");
return -RT_ERROR;
}
if(ap_info.password == NULL)
{
ap_info.security_type = RTW_SECURITY_OPEN;
}
else
{
if(ap_info.password_len <= RTW_MAX_PSK_LEN && ap_info.password_len >= RTW_MIN_PSK_LEN)
{
ap_info.security_type = RTW_SECURITY_WPA2_AES_PSK;
}
else
{
rt_kprintf("ERROR: password length is between 8 to 64 \n");
return -RT_ERROR;
}
}
//#if CONFIG_LWIP_LAYER
// dhcps_deinit();
// IP4_ADDR(&ipaddr, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
// IP4_ADDR(&netmask, NETMASK_ADDR0, NETMASK_ADDR1 , NETMASK_ADDR2, NETMASK_ADDR3);
// IP4_ADDR(&gw, GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
// netif_set_addr(pnetif, &ipaddr, &netmask,&gw);
//#ifdef CONFIG_DONT_CARE_TP
// pnetif->flags |= NETIF_FLAG_IPSWITCH;
//#endif
//#endif
wifi_off();
vTaskDelay(20);
if (wifi_on(RTW_MODE_AP) < 0)
{
rt_kprintf("ERROR: Wifi on failed!\n");
return -RT_ERROR;
}
rt_kprintf("Now start AP mode...\n");
if((ret = wifi_start_ap((char*)ap_info.ssid.val, ap_info.security_type, (char*)ap_info.password, ap_info.ssid.len, ap_info.password_len, ap_info.channel) ) < 0)
{
rt_kprintf("ERROR: Operation failed!");
return -RT_ERROR;
}
while(1)
{
char essid[33];
if(wext_get_ssid(WLAN0_NAME, (unsigned char *) essid) > 0)
{
if(strcmp((const char *) essid, (const char *)ap_info.ssid.val) == 0)
{
rt_kprintf("AP %s started...\n", ap_info.ssid.val);
ret = RTW_SUCCESS;
break;
}
}
if(timeout == 0)
{
rt_kprintf("ERROR: Start AP timeout!");
ret = RTW_TIMEOUT;
break;
}
vTaskDelay(10);
timeout --;
}
if(ret != RTW_SUCCESS)
return -RT_ERROR;
//#if CONFIG_LWIP_LAYER
//LwIP_UseStaticIP(pnetif);
// dhcps_init(pnetif);
//#endif
return RT_EOK;
}
static int amebaz_wifi_do_ap_stop(void)
{
return RT_EOK;
}
int amebaz_wifi_ap_start(char *ssid, char *passwd, int channel)
{
int ret;
ret = amebaz_wifi_set_ap_info(ssid, passwd, channel);
if(ret != RT_EOK)
{
amebaz_wifi_info_init();
return ret;
}
if(amebaz_wifi_do_ap_start() != RT_EOK)
{
amebaz_wifi_info_init();
rt_kprintf("amebaz_wifi_ap_start failed...\n");
return -RT_ERROR;
}
amebaz_wifi_info_init();
return RT_EOK;
}
int amebaz_wifi_ap_stop(void)
{
int ret;
if(amebaz_wifi_do_ap_stop() != RT_EOK)
{
amebaz_wifi_info_init();
rt_kprintf("amebaz_wifi_ap_stop failed...\n");
return -RT_ERROR;
}
amebaz_wifi_info_init();
return RT_EOK;
}
int amebaz_wifi_get_rssi(void)
{
int rssi = 0;
wifi_get_rssi(&rssi);
return rssi;
}
void amebaz_wifi_set_channel(int channel)
{
wifi_set_channel(channel);
}
int amebaz_wifi_get_channel(void)
{
int channel;
wifi_get_channel(&channel);
return channel;
}
int amebaz_wifi_init(rt_wlan_mode_t mode)
{
int ret;
rtw_mode_t rtw_mode;
if(mode == WIFI_STATION)
rtw_mode = RTW_MODE_STA;
else if(mode == WIFI_AP)
rtw_mode = RTW_MODE_AP;
if(wifi_on(mode) < 0)
return -RT_ERROR;
if(wifi_set_autoreconnect(1) < 0)
return -RT_ERROR;
return RT_EOK;
}
/*
* File : amebaz_wlan.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2017-5-30 Bernard the first version
* 2018-6-12 flyingcys add amebaz wlan interface
*/
#ifndef __AMEBAZ_WLAN_H__
#define __AMEBAZ_WLAN_H__
int amebaz_wifi_scan();
void amebaz_wifi_info_init(void);
int amebaz_wifi_set_sta_info(char *ssid, char *passwd);
int amebaz_wifi_set_ap_info(char *ssid, char *passwd);
int amebaz_wifi_connect(char *ssid, char *passwd);
int amebaz_wifi_disconnect(void);
int amebaz_wifi_ap_start(char *ssid, char *passwd, int channel);
int amebaz_wifi_ap_stop(void);
int amebaz_wifi_get_rssi(void);
void amebaz_wifi_set_channel(int channel);
int amebaz_wifi_get_channel(void);
int amebaz_wifi_init(rt_wlan_mode_t mode);
#endif /* __AMEBAZ_WLAN_H__ */
此差异已折叠。
......@@ -25,7 +25,7 @@
#ifndef __DRV_WIFI_H__
#define __DRV_WIFI_H__
int rt_hw_wifi_init(void);
int rthw_wifi_low_init(void);
#endif /* __DRV_WIFI_H__ */
/*
* File : drv_wlan.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-7-10 tyx the first version
*/
#include "wifi_structures.h"
#include "wifi_constants.h"
#include <wifi/wifi_util.h>
#include <wifi/wifi_conf.h>
#ifdef _LITTLE_ENDIAN
#undef _LITTLE_ENDIAN
#endif
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <wlan_dev.h>
#include "drv_wlan.h"
// #define SCAN_WAIT_TIME (10000)
struct scan_user_data
{
struct rt_completion done;
scan_callback_fn fun;
void *data;
};
extern rthw_mode_t wifi_mode;
rthw_mode_t rthw_wifi_mode_get(void)
{
return wifi_mode;
}
int rthw_wifi_stop(void)
{
return wifi_off();
}
int rthw_wifi_start(rthw_mode_t mode)
{
if(wifi_on(mode) < 0)
{
rt_kprintf("ERROR: wifi_on failed\n");
return -1;
}
return 0;
}
int rthw_wifi_connect(char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type)
{
int mode;
rtw_wifi_setting_t setting;
mode = rthw_wifi_mode_get();
if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP))
{
return -1;
}
if(wext_get_mode(WLAN0_NAME, &mode) < 0)
{
rt_kprintf("L:%d wifi get mode err\n", __LINE__);
return -1;
}
if(wifi_connect(ssid, security_type, password, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS)
{
rt_kprintf("wifi connect fail\n");
return -1;
}
rt_kprintf("wifi connect success\n");
rt_kprintf("Show Wi-Fi information\n");
wifi_get_setting(WLAN0_NAME,&setting);
wifi_show_setting(WLAN0_NAME,&setting);
return 0;
}
int rthw_wifi_connect_bssid(char *bssid, char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type)
{
int mode;
rtw_wifi_setting_t setting;
mode = rthw_wifi_mode_get();
if ((mode != RTHW_MODE_STA) && (mode != RTHW_MODE_STA_AP))
{
return -1;
}
if(wext_get_mode(WLAN0_NAME, &mode) < 0)
{
rt_kprintf("L:%d wifi get mode err\n", __LINE__);
return -1;
}
if(wifi_connect_bssid(bssid, ssid, security_type, password, 6, ssid_len, pass_len, -1, NULL) != RTW_SUCCESS)
{
rt_kprintf("wifi connect fail\n");
return -1;
}
rt_kprintf("wifi connect success\n");
rt_kprintf("Show Wi-Fi information\n");
wifi_get_setting(WLAN0_NAME,&setting);
wifi_show_setting(WLAN0_NAME,&setting);
return 0;
}
int rthw_wifi_ap_start(char *ssid, char *password, int channel)
{
int mode = 0, timeout = 20;
rtw_security_t security_type = RTW_SECURITY_WPA2_AES_PSK;
char *name = RT_NULL;
mode = rthw_wifi_mode_get();
if (mode == RTHW_MODE_AP)
{
name = WLAN0_NAME;
}
else if (mode == RTHW_MODE_STA_AP)
{
name = WLAN1_NAME;
}
else
{
return -1;
}
if(wext_get_mode(name, &mode) < 0)
{
rt_kprintf("L:%d wifi get mode err\n", __LINE__);
return -1;
}
if (password == RT_NULL)
{
security_type = RTW_SECURITY_OPEN;
}
if(wifi_start_ap(ssid, security_type, password, rt_strlen(ssid), rt_strlen(password), 6) != 0)
{
rt_kprintf("ERROR: wifi_start_ap failed\n");
return -1;
}
while(1)
{
char essid[33];
if(wext_get_ssid(name, (unsigned char *) essid) > 0)
{
if(strcmp((const char *) essid, (const char *)ssid) == 0)
{
rt_kprintf("%s started\n", ssid);
break;
}
}
if(timeout == 0)
{
rt_kprintf("Start AP timeout\n");
return -1;
}
rt_thread_delay(1 * RT_TICK_PER_SECOND);
timeout --;
}
return 0;
}
static int rthw_wifi_disconnect(char *name)
{
char essid[33];
int timeout = 20;
const rt_uint8_t null_bssid[ETH_ALEN + 2] = {0, 0, 0, 0, 0, 1, 0, 0};
if (name == RT_NULL)
return -1;
if (wext_get_ssid(name, (unsigned char *) essid) < 0)
{
rt_kprintf("\nWIFI disconnected!\n");
return -1;
}
if (wext_set_bssid(name, null_bssid) < 0)
{
rt_kprintf("Failed to set bogus BSSID to disconnect");
return -1;
}
while (1)
{
if(wext_get_ssid(name, (unsigned char *) essid) < 0)
{
rt_kprintf("WIFI disconnected!\n");
break;
}
if(timeout == 0)
{
rt_kprintf("ERROR: Deassoc timeout!\n");
return -1;
}
rt_thread_delay(10);
timeout --;
}
return 0;
}
int rthw_wifi_sta_disconnect(void)
{
int mode = 0;
char *name = RT_NULL;
mode = rthw_wifi_mode_get();
if (mode == RTHW_MODE_STA)
{
name = WLAN0_NAME;
}
else if (mode == RTHW_MODE_STA_AP)
{
name = WLAN0_NAME;
}
else
{
return -1;
}
return rthw_wifi_disconnect(name);
}
int rthw_wifi_ap_disconnect(void)
{
int mode = 0;
char *name = RT_NULL;
mode = rthw_wifi_mode_get();
if (mode == RTHW_MODE_AP)
{
name = WLAN0_NAME;
}
else if (mode == RTHW_MODE_STA_AP)
{
name = WLAN1_NAME;
}
else
{
return -1;
}
return rthw_wifi_disconnect(name);
}
int rthw_wifi_rssi_get(void)
{
int rssi = 0;
wifi_get_rssi(&rssi);
return rssi;
}
void rthw_wifi_channel_set(int channel)
{
wifi_set_channel(channel);
}
int rthw_wifi_channel_get(void)
{
int channel;
wifi_get_channel(&channel);
return channel;
}
static rtw_result_t rthw_wifi_scan_result_handler( rtw_scan_handler_result_t* malloced_scan_result)
{
struct scan_user_data* user_data = malloced_scan_result->user_data;
struct rthw_wlan_info info = { 0 };
if (malloced_scan_result->scan_complete != RTW_TRUE)
{
rtw_scan_result_t* record = &malloced_scan_result->ap_details;
if (user_data->fun != RT_NULL)
{
record->SSID.val[record->SSID.len] = 0; /* Ensure the SSID is null terminated */
info.ssid = record->SSID.val;
info.bssid = record->BSSID.octet;
info.band = record->band;
info.datarate = 0;
info.channel = record->channel;
info.rssi = record->signal_strength;
info.security = record->security;
user_data->fun(&info, user_data->data);
}
}
else
{
user_data->fun(RT_NULL, user_data->data);
rt_free(user_data);
rt_kprintf("scan ap down\n");
}
return RTW_SUCCESS;
}
int rthw_wifi_scan(scan_callback_fn fun, void *data)
{
struct scan_user_data *user_data;
if (fun == RT_NULL)
{
rt_kprintf("scan callback fun is null\n");
return -1;
}
user_data = rt_malloc(sizeof(struct scan_user_data));
if (user_data == RT_NULL)
{
rt_kprintf("wifi scan malloc fail\n");
return -1;
}
user_data->fun = fun;
user_data->data = data;
if (wifi_scan_networks(rthw_wifi_scan_result_handler, user_data) != RTW_SUCCESS)
{
rt_kprintf("ERROR: wifi scan failed\n\r");
return -1;
}
return 0;
}
static rthw_wlan_monitor_callback_t monitor_callback;
static void rthw_wifi_promisc_callback(unsigned char *buf, unsigned int len, void* userdata)
{
if (monitor_callback)
{
monitor_callback(buf, len, RT_NULL);
}
}
void rthw_wifi_monitor_callback_set(rthw_wlan_monitor_callback_t callback)
{
monitor_callback = callback;
}
void rthw_wifi_monitor_enable(int enable)
{
if (enable)
{
wifi_enter_promisc_mode();
wifi_set_promisc(RTW_PROMISC_ENABLE, rthw_wifi_promisc_callback, 1);
rt_kprintf("%s run \n", __FUNCTION__);
}
else
{
wifi_set_promisc(RTW_PROMISC_DISABLE, RT_NULL, 0);
rthw_wifi_stop();
}
}
/*
* File : drv_wlan.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2017, RT-Thread Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Change Logs:
* Date Author Notes
* 2018-7-10 tyx the first version
*/
#ifndef __DRV_WLAN_H__
#define __DRV_WLAN_H__
#include <rtthread.h>
typedef enum
{
RTHW_MODE_NONE = 0,
RTHW_MODE_STA,
RTHW_MODE_AP,
RTHW_MODE_STA_AP,
RTHW_MODE_PROMISC,
RTHW_MODE_P2P
}rthw_mode_t;
#define SHARED_ENABLED 0x00008000
#define WPA_SECURITY 0x00200000
#define WPA2_SECURITY 0x00400000
#define WPS_ENABLED 0x10000000
#define WEP_ENABLED 0x0001
#define TKIP_ENABLED 0x0002
#define AES_ENABLED 0x0004
#define WSEC_SWFLAG 0x0008
typedef enum {
RTHW_SECURITY_OPEN = 0, /**< Open security */
RTHW_SECURITY_WEP_PSK = WEP_ENABLED, /**< WEP Security with open authentication */
RTHW_SECURITY_WEP_SHARED = ( WEP_ENABLED | SHARED_ENABLED ), /**< WEP Security with shared authentication */
RTHW_SECURITY_WPA_TKIP_PSK = ( WPA_SECURITY | TKIP_ENABLED ), /**< WPA Security with TKIP */
RTHW_SECURITY_WPA_AES_PSK = ( WPA_SECURITY | AES_ENABLED ), /**< WPA Security with AES */
RTHW_SECURITY_WPA2_AES_PSK = ( WPA2_SECURITY | AES_ENABLED ), /**< WPA2 Security with AES */
RTHW_SECURITY_WPA2_TKIP_PSK = ( WPA2_SECURITY | TKIP_ENABLED ), /**< WPA2 Security with TKIP */
RTHW_SECURITY_WPA2_MIXED_PSK = ( WPA2_SECURITY | AES_ENABLED | TKIP_ENABLED ), /**< WPA2 Security with AES & TKIP */
RTHW_SECURITY_WPA_WPA2_MIXED = ( WPA_SECURITY | WPA2_SECURITY ), /**< WPA/WPA2 Security */
RTHW_SECURITY_WPS_OPEN = WPS_ENABLED, /**< WPS with open security */
RTHW_SECURITY_WPS_SECURE = (WPS_ENABLED | AES_ENABLED), /**< WPS with AES security */
RTHW_SECURITY_UNKNOWN = -1, /**< May be returned by scan function if security is unknown. Do not pass this to the join function! */
RTHW_SECURITY_FORCE_32_BIT = 0x7fffffff /**< Exists only to force rtw_security_t type to 32 bits */
} rthw_security_t;
typedef enum {
RTHW_802_11_BAND_5GHZ = 0, /**< Denotes 5GHz radio band */
RTHW_802_11_BAND_2_4GHZ = 1 /**< Denotes 2.4GHz radio band */
} rthw_802_11_band_t;
struct rthw_wlan_info
{
char *ssid;
rt_uint8_t *bssid;
rthw_802_11_band_t band;
rt_uint32_t datarate;
rt_uint16_t channel;
rt_int16_t rssi;
rthw_security_t security;
};
typedef void (*scan_callback_fn)(struct rthw_wlan_info *info, void *user_data);
typedef void (*rthw_wlan_monitor_callback_t)(rt_uint8_t *data, int len, void *user_data);
rthw_mode_t rthw_wifi_mode_get(void);
int rthw_wifi_stop(void);
int rthw_wifi_start(rthw_mode_t mode);
int rthw_wifi_connect(char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type);
int rthw_wifi_connect_bssid(char *bssid, char *ssid, int ssid_len, char *password, int pass_len, rthw_security_t security_type);
int rthw_wifi_ap_start(char *ssid, char *password, int channel);
int rthw_wifi_rssi_get(void);
void rthw_wifi_channel_set(int channel);
int rthw_wifi_channel_get(void);
int rthw_wifi_sta_disconnect(void);
int rthw_wifi_ap_disconnect(void);
int rthw_wifi_scan(scan_callback_fn fun, void *data);
void rthw_wifi_monitor_enable(int enable);
void rthw_wifi_monitor_callback_set(rthw_wlan_monitor_callback_t callback);
#endif
此差异已折叠。
此差异已折叠。
<?xml version="1.0" encoding="iso-8859-1"?>
<workspace>
<project>
<path>$WS_DIR$\project.ewp</path>
</project>
<batchBuild/>
</workspace>
......@@ -11,15 +11,14 @@
#define RT_THREAD_PRIORITY_32
#define RT_THREAD_PRIORITY_MAX 32
#define RT_TICK_PER_SECOND 1000
#define RT_DEBUG
#define RT_USING_OVERFLOW_CHECK
#define RT_DEBUG_INIT 0
#define RT_DEBUG_THREAD 0
#define RT_USING_HOOK
#define RT_IDEL_HOOK_LIST_SIZE 4
#define IDLE_THREAD_STACK_SIZE 256
#define RT_USING_TIMER_SOFT
#define RT_TIMER_THREAD_PRIO 4
#define RT_TIMER_THREAD_STACK_SIZE 512
#define RT_DEBUG
/* Inter-Thread communication */
......@@ -45,6 +44,9 @@
/* RT-Thread Components */
#define RT_USING_COMPONENTS_INIT
#define RT_USING_USER_MAIN
#define RT_MAIN_THREAD_STACK_SIZE 2048
#define RT_MAIN_THREAD_PRIORITY 10
/* C++ features */
......@@ -62,6 +64,7 @@
#define FINSH_CMD_SIZE 80
#define FINSH_USING_MSH
#define FINSH_USING_MSH_DEFAULT
#define FINSH_USING_MSH_ONLY
#define FINSH_ARG_MAX 10
/* Device virtual file system */
......@@ -70,13 +73,27 @@
/* Device Drivers */
#define RT_USING_DEVICE_IPC
#define RT_PIPE_BUFSZ 512
#define RT_USING_SERIAL
#define RT_USING_PIN
/* Using WiFi */
#define RT_USING_WIFI
#define RT_USING_WLAN_STA
#define RT_USING_WLAN_AP
#define WIFI_DEVICE_STA_NAME "w0"
#define WIFI_DEVICE_AP_NAME "ap"
#define RT_WLAN_DEVICE_STA_NAME "wlan0"
#define RT_WLAN_DEVICE_AP_NAME "wlan1"
#define RT_WLAN_DEFAULT_PROT "lwip"
#define RT_WLAN_SCAN_WAIT_MS 10000
#define RT_WLAN_CONNECT_WAIT_MS 10000
#define RT_WLAN_SSID_MAX_LENGTH 32
#define RT_WLAN_PASSWORD_MAX_LENGTH 32
#define RT_WLAN_SCAN_SORT
#define RT_WLAN_CFG_INFO_MAX 3
#define RT_WLAN_WORKQUEUE_THREAD_NAME "wlan_job"
#define RT_WLAN_WORKQUEUE_THREAD_SIZE 2048
#define RT_WLAN_WORKQUEUE_THREAD_PRIO 22
#define RT_WLAN_DEV_EVENT_NUM 2
#define RT_WLAN_DEBUG
#define RT_WLAN_CMD_DEBUG
/* Using USB */
......@@ -85,12 +102,15 @@
#define RT_USING_LIBC
/* Network stack */
/* Network */
/* Socket abstraction layer */
/* light weight TCP/IP stack */
#define RT_USING_LWIP
#define RT_USING_LWIP141
#define RT_USING_LWIP202
#define RT_LWIP_IGMP
#define RT_LWIP_ICMP
#define RT_LWIP_DNS
......@@ -129,6 +149,9 @@
/* Modbus master and slave stack */
/* AT commands */
/* VBUS(Virtual Software BUS) */
......@@ -148,6 +171,9 @@
/* Wiced WiFi */
/* IoT Cloud */
/* security packages */
......@@ -173,12 +199,23 @@
/* sample package */
/* samples: kernel and components samples */
/* example package: hello */
/* Privated Packages of RealThread */
/* Network Utilities */
/* Test Packages of RealThread */
/* Env config */
#define RT_USING_UART0
#define BSP_USING_WIFI
#endif
......@@ -9,12 +9,17 @@ if os.getenv('RTT_CC'):
CROSS_TOOL = os.getenv('RTT_CC')
if os.getenv('RTT_ROOT'):
RTT_ROOT = os.getenv('RTT_ROOT')
else:
RTT_ROOT = os.path.join(os.path.normpath(os.getcwd()), 'rt-thread')
# cross_tool provides the cross compiler
# EXEC_PATH is the compiler execute path, for example, CodeSourcery, Keil MDK, IAR
if CROSS_TOOL == 'gcc':
PLATFORM = 'gcc'
EXEC_PATH = r'/opt/gcc-arm-none-eabi-5_4-2016q3/bin'
EXEC_PATH = 'C:/work/env/tools/gnu_gcc/arm_gcc/mingw/bin'
elif CROSS_TOOL == 'iar':
PLATFORM = 'iar'
EXEC_PATH = 'C:/Program Files (x86)/IAR Systems/Embedded Workbench 7.0'
else:
print 'Please make sure your toolchains is GNU GCC!'
exit(0)
......@@ -39,7 +44,7 @@ if PLATFORM == 'gcc':
NM = PREFIX + 'nm'
DEVICE = ' -DM3 -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections'
CFLAGS = DEVICE + ' -g2 -w -O2 -Wno-pointer-sign -fno-common -fmessage-length=0 -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-short-enums -DF_CPU=166000000L -std=gnu99 -fsigned-char'
CFLAGS = DEVICE + ' -g2 -Wall -Wno-pointer-sign -fno-common -fmessage-length=0 -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-short-enums -DF_CPU=166000000L -std=gnu99 -fsigned-char'
AFLAGS = ' -c' + DEVICE + ' -x assembler-with-cpp -Wa,-mimplicit-it=thumb '
LFLAGS = DEVICE + ' -lm -lgcc -lc' + ' -g --specs=nano.specs -nostartfiles -Wl,-Map=rtthread.map -Os -Wl,--gc-sections -Wl,--cref -Wl,--entry=Reset_Handler -Wl,--no-enum-size-warning -Wl,--no-wchar-size-warning -T./rlx8711B-symbol-v02-img2_xip1.ld'
LFLAGS += ' -Wl,-wrap,rtl_printf'
......@@ -48,7 +53,7 @@ if PLATFORM == 'gcc':
LPATH = ''
if BUILD == 'debug':
CFLAGS += ' -O0 -gdwarf-2'
CFLAGS += ' -Os -gdwarf-2'
AFLAGS += ' -gdwarf-2'
else:
CFLAGS += ' -O2'
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
<?xml version="1.0" encoding="iso-8859-1"?>
<flash_board>
<pass>
<range>CODE 0x8000000 0x8004fff</range>
<loader>$PROJ_DIR$\packages\realtek_ameba-latest\sdk-ameba-v4.0b_without_NDA_GCC_V1.0.0\component\soc\realtek\8711b\misc\iar_utility\common\flashloader\FlashRTL8195aMP.flash</loader>
<abs_offset>0x00000000</abs_offset>
</pass>
<pass>
<range>CODE 0x800b020 0x807ffff</range>
<loader>$PROJ_DIR$\packages\realtek_ameba-latest\sdk-ameba-v4.0b_without_NDA_GCC_V1.0.0\component\soc\realtek\8711b\misc\iar_utility\common\flashloader\FlashRTL8195aMP.flash</loader>
<abs_offset>0xb020</abs_offset>
</pass>
<pass>
<range>CODE 0x10005000 0x10005A6B</range>
<loader>$PROJ_DIR$\packages\realtek_ameba-latest\sdk-ameba-v4.0b_without_NDA_GCC_V1.0.0\component\soc\realtek\8711b\misc\iar_utility\common\flashloader\FlashRTL8195aMP.flash</loader>
<abs_offset>0xb000</abs_offset>
<args>--concat</args>
</pass>
<ignore>CODE 0x10005a6c 0x1003dfff</ignore>
</flash_board>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册