Skip to content

Commit 0bb7ccb

Browse files
decsnykartben
authored andcommitted
drivers: spi_context: Functions for remaining len
Add functions to get remaining length in the transfer. Refactor a bit to avoid duplicate code for the for loop that is the same as in the total length function. The difference between the total length and left length function is that the current buffer total length is counted in the former and the current buffer remaining length is counted in the latter. Signed-off-by: Declan Snyder <[email protected]>
1 parent 9966d29 commit 0bb7ccb

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

drivers/spi/spi_context.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,30 +437,51 @@ static inline size_t spi_context_longest_current_buf(struct spi_context *ctx)
437437
return ctx->tx_len > ctx->rx_len ? ctx->tx_len : ctx->rx_len;
438438
}
439439

440-
static inline size_t spi_context_total_tx_len(struct spi_context *ctx)
440+
static size_t spi_context_count_tx_buf_lens(struct spi_context *ctx, size_t start_index)
441441
{
442442
size_t n;
443443
size_t total_len = 0;
444444

445-
for (n = 0; n < ctx->tx_count; ++n) {
445+
for (n = start_index; n < ctx->tx_count; ++n) {
446446
total_len += ctx->current_tx[n].len;
447447
}
448448

449449
return total_len;
450450
}
451451

452-
static inline size_t spi_context_total_rx_len(struct spi_context *ctx)
452+
static size_t spi_context_count_rx_buf_lens(struct spi_context *ctx, size_t start_index)
453453
{
454454
size_t n;
455455
size_t total_len = 0;
456456

457-
for (n = 0; n < ctx->rx_count; ++n) {
457+
for (n = start_index; n < ctx->rx_count; ++n) {
458458
total_len += ctx->current_rx[n].len;
459459
}
460460

461461
return total_len;
462462
}
463463

464+
465+
static inline size_t spi_context_total_tx_len(struct spi_context *ctx)
466+
{
467+
return spi_context_count_tx_buf_lens(ctx, 0);
468+
}
469+
470+
static inline size_t spi_context_total_rx_len(struct spi_context *ctx)
471+
{
472+
return spi_context_count_rx_buf_lens(ctx, 0);
473+
}
474+
475+
static inline size_t spi_context_tx_len_left(struct spi_context *ctx)
476+
{
477+
return ctx->tx_len + spi_context_count_tx_buf_lens(ctx, 1);
478+
}
479+
480+
static inline size_t spi_context_rx_len_left(struct spi_context *ctx)
481+
{
482+
return ctx->rx_len + spi_context_count_rx_buf_lens(ctx, 1);
483+
}
484+
464485
#ifdef __cplusplus
465486
}
466487
#endif

0 commit comments

Comments
 (0)