一、lwip網(wǎng)卡接口ethernetif.c
ethernetif.c是lwip的網(wǎng)卡接口,在該接口中處理網(wǎng)卡的數(shù)據(jù)接收和發(fā)送,rt-thread在該接口文件中抽象了一個(gè)eth_device,管理網(wǎng)絡(luò)數(shù)據(jù)的收發(fā)和向內(nèi)核的netdev_list添加netdev。
二、網(wǎng)絡(luò)設(shè)備eth_device
eth_device是rt-thread實(shí)現(xiàn)的ethernetif。
struct eth_device
{
/* inherit from rt_device /
struct rt_device parent;
/ network interface for lwip */
struct netif netif;
struct rt_semaphore tx_ack;
rt_uint16_t flags;
rt_uint8_t link_changed;
rt_uint8_t link_status;
/ eth device interface /
struct pbuf (*eth_rx)(rt_device_t dev);
rt_err_t (eth_tx)(rt_device_t dev, struct pbuf p);
};
netif:lwip的網(wǎng)絡(luò)接口。
eth_rx:底層數(shù)據(jù)接收接口。
eth_tx:底層數(shù)據(jù)發(fā)送接口。
三、網(wǎng)絡(luò)設(shè)備數(shù)據(jù)的接收和發(fā)送
網(wǎng)絡(luò)設(shè)備的接收和發(fā)送通過(guò)eth_device的eth_rx和eth_tx完成。在系統(tǒng)初始化時(shí)內(nèi)核調(diào)用eth_system_device_init創(chuàng)建erx和etx兩個(gè)線程,用于處理接收和發(fā)送。
3.1 數(shù)據(jù)接收
當(dāng)erx線程起來(lái)后,等待eth_rx_thread_mb,當(dāng)網(wǎng)卡準(zhǔn)備好或者改變網(wǎng)卡狀態(tài)時(shí),往下執(zhí)行,進(jìn)入while(1)處理網(wǎng)卡接收,調(diào)用網(wǎng)卡注冊(cè)的eth_rx接收網(wǎng)卡數(shù)據(jù),并傳遞給協(xié)議棧上層。
static void eth_rx_thread_entry(void* parameter)
{
struct eth_device* device;
while (1)
{
if (rt_mb_recv(e_rx_thread_mb, (rt_ubase_t*)&device, RT_WAITING_FOREVER) == RT_EOK)
{
struct pbuf p;
/ check link status /
if (device->link_changed)
{
int status;
rt_uint32_t level;
level = rt_hw_interrupt_disable();
status = device->link_status;
device->link_changed = 0x00;
rt_hw_interrupt_enable(level);
if (status)
netifapi_netif_set_link_up(device->netif);
else
netifapi_netif_set_link_down(device->netif);
}
/ receive all of buffer /
while (1)
{
if(device->eth_rx == RT_NULL) break;
p = device->eth_rx(&(device->parent));
if (p != RT_NULL)
{
/ notify to upper layer */
if( device->netif->input(p, device->netif) != ERR_OK )
{
LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: Input errorn"));
pbuf_free(p);
p = NULL;
}
}
else break;
}
}
else
{
LWIP_ASSERT("Should not happen!n",0);
}
}
}
3.2 數(shù)據(jù)發(fā)送
當(dāng)協(xié)議棧需要發(fā)送數(shù)據(jù)時(shí),調(diào)用netif的linkoutput接口,在linkoutput中,將數(shù)據(jù)封裝成消息發(fā)送給etx線程,最終通過(guò)eth_device的eth_tx接口將數(shù)據(jù)發(fā)送出去。
static void eth_tx_thread_entry(void* parameter)
{
struct eth_tx_msg* msg;
while (1)
{
if (rt_mb_recv(e_tx_thread_mb, (rt_ubase_t*)&msg, RT_WAITING_FOREVER) == RT_EOK)
{
struct eth_device* enetif;
RT_ASSERT(msg->netif != RT_NULL);
RT_ASSERT(msg->buf != RT_NULL);
enetif = (struct eth_device*)msg->netif->state;
if (enetif != RT_NULL)
{
/* call driver's interface /
if (enetif->eth_tx(&(enetif->parent), msg->buf) != RT_EOK)
{
/ transmit eth packet failed /
}
}
/ send ACK */
rt_sem_release(&(enetif->tx_ack));
}
}
}
四、wlan設(shè)備數(shù)據(jù)的接收和發(fā)送
wlan設(shè)備的數(shù)據(jù)接收和發(fā)送是通過(guò)rt_wlan_prot完成的,rt_wlan_prot是對(duì)不同協(xié)議簇的抽象,在rt_wlan_set_mode啟動(dòng)wlan設(shè)備時(shí),最終會(huì)調(diào)用rt_wlan_prot_attach將lwip協(xié)議簇掛載到wlan設(shè)備,在掛載過(guò)程中,會(huì)根據(jù)協(xié)議簇的名稱匹配注冊(cè)的rt_wlan_prot,并調(diào)用rt_wlan_prot的register將wlan設(shè)備注冊(cè)到內(nèi)核。
4.1 數(shù)據(jù)接收
在網(wǎng)卡的接收中斷中,調(diào)用rt_wlan_dev_transfer_prot將網(wǎng)卡設(shè)備接收的數(shù)據(jù)傳遞給上層處理。
rt_err_t rt_wlan_dev_transfer_prot(struct rt_wlan_device *wlan, void *buff, int len)
{
struct rt_wlan_prot *prot = wlan->prot;
if (prot != RT_NULL)
{
return prot->ops->prot_recv(wlan, buff, len);
}
return -RT_ERROR;
}
注冊(cè)到rt_wlan_prot的接收函數(shù)是rt_wlan_lwip_protocol_recv,在rt_wlan_lwip_protocol_recv中,通過(guò)lwip_prot_des獲取eth_device(netif在eth_device_init_with_flag中被注冊(cè)到eth_device),接著便可使用其中的netif的input將數(shù)據(jù)交給上層。
static rt_err_t rt_wlan_lwip_protocol_recv(struct rt_wlan_device *wlan, void *buff, int len)
{
struct eth_device *eth_dev = &((struct lwip_prot_des *)wlan->prot)->eth;//eth在rt_wlan_lwip_protocol_register中注冊(cè)
struct pbuf *p = RT_NULL;
//...省略
{
p = buff;
if ((eth_dev->netif->input(p, eth_dev->netif)) != ERR_OK)
{
return -RT_ERROR;
}
return RT_EOK;
}
//...省略
}
4.2 數(shù)據(jù)發(fā)送
當(dāng)協(xié)議棧需要發(fā)送數(shù)據(jù)時(shí),會(huì)調(diào)用rt_wlan_prot_transfer_dev將數(shù)據(jù)傳遞給wlan設(shè)備,在rt_wlan_prot_transfer_dev中,會(huì)調(diào)用wlan設(shè)備的發(fā)送接口將數(shù)據(jù)發(fā)送出去。
4.3 以w601舉例
w601的wlan驅(qū)動(dòng)在driver文件夾下的drv_wifi.c。
4.3.1 數(shù)據(jù)接收
在內(nèi)核需要初始化wlan的時(shí)候,調(diào)用drv_wlan_init,在tls_ethernet_data_rx_callback函數(shù)中注冊(cè)wm_ethernetif_input到wifi接收中斷,在wm_ethernetif_input中調(diào)用rt_wlan_dev_transfer_prot將接收的數(shù)據(jù)傳給協(xié)議棧上層。
4.3.2 數(shù)據(jù)發(fā)送
在drv_wifi.c中,將drv_wlan_send函數(shù)注冊(cè)到發(fā)送接口,在協(xié)議棧需要發(fā)送數(shù)據(jù)時(shí),通過(guò)rt_wlan_prot_transfer_dev調(diào)用這個(gè)接口完成數(shù)據(jù)的發(fā)送。
static const struct rt_wlan_dev_ops ops =
{
//...省略
.wlan_recv = drv_wlan_recv,
.wlan_send = drv_wlan_send,/ 向內(nèi)核注冊(cè)的發(fā)送接口 /
};
-
接收機(jī)
+關(guān)注
關(guān)注
8文章
1180瀏覽量
53453 -
WLAN技術(shù)
+關(guān)注
關(guān)注
0文章
23瀏覽量
9273 -
LwIP協(xié)議
+關(guān)注
關(guān)注
0文章
11瀏覽量
8911 -
串口中斷
+關(guān)注
關(guān)注
0文章
64瀏覽量
13882 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1285瀏覽量
40081
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論