From 87abafb13f46afd5aca19a538c1bd14ff272f1e2 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 30 Jan 2024 11:13:09 +0100 Subject: [PATCH 1/2] drivers/periph_spi: Add spi_transfer_u16_be() This adds a helper function to exchange a 16 bit number that is to be transferred in big endian order on the wire. --- drivers/include/periph/spi.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/drivers/include/periph/spi.h b/drivers/include/periph/spi.h index 85d7d6d91f35..98680671eece 100644 --- a/drivers/include/periph/spi.h +++ b/drivers/include/periph/spi.h @@ -66,8 +66,8 @@ #ifndef PERIPH_SPI_H #define PERIPH_SPI_H +#include #include -#include #include #include #include @@ -430,6 +430,25 @@ uint8_t spi_transfer_reg(spi_t bus, spi_cs_t cs, uint8_t reg, uint8_t out); void spi_transfer_regs(spi_t bus, spi_cs_t cs, uint8_t reg, const void *out, void *in, size_t len); +/** + * @brief Transfer a 16 bit number in big endian byte order + * + * @param[in] bus SPI device to use + * @param[in] cs chip select pin/line to use, set to + * SPI_CS_UNDEF if chip select should not be + * handled by the SPI driver + * @param[in] cont if true, keep device selected after transfer + * @param[in] host_number number to transfer in host byte order + * @return The 16 bit number received in host byte order + */ +static inline uint16_t spi_transfer_u16_be(spi_t bus, spi_cs_t cs, bool cont, uint16_t host_number) +{ + const uint16_t send = htobe16(host_number); + uint16_t receive; + spi_transfer_bytes(bus, cs, cont, &send, &receive, sizeof(receive)); + return be16toh(receive); +} + #ifdef __cplusplus } #endif From 47280b3a9b1605e6acc415488057a45d85926423 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Tue, 30 Jan 2024 11:14:24 +0100 Subject: [PATCH 2/2] drivers/w5100: make use of spi_transfer_u16_be() This also fixes a bug, as the free coded byte swap code was incorrect. --- drivers/w5100/w5100.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/w5100/w5100.c b/drivers/w5100/w5100.c index 50be84546029..8ca58b8e5d11 100644 --- a/drivers/w5100/w5100.c +++ b/drivers/w5100/w5100.c @@ -47,13 +47,7 @@ static const netdev_driver_t netdev_driver_w5100; static inline void send_addr(w5100_t *dev, uint16_t addr) { -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - spi_transfer_byte(dev->p.spi, dev->p.cs, true, (addr >> 8)); - spi_transfer_byte(dev->p.spi, dev->p.cs, true, (addr & 0xff)); -#else - spi_transfer_byte(dev->p.spi, dev->p.cs, true, (addr & 0xff)); - spi_transfer_byte(dev->p.spi, dev->p.cs, true, (addr >> 8)); -#endif + spi_transfer_u16_be(dev->p.spi, dev->p.cs, true, addr); } static uint8_t rreg(w5100_t *dev, uint16_t reg)