Lines Matching refs:ctx

78 static inline bool spi_context_configured(struct spi_context *ctx,  in spi_context_configured()  argument
81 return !!(ctx->config == config); in spi_context_configured()
84 static inline bool spi_context_is_slave(struct spi_context *ctx) in spi_context_is_slave() argument
86 return (ctx->config->operation & SPI_OP_MODE_SLAVE); in spi_context_is_slave()
89 static inline void spi_context_lock(struct spi_context *ctx, in spi_context_lock() argument
96 (k_sem_count_get(&ctx->lock) == 0) && in spi_context_lock()
97 (ctx->owner == spi_cfg)) { in spi_context_lock()
101 k_sem_take(&ctx->lock, K_FOREVER); in spi_context_lock()
102 ctx->owner = spi_cfg; in spi_context_lock()
105 ctx->asynchronous = asynchronous; in spi_context_lock()
106 ctx->callback = callback; in spi_context_lock()
107 ctx->callback_data = callback_data; in spi_context_lock()
111 static inline void spi_context_release(struct spi_context *ctx, int status) in spi_context_release() argument
114 if (status >= 0 && (ctx->config->operation & SPI_LOCK_ON)) { in spi_context_release()
120 if (!ctx->asynchronous || (status < 0)) { in spi_context_release()
121 ctx->owner = NULL; in spi_context_release()
122 k_sem_give(&ctx->lock); in spi_context_release()
125 if (!(ctx->config->operation & SPI_LOCK_ON)) { in spi_context_release()
126 ctx->owner = NULL; in spi_context_release()
127 k_sem_give(&ctx->lock); in spi_context_release()
132 static inline size_t spi_context_total_tx_len(struct spi_context *ctx);
133 static inline size_t spi_context_total_rx_len(struct spi_context *ctx);
135 static inline int spi_context_wait_for_completion(struct spi_context *ctx) in spi_context_wait_for_completion() argument
141 wait = !ctx->asynchronous; in spi_context_wait_for_completion()
153 if (IS_ENABLED(CONFIG_SPI_SLAVE) && spi_context_is_slave(ctx)) { in spi_context_wait_for_completion()
156 uint32_t tx_len = spi_context_total_tx_len(ctx); in spi_context_wait_for_completion()
157 uint32_t rx_len = spi_context_total_rx_len(ctx); in spi_context_wait_for_completion()
161 ctx->config->frequency; in spi_context_wait_for_completion()
167 if (k_sem_take(&ctx->sync, timeout)) { in spi_context_wait_for_completion()
171 status = ctx->sync_status; in spi_context_wait_for_completion()
175 if (spi_context_is_slave(ctx) && !status) { in spi_context_wait_for_completion()
176 return ctx->recv_frames; in spi_context_wait_for_completion()
183 static inline void spi_context_complete(struct spi_context *ctx, in spi_context_complete() argument
188 if (!ctx->asynchronous) { in spi_context_complete()
189 ctx->sync_status = status; in spi_context_complete()
190 k_sem_give(&ctx->sync); in spi_context_complete()
192 if (ctx->callback) { in spi_context_complete()
194 if (spi_context_is_slave(ctx) && !status) { in spi_context_complete()
198 status = ctx->recv_frames; in spi_context_complete()
201 ctx->callback(dev, status, ctx->callback_data); in spi_context_complete()
204 if (!(ctx->config->operation & SPI_LOCK_ON)) { in spi_context_complete()
205 ctx->owner = NULL; in spi_context_complete()
206 k_sem_give(&ctx->lock); in spi_context_complete()
210 ctx->sync_status = status; in spi_context_complete()
211 k_sem_give(&ctx->sync); in spi_context_complete()
215 static inline int spi_context_cs_configure_all(struct spi_context *ctx) in spi_context_cs_configure_all() argument
220 for (cs_gpio = ctx->cs_gpios; cs_gpio < &ctx->cs_gpios[ctx->num_cs_gpios]; cs_gpio++) { in spi_context_cs_configure_all()
236 static inline void _spi_context_cs_control(struct spi_context *ctx, in _spi_context_cs_control() argument
239 if (ctx->config && spi_cs_is_gpio(ctx->config)) { in _spi_context_cs_control()
241 gpio_pin_set_dt(&ctx->config->cs.gpio, 1); in _spi_context_cs_control()
242 k_busy_wait(ctx->config->cs.delay); in _spi_context_cs_control()
245 ctx->config->operation & SPI_HOLD_ON_CS) { in _spi_context_cs_control()
249 k_busy_wait(ctx->config->cs.delay); in _spi_context_cs_control()
250 gpio_pin_set_dt(&ctx->config->cs.gpio, 0); in _spi_context_cs_control()
255 static inline void spi_context_cs_control(struct spi_context *ctx, bool on) in spi_context_cs_control() argument
257 _spi_context_cs_control(ctx, on, false); in spi_context_cs_control()
260 static inline void spi_context_unlock_unconditionally(struct spi_context *ctx) in spi_context_unlock_unconditionally() argument
263 _spi_context_cs_control(ctx, false, true); in spi_context_unlock_unconditionally()
265 if (!k_sem_count_get(&ctx->lock)) { in spi_context_unlock_unconditionally()
266 ctx->owner = NULL; in spi_context_unlock_unconditionally()
267 k_sem_give(&ctx->lock); in spi_context_unlock_unconditionally()
291 void spi_context_buffers_setup(struct spi_context *ctx, in spi_context_buffers_setup() argument
298 ctx->current_tx = tx_bufs ? tx_bufs->buffers : NULL; in spi_context_buffers_setup()
299 ctx->tx_count = ctx->current_tx ? tx_bufs->count : 0; in spi_context_buffers_setup()
300 ctx->tx_buf = (const uint8_t *) in spi_context_buffers_setup()
301 spi_context_get_next_buf(&ctx->current_tx, &ctx->tx_count, in spi_context_buffers_setup()
302 &ctx->tx_len, dfs); in spi_context_buffers_setup()
304 ctx->current_rx = rx_bufs ? rx_bufs->buffers : NULL; in spi_context_buffers_setup()
305 ctx->rx_count = ctx->current_rx ? rx_bufs->count : 0; in spi_context_buffers_setup()
306 ctx->rx_buf = (uint8_t *) in spi_context_buffers_setup()
307 spi_context_get_next_buf(&ctx->current_rx, &ctx->rx_count, in spi_context_buffers_setup()
308 &ctx->rx_len, dfs); in spi_context_buffers_setup()
310 ctx->sync_status = 0; in spi_context_buffers_setup()
313 ctx->recv_frames = 0; in spi_context_buffers_setup()
318 ctx->current_tx, ctx->tx_count, in spi_context_buffers_setup()
319 ctx->current_rx, ctx->rx_count, in spi_context_buffers_setup()
320 (void *)ctx->tx_buf, ctx->tx_len, in spi_context_buffers_setup()
321 (void *)ctx->rx_buf, ctx->rx_len); in spi_context_buffers_setup()
325 void spi_context_update_tx(struct spi_context *ctx, uint8_t dfs, uint32_t len) in spi_context_update_tx() argument
327 if (!ctx->tx_len) { in spi_context_update_tx()
331 if (len > ctx->tx_len) { in spi_context_update_tx()
336 ctx->tx_len -= len; in spi_context_update_tx()
337 if (!ctx->tx_len) { in spi_context_update_tx()
339 ++ctx->current_tx; in spi_context_update_tx()
340 --ctx->tx_count; in spi_context_update_tx()
341 ctx->tx_buf = (const uint8_t *) in spi_context_update_tx()
342 spi_context_get_next_buf(&ctx->current_tx, in spi_context_update_tx()
343 &ctx->tx_count, in spi_context_update_tx()
344 &ctx->tx_len, dfs); in spi_context_update_tx()
345 } else if (ctx->tx_buf) { in spi_context_update_tx()
346 ctx->tx_buf += dfs * len; in spi_context_update_tx()
349 LOG_DBG("tx buf/len %p/%zu", (void *)ctx->tx_buf, ctx->tx_len); in spi_context_update_tx()
353 bool spi_context_tx_on(struct spi_context *ctx) in spi_context_tx_on() argument
355 return !!(ctx->tx_len); in spi_context_tx_on()
359 bool spi_context_tx_buf_on(struct spi_context *ctx) in spi_context_tx_buf_on() argument
361 return !!(ctx->tx_buf && ctx->tx_len); in spi_context_tx_buf_on()
365 void spi_context_update_rx(struct spi_context *ctx, uint8_t dfs, uint32_t len) in spi_context_update_rx() argument
368 if (spi_context_is_slave(ctx)) { in spi_context_update_rx()
369 ctx->recv_frames += len; in spi_context_update_rx()
374 if (!ctx->rx_len) { in spi_context_update_rx()
378 if (len > ctx->rx_len) { in spi_context_update_rx()
383 ctx->rx_len -= len; in spi_context_update_rx()
384 if (!ctx->rx_len) { in spi_context_update_rx()
386 ++ctx->current_rx; in spi_context_update_rx()
387 --ctx->rx_count; in spi_context_update_rx()
388 ctx->rx_buf = (uint8_t *) in spi_context_update_rx()
389 spi_context_get_next_buf(&ctx->current_rx, in spi_context_update_rx()
390 &ctx->rx_count, in spi_context_update_rx()
391 &ctx->rx_len, dfs); in spi_context_update_rx()
392 } else if (ctx->rx_buf) { in spi_context_update_rx()
393 ctx->rx_buf += dfs * len; in spi_context_update_rx()
396 LOG_DBG("rx buf/len %p/%zu", (void *)ctx->rx_buf, ctx->rx_len); in spi_context_update_rx()
400 bool spi_context_rx_on(struct spi_context *ctx) in spi_context_rx_on() argument
402 return !!(ctx->rx_len); in spi_context_rx_on()
406 bool spi_context_rx_buf_on(struct spi_context *ctx) in spi_context_rx_buf_on() argument
408 return !!(ctx->rx_buf && ctx->rx_len); in spi_context_rx_buf_on()
416 static inline size_t spi_context_max_continuous_chunk(struct spi_context *ctx) in spi_context_max_continuous_chunk() argument
418 if (!ctx->tx_len) { in spi_context_max_continuous_chunk()
419 return ctx->rx_len; in spi_context_max_continuous_chunk()
420 } else if (!ctx->rx_len) { in spi_context_max_continuous_chunk()
421 return ctx->tx_len; in spi_context_max_continuous_chunk()
424 return MIN(ctx->tx_len, ctx->rx_len); in spi_context_max_continuous_chunk()
427 static inline size_t spi_context_longest_current_buf(struct spi_context *ctx) in spi_context_longest_current_buf() argument
429 return ctx->tx_len > ctx->rx_len ? ctx->tx_len : ctx->rx_len; in spi_context_longest_current_buf()
432 static inline size_t spi_context_total_tx_len(struct spi_context *ctx) in spi_context_total_tx_len() argument
437 for (n = 0; n < ctx->tx_count; ++n) { in spi_context_total_tx_len()
438 total_len += ctx->current_tx[n].len; in spi_context_total_tx_len()
444 static inline size_t spi_context_total_rx_len(struct spi_context *ctx) in spi_context_total_rx_len() argument
449 for (n = 0; n < ctx->rx_count; ++n) { in spi_context_total_rx_len()
450 total_len += ctx->current_rx[n].len; in spi_context_total_rx_len()