LwIP协议栈在uCOS II下的实现 下载本文

内容发布更新时间 : 2024/11/10 6:18:58星期一 下面是文章的全部内容请认真阅读。

LwIP鍗忚鏍堝湪uCOS II涓嬬殑瀹炵幇

1銆佹杩帮細 LwIP鍗忚鏍堝湪璁捐鏃跺氨鑰冭檻鍒颁簡灏嗘潵鐨勭Щ妞嶉棶棰橈紝鍥犳鎶婃墍鏈変笌纭欢銆丱S銆佺紪璇戝櫒鐩稿叧鐨勯儴浠界嫭绔嬪嚭鏉ワ紝鏀惧湪ucosii&LwIPsource etlwiparch鐩綍涓嬨傚洜姝wIP鍦╱COS II涓婄殑瀹炵幇灏辨槸淇敼杩欎釜鐩綍涓嬬殑鏂囦欢锛屽叾瀹冪殑鏂囦欢涓鑸笉搴旇淇敼銆備笅闈㈠垎鍑犻儴浠藉垎鍒鏄庣浉搴旀枃浠剁殑瀹炵幇鍘熺悊鍜岃繃绋嬨?銆佷笌CPU鎴栫紪璇戝櫒鐩稿叧鐨刬nclude鏂囦欢锛歶cosii&LwIPsource etlwiparchucosIIincludearch鐩綍涓媍c.h銆乧pu.h銆乸erf.h涓湁涓浜涗笌CPU鎴栫紪璇戝櫒鐩稿叧鐨勫畾涔夛紝濡傛暟鎹暱搴︼紝瀛楃殑楂樹綆浣嶉『搴忕瓑銆傝繖搴旇涓庣敤鎴峰疄鐜?micro;C/OS II鏃跺畾涔夌殑鏁版嵁闀垮害绛夊弬鏁版槸涓鑷寸殑銆?define BYTE_ORDER LITTLE_ENDIAN //C33209榛樿涓哄皬绔瓨鍌ㄧ郴缁?/鏁版嵁绫

诲瀷闀垮害鐨勫畾涔塼ypedef unsigned char u8_t;typedef signed char s8_t;typedef unsigned short u16_t;typedef signed short s16_t;typedef unsigned int u32_t;typedef signed int s32_t;姝ゅ杩樻湁涓鐐癸細涓鑸儏鍐典笅C璇█鐨勭粨鏋勪綋struct鏄?瀛楄妭瀵归綈鐨勶紝浣嗘槸鍦ㄥ鐞嗘暟鎹寘鐨勬椂鍊欙紝LwIP浣跨敤鐨勬槸閫氳繃缁撴瀯浣撲腑涓嶅悓鏁版嵁鐨勯暱搴︽潵璇诲彇鐩稿簲鐨勬暟鎹殑锛屾墍浠ワ紝涓瀹氳鍦ㄥ畾涔塻truct鐨勬椂鍊欎娇鐢╛packed鍏抽敭瀛楋紝璁╃紪璇戝櫒鏀惧純struct鐨勫瓧鑺傚榻愩侺wIP涔熻冭檻鍒颁簡杩欎釜闂锛屾墍浠ワ紝鍦ㄥ畠鐨勭粨鏋勪綋瀹氫箟涓湁鍑犱釜PACKED_FIELD_xxx瀹忥紝榛樿鐨勬椂鍊欒繖鍑犱釜瀹忛兘鏄┖鐨勶紝鍙互鍦ㄧЩ妞嶇殑鏃跺欐坊鍔犱笉鍚岀殑缂栬瘧鍣ㄦ墍瀵瑰簲鐨刜packed鍏抽敭瀛椼傛瘮濡傚湪Skyeye锛圕33209锛変笂瀵瑰簲gcc缂栬瘧鍣ㄧ殑瀹氫箟锛?define PACK_STRUCT_FIELD(x) x __attribute__((packed))#define PACK_STRUCT_STRUCT __attribute__((packed))#define PACK_STRUCT_BEGIN#define PACK_STRUCT_END3銆乻ys_arch鎿嶄綔绯荤粺鐩稿叧閮ㄤ唤锛? sys_arch.[ch]涓殑鍐呭鏄笌OS鐩稿叧鐨勪竴浜涚粨鏋勫拰鍑芥暟锛屼富瑕佸彲浠ュ垎涓哄洓涓儴浠斤細(1) sys_sem_t 淇″彿閲廘wIP涓渶瑕佷娇鐢ㄤ俊鍙烽噺閫氫俊锛屾墍浠ュ湪sys_arch涓簲瀹炵幇淇″彿閲忕粨鏋勪綋鍜屽鐞嗗嚱鏁帮細struct sys_sem_t sys_sem_new锛堬級 //鍒涘缓涓涓俊鍙烽噺缁撴瀯 sys_ sem _free锛

堬級 //閲婃斁涓涓俊鍙烽噺缁撴瀯 sys_ sem _signal锛堬級 //鍙戦佷俊鍙烽噺 sys_ arch_sem _wait锛堬級 //璇锋眰淇″彿閲忕敱浜?micro;C/OSII宸茬粡瀹炵幇浜嗕俊鍙烽噺OS_EVENT鐨勫悇绉嶆搷浣滐紝骞朵笖鍔熻兘鍜孡wIP涓婇潰鍑犱釜鍑芥暟鐨勭洰鐨勫姛鑳芥槸瀹屽叏涓鏍风殑锛屾墍浠ュ彧瑕佹妸µC/OSII鐨勫嚱鏁伴噸鏂板寘瑁呮垚涓婇潰鐨勫嚱鏁帮紝灏卞彲浠ョ洿鎺ヤ娇鐢ㄤ簡銆?2) sys_mbox_t 娑堟伅LwIP浣跨敤娑堟伅闃熷垪鏉ョ紦鍐层佷紶閫掓暟鎹姤鏂囷紝鍥犳瑕佸湪sys_arch涓疄鐜版秷鎭槦鍒楃粨鏋剆ys_mbox_t锛屼互鍙婄浉搴旂殑鎿嶄綔鍑芥暟锛歴ys_mbox_new锛堬級 //鍒涘缓涓涓秷鎭槦鍒梥

ys_mbox_free锛堬級 //閲婃斁涓涓秷鎭槦鍒梥ys_mbox_post锛堬級 //鍚戞秷鎭槦鍒楀彂閫佹秷鎭痵ys_arch_mbox_fetch锛堬級 //浠庢秷鎭槦鍒椾腑鑾峰彇娑堟伅µC/OSII鍚屾牱瀹炵幇浜嗘秷鎭槦鍒楃粨鏋凮SQ鍙婂叾鎿嶄綔锛屼絾鏄?micro;C/OS-II娌℃湁瀵规秷鎭槦鍒椾腑鐨勬秷鎭繘琛岀鐞嗭紝鍥犳涓嶈兘鐩存帴浣跨敤锛屽繀椤诲湪µC/OS-II鐨勫熀纭涓婇噸鏂板疄鐜般備负浜嗗疄鐜板娑堟伅鐨勭鐞嗭紝鎴戜滑瀹氫箟浜嗕互涓嬬粨鏋勶細typedef struct { OS_EVENT* pQ; void* pvQEntries[MAX_QUEUE_ENTRIES];} sys_mbox_t

锛涘湪浠ヤ笂缁撴瀯涓紝鍖呮嫭OS_EVENT绫诲瀷鐨勯槦鍒楁寚閽堬紙pQ锛夊拰闃熷垪鍐呯殑娑堟伅锛坧vQEntries锛変袱閮ㄥ垎锛屽闃熷垪鏈韩鐨勭鐞嗗埄鐢?micro;C/OS-II鑷繁鐨凮SQ鎿嶄綔瀹屾垚锛岀劧鍚庝娇鐢?micro;C/OS-II涓殑鍐呭瓨绠$悊妯″潡瀹炵幇瀵规秷鎭殑鍒涘缓銆佷娇鐢ㄣ佸垹闄ゅ洖鏀讹紝涓ら儴鍒嗙患鍚堣捣鏉ュ舰鎴愪簡LwIP鐨勬秷鎭槦鍒楀姛鑳姐?3) sys_arch_timeout 鍑芥暟LwIP涓瘡涓笌澶栫晫缃戠粶杩炴帴鐨勭嚎绋嬮兘鏈夎嚜宸辩殑timeout灞炴э紝鍗崇瓑寰呰秴鏃舵椂闂淬傝繖涓睘鎬ц〃鐜颁负姣忎釜绾跨▼閮藉搴斾竴涓猻ys_timeout缁撴瀯浣撻槦鍒楋紝鍖呮嫭杩欎釜绾跨▼鐨則imeout鏃堕棿闀垮害锛屼互鍙婅秴鏃跺悗搴旇皟鐢ㄧ殑timeout鍑芥暟锛岃鍑芥暟浼氬仛涓浜涢噴鏀捐繛鎺ワ紝鍥炴敹璧勬簮鐨勫伐浣溿傚鏋滀竴涓嚎绋嬪搴旂殑sys_timeout涓虹┖锛圢ULL锛夛紝璇存槑璇ョ嚎绋嬪杩炴帴鍋氭案涔呯殑绛夊緟銆倀imeout缁撴瀯浣撳凡缁忕敱LwIP鑷繁鍦╯ys.h涓畾涔夊ソ浜嗭紝鑰屼笖瀵圭粨鏋勪綋闃熷垪鐨勬暟鎹搷浣滀篃鐢盠wIP璐熻矗锛屾垜浠墍瑕佸疄鐜扮殑鏄涓嬪嚱鏁帮細struct sys_timeouts * sys_arch_timeouts(void)杩欎釜鍑芥暟鐨勫姛鑳芥槸杩斿洖鐩墠姝e浜庤繍琛屾佺殑绾跨▼鎵瀵瑰簲鐨則imeout闃熷垪鎸囬拡銆倀imeout闃熷垪灞炰簬绾跨▼鐨勫睘鎬э紝鍥犳鏄疧S鐩稿叧鐨勫嚱鏁帮紝鍙兘鐢辩敤鎴峰疄鐜般?4) sys_thread_new 鍒涘缓鏂扮嚎绋婰wIP鍙互鏄崟绾跨▼杩愯锛屽嵆鍙湁涓涓猼cpip绾跨▼锛坱cpip_thread锛夛紝璐熻矗澶勭悊鎵鏈夌殑tcp/ucp杩炴帴锛屽悇绉嶇綉缁滅▼搴忛兘閫氳繃tcpip绾跨▼涓庣綉缁滀氦浜掋備絾LwIP涔熷彲浠ュ绾跨▼杩愯锛屼互鎻愰珮鏁堢巼锛岄檷浣庣紪绋嬪鏉傚害銆傝繖鏃跺氨闇瑕佺敤鎴峰疄鐜板垱寤烘柊绾跨▼鐨勫嚱鏁帮細void sys_thread_new(void (* thread)(void *arg), void *arg);鍦?micro;C/OS II涓紝娌℃湁绾跨▼锛坱hread锛夌殑姒傚康锛屽彧鏈変换鍔★紙Task锛夈傚畠宸茬粡鎻愪緵浜嗗垱寤烘柊浠诲姟鐨勭郴缁烝PI璋冪敤OSTaskCreate锛屽洜姝ゅ彧瑕佹妸OSTaskCreate灏佽涓涓嬶紝灏卞彲浠ュ疄鐜皊ys_thread_new銆傞渶瑕佹敞鎰忕殑鏄疞wIP涓殑thread骞舵病鏈?micro;C/OS II涓紭鍏堢骇鐨勬蹇碉紝瀹炵幇鏃惰鐢辩敤鎴蜂簨鍏堜负LwIP涓垱寤虹殑绾跨▼鍒嗛厤濂戒紭鍏堢骇銆?銆乴ib_arch涓簱鍑芥暟鐨勫疄鐜帮細 LwIP鍗忚鏍堜腑鐢ㄥ埌浜?涓閮ㄥ嚱鏁帮紝杩欎簺鍑芥暟閫氬父涓庣敤鎴蜂娇鐢ㄧ殑绯荤粺鎴栫紪璇戝櫒鏈夊叧锛屽洜姝ょ暀缁欑敤鎴疯嚜宸卞疄鐜般傚涓嬶細u16_t htons(u16_t n); //16浣嶆暟鎹珮浣庡瓧鑺備氦鎹16_t ntohs(u16_t n);u32_t htonl(u32_t n); //32浣嶆暟鎹ぇ灏忓ご瀵硅皟u32_t ntohl(u32_t n);int strlen(const char *str); //杩斿洖瀛楃涓查暱搴nt strncmp(const char *str1, const char *str2, int len); //瀛楃涓叉瘮杈僾oid bcopy(const void *src, void *dest,

int len); //鍐呭瓨鏁版嵁鍧椾箣闂寸殑浜掔浉鎷疯礉void bzero(void *data, int n); //鍐呭瓨涓寚瀹氶暱搴︾殑鏁版嵁鍧楁竻闆? 鍓嶅洓涓嚱鏁伴氬父鐢辩敤鎴疯嚜宸卞疄鐜般傚湪鎴戠殑绯荤粺涓紝鐢变簬浣跨敤浜唃cc缂栬瘧鍣紝gcc鐨刲ib搴撻噷宸茬粡鏈変簡涓や釜瀛楃涓叉搷浣滃嚱鏁般傝嫢鐢ㄦ埛鐨勭紪璇戝櫒鐨勫簱涓病鏈夎繖浜涘嚱鏁帮紝闇瑕佽嚜宸辩紪鍐欍?銆佺綉缁滆澶囬┍鍔ㄧ▼搴忥細 鍦ㄦ垜鐨勭郴缁熶腑浣跨敤鐨勭綉缁滆姱鐗囦负RealTek鐨?019as鑺墖锛岃繖鏄疘SA 10BASE锛峊鐨勪互澶綉鑺墖锛屼笌Ne2k鍏煎銆傛墍浠ョ洰鍓嶅疄鐜扮殑缃戠粶璁惧椹卞姩鏄拡瀵筃e2k鐨勶紝鍏跺畠绫诲瀷鐨勭綉缁滆姱鐗囬┍鍔ㄥ彲浠ュ湪LwIP鐨勭綉绔欎笂鎵惧埌銆侺wIP鐨勭綉缁滈┍鍔ㄦ湁涓瀹氱殑妯″瀷锛寀cosii&LwIPsource etlwiparchucosII etif 涓殑ne2kif.c鏂囦欢鍗充负椹卞姩鐨勬ā鏉匡紝鐢ㄦ埛涓鸿嚜宸辩殑缃戠粶璁惧瀹炵幇椹卞姩鏃跺簲鍙傜収姝ゆā鏉裤? 鍦↙wIP涓彲浠ユ湁澶氫釜缃戠粶鎺ュ彛锛屾瘡涓綉缁滄帴鍙i兘瀵瑰簲浜嗕竴涓猻truct netif锛岃繖涓猲

e2kif鍖呭惈浜嗙浉搴旂綉缁滄帴鍙g殑灞炴с佹敹鍙戝嚱鏁般侺wIP璋冪敤ne2kif鐨勬柟娉昻etif->input()鍙妌etif->output()杩涜浠ュお缃憄acket鐨勬敹銆佸彂绛夋搷浣溿傚湪椹卞姩涓富瑕佸仛鐨勶紝灏辨槸瀹炵幇缃戠粶鎺ュ彛鐨勬敹銆佸彂銆佸垵濮嬪寲浠ュ強涓柇澶勭悊鍑芥暟銆傞┍鍔ㄧ▼搴忓伐浣滃湪IP鍗忚妯″瀷鐨勭綉缁滄帴鍙e眰锛屽畠鎻愪緵缁欎笂灞傦紙IP灞傦級鐨勬帴鍙e嚱鏁板涓嬶細//缃戝崱鍒濆鍖栧嚱鏁皏oid low_level_init (struct netif *netif)//缃戝崱鎺ユ敹鍑芥暟锛屼粠缃戠粶鎺ュ彛鎺ユ敹浠ュお缃戞暟鎹寘骞舵妸鍏朵腑鐨処P鎶ユ枃鍚慖P灞傚彂閫?/鍦ㄤ腑鏂柟寮忎笅鐢辩綉鍗SR璋冪敤void ne2k_recv_packet (struct netif *netif)//缃戝崱鍙戦佸嚱鏁帮紝缁橧P灞備紶杩囨潵鐨処P鎶ユ枃鍔犱笂浠ュお缃戝寘澶村苟閫氳繃缃戠粶鎺ュ彛鍙戦乪rr_t ne2k_send_packet (struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)//缃戝崱涓柇澶勭悊鍑芥暟ISRvoid ne2k_isr (void); 浠ヤ笂鐨勫嚱鏁伴兘鍙互鍒嗕负鍗忚鏍堟湰韬殑澶勭悊鍜屽缃戠粶鎺ュ彛纭欢鐨勬搷浣滀袱閮ㄤ唤锛屼絾纭欢鎿嶄綔鏄涓婂眰灞忚斀鐨勶紝鍏蜂綋鍙傝RTL8019as銆丏M9008绛塏e2k缃戠粶鑺墖鐨勬暟鎹墜鍐屻傞┍鍔ㄧ▼搴忓彲浠ュ埌LwIP鐨勭綉绔欎笅杞姐?銆佸簲鐢ㄥ疄渚嬬殑寤虹珛鍜屾祴璇? 鍋氬畬涓婇潰鐨勭Щ妞嶄慨鏀瑰伐浣滀互鍚庯紝灏卞彲浠ュ湪uCOS II涓垵濮嬪寲LwIP锛屽苟鍒涘缓TCP鎴朥DP浠诲姟杩涜娴嬭瘯浜嗐傝繖閮ㄤ唤瀹屽叏鏄疌璇█鐨勫疄鐜帮紝鍥犳杩欓儴浠藉湪ez80鍜孉RM7涓婂熀鏈兘鏄竴鏍风殑銆傚煎緱娉ㄦ剰鐨勬槸LwIP鐨勫垵濮嬪寲蹇呴』鍦╱COS II瀹屽叏鍚姩涔嬪悗涔熷氨鏄湪浠诲姟涓繘琛岋紝鍥犱负瀹冪殑鍒濆鍖栫敤鍒颁簡淇″彿閲忕瓑OS鐩稿叧鐨勬搷浣溿傚叧閿儴浠界殑浠g爜鍜岃鏄庡涓嬶細void start_kernel(void){ int LineNo10 = 0; int LineNo11 = 1; int LineNo12 = 2; int LineNo13 = 3; int LineNo14 = 4; OSInit(); OSTaskCreate(lwip_init_task, &LineNo10, &lwip_init_stk[TASK_STK_SIZE-1], 0); OSTaskCreate(usr_task,&LineNo14,&usr_stk[TASK_STK_SIZE-1],20); vRTCStart(); OSStart(); /* NEVER EXECUTED */ while(1);}涓荤▼搴忎腑鍒涘缓浜唋wip_init_task鍒濆鍖朙wIP浠诲姟锛堜紭鍏堢骇0锛夊拰usr_task鐢ㄦ埛浠诲姟锛堜紭鍏堢骇20锛夈俵wip_init_task浠诲姟涓櫎浜嗗垵濮嬪寲纭欢鏃堕挓鍜孡wIP涔嬪锛岃繕鍒涘缓浜唗cpip_thread锛堜紭鍏堢骇5锛夊拰tcpecho_thread锛堜紭鍏堢骇6锛夈傚疄闄呬笂tcpip_thread鎵嶆槸LwIP鐨勪富绾跨▼锛屽绾跨▼鐨凚erkley API涔熸槸鍩轰簬杩欎釜绾跨▼瀹炵幇鐨勶紝鍗充笂闈㈢殑tcpecho_thread绾跨▼涔熻渚濋潬tcpip_thread绾跨▼鏉ヤ笌澶栫晫閫氫俊锛岃繖鏍峰仛鐨勫ソ澶勬槸缂栫▼绠鍗曪紝缁撴瀯娓呮櫚銆傚疄鐢˙erkley API瀹炵幇鐨則cpecho_thread鏄竴涓猅CP echo鏈嶅姟鍣紝鐩戝惉7鍙风鍙o紝绋嬪簭妗嗘灦濡備笅锛歷oid tcpecho_thread(void *arg){conn = netconn_new(NETCONN_TCP); //鍒涘缓鏂扮殑杩炴帴鏍囪瘑netconn_bind(conn, NULL, 7); //缁戝畾鍒?鍙风鍙etconn_listen(conn); //寮濮嬬洃鍚鍙? while(1){ newconn = netconn_accept(conn); //鎺ユ敹澶栭儴鍒版潵鐨勮繛鎺? buf = netconn_recv(newconn) //鑾峰彇鏁版嵁 ……. //澶勭悊鏁版嵁 netconn_write(newconn, data, len, NETCONN_COPY); //鍙戦佹暟鎹? netconn_delete(newconn); //閲婃斁鏈杩炴帴} } 缂栬瘧杩愯鍚庯紝鐢╬ing ip鍦板潃鍛戒护鍙互寰楀埌ICMP reply鍝嶅簲銆傜敤telnet ip鍦板潃 7锛堢櫥褰?鍙风鍙o級鍛戒护鍙互鐪嬪埌echo server鐨勫洖鏄炬晥鏋溿傝鏄嶢RP銆両CMP銆両P銆乀CP鍗忚閮藉凡姝g‘杩愯銆