本帖最后由 比神乐 于 2023-3-15 10:07 编辑
今天搞了一下网口TCP服务器。
CS:PC10;SCLK:PA1;MOSI:PA7;MISO:PA6;RST:PF7.
用硬件SPI没搞成功,只好模拟时序。
原理图:
主程序代码:#include "main.h"
#include "W5500.h"
#include <string.h>
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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 ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
static void MX_GPIO_Init(void);
static void MX_ICACHE_Init(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
unsigned int W5500_Send_Delay_Counter=0; //W5500·¢ËÍÑÓʱ¼ÆÊý±äÁ¿(ms)
unsigned char temp=0;
/*******************************************************************************
* º¯ÊýÃû : W5500_Initialization
* ÃèÊö : W5500³õʼ»õÅäÖÃ
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void W5500_Initialization(void)
{
W5500_Init(); //³õʼ»¯W5500¼Ä´æÆ÷º¯Êý
Detect_Gateway(); //¼ì²éÍø¹Ø·þÎñÆ÷
Socket_Init(0); //Ö¸¶¨Socket(0~7)³õʼ»¯,³õʼ»¯¶Ë¿Ú0
}
/*******************************************************************************
* º¯ÊýÃû : Load_Net_Parameters
* ÃèÊö : ×°ÔØÍøÂç²ÎÊý
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : Íø¹Ø¡¢ÑÚÂë¡¢ÎïÀíµØÖ·¡¢±¾»úIPµØÖ·¡¢¶Ë¿ÚºÅ¡¢Ä¿µÄIPµØÖ·¡¢Ä¿µÄ¶Ë¿ÚºÅ¡¢¶Ë¿Ú¹¤×÷ģʽ
*******************************************************************************/
void Load_Net_Parameters(void)
{
Gateway_IP[0] = 169;//¼ÓÔØÍø¹Ø²ÎÊý
Gateway_IP[1] = 254;
Gateway_IP[2] = 159;
Gateway_IP[3] = 1;
Sub_Mask[0]=255;//¼ÓÔØ×ÓÍøÑÚÂë
Sub_Mask[1]=255;
Sub_Mask[2]=0;
Sub_Mask[3]=0;
Phy_Addr[0]=0x6D;//¼ÓÔØÎïÀíµØÖ·
Phy_Addr[1]=0x58;
Phy_Addr[2]=0x49;
Phy_Addr[3]=0xFC;
Phy_Addr[4]=0xA9;
Phy_Addr[5]=0xB4;
IP_Addr[0]=169;//¼ÓÔر¾»úIPµØÖ·
IP_Addr[1]=254;
IP_Addr[2]=159;
IP_Addr[3]=40;
S0_Port[0] = 0x13;//¼ÓÔض˿Ú0µÄ¶Ë¿ÚºÅ5000
S0_Port[1] = 0x88;
S0_DIP[0]=169;//¼ÓÔض˿Ú0µÄÄ¿µÄIPµØÖ·
S0_DIP[1]=254;
S0_DIP[2]=159;
S0_DIP[3]=18;
S0_DPort[0] = 0x17;//¼ÓÔض˿Ú0µÄÄ¿µÄ¶Ë¿ÚºÅ6000
S0_DPort[1] = 0x70;
S0_Mode=TCP_SERVER;//¼ÓÔض˿Ú0µÄ¹¤×÷ģʽ,TCP¿Í»§¶Ëģʽ
}
/*******************************************************************************
* º¯ÊýÃû : W5500_Socket_Set
* ÃèÊö : W5500¶Ë¿Ú³õʼ»¯ÅäÖÃ
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ·Ö±ðÉèÖÃ4¸ö¶Ë¿Ú,¸ù¾Ý¶Ë¿Ú¹¤×÷ģʽ,½«¶Ë¿ÚÖÃÓÚTCP·þÎñÆ÷¡¢TCP¿Í»§¶Ë»òUDPģʽ.
* ´Ó¶Ë¿Ú״̬×Ö½ÚSocket_State¿ÉÒÔÅж϶˿ڵŤ×÷Çé¿ö
*******************************************************************************/
void W5500_Socket_Set(void)
{
if(S0_State==0)//¶Ë¿Ú0³õʼ»¯ÅäÖÃ
{
if(S0_Mode==TCP_SERVER)//TCP·þÎñÆ÷ģʽ
{
if(Socket_Listen(0)==TRUE)
S0_State=S_INIT;
else
S0_State=0;
}
else if(S0_Mode==TCP_CLIENT)//TCP¿Í»§¶Ëģʽ
{
if(Socket_Connect(0)==TRUE)
S0_State=S_INIT;
else
S0_State=0;
}
else//UDPģʽ
{
if(Socket_UDP(0)==TRUE)
S0_State=S_INIT|S_CONN;
else
S0_State=0;
}
}
}
/*******************************************************************************
* º¯ÊýÃû : Process_Socket_Data
* ÃèÊö : W5500½ÓÊÕ²¢·¢ËͽÓÊÕµ½µÄÊý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ±¾¹ý³ÌÏȵ÷ÓÃS_rx_process()´ÓW5500µÄ¶Ë¿Ú½ÓÊÕÊý¾Ý»º³åÇø¶ÁÈ¡Êý¾Ý,
* È»ºó½«¶ÁÈ¡µÄÊý¾Ý´ÓRx_Buffer¿½±´µ½Temp_Buffer»º³åÇø½øÐд¦Àí¡£
* ´¦ÀíÍê±Ï£¬½«Êý¾Ý´ÓTemp_Buffer¿½±´µ½Tx_Buffer»º³åÇø¡£µ÷ÓÃS_tx_process()
* ·¢ËÍÊý¾Ý¡£
*******************************************************************************/
void Process_Socket_Data(SOCKET s)
{
unsigned short size;
size=Read_SOCK_Data_Buffer(s, Rx_Buffer);
if(Rx_Buffer[0]=='1')
BSP_LED_On(LED1);
if(Rx_Buffer[0]=='0')
BSP_LED_Off(LED1);
memcpy(Tx_Buffer, Rx_Buffer, size);
Write_SOCK_Data_Buffer(s, Tx_Buffer, size);
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* STM32U5xx HAL library initialization:
- Configure the Flash prefetch
- Configure the Systick to generate an interrupt each 1 msec
- Set NVIC Group Priority to 3
- Low Level Initialization
*/
/* 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();
/* Configure the System Power */
SystemPower_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ICACHE_Init();
/* USER CODE BEGIN 2 */
/* Configure LED1, LED2 and LED3 */
BSP_LED_Init(LED1);
BSP_LED_Init(LED2);
BSP_LED_Init(LED3);
/* Configure User push-button button */
/*##-1- Start the Full Duplex Communication process ########################*/
/* While the SPI in TransmitReceive process, user can transmit data through
"aTxBuffer" buffer & receive data through "aRxBuffer" */
/* Timeout is set to 5S */
//BSP_LED_On(LED1);
/* USER CODE END 2 */
Load_Net_Parameters(); //×°ÔØÍøÂç²ÎÊý
W5500_Hardware_Reset(); //Ó²¼þ¸´Î»W5500
//BSP_LED_On(LED2);
W5500_Initialization(); //W5500³õʼ»õÅäÖÃ
//BSP_LED_On(LED3);
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
W5500_Socket_Set();//W5500¶Ë¿Ú³õʼ»¯ÅäÖÃ
W5500_Interrupt_Process();//W5500Öжϴ¦Àí³ÌÐò¿ò¼Ü
if((S0_Data & S_RECEIVE) == S_RECEIVE)//Èç¹ûSocket0½ÓÊÕµ½Êý¾Ý
{
S0_Data&=~S_RECEIVE;
Process_Socket_Data(0);//W5500½ÓÊÕ²¢·¢ËͽÓÊÕµ½µÄÊý¾Ý
}
else if(W5500_Send_Delay_Counter >= 50000)//¶¨Ê±·¢ËÍ×Ö·û´®
{
if(S0_State == (S_INIT|S_CONN))
{
S0_Data&=~S_TRANSMITOK;
memcpy(Tx_Buffer, "\r\nWelcome To ChuangWeiElec!\r\n", 27);
Write_SOCK_Data_Buffer(0, Tx_Buffer, 27);//Ö¸¶¨Socket(0~7)·¢ËÍÊý¾Ý´¦Àí,¶Ë¿Ú0·¢ËÍ27×Ö½ÚÊý¾Ý
}
W5500_Send_Delay_Counter=0;
}
W5500_Send_Delay_Counter++;
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
RCC_OscInitStruct.MSIState = RCC_MSI_ON;
RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 80;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] Power Configuration
* @retval None
*/
static void SystemPower_Config(void)
{
/*
* Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
*/
HAL_PWREx_DisableUCPDDeadBattery();
/*
* Switch to SMPS regulator instead of LDO
*/
if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
{
Error_Handler();
}
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] ICACHE Initialization Function
* @param None
* @retval None
*/
static void MX_ICACHE_Init(void)
{
/* USER CODE BEGIN ICACHE_Init 0 */
/* USER CODE END ICACHE_Init 0 */
/* USER CODE BEGIN ICACHE_Init 1 */
/* USER CODE END ICACHE_Init 1 */
/** Enable instruction cache in 1-way (direct mapped cache)
*/
if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
{
Error_Handler();
}
if (HAL_ICACHE_Enable() != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ICACHE_Init 2 */
/* USER CODE END ICACHE_Init 2 */
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef gpio_init_structure;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
/* Configure the GPIO_LED pin */
gpio_init_structure.Pin = GPIO_PIN_1 | GPIO_PIN_7;
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_7;
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOF, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_10;
gpio_init_structure.Mode = GPIO_MODE_OUTPUT_PP;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOC, &gpio_init_structure);
gpio_init_structure.Pin = GPIO_PIN_6;
gpio_init_structure.Mode = GPIO_MODE_INPUT;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &gpio_init_structure);
}
/* USER CODE BEGIN 4 */
/**
* @brief This function is executed in case of error occurrence.
* @param None
* @retval None
*/
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* Turn LED3 on */
BSP_LED_On(LED3);
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
W5500.C:
#include "W5500.h"
/***************----- ÍøÂç²ÎÊý±äÁ¿¶¨Òå -----***************/
unsigned char Gateway_IP[4];//Íø¹ØIPµØÖ·
unsigned char Sub_Mask[4]; //×ÓÍøÑÚÂë
unsigned char Phy_Addr[6]; //ÎïÀíµØÖ·(MAC)
unsigned char IP_Addr[4]; //±¾»úIPµØÖ·
unsigned char S0_Port[2]; //¶Ë¿Ú0µÄ¶Ë¿ÚºÅ
unsigned char S0_DIP[4]; //¶Ë¿Ú0Ä¿µÄIPµØÖ·
unsigned char S0_DPort[2]; //¶Ë¿Ú0Ä¿µÄ¶Ë¿ÚºÅ
unsigned char UDP_DIPR[4]; //UDP(¹ã²¥)ģʽ,Ä¿µÄÖ÷»úIPµØÖ·
unsigned char UDP_DPORT[2]; //UDP(¹ã²¥)ģʽ,Ä¿µÄÖ÷»ú¶Ë¿ÚºÅ
/***************----- ¶Ë¿ÚµÄÔËÐÐģʽ -----***************/
unsigned char S0_Mode =3; //¶Ë¿Ú0µÄÔËÐÐģʽ,0:TCP·þÎñÆ÷ģʽ,1:TCP¿Í»§¶Ëģʽ,2:UDP(¹ã²¥)ģʽ
#define TCP_SERVER 0x00 //TCP·þÎñÆ÷ģʽ
#define TCP_CLIENT 0x01 //TCP¿Í»§¶Ëģʽ
#define UDP_MODE 0x02 //UDP(¹ã²¥)ģʽ
/***************----- ¶Ë¿ÚµÄÔËÐÐ״̬ -----***************/
unsigned char S0_State =0; //¶Ë¿Ú0״̬¼Ç¼,1:¶Ë¿ÚÍê³É³õʼ»¯,2¶Ë¿ÚÍê³ÉÁ¬½Ó(¿ÉÒÔÕý³£´«ÊäÊý¾Ý)
#define S_INIT 0x01 //¶Ë¿ÚÍê³É³õʼ»¯
#define S_CONN 0x02 //¶Ë¿ÚÍê³ÉÁ¬½Ó,¿ÉÒÔÕý³£´«ÊäÊý¾Ý
/***************----- ¶Ë¿ÚÊÕ·¢Êý¾ÝµÄ״̬ -----***************/
unsigned char S0_Data; //¶Ë¿Ú0½ÓÊպͷ¢ËÍÊý¾ÝµÄ״̬,1:¶Ë¿Ú½ÓÊÕµ½Êý¾Ý,2:¶Ë¿Ú·¢ËÍÊý¾ÝÍê³É
#define S_RECEIVE 0x01 //¶Ë¿Ú½ÓÊÕµ½Ò»¸öÊý¾Ý°ü
#define S_TRANSMITOK 0x02 //¶Ë¿Ú·¢ËÍÒ»¸öÊý¾Ý°üÍê³É
/***************----- ¶Ë¿ÚÊý¾Ý»º³åÇø -----***************/
unsigned char Rx_Buffer[30]; //¶Ë¿Ú½ÓÊÕÊý¾Ý»º³åÇø
unsigned char Tx_Buffer[30]; //¶Ë¿Ú·¢ËÍÊý¾Ý»º³åÇø
//unsigned char Tx_Buffer[30]="How are you?"; //¶Ë¿Ú·¢ËÍÊý¾Ý»º³åÇø
unsigned char W5500_Interrupt; //W5500ÖжϱêÖ¾(0:ÎÞÖжÏ,1:ÓÐÖжÏ)
/*******************************************************************************
* ??? : Delay
* ?? : ????(ms)
* ?? : d:????,?????
* ?? : ?
* ?? : ?
* ?? : ?????Timer2??????1?????????
*******************************************************************************/
void Delay(unsigned int x)
{
unsigned int i,j;
for(j=0;j<5;j++)
for(i=0;i<x;i++);
}
/*******************************************************************************
* º¯ÊýÃû : SPI_ReadByte
* ÃèÊö : ¶ÁÈ¡Ò»¸ö×Ö½ÚSPI·µ»ØµÄÊý¾Ý
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ¶ÁÈ¡µ½µÄ¼Ä´æÆ÷Êý¾Ý
* ˵Ã÷ : ÎÞ
*******************************************************************************/
unsigned char SPI_Read_Byte(void)
{
unsigned char i,rByte=0;
W5500_SCLK_0;
for(i=0;i<8;i++)
{
W5500_SCLK_1;
rByte<<=1;
rByte|=W5500_MISO;
W5500_SCLK_0;
}
return rByte;
}
/*******************************************************************************
* º¯ÊýÃû : SPI_SendByte
* ÃèÊö : SPI·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : dt:´ý·¢Ë͵ÄÊý¾Ý
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void SPI_Send_Byte(unsigned char dt)
{
unsigned char i;
for(i=0;i<8;i++)
{
W5500_SCLK_0;
if((dt<<i)&0x80)
W5500_MOSI_1;
else
W5500_MOSI_0;
W5500_SCLK_1;
}
W5500_SCLK_0;
}
/*******************************************************************************
* º¯ÊýÃû : SPI_Send_Short
* ÃèÊö : SPI·¢ËÍ2¸ö×Ö½ÚÊý¾Ý(16λ)
* ÊäÈë : dat:´ý·¢Ë͵Ä16λÊý¾Ý
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void SPI_Send_Short(unsigned short dt)
{
SPI_Send_Byte((unsigned char)(dt/256)); //дÊý¾Ý¸ßλ
SPI_Send_Byte(dt); //дÊý¾ÝµÍλ
}
/*******************************************************************************
* º¯ÊýÃû : Write_W5500_1Byte
* ÃèÊö : ͨ¹ýSPIÏòÖ¸¶¨µØÖ·¼Ä´æÆ÷д1¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : reg:16λ¼Ä´æÆ÷µØÖ·,dat:´ýдÈëµÄÊý¾Ý
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_W5500_1Byte(unsigned short reg, unsigned char dat)
{
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM1|RWB_WRITE|COMMON_R);//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,1¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡ÔñͨÓüĴæÆ÷
SPI_Send_Byte(dat);//д1¸ö×Ö½ÚÊý¾Ý
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
}
/*******************************************************************************
* º¯ÊýÃû : Write_W5500_2Byte
* ÃèÊö : ͨ¹ýSPIÏòÖ¸¶¨µØÖ·¼Ä´æÆ÷д2¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : reg:16λ¼Ä´æÆ÷µØÖ·,dat:16λ´ýдÈëµÄÊý¾Ý(2¸ö×Ö½Ú)
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_W5500_2Byte(unsigned short reg, unsigned short dat)
{
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM2|RWB_WRITE|COMMON_R);//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,2¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡ÔñͨÓüĴæÆ÷
SPI_Send_Short(dat);//д16λÊý¾Ý
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
}
/*******************************************************************************
* º¯ÊýÃû : Write_W5500_nByte
* ÃèÊö : ͨ¹ýSPIÏòÖ¸¶¨µØÖ·¼Ä´æÆ÷дn¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : reg:16λ¼Ä´æÆ÷µØÖ·,*dat_ptr:´ýдÈëÊý¾Ý»º³åÇøÖ¸Õë,size:´ýдÈëµÄÊý¾Ý³¤¶È
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_W5500_nByte(unsigned short reg, unsigned char *dat_ptr, unsigned short size)
{
unsigned short i;
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(VDM|RWB_WRITE|COMMON_R);//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,N¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡ÔñͨÓüĴæÆ÷
for(i=0;i<size;i++)//Ñ»·½«»º³åÇøµÄsize¸ö×Ö½ÚÊý¾ÝдÈëW5500
{
SPI_Send_Byte(*dat_ptr++);//дһ¸ö×Ö½ÚÊý¾Ý
}
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
}
/*******************************************************************************
* º¯ÊýÃû : Write_W5500_SOCK_1Byte
* ÃèÊö : ͨ¹ýSPIÏòÖ¸¶¨¶Ë¿Ú¼Ä´æÆ÷д1¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ,reg:16λ¼Ä´æÆ÷µØÖ·,dat:´ýдÈëµÄÊý¾Ý
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_W5500_SOCK_1Byte(SOCKET s, unsigned short reg, unsigned char dat)
{
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM1|RWB_WRITE|(s*0x20+0x08));//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,1¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
SPI_Send_Byte(dat);//д1¸ö×Ö½ÚÊý¾Ý
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
}
/*******************************************************************************
* º¯ÊýÃû : Write_W5500_SOCK_2Byte
* ÃèÊö : ͨ¹ýSPIÏòÖ¸¶¨¶Ë¿Ú¼Ä´æÆ÷д2¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ,reg:16λ¼Ä´æÆ÷µØÖ·,dat:16λ´ýдÈëµÄÊý¾Ý(2¸ö×Ö½Ú)
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_W5500_SOCK_2Byte(SOCKET s, unsigned short reg, unsigned short dat)
{
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM2|RWB_WRITE|(s*0x20+0x08));//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,2¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
SPI_Send_Short(dat);//д16λÊý¾Ý
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
}
/*******************************************************************************
* º¯ÊýÃû : Write_W5500_SOCK_4Byte
* ÃèÊö : ͨ¹ýSPIÏòÖ¸¶¨¶Ë¿Ú¼Ä´æÆ÷д4¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ,reg:16λ¼Ä´æÆ÷µØÖ·,*dat_ptr:´ýдÈëµÄ4¸ö×Ö½Ú»º³åÇøÖ¸Õë
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_W5500_SOCK_4Byte(SOCKET s, unsigned short reg, unsigned char *dat_ptr)
{
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM4|RWB_WRITE|(s*0x20+0x08));//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,4¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
SPI_Send_Byte(*dat_ptr++);//дµÚ1¸ö×Ö½ÚÊý¾Ý
SPI_Send_Byte(*dat_ptr++);//дµÚ2¸ö×Ö½ÚÊý¾Ý
SPI_Send_Byte(*dat_ptr++);//дµÚ3¸ö×Ö½ÚÊý¾Ý
SPI_Send_Byte(*dat_ptr++);//дµÚ4¸ö×Ö½ÚÊý¾Ý
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
}
/*******************************************************************************
* º¯ÊýÃû : Read_W5500_1Byte
* ÃèÊö : ¶ÁW5500Ö¸¶¨µØÖ·¼Ä´æÆ÷µÄ1¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : reg:16λ¼Ä´æÆ÷µØÖ·
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ¶ÁÈ¡µ½¼Ä´æÆ÷µÄ1¸ö×Ö½ÚÊý¾Ý
* ˵Ã÷ : ÎÞ
*******************************************************************************/
unsigned char Read_W5500_1Byte(unsigned short reg)
{
unsigned char i;
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM1|RWB_READ|COMMON_R);//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,1¸ö×Ö½ÚÊý¾Ý³¤¶È,¶ÁÊý¾Ý,Ñ¡ÔñͨÓüĴæÆ÷
i=SPI_Read_Byte();
W5500_SCS_1;//ÖÃW5500µÄSCSΪ¸ßµçƽ
return i;//·µ»Ø¶ÁÈ¡µ½µÄ¼Ä´æÆ÷Êý¾Ý
}
/*******************************************************************************
* º¯ÊýÃû : Read_W5500_SOCK_1Byte
* ÃèÊö : ¶ÁW5500Ö¸¶¨¶Ë¿Ú¼Ä´æÆ÷µÄ1¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ,reg:16λ¼Ä´æÆ÷µØÖ·
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ¶ÁÈ¡µ½¼Ä´æÆ÷µÄ1¸ö×Ö½ÚÊý¾Ý
* ˵Ã÷ : ÎÞ
*******************************************************************************/
unsigned char Read_W5500_SOCK_1Byte(SOCKET s, unsigned short reg)
{
unsigned char i;
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM1|RWB_READ|(s*0x20+0x08));//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,1¸ö×Ö½ÚÊý¾Ý³¤¶È,¶ÁÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
i=SPI_Read_Byte();
W5500_SCS_1;//ÖÃW5500µÄSCSΪ¸ßµçƽ
return i;//·µ»Ø¶ÁÈ¡µ½µÄ¼Ä´æÆ÷Êý¾Ý
}
/*******************************************************************************
* º¯ÊýÃû : Read_W5500_SOCK_2Byte
* ÃèÊö : ¶ÁW5500Ö¸¶¨¶Ë¿Ú¼Ä´æÆ÷µÄ2¸ö×Ö½ÚÊý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ,reg:16λ¼Ä´æÆ÷µØÖ·
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ¶ÁÈ¡µ½¼Ä´æÆ÷µÄ2¸ö×Ö½ÚÊý¾Ý(16λ)
* ˵Ã÷ : ÎÞ
*******************************************************************************/
unsigned short Read_W5500_SOCK_2Byte(SOCKET s, unsigned short reg)
{
unsigned short i;
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(reg);//ͨ¹ýSPIд16λ¼Ä´æÆ÷µØÖ·
SPI_Send_Byte(FDM2|RWB_READ|(s*0x20+0x08));//ͨ¹ýSPIд¿ØÖÆ×Ö½Ú,2¸ö×Ö½ÚÊý¾Ý³¤¶È,¶ÁÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
i=SPI_Read_Byte();
i*=256;
i+=SPI_Read_Byte();//¶ÁÈ¡µÍλÊý¾Ý
W5500_SCS_1;//ÖÃW5500µÄSCSΪ¸ßµçƽ
return i;//·µ»Ø¶ÁÈ¡µ½µÄ¼Ä´æÆ÷Êý¾Ý
}
/*******************************************************************************
* º¯ÊýÃû : Read_SOCK_Data_Buffer
* ÃèÊö : ´ÓW5500½ÓÊÕÊý¾Ý»º³åÇøÖжÁÈ¡Êý¾Ý
* ÊäÈë : s:¶Ë¿ÚºÅ,*dat_ptr:Êý¾Ý±£´æ»º³åÇøÖ¸Õë
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ¶ÁÈ¡µ½µÄÊý¾Ý³¤¶È,rx_size¸ö×Ö½Ú
* ˵Ã÷ : ÎÞ
*******************************************************************************/
unsigned short Read_SOCK_Data_Buffer(SOCKET s, unsigned char *dat_ptr)
{
unsigned short rx_size;
unsigned short offset, offset1;
unsigned short i;
unsigned char j;
rx_size=Read_W5500_SOCK_2Byte(s,Sn_RX_RSR);
if(rx_size==0) return 0;//û½ÓÊÕµ½Êý¾ÝÔò·µ»Ø
if(rx_size>1460) rx_size=1460;
offset=Read_W5500_SOCK_2Byte(s,Sn_RX_RD);
offset1=offset;
offset&=(S_RX_SIZE-1);//¼ÆËãʵ¼ÊµÄÎïÀíµØÖ·
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(offset);//д16λµØÖ·
SPI_Send_Byte(VDM|RWB_READ|(s*0x20+0x18));//д¿ØÖÆ×Ö½Ú,N¸ö×Ö½ÚÊý¾Ý³¤¶È,¶ÁÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
if((offset+rx_size)<S_RX_SIZE)//Èç¹û×î´óµØַ䳬¹ýW5500½ÓÊÕ»º³åÇø¼Ä´æÆ÷µÄ×î´óµØÖ·
{
for(i=0;i<rx_size;i++)//Ñ»·¶ÁÈ¡rx_size¸ö×Ö½ÚÊý¾Ý
{
j=SPI_Read_Byte();//¶ÁÈ¡1¸ö×Ö½ÚÊý¾Ý
*dat_ptr=j;//½«¶ÁÈ¡µ½µÄÊý¾Ý±£´æµ½Êý¾Ý±£´æ»º³åÇø
dat_ptr++;//Êý¾Ý±£´æ»º³åÇøÖ¸ÕëµØÖ·×ÔÔö1
}
}
else//Èç¹û×î´óµØÖ·³¬¹ýW5500½ÓÊÕ»º³åÇø¼Ä´æÆ÷µÄ×î´óµØÖ·
{
offset=S_RX_SIZE-offset;
for(i=0;i<offset;i++)//Ñ»·¶ÁÈ¡³öÇ°offset¸ö×Ö½ÚÊý¾Ý
{
j=SPI_Read_Byte();//¶ÁÈ¡1¸ö×Ö½ÚÊý¾Ý
*dat_ptr=j;//½«¶ÁÈ¡µ½µÄÊý¾Ý±£´æµ½Êý¾Ý±£´æ»º³åÇø
dat_ptr++;//Êý¾Ý±£´æ»º³åÇøÖ¸ÕëµØÖ·×ÔÔö1
}
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(0x00);//д16λµØÖ·
SPI_Send_Byte(VDM|RWB_READ|(s*0x20+0x18));//д¿ØÖÆ×Ö½Ú,N¸ö×Ö½ÚÊý¾Ý³¤¶È,¶ÁÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
for(;i<rx_size;i++)//Ñ»·¶ÁÈ¡ºórx_size-offset¸ö×Ö½ÚÊý¾Ý
{
j=SPI_Read_Byte();//¶ÁÈ¡1¸ö×Ö½ÚÊý¾Ý
*dat_ptr=j;//½«¶ÁÈ¡µ½µÄÊý¾Ý±£´æµ½Êý¾Ý±£´æ»º³åÇø
dat_ptr++;//Êý¾Ý±£´æ»º³åÇøÖ¸ÕëµØÖ·×ÔÔö1
}
}
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
offset1+=rx_size;//¸üÐÂʵ¼ÊÎïÀíµØÖ·,¼´Ï´ζÁÈ¡½ÓÊÕµ½µÄÊý¾ÝµÄÆðʼµØÖ·
Write_W5500_SOCK_2Byte(s, Sn_RX_RD, offset1);
Write_W5500_SOCK_1Byte(s, Sn_CR, RECV);//·¢ËÍÆô¶¯½ÓÊÕÃüÁî
return rx_size;//·µ»Ø½ÓÊÕµ½Êý¾ÝµÄ³¤¶È
}
/*******************************************************************************
* º¯ÊýÃû : Write_SOCK_Data_Buffer
* ÃèÊö : ½«Êý¾ÝдÈëW5500µÄÊý¾Ý·¢ËÍ»º³åÇø
* ÊäÈë : s:¶Ë¿ÚºÅ,*dat_ptr:Êý¾Ý±£´æ»º³åÇøÖ¸Õë,size:´ýдÈëÊý¾ÝµÄ³¤¶È
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Write_SOCK_Data_Buffer(SOCKET s, unsigned char *dat_ptr, unsigned short size)
{
unsigned short offset,offset1;
unsigned short i;
//Èç¹ûÊÇUDPģʽ,¿ÉÒÔÔÚ´ËÉèÖÃÄ¿µÄÖ÷»úµÄIPºÍ¶Ë¿ÚºÅ
if((Read_W5500_SOCK_1Byte(s,Sn_MR)&0x0f) != SOCK_UDP)//Èç¹ûSocket´ò¿ªÊ§°Ü
{
Write_W5500_SOCK_4Byte(s, Sn_DIPR, UDP_DIPR);//ÉèÖÃÄ¿µÄÖ÷»úIP
Write_W5500_SOCK_2Byte(s, Sn_DPORTR, UDP_DPORT[0]*256+UDP_DPORT[1]);//ÉèÖÃÄ¿µÄÖ÷»ú¶Ë¿ÚºÅ
}
offset=Read_W5500_SOCK_2Byte(s,Sn_TX_WR);
offset1=offset;
offset&=(S_TX_SIZE-1);//¼ÆËãʵ¼ÊµÄÎïÀíµØÖ·
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(offset);//д16λµØÖ·
SPI_Send_Byte(VDM|RWB_WRITE|(s*0x20+0x10));//д¿ØÖÆ×Ö½Ú,N¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
if((offset+size)<S_TX_SIZE)//Èç¹û×î´óµØַ䳬¹ýW5500·¢ËÍ»º³åÇø¼Ä´æÆ÷µÄ×î´óµØÖ·
{
for(i=0;i<size;i++)//Ñ»·Ð´Èësize¸ö×Ö½ÚÊý¾Ý
{
SPI_Send_Byte(*dat_ptr++);//дÈëÒ»¸ö×Ö½ÚµÄÊý¾Ý
}
}
else//Èç¹û×î´óµØÖ·³¬¹ýW5500·¢ËÍ»º³åÇø¼Ä´æÆ÷µÄ×î´óµØÖ·
{
offset=S_TX_SIZE-offset;
for(i=0;i<offset;i++)//Ñ»·Ð´ÈëÇ°offset¸ö×Ö½ÚÊý¾Ý
{
SPI_Send_Byte(*dat_ptr++);//дÈëÒ»¸ö×Ö½ÚµÄÊý¾Ý
}
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
W5500_SCS_0;//ÖÃW5500µÄSCSΪµÍµçƽ
SPI_Send_Short(0x00);//д16λµØÖ·
SPI_Send_Byte(VDM|RWB_WRITE|(s*0x20+0x10));//д¿ØÖÆ×Ö½Ú,N¸ö×Ö½ÚÊý¾Ý³¤¶È,дÊý¾Ý,Ñ¡Ôñ¶Ë¿ÚsµÄ¼Ä´æÆ÷
for(;i<size;i++)//Ñ»·Ð´Èësize-offset¸ö×Ö½ÚÊý¾Ý
{
SPI_Send_Byte(*dat_ptr++);//дÈëÒ»¸ö×Ö½ÚµÄÊý¾Ý
}
}
W5500_SCS_1; //ÖÃW5500µÄSCSΪ¸ßµçƽ
offset1+=size;//¸üÐÂʵ¼ÊÎïÀíµØÖ·,¼´Ï´Îд´ý·¢ËÍÊý¾Ýµ½·¢ËÍÊý¾Ý»º³åÇøµÄÆðʼµØÖ·
Write_W5500_SOCK_2Byte(s, Sn_TX_WR, offset1);
Write_W5500_SOCK_1Byte(s, Sn_CR, SEND);//·¢ËÍÆô¶¯·¢ËÍÃüÁî
}
/*******************************************************************************
* º¯ÊýÃû : W5500_Hardware_Reset
* ÃèÊö : Ó²¼þ¸´Î»W5500
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : W5500µÄ¸´Î»Òý½Å±£³ÖµÍµçƽÖÁÉÙ500usÒÔÉÏ,²ÅÄÜÖØΧW5500
*******************************************************************************/
void W5500_Hardware_Reset(void)
{
W5500_RST_0;//¸´Î»Òý½ÅÀµÍ
HAL_Delay(200);
W5500_RST_1;//¸´Î»Òý½ÅÀ¸ß
HAL_Delay(200);
while((Read_W5500_1Byte(PHYCFGR)&LINK)==0);//µÈ´ýÒÔÌ«ÍøÁ¬½ÓÍê³É
}
/*******************************************************************************
* º¯ÊýÃû : W5500_Init
* ÃèÊö : ³õʼ»¯W5500¼Ä´æÆ÷º¯Êý
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÔÚʹÓÃW5500֮ǰ£¬ÏȶÔW5500³õʼ»¯
*******************************************************************************/
void W5500_Init(void)
{
unsigned char i=0;
Write_W5500_1Byte(MR, RST);//Èí¼þ¸´Î»W5500,ÖÃ1ÓÐЧ,¸´Î»ºó×Ô¶¯Çå0
HAL_Delay(10);//ÑÓʱ10ms,×Ô¼º¶¨Òå¸Ãº¯Êý
//ÉèÖÃÍø¹Ø(Gateway)µÄIPµØÖ·,Gateway_IPΪ4×Ö½Úunsigned charÊý×é,×Ô¼º¶¨Òå
//ʹÓÃÍø¹Ø¿ÉÒÔʹͨÐÅÍ»ÆÆ×ÓÍøµÄ¾ÖÏÞ£¬Í¨¹ýÍø¹Ø¿ÉÒÔ·ÃÎʵ½ÆäËü×ÓÍø»ò½øÈëInternet
Write_W5500_nByte(GAR, Gateway_IP, 4);
//ÉèÖÃ×ÓÍøÑÚÂë(MASK)Öµ,SUB_MASKΪ4×Ö½Úunsigned charÊý×é,×Ô¼º¶¨Òå
//×ÓÍøÑÚÂëÓÃÓÚ×ÓÍøÔËËã
Write_W5500_nByte(SUBR,Sub_Mask,4);
//ÉèÖÃÎïÀíµØÖ·,PHY_ADDRΪ6×Ö½Úunsigned charÊý×é,×Ô¼º¶¨Òå,ÓÃÓÚΨһ±êʶÍøÂçÉ豸µÄÎïÀíµØÖ·Öµ
//¸ÃµØÖ·ÖµÐèÒªµ½IEEEÉêÇ룬°´ÕÕOUIµÄ¹æ¶¨£¬Ç°3¸ö×Ö½ÚΪ³§ÉÌ´úÂ룬ºóÈý¸ö×Ö½ÚΪ²úÆ·ÐòºÅ
//Èç¹û×Ô¼º¶¨ÒåÎïÀíµØÖ·£¬×¢ÒâµÚÒ»¸ö×Ö½Ú±ØÐëΪżÊý
Write_W5500_nByte(SHAR,Phy_Addr,6);
//ÉèÖñ¾»úµÄIPµØÖ·,IP_ADDRΪ4×Ö½Úunsigned charÊý×é,×Ô¼º¶¨Òå
//×¢Ò⣬Íø¹ØIP±ØÐëÓë±¾»úIPÊôÓÚͬһ¸ö×ÓÍø£¬·ñÔò±¾»ú½«ÎÞ·¨ÕÒµ½Íø¹Ø
Write_W5500_nByte(SIPR,IP_Addr,4);
//ÉèÖ÷¢ËÍ»º³åÇøºÍ½ÓÊÕ»º³åÇøµÄ´óС£¬²Î¿¼W5500Êý¾ÝÊÖ²á
for(i=0;i<8;i++)
{
Write_W5500_SOCK_1Byte(i,Sn_RXBUF_SIZE, 0x02);//Socket Rx memory size=2k
Write_W5500_SOCK_1Byte(i,Sn_TXBUF_SIZE, 0x02);//Socket Tx mempry size=2k
}
//ÉèÖÃÖØÊÔʱ¼ä£¬Ä¬ÈÏΪ2000(200ms)
//ÿһµ¥Î»ÊýֵΪ100΢Ãë,³õʼ»¯Ê±ÖµÉèΪ2000(0x07D0),µÈÓÚ200ºÁÃë
Write_W5500_2Byte(RTR, 0x07d0);
//ÉèÖÃÖØÊÔ´ÎÊý£¬Ä¬ÈÏΪ8´Î
//Èç¹ûÖØ·¢µÄ´ÎÊý³¬¹ýÉ趨ֵ,Ôò²úÉú³¬Ê±ÖжÏ(Ïà¹ØµÄ¶Ë¿ÚÖжϼĴæÆ÷ÖеÄSn_IR ³¬Ê±Î»(TIMEOUT)Öá°1¡±)
Write_W5500_1Byte(RCR,8);
}
/*******************************************************************************
* º¯ÊýÃû : Detect_Gateway
* ÃèÊö : ¼ì²éÍø¹Ø·þÎñÆ÷
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ³É¹¦·µ»ØTRUE(0xFF),ʧ°Ü·µ»ØFALSE(0x00)
* ˵Ã÷ : ÎÞ
*******************************************************************************/
unsigned char Detect_Gateway(void)
{
unsigned char ip_adde[4];
ip_adde[0]=IP_Addr[0]+1;
ip_adde[1]=IP_Addr[1]+1;
ip_adde[2]=IP_Addr[2]+1;
ip_adde[3]=IP_Addr[3]+1;
//¼ì²éÍø¹Ø¼°»ñÈ¡Íø¹ØµÄÎïÀíµØÖ·
Write_W5500_SOCK_4Byte(0,Sn_DIPR,ip_adde);//ÏòÄ¿µÄµØÖ·¼Ä´æÆ÷дÈëÓë±¾»úIP²»Í¬µÄIPÖµ
Write_W5500_SOCK_1Byte(0,Sn_MR,MR_TCP);//ÉèÖÃsocketΪTCPģʽ
Write_W5500_SOCK_1Byte(0,Sn_CR,OPEN);//´ò¿ªSocket
HAL_Delay(5);//ÑÓʱ5ms
if(Read_W5500_SOCK_1Byte(0,Sn_SR) != SOCK_INIT)//Èç¹ûsocket´ò¿ªÊ§°Ü
{
Write_W5500_SOCK_1Byte(0,Sn_CR,CLOSE);//´ò¿ª²»³É¹¦,¹Ø±ÕSocket
return FALSE;//·µ»ØFALSE(0x00)
}
Write_W5500_SOCK_1Byte(0,Sn_CR,CONNECT);//ÉèÖÃSocketΪConnectģʽ
do
{
unsigned char j=0;
j=Read_W5500_SOCK_1Byte(0,Sn_IR);//¶ÁÈ¡Socket0ÖжϱêÖ¾¼Ä´æÆ÷
if(j!=0)
Write_W5500_SOCK_1Byte(0,Sn_IR,j);
HAL_Delay(5);//ÑÓʱ5ms
if((j&IR_TIMEOUT) == IR_TIMEOUT)
{
return FALSE;
}
else if(Read_W5500_SOCK_1Byte(0,Sn_DHAR) != 0xff)
{
Write_W5500_SOCK_1Byte(0,Sn_CR,CLOSE);//¹Ø±ÕSocket
return TRUE;
}
}while(1);
}
/*******************************************************************************
* º¯ÊýÃû : Socket_Init
* ÃèÊö : Ö¸¶¨Socket(0~7)³õʼ»¯
* ÊäÈë : s:´ý³õʼ»¯µÄ¶Ë¿Ú
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void Socket_Init(SOCKET s)
{
//ÉèÖ÷ÖƬ³¤¶È£¬²Î¿¼W5500Êý¾ÝÊֲᣬ¸ÃÖµ¿ÉÒÔ²»ÐÞ¸Ä
Write_W5500_SOCK_2Byte(0, Sn_MSSR, 30);//×î´ó·ÖƬ×Ö½ÚÊý=30(0x001e)
//ÉèÖÃÖ¸¶¨¶Ë¿Ú
switch(s)
{
case 0:
//ÉèÖö˿Ú0µÄ¶Ë¿ÚºÅ
Write_W5500_SOCK_2Byte(0, Sn_PORT, S0_Port[0]*256+S0_Port[1]);
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
break;
}
}
/*******************************************************************************
* º¯ÊýÃû : Socket_Connect
* ÃèÊö : ÉèÖÃÖ¸¶¨Socket(0~7)Ϊ¿Í»§¶ËÓëÔ¶³Ì·þÎñÆ÷Á¬½Ó
* ÊäÈë : s:´ýÉ趨µÄ¶Ë¿Ú
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ³É¹¦·µ»ØTRUE(0xFF),ʧ°Ü·µ»ØFALSE(0x00)
* ˵Ã÷ : µ±±¾»úSocket¹¤×÷ÔÚ¿Í»§¶Ëģʽʱ,ÒýÓøóÌÐò,ÓëÔ¶³Ì·þÎñÆ÷½¨Á¢Á¬½Ó
* Èç¹ûÆô¶¯Á¬½Óºó³öÏÖ³¬Ê±Öжϣ¬ÔòÓë·þÎñÆ÷Á¬½Óʧ°Ü,ÐèÒªÖØе÷ÓøóÌÐòÁ¬½Ó
* ¸Ã³ÌÐòÿµ÷ÓÃÒ»´Î,¾ÍÓë·þÎñÆ÷²úÉúÒ»´ÎÁ¬½Ó
*******************************************************************************/
unsigned char Socket_Connect(SOCKET s)
{
Write_W5500_SOCK_1Byte(s,Sn_MR,MR_TCP);//ÉèÖÃsocketΪTCPģʽ
Write_W5500_SOCK_1Byte(s,Sn_CR,OPEN);//´ò¿ªSocket
HAL_Delay(5);//ÑÓʱ5ms
if(Read_W5500_SOCK_1Byte(s,Sn_SR)!=SOCK_INIT)//Èç¹ûsocket´ò¿ªÊ§°Ü
{
Write_W5500_SOCK_1Byte(s,Sn_CR,CLOSE);//´ò¿ª²»³É¹¦,¹Ø±ÕSocket
return FALSE;//·µ»ØFALSE(0x00)
}
Write_W5500_SOCK_1Byte(s,Sn_CR,CONNECT);//ÉèÖÃSocketΪConnectģʽ
return TRUE;//·µ»ØTRUE,ÉèÖóɹ¦
}
/*******************************************************************************
* º¯ÊýÃû : Socket_Listen
* ÃèÊö : ÉèÖÃÖ¸¶¨Socket(0~7)×÷Ϊ·þÎñÆ÷µÈ´ýÔ¶³ÌÖ÷»úµÄÁ¬½Ó
* ÊäÈë : s:´ýÉ趨µÄ¶Ë¿Ú
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ³É¹¦·µ»ØTRUE(0xFF),ʧ°Ü·µ»ØFALSE(0x00)
* ˵Ã÷ : µ±±¾»úSocket¹¤×÷ÔÚ·þÎñÆ÷ģʽʱ,ÒýÓøóÌÐò,µÈµÈÔ¶³ÌÖ÷»úµÄÁ¬½Ó
* ¸Ã³ÌÐòÖ»µ÷ÓÃÒ»´Î,¾ÍʹW5500ÉèÖÃΪ·þÎñÆ÷ģʽ
*******************************************************************************/
unsigned char Socket_Listen(SOCKET s)
{
Write_W5500_SOCK_1Byte(s,Sn_MR,MR_TCP);//ÉèÖÃsocketΪTCPģʽ
Write_W5500_SOCK_1Byte(s,Sn_CR,OPEN);//´ò¿ªSocket
HAL_Delay(5);//ÑÓʱ5ms
if(Read_W5500_SOCK_1Byte(s,Sn_SR)!=SOCK_INIT)//Èç¹ûsocket´ò¿ªÊ§°Ü
{
Write_W5500_SOCK_1Byte(s,Sn_CR,CLOSE);//´ò¿ª²»³É¹¦,¹Ø±ÕSocket
return FALSE;//·µ»ØFALSE(0x00)
}
Write_W5500_SOCK_1Byte(s,Sn_CR,LISTEN);//ÉèÖÃSocketΪÕìÌýģʽ
HAL_Delay(5);//ÑÓʱ5ms
if(Read_W5500_SOCK_1Byte(s,Sn_SR)!=SOCK_LISTEN)//Èç¹ûsocketÉèÖÃʧ°Ü
{
Write_W5500_SOCK_1Byte(s,Sn_CR,CLOSE);//ÉèÖò»³É¹¦,¹Ø±ÕSocket
return FALSE;//·µ»ØFALSE(0x00)
}
return TRUE;
//ÖÁ´ËÍê³ÉÁËSocketµÄ´ò¿ªºÍÉèÖÃÕìÌý¹¤×÷,ÖÁÓÚÔ¶³Ì¿Í»§¶ËÊÇ·ñÓëËü½¨Á¢Á¬½Ó,ÔòÐèÒªµÈ´ýSocketÖжϣ¬
//ÒÔÅжÏSocketµÄÁ¬½ÓÊÇ·ñ³É¹¦¡£²Î¿¼W5500Êý¾ÝÊÖ²áµÄSocketÖжÏ״̬
//ÔÚ·þÎñÆ÷ÕìÌýģʽ²»ÐèÒªÉèÖÃÄ¿µÄIPºÍÄ¿µÄ¶Ë¿ÚºÅ
}
/*******************************************************************************
* º¯ÊýÃû : Socket_UDP
* ÃèÊö : ÉèÖÃÖ¸¶¨Socket(0~7)ΪUDPģʽ
* ÊäÈë : s:´ýÉ趨µÄ¶Ë¿Ú
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ³É¹¦·µ»ØTRUE(0xFF),ʧ°Ü·µ»ØFALSE(0x00)
* ˵Ã÷ : Èç¹ûSocket¹¤×÷ÔÚUDPģʽ,ÒýÓøóÌÐò,ÔÚUDPģʽÏÂ,SocketͨÐŲ»ÐèÒª½¨Á¢Á¬½Ó
* ¸Ã³ÌÐòÖ»µ÷ÓÃÒ»´Î£¬¾ÍʹW5500ÉèÖÃΪUDPģʽ
*******************************************************************************/
unsigned char Socket_UDP(SOCKET s)
{
Write_W5500_SOCK_1Byte(s,Sn_MR,MR_UDP);//ÉèÖÃSocketΪUDPģʽ*/
Write_W5500_SOCK_1Byte(s,Sn_CR,OPEN);//´ò¿ªSocket*/
Delay(5);//ÑÓʱ5ms
if(Read_W5500_SOCK_1Byte(s,Sn_SR)!=SOCK_UDP)//Èç¹ûSocket´ò¿ªÊ§°Ü
{
Write_W5500_SOCK_1Byte(s,Sn_CR,CLOSE);//´ò¿ª²»³É¹¦,¹Ø±ÕSocket
return FALSE;//·µ»ØFALSE(0x00)
}
else
return TRUE;
//ÖÁ´ËÍê³ÉÁËSocketµÄ´ò¿ªºÍUDPģʽÉèÖÃ,ÔÚÕâÖÖģʽÏÂËü²»ÐèÒªÓëÔ¶³ÌÖ÷»ú½¨Á¢Á¬½Ó
//ÒòΪSocket²»ÐèÒª½¨Á¢Á¬½Ó,ËùÒÔÔÚ·¢ËÍÊý¾ÝÇ°¶¼¿ÉÒÔÉèÖÃÄ¿µÄÖ÷»úIPºÍÄ¿µÄSocketµÄ¶Ë¿ÚºÅ
//Èç¹ûÄ¿µÄÖ÷»úIPºÍÄ¿µÄSocketµÄ¶Ë¿ÚºÅÊǹ̶¨µÄ,ÔÚÔËÐйý³ÌÖÐûÓиıä,ÄÇôҲ¿ÉÒÔÔÚÕâÀïÉèÖÃ
}
/*******************************************************************************
* º¯ÊýÃû : W5500_Interrupt_Process
* ÃèÊö : W5500Öжϴ¦Àí³ÌÐò¿ò¼Ü
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
* ·µ»ØÖµ : ÎÞ
* ˵Ã÷ : ÎÞ
*******************************************************************************/
void W5500_Interrupt_Process(void)
{
unsigned char i,j;
IntDispose:
i=Read_W5500_1Byte(SIR);//¶ÁÈ¡¶Ë¿ÚÖжϱêÖ¾¼Ä´æÆ÷
if((i & S0_INT) == S0_INT)//Socket0ʼþ´¦Àí
{
j=Read_W5500_SOCK_1Byte(0,Sn_IR);//¶ÁÈ¡Socket0ÖжϱêÖ¾¼Ä´æÆ÷
Write_W5500_SOCK_1Byte(0,Sn_IR,j);
if(j&IR_CON)//ÔÚTCPģʽÏÂ,Socket0³É¹¦Á¬½Ó
{
S0_State|=S_CONN;//ÍøÂçÁ¬½Ó״̬0x02,¶Ë¿ÚÍê³ÉÁ¬½Ó£¬¿ÉÒÔÕý³£´«ÊäÊý¾Ý
}
if(j&IR_DISCON)//ÔÚTCPģʽÏÂSocket¶Ï¿ªÁ¬½Ó´¦Àí
{
Write_W5500_SOCK_1Byte(0,Sn_CR,CLOSE);//¹Ø±Õ¶Ë¿Ú,µÈ´ýÖØдò¿ªÁ¬½Ó
Socket_Init(0); //Ö¸¶¨Socket(0~7)³õʼ»¯,³õʼ»¯¶Ë¿Ú0
S0_State=0;//ÍøÂçÁ¬½Ó״̬0x00,¶Ë¿ÚÁ¬½Óʧ°Ü
}
if(j&IR_SEND_OK)//Socket0Êý¾Ý·¢ËÍÍê³É,¿ÉÒÔÔÙ´ÎÆô¶¯S_tx_process()º¯Êý·¢ËÍÊý¾Ý
{
S0_Data|=S_TRANSMITOK;//¶Ë¿Ú·¢ËÍÒ»¸öÊý¾Ý°üÍê³É
}
if(j&IR_RECV)//Socket½ÓÊÕµ½Êý¾Ý,¿ÉÒÔÆô¶¯S_rx_process()º¯Êý
{
S0_Data|=S_RECEIVE;//¶Ë¿Ú½ÓÊÕµ½Ò»¸öÊý¾Ý°ü
}
if(j&IR_TIMEOUT)//SocketÁ¬½Ó»òÊý¾Ý´«Ê䳬ʱ´¦Àí
{
Write_W5500_SOCK_1Byte(0,Sn_CR,CLOSE);// ¹Ø±Õ¶Ë¿Ú,µÈ´ýÖØдò¿ªÁ¬½Ó
S0_State=0;//ÍøÂçÁ¬½Ó״̬0x00,¶Ë¿ÚÁ¬½Óʧ°Ü
}
}
if(Read_W5500_1Byte(SIR) != 0)
goto IntDispose;
}
效果图:
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
共1人点赞
|
@forgot :没有办法,在keil里显示正常,贴上就变了
你这代码注释变成天书了