spi_write_then_read(9)
π« spi_write_then_read (kernel API manual, section 9) π«
okay bestie, so this whole page is from the Kernel API Manual, section 9, and it's literally about one function that writes a thing then reads a thing. iconic behavior honestly.
β¨ NAME β¨
spi_write_then_read β SPI synchronous write followed by read π¦
πΈ SYNOPSIS πΈ
int spi_write_then_read (struct spi_device *spi , const void *txbuf ,
unsigned n_tx , void *rxbuf , unsigned n_rx );
π ARGUMENTS π
spiβ the device you're exchanging data with, periodt.txbufβ the data that's getting written out. heads up: it does not need to be DMA-safe. ngl that's a whole relief.n_txβ the size oftxbuf, counted in bytes.rxbufβ the buffer where the data you read lands. also not required to be DMA-safe πn_rxβ the size ofrxbuf, in bytes.
π CONTEXT π
can sleep π΄ (yes, literally. this one's allowed to nap.)
π DESCRIPTION π
so what this does is a half duplex MicroWire style transaction with the device β it sends txbuf and then reads rxbuf. that's the whole arc, no cap.
The return value is zero for success, else a negative errno status code. and the vibe check on where you can call it: this call may only be used from a context that may sleep. so don't be out here calling it from somewhere spicy that can't sleep, fr fr.
Parameters to this routine are always copied using a small buffer. that's the tea. so if you're doing performance-sensitive or bulk transfer code, don't use this one β use spi_{async,sync}() calls with DMA-safe buffers instead. it's giving "right tool for the right job" β¨
β RETURN β
zero on success, else a negative error code. simple as that π
π SEE ALSO π
Kernel file ./drivers/spi/spi.c plus this whole glorious friend group:
__spi_register_driver(9)spi_alloc_device(9)spi_add_device(9)spi_new_device(9)spi_unregister_device(9)spi_register_board_info(9)spi_res_alloc(9)spi_res_free(9)spi_res_add(9)spi_res_release(9)spi_finalize_current_transfer(9)__spi_pump_messages(9)spi_pump_messages(9)spi_take_timestamp_pre(9)spi_take_timestamp_post(9)spi_set_thread_rt(9)spi_get_next_queued_message(9)spi_finalize_current_message(9)spi_queued_transfer(9)spi_flush_queue(9)of_register_spi_devices(9)spi_new_ancillary_device(9)devm_spi_new_ancillary_device(9)acpi_spi_count_resources(9)acpi_spi_device_alloc(9)spi_target_abort(9)__spi_alloc_controller(9)__devm_spi_alloc_controller(9)spi_get_gpio_descs(9)spi_register_controller(9)devm_spi_register_controller(9)spi_unregister_controller(9)spi_replace_transfers(9)spi_split_transfers_maxsize(9)spi_split_transfers_maxwords(9)spi_set_cs_timing(9)spi_setup(9)spi_optimize_message(9)spi_unoptimize_message(9)devm_spi_optimize_message(9)spi_async(9)spi_sync(9)spi_sync_locked(9)spi_bus_lock(9)spi_bus_unlock(9)
anyway that's the page π«Ά it lives over in ./drivers/spi, dated October 2026, and it's filed under spi_write_then_read(9) β mwah, go read some SPI, ily π
π
π¦ where this page comes from
- nixpkgs unstable: linux-manual 7.3.1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms