當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀] LWIP是一款開源的嵌入式網(wǎng)絡(luò)協(xié)議棧,支持的功能很多,而且能在多任務(wù)環(huán)境下和單任務(wù)裸機(jī)環(huán)境下跑,今天說(shuō)說(shuō)他的移植過(guò)程,芯片為STM32,網(wǎng)卡為ENC28J60,無(wú)操作系統(tǒng) 首先下載LWIP的源代碼,我下載的是1.4.1的源碼,下載后解

LWIP是一款開源的嵌入式網(wǎng)絡(luò)協(xié)議棧,支持的功能很多,而且能在多任務(wù)環(huán)境下和單任務(wù)裸機(jī)環(huán)境下跑,今天說(shuō)說(shuō)他的移植過(guò)程,芯片為STM32,網(wǎng)卡為ENC28J60,無(wú)操作系統(tǒng)

首先下載LWIP的源代碼,我下載的是1.4.1的源碼,下載后解壓,文件結(jié)構(gòu)如圖

將這四個(gè)目錄中的文件全部拷貝到工程中,API是一些socket通訊的接口,需要在多任務(wù)的環(huán)境下實(shí)現(xiàn),core里面存放的內(nèi)核源碼,我們主要使用IPV4,include目錄下是需要包含的目錄,lwip只要求我們包含include目錄,里面的內(nèi)層目錄會(huì)自動(dòng)找到,最后建立的工程目錄如下

好了,此時(shí)源碼已經(jīng)做好,還有需要做的,在include目錄下新建一個(gè)文件夾,必須叫arch,里面存放這幾個(gè)文件,自己新建

文件的具體內(nèi)容如下

cc.h

/*

* Copyright (c) 2001-2003 Swedish Institute of Computer Science.

* All rights reserved.

*

* Redistribution and use in source and binary forms, with or without modification,

* are permitted provided that the following conditions are met:

*

* 1. Redistributions of source code must retain the above copyright notice,

* this list of conditions and the following disclaimer.

* 2. Redistributions in binary form must reproduce the above copyright notice,

* this list of conditions and the following disclaimer in the documentation

* and/or other materials provided with the distribution.

* 3. The name of the author may not be used to endorse or promote products

* derived from this software without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED

* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF

* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT

* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,

* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT

* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING

* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY

* OF SUCH DAMAGE.

*

* This file is part of the lwIP TCP/IP stack.

*

* Author: Adam Dunkels

*

*/

#ifndef __CC_H__

#define __CC_H__


#include "cpu.h"


//編譯器無(wú)關(guān)的數(shù)據(jù)類型定義

typedef unsigned char u8_t;

typedef signed char s8_t;

typedef unsigned short u16_t;

typedef signed short s16_t;

typedef unsigned long u32_t;

typedef signed long s32_t;

typedef u32_t mem_ptr_t;

typedef int sys_prot_t;


//lwip調(diào)試的時(shí)候數(shù)據(jù)類型定義

#define U16_F "hu"

#define S16_F "d"

#define X16_F "hx"

#define U32_F "u"

#define S32_F "d"

#define X32_F "x"

#define SZT_F "uz"



//根據(jù)不同的編譯器的符號(hào)定義

#if defined (__ICCARM__)


#define PACK_STRUCT_BEGIN

#define PACK_STRUCT_STRUCT

#define PACK_STRUCT_END

#define PACK_STRUCT_FIELD(x) x

#define PACK_STRUCT_USE_INCLUDES


#elif defined (__CC_ARM)


#define PACK_STRUCT_BEGIN __packed

#define PACK_STRUCT_STRUCT

#define PACK_STRUCT_END

#define PACK_STRUCT_FIELD(x) x


#elif defined (__GNUC__)


#define PACK_STRUCT_BEGIN

#define PACK_STRUCT_STRUCT __attribute__ ((__packed__))

#define PACK_STRUCT_END

#define PACK_STRUCT_FIELD(x) x


#elif defined (__TASKING__)


#define PACK_STRUCT_BEGIN

#define PACK_STRUCT_STRUCT

#define PACK_STRUCT_END

#define PACK_STRUCT_FIELD(x) x


#endif


#define LWIP_PLATFORM_ASSERT(x) //do { if(!(x)) while(1); } while(0)


#endif /* __CC_H__ */


cpu.h



/*

* Copyright (c) 2001-2003 Swedish Institute of Computer Science.

* All rights reserved.

*

* Redistribution and use in source and binary forms, with or without modification,

* are permitted provided that the following conditions are met:

*

* 1. Redistributions of source code must retain the above copyright notice,

* this list of conditions and the following disclaimer.

* 2. Redistributions in binary form must reproduce the above copyright notice,

* this list of conditions and the following disclaimer in the documentation

* and/or other materials provided with the distribution.

* 3. The name of the author may not be used to endorse or promote products

* derived from this software without specific prior written permission.

*

* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED

* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF

* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT

* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,

* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT

* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN

* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING

* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY

* OF SUCH DAMAGE.

*

* This file is part of the lwIP TCP/IP stack.

*

* Author: Adam Dunkels

*

*/

#ifndef __CPU_H__

#define __CPU_H__


//定義cpu的數(shù)據(jù)模式,大端小端

#define BYTE_ORDER LITTLE_ENDIAN


#endif /* __CPU_H__ */


perf.h



#ifndef __PERF_H__

#define __PERF_H__


//用于lwip內(nèi)置的統(tǒng)計(jì)功能

//不使能定義為空就可以了

#define PERF_START /* null definition */

#define PERF_STOP(x) /* null definition */


#endif /* __PERF_H__ */


sys_arch.h



#ifndef __SYS_RTXC_H__

#define __SYS_RTXC_H__



void init_lwip_timer(void); //初始化LWIP定時(shí)器


u8_t timer_expired(u32_t *last_time,u32_t tmr_interval); //定時(shí)器超時(shí)判斷


#endif /* __SYS_RTXC_H__ */


sya_arch.c--注意該文件要加入源文件列表中,這是c文件哦



#include "lwip/debug.h"

#include "lwip/def.h"

#include "lwip/sys.h"

#include "lwip/mem.h"

#include "timerx.h"


//初始化LWIP定時(shí)器

void init_lwip_timer(void)

{

TIM6_Int_Init(1000,719);//100Khz計(jì)數(shù)頻率,計(jì)數(shù)到100為10ms

}


//為L(zhǎng)WIP提供計(jì)時(shí)

extern u32_t lwip_timer;//lwip 計(jì)時(shí)器,每10ms增加1.


u32_t sys_now(void)

{

return lwip_timer;

}


//定時(shí)器超時(shí)判斷

//last_time:最近時(shí)間

//tmr_interval:定時(shí)器溢出周期

u8_t timer_expired(u32_t *last_time,u32_t tmr_interval)

{

u32_t time;

time = *last_time;

if((lwip_timer-time)>=tmr_interval){

*last_time = lwip_timer;

return 1;

}

return 0;

}


可以看到我們定義了定時(shí)器,那么就要修改相關(guān)的定時(shí)器文件,文件如下


timerx.c



#include "timerx.h"



u32 lwip_timer=0;//lwip 計(jì)時(shí)器,每10ms增加1.


//定時(shí)器6中斷服務(wù)程序

void TIM6_IRQHandler(void)

{


if (TIM_GetITStatus(TIM6, TIM_IT_Update) != RESET) //檢查指定的TIM中斷發(fā)生與否:TIM 中斷源

{

TIM_ClearITPendingBit(TIM6, TIM_IT_Update ); //清除TIMx的中斷待處理位:TIM 中斷源

lwip_timer++;//lwip計(jì)時(shí)器增加1

}


}





//基本定時(shí)器6中斷初始化

//這里時(shí)鐘選擇為APB1的2倍,而APB1為36M

//arr:自動(dòng)重裝值。

//psc:時(shí)鐘預(yù)分頻數(shù)

//這里使用的是定時(shí)器3!

void TIM6_Int_Init(u16 arr,u16 psc)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

NVIC_InitTypeDef NVIC_InitStructure;


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); //時(shí)鐘使能


TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期的值 計(jì)數(shù)到5000為500ms

TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來(lái)作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值 10Khz的計(jì)數(shù)頻率

TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設(shè)置時(shí)鐘分割:TDTS = Tck_tim

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計(jì)數(shù)模式

TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位

TIM_ITConfig( TIM6,TIM_IT_Update"TIM_IT_Trigger,ENABLE);//使能定時(shí)器6更新觸發(fā)中斷

TIM_Cmd(TIM6, ENABLE); //使能TIMx外設(shè)

NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn; //TIM3中斷

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占優(yōu)先級(jí)0級(jí)

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //從優(yōu)先級(jí)3級(jí)

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能

NVIC_Init(&NVIC_InitStructure); //根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)NVIC寄存器

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車。 SODA V工具的開發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱,數(shù)字世界的話語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉