Skip to content

Commit f104fad

Browse files
author
Tomasz Bursztyka
committed
api/spi: Slave transactions will return received frames on success
Unlike master mode which will always return 0 on success. Signed-off-by: Tomasz Bursztyka <[email protected]>
1 parent d52563c commit f104fad

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

drivers/spi/spi_context.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ struct spi_context {
4444
size_t tx_len;
4545
u8_t *rx_buf;
4646
size_t rx_len;
47+
48+
#ifdef CONFIG_SPI_SLAVE
49+
int recv_frames;
50+
#endif /* CONFIG_SPI_SLAVE */
4751
};
4852

4953
#define SPI_CONTEXT_INIT_LOCK(_data, _ctx_name) \
@@ -77,10 +81,12 @@ static inline void spi_context_lock(struct spi_context *ctx,
7781

7882
static inline void spi_context_release(struct spi_context *ctx, int status)
7983
{
80-
if (!status &&
84+
#ifdef CONFIG_SPI_SLAVE
85+
if (status >= 0 &&
8186
(ctx->config->operation & (SPI_LOCK_ON | SPI_OP_MODE_SLAVE))) {
8287
return;
8388
}
89+
#endif /* CONFIG_SPI_SLAVE */
8490

8591
#ifdef CONFIG_SPI_ASYNC
8692
if (!ctx->asynchronous || status) {
@@ -110,6 +116,13 @@ static inline int spi_context_wait_for_completion(struct spi_context *ctx)
110116
k_sem_take(&ctx->sync, K_FOREVER);
111117
status = ctx->sync_status;
112118
#endif /* CONFIG_SPI_ASYNC */
119+
120+
#ifdef CONFIG_SPI_SLAVE
121+
if (spi_context_is_slave(ctx) && !status) {
122+
return ctx->recv_frames;
123+
}
124+
#endif /* CONFIG_SPI_SLAVE */
125+
113126
return status;
114127
}
115128

@@ -221,6 +234,10 @@ void spi_context_buffers_setup(struct spi_context *ctx,
221234

222235
ctx->sync_status = 0;
223236

237+
#ifdef CONFIG_SPI_SLAVE
238+
ctx->recv_frames = 0;
239+
#endif /* CONFIG_SPI_SLAVE */
240+
224241
SYS_LOG_DBG("current_tx %p (%zu), current_rx %p (%zu),"
225242
" tx buf/len %p/%zu, rx buf/len %p/%zu",
226243
ctx->current_tx, ctx->tx_count,
@@ -272,6 +289,13 @@ bool spi_context_tx_buf_on(struct spi_context *ctx)
272289
static ALWAYS_INLINE
273290
void spi_context_update_rx(struct spi_context *ctx, u8_t dfs, u32_t len)
274291
{
292+
#ifdef CONFIG_SPI_SLAVE
293+
if (spi_context_is_slave(ctx)) {
294+
ctx->recv_frames += len;
295+
}
296+
297+
#endif /* CONFIG_SPI_SLAVE */
298+
275299
if (!ctx->rx_len) {
276300
return;
277301
}

include/spi.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,9 @@ struct spi_driver_api {
244244
* @param rx_bufs Buffer array where data to be read will be written to,
245245
* or NULL if none.
246246
*
247-
* @retval 0 If successful, negative errno code otherwise.
247+
* @retval 0 If successful, negative errno code otherwise. In case of slave
248+
* transaction: if successful it will return the amount of frames
249+
* received, negative errno code otherwise.
248250
*/
249251
__syscall int spi_transceive(struct device *dev,
250252
const struct spi_config *config,
@@ -318,7 +320,9 @@ static inline int spi_write(struct device *dev,
318320
* notify the end of the transaction, and whether it went
319321
* successfully or not).
320322
*
321-
* @retval 0 If successful, negative errno code otherwise.
323+
* @retval 0 If successful, negative errno code otherwise. In case of slave
324+
* transaction: if successful it will return the amount of frames
325+
* received, negative errno code otherwise.
322326
*/
323327
static inline int spi_transceive_async(struct device *dev,
324328
const struct spi_config *config,

0 commit comments

Comments
 (0)