一、SPI的配置过程
1.使能SPI时钟
1 /** 2 * @brief 使能SPI时钟 3 * @retval None 4 */ 5 static void SPI_RCC_Configuration(void) 6 { 7 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_SPI1,ENABLE); 8 }
2.配置指定SPI的引脚
1 /** 2 * @brief 配置指定SPI的引脚 3 * @retval None 4 */ 5 static void SPI_GPIO_Configuration(void) 6 { 7 GPIO_InitTypeDef GPIO_InitStruct; 8 //PA4->CS,PA5->SCK,PA6->MISO,PA7->MOSI 9 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6|GPIO_Pin_7; 10 GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 11 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; 12 GPIO_Init(GPIOA, &GPIO_InitStruct); 13 //初始化片选输出引脚 14 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; 15 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; 16 GPIO_Init(GPIOA, &GPIO_InitStruct); 17 GPIO_SetBits(GPIOA,GPIO_Pin_4); 18 }
3.根据外部SPI设备配置SPI相关参数
1 /** 2 * @brief 根据外部SPI设备配置SPI相关参数 3 * @retval None 4 */ 5 void SPI_Configuration(void) 6 { 7 SPI_InitTypeDef SPI_InitStruct; 8 9 SPI_RCC_Configuration(); 10 11 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; 12 SPI_InitStruct.SPI_Direction= SPI_Direction_2Lines_FullDuplex; 13 SPI_InitStruct.SPI_Mode = SPI_Mode_Master; 14 SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; 15 SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; 16 SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; 17 SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; 18 SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; 19 SPI_InitStruct.SPI_CRCPolynomial = 7; 20 SPI_Init(SPI1,&SPI_InitStruct); 21 22 SPI_GPIO_Configuration(); 23 24 SPI_SSOutputCmd(SPI1, ENABLE); 25 SPI_Cmd(SPI1, ENABLE); 26 }
二、W5500初始化过程
注册SPI函数、初始化Socket Buffer、PHY状态检查、初始化Network等工作
1 void wizchip_user_init(void) 2 { 3 uint8_t tmp; 4 uint8_t memsize[2][8] = {{2,2,2,2,2,2,2,2},{2,2,2,2,2,2,2,2}}; 5 6 // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP 7 /* Critical section callback */ 8 reg_wizchip_cris_cbfunc(SPI_CrisEnter, SPI_CrisExit); //注册临界区函数 9 /* Chip selection call back */ 10 #if _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_VDM_ 11 reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect);//注册SPI片选信号函数 12 #elif _WIZCHIP_IO_MODE_ == _WIZCHIP_IO_MODE_SPI_FDM_ 13 reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW. 14 #else 15 #if (_WIZCHIP_IO_MODE_ & _WIZCHIP_IO_MODE_SIP_) != _WIZCHIP_IO_MODE_SIP_ 16 #error "Unknown _WIZCHIP_IO_MODE_" 17 #else 18 reg_wizchip_cs_cbfunc(wizchip_select, wizchip_deselect); 19 #endif 20 #endif 21 /* SPI Read & Write callback function */ 22 reg_wizchip_spi_cbfunc(SPI_ReadByte, SPI_WriteByte); //注册读写函数 23 24 /* WIZCHIP SOCKET Buffer initialize */ 25 if(ctlwizchip(CW_INIT_WIZCHIP,(void*)memsize) == -1){ 26 printf("WIZCHIP Initialized fail.\r\n"); 27 while(1); 28 } 29 30 /* PHY link status check */ 31 do{ 32 if(ctlwizchip(CW_GET_PHYLINK, (void*)&tmp) == -1){ 33 printf("Unknown PHY Link stauts.\r\n"); 34 } 35 }while(tmp == PHY_LINK_OFF); 36 37 /* Network initialization */ 38 network_init(); 39 }
网络初始化
1 /** 2 * @brief Intialize the network information to be used in WIZCHIP 3 * @retval None 4 */ 5 void network_init(void) 6 { 7 uint8_t tmpstr[6]; 8 ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO); 9 ctlnetwork(CN_GET_NETINFO, (void*)&gWIZNETINFO); 10 11 // Display Network Information 12 ctlwizchip(CW_GET_ID,(void*)tmpstr); 13 printf("\r\n=== %s NET CONF ===\r\n",(char*)tmpstr); 14 printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2], 15 gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]); 16 printf("SIP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]); 17 printf("GAR: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]); 18 printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]); 19 printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]); 20 printf("======================\r\n"); 21 }
三、下面着重介绍几个重要的函数:
3.1 函数原型:ctlwizchip(ctlwizchip_type cwtype,void* arg)
参数1:cwtype
参数2:arg
功能:控制WIZCHIP芯片,重置WIZCHIP和内部PHY,配置PHY模式,监视PHY(链接,速度,半/全/自动),控制中断和屏蔽等。
参数1:cwtype是一枚举类型,具体如下:
参数2:arg空类型
3.2 函数原型:ctlnetwork(ctlnetwork_type cntype,void* arg)
参数1:cntype
参数2:arg
功能:控制网络,控制网络环境,模式,超时等
参数1:cntype是一枚举类型,具体如下:
参数2:arg空类型
3.3 函数原型:void reg_wizchip_cris_cbfunc ( void(*)(void) cris_en, void(*)(void) cris_ex )
参数1、2为函数指针类型的参数
功能:为I / O功能的关键部分注册回调函数,例如WIZCHIP_READ,WIZCHIP_WRITE,WIZCHIP_READ_BUF和WIZCHIP_WRITE_BUF。
这几个函数的具体实现,可以参考官方的源码,我这里给出官方的源码下载地址:https://github.com/Wiznet/ioLibrary_Driver,如果感觉文章写的还可以,关注本人博客:https://www.cnblogs.com/TonyJia/