mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-25 20:28:27 +00:00
Add TinyUSB example
This commit is contained in:
1 parent
6921227ec8
commit
57a5c0ba38
40 files changed
+23502
-313
No files matched your search
@@ -1,41 +0,0 @@
|
||||
#include "mip.h"
|
||||
|
||||
#if MG_ENABLE_MIP
|
||||
|
||||
// Instruction set
|
||||
enum { OP_RCR, OP_RBM, OP_WCR, OP_WBM, OP_BFS, OP_BFC, OP_SRC };
|
||||
|
||||
static uint8_t rd(struct mip_spi *spi, uint8_t op, uint8_t addr) {
|
||||
spi->begin(spi->spi);
|
||||
spi->txn(spi->spi, (uint8_t) ((op << 5) | (addr & 0x1f)));
|
||||
uint8_t value = spi->txn(spi->spi, 255);
|
||||
if (addr & 0x80) value = spi->txn(spi->spi, 255);
|
||||
spi->end(spi->spi);
|
||||
return value;
|
||||
}
|
||||
|
||||
static bool mip_driver_enc28j60_init(uint8_t *mac, void *data) {
|
||||
(void) mac, (void) data;
|
||||
rd((struct mip_spi *) data, OP_SRC, 0x1f);
|
||||
return false;
|
||||
}
|
||||
|
||||
static size_t mip_driver_enc28j60_tx(const void *buf, size_t len, void *data) {
|
||||
(void) buf, (void) len, (void) data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static size_t mip_driver_enc28j60_rx(void *buf, size_t len, void *data) {
|
||||
(void) buf, (void) len, (void) data;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool mip_driver_enc28j60_up(void *data) {
|
||||
(void) data;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct mip_driver mip_driver_enc28j60 = {
|
||||
mip_driver_enc28j60_init, mip_driver_enc28j60_tx, mip_driver_enc28j60_rx,
|
||||
mip_driver_enc28j60_up, NULL};
|
||||
#endif
|
||||
+15
-21
@@ -28,8 +28,7 @@ static uint32_t s_rxdesc[ETH_DESC_CNT][ETH_DS]; // RX descriptors
|
||||
static uint32_t s_txdesc[ETH_DESC_CNT][ETH_DS]; // TX descriptors
|
||||
static uint8_t s_rxbuf[ETH_DESC_CNT][ETH_PKT_SIZE]; // RX ethernet buffers
|
||||
static uint8_t s_txbuf[ETH_DESC_CNT][ETH_PKT_SIZE]; // TX ethernet buffers
|
||||
static void (*s_rx)(void *, size_t, void *); // Recv callback
|
||||
static void *s_rxdata; // Recv callback data
|
||||
static struct mip_if *s_ifp; // MIP interface
|
||||
enum { PHY_ADDR = 0, PHY_BCR = 0, PHY_BSR = 1 }; // PHY constants
|
||||
|
||||
static uint32_t eth_read_phy(uint8_t addr, uint8_t reg) {
|
||||
@@ -102,8 +101,10 @@ static int guess_mdc_cr(void) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool mip_driver_stm32_init(uint8_t *mac, void *userdata) {
|
||||
struct mip_driver_stm32 *d = (struct mip_driver_stm32 *) userdata;
|
||||
static bool mip_driver_stm32_init(struct mip_if *ifp) {
|
||||
struct mip_driver_stm32 *d = (struct mip_driver_stm32 *) ifp->driver_data;
|
||||
s_ifp = ifp;
|
||||
|
||||
// Init RX descriptors
|
||||
for (int i = 0; i < ETH_DESC_CNT; i++) {
|
||||
s_rxdesc[i][0] = BIT(31); // Own
|
||||
@@ -142,20 +143,15 @@ static bool mip_driver_stm32_init(uint8_t *mac, void *userdata) {
|
||||
ETH->DMAOMR = BIT(1) | BIT(13) | BIT(21) | BIT(25); // SR, ST, TSF, RSF
|
||||
|
||||
// MAC address filtering
|
||||
ETH->MACA0HR = ((uint32_t) mac[5] << 8U) | mac[4];
|
||||
ETH->MACA0LR = (uint32_t) (mac[3] << 24) | ((uint32_t) mac[2] << 16) |
|
||||
((uint32_t) mac[1] << 8) | mac[0];
|
||||
ETH->MACA0HR = ((uint32_t) ifp->mac[5] << 8U) | ifp->mac[4];
|
||||
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
|
||||
((uint32_t) ifp->mac[2] << 16) |
|
||||
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
|
||||
return true;
|
||||
}
|
||||
|
||||
static void mip_driver_stm32_setrx(void (*rx)(void *, size_t, void *),
|
||||
void *rxdata) {
|
||||
s_rx = rx;
|
||||
s_rxdata = rxdata;
|
||||
}
|
||||
|
||||
static uint32_t s_txno;
|
||||
static size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) {
|
||||
static size_t mip_driver_stm32_tx(const void *buf, size_t len, struct mip_if *ifp) {
|
||||
if (len > sizeof(s_txbuf[s_txno])) {
|
||||
MG_ERROR(("Frame too big, %ld", (long) len));
|
||||
len = 0; // Frame is too big
|
||||
@@ -173,12 +169,12 @@ static size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) {
|
||||
ETH->DMASR = BIT(2) | BIT(5); // Clear any prior TBUS/TUS
|
||||
ETH->DMATPDR = 0; // and resume
|
||||
return len;
|
||||
(void) userdata;
|
||||
(void) ifp;
|
||||
}
|
||||
|
||||
static bool mip_driver_stm32_up(void *userdata) {
|
||||
static bool mip_driver_stm32_up(struct mip_if *ifp) {
|
||||
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
|
||||
(void) userdata;
|
||||
(void) ifp;
|
||||
return bsr & BIT(2) ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -195,8 +191,7 @@ void ETH_IRQHandler(void) {
|
||||
uint32_t len = ((s_rxdesc[s_rxno][0] >> 16) & (BIT(14) - 1));
|
||||
// printf("%lx %lu %lx %.8lx\n", s_rxno, len, s_rxdesc[s_rxno][0],
|
||||
// ETH->DMASR);
|
||||
if (s_rx != NULL)
|
||||
s_rx(s_rxbuf[s_rxno], len > 4 ? len - 4 : len, s_rxdata);
|
||||
mip_rxcb(s_rxbuf[s_rxno], len > 4 ? len - 4 : len, s_ifp);
|
||||
}
|
||||
s_rxdesc[s_rxno][0] = BIT(31);
|
||||
if (++s_rxno >= ETH_DESC_CNT) s_rxno = 0;
|
||||
@@ -207,6 +202,5 @@ void ETH_IRQHandler(void) {
|
||||
}
|
||||
|
||||
struct mip_driver mip_driver_stm32 = {
|
||||
mip_driver_stm32_init, mip_driver_stm32_tx, NULL, mip_driver_stm32_up,
|
||||
mip_driver_stm32_setrx};
|
||||
mip_driver_stm32_init, mip_driver_stm32_tx, NULL, mip_driver_stm32_up};
|
||||
#endif
|
||||
+15
-21
@@ -34,8 +34,7 @@ static uint32_t s_rxdesc[ETH_DESC_CNT][ETH_DS]; // RX descriptors
|
||||
static uint32_t s_txdesc[ETH_DESC_CNT][ETH_DS]; // TX descriptors
|
||||
static uint8_t s_rxbuf[ETH_DESC_CNT][ETH_PKT_SIZE]; // RX ethernet buffers
|
||||
static uint8_t s_txbuf[ETH_DESC_CNT][ETH_PKT_SIZE]; // TX ethernet buffers
|
||||
static void (*s_rx)(void *, size_t, void *); // Recv callback
|
||||
static void *s_rxdata; // Recv callback data
|
||||
static struct mip_if *s_ifp; // MIP interface
|
||||
enum { EPHY_ADDR = 0, EPHYBMCR = 0, EPHYBMSR = 1 }; // PHY constants
|
||||
|
||||
static inline void tm4cspin(volatile uint32_t count) {
|
||||
@@ -124,8 +123,10 @@ static int guess_mdc_cr(void) {
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool mip_driver_tm4c_init(uint8_t *mac, void *userdata) {
|
||||
struct mip_driver_tm4c *d = (struct mip_driver_tm4c *) userdata;
|
||||
static bool mip_driver_tm4c_init(struct mip_if *ifp) {
|
||||
struct mip_driver_tm4c *d = (struct mip_driver_tm4c *) ifp->driver_data;
|
||||
s_ifp = ifp;
|
||||
|
||||
// Init RX descriptors
|
||||
for (int i = 0; i < ETH_DESC_CNT; i++) {
|
||||
s_rxdesc[i][0] = BIT(31); // Own
|
||||
@@ -165,23 +166,18 @@ static bool mip_driver_tm4c_init(uint8_t *mac, void *userdata) {
|
||||
EMAC->EMACCFG = BIT(2) | BIT(3) | BIT(11) | BIT(14); // RE, TE, Duplex, Fast
|
||||
EMAC->EMACDMAOPMODE =
|
||||
BIT(1) | BIT(13) | BIT(21) | BIT(25); // SR, ST, TSF, RSF
|
||||
EMAC->EMACADDR0H = ((uint32_t) mac[5] << 8U) | mac[4];
|
||||
EMAC->EMACADDR0L = (uint32_t) (mac[3] << 24) | ((uint32_t) mac[2] << 16) |
|
||||
((uint32_t) mac[1] << 8) | mac[0];
|
||||
EMAC->EMACADDR0H = ((uint32_t) ifp->mac[5] << 8U) | ifp->mac[4];
|
||||
EMAC->EMACADDR0L = (uint32_t) (ifp->mac[3] << 24) |
|
||||
((uint32_t) ifp->mac[2] << 16) |
|
||||
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
|
||||
// NOTE(scaprile) There are 3 additional slots for filtering, disabled by
|
||||
// default. This also applies to the STM32 driver (at least for F7)
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static void mip_driver_tm4c_setrx(void (*rx)(void *, size_t, void *),
|
||||
void *rxdata) {
|
||||
s_rx = rx;
|
||||
s_rxdata = rxdata;
|
||||
}
|
||||
|
||||
static uint32_t s_txno;
|
||||
static size_t mip_driver_tm4c_tx(const void *buf, size_t len, void *userdata) {
|
||||
static size_t mip_driver_tm4c_tx(const void *buf, size_t len, struct mip_if *ifp) {
|
||||
if (len > sizeof(s_txbuf[s_txno])) {
|
||||
MG_ERROR(("Frame too big, %ld", (long) len));
|
||||
len = 0; // fail
|
||||
@@ -201,12 +197,12 @@ static size_t mip_driver_tm4c_tx(const void *buf, size_t len, void *userdata) {
|
||||
EMAC->EMACDMARIS = BIT(2) | BIT(5); // Clear any prior TU/UNF
|
||||
EMAC->EMACTXPOLLD = 0; // and resume
|
||||
return len;
|
||||
(void) userdata;
|
||||
(void) ifp;
|
||||
}
|
||||
|
||||
static bool mip_driver_tm4c_up(void *userdata) {
|
||||
static bool mip_driver_tm4c_up(struct mip_if *ifp) {
|
||||
uint32_t bmsr = emac_read_phy(EPHY_ADDR, EPHYBMSR);
|
||||
(void) userdata;
|
||||
(void) ifp;
|
||||
return (bmsr & BIT(2)) ? 1 : 0;
|
||||
}
|
||||
|
||||
@@ -223,8 +219,7 @@ void EMAC0_IRQHandler(void) {
|
||||
uint32_t len = ((s_rxdesc[s_rxno][0] >> 16) & (BIT(14) - 1));
|
||||
// printf("%lx %lu %lx %.8lx\n", s_rxno, len, s_rxdesc[s_rxno][0],
|
||||
// EMAC->EMACDMARIS);
|
||||
if (s_rx != NULL)
|
||||
s_rx(s_rxbuf[s_rxno], len > 4 ? len - 4 : len, s_rxdata);
|
||||
mip_rxcb(s_rxbuf[s_rxno], len > 4 ? len - 4 : len, s_ifp);
|
||||
}
|
||||
s_rxdesc[s_rxno][0] = BIT(31);
|
||||
if (++s_rxno >= ETH_DESC_CNT) s_rxno = 0;
|
||||
@@ -235,6 +230,5 @@ void EMAC0_IRQHandler(void) {
|
||||
}
|
||||
|
||||
struct mip_driver mip_driver_tm4c = {mip_driver_tm4c_init, mip_driver_tm4c_tx,
|
||||
NULL, mip_driver_tm4c_up,
|
||||
mip_driver_tm4c_setrx};
|
||||
NULL, mip_driver_tm4c_up};
|
||||
#endif
|
||||
+10
-11
@@ -27,8 +27,8 @@ static uint8_t w5500_r1(struct mip_spi *s, uint8_t block, uint16_t addr) { uint
|
||||
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]); }
|
||||
// clang-format on
|
||||
|
||||
static size_t w5500_rx(void *buf, size_t buflen, void *data) {
|
||||
struct mip_spi *s = (struct mip_spi *) data;
|
||||
static size_t w5500_rx(void *buf, size_t buflen, struct mip_if *ifp) {
|
||||
struct mip_spi *s = (struct mip_spi *) ifp->driver_data;
|
||||
uint16_t r = 0, n = 0, len = (uint16_t) buflen, n2; // Read recv len
|
||||
while ((n2 = w5500_r2(s, W5500_S0, 0x26)) > n) n = n2; // Until it is stable
|
||||
// printf("RSR: %d\n", (int) n);
|
||||
@@ -46,8 +46,8 @@ static size_t w5500_rx(void *buf, size_t buflen, void *data) {
|
||||
return r;
|
||||
}
|
||||
|
||||
static size_t w5500_tx(const void *buf, size_t buflen, void *data) {
|
||||
struct mip_spi *s = (struct mip_spi *) data;
|
||||
static size_t w5500_tx(const void *buf, size_t buflen, struct mip_if *ifp) {
|
||||
struct mip_spi *s = (struct mip_spi *) ifp->driver_data;
|
||||
uint16_t n = 0, len = (uint16_t) buflen;
|
||||
while (n < len) n = w5500_r2(s, W5500_S0, 0x20); // Wait for space
|
||||
uint16_t ptr = w5500_r2(s, W5500_S0, 0x24); // Get write pointer
|
||||
@@ -65,8 +65,8 @@ static size_t w5500_tx(const void *buf, size_t buflen, void *data) {
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool w5500_init(uint8_t *mac, void *data) {
|
||||
struct mip_spi *s = (struct mip_spi *) data;
|
||||
static bool w5500_init(struct mip_if *ifp) {
|
||||
struct mip_spi *s = (struct mip_spi *) ifp->driver_data;
|
||||
s->end(s->spi);
|
||||
w5500_w1(s, W5500_CR, 0, 0x80); // Reset chip: CR -> 0x80
|
||||
w5500_w1(s, W5500_CR, 0x2e, 0); // CR PHYCFGR -> reset
|
||||
@@ -77,14 +77,13 @@ static bool w5500_init(uint8_t *mac, void *data) {
|
||||
w5500_w1(s, W5500_S0, 0, 4); // Sock0 MR -> MACRAW
|
||||
w5500_w1(s, W5500_S0, 1, 1); // Sock0 CR -> OPEN
|
||||
return w5500_r1(s, W5500_S0, 3) == 0x42; // Sock0 SR == MACRAW
|
||||
(void) mac;
|
||||
}
|
||||
|
||||
static bool w5500_up(void *data) {
|
||||
uint8_t phycfgr = w5500_r1((struct mip_spi *) data, W5500_CR, 0x2e);
|
||||
static bool w5500_up(struct mip_if *ifp) {
|
||||
struct mip_spi *spi = (struct mip_spi *) ifp->driver_data;
|
||||
uint8_t phycfgr = w5500_r1(spi, W5500_CR, 0x2e);
|
||||
return phycfgr & 1; // Bit 0 of PHYCFGR is LNK (0 - down, 1 - up)
|
||||
}
|
||||
|
||||
struct mip_driver mip_driver_w5500 = {w5500_init, w5500_tx, w5500_rx, w5500_up,
|
||||
NULL};
|
||||
struct mip_driver mip_driver_w5500 = {w5500_init, w5500_tx, w5500_rx, w5500_up};
|
||||
#endif
|
||||
@@ -259,7 +259,7 @@ static size_t ether_output(struct mip_if *ifp, size_t len) {
|
||||
// size_t min = 64; // Pad short frames to 64 bytes (minimum Ethernet size)
|
||||
// if (len < min) memset(ifp->tx.ptr + len, 0, min - len), len = min;
|
||||
// mg_hexdump(ifp->tx.ptr, len);
|
||||
return ifp->driver->tx(ifp->tx.ptr, len, ifp->driver_data);
|
||||
return ifp->driver->tx(ifp->tx.ptr, len, ifp);
|
||||
}
|
||||
|
||||
static void arp_ask(struct mip_if *ifp, uint32_t ip) {
|
||||
@@ -280,7 +280,10 @@ static void onstatechange(struct mip_if *ifp) {
|
||||
if (ifp->state == MIP_STATE_READY) {
|
||||
MG_INFO(("READY, IP: %I", 4, &ifp->ip));
|
||||
MG_INFO((" GW: %I", 4, &ifp->gw));
|
||||
MG_INFO((" Lease: %lld sec", (ifp->lease_expire - ifp->now) / 1000));
|
||||
if (ifp->lease_expire > ifp->now) {
|
||||
MG_INFO(
|
||||
(" Lease: %lld sec", (ifp->lease_expire - ifp->now) / 1000));
|
||||
}
|
||||
arp_ask(ifp, ifp->gw);
|
||||
} else if (ifp->state == MIP_STATE_UP) {
|
||||
MG_ERROR(("Link up"));
|
||||
@@ -410,7 +413,7 @@ static void rx_icmp(struct mip_if *ifp, struct pkt *pkt) {
|
||||
}
|
||||
}
|
||||
|
||||
static void rx_dhcp(struct mip_if *ifp, struct pkt *pkt) {
|
||||
static void rx_dhcp_client(struct mip_if *ifp, struct pkt *pkt) {
|
||||
uint32_t ip = 0, gw = 0, mask = 0;
|
||||
uint8_t *p = pkt->dhcp->options,
|
||||
*end = (uint8_t *) &pkt->raw.ptr[pkt->raw.len];
|
||||
@@ -437,6 +440,43 @@ static void rx_dhcp(struct mip_if *ifp, struct pkt *pkt) {
|
||||
}
|
||||
}
|
||||
|
||||
// Simple DHCP server that assigns a next IP address: ifp->ip + 1
|
||||
static void rx_dhcp_server(struct mip_if *ifp, struct pkt *pkt) {
|
||||
uint8_t op = 0, *p = pkt->dhcp->options,
|
||||
*end = (uint8_t *) &pkt->raw.ptr[pkt->raw.len];
|
||||
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
|
||||
// struct dhcp *req = pkt->dhcp;
|
||||
struct dhcp res = {2, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, {0}};
|
||||
res.yiaddr = ifp->ip;
|
||||
((uint8_t *) (&res.yiaddr))[3]++; // Offer our IP + 1
|
||||
while (p + 1 < end && p[0] != 255) { // Parse options
|
||||
if (p[0] == 53 && p[1] == 1 && p + 2 < end) { // Message type
|
||||
op = p[2];
|
||||
}
|
||||
p += p[1] + 2;
|
||||
}
|
||||
if (op == 1 || op == 3) { // DHCP Discover or DHCP Request
|
||||
uint8_t msg = op == 1 ? 2 : 5; // Message type: DHCP OFFER or DHCP ACK
|
||||
uint8_t opts[] = {
|
||||
53, 1, msg, // Message type
|
||||
1, 4, 0, 0, 0, 0, // Subnet mask
|
||||
54, 4, 0, 0, 0, 0, // Server ID
|
||||
12, 3, 'm', 'i', 'p', // Host name: "mip"
|
||||
51, 4, 255, 255, 255, 255, // Lease time
|
||||
255 // End of options
|
||||
};
|
||||
memcpy(&res.hwaddr, pkt->dhcp->hwaddr, 6);
|
||||
memcpy(opts + 5, &ifp->mask, sizeof(ifp->mask));
|
||||
memcpy(opts + 11, &ifp->ip, sizeof(ifp->ip));
|
||||
memcpy(&res.options, opts, sizeof(opts));
|
||||
res.magic = pkt->dhcp->magic;
|
||||
res.xid = pkt->dhcp->xid;
|
||||
arp_cache_add(ifp, res.yiaddr, pkt->eth->src);
|
||||
tx_udp(ifp, ifp->ip, mg_htons(67), op == 1 ? ~0U : res.yiaddr, mg_htons(68),
|
||||
&res, sizeof(res));
|
||||
}
|
||||
}
|
||||
|
||||
static struct mg_connection *getpeer(struct mg_mgr *mgr, struct pkt *pkt,
|
||||
bool lsn) {
|
||||
struct mg_connection *c = NULL;
|
||||
@@ -671,13 +711,15 @@ static void rx_ip(struct mip_if *ifp, struct pkt *pkt) {
|
||||
} else if (pkt->ip->proto == 17) {
|
||||
pkt->udp = (struct udp *) (pkt->ip + 1);
|
||||
if (pkt->pay.len < sizeof(*pkt->udp)) return;
|
||||
// MG_DEBUG((" UDP %u %u -> %u", len, mg_htons(udp->sport),
|
||||
// mg_htons(udp->dport)));
|
||||
mkpay(pkt, pkt->udp + 1);
|
||||
if (pkt->udp->dport == mg_htons(68)) {
|
||||
pkt->dhcp = (struct dhcp *) (pkt->udp + 1);
|
||||
mkpay(pkt, pkt->dhcp + 1);
|
||||
rx_dhcp(ifp, pkt);
|
||||
rx_dhcp_client(ifp, pkt);
|
||||
} else if (ifp->enable_dhcp_server && pkt->udp->dport == mg_htons(67)) {
|
||||
pkt->dhcp = (struct dhcp *) (pkt->udp + 1);
|
||||
mkpay(pkt, pkt->dhcp + 1);
|
||||
rx_dhcp_server(ifp, pkt);
|
||||
} else {
|
||||
rx_udp(ifp, pkt);
|
||||
}
|
||||
@@ -710,7 +752,6 @@ static void rx_ip6(struct mip_if *ifp, struct pkt *pkt) {
|
||||
|
||||
static void mip_rx(struct mip_if *ifp, void *buf, size_t len) {
|
||||
const uint8_t broadcast[] = {255, 255, 255, 255, 255, 255};
|
||||
// struct pkt pkt = {.raw = {.buf = (uint8_t *) buf, .len = len}};
|
||||
struct pkt pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
pkt.raw.ptr = (char *) buf;
|
||||
@@ -753,13 +794,13 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) {
|
||||
|
||||
// Handle physical interface up/down status
|
||||
if (expired_1000ms && ifp->driver->up) {
|
||||
bool up = ifp->driver->up(ifp->driver_data);
|
||||
bool up = ifp->driver->up(ifp);
|
||||
bool current = ifp->state != MIP_STATE_DOWN;
|
||||
if (up != current) {
|
||||
ifp->state = up == false ? MIP_STATE_DOWN
|
||||
: ifp->use_dhcp ? MIP_STATE_UP
|
||||
: MIP_STATE_READY;
|
||||
if (!up && ifp->use_dhcp) ifp->ip = 0;
|
||||
ifp->state = up == false ? MIP_STATE_DOWN
|
||||
: ifp->enable_dhcp_client ? MIP_STATE_UP
|
||||
: MIP_STATE_READY;
|
||||
if (!up && ifp->enable_dhcp_client) ifp->ip = 0;
|
||||
onstatechange(ifp);
|
||||
}
|
||||
}
|
||||
@@ -768,7 +809,7 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) {
|
||||
|
||||
if (ifp->ip == 0 && expired_1000ms) {
|
||||
tx_dhcp_discover(ifp); // If IP not configured, send DHCP
|
||||
} else if (ifp->use_dhcp == false && expired_1000ms &&
|
||||
} else if (ifp->enable_dhcp_client == false && expired_1000ms && ifp->gw &&
|
||||
arp_cache_find(ifp, ifp->gw) == NULL) {
|
||||
arp_ask(ifp, ifp->gw); // If GW's MAC address in not in ARP cache
|
||||
}
|
||||
@@ -776,8 +817,7 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) {
|
||||
// Read data from the network
|
||||
size_t len = ifp->queue.len > 0
|
||||
? q_read(&ifp->queue, (void *) ifp->rx.ptr)
|
||||
: ifp->driver->rx((void *) ifp->rx.ptr, ifp->rx.len,
|
||||
ifp->driver_data);
|
||||
: ifp->driver->rx((void *) ifp->rx.ptr, ifp->rx.len, ifp);
|
||||
qp_mark(QP_FRAMEPOPPED, (int) q_space(&ifp->queue));
|
||||
mip_rx(ifp, (void *) ifp->rx.ptr, len);
|
||||
qp_mark(QP_FRAMEDONE, (int) q_space(&ifp->queue));
|
||||
@@ -809,8 +849,7 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) {
|
||||
// This function executes in interrupt context, thus it should copy data
|
||||
// somewhere fast. Note that newlib's malloc is not thread safe, thus use
|
||||
// our lock-free queue with preallocated buffer to copy data and return asap
|
||||
static void on_rx(void *buf, size_t len, void *userdata) {
|
||||
struct mip_if *ifp = (struct mip_if *) userdata;
|
||||
void mip_rxcb(void *buf, size_t len, struct mip_if *ifp) {
|
||||
if (q_write(&ifp->queue, buf, len)) {
|
||||
qp_mark(QP_FRAMEPUSHED, (int) q_space(&ifp->queue));
|
||||
} else {
|
||||
@@ -821,22 +860,19 @@ static void on_rx(void *buf, size_t len, void *userdata) {
|
||||
}
|
||||
|
||||
void mip_init(struct mg_mgr *mgr, struct mip_if *ifp) {
|
||||
if (ifp->driver->init && !ifp->driver->init(ifp->mac, ifp->driver_data)) {
|
||||
if (ifp->driver->init && !ifp->driver->init(ifp)) {
|
||||
MG_ERROR(("driver init failed"));
|
||||
} else {
|
||||
size_t maxpktsize = 1540;
|
||||
ifp->rx.ptr = (char *) calloc(1, maxpktsize), ifp->rx.len = maxpktsize;
|
||||
ifp->tx.ptr = (char *) calloc(1, maxpktsize), ifp->tx.len = maxpktsize;
|
||||
if (ifp->driver->setrx) {
|
||||
ifp->queue.len = MIP_QSIZE;
|
||||
ifp->queue.buf = (uint8_t *) calloc(1, ifp->queue.len);
|
||||
ifp->driver->setrx(on_rx, ifp);
|
||||
}
|
||||
if (ifp->queue.len) ifp->queue.buf = (uint8_t *) calloc(1, ifp->queue.len);
|
||||
ifp->timer_1000ms = mg_millis();
|
||||
arp_cache_init(ifp->arp_cache, MIP_ARP_ENTRIES, 12);
|
||||
mgr->priv = ifp;
|
||||
ifp->mgr = mgr;
|
||||
mgr->extraconnsize = sizeof(struct connstate);
|
||||
if (ifp->ip == 0) ifp->enable_dhcp_client = true;
|
||||
#ifdef MIP_QPROFILE
|
||||
qp_init();
|
||||
#endif
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
#include "arch.h"
|
||||
#include "net.h"
|
||||
|
||||
struct mip_if; // MIP network interface
|
||||
|
||||
struct mip_driver {
|
||||
bool (*init)(uint8_t *mac, void *data); // Initialise driver
|
||||
size_t (*tx)(const void *, size_t, void *data); // Transmit frame
|
||||
size_t (*rx)(void *buf, size_t len, void *data); // Receive frame (polling)
|
||||
bool (*up)(void *data); // Up/down status
|
||||
// Set receive callback for interrupt-driven drivers
|
||||
void (*setrx)(void (*fn)(void *buf, size_t len, void *rxdata), void *rxdata);
|
||||
bool (*init)(struct mip_if *); // Initialise driver
|
||||
size_t (*tx)(const void *, size_t, struct mip_if *); // Transmit frame
|
||||
size_t (*rx)(void *buf, size_t len, struct mip_if *); // Receive frame (poll)
|
||||
bool (*up)(struct mip_if *); // Up/down status
|
||||
};
|
||||
|
||||
// Receive queue - single producer, single consumer queue. Interrupt-based
|
||||
@@ -27,13 +27,15 @@ struct queue {
|
||||
// Network interface
|
||||
struct mip_if {
|
||||
uint8_t mac[6]; // MAC address. Must be set to a valid MAC
|
||||
uint32_t ip, mask, gw; // IP address, mask, default gateway. Can be 0
|
||||
uint32_t ip, mask, gw; // IP address, mask, default gateway
|
||||
struct mg_str rx; // Output (TX) buffer
|
||||
struct mg_str tx; // Input (RX) buffer
|
||||
bool use_dhcp; // Enable DCHP
|
||||
bool enable_dhcp_client; // Enable DCHP client
|
||||
bool enable_dhcp_server; // Enable DCHP server
|
||||
struct mip_driver *driver; // Low level driver
|
||||
void *driver_data; // Driver-specific data
|
||||
struct mg_mgr *mgr; // Mongoose event manager
|
||||
struct queue queue; // Set queue.len for interrupt based drivers
|
||||
|
||||
// Internal state, user can use it but should not change it
|
||||
uint64_t now; // Current time
|
||||
@@ -46,14 +48,13 @@ struct mip_if {
|
||||
#define MIP_STATE_DOWN 0 // Interface is down
|
||||
#define MIP_STATE_UP 1 // Interface is up
|
||||
#define MIP_STATE_READY 2 // Interface is up and has IP
|
||||
struct queue queue; // Receive queue
|
||||
};
|
||||
|
||||
void mip_init(struct mg_mgr *, struct mip_if *);
|
||||
void mip_free(struct mip_if *);
|
||||
void mip_rxcb(void *buf, size_t len, struct mip_if *ifp);
|
||||
|
||||
extern struct mip_driver mip_driver_stm32;
|
||||
extern struct mip_driver mip_driver_enc28j60;
|
||||
extern struct mip_driver mip_driver_w5500;
|
||||
extern struct mip_driver mip_driver_tm4c;
|
||||
|
||||
|
||||
Reference in new issue
Block a user