driver_w5500.c 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "mip.h"
  2. #if MG_ENABLE_MIP
  3. enum { W5500_CR = 0, W5500_S0 = 1, W5500_TX0 = 2, W5500_RX0 = 3 };
  4. static void w5500_txn(struct mip_spi *s, uint8_t block, uint16_t addr, bool wr,
  5. void *buf, size_t len) {
  6. uint8_t *p = buf, cmd[] = {(uint8_t) (addr >> 8), (uint8_t) (addr & 255),
  7. (uint8_t) ((block << 3) | (wr ? 4 : 0))};
  8. s->begin(s->spi);
  9. for (size_t i = 0; i < sizeof(cmd); i++) s->txn(s->spi, cmd[i]);
  10. for (size_t i = 0; i < len; i++) {
  11. uint8_t r = s->txn(s->spi, p[i]);
  12. if (!wr) p[i] = r;
  13. }
  14. s->end(s->spi);
  15. }
  16. // clang-format off
  17. static void w5500_wn(struct mip_spi *s, uint8_t block, uint16_t addr, void *buf, size_t len) { w5500_txn(s, block, addr, true, buf, len); }
  18. static void w5500_w1(struct mip_spi *s, uint8_t block, uint16_t addr, uint8_t val) { w5500_wn(s, block, addr, &val, 1); }
  19. static void w5500_w2(struct mip_spi *s, uint8_t block, uint16_t addr, uint16_t val) { uint8_t buf[2] = {(uint8_t) (val >> 8), (uint8_t) (val & 255)}; w5500_wn(s, block, addr, buf, sizeof(buf)); }
  20. static void w5500_rn(struct mip_spi *s, uint8_t block, uint16_t addr, void *buf, size_t len) { w5500_txn(s, block, addr, false, buf, len); }
  21. static uint8_t w5500_r1(struct mip_spi *s, uint8_t block, uint16_t addr) { uint8_t r = 0; w5500_rn(s, block, addr, &r, 1); return r; }
  22. static uint16_t w5500_r2(struct mip_spi *s, uint8_t block, uint16_t addr) { uint8_t buf[2] = {0, 0}; w5500_rn(s, block, addr, buf, sizeof(buf)); return (uint16_t) ((buf[0] << 8) | buf[1]); }
  23. // clang-format on
  24. static size_t w5500_rx(void *buf, size_t buflen, void *data) {
  25. struct mip_spi *s = (struct mip_spi *) data;
  26. uint16_t r = 0, n = 0, len = (uint16_t) buflen, n2; // Read recv len
  27. while ((n2 = w5500_r2(s, W5500_S0, 0x26)) > n) n = n2; // Until it is stable
  28. // printf("RSR: %d\n", (int) n);
  29. if (n > 0) {
  30. uint16_t ptr = w5500_r2(s, W5500_S0, 0x28); // Get read pointer
  31. n = w5500_r2(s, W5500_RX0, ptr); // Read frame length
  32. if (n <= len + 2) r = n - 2, w5500_rn(s, W5500_RX0, ptr + 2, buf, r);
  33. w5500_w2(s, W5500_S0, 0x28, ptr + n); // Advance read pointer
  34. w5500_w1(s, W5500_S0, 1, 0x40); // Sock0 CR -> RECV
  35. // printf(" RX_RD: tot=%u n=%u r=%u\n", n2, n, r);
  36. }
  37. return r;
  38. }
  39. static size_t w5500_tx(const void *buf, size_t buflen, void *data) {
  40. struct mip_spi *s = (struct mip_spi *) data;
  41. uint16_t n = 0, len = (uint16_t) buflen;
  42. while (n < len) n = w5500_r2(s, W5500_S0, 0x20); // Wait for space
  43. uint16_t ptr = w5500_r2(s, W5500_S0, 0x24); // Get write pointer
  44. w5500_wn(s, W5500_TX0, ptr, (void *) buf, len); // Write data
  45. w5500_w2(s, W5500_S0, 0x24, ptr + len); // Advance write pointer
  46. w5500_w1(s, W5500_S0, 1, 0x20); // Sock0 CR -> SEND
  47. for (int i = 0; i < 40; i++) {
  48. uint8_t ir = w5500_r1(s, W5500_S0, 2); // Read S0 IR
  49. if (ir == 0) continue;
  50. // printf("IR %d, len=%d, free=%d, ptr %d\n", ir, (int) len, (int) n, ptr);
  51. w5500_w1(s, W5500_S0, 2, ir); // Write S0 IR: clear it!
  52. if (ir & 8) len = 0; // Timeout. Report error
  53. if (ir & (16 | 8)) break; // Stop on SEND_OK or timeout
  54. }
  55. return len;
  56. }
  57. static bool w5500_init(uint8_t *mac, void *data) {
  58. struct mip_spi *s = (struct mip_spi *) data;
  59. s->end(s->spi);
  60. w5500_w1(s, W5500_CR, 0, 0x80); // Reset chip: CR -> 0x80
  61. w5500_w1(s, W5500_CR, 0x2e, 0); // CR PHYCFGR -> reset
  62. w5500_w1(s, W5500_CR, 0x2e, 0xf8); // CR PHYCFGR -> set
  63. // w5500_wn(s, W5500_CR, 9, s->mac, 6); // Set source MAC
  64. w5500_w1(s, W5500_S0, 0x1e, 16); // Sock0 RX buf size
  65. w5500_w1(s, W5500_S0, 0x1f, 16); // Sock0 TX buf size
  66. w5500_w1(s, W5500_S0, 0, 4); // Sock0 MR -> MACRAW
  67. w5500_w1(s, W5500_S0, 1, 1); // Sock0 CR -> OPEN
  68. return w5500_r1(s, W5500_S0, 3) == 0x42; // Sock0 SR == MACRAW
  69. (void) mac;
  70. }
  71. static bool w5500_up(void *data) {
  72. uint8_t phycfgr = w5500_r1((struct mip_spi *) data, W5500_CR, 0x2e);
  73. return phycfgr & 1; // Bit 0 of PHYCFGR is LNK (0 - down, 1 - up)
  74. }
  75. struct mip_driver mip_driver_w5500 = {
  76. .init = w5500_init, .tx = w5500_tx, .rx = w5500_rx, .up = w5500_up};
  77. #endif