w5500.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //*****************************************************************************
  2. //
  3. //! \file w5500.c
  4. //! \brief W5500 HAL Interface.
  5. //! \version 1.0.2
  6. //! \date 2013/10/21
  7. //! \par Revision history
  8. //! <2015/02/05> Notice
  9. //! The version history is not updated after this point.
  10. //! Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
  11. //! >> https://github.com/Wiznet/ioLibrary_Driver
  12. //! <2014/05/01> V1.0.2
  13. //! 1. Implicit type casting -> Explicit type casting. Refer to M20140501
  14. //! Fixed the problem on porting into under 32bit MCU
  15. //! Issued by Mathias ClauBen, wizwiki forum ID Think01 and bobh
  16. //! Thank for your interesting and serious advices.
  17. //! <2013/12/20> V1.0.1
  18. //! 1. Remove warning
  19. //! 2. WIZCHIP_READ_BUF WIZCHIP_WRITE_BUF in case _WIZCHIP_IO_MODE_SPI_FDM_
  20. //! for loop optimized(removed). refer to M20131220
  21. //! <2013/10/21> 1st Release
  22. //! \author MidnightCow
  23. //! \copyright
  24. //!
  25. //! Copyright (c) 2013, WIZnet Co., LTD.
  26. //! All rights reserved.
  27. //!
  28. //! Redistribution and use in source and binary forms, with or without
  29. //! modification, are permitted provided that the following conditions
  30. //! are met:
  31. //!
  32. //! * Redistributions of source code must retain the above copyright
  33. //! notice, this list of conditions and the following disclaimer.
  34. //! * Redistributions in binary form must reproduce the above copyright
  35. //! notice, this list of conditions and the following disclaimer in the
  36. //! documentation and/or other materials provided with the distribution.
  37. //! * Neither the name of the <ORGANIZATION> nor the names of its
  38. //! contributors may be used to endorse or promote products derived
  39. //! from this software without specific prior written permission.
  40. //!
  41. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  42. //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  45. //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  46. //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  47. //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  48. //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49. //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  51. //! THE POSSIBILITY OF SUCH DAMAGE.
  52. //
  53. //*****************************************************************************
  54. //#include <stdio.h>
  55. #include "w5500.h"
  56. #define _W5500_SPI_VDM_OP_ 0x00
  57. #define _W5500_SPI_FDM_OP_LEN1_ 0x01
  58. #define _W5500_SPI_FDM_OP_LEN2_ 0x02
  59. #define _W5500_SPI_FDM_OP_LEN4_ 0x03
  60. #if (_WIZCHIP_ == 5500)
  61. ////////////////////////////////////////////////////
  62. uint8_t WIZCHIP_READ(uint32_t AddrSel)
  63. {
  64. uint8_t ret;
  65. uint8_t spi_data[3];
  66. WIZCHIP_CRITICAL_ENTER();
  67. WIZCHIP.CS._select();
  68. AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
  69. if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte operation
  70. {
  71. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
  72. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
  73. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
  74. }
  75. else // burst operation
  76. {
  77. spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
  78. spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
  79. spi_data[2] = (AddrSel & 0x000000FF) >> 0;
  80. WIZCHIP.IF.SPI._write_burst(spi_data, 3);
  81. }
  82. ret = WIZCHIP.IF.SPI._read_byte();
  83. WIZCHIP.CS._deselect();
  84. WIZCHIP_CRITICAL_EXIT();
  85. return ret;
  86. }
  87. void WIZCHIP_WRITE(uint32_t AddrSel, uint8_t wb )
  88. {
  89. uint8_t spi_data[4];
  90. WIZCHIP_CRITICAL_ENTER();
  91. WIZCHIP.CS._select();
  92. AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
  93. //if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte operation
  94. if(!WIZCHIP.IF.SPI._write_burst) // byte operation
  95. {
  96. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
  97. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
  98. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
  99. WIZCHIP.IF.SPI._write_byte(wb);
  100. }
  101. else // burst operation
  102. {
  103. spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
  104. spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
  105. spi_data[2] = (AddrSel & 0x000000FF) >> 0;
  106. spi_data[3] = wb;
  107. WIZCHIP.IF.SPI._write_burst(spi_data, 4);
  108. }
  109. WIZCHIP.CS._deselect();
  110. WIZCHIP_CRITICAL_EXIT();
  111. }
  112. void WIZCHIP_READ_BUF (uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
  113. {
  114. uint8_t spi_data[3];
  115. uint16_t i;
  116. WIZCHIP_CRITICAL_ENTER();
  117. WIZCHIP.CS._select();
  118. AddrSel |= (_W5500_SPI_READ_ | _W5500_SPI_VDM_OP_);
  119. if(!WIZCHIP.IF.SPI._read_burst || !WIZCHIP.IF.SPI._write_burst) // byte operation
  120. {
  121. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
  122. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
  123. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
  124. for(i = 0; i < len; i++)
  125. pBuf[i] = WIZCHIP.IF.SPI._read_byte();
  126. }
  127. else // burst operation
  128. {
  129. spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
  130. spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
  131. spi_data[2] = (AddrSel & 0x000000FF) >> 0;
  132. WIZCHIP.IF.SPI._write_burst(spi_data, 3);
  133. WIZCHIP.IF.SPI._read_burst(pBuf, len);
  134. }
  135. WIZCHIP.CS._deselect();
  136. WIZCHIP_CRITICAL_EXIT();
  137. }
  138. void WIZCHIP_WRITE_BUF(uint32_t AddrSel, uint8_t* pBuf, uint16_t len)
  139. {
  140. uint8_t spi_data[3];
  141. uint16_t i;
  142. WIZCHIP_CRITICAL_ENTER();
  143. WIZCHIP.CS._select();
  144. AddrSel |= (_W5500_SPI_WRITE_ | _W5500_SPI_VDM_OP_);
  145. if(!WIZCHIP.IF.SPI._write_burst) // byte operation
  146. {
  147. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x00FF0000) >> 16);
  148. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x0000FF00) >> 8);
  149. WIZCHIP.IF.SPI._write_byte((AddrSel & 0x000000FF) >> 0);
  150. for(i = 0; i < len; i++)
  151. WIZCHIP.IF.SPI._write_byte(pBuf[i]);
  152. }
  153. else // burst operation
  154. {
  155. spi_data[0] = (AddrSel & 0x00FF0000) >> 16;
  156. spi_data[1] = (AddrSel & 0x0000FF00) >> 8;
  157. spi_data[2] = (AddrSel & 0x000000FF) >> 0;
  158. WIZCHIP.IF.SPI._write_burst(spi_data, 3);
  159. WIZCHIP.IF.SPI._write_burst(pBuf, len);
  160. }
  161. WIZCHIP.CS._deselect();
  162. WIZCHIP_CRITICAL_EXIT();
  163. }
  164. uint16_t getSn_TX_FSR(uint8_t sn)
  165. {
  166. uint16_t val=0,val1=0;
  167. do
  168. {
  169. val1 = WIZCHIP_READ(Sn_TX_FSR(sn));
  170. val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
  171. if (val1 != 0)
  172. {
  173. val = WIZCHIP_READ(Sn_TX_FSR(sn));
  174. val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_TX_FSR(sn),1));
  175. }
  176. }while (val != val1);
  177. return val;
  178. }
  179. uint16_t getSn_RX_RSR(uint8_t sn)
  180. {
  181. uint16_t val=0,val1=0;
  182. do
  183. {
  184. val1 = WIZCHIP_READ(Sn_RX_RSR(sn));
  185. val1 = (val1 << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
  186. if (val1 != 0)
  187. {
  188. val = WIZCHIP_READ(Sn_RX_RSR(sn));
  189. val = (val << 8) + WIZCHIP_READ(WIZCHIP_OFFSET_INC(Sn_RX_RSR(sn),1));
  190. }
  191. }while (val != val1);
  192. return val;
  193. }
  194. void wiz_send_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
  195. {
  196. uint16_t ptr = 0;
  197. uint32_t addrsel = 0;
  198. if(len == 0) return;
  199. ptr = getSn_TX_WR(sn);
  200. //M20140501 : implict type casting -> explict type casting
  201. //addrsel = (ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
  202. addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_TXBUF_BLOCK(sn) << 3);
  203. //
  204. WIZCHIP_WRITE_BUF(addrsel,wizdata, len);
  205. ptr += len;
  206. setSn_TX_WR(sn,ptr);
  207. }
  208. void wiz_recv_data(uint8_t sn, uint8_t *wizdata, uint16_t len)
  209. {
  210. uint16_t ptr = 0;
  211. uint32_t addrsel = 0;
  212. if(len == 0) return;
  213. ptr = getSn_RX_RD(sn);
  214. //M20140501 : implict type casting -> explict type casting
  215. //addrsel = ((ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
  216. addrsel = ((uint32_t)ptr << 8) + (WIZCHIP_RXBUF_BLOCK(sn) << 3);
  217. //
  218. WIZCHIP_READ_BUF(addrsel, wizdata, len);
  219. ptr += len;
  220. setSn_RX_RD(sn,ptr);
  221. }
  222. void wiz_recv_ignore(uint8_t sn, uint16_t len)
  223. {
  224. uint16_t ptr = 0;
  225. ptr = getSn_RX_RD(sn);
  226. ptr += len;
  227. setSn_RX_RD(sn,ptr);
  228. }
  229. #endif