Merge pull request #3481 from cesanta/spi

unify SPI helper APIs
This commit is contained in:
Sergey Lyubka authored and GitHub committed 2026-04-03 11:39:33 +01:00
commit 02e01f90b9
18 files changed
+75 -104

No files matched your search

@@ -8,7 +8,13 @@ struct mg_tcpip_spi spi = {
NULL, // SPI metadata
[](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); },
[](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); },
[](void *, uint8_t c) { return SPI.transfer(c); }, // Execute transaction
[](void *, uint8_t *w, uint8_t *r, size_t n) { // Execute transaction
while (n--) {
uint8_t c = (w != NULL) ? *w++ : 0xFF;
uint8_t d = SPI.transfer(c);
if (r != NULL) *r++ = d;
}
},
};
struct mg_mgr mgr; // Mongoose event manager
struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface
@@ -12,7 +12,13 @@ struct mg_tcpip_spi spi = {
NULL, // SPI metadata
[](void *) { digitalWrite(SS_PIN, LOW); SPI.beginTransaction(SPISettings()); },
[](void *) { digitalWrite(SS_PIN, HIGH); SPI.endTransaction(); },
[](void *, uint8_t c) { return SPI.transfer(c); }, // Execute transaction
[](void *, uint8_t *w, uint8_t *r, size_t n) { // Execute transaction
while (n--) {
uint8_t c = (w != NULL) ? *w++ : 0xFF;
uint8_t d = SPI.transfer(c);
if (r != NULL) *r++ = d;
}
},
};
struct mg_mgr mgr; // Mongoose event manager
struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface
@@ -9,7 +9,7 @@
#define WIFI_PASS "YOUR_WIFI_PASSWORD" // SET THIS!
static const struct mg_tcpip_spi_ spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
static const struct mg_tcpip_spi spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
#ifndef CYW43_RESOURCE_ATTRIBUTE
#define CYW43_RESOURCE_ATTRIBUTE
@@ -24,7 +24,6 @@ static const struct mg_tcpip_driver_cyw_firmware fw = {
// mif user states
enum {AP, SCANNING, STOPPING_AP, CONNECTING, READY};
static unsigned int state;
static uint32_t s_ip, s_mask;
static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
@@ -34,13 +33,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
}
switch(state) {
case AP: // we are in AP mode, wait for a user connection to trigger a scan or a connection to a network
if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_UP) {
MG_INFO(("Access Point started"));
s_ip = ifp->ip, ifp->ip = MG_IPV4(192, 168, 169, 1);
s_mask = ifp->mask, ifp->mask = MG_IPV4(255, 255, 255, 0);
ifp->enable_dhcp_client = false;
ifp->enable_dhcp_server = true;
} else if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_READY) {
if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_READY) {
MG_INFO(("Access Point READY !"));
// simulate user request to scan for networks
@@ -54,7 +47,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
struct mg_wifi_scan_bss_data *bss = (struct mg_wifi_scan_bss_data *) ev_data;
MG_INFO(("BSS: %.*s (%u) (%M) %d dBm %u", bss->SSID.len, bss->SSID.buf, bss->channel, mg_print_mac, bss->BSSID, (int) bss->RSSI, bss->security));
} else if (ev == MG_TCPIP_EV_WIFI_SCAN_END) {
struct mg_tcpip_driver_cyw_data *d = (struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
//struct mg_tcpip_driver_cyw_data *d = (struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
MG_INFO(("Wi-Fi scan finished"));
// simulate user selection of a network (1/2: stop AP)
@@ -66,18 +59,14 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
break;
case STOPPING_AP:
if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_DOWN) {
struct mg_tcpip_driver_cyw_data *d = (struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
d->apmode = false;
struct mg_wifi_data *wifi = &((struct mg_tcpip_driver_cyw_data *) ifp->driver_data)->wifi;
wifi->apmode = false;
// simulate user selection of a network (2/2: actual connect)
bool res = mg_wifi_connect(d->ssid, d->pass);
bool res = mg_wifi_connect(wifi);
MG_INFO(("Manually connecting: %s", res ? "OK":"FAIL"));
if (res) {
state = CONNECTING;
ifp->ip = s_ip;
ifp->mask = s_mask;
if (ifp->ip == 0) ifp->enable_dhcp_client = true;
ifp->enable_dhcp_server = false;
} // else manually start AP as below
}
break;
@@ -97,13 +86,13 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
case READY:
// go back to AP mode after a disconnection (simulation 2/2), you could retry
if (ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_DOWN) {
struct mg_tcpip_driver_cyw_data *d = (struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
bool res = mg_wifi_ap_start(d->apssid, d->appass, d->apchannel);
struct mg_wifi_data *wifi = &((struct mg_tcpip_driver_cyw_data *) ifp->driver_data)->wifi;
bool res = mg_wifi_ap_start(wifi);
MG_INFO(("Disconnected"));
MG_INFO(("Manually starting AP: %s", res ? "OK":"FAIL"));
if (res) {
state = AP;
d->apmode = true;
wifi->apmode = true;
}
}
break;
@@ -112,7 +101,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
static struct mg_tcpip_driver_cyw_data d = {
(struct mg_tcpip_spi_ *)&spi, (struct mg_tcpip_driver_cyw_firmware *)&fw, WIFI_SSID, WIFI_PASS, "mongoose", "mongoose", 0, 0, 10, true, true};
{WIFI_SSID, WIFI_PASS, "mongoose", "mongoose", 0, 0, 0, 0, 10, true}, (struct mg_tcpip_spi *)&spi, (struct mg_tcpip_driver_cyw_firmware *)&fw, false};
int main(void) {
// initialize stdio
@@ -120,7 +109,9 @@ int main(void) {
hwspecific_spi_init();
state = d.apmode ? AP : CONNECTING;
d.wifi.apip = MG_IPV4(192, 168, 169, 1),
d.wifi.apmask = MG_IPV4(255, 255, 255, 0),
state = d.wifi.apmode ? AP : CONNECTING;
struct mg_mgr mgr; // Initialise Mongoose event manager
mg_mgr_init(&mgr); // and attach it to the interface
@@ -159,7 +159,7 @@ static inline void spi_init(struct spi *spi) {
}
// Send a byte, and return a received byte
static inline uint8_t spi_txn(struct spi *spi, uint8_t write_byte) {
static inline uint8_t spi_1byte(struct spi *spi, uint8_t write_byte) {
unsigned count = spi->spin <= 0 ? 9 : (unsigned) spi->spin;
uint8_t rx = 0, tx = write_byte;
for (int i = 0; i < 8; i++) {
@@ -176,6 +176,14 @@ static inline uint8_t spi_txn(struct spi *spi, uint8_t write_byte) {
return rx; // Return the received byte
}
static inline void spi_txn(struct spi *spi, uint8_t *w, uint8_t *r, size_t n) {
while (n--) {
uint8_t c = (w != NULL) ? *w++ : 0xFF;
uint8_t d = spi_1byte(spi, c);
if (r != NULL) *r++ = d;
}
}
#define UUID ((uint32_t *) UID_BASE) // Unique 96-bit chip ID. TRM 59.1
// Helper macro for MAC generation, byte reads not allowed
@@ -86,7 +86,7 @@ int main(void) {
struct mg_tcpip_spi spi = {
.begin = (void (*)(void *)) spi_begin,
.end = (void (*)(void *)) spi_end,
.txn = (uint8_t(*)(void *, uint8_t)) spi_txn,
.txn = (void (*)(void *, uint8_t *, uint8_t *, size_t)) spi_txn,
.spi = &spi_pins,
};
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
@@ -66,7 +66,7 @@ static void hwspecific_spi_init(void) {
mg_delayms(50);
}
static const struct mg_tcpip_spi_ spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
static const struct mg_tcpip_spi spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
#ifndef CYW43_RESOURCE_ATTRIBUTE
#define CYW43_RESOURCE_ATTRIBUTE
@@ -162,7 +162,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
static struct mg_tcpip_driver_cyw_data d = {
{WIFI_SSID, WIFI_PASS, "mongoose", "mongoose", 0, 0, 0, 0, 10, true}, (struct mg_tcpip_spi_ *)&spi, (struct mg_tcpip_driver_cyw_firmware *)&fw, false};
{WIFI_SSID, WIFI_PASS, "mongoose", "mongoose", 0, 0, 0, 0, 10, true}, (struct mg_tcpip_spi *)&spi, (struct mg_tcpip_driver_cyw_firmware *)&fw, false};
int main(void) {
uart_init(UART_DEBUG, 115200); // Initialise debug printf
@@ -66,7 +66,7 @@ static void hwspecific_spi_init(void) {
mg_delayms(50);
}
static const struct mg_tcpip_spi_ spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
static const struct mg_tcpip_spi spi = {NULL, hwspecific_spi_begin, hwspecific_spi_end, hwspecific_spi_txn};
#ifndef CYW43_RESOURCE_ATTRIBUTE
#define CYW43_RESOURCE_ATTRIBUTE
@@ -162,7 +162,7 @@ static void mif_fn(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
static struct mg_tcpip_driver_cyw_data d = {
{WIFI_SSID, WIFI_PASS, "mongoose", "mongoose", 0, 0, 0, 0, 10, true}, (struct mg_tcpip_spi_ *)&spi, (struct mg_tcpip_driver_cyw_firmware *)&fw, false};
{WIFI_SSID, WIFI_PASS, "mongoose", "mongoose", 0, 0, 0, 0, 10, true}, (struct mg_tcpip_spi *)&spi, (struct mg_tcpip_driver_cyw_firmware *)&fw, false};
int main(void) {
uart_init(UART_DEBUG, 115200); // Initialise debug printf