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

@@ -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