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
| /\* \* w5500\_port.c \* \* Created on: 2022年6月29日 \* Author: jenson \*/
#include "w5500\_port.h" #include "main.h" #include "spi.h" #include <stdio.h> #include "wizchip\_conf.h" #include "socket.h" #include "loopback.h" #include "DHCP/dhcp.h" #include <string.h>
static wiz_NetInfo gWIZNETINFO = { .mac = { 0x00, 0x08, 0xdc, 0x11, 0x11, 0x11 }, .ip = { 192, 168, 2, 99 }, .sn = { 255, 255, 255, 0 }, .gw = { 192, 168,2, 1 }, .dns = { 8, 8, 8, 8 }, .dhcp = NETINFO_STATIC };
static void \_\_w5500\_spi\_cs\_select(void) { HAL\_GPIO\_WritePin(W5500_CS_GPIO_Port, W5500_CS_Pin, GPIO_PIN_RESET); }
static void \_\_w5500\_spi\_cs\_deselect(void) { HAL\_GPIO\_WritePin(W5500_CS_GPIO_Port, W5500_CS_Pin, GPIO_PIN_SET); }
void w5500\_restart(void) { HAL\_GPIO\_WritePin(W5500_RST_GPIO_Port, W5500_RST_Pin, GPIO_PIN_RESET); HAL\_Delay(1); // delay 1ms HAL\_GPIO\_WritePin(W5500_RST_GPIO_Port, W5500_RST_Pin, GPIO_PIN_SET); HAL\_Delay(1600); // delay 1600ms printf("w5500 restarted\r\n"); }
/\*\* \* @brief? 进入临界区 \* @retval None \*/ void \_\_w5500\_spi\_cris\_enter(void) { \_\_set\_PRIMASK(1); }
/\*\* \* @brief? 退出临界区 \* @retval None \*/ void \_\_w5500\_spi\_cris\_exit(void) { \_\_set\_PRIMASK(0); }
static void \_\_w5500\_spi\_read\_buffer(uint8\_t \*buff, uint16\_t len) { HAL\_SPI\_Receive(&hspi1, buff, len, HAL_MAX_DELAY); }
static void \_\_w5500\_spi\_write\_buffer(uint8\_t \*buff, uint16\_t len) { HAL\_SPI\_Transmit(&hspi1, buff, len, HAL_MAX_DELAY); }
static uint8\_t \_\_w5500\_read\_byte(void) { uint8\_t byte; \_\_w5500\_spi\_read\_buffer(&byte, sizeof(byte)); return byte; }
static void \_\_w5500\_write\_byte(uint8\_t byte) { \_\_w5500\_spi\_write\_buffer(&byte, sizeof(byte)); }
void wiznet\_register(void) { // First of all, Should register SPI callback functions implemented by user for accessing WIZCHIP /\* Critical section callback \*/ reg\_wizchip\_cris\_cbfunc(__w5500_spi_cris_enter, __w5500_spi_cris_exit); //注册临界区函数 /\* Chip selection call back \*/ #if \_WIZCHIP\_IO\_MODE\_ == \_WIZCHIP\_IO\_MODE\_SPI\_VDM\_ reg\_wizchip\_cs\_cbfunc(__w5500_spi_cs_select, __w5500_spi_cs_deselect); //注册SPI片选信号函数 #elif \_WIZCHIP\_IO\_MODE\_ == \_WIZCHIP\_IO\_MODE\_SPI\_FDM\_ reg\_wizchip\_cs\_cbfunc(SPI_CS_Select, SPI_CS_Deselect); // CS must be tried with LOW. #else #if (\_WIZCHIP\_IO\_MODE\_ & \_WIZCHIP\_IO\_MODE\_SIP\_) != \_WIZCHIP\_IO\_MODE\_SIP\_ #error "Unknown \_WIZCHIP\_IO\_MODE\_" #else reg\_wizchip\_cs\_cbfunc(wizchip_select, wizchip_deselect); #endif #endif /\* SPI Read & Write callback function \*/ reg\_wizchip\_spi\_cbfunc(__w5500_read_byte, __w5500_write_byte); //注册读写函数
return; }
/\*\* \* @brief : w5500\_network\_init \* @note : W5500 网络初始化 \* @param : \* @retval : \*/ void w5500\_network\_init\_static(void) {
uint8\_t chipid[6]; uint8\_t mac[6] = { 0x00, 0x08, 0xdc, 0x11, 0x11, 0x11 }; ///< Source Mac Address uint8\_t ip[4] = { 192, 168, 2, 99 }; ///< Source IP Address uint8\_t sn[4] = { 255, 255, 255, 0 }; ///< Subnet Mask uint8\_t gw[4] = { 192, 168, 2, 1 }; ///< Gateway IP Address uint8\_t dns[4] = { 114, 114, 114, 114 }; ///< DNS server IP Address
memcpy(gWIZNETINFO.ip, ip, 4); memcpy(gWIZNETINFO.sn, sn, 4); memcpy(gWIZNETINFO.gw, gw, 4); memcpy(gWIZNETINFO.mac, mac, 6); memcpy(gWIZNETINFO.dns, dns, 4); printf("start init w5500(static)\r\n"); gWIZNETINFO.dhcp = NETINFO_STATIC; //< 1 - Static, 2 - DHCP ctlnetwork(CN_SET_NETINFO, (void\*) &gWIZNETINFO);
ctlnetwork(CN_GET_NETINFO, (void\*) &gWIZNETINFO); // Display Network Information ctlwizchip(CW_GET_ID, (void\*) chipid); printf("\r\n=== %s NET CONF ===\r\n", (char\*) chipid); printf("MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", gWIZNETINFO.mac[0], gWIZNETINFO.mac[1], gWIZNETINFO.mac[2], gWIZNETINFO.mac[3], gWIZNETINFO.mac[4], gWIZNETINFO.mac[5]); printf("SIP: %d.%d.%d.%d\r\n", gWIZNETINFO.ip[0], gWIZNETINFO.ip[1], gWIZNETINFO.ip[2], gWIZNETINFO.ip[3]); printf("GAR: %d.%d.%d.%d\r\n", gWIZNETINFO.gw[0], gWIZNETINFO.gw[1], gWIZNETINFO.gw[2], gWIZNETINFO.gw[3]); printf("SUB: %d.%d.%d.%d\r\n", gWIZNETINFO.sn[0], gWIZNETINFO.sn[1], gWIZNETINFO.sn[2], gWIZNETINFO.sn[3]); printf("DNS: %d.%d.%d.%d\r\n", gWIZNETINFO.dns[0], gWIZNETINFO.dns[1], gWIZNETINFO.dns[2], gWIZNETINFO.dns[3]); printf("======================\r\n");
wizchip\_init(NULL, NULL); printf("w5500 inited\r\n"); }
//初始化芯片参数 void wiznet\_chip\_config(void) { uint8\_t memsize[2][8] = { { 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2 } };
//WIZCHIP SOCKET缓存区初始化 if (ctlwizchip(CW_INIT_WIZCHIP, (void\*) memsize) == -1) { printf("WIZCHIP Initialized fail.\r\n"); while (1) ; } printf("w5500 chip configured\r\n"); }
|