【STM32F723 Discovery Kit】SPI driver W5500 module ping network
[Copy link]
In this article, we will learn how to use the STM32F723 Discovery Kit development board to drive the W5500 module in SPI mode to ping the network.
1. Hardware Circuit
The test interface uses PMOD's SPI2. When using this interface, use PMOD_SEL_0 and PMOD_SEL_1 to switch the interface function.
Hardware connection diagram
II. Procedure
Some drivers
2.1、spi.c
#include "main.h"
#include "spi/spi.h"
SPI_HandleTypeDef hspi2;
void init_spi(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
/**SPI1 GPIO Configuration
PI3 ------> SPI2_MOSI
PI2 ------> SPI2_MISO
PI1 ------> SPI2_SCK
*/
GPIO_InitStruct.Pin = GPIO_PIN_3 | GPIO_PIN_2 | GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
//PH15 ----> PMOD_SEL_0
//PI10 ----> PMOD_SEL_1
GPIO_InitStruct.Pin = GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOH, GPIO_PIN_15, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_10, GPIO_PIN_RESET);
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 7;
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
}
}
2.2, w5500_conf.c
The w5500 module driver part is mainly modified in this file
/**
******************************************************************************
* [url=home.php?mod=space&uid=1307177]@File[/url] w5500_conf.c
* [url=home.php?mod=space&uid=252314]@version[/url] V1.2
* [url=home.php?mod=space&uid=311857]@date[/url] 2017-11-01
* [url=home.php?mod=space&uid=159083]@brief[/url] 3.5.0°±¨¤°
* [url=home.php?mod=space&uid=1020061]@attention[/url] MCUw5500òèw5500MACIP·
******************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include "w5500_conf.h"
//#include "bsp_spi_flash.h"
#include "utility.h"
#include "w5500.h"
#include "dhcp.h"
//#include "bsp_TiMbase.h"
extern SPI_HandleTypeDef hspi2;
CONFIG_MSG ConfigMsg; /*á*/
EEPROM_MSG_STR EEPROM_MSG; /*EEPROMá*/
/*MAC·××±*/
/*àéW5500°¤×÷MAC·*/
uint8 mac[6]={0x00,0x08,0xdc,0x11,0x11,0x11};
/*¨IP*/
uint8 local_ip[4]={192,168,1,25}; /*¨W5500IP·*/
uint8 subnet[4]={255,255,255,0}; /*¨W5500×*/
uint8 gateway[4]={192,168,1,1}; /*¨W5500*/
uint8 dns_server[4]={114,114,114,114}; /*¨W5500DNS*/
uint16 local_port=8080; /*¨±*/
/*¨IP*/
uint8 remote_ip[4]={192,168,1,160}; /*IP·*/
uint16 remote_port=8080; /**/
/*IP··¨×*/
uint8 ip_from=IP_FROM_DEFINE;
uint8 dhcp_ok=0; /*dhcpIP*/
uint32 ms=0; /**/
uint32 dhcp_time= 0; /*DHCP*/
vu8 ntptimer = 0; /*NPT*/
/**
*@brief W5500IP·
*@param
*[url=home.php?mod=space&uid=784970]@return[/url]
*/
void set_w5500_ip(void)
{
/*¨á*/
memcpy(ConfigMsg.mac, mac, 6);
memcpy(ConfigMsg.lip,local_ip,4);
memcpy(ConfigMsg.sub,subnet,4);
memcpy(ConfigMsg.gw,gateway,4);
memcpy(ConfigMsg.dns,dns_server,4);
if(ip_from==IP_FROM_DEFINE)
printf(" ¨IPW5500:\r\n");
/*EEPROMIP*/
if(ip_from==IP_FROM_EEPROM)
{
/*EEPROMIP*/
//read_config_from_eeprom();
/*EEPROMMAC,ò*/
if( *(EEPROM_MSG.mac)==0x00&& *(EEPROM_MSG.mac+1)==0x08&&*(EEPROM_MSG.mac+2)==0xdc)
{
printf(" IP from EEPROM\r\n");
/*EEPROMá±*/
memcpy(ConfigMsg.lip,EEPROM_MSG.lip, 4);
memcpy(ConfigMsg.sub,EEPROM_MSG.sub, 4);
memcpy(ConfigMsg.gw, EEPROM_MSG.gw, 4);
}
else
{
printf(" EEPROM,¨IPW5500,EEPROM\r\n");
//write_config_to_eeprom(); /*IPEEPROM*/
}
}
/*DHCPIPè÷DHCP×*/
if(ip_from==IP_FROM_DHCP)
{
/*DHCPá*/
if(dhcp_ok==1)
{
printf(" IP from DHCP\r\n");
memcpy(ConfigMsg.lip,DHCP_GET.lip, 4);
memcpy(ConfigMsg.sub,DHCP_GET.sub, 4);
memcpy(ConfigMsg.gw,DHCP_GET.gw, 4);
memcpy(ConfigMsg.dns,DHCP_GET.dns,4);
}
else
{
printf(" DHCP×ò,ò\r\n");
printf(" ¨IPW5500\r\n");
}
}
/*ùè*/
ConfigMsg.sw_ver[0]=FW_VER_HIGH;
ConfigMsg.sw_ver[1]=FW_VER_LOW;
/*IPW5500à÷*/
setSUBR(ConfigMsg.sub);
setGAR(ConfigMsg.gw);
setSIPR(ConfigMsg.lip);
getSIPR (local_ip);
printf(" W5500 IP· : %d.%d.%d.%d\r\n", local_ip[0],local_ip[1],local_ip[2],local_ip[3]);
getSUBR(subnet);
printf(" W5500 × : %d.%d.%d.%d\r\n", subnet[0],subnet[1],subnet[2],subnet[3]);
getGAR(gateway);
printf(" W5500 : %d.%d.%d.%d\r\n", gateway[0],gateway[1],gateway[2],gateway[3]);
}
/**
*@brief W5500MAC·
*@param
*@return
*/
void set_w5500_mac(void)
{
memcpy(ConfigMsg.mac, mac, 6);
setSHAR(ConfigMsg.mac); /**/
memcpy(DHCP_GET.mac, mac, 6);
}
/**
*@brief W5500GPIO
*@param
*@return
*/
void gpio_for_w5500_config(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOI_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
//PI0 ----> ETH_CS
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOI, &GPIO_InitStruct);
//PF11 ----> ETH_RST
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
//PB11 ----> ETH_INT
GPIO_InitStruct.Pin = GPIO_PIN_11;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET);
}
/**
*@brief W5500è
*@param val: °0±±í°1±±í
*@return
*/
void wiz_cs(uint8_t val)
{
#ifdef STM32_SPI1
if (val == LOW)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
}
else if (val == HIGH)
{
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
#endif
#ifdef STM32_SPI2
if (val == LOW)
{
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_0, GPIO_PIN_RESET);
}
else if (val == HIGH)
{
HAL_GPIO_WritePin(GPIOI, GPIO_PIN_0, GPIO_PIN_SET);
}
#endif
}
/**
*@brief èW5500SCSn
*@param
*@return
*/
void iinchip_csoff(void)
{
wiz_cs(LOW);
}
/**
*@brief èW5500SCSn
*@param
*@return
*/
void iinchip_cson(void)
{
wiz_cs(HIGH);
}
/**
*@brief W5500è
*@param
*@return
*/
void reset_w5500(void)
{
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_RESET);
delay_us(50);
HAL_GPIO_WritePin(GPIOF, GPIO_PIN_11, GPIO_PIN_SET);
delay_us(50);
}
uint8_t SPI_FLASH_SendByte(uint8_t byte)
{
u8 temp = 0;
HAL_SPI_TransmitReceive(&hspi2,&byte,&temp,1,1000);
return temp;
}
/**
*@brief STM32 SPI18
*@param dat8
*@return
*/
uint8 IINCHIP_SpiSendData(uint8 dat)
{
return(SPI_FLASH_SendByte(dat));
}
/**
*@brief 8W5500
*@param addrbsb: ·
*@param data8
*@return
*/
void IINCHIP_WRITE( uint32 addrbsb, uint8 data)
{
iinchip_csoff();
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);
IINCHIP_SpiSendData( (addrbsb & 0x000000F8) + 4);
IINCHIP_SpiSendData(data);
iinchip_cson();
}
/**
*@brief W55008
*@param addrbsb: ·
*@param data·8
*@return
*/
uint8 IINCHIP_READ(uint32 addrbsb)
{
uint8 data = 0;
//printf(" addrbsb=%x\r\n",addrbsb);
iinchip_csoff();
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);
IINCHIP_SpiSendData( (addrbsb & 0x000000F8)) ;
data = IINCHIP_SpiSendData(0x00);
iinchip_cson();
return data;
}
/**
*@brief òW5500len×
*@param addrbsb: ·
*@param buf×·
*@param len×·¤
*@return len·×·¤
*/
uint16 wiz_write_buf(uint32 addrbsb,uint8* buf,uint16 len)
{
uint16 idx = 0;
if(len == 0) printf(" Unexpected2 length 0\r\n");
iinchip_csoff();
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);
IINCHIP_SpiSendData( (addrbsb & 0x000000F8) + 4);
for(idx = 0; idx < len; idx++)
{
IINCHIP_SpiSendData(buf[idx]);
}
iinchip_cson();
return len;
}
/**
*@brief W5500len×
*@param addrbsb: ·
*@param buf·
*@param len×·¤
*@return len·×·¤
*/
uint16 wiz_read_buf(uint32 addrbsb, uint8* buf,uint16 len)
{
uint16 idx = 0;
if(len == 0)
{
printf(" Unexpected2 length 0\r\n");
}
iinchip_csoff();
IINCHIP_SpiSendData( (addrbsb & 0x00FF0000)>>16);
IINCHIP_SpiSendData( (addrbsb & 0x0000FF00)>> 8);
IINCHIP_SpiSendData( (addrbsb & 0x000000F8));
for(idx = 0; idx < len; idx++)
{
buf[idx] = IINCHIP_SpiSendData(0x00);
}
iinchip_cson();
return len;
}
/**
*@brief EEPROM
*@param
*@return
*/
void write_config_to_eeprom(void)
{
//uint16 dAddr=0;
//ee_WriteBytes(ConfigMsg.mac,dAddr,(uint8)EEPROM_MSG_LEN);
//delay_ms(10);
}
/**
*@brief EEPROM
*@param
*@return
*/
void read_config_from_eeprom(void)
{
//ee_ReadBytes(EEPROM_MSG.mac,0,EEPROM_MSG_LEN);
//delay_us(10);
}
/**
*@brief STM32¨±÷2
*@param
*@return
*/
void timer2_init(void)
{
// TIM2_Configuration(); /* TIM2 ¨± */
// TIM2_NVIC_Configuration(); /* ¨±÷ */
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE); /* TIM2 ±± */
}
/**
*@brief dhcp¨±÷
*@param
*@return
*/
void dhcp_timer_init(void)
{
//timer2_init();
init_tim();
}
/**
*@brief ntp¨±÷
*@param
*@return
*/
void ntp_timer_init(void)
{
//timer2_init();
init_tim();
}
/**
*@brief ¨±÷2
*@param
*@return
*/
//void timer2_isr(void)
void timer2_hdl(void)
{
ms++;
if(ms>=1000)
{
ms=0;
dhcp_time++; /*DHCP¨±1S*/
#ifndef __NTP_H__
ntptimer++; /*NTP±1S*/
#endif
}
}
/**
*@brief STM32í
*@param
*@return
*/
void reboot(void)
{
// pFunction Jump_To_Application;
// uint32 JumpAddress;
printf(" ......\r\n");
// JumpAddress = *(vu32*) (0x00000004);
// Jump_To_Application = (pFunction) JumpAddress;
// Jump_To_Application();
}
2.3, main.c
#include "main.h"
#include "led/led.h"
#include "delay/delay.h"
#include "key/key.h"
#include "usart/usart.h"
#include "sram/sram.h"
#include "STM32F723E-Discovery/stm32f723e_discovery_lcd.h"
#include "STM32F723E-Discovery/stm32f723e_discovery_qspi.h"
#include "i2c/i2c.h"
#include "spi/spi.h"
#include "tim/tim.h"
#include "w5500_conf.h"
#include "w5500.h"
#include "ping.h"
static void MPU_Config(void);
static void SystemClock_Config(void);
static void CPU_CACHE_Enable(void);
int main(void)
{
uint16_t i=0;
uint8_t txbuf[10];
uint8_t rxbuf[10];
HAL_Init();
MPU_Config(); /* Configure the MPU attributes */
CPU_CACHE_Enable(); /* Enable the CPU Cache */
SystemClock_Config(); /* Configure the system clock to 216 MHz */
delay_init(216);
led_init();
usart_init(115200);
init_spi();
BSP_LCD_Init();
gpio_for_w5500_config();
reset_w5500();
PHY_check();
set_w5500_mac();
set_w5500_ip();
socket_buf_init(txsize, rxsize);
printf(" 应用程序执行中......\r\n");
LED5(0);
LED6(1);
BSP_LCD_Clear(LCD_COLOR_RED);
while (1)
{
do_ping();
if(req>=4)
{
close(SOCK_PING);
break;
}
}
}
2.4, w5500 source code
3. Program running
3.1、Ping the network
3.2. Serial port output during operation
|