void SPI1_Init()
{
GPIO_InitType GPIO_InitStructure = {0};
SPI_InitType SPI_InitStructure = {0};
RCC_ConfigPclk2(RCC_HCLK_DIV2);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_SPI1 , ENABLE);
//GPIO_ConfigPinRemap
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE );
GPIO_InitStructure.Pin = GPIO_PIN_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOAtiNG;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.DataDirection = SPI_DIR_DOUBLELINE_FULLDUPLEX;
SPI_InitStructure.SpiMode = SPI_MODE_MASTER;
SPI_InitStructure.DataLen = SPI_DATA_SIZE_8BITS;
SPI_InitStructure.CLKPOL = SPI_CLKPOL_LOW;
SPI_InitStructure.CLKPHA = SPI_CLKPHA_FIRST_EDGE;
SPI_InitStructure.NSS = SPI_NSS_SOFT;
SPI_InitStructure.BaudRatePres = SPI_BR_PRESCALER_64;
SPI_InitStructure.FirstBit = SPI_FB_MSB;
SPI_InitStructure.CRCPoly = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Enable(SPI1, ENABLE);
}
uint8_t SPI1_ReadWriteByte(uint8_t data)
{
uint8_t retry = 0;
while ((SPI1->STS & SPI_I2S_TE_FLAG) == RESET)
{
retry ++;
if(retry > 200)return 0;
}
SPI1->DAT = data;
retry=0;
while ((SPI1->STS & SPI_I2S_RNE_FLAG) == RESET)
{
retry ++;
if(retry > 200)return 0;
}
return SPI1->DAT;
}
void W5500_SetEthernet(void)
{
char tdata[20];
uint8_t i;
uint8_t w5500_get_ip[4] = {0};
uint8_t w5500_get_mask[4] = {0};
uint8_t w5500_get_gateway[4] = {0};
memcpy(tdata,(char*)(0X1FFFF7E8),12);
memcpy(MAC, tdata, 6);
for(i = 0; i < 6; i++)
{
if(MAC[i] == 0XFF) MAC[i] = 0X90 + i;
}
MAC[0] = 40;
MAC[1] = 150;
printf("MAC = %xh %xh %xh %xh %xh %xhn" , MAC[0], MAC[1], MAC[2], MAC[3], MAC[4], MAC[5]);
W5500_Set_Mac(MAC);
W5500_Set_IP(Local_IP);
W5500_Get_IP(&w5500_get_ip);
printf("W5500 w5500_get_ip = %d %d %d %dn" , w5500_get_ip[0] , w5500_get_ip[1] , w5500_get_ip[2] , w5500_get_ip[3]);
W5500_Set_Mask(Mask);
W5500_Get_Mask(&w5500_get_mask);
printf("W5500 w5500_get_mask = %d %d %d %dn" , w5500_get_mask[0] , w5500_get_mask[1] , w5500_get_mask[2] , w5500_get_mask[3]);
W5500_Set_Gateway(Gateway);
W5500_Get_Gateway(&w5500_get_gateway);
printf("W5500 w5500_get_gateway = %d %d %d %dn" , w5500_get_gateway[0] , w5500_get_gateway[1] , w5500_get_gateway[2] , w5500_get_gateway[3]);
W5500_SocketInit(txsize , rxsize);
if(socketopen & 0x01) W5500_Socket_Mode_Init(0 , socketType[0]);
if(socketopen & 0x02) W5500_Socket_Mode_Init(1 , socketType[1]);
W5500_Socket_Mode_Init(2 , socketType[2]);
W5500_Socket_Mode_Init(3 , socketType[3]);
//Enable socket(channel) 0 and 1 , set socket 0 and 1 for UDP mode/unicast
}
void W5500_Init()
{
GPIO_InitType GPIO_InitStructure = {0};
RCC_EnableAPB2PeriphClk(W5500_CS_GPIOEN | W5500_RST_GPIOEN | W5500_INT_GPIOEN , ENABLE);
GPIO_InitStructure.Pin = W5500_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(W5500_CS_GPIO , &GPIO_InitStructure);
GPIO_InitStructure.Pin = W5500_RST_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(W5500_RST_GPIO , &GPIO_InitStructure);
GPIO_InitStructure.Pin = W5500_INT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(W5500_INT_GPIO , &GPIO_InitStructure);
W5500_RST_GPIO->PBC |= W5500_RST_PIN;
Delay_ms(100);
W5500_RST_GPIO->PBSC |= W5500_RST_PIN;
Delay_ms(100);
SPI1_Init();
W5500_SetEthernet();
W5500_ReconnectTimeout(2000);
W5500_ReconnectCount(3);
W5500_InterruptMask(socketopen | 0x10);
W5500_Set_INTLEVEL(0x0008);
}
void W5500_Send_UDP_Buffer_Specify_IP_port(uint8_t s, uint8_t buffer[] , uint16_t len ,
uint8_t specify_ip[] , int specify_port)
//W5500 send udp buffer
{
uint8_t temp[4];
uint32_t writeAddr;
IINCHIP_ISR_DISABLE();
//disable W5500 IRQ
W5500_Write_Buffer(Sn_DIPR0(s), specify_ip , 4);
W5500_WriteByte(Sn_DPORT0(s), specify_port >> 8);
W5500_WriteByte(Sn_DPORT1(s), specify_port);
W5500_Read_Buffer(Sn_TX_WR0(s), temp, 2);
writeAddr = ((uint32_t)temp[0] << 16) | ((uint32_t)temp[1] << 8) | 0x10 | (s << 5);
W5500_Write_Buffer(writeAddr, (uint8_t *)buffer, len);
writeAddr >>= 8;
writeAddr += len;
temp[0] = writeAddr >> 8;
temp[1] = writeAddr;
W5500_Write_Buffer(Sn_TX_WR0(s), temp, 2);
W5500_WriteByte(Sn_CR(s), Sn_CR_SEND);
IINCHIP_ISR_ENABLE();
//enable W5500 IRQ
}
更多回帖