本文会利用W5500实现服务器,并自动分配网络IP地址,使用SPI轮询方式和SPI+DMA方式,实现网络通信
不会讲解W5500代码,只是快速搭建
一、SPI轮询方式驱动W5500
1、CubeMx配置
由于引脚没有硬件上拉电阻,所以使用内部上拉
生成工程
2、代码移植编写
复制代码文件到工程路径下:此文件可以从后面工程文件获取
修改引脚端口
为DHCP所需定时器实现回调函数调用
main.c主函数调用W550配置,并启动服务器
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "w5500.h"
#include "W5500_conf.h"
#include "socket.h"
#include "utility.h"
#include "dhcp.h"
#include "tcp_demo.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_TIM6_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
gpio_for_w5500_config(); /*初始化MCU相关引脚*/
reset_w5500(); /*硬复位W5500*/
set_w5500_mac(); /*配置MAC地址*/
socket_buf_init(txsize, rxsize); /*初始化8个Socket的发送接收缓存大小*/
IP_AutoSet(3500);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
do_tcp_server();
}
/* USER CODE END 3 */
}
收发回显测试
收发速度35.5KB/秒
二、SPI+DMA方式驱动W5500
1、CubeMx配置
沿用上一个工程得基础直接修改
生成代码
2、代码移植编写
文件复制:
其余代码不用修改
记得把printf打印所需要得库勾选
可见接收依旧很慢
主要是发送会增加,向上位机发送数据速度测试达到200多KB/s
三、工程文件下载
21.0_W5500_所需文件
链接:https://pan.baidu.com/s/1HpFj8-_SDYefrTu02EATtg
提取码:9knm
21.1_STM32H750_W5500_SPI轮询方式
链接:https://pan.baidu.com/s/1Lc_plrbWAREKgrgDq-VhZA
提取码:5ee7
21.2_STM32H750_W5500_SPI_DMA方式
链接:https://pan.baidu.com/s/1riEKrhBvXRQbc8sNRkNS5Q
提取码:dytm