|
stm控制W5100S的程序
51hei.png (5.27 KB, 下載次數(shù): 89)
下載附件
2021-1-19 21:47 上傳
單片機(jī)源程序如下:
- #include "socket.h"
- static uint16 local_port;
- extern uint16 sent_ptr;
-
- #define __MACRAW__
- /**
- @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5200 done it.
- @return 1 for sucess else 0.
- */
- void setkeepalive(SOCKET s);
- uint8 socket(SOCKET s, uint8 protocol, uint16 port, uint8 flag) // 2017-07-17
- {
- uint8 ret;
- if (
- ((protocol&0x0F) == Sn_MR_TCP) ||
- ((protocol&0x0F) == Sn_MR_UDP) ||
- ((protocol&0x0F) == Sn_MR_IPRAW) ||
- ((protocol&0x0F) == Sn_MR_MACRAW)
-
- )
- {
- close(s);
-
- if((protocol&0x0F)==Sn_MR_TCP)
- {
- setkeepalive(s);
- }
- IINCHIP_WRITE(W5100S_Sn_MR(s) ,protocol | flag);
- if (port != 0) {
- IINCHIP_WRITE( W5100S_Sn_PORT0(s) ,(uint8)((port & 0xff00) >> 8));
- IINCHIP_WRITE( W5100S_Sn_PORT1(s) ,(uint8)(port & 0x00ff));
- } else {
- local_port++; // if don't set the source port, set local_port number.
- IINCHIP_WRITE(W5100S_Sn_PORT0(s) ,(uint8)((local_port & 0xff00) >> 8));
- IINCHIP_WRITE(W5100S_Sn_PORT1(s) ,(uint8)(local_port & 0x00ff));
- }
- IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_OPEN); // run sockinit Sn_CR
- /* wait to process the command... */
- while( IINCHIP_READ(W5100S_Sn_CR(s)) )
- ;
- /* ------- */
- ret = 1;
- }
- else
- {
- ret = 0;
- }
- return ret;
- }
- /**
- @brief This function close the socket and parameter is "s" which represent the socket number
- */
- void close(SOCKET s)
- {
- IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_CLOSE); //SOCKET關(guān)閉
- /* wait to process the command... */
- while( IINCHIP_READ(W5100S_Sn_CR(s) ) )
- ;
- /* ------- */
- /* all clear */
- IINCHIP_WRITE( W5100S_Sn_IR(s) , 0xFF);
- }
- /**
- @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
- @return 1 for success else 0.
- */
- uint8 listen(SOCKET s) //設(shè)置為等待客戶端發(fā)出請(qǐng)求模式
- {
- uint8 ret;
- if (IINCHIP_READ( W5100S_Sn_SR(s) ) == Sn_SR_INIT) //指示SOCKET打開并處于TCP模式
- {
- IINCHIP_WRITE(W5100S_Sn_CR(s) ,Sn_CR_LISTEN); //設(shè)置為等待客戶端發(fā)出請(qǐng)求模式
- /* wait to process the command... */
- while( IINCHIP_READ(W5100S_Sn_CR(s) ) ) //等待設(shè)置完成
- ;
- /* ------- */
- ret = 1;
- }
- else
- {
- ret = 0;
- }
- return ret;
- }
- /**
- @brief This function established the connection for the channel in Active (client) mode.
- This function waits for the untill the connection is established.
- @return 1 for success else 0.
- */
- uint8 connect(SOCKET s, uint8 * addr, uint16 port)
- {
- uint8 ret; // ret定義為是否連接的標(biāo)志位,ret=0 連接中斷;ret=1連接成功
- if
- (
- ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
- ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
- (port == 0x00)
- )
- {
- ret = 0; // 如果IP地址和Port無(wú)法獲取,則連接中斷
- }
- else // 如果目的IP和Port未設(shè)置,則進(jìn)行設(shè)置
- {
- ret = 1;
- IINCHIP_WRITE( W5100S_Sn_DIPR0(s), addr[0]);
- IINCHIP_WRITE( W5100S_Sn_DIPR1(s), addr[1]);
- IINCHIP_WRITE( W5100S_Sn_DIPR2(s), addr[2]);
- IINCHIP_WRITE( W5100S_Sn_DIPR3(s), addr[3]);
- IINCHIP_WRITE( W5100S_Sn_DPORT0(s), (uint8)((port & 0xff00) >> 8));
- IINCHIP_WRITE( W5100S_Sn_DPORT1(s), (uint8)(port & 0x00ff));
-
- IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_CONNECT); // Sn_CR數(shù)值設(shè)為0x04,并執(zhí)行TCP連接請(qǐng)求命令
- while ( IINCHIP_READ(W5100S_Sn_CR(s) ) ) ; // MCU讀取Sn_CR(s)的數(shù)值
- while ( IINCHIP_READ(W5100S_Sn_SR(s)) != Sn_SR_SYNSEND ) // 此時(shí)Sn_SR(s)寄存器應(yīng)該處于SOCK_SYNSENT,下面排除不在該狀態(tài)的幾種情況
- {
- if(IINCHIP_READ(W5100S_Sn_SR(s)) == Sn_SR_ESTABLISHED) // Socket連接已經(jīng)建立,正常連接
- {
- break;
- }
- if (getSn_IR(s) & Sn_IR_TIMEOUT) // 當(dāng)ARPto或TCPto超時(shí),異常
- {
- IINCHIP_WRITE(W5100S_Sn_IR(s), (Sn_IR_TIMEOUT)); // 通知MCU該中斷,并清中斷
- ret = 0; // ret置0,連接中斷
- break;
- }
- }
- }
- return ret;
- }
- /**
- @brief This function used for disconnect the socket and parameter is "s" which represent the socket number
- @return 1 for success else 0.
- */
- void disconnect(SOCKET s)
- {
- IINCHIP_WRITE( W5100S_Sn_CR(s) ,Sn_CR_DISCON);
- /* wait to process the command... */
- while( IINCHIP_READ(W5100S_Sn_CR(s) ) )
- ;
- /* ------- */
- }
- /**
- @brief This function used to send the data in TCP mode
- @return 1 for success else 0.
- */
- uint16 send(SOCKET s, const uint8 * buf, uint16 len)
- {
- uint8 status=0;
- uint16 ret=0;
- uint16 freesize=0;
- if (len > getSn_TXBUF_SIZE( s)*1024)
- ret = getSn_TXBUF_SIZE( s)*1024;
- else ret = len;
- do
- {
- freesize = getSn_TX_FSR(s);
- status = IINCHIP_READ(W5100S_Sn_SR(s));
- if ((status != Sn_SR_ESTABLISHED) && (status != Sn_SR_CLOSE_WAIT))
- {
-
- printf("status break\r\n");
- ret = 0;
- break;
- }
- }
- while (freesize < ret);
- send_data_processing(s, (uint8 *)buf, ret);
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼 網(wǎng)絡(luò)調(diào)試助手:http://www.zg4o1577.cn/bbs/dpj-201792-1.html
所有資料51hei提供下載:
STM32F407通過(guò)W5100S進(jìn)行網(wǎng)絡(luò)通信.7z
(5.15 MB, 下載次數(shù): 58)
2021-1-19 22:35 上傳
點(diǎn)擊文件名下載附件
STM32F407通過(guò)W5100S進(jìn)行網(wǎng)絡(luò)通信 下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|