1
0

WIZ5500.C 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /*
  2. * Wiznet5500 library
  3. * Contains functions to allow for networking
  4. *
  5. * Functions from this library can be used to operate up to 8 sockets
  6. * Application based code should not be in this library
  7. */
  8. // uses stdlib.c
  9. // Wiznet W5500 Op Codes
  10. #define WIZNET_WRITE_COMMON 0x04 //opcode to write to one of the common block of registers
  11. #define WIZNET_READ_COMMON 0x00 //opcode to read one of the common block of registers
  12. #define WIZNET_WRITE_SnR 0x0C // s<<5 (nnn 01 1 00) opcode to write to one of the socket n registers
  13. #define WIZNET_READ_SnR 0x08 // s<<5 (nnn 01 0 00) opcode to read one of the socket n registers
  14. #define WIZNET_WRITE_SnTX 0x14 // s<<5 (nnn 10 1 00) opcode to write to the socket n transmit buffer
  15. #define WIZNET_READ_SnRX 0x18 // s<<5 (nnn 11 0 00) opcode to read from the socket n receive buffer
  16. // Wiznet W5500 Register Addresses
  17. #define WIZNET_MR 0x0000 // Mode
  18. #define WIZNET_GAR 0x0001 // Gateway IP address
  19. #define WIZNET_SUBR 0x0005 // Subnet mask address
  20. #define WIZNET_SHAR 0x0009 // Source MAC address
  21. #define WIZNET_SIPR 0x000F // Source IP address
  22. #define WIZNET_IR 0x0015 // Interrupt
  23. #define WIZNET_IMR 0x0016 // Interrupt Mask
  24. #define WIZNET_RTR 0x0019 // Timeout address
  25. #define WIZNET_RCR 0x001B // Retry count
  26. #define WIZNET_UIPR 0x0028 // Unreachable IP address in UDP mode
  27. #define WIZNET_UPORT 0x002C // Unreachable Port address in UDP mode
  28. //W5500 Socket Registers follow
  29. #define WIZNET_SnMR 0x0000 // Mode
  30. #define WIZNET_SnCR 0x0001 // Command
  31. #define WIZNET_SnIR 0x0002 // Interrupt
  32. #define WIZNET_SnSR 0x0003 // Status
  33. #define WIZNET_SnPORT 0x0004 // Source Port (2 bytes)
  34. #define WIZNET_SnDHAR 0x0006 // Destination Hardw Addr
  35. #define WIZNET_SnDIPR 0x000C // Destination IP Addr
  36. #define WIZNET_SnDPORT 0x0010 // Destination Port
  37. #define WIZNET_SnMSSR 0x0012 // Max Segment Size
  38. #define WIZNET_SnPROTO 0x0014 // Protocol in IP RAW Mode
  39. #define WIZNET_SnTOS 0x0015 // IP TOS
  40. #define WIZNET_SnTTL 0x0016 // IP TTL
  41. #define WIZNET_SnRX_BSZ 0x001E // RX Buffer Size
  42. #define WIZNET_SnTX_BSZ 0x001F // TX Buffer Size
  43. #define WIZNET_SnTX_FSR 0x0020 // TX Free Size
  44. #define WIZNET_SnTX_RD 0x0022 // TX Read Pointer
  45. #define WIZNET_SnTX_WR 0x0024 // TX Write Pointer
  46. #define WIZNET_SnRX_RSR 0x0026 // RX RECEIVED SIZE REGISTER
  47. #define WIZNET_SnRX_RD 0x0028 // RX Read Pointer
  48. #define WIZNET_SnRX_WR 0x002A // RX Write Pointer (supported?
  49. //Socket n Mode Register (0x0000)
  50. //WIZNET_SnMR
  51. #define WIZNET_MR_CLOSE 0x00 // Unused socket
  52. #define WIZNET_MR_TCP 0x01 // TCP
  53. #define WIZNET_MR_UDP 0x02 // UDP
  54. #define WIZNET_MR_IPRAW 0x03 // IP LAYER RAW SOCK
  55. #define WIZNET_MR_MACRAW 0x04 // MAC LAYER RAW SOCK
  56. #define WIZNET_MR_PPPOE 0x05 // PPPoE
  57. #define WIZNET_MR_ND 0x20 // No Delayed Ack(TCP) flag
  58. #define WIZNET_MR_MULTI 0x80 // support multicating
  59. //Socket n Command Register (0x0001)
  60. //WIZNET_SnCR
  61. #define WIZNET_CR_OPEN 0x01 // Initialize or open socket
  62. #define WIZNET_CR_LISTEN 0x02 // Wait connection request in tcp mode(Server mode)
  63. #define WIZNET_CR_CONNECT 0x04 // Send connection request in tcp mode(Client mode)
  64. #define WIZNET_CR_DISCON 0x08 // Send closing reqeuset in tcp mode
  65. #define WIZNET_CR_CLOSE 0x10 // Close socket
  66. #define WIZNET_CR_SEND 0x20 // Update Tx memory pointer and send data
  67. #define WIZNET_CR_SEND_MAC 0x21 // Send data with MAC address, so without ARP process
  68. #define WIZNET_CR_SEND_KEEP 0x22 // Send keep alive message
  69. #define WIZNET_CR_RECV 0x40 // Update Rx memory buffer pointer and receive data
  70. //Socket n Interrupt Register (0x0002)
  71. //WIZNET_SnIR
  72. // Bit 0: CON
  73. // Bit 1: DISCON
  74. // Bit 2: RECV
  75. // Bit 3: TIMEOUT
  76. // Bit 4: SEND_OK
  77. //Socket n Status Register (0x0003)
  78. //WIZNET_SnSR
  79. #define WIZNET_SOCK_CLOSED 0x00 // Closed
  80. #define WIZNET_SOCK_INIT 0x13 // Init state
  81. #define WIZNET_SOCK_LISTEN 0x14 // Listen state
  82. #define WIZNET_SOCK_SYNSENT 0x15 // Connection state
  83. #define WIZNET_SOCK_SYNRECV 0x16 // Connection state
  84. #define WIZNET_SOCK_ESTABLISHED 0x17 // Success to connect
  85. #define WIZNET_SOCK_FIN_WAIT 0x18 // Closing state
  86. #define WIZNET_SOCK_CLOSING 0x1A // Closing state
  87. #define WIZNET_SOCK_TIME_WAIT 0x1B // Closing state
  88. #define WIZNET_SOCK_CLOSE_WAIT 0x1C // Closing state
  89. #define WIZNET_SOCK_LAST_ACK 0x1D // Closing state
  90. #define WIZNET_SOCK_UDP 0x22 // UDP socket
  91. #define WIZNET_SOCK_IPRAW 0x32 // IP raw mode socket
  92. #define WIZNET_SOCK_MACRAW 0x42 // MAC raw mode socket
  93. #define WIZNET_SOCK_PPPOE 0x5F // PPPOE socket
  94. //Socket n Source Port Register (0x0004, 0x0005)
  95. //WIZNET_SnPORT
  96. // MSByte: 0x0004
  97. // LSByte: 0x0005
  98. #define WIZNET_MAX_RBUF 2048 // buffer for receiving data (max rx packet size!)
  99. #define WIZNET_MAX_TBUF 2048 // buffer for sending data (max tx packet size!)
  100. //-------------------
  101. //BASIC READ AND WRITE FUNCTIONS
  102. //-------------------
  103. // Sets SPI3_CS low
  104. void WizSpiBeginTransfer()
  105. {
  106. asm(
  107. "; backup regs\n"
  108. "push r1\n"
  109. "push r2\n"
  110. "load32 0xC02732 r2 ; r2 = 0xC02732\n"
  111. "load 0 r1 ; r1 = 0 (enable)\n"
  112. "write 0 r2 r1 ; write to SPI3_CS\n"
  113. "; restore regs\n"
  114. "pop r2\n"
  115. "pop r1\n"
  116. );
  117. }
  118. // Sets SPI3_CS high
  119. void WizSpiEndTransfer()
  120. {
  121. asm(
  122. "; backup regs\n"
  123. "push r1\n"
  124. "push r2\n"
  125. "load32 0xC02732 r2 ; r2 = 0xC02732\n"
  126. "load 1 r1 ; r1 = 1 (disable)\n"
  127. "write 0 r2 r1 ; write to SPI3_CS\n"
  128. "; restore regs\n"
  129. "pop r2\n"
  130. "pop r1\n"
  131. );
  132. }
  133. // write dataByte and return read value
  134. // write 0x00 for a read
  135. // Writes byte over SPI3 to W5500
  136. word WizSpiTransfer(word dataByte)
  137. {
  138. word retval = 0;
  139. asm(
  140. "load32 0xC02731 r2 ; r2 = 0xC02731\n"
  141. "write 0 r2 r4 ; write r4 over SPI3\n"
  142. "read 0 r2 r2 ; read return value\n"
  143. "write -4 r14 r2 ; write to stack to return\n"
  144. );
  145. return retval;
  146. }
  147. // Write data to W5500
  148. void wizWrite(word addr, word cb, char* buf, word len)
  149. {
  150. WizSpiBeginTransfer();
  151. // Send address
  152. word addrMSB = addr >> 8;
  153. WizSpiTransfer(addrMSB); //msByte
  154. WizSpiTransfer(addr); //lsByte
  155. // Send control byte
  156. WizSpiTransfer(cb);
  157. // Send data
  158. word i;
  159. for (i = 0; i < len; i++)
  160. {
  161. WizSpiTransfer(buf[i]);
  162. }
  163. WizSpiEndTransfer();
  164. }
  165. // Write single byte to W5500
  166. word wizWriteSingle(word addr, word cb, word data)
  167. {
  168. WizSpiBeginTransfer();
  169. // Send address
  170. word addrMSB = addr >> 8;
  171. WizSpiTransfer(addrMSB); //msByte
  172. WizSpiTransfer(addr); //lsByte
  173. // Send control byte
  174. WizSpiTransfer(cb);
  175. // Send data
  176. WizSpiTransfer(data);
  177. WizSpiEndTransfer();
  178. return data;
  179. }
  180. // Write two bytes to W5500
  181. void wizWriteDouble(word addr, word cb, word data)
  182. {
  183. WizSpiBeginTransfer();
  184. // Send address
  185. word addrMSB = addr >> 8;
  186. WizSpiTransfer(addrMSB); //msByte
  187. WizSpiTransfer(addr); //lsByte
  188. // Send control byte
  189. WizSpiTransfer(cb);
  190. // Send data
  191. word dataMSB = data >> 8;
  192. WizSpiTransfer(dataMSB);
  193. WizSpiTransfer(data);
  194. WizSpiEndTransfer();
  195. }
  196. void wizRead(word addr, word cb, char* buf, word len)
  197. {
  198. WizSpiBeginTransfer();
  199. // Send address
  200. word addrMSB = addr >> 8;
  201. WizSpiTransfer(addrMSB); //msByte
  202. WizSpiTransfer(addr); //lsByte
  203. // Send control byte
  204. WizSpiTransfer(cb);
  205. // Read data
  206. word i;
  207. for (i = 0; i < len; i++)
  208. {
  209. buf[i] = WizSpiTransfer(0);
  210. }
  211. WizSpiEndTransfer();
  212. }
  213. word wizReadSingle(word addr, word cb)
  214. {
  215. WizSpiBeginTransfer();
  216. // Send address
  217. word addrMSB = addr >> 8;
  218. WizSpiTransfer(addrMSB); //msByte
  219. WizSpiTransfer(addr); //lsByte
  220. // Send control byte
  221. WizSpiTransfer(cb);
  222. // Read data
  223. word retval = WizSpiTransfer(0);
  224. WizSpiEndTransfer();
  225. // Return read value
  226. return retval;
  227. }
  228. word wizReadDouble(word addr, word cb)
  229. {
  230. WizSpiBeginTransfer();
  231. // Send address
  232. word addrMSB = addr >> 8;
  233. WizSpiTransfer(addrMSB); //msByte
  234. WizSpiTransfer(addr); //lsByte
  235. // Send control byte
  236. WizSpiTransfer(cb);
  237. // Read data
  238. word retval = WizSpiTransfer(0) << 8;
  239. retval = retval + WizSpiTransfer(0);
  240. WizSpiEndTransfer();
  241. // Return read value
  242. return retval;
  243. }
  244. //-------------------
  245. //W5500 SOCKET REGISTER FUNCTIONS
  246. //-------------------
  247. // Send a command cmd to socket s
  248. void wizCmd(word s, word cmd)
  249. {
  250. //wizWriteSingle(WIZNET_SnCR, WIZNET_WRITE_SnR, cmd);
  251. WizSpiBeginTransfer();
  252. WizSpiTransfer(0); //msByte
  253. WizSpiTransfer(WIZNET_SnCR); //lsByte
  254. WizSpiTransfer(WIZNET_WRITE_SnR + (s << 5));
  255. WizSpiTransfer(cmd);
  256. WizSpiEndTransfer();
  257. // wait untill done
  258. while ( wizReadSingle(WIZNET_SnCR, WIZNET_READ_SnR) );
  259. }
  260. // Write 8 bits to a sockets control register
  261. void wizSetSockReg8(word s, word addr, word val)
  262. {
  263. //wizWriteSingle(addr, WIZNET_WRITE_SnR, val);
  264. WizSpiBeginTransfer();
  265. WizSpiTransfer(0); //msByte
  266. WizSpiTransfer(addr); //lsByte
  267. WizSpiTransfer(WIZNET_WRITE_SnR + (s << 5));
  268. WizSpiTransfer(val);
  269. WizSpiEndTransfer();
  270. }
  271. // Read 8 bits from a sockets control register
  272. word wizGetSockReg8(word s, word addr){
  273. //return wizReadSingle(addr, WIZNET_READ_SnR);
  274. WizSpiBeginTransfer();
  275. // Send address
  276. word addrMSB = addr >> 8;
  277. WizSpiTransfer(addrMSB); //msByte
  278. WizSpiTransfer(addr); //lsByte
  279. word cb = WIZNET_READ_SnR + (s << 5);
  280. // Send control byte
  281. WizSpiTransfer(cb);
  282. // Read data
  283. word retval = WizSpiTransfer(0);
  284. WizSpiEndTransfer();
  285. // Return read value
  286. return retval;
  287. }
  288. // Write 16 bits to a sockets control register
  289. void wizSetSockReg16(word s, word addr, word val)
  290. {
  291. //wizWriteDouble(addr, WIZNET_WRITE_SnR + (s << 5), val);
  292. WizSpiBeginTransfer();
  293. WizSpiTransfer(0); //msByte
  294. WizSpiTransfer(addr); //lsByte
  295. WizSpiTransfer(WIZNET_WRITE_SnR + (s << 5));
  296. word valMSB = val >> 8;
  297. WizSpiTransfer(valMSB);
  298. WizSpiTransfer(val);
  299. WizSpiEndTransfer();
  300. }
  301. // Read 16 bits from a sockets control register
  302. word wizGetSockReg16(word s, word addr)
  303. {
  304. //return wizReadDouble(addr, WIZNET_READ_SnR + (s << 5));
  305. WizSpiBeginTransfer();
  306. // Send address
  307. word addrMSB = addr >> 8;
  308. WizSpiTransfer(addrMSB); //msByte
  309. WizSpiTransfer(addr); //lsByte
  310. word cb = WIZNET_READ_SnR + (s << 5);
  311. // Send control byte
  312. WizSpiTransfer(cb);
  313. // Read data
  314. word retval = WizSpiTransfer(0) << 8;
  315. retval = retval + WizSpiTransfer(0);
  316. WizSpiEndTransfer();
  317. // Return read value
  318. return retval;
  319. }
  320. //-------------------
  321. //W5500 SETUP FUNCTIONS
  322. //-------------------
  323. // Initialize W5500 chip
  324. void wiz_Init(char* ip_addr, char* gateway_addr, char* mac_addr, char* sub_mask)
  325. {
  326. WizSpiEndTransfer();
  327. delay(10);
  328. wizWrite(WIZNET_SIPR, WIZNET_WRITE_COMMON, ip_addr, 4);
  329. wizWrite(WIZNET_GAR, WIZNET_WRITE_COMMON, gateway_addr, 4);
  330. wizWrite(WIZNET_SHAR, WIZNET_WRITE_COMMON, mac_addr, 6);
  331. wizWrite(WIZNET_SUBR, WIZNET_WRITE_COMMON, sub_mask, 4);
  332. }
  333. // Initialize socket s for TCP
  334. void wizInitSocketTCP(word s, word port)
  335. {
  336. wizCmd(s, WIZNET_CR_CLOSE);
  337. wizSetSockReg8 (s, WIZNET_SnIR, 0xFF); //reset interrupt register
  338. wizSetSockReg8 (s, WIZNET_SnMR, WIZNET_MR_TCP); //set mode register to tcp
  339. wizSetSockReg16 (s, WIZNET_SnPORT, port); //set tcp port
  340. wizCmd(s, WIZNET_CR_OPEN);
  341. wizCmd(s, WIZNET_CR_LISTEN);
  342. delay(10); //wait a bit to make sure the socket is in the correct state (technically not necessary)
  343. }
  344. // Initialize socket s for TCP client
  345. void wizInitSocketTCPClient(word s, word port)
  346. {
  347. wizCmd(s, WIZNET_CR_CLOSE);
  348. wizSetSockReg8 (s, WIZNET_SnIR, 0xFF); //reset interrupt register
  349. wizSetSockReg8 (s, WIZNET_SnMR, WIZNET_MR_TCP); //set mode register to tcp
  350. wizSetSockReg16 (s, WIZNET_SnPORT, port); //set tcp port
  351. wizCmd(s, WIZNET_CR_OPEN);
  352. delay(10); //wait a bit to make sure the socket is in the correct state (technically not necessary)
  353. }
  354. //-------------------
  355. //W5500 READING AND WRITING FUNCTIONS
  356. //-------------------
  357. // from memory, so no need for unsigned comparisons (because we have less than 2GB RAM)
  358. word wizWriteDataFromMemory(word s, char* buf, word buflen)
  359. {
  360. // Make sure there is something to send
  361. if (buflen <= 0)
  362. {
  363. return 0;
  364. }
  365. word bytesSent = 0;
  366. // loop until all bytes are sent
  367. while (bytesSent != buflen)
  368. {
  369. if (wizGetSockReg8(s, WIZNET_SnSR) == WIZNET_SOCK_CLOSED)
  370. {
  371. //uprintln("connection closed");
  372. return 0;
  373. }
  374. word partToSend = buflen - bytesSent;
  375. if (partToSend > WIZNET_MAX_TBUF)
  376. partToSend = WIZNET_MAX_TBUF;
  377. // Make sure there is room in the transmit buffer for what we want to send
  378. word txfree = wizGetSockReg16(s, WIZNET_SnTX_FSR); // Size of the available buffer area
  379. word timeout = 0;
  380. while (txfree < partToSend)
  381. {
  382. timeout++; // Increase timeout counter
  383. delay(1); // Wait a bit
  384. txfree = wizGetSockReg16(s, WIZNET_SnTX_FSR); // Size of the available buffer area
  385. // After a second
  386. if (timeout > 1000)
  387. {
  388. wizCmd(s, WIZNET_CR_DISCON); // Disconnect the connection
  389. //uprintln("timeout");
  390. return 0;
  391. }
  392. }
  393. // Space is available so we will send the buffer
  394. word txwr = wizGetSockReg16(s, WIZNET_SnTX_WR); // Read the Tx Write Pointer
  395. // Write the outgoing data to the transmit buffer
  396. wizWrite(txwr, WIZNET_WRITE_SnTX + (s << 5), buf + bytesSent, partToSend);
  397. // update the buffer pointer
  398. word newSize = txwr + partToSend;
  399. wizSetSockReg16(s, WIZNET_SnTX_WR, newSize);
  400. // Now Send the SEND command which tells the wiznet the pointer is updated
  401. wizCmd(s, WIZNET_CR_SEND);
  402. // Update the amount of bytes sent
  403. bytesSent += partToSend;
  404. }
  405. return 1;
  406. }
  407. // Read received data
  408. word wizReadRecvData(word s, char* buf, word buflen)
  409. {
  410. if (buflen == 0)
  411. {
  412. return 1;
  413. }
  414. if (buflen > WIZNET_MAX_RBUF) // If the request size > WIZNET_MAX_RBUF, truncate it to prevent overflow
  415. {
  416. //uprintln("W: Received too large TCP data");
  417. buflen = WIZNET_MAX_RBUF; // - 1; // -1 Because room for 0 terminator
  418. }
  419. // Get the address where the wiznet is holding the data
  420. word rxrd = wizGetSockReg16(s, WIZNET_SnRX_RD);
  421. // Read the data into the buffer
  422. wizRead(rxrd, WIZNET_READ_SnRX + (s << 5), buf, buflen);
  423. // Remove read data from rxbuffer to make space for new data
  424. word nsize = rxrd + buflen;
  425. wizSetSockReg16(s, WIZNET_SnRX_RD, nsize); //replace read data pointer
  426. //tell the wiznet we have retrieved the data
  427. wizCmd(s, WIZNET_CR_RECV);
  428. // Terminate buffer for printing in case the data was a string
  429. *(buf + buflen) = 0;
  430. return 1;
  431. }
  432. // Remove the received data
  433. void wizFlush(word s, word rsize)
  434. {
  435. if (rsize > 0)
  436. {
  437. word rxrd = wizGetSockReg16(s, WIZNET_SnRX_RD); //retrieve read data pointer
  438. word nsize = rxrd + rsize;
  439. wizSetSockReg16(s, WIZNET_SnRX_RD, nsize); //replace read data pointer
  440. //tell the wiznet we have retrieved the data
  441. wizCmd(s, WIZNET_CR_RECV);
  442. }
  443. }