Socket APIs
wizchip_conf.c
Go to the documentation of this file.
1 //****************************************************************************/
39 //
40 //*****************************************************************************/
41 #include "wizchip_conf.h"
47 void wizchip_cris_enter(void) {};
53 void wizchip_cris_exit(void) {};
59 void wizchip_cs_select(void) {};
65 void wizchip_cs_deselect(void) {};
71 uint8_t wizchip_bus_readbyte(uint32_t AddrSel) { return * ((volatile uint8_t *) AddrSel); };
78 void wizchip_bus_writebyte(uint32_t AddrSel, uint8_t wb) { *((volatile uint8_t*)AddrSel) = wb; };
84 uint8_t wizchip_spi_readbyte(void) {return 0;};
90 void wizchip_spi_writebyte(uint8_t wb) {};
91 
96  {
97  .id = _WIZCHIP_ID_,
98  .if_mode = _WIZCHIP_IO_MODE_,
99  .CRIS._enter = wizchip_cris_enter,
100  .CRIS._exit = wizchip_cris_exit,
101  .CS._select = wizchip_cs_select,
102  .CS._deselect = wizchip_cs_deselect,
103  .IF.BUS._read_byte = wizchip_bus_readbyte,
104  .IF.BUS._write_byte = wizchip_bus_writebyte
105 // .IF.SPI._read_byte = wizchip_spi_readbyte,
106 // .IF.SPI._write_byte = wizchip_spi_writebyte
107  };
108 
109 static uint8_t _DNS_[4]; // DNS server ip address
110 static dhcp_mode _DHCP_; // DHCP mode
111 
112 void reg_wizchip_cris_cbfunc(void(*cris_en)(void), void(*cris_ex)(void))
113 {
114  if(!cris_en || !cris_ex)
115  {
116  WIZCHIP.CRIS._enter = wizchip_cris_enter;
117  WIZCHIP.CRIS._exit = wizchip_cris_exit;
118  }
119  else
120  {
121  WIZCHIP.CRIS._enter = cris_en;
122  WIZCHIP.CRIS._exit = cris_ex;
123  }
124 }
125 
126 void reg_wizchip_cs_cbfunc(void(*cs_sel)(void), void(*cs_desel)(void))
127 {
128  if(!cs_sel || !cs_desel)
129  {
130  WIZCHIP.CS._select = wizchip_cs_select;
131  WIZCHIP.CS._deselect = wizchip_cs_deselect;
132  }
133  else
134  {
135  WIZCHIP.CS._select = cs_sel;
136  WIZCHIP.CS._deselect = cs_desel;
137  }
138 }
139 
140 void reg_wizchip_bus_cbfunc(uint8_t(*bus_rb)(uint32_t addr), void (*bus_wb)(uint32_t addr, uint8_t wb))
141 {
142  while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_BUS_));
143 
144  if(!bus_rb || !bus_wb)
145  {
148  }
149  else
150  {
151  WIZCHIP.IF.BUS._read_byte = bus_rb;
152  WIZCHIP.IF.BUS._write_byte = bus_wb;
153  }
154 }
155 
156 void reg_wizchip_spi_cbfunc(uint8_t (*spi_rb)(void), void (*spi_wb)(uint8_t wb))
157 {
158  while(!(WIZCHIP.if_mode & _WIZCHIP_IO_MODE_SPI_));
159 
160  if(!spi_rb || !spi_wb)
161  {
164  }
165  else
166  {
167  WIZCHIP.IF.SPI._read_byte = spi_rb;
168  WIZCHIP.IF.SPI._write_byte = spi_wb;
169  }
170 }
171 
172 int8_t ctlwizchip(ctlwizchip_type cwtype, void* arg)
173 {
174  uint8_t tmp = 0;
175  uint8_t* ptmp[2] = {0,0};
176  switch(cwtype)
177  {
178  case CW_RESET_WIZCHIP:
180  break;
181  case CW_INIT_WIZCHIP:
182  if(arg != 0)
183  {
184  ptmp[0] = (uint8_t*)arg;
185  ptmp[1] = ptmp[0] + _WIZCHIP_SOCK_NUM_;
186  }
187  return wizchip_init(ptmp[0], ptmp[1]);;
188  case CW_CLR_INTERRUPT:
190  break;
191  case CW_GET_INTERRUPT:
192  *((intr_kind*)arg) = wizchip_getinterrupt();
193  break;
194  case CW_SET_INTRMASK:
196  break;
197  case CW_GET_INTRMASK:
198  *((intr_kind*)arg) = wizchip_getinterruptmask();
199  break;
200  #if _WIZCHIP_ > 5100
201  case CW_SET_INTRTIME:
202  setINTLEVEL(*(uint16_t*)arg);
203  break;
204  case CW_GET_INTRTIME:
205  *(uint16_t*)arg = getINTLEVEL();
206  break;
207  #endif
208  case CW_GET_ID:
209  ((uint8_t*)arg)[0] = WIZCHIP.id[0];
210  ((uint8_t*)arg)[1] = WIZCHIP.id[1];
211  ((uint8_t*)arg)[2] = WIZCHIP.id[2];
212  ((uint8_t*)arg)[3] = WIZCHIP.id[3];
213  ((uint8_t*)arg)[4] = WIZCHIP.id[4];
214  ((uint8_t*)arg)[5] = 0;
215  break;
216  #if _WIZCHIP_ == 5500
217  case CW_RESET_PHY:
218  wizphy_reset();
219  break;
220  case CW_SET_PHYCONF:
222  break;
223  case CW_GET_PHYCONF:
225  break;
226  case CW_GET_PHYSTATUS:
227  break;
228  case CW_SET_PHYPOWMODE:
229  return wizphy_setphypmode(*(uint8_t*)arg);
230  #endif
231  case CW_GET_PHYPOWMODE:
232  tmp = wizphy_getphypmode();
233  if((int8_t)tmp == -1) return -1;
234  *(uint8_t*)arg = tmp;
235  break;
236  case CW_GET_PHYLINK:
237  tmp = wizphy_getphylink();
238  if((int8_t)tmp == -1) return -1;
239  *(uint8_t*)arg = tmp;
240  break;
241  default:
242  return -1;
243  }
244  return 0;
245 }
246 
247 
248 int8_t ctlnetwork(ctlnetwork_type cntype, void* arg)
249 {
250 
251  switch(cntype)
252  {
253  case CN_SET_NETINFO:
255  break;
256  case CN_GET_NETINFO:
258  break;
259  case CN_SET_NETMODE:
260  return wizchip_setnetmode(*(netmode_type*)arg);
261  case CN_GET_NETMODE:
262  *(netmode_type*)arg = wizchip_getnetmode();
263  break;
264  case CN_SET_TIMEOUT:
266  break;
267  case CN_GET_TIMEOUT:
269  break;
270  default:
271  return -1;
272  }
273  return 0;
274 }
275 
277 {
278  uint8_t gw[4], sn[4], sip[4];
279  uint8_t mac[6];
280  getSHAR(mac);
281  getGAR(gw); getSUBR(sn); getSIPR(sip);
282  setMR(MR_RST);
283  getMR(); // for delay
284  setSHAR(mac);
285  setGAR(gw);
286  setSUBR(sn);
287  setSIPR(sip);
288 }
289 
290 int8_t wizchip_init(uint8_t* txsize, uint8_t* rxsize)
291 {
292  int8_t i;
293  int8_t tmp = 0;
295  if(txsize)
296  {
297  tmp = 0;
298  for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
299  tmp += txsize[i];
300  if(tmp > 16) return -1;
301  for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
302  setSn_TXBUF_SIZE(i, txsize[i]);
303  }
304  if(rxsize)
305  {
306  tmp = 0;
307  for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
308  tmp += rxsize[i];
309  if(tmp > 16) return -1;
310  for(i = 0 ; i < _WIZCHIP_SOCK_NUM_; i++)
311  setSn_RXBUF_SIZE(i, rxsize[i]);
312  }
313  return 0;
314 }
315 
317 {
318  uint8_t ir = (uint8_t)intr;
319  uint8_t sir = (uint8_t)((uint16_t)intr >> 8);
320 #if _WIZCHIP_ < 5500
321  ir |= (1<<4); // IK_WOL
322 #endif
323 #if _WIZCHIP_ == 5200
324  ir |= (1 << 6);
325 #endif
326 
327 #if _WIZCHIP_ < 5200
328  sir &= 0x0F;
329 #endif
330 
331 #if _WIZCHIP_ == 5100
332  ir |= sir;
333  setIR(ir);
334 #else
335  setIR(ir);
336  setSIR(sir);
337 #endif
338 }
339 
341 {
342  uint8_t ir = 0;
343  uint8_t sir = 0;
344  uint16_t ret = 0;
345 #if _WIZCHIP_ == 5100
346  ir = getIR();
347  sir = ir 0x0F;
348 #else
349  ir = getIR();
350  sir = getSIR();
351 #endif
352 
353 #if _WIZCHIP_ < 5500
354  ir &= ~(1<<4); // IK_WOL
355 #endif
356 #if _WIZCHIP_ == 5200
357  ir &= ~(1 << 6);
358 #endif
359  ret = sir;
360  ret = (ret << 8) + ir;
361  return (intr_kind)ret;
362 }
363 
365 {
366  uint8_t imr = (uint8_t)intr;
367  uint8_t simr = (uint8_t)((uint16_t)intr >> 8);
368 #if _WIZCHIP_ < 5500
369  imr &= ~(1<<4); // IK_WOL
370 #endif
371 #if _WIZCHIP_ == 5200
372  imr &= ~(1 << 6);
373 #endif
374 
375 #if _WIZCHIP_ < 5200
376  simr &= 0x0F;
377 #endif
378 
379 #if _WIZCHIP_ == 5100
380  imr |= simr;
381  setIMR(imr);
382 #else
383  setIMR(imr);
384  setSIMR(simr);
385 #endif
386 }
387 
389 {
390  uint8_t imr = 0;
391  uint8_t simr = 0;
392  uint16_t ret = 0;
393 #if _WIZCHIP_ == 5100
394  imr = getIMR();
395  simr = imr 0x0F;
396 #else
397  imr = getIMR();
398  simr = getSIMR();
399 #endif
400 
401 #if _WIZCHIP_ < 5500
402  imr &= ~(1<<4); // IK_WOL
403 #endif
404 #if _WIZCHIP_ == 5200
405  imr &= ~(1 << 6); // IK_DEST_UNREACH
406 #endif
407  ret = simr;
408  ret = (ret << 8) + imr;
409  return (intr_kind)ret;
410 }
411 
412 int8_t wizphy_getphylink(void)
413 {
414  int8_t tmp;
415 #if _WIZCHIP_ == 5200
416  if(getPHYSTATUS() & PHYSTATUS_LINK)
417  tmp = PHY_LINK_ON;
418  else
419  tmp = PHY_LINK_OFF;
420 #elif _WIZCHIP_ == 5500
421  if(getPHYCFGR() & PHYCFGR_LNK_ON)
422  tmp = PHY_LINK_ON;
423  else
424  tmp = PHY_LINK_OFF;
425 #else
426  tmp = -1;
427 #endif
428  return tmp;
429 }
430 
431 #if _WIZCHIP_ > 5100
432 
433 int8_t wizphy_getphypmode(void)
434 {
435  int8_t tmp = 0;
436  #if _WIZCHIP_ == 5200
437  if(getPHYSTATUS() & PHYSTATUS_POWERDOWN)
438  tmp = PHY_POWER_DOWN;
439  else
440  tmp = PHY_POWER_NORM;
441  #elif _WIZCHIP_ == 5500
442  if(getPHYCFGR() & PHYCFGR_OPMDC_PDOWN)
443  tmp = PHY_POWER_DOWN;
444  else
445  tmp = PHY_POWER_NORM;
446  #else
447  tmp = -1;
448  #endif
449  return tmp;
450 }
451 #endif
452 
453 #if _WIZCHIP_ == 5500
454 void wizphy_reset(void)
455 {
456  uint8_t tmp = getPHYCFGR();
457  tmp &= PHYCFGR_RST;
458  setPHYCFGR(tmp);
459  tmp = getPHYCFGR();
460  tmp |= ~PHYCFGR_RST;
461  setPHYCFGR(tmp);
462 }
463 
465 {
466  uint8_t tmp = 0;
467  if(phyconf->by == PHY_CONFBY_SW)
468  tmp |= PHYCFGR_OPMD;
469  else
470  tmp &= ~PHYCFGR_OPMD;
471  if(phyconf->mode == PHY_MODE_AUTONEGO)
472  tmp |= PHYCFGR_OPMDC_ALLA;
473  else
474  {
475  if(phyconf->duplex == PHY_DUPLEX_FULL)
476  {
477  if(phyconf->speed == PHY_SPEED_100)
478  tmp |= PHYCFGR_OPMDC_100F;
479  else
480  tmp |= PHYCFGR_OPMDC_10F;
481  }
482  else
483  {
484  if(phyconf->speed == PHY_SPEED_100)
485  tmp |= PHYCFGR_OPMDC_100H;
486  else
487  tmp |= PHYCFGR_OPMDC_10H;
488  }
489  }
490  setPHYCFGR(tmp);
491  wizphy_reset();
492 }
493 
495 {
496  uint8_t tmp = 0;
497  tmp = getPHYCFGR();
498  phyconf->by = (tmp & PHYCFGR_OPMD) ? PHY_CONFBY_SW : PHY_CONFBY_HW;
499  switch(tmp & PHYCFGR_OPMDC_ALLA)
500  {
501  case PHYCFGR_OPMDC_ALLA:
502  case PHYCFGR_OPMDC_100FA:
503  phyconf->mode = PHY_MODE_AUTONEGO;
504  break;
505  default:
506  phyconf->mode = PHY_MODE_MANUAL;
507  break;
508  }
509  switch(tmp & PHYCFGR_OPMDC_ALLA)
510  {
511  case PHYCFGR_OPMDC_100FA:
512  case PHYCFGR_OPMDC_100F:
513  case PHYCFGR_OPMDC_100H:
514  phyconf->speed = PHY_SPEED_100;
515  break;
516  default:
517  phyconf->speed = PHY_SPEED_10;
518  break;
519  }
520  switch(tmp & PHYCFGR_OPMDC_ALLA)
521  {
522  case PHYCFGR_OPMDC_100FA:
523  case PHYCFGR_OPMDC_100F:
524  case PHYCFGR_OPMDC_10F:
525  phyconf->duplex = PHY_DUPLEX_FULL;
526  break;
527  default:
528  phyconf->duplex = PHY_DUPLEX_HALF;
529  break;
530  }
531 }
532 
534 {
535  uint8_t tmp = getPHYCFGR();
536  phyconf->duplex = (tmp & PHYCFGR_DPX_FULL) ? PHY_DUPLEX_FULL : PHY_DUPLEX_HALF;
537  phyconf->speed = (tmp & PHYCFGR_SPD_100) ? PHY_SPEED_100 : PHY_SPEED_10;
538 }
539 
540 int8_t wizphy_setphypmode(uint8_t pmode)
541 {
542  uint8_t tmp = 0;
543  tmp = getPHYCFGR();
544  if((tmp & PHYCFGR_OPMD)== 0) return -1;
545  tmp &= ~PHYCFGR_OPMDC_ALLA;
546  if( pmode == PHY_POWER_DOWN)
547  tmp |= PHYCFGR_OPMDC_PDOWN;
548  else
549  tmp |= PHYCFGR_OPMDC_ALLA;
550  setPHYCFGR(tmp);
551  wizphy_reset();
552  tmp = getPHYCFGR();
553  if( pmode == PHY_POWER_DOWN)
554  {
555  if(tmp & PHYCFGR_OPMDC_PDOWN) return 0;
556  }
557  else
558  {
559  if(tmp & PHYCFGR_OPMDC_ALLA) return 0;
560  }
561  return -1;
562 }
563 #endif
564 
565 
567 {
568  setSHAR(pnetinfo->mac);
569  setGAR(pnetinfo->gw);
570  setSUBR(pnetinfo->sn);
571  setSIPR(pnetinfo->ip);
572  _DNS_[0] = pnetinfo->dns[0];
573  _DNS_[1] = pnetinfo->dns[1];
574  _DNS_[2] = pnetinfo->dns[2];
575  _DNS_[3] = pnetinfo->dns[3];
576  _DHCP_ = pnetinfo->dhcp;
577 }
578 
580 {
581  getSHAR(pnetinfo->mac);
582  getGAR(pnetinfo->gw);
583  getSUBR(pnetinfo->sn);
584  getSIPR(pnetinfo->ip);
585  pnetinfo->dns[0]= _DNS_[0];
586  pnetinfo->dns[1]= _DNS_[1];
587  pnetinfo->dns[2]= _DNS_[2];
588  pnetinfo->dns[3]= _DNS_[3];
589  pnetinfo->dhcp = _DHCP_;
590 }
591 
593 {
594  uint8_t tmp = 0;
595 #if _WIZCHIP_ != 5500
596  if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK)) return -1;
597 #else
598  if(netmode & ~(NM_WAKEONLAN | NM_PPPOE | NM_PINGBLOCK | NM_FORCEARP)) return -1;
599 #endif
600  tmp = getMR();
601  tmp |= (uint8_t)netmode;
602  setMR(tmp);
603  return 0;
604 }
605 
607 {
608  return (netmode_type) getMR();
609 }
610 
612 {
613  setRCR(nettime->retry_cnt);
614  setRTR(nettime->time_100us);
615 }
616 
618 {
619  nettime->retry_cnt = getRCR();
620  nettime->time_100us = getRTR();
621 }