Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/periph_spi: Add spi_transfer_u16_be() #20312

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion drivers/include/periph/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
#ifndef PERIPH_SPI_H
#define PERIPH_SPI_H

#include <endian.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions drivers/w5100/w5100.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading