STM32-Arduino编程 W5500 UDP通信

7 篇文章 2 订阅
7 篇文章 0 订阅

环境说明

开发软件:vscode+PlatformIO
操作系统:win10
开发板:STM32F103C8T6
UDP通信模块:W5500网络模块

使用Arduino开发STM32原因

STM32开发比较常用的软件有Keil和STM32Cube,但是用过的同学都应该发现这两款软件对于新手不怎么友好,需要花费很长的时间才能做到入门。特别是在开发一些比较复杂的程序时,比如植入freertos,安排几个任务,能把人给折磨死。与Keil和STM32Cube相同的的是,STM32-Arduino也是采用的库函数开发,并不存在低人一等的说法。当然需要承认的是相比于寄存器开发的程序还是要差一点的。STM32-Arduino在库函数得到基础上进一步的封装,可以大大的降低程序中出现的配置出错的问题,让新手的同学可以更快的开发出高质量的STM32的程序。特别是UDP通信,绝对可以说极大的体现了STM32-Arduino开发程序质量的优越性。

直接开始操作

首先打开platformIO,创建工程。
在这里插入图片描述
工程名称随便,要设定好对应的开发板。我这里使用的是开发板STM32F103C8T6,至于为什么要用这个开发板,主要是因为穷。如果需要使用其他的板子,在创建工程时设定好对应的板子即可。
在这里插入图片描述

工程创建好之后,在Libraries中搜索W5500,便可以找到Ethernet3库,将其加入到STM32UDP的工程中。为什么不使用Ethernet2的库,根据库的说明,这个库也是适用于STM32开发板的呀,可能就是缘分吧,Ethernet2那个库导入工程后,最后UDP通信没反应,Ethernet3这个库一下子就通了。
在这里插入图片描述

Ethernet3的库中找到example文件夹中找到例程,将其中一个例程复制到main.cpp中,我复制的是UDPSendReceiveString.ino中的例程。

#include <Arduino.h>
/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet3.h>
#include <EthernetUdp3.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 177);

unsigned int localPort = 8888;      // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if (packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i = 0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());
    // read the packet into packetBufffer
    Udp.read(packetBuffer, packetSize);
    //Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(packetBuffer,packetSize);
    //Udp.write(packetBuffer);
    //Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

说到这里,是不是就可以直接进行UDP通信了呢?当然不形,因为这个库默认不是给STM32使用的,在使用W5500模块是还需要配置SPI的引脚、SCS和RST引脚,不进行重新的配置,程序将无法运行。修改的地方总共有两处,首先在W5500.cpp文件中找到init函数,在该函数中默认配置的是SPI,但是在相关的程序中使用go to Definition是找不到SPI到底是配置哪些引脚的,所以就很坑。所以这里用SPIClass定义一个自己的SPI,然后用自己定义的SPI替换掉原本的SPI即可。
在这里插入图片描述

如上图用SPIClass定义一个SPI_2的变量,根据STM32的SPI对应的引脚填进去即可,然后用SPI_2.替换掉SPI.即可,在替换前确定替换的总数量在31个左右,否则替换的字符可能是错的。
还有一处需要进行修改的是在Ethernet3.cpp中,搜索w5500.init,一共有四处,在其前面均加上 setCsPin(PIN1);setRstPin(PIN2);其中PIN是配置的Cs引脚,PIN2是配置的Rst引脚,这里我使用的是PA4和PA3。另外最好不要试图直接从SPIClass中进行修改。

测试的硬件如下图所示:
在这里插入图片描述

如果感觉程序都对了,但是测试没有反应,可能是SPI的MISO和MOSI接反了。测试过UDP通信你会发现,正点原子和普中的UDP通信的例程简直都是渣渣,UDP通信根本不存发几串数据还能卡住的问题,UDP通信相当的丝滑。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值