1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
| #include "BSP_W5500.h" #include "wizchip_conf.h" #include "delay.h" #include "string.h" #include "stdio.h"
void wiz_spi_init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = WIZ_SCS_PIN; GPIO_Init(WIZ_SCS_PORT, &GPIO_InitStructure); GPIO_SetBits(WIZ_SCS_PORT, WIZ_SCS_PIN); }
void wiz_rst_int_init(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStructure.GPIO_Pin = WIZ_RST_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(WIZ_RST_PORT, &GPIO_InitStructure); GPIO_SetBits(WIZ_RST_PORT, WIZ_RST_PIN);
GPIO_InitStructure.GPIO_Pin = WIZ_INT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(WIZ_INT_PORT, &GPIO_InitStructure); }
extern uint8_t Gateway_ip[4]; extern uint8_t local_ip[4]; wiz_NetInfo gWIZNETINFO = { .mac = {0x78, 0x83, 0x68, 0x88, 0x56, 0x72}, .ip = {0}, .sn = {255,255,255,0}, .gw = {0}, .dns = {144,144,144,144}, .dhcp = NETINFO_STATIC};
void SPI_CS_Select(void) { W5500_CS_L; }
void SPI_CS_Deselect(void) { W5500_CS_H; }
void wizchip_write_byte(uint8_t TxData) { while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) { } SPI_I2S_SendData(SPI2, TxData); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) { } SPI_I2S_ReceiveData(SPI2); }
uint8_t wizchip_read_byte(void) { while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET) { } SPI_I2S_SendData(SPI2, 0xffff); while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET) { } return SPI_I2S_ReceiveData(SPI2); }
void wizchip_write_buff(uint8_t *buf, uint16_t len) { uint16_t idx = 0; for (idx = 0; idx < len; idx++) { wizchip_write_byte(buf[idx]); } }
void wizchip_read_buff(uint8_t *buf, uint16_t len) { uint16_t idx = 0; for (idx = 0; idx < len; idx++) { buf[idx] = wizchip_read_byte(); } }
void wiz_print_phy_info(void) { uint8_t get_phy_conf; get_phy_conf = getPHYCFGR(); printf("The current Mbtis speed : %dMbps\r\n", get_phy_conf & 0x02 ? 100 : 10); printf("The current Duplex Mode : %s\r\n", get_phy_conf & 0x04 ? "Full-Duplex" : "Half-Duplex"); }
void print_network_information(void) { wiz_NetInfo net_info; wizchip_getnetinfo(&net_info);
if (net_info.dhcp == NETINFO_DHCP) { printf("====================================================================================================\r\n"); printf(" %s network configuration : DHCP\r\n\r\n", _WIZCHIP_ID_); } else { printf("====================================================================================================\r\n"); printf(" %s network configuration : static\r\n\r\n", _WIZCHIP_ID_); }
printf(" MAC : %02X:%02X:%02X:%02X:%02X:%02X\r\n", net_info.mac[0], net_info.mac[1], net_info.mac[2], net_info.mac[3], net_info.mac[4], net_info.mac[5]); printf(" IP : %d.%d.%d.%d\r\n", net_info.ip[0], net_info.ip[1], net_info.ip[2], net_info.ip[3]); printf(" Subnet Mask : %d.%d.%d.%d\r\n", net_info.sn[0], net_info.sn[1], net_info.sn[2], net_info.sn[3]); printf(" Gateway : %d.%d.%d.%d\r\n", net_info.gw[0], net_info.gw[1], net_info.gw[2], net_info.gw[3]); printf(" DNS : %d.%d.%d.%d\r\n", net_info.dns[0], net_info.dns[1], net_info.dns[2], net_info.dns[3]); printf("====================================================================================================\r\n\r\n"); }
void W5500_RESET(void) { W5500_REST_L; delay_ms(50); W5500_REST_H; delay_ms(50); }
void wizchip_spi_cb_reg(void) { reg_wizchip_cs_cbfunc(SPI_CS_Select, SPI_CS_Deselect); reg_wizchip_spi_cbfunc(wizchip_read_byte, wizchip_write_byte); reg_wizchip_spiburst_cbfunc(wizchip_read_buff, wizchip_write_buff); }
void wiz_phy_link_check(void) { uint8_t phy_link_status; do { delay_ms(1000); ctlwizchip(CW_GET_PHYLINK, (void *)&phy_link_status); if (phy_link_status == PHY_LINK_ON) { printf("PHY link\r\n"); wiz_print_phy_info(); } else { printf("PHY no link\r\n"); } } while (phy_link_status == PHY_LINK_OFF); }
void W5500_ChipInit(void) { wizchip_spi_cb_reg(); W5500_RESET(); wiz_phy_link_check(); memcpy(gWIZNETINFO.ip,local_ip,sizeof(local_ip)); memcpy(gWIZNETINFO.gw,Gateway_ip,sizeof(Gateway_ip)); wizchip_setnetinfo(&gWIZNETINFO); print_network_information();
} #ifndef __BSP_W5500_H #define __BSP_W5500_H #include "sys.h"
#define W5500_REST_L GPIO_ResetBits(WIZ_RST_PORT,WIZ_RST_PIN); #define W5500_REST_H GPIO_SetBits(WIZ_RST_PORT,WIZ_RST_PIN);
#define W5500_CS_L GPIO_ResetBits(WIZ_SCS_PORT,WIZ_SCS_PIN); #define W5500_CS_H GPIO_SetBits(WIZ_SCS_PORT,WIZ_SCS_PIN);
#define WIZ_RST_PIN GPIO_Pin_10 #define WIZ_RST_PORT GPIOB #define WIZ_INT_PIN GPIO_Pin_11 #define WIZ_INT_PORT GPIOB #define WIZ_SCS_PIN GPIO_Pin_12 #define WIZ_SCS_PORT GPIOB #define WIZ_SCK_PIN GPIO_Pin_13 #define WIZ_SCK_PORT GPIOB #define WIZ_MISO_PIN GPIO_Pin_14 #define WIZ_MISO_PORT GPIOB #define WIZ_MOSI_PIN GPIO_Pin_15 #define WIZ_MOSI_PORT GPIOB
void wiz_spi_init(void); void wiz_rst_int_init(void); void W5500_ChipInit(void);
#endif
|