提交 | 用户 | age
|
85d591
|
1 |
|
Q |
2 |
#include "stm32f0xx_hal.h"
|
|
3 |
#include "w5500_port.h"
|
|
4 |
#include "Globaldef.h"
|
|
5 |
#include "KMachine.h"
|
|
6 |
/**
|
|
7 |
* @brief enter critical section
|
|
8 |
* @param none
|
|
9 |
* @return none
|
|
10 |
*/
|
|
11 |
static void w5500_cris_enter(void)
|
|
12 |
{
|
|
13 |
__disable_irq();
|
|
14 |
}
|
|
15 |
|
|
16 |
/**
|
|
17 |
* @brief exit critical section
|
|
18 |
* @param none
|
|
19 |
* @return none
|
|
20 |
*/
|
|
21 |
static void w5500_cris_exit(void)
|
|
22 |
{
|
|
23 |
__enable_irq();
|
|
24 |
}
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
#define setW5500CLK_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_3)
|
|
29 |
#define setW5500CLK_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_3)
|
|
30 |
#define SetW5500SER_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_5)
|
|
31 |
#define SetW5500SER_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_5)
|
|
32 |
#define GetW5500SER() LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_4)
|
|
33 |
|
|
34 |
/**
|
|
35 |
* @brief select chip
|
|
36 |
* @param none
|
|
37 |
* @return none
|
|
38 |
*/
|
|
39 |
int nIndexCount=0;
|
|
40 |
static void w5500_cs_select(void)
|
|
41 |
{
|
|
42 |
// setW5500CLK_1();
|
|
43 |
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15);
|
|
44 |
nIndexCount=0;
|
|
45 |
}
|
|
46 |
|
|
47 |
/**
|
|
48 |
* @brief deselect chip
|
|
49 |
* @param none
|
|
50 |
* @return none
|
|
51 |
*/
|
|
52 |
static void w5500_cs_deselect(void)
|
|
53 |
{
|
|
54 |
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15);
|
|
55 |
}
|
|
56 |
|
|
57 |
|
|
58 |
#ifdef SOFT_SPI
|
|
59 |
|
|
60 |
static uint8_t w5500_spi_readbyte_sw(void)
|
|
61 |
{
|
|
62 |
uint8_t value=0;
|
|
63 |
unsigned int mask1=0x0080;
|
|
64 |
// volatile uint32_t * p1=&GPIOA->IDR;
|
|
65 |
SetW5500SER_1();
|
|
66 |
for (;mask1;)
|
|
67 |
{
|
|
68 |
setW5500CLK_0();
|
|
69 |
//*p1=LL_GPIO_PIN_13;
|
|
70 |
|
|
71 |
setW5500CLK_1();
|
|
72 |
if (GetW5500SER()) value |= mask1;
|
|
73 |
mask1>>=1;
|
|
74 |
// __nop();
|
|
75 |
//*p2=LL_GPIO_PIN_13;
|
|
76 |
}
|
|
77 |
KMem.SDB[128+nIndexCount] = value;
|
|
78 |
nIndexCount++;
|
|
79 |
if (nIndexCount>40) {nIndexCount=0;}
|
|
80 |
return value;
|
|
81 |
}
|
|
82 |
|
|
83 |
static void w5500_spi_writebyte_sw(uint8_t wb)
|
|
84 |
{
|
|
85 |
unsigned int mask1=0x0080;
|
|
86 |
//volatile uint32_t * p1 = &GPIOB->BRR;
|
|
87 |
//volatile uint32_t * p2 = &GPIOB->BSRR;
|
|
88 |
KMem.SDB[128+nIndexCount] = wb;
|
|
89 |
nIndexCount++;
|
|
90 |
if (nIndexCount>40) {nIndexCount=0;}
|
|
91 |
|
|
92 |
for (;mask1;)
|
|
93 |
{
|
|
94 |
setW5500CLK_0();
|
|
95 |
//*p1=LL_GPIO_PIN_13;
|
|
96 |
if (wb&mask1) {SetW5500SER_1();}
|
|
97 |
else {SetW5500SER_0();}
|
|
98 |
mask1>>=1;
|
|
99 |
setW5500CLK_1();
|
|
100 |
// __nop();
|
|
101 |
//*p2=LL_GPIO_PIN_13;
|
|
102 |
}
|
|
103 |
|
|
104 |
}
|
|
105 |
#else
|
|
106 |
/**
|
|
107 |
* @brief read byte in SPI interface
|
|
108 |
* @param none
|
|
109 |
* @return the value of the byte read
|
|
110 |
*/
|
|
111 |
#define TIMEOUT 50
|
|
112 |
static uint8_t w5500_spi_readbyte(void)
|
|
113 |
{
|
|
114 |
uint8_t value;
|
|
115 |
int timeout_cnt= 0;
|
|
116 |
|
|
117 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;}
|
|
118 |
KMem.SDT[88]=timeout_cnt;
|
|
119 |
value = LL_SPI_ReceiveData8( SPI1);
|
|
120 |
LL_SPI_TransmitData8(SPI1,0x5a);
|
|
121 |
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; }
|
|
122 |
KMem.SDT[89]=timeout_cnt;
|
|
123 |
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;}
|
|
124 |
KMem.SDT[89]+=timeout_cnt;
|
|
125 |
value = LL_SPI_ReceiveData8( SPI1);
|
|
126 |
KMem.SDB[128+nIndexCount] = value;
|
|
127 |
|
|
128 |
nIndexCount++;
|
|
129 |
if (nIndexCount>40) {nIndexCount=0;}
|
|
130 |
return value;
|
|
131 |
}
|
|
132 |
|
|
133 |
/**
|
|
134 |
* @brief write byte in SPI interface
|
|
135 |
* @param wb the value to write
|
|
136 |
* @return none
|
|
137 |
*/
|
|
138 |
static void w5500_spi_writebyte(uint8_t wb)
|
|
139 |
{
|
|
140 |
int timeout_cnt= 0;
|
|
141 |
uint8_t value;
|
|
142 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; }
|
|
143 |
KMem.SDT[90]=timeout_cnt;
|
|
144 |
LL_SPI_TransmitData8(SPI1,wb);
|
|
145 |
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; }
|
|
146 |
KMem.SDT[91]=timeout_cnt;
|
|
147 |
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;}
|
|
148 |
KMem.SDT[91]+=timeout_cnt;
|
|
149 |
value = LL_SPI_ReceiveData8( SPI1);
|
|
150 |
|
|
151 |
}
|
|
152 |
#endif
|
|
153 |
|
|
154 |
/**
|
|
155 |
* @brief burst read byte in SPI interface
|
|
156 |
* @param pBuf pointer of data buf
|
|
157 |
* @param len number of bytes to read
|
|
158 |
* @return none
|
|
159 |
*/
|
|
160 |
static void w5500_spi_readburst(uint8_t* pBuf, uint16_t len)
|
|
161 |
{
|
|
162 |
if (!pBuf) {
|
|
163 |
return;
|
|
164 |
}
|
|
165 |
uint8_t value;
|
|
166 |
for (int i=0;i<len;i++){
|
|
167 |
LL_SPI_TransmitData8(SPI1,0xFF);
|
|
168 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { }
|
|
169 |
while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) { }
|
|
170 |
value = LL_SPI_ReceiveData8( SPI1);
|
|
171 |
pBuf[i]=value;
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* @brief burst write byte in SPI interface
|
|
177 |
* @param pBuf pointer of data buf
|
|
178 |
* @param len number of bytes to write
|
|
179 |
* @return none
|
|
180 |
*/
|
|
181 |
static void w5500_spi_writeburst(uint8_t* pBuf, uint16_t len)
|
|
182 |
{
|
|
183 |
if (!pBuf) {
|
|
184 |
return;
|
|
185 |
}
|
|
186 |
for (int i=0;i<len;i++)
|
|
187 |
{
|
|
188 |
|
|
189 |
LL_SPI_TransmitData8(SPI1,pBuf[i]);
|
|
190 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) { }
|
|
191 |
}
|
|
192 |
while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) { }
|
|
193 |
}
|
|
194 |
|
|
195 |
/**
|
|
196 |
* @brief hard reset
|
|
197 |
* @param none
|
|
198 |
* @return none
|
|
199 |
*/
|
|
200 |
static void w5500_hard_reset(void)
|
|
201 |
{
|
|
202 |
HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST_PIN, GPIO_PIN_RESET);
|
|
203 |
HAL_Delay(50);
|
|
204 |
HAL_GPIO_WritePin(W5500_RST_PORT, W5500_RST_PIN, GPIO_PIN_SET);
|
|
205 |
HAL_Delay(10);
|
|
206 |
}
|
|
207 |
|
|
208 |
/**
|
|
209 |
* @brief Initializes WIZCHIP with socket buffer size
|
|
210 |
* @param none
|
|
211 |
* @return errcode
|
|
212 |
* @retval 0 success
|
|
213 |
* @retval -1 fail
|
|
214 |
*/
|
|
215 |
static int w5500_chip_init(void)
|
|
216 |
{
|
|
217 |
/* default size is 2KB */
|
|
218 |
|
|
219 |
return wizchip_init(NULL, NULL);
|
|
220 |
}
|
|
221 |
|
|
222 |
/**
|
|
223 |
* @brief set phy config if autonego is disable
|
|
224 |
* @param none
|
|
225 |
* @return none
|
|
226 |
*/
|
|
227 |
static void w5500_phy_init(void)
|
|
228 |
{
|
|
229 |
#ifdef USE_AUTONEGO
|
|
230 |
// no thing to do
|
|
231 |
#else
|
|
232 |
wiz_PhyConf conf;
|
|
233 |
|
|
234 |
conf.by = PHY_CONFBY_SW;
|
|
235 |
conf.mode = PHY_MODE_MANUAL;
|
|
236 |
conf.speed = PHY_SPEED_100;
|
|
237 |
conf.duplex = PHY_DUPLEX_FULL;
|
|
238 |
|
|
239 |
wizphy_setphyconf(&conf);
|
|
240 |
#endif
|
|
241 |
}
|
|
242 |
|
|
243 |
/**
|
|
244 |
* @brief initializes the network infomation
|
|
245 |
* @param none
|
|
246 |
* @return none
|
|
247 |
*/
|
|
248 |
static void w5500_network_info_init(void)
|
|
249 |
{
|
|
250 |
wiz_NetInfo info;
|
|
251 |
|
|
252 |
uint8_t mac[6] = DEFAULT_MAC_ADDR;
|
|
253 |
uint8_t ip[4] = DEFAULT_IP_ADDR;
|
|
254 |
uint8_t sn[4] = DEFAULT_SUB_MASK;
|
|
255 |
uint8_t gw[4] = DEFAULT_GW_ADDR;
|
|
256 |
uint8_t dns[4] = DEFAULT_DNS_ADDR;
|
|
257 |
|
|
258 |
memcpy(info.mac, mac, 6);
|
|
259 |
memcpy(info.ip, ip, 4);
|
|
260 |
memcpy(info.sn, sn, 4);
|
|
261 |
memcpy(info.gw, gw, 4);
|
|
262 |
memcpy(info.dns, dns, 4);
|
|
263 |
|
|
264 |
#ifdef USE_DHCP
|
|
265 |
info.dhcp = NETINFO_DHCP;
|
|
266 |
#else
|
|
267 |
info.dhcp = NETINFO_STATIC;
|
|
268 |
#endif
|
|
269 |
|
|
270 |
wizchip_setnetinfo(&info);
|
|
271 |
}
|
|
272 |
|
|
273 |
/**
|
|
274 |
* @brief read and show the network infomation
|
|
275 |
* @param none
|
|
276 |
* @return none
|
|
277 |
*/
|
|
278 |
void w5500_network_info_show(void)
|
|
279 |
{
|
|
280 |
wiz_NetInfo info;
|
|
281 |
|
|
282 |
wizchip_getnetinfo(&info);
|
|
283 |
|
|
284 |
// printf("w5500 network infomation:\r\n");
|
|
285 |
// printf(" -mac:%d:%d:%d:%d:%d:%d\r\n", info.mac[0], info.mac[1], info.mac[2],
|
|
286 |
// info.mac[3], info.mac[4], info.mac[5]);
|
|
287 |
// printf(" -ip:%d.%d.%d.%d\r\n", info.ip[0], info.ip[1], info.ip[2], info.ip[3]);
|
|
288 |
// printf(" -sn:%d.%d.%d.%d\r\n", info.sn[0], info.sn[1], info.sn[2], info.sn[3]);
|
|
289 |
// printf(" -gw:%d.%d.%d.%d\r\n", info.gw[0], info.gw[1], info.gw[2], info.gw[3]);
|
|
290 |
// printf(" -dns:%d.%d.%d.%d\r\n", info.dns[0], info.dns[1], info.dns[2], info.dns[3]);
|
|
291 |
|
|
292 |
if (info.dhcp == NETINFO_DHCP) {
|
|
293 |
// printf(" -dhcp_mode: dhcp\r\n");
|
|
294 |
} else {
|
|
295 |
// printf(" -dhcp_mode: static\r\n");
|
|
296 |
}
|
|
297 |
|
|
298 |
KMem.SDB[192]=info.mac[0];KMem.SDB[193]=info.mac[1];KMem.SDB[194]=info.mac[2];
|
|
299 |
KMem.SDB[195]=info.mac[3];KMem.SDB[196]=info.mac[4];KMem.SDB[197]=info.mac[5];
|
|
300 |
|
|
301 |
KMem.SDB[200]=info.ip[0];KMem.SDB[201]=info.ip[1];KMem.SDB[202]=info.ip[2]; KMem.SDB[203]=info.ip[3];
|
|
302 |
KMem.SDB[204]=info.sn[0];KMem.SDB[205]=info.sn[1];KMem.SDB[206]=info.sn[2]; KMem.SDB[207]=info.sn[3];
|
|
303 |
|
|
304 |
KMem.SDB[208]=info.gw[0];KMem.SDB[209]=info.gw[1];KMem.SDB[210]=info.gw[2]; KMem.SDB[211]=info.gw[3];
|
|
305 |
KMem.SDB[212]=info.dns[0];KMem.SDB[213]=info.dns[1];KMem.SDB[214]=info.dns[2]; KMem.SDB[215]=info.dns[3];
|
|
306 |
|
|
307 |
}
|
|
308 |
|
|
309 |
/**
|
|
310 |
* @brief w5500 init
|
|
311 |
* @param none
|
|
312 |
* @return errcode
|
|
313 |
* @retval 0 success
|
|
314 |
* @retval -1 chip init fail
|
|
315 |
*/
|
|
316 |
int w5500_init(void)
|
|
317 |
{
|
|
318 |
/* W5500 hard reset */
|
|
319 |
w5500_hard_reset();
|
|
320 |
|
|
321 |
/* Register spi driver function */
|
|
322 |
reg_wizchip_cris_cbfunc(w5500_cris_enter, w5500_cris_exit);
|
|
323 |
reg_wizchip_cs_cbfunc(w5500_cs_select, w5500_cs_deselect);
|
|
324 |
#ifdef SOFT_SPI
|
|
325 |
reg_wizchip_spi_cbfunc(w5500_spi_readbyte_sw, w5500_spi_writebyte_sw);
|
|
326 |
|
|
327 |
#else
|
|
328 |
reg_wizchip_spi_cbfunc(w5500_spi_readbyte, w5500_spi_writebyte);
|
|
329 |
#endif
|
|
330 |
|
|
331 |
// reg_wizchip_spiburst_cbfunc(w5500_spi_readburst, w5500_spi_writeburst);
|
|
332 |
|
|
333 |
/* socket buffer size init */
|
|
334 |
if (w5500_chip_init() != 0) {
|
|
335 |
return -1;
|
|
336 |
}
|
|
337 |
|
|
338 |
/* phy init */
|
|
339 |
w5500_phy_init();
|
|
340 |
|
|
341 |
/* network infomation init */
|
|
342 |
w5500_network_info_init();
|
|
343 |
|
|
344 |
/* show network infomation */
|
|
345 |
w5500_network_info_show();
|
|
346 |
|
|
347 |
return 0;
|
|
348 |
}
|