1
0

wiz5500.c 15 KB

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