Skip to content

drivers/spi: Properly check for rx/tx and buffering on #5785

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

Merged
merged 1 commit into from
Jan 30, 2018
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
16 changes: 14 additions & 2 deletions drivers/spi/spi_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,13 @@ void spi_context_update_tx(struct spi_context *ctx, u8_t dfs, u32_t len)
static ALWAYS_INLINE
bool spi_context_tx_on(struct spi_context *ctx)
{
return !!(ctx->tx_buf || ctx->tx_len);
return !!(ctx->tx_len);
}

static ALWAYS_INLINE
bool spi_context_tx_buf_on(struct spi_context *ctx)
{
return !!(ctx->tx_buf && ctx->tx_len);
}

static ALWAYS_INLINE
Expand Down Expand Up @@ -263,7 +269,13 @@ void spi_context_update_rx(struct spi_context *ctx, u8_t dfs, u32_t len)
static ALWAYS_INLINE
bool spi_context_rx_on(struct spi_context *ctx)
{
return !!(ctx->rx_buf || ctx->rx_len);
return !!(ctx->rx_len);
}

static ALWAYS_INLINE
bool spi_context_rx_buf_on(struct spi_context *ctx)
{
return !!(ctx->rx_buf && ctx->rx_len);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tbursztyka this might break the stm32 driver. Inside the shift-next-byte loop, the stm32 does the following:

data = read byte
if spi_context_rx_on(...) {
   *ctx.rx_buf = data
   spi_context_update_rx(...)
}

With this change, the above code will fail when rx_buf.buf = NULL and rx_buf.len > 0.

WDYT about documenting the spi_context API then we can go through and fix the current users?

Copy link
Collaborator

@ydamigos ydamigos Jan 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have already pushed a commit to fix it in PR #5413.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

}

static inline size_t spi_context_longest_current_buf(struct spi_context *ctx)
Expand Down
4 changes: 2 additions & 2 deletions drivers/spi/spi_dw.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static void push_data(struct device *dev)
}

while (f_tx) {
if (spi_context_tx_on(&spi->ctx)) {
if (spi_context_tx_buf_on(&spi->ctx)) {
switch (spi->dfs) {
case 1:
data = UNALIGNED_GET((u8_t *)
Expand Down Expand Up @@ -155,7 +155,7 @@ static void pull_data(struct device *dev)

DBG_COUNTER_INC();

if (spi_context_rx_on(&spi->ctx)) {
if (spi_context_rx_buf_on(&spi->ctx)) {
switch (spi->dfs) {
case 1:
UNALIGNED_PUT(data, (u8_t *)spi->ctx.rx_buf);
Expand Down