From 8a61969c8f6168021886aeb132b093612a698f2a Mon Sep 17 00:00:00 2001 From: cpq Date: Wed, 31 Aug 2022 08:34:38 +0100 Subject: [PATCH] Add struct mip_spi for SPI drivers, and skeleton for enc28j60 driver --- mip/driver_stm32.c | 11 ++++------ mip/mip.c | 8 ++++---- mip/mip.h | 13 ++++++++++-- mongoose.c | 51 +++++++++++++++++++++++++++++++++------------- mongoose.h | 13 ++++++++++-- src/timer.c | 3 --- 6 files changed, 67 insertions(+), 32 deletions(-) diff --git a/mip/driver_stm32.c b/mip/driver_stm32.c index c3c4c448..dbd0b052 100644 --- a/mip/driver_stm32.c +++ b/mip/driver_stm32.c @@ -1,6 +1,3 @@ -// Copyright (c) 2022 Cesanta Software Limited -// All rights reserved - #include "mip.h" #if MG_ENABLE_MIP && defined(__arm__) @@ -120,10 +117,10 @@ static size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) { (void) userdata; } -static bool mip_driver_stm32_status(void *userdata) { +static bool mip_driver_stm32_up(void *userdata) { uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR); - return bsr & BIT(2) ? 1 : 0; (void) userdata; + return bsr & BIT(2) ? 1 : 0; } void ETH_IRQHandler(void); @@ -144,6 +141,6 @@ void ETH_IRQHandler(void) { struct mip_driver mip_driver_stm32 = {.init = mip_driver_stm32_init, .tx = mip_driver_stm32_tx, - .rxcb = mip_driver_stm32_setrx, - .status = mip_driver_stm32_status}; + .setrx = mip_driver_stm32_setrx, + .up = mip_driver_stm32_up}; #endif // MG_ENABLE_MIP diff --git a/mip/mip.c b/mip/mip.c index cf4b50bd..1940594f 100644 --- a/mip/mip.c +++ b/mip/mip.c @@ -681,8 +681,8 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) { } // Handle physical interface up/down status - if (ifp->driver->status) { - bool up = ifp->driver->status(ifp->driver_data); + if (ifp->driver->up) { + bool up = ifp->driver->up(ifp->driver_data); bool current = ifp->state != MIP_STATE_DOWN; if (up != current) { ifp->state = up == false ? MIP_STATE_DOWN @@ -713,7 +713,7 @@ static void on_rx(void *buf, size_t len, void *userdata) { void mip_init(struct mg_mgr *mgr, struct mip_ipcfg *ipcfg, struct mip_driver *driver, void *driver_data) { - size_t maxpktsize = 1500, qlen = driver->rxcb ? 1024 * 16 : 0; + size_t maxpktsize = 1500, qlen = driver->setrx ? 1024 * 16 : 0; struct mip_if *ifp = (struct mip_if *) calloc(1, sizeof(*ifp) + 2 * maxpktsize + qlen); memcpy(ifp->mac, ipcfg->mac, sizeof(ifp->mac)); @@ -727,7 +727,7 @@ void mip_init(struct mg_mgr *mgr, struct mip_ipcfg *ipcfg, ifp->queue.buf = ifp->tx.buf + maxpktsize; ifp->queue.len = qlen; if (driver->init) driver->init(ipcfg->mac, driver_data); - if (driver->rxcb) driver->rxcb(on_rx, ifp); + if (driver->setrx) driver->setrx(on_rx, ifp); mgr->priv = ifp; mgr->extraconnsize = sizeof(struct tcpstate); } diff --git a/mip/mip.h b/mip/mip.h index 9908a5f3..2adcda12 100644 --- a/mip/mip.h +++ b/mip/mip.h @@ -7,9 +7,9 @@ struct mip_driver { void (*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 (*status)(void *data); // Up/down status + bool (*up)(void *data); // Up/down status // Set receive callback for interrupt-driven drivers - void (*rxcb)(void (*fn)(void *buf, size_t len, void *rxdata), void *rxdata); + void (*setrx)(void (*fn)(void *buf, size_t len, void *rxdata), void *rxdata); }; struct mip_ipcfg { @@ -20,3 +20,12 @@ struct mip_ipcfg { void mip_init(struct mg_mgr *, struct mip_ipcfg *, struct mip_driver *, void *); extern struct mip_driver mip_driver_stm32; +extern struct mip_driver mip_driver_enc28j60; + +// Drivers that require SPI, can use this SPI abstraction +struct mip_spi { + void *spi; // Opaque SPI bus descriptor + uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply + void (*begin)(void *); // SPI begin: slave select low + void (*end)(void *); // SPI end: slave select high +}; diff --git a/mongoose.c b/mongoose.c index d94ce7ef..51038630 100644 --- a/mongoose.c +++ b/mongoose.c @@ -4970,9 +4970,6 @@ char *mg_remove_double_dots(char *s) { #ifdef MG_ENABLE_LINES #line 1 "src/timer.c" #endif -// Copyright (c) Cesanta Software Limited -// All rights reserved - @@ -5928,12 +5925,38 @@ size_t mg_ws_wrap(struct mg_connection *c, size_t len, int op) { return c->send.len; } +#ifdef MG_ENABLE_LINES +#line 1 "mip/driver_enc28j60.c" +#endif + + +static void mip_driver_enc28j60_init(uint8_t *mac, void *data) { + (void) mac, (void) data; +} + +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 = {.init = mip_driver_enc28j60_init, + .tx = mip_driver_enc28j60_tx, + .rx = mip_driver_enc28j60_rx, + .up = mip_driver_enc28j60_up}; + #ifdef MG_ENABLE_LINES #line 1 "mip/driver_stm32.c" #endif -// Copyright (c) 2022 Cesanta Software Limited -// All rights reserved - #if MG_ENABLE_MIP && defined(__arm__) @@ -6053,10 +6076,10 @@ static size_t mip_driver_stm32_tx(const void *buf, size_t len, void *userdata) { (void) userdata; } -static bool mip_driver_stm32_status(void *userdata) { +static bool mip_driver_stm32_up(void *userdata) { uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR); - return bsr & BIT(2) ? 1 : 0; (void) userdata; + return bsr & BIT(2) ? 1 : 0; } void ETH_IRQHandler(void); @@ -6077,8 +6100,8 @@ void ETH_IRQHandler(void) { struct mip_driver mip_driver_stm32 = {.init = mip_driver_stm32_init, .tx = mip_driver_stm32_tx, - .rxcb = mip_driver_stm32_setrx, - .status = mip_driver_stm32_status}; + .setrx = mip_driver_stm32_setrx, + .up = mip_driver_stm32_up}; #endif // MG_ENABLE_MIP #ifdef MG_ENABLE_LINES @@ -6767,8 +6790,8 @@ static void mip_poll(struct mip_if *ifp, uint64_t uptime_ms) { } // Handle physical interface up/down status - if (ifp->driver->status) { - bool up = ifp->driver->status(ifp->driver_data); + if (ifp->driver->up) { + bool up = ifp->driver->up(ifp->driver_data); bool current = ifp->state != MIP_STATE_DOWN; if (up != current) { ifp->state = up == false ? MIP_STATE_DOWN @@ -6799,7 +6822,7 @@ static void on_rx(void *buf, size_t len, void *userdata) { void mip_init(struct mg_mgr *mgr, struct mip_ipcfg *ipcfg, struct mip_driver *driver, void *driver_data) { - size_t maxpktsize = 1500, qlen = driver->rxcb ? 1024 * 16 : 0; + size_t maxpktsize = 1500, qlen = driver->setrx ? 1024 * 16 : 0; struct mip_if *ifp = (struct mip_if *) calloc(1, sizeof(*ifp) + 2 * maxpktsize + qlen); memcpy(ifp->mac, ipcfg->mac, sizeof(ifp->mac)); @@ -6813,7 +6836,7 @@ void mip_init(struct mg_mgr *mgr, struct mip_ipcfg *ipcfg, ifp->queue.buf = ifp->tx.buf + maxpktsize; ifp->queue.len = qlen; if (driver->init) driver->init(ipcfg->mac, driver_data); - if (driver->rxcb) driver->rxcb(on_rx, ifp); + if (driver->setrx) driver->setrx(on_rx, ifp); mgr->priv = ifp; mgr->extraconnsize = sizeof(struct tcpstate); } diff --git a/mongoose.h b/mongoose.h index 0bc97e17..a2f933af 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1426,9 +1426,9 @@ struct mip_driver { void (*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 (*status)(void *data); // Up/down status + bool (*up)(void *data); // Up/down status // Set receive callback for interrupt-driven drivers - void (*rxcb)(void (*fn)(void *buf, size_t len, void *rxdata), void *rxdata); + void (*setrx)(void (*fn)(void *buf, size_t len, void *rxdata), void *rxdata); }; struct mip_ipcfg { @@ -1439,6 +1439,15 @@ struct mip_ipcfg { void mip_init(struct mg_mgr *, struct mip_ipcfg *, struct mip_driver *, void *); extern struct mip_driver mip_driver_stm32; +extern struct mip_driver mip_driver_enc28j60; + +// Drivers that require SPI, can use this SPI abstraction +struct mip_spi { + void *spi; // Opaque SPI bus descriptor + uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply + void (*begin)(void *); // SPI begin: slave select low + void (*end)(void *); // SPI end: slave select high +}; #ifdef __cplusplus } diff --git a/src/timer.c b/src/timer.c index ec3f7990..e925b511 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,6 +1,3 @@ -// Copyright (c) Cesanta Software Limited -// All rights reserved - #include "timer.h" #include "arch.h"