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(?。▁)) 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 “l(fā)wip/debug.h”
#include “l(fā)wip/def.h”
#include “l(fā)wip/sys.h”
#include “l(fā)wip/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寄存器
評(píng)論
查看更多