|
idfxx 1.0.0
Modern C++23 components for ESP-IDF
|
A device on a SPI master bus. More...
Classes | |
| struct | config |
| SPI device configuration. More... | |
Public Member Functions | |
| master_device (master_bus &bus, const struct config &config) | |
| Creates a new SPI device on the specified bus. | |
| ~master_device () | |
| master_device (const master_device &)=delete | |
| master_device & | operator= (const master_device &)=delete |
| master_device (master_device &&other) noexcept | |
| master_device & | operator= (master_device &&other) noexcept |
| master_bus & | bus () const |
| Returns the parent bus. | |
| spi_device_handle_t | idf_handle () const |
| Returns the underlying ESP-IDF device handle. | |
| freq::hertz | frequency () const |
| Returns the actual clock frequency in use. | |
| void | transmit (std::span< const uint8_t > tx_data) |
| Transmits data to the device. | |
| void | receive (std::span< uint8_t > rx_data) |
| Receives data from the device into the provided buffer. | |
| std::vector< uint8_t > | receive (size_t size) |
| Receives data from the device. | |
| void | transfer (std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data) |
| Performs a full-duplex transfer. | |
| result< void > | try_transmit (std::span< const uint8_t > tx_data) |
| Transmits data to the device. | |
| result< void > | try_receive (std::span< uint8_t > rx_data) |
| Receives data from the device into the provided buffer. | |
| result< std::vector< uint8_t > > | try_receive (size_t size) |
| Receives data from the device. | |
| result< void > | try_transfer (std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data) |
| Performs a full-duplex transfer. | |
| void | transmit (const transaction &trans) |
| Executes a full-control transaction. | |
| result< void > | try_transmit (const transaction &trans) |
| Executes a full-control transaction. | |
| void | polling_transmit (std::span< const uint8_t > tx_data) |
| Transmits data using polling (busy-wait). | |
| void | polling_receive (std::span< uint8_t > rx_data) |
| Receives data using polling (busy-wait). | |
| std::vector< uint8_t > | polling_receive (size_t size) |
| Receives data using polling (busy-wait). | |
| void | polling_transfer (std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data) |
| Performs a full-duplex transfer using polling (busy-wait). | |
| void | polling_transmit (const transaction &trans) |
| Executes a full-control transaction using polling (busy-wait). | |
| result< void > | try_polling_transmit (std::span< const uint8_t > tx_data) |
| Transmits data using polling (busy-wait). | |
| result< void > | try_polling_receive (std::span< uint8_t > rx_data) |
| Receives data using polling (busy-wait). | |
| result< std::vector< uint8_t > > | try_polling_receive (size_t size) |
| Receives data using polling (busy-wait). | |
| result< void > | try_polling_transfer (std::span< const uint8_t > tx_data, std::span< uint8_t > rx_data) |
| Performs a full-duplex transfer using polling (busy-wait). | |
| result< void > | try_polling_transmit (const transaction &trans) |
| Executes a full-control transaction using polling (busy-wait). | |
| idfxx::future< void > | queue_trans (const transaction &trans) |
| Queues a transaction for asynchronous execution. | |
| template<typename Rep , typename Period > | |
| idfxx::future< void > | queue_trans (const transaction &trans, const std::chrono::duration< Rep, Period > &timeout) |
| Queues a transaction for asynchronous execution with timeout. | |
| result< idfxx::future< void > > | try_queue_trans (const transaction &trans) |
| Queues a transaction for asynchronous execution. | |
| template<typename Rep , typename Period > | |
| result< idfxx::future< void > > | try_queue_trans (const transaction &trans, const std::chrono::duration< Rep, Period > &timeout) |
| Queues a transaction for asynchronous execution with timeout. | |
| void | lock () const |
| Acquires exclusive access to this device and its bus. | |
| bool | try_lock () const noexcept |
| Tries to acquire exclusive access without waiting for the device. | |
| void | unlock () const |
| Releases exclusive access acquired by lock() or try_lock(). | |
Static Public Member Functions | |
| static result< master_device > | make (master_bus &bus, const struct config &config) |
| Creates a new SPI device on the specified bus. | |
A device on a SPI master bus.
Represents a device attached to a SPI bus. Provides byte-oriented transmit/receive helpers, a full-control transaction API, a polling (busy-wait) variant for low-latency transfers, and an asynchronous queue API for overlapping transfers with other work.
All transaction APIs are thread-safe and may be called concurrently from multiple tasks; individual polling calls are serialized against each other and against lock(). To keep a multi-transaction sequence atomic, hold the device lock (e.g. via std::lock_guard) across the sequence.
Satisfies the Lockable named requirement, so it can be used directly with std::lock_guard and similar standard RAII wrappers. Locking provides mutual exclusion between threads sharing this device and holds exclusive access to the underlying bus for the duration. The lock is recursive: a thread may re-lock a device it already holds.
This type is non-copyable and move-only. The caller must ensure the parent master_bus outlives this device. A moved-from object must not be used: any operation other than destruction or assignment is undefined behavior.
Definition at line 340 of file master.hpp.
|
explicit |
Creates a new SPI device on the specified bus.
Does not take ownership of bus. The caller must ensure that this device does not outlive the bus.
| bus | The parent SPI master bus. |
| config | Device configuration. |
| std::system_error | on failure. |
| idfxx::spi::master_device::~master_device | ( | ) |
|
delete |
|
noexcept |
|
inline |
Returns the parent bus.
Definition at line 430 of file master.hpp.
| freq::hertz idfxx::spi::master_device::frequency | ( | ) | const |
Returns the actual clock frequency in use.
The requested config::clock_speed may not always be exactly achievable; this returns the closest frequency the hardware is running at.
|
inline |
Returns the underlying ESP-IDF device handle.
Definition at line 436 of file master.hpp.
| void idfxx::spi::master_device::lock | ( | ) | const |
Acquires exclusive access to this device and its bus.
Provides mutual exclusion against other threads locking or issuing polling transactions on this device. While the lock is held, transactions to all other devices on the same bus are also deferred. Blocks indefinitely until the device and bus are available.
The lock is recursive: a thread may lock a device it already holds. Each lock() must be balanced by a matching unlock().
| std::system_error | if the driver rejects bus acquisition (only possible when a polling transaction was started through the raw idf_handle(), bypassing this API). When CONFIG_COMPILER_CXX_EXCEPTIONS is disabled, this aborts instead. |
|
static |
Creates a new SPI device on the specified bus.
Does not take ownership of bus. The caller must ensure that this device does not outlive the bus.
| bus | The parent SPI master bus. |
| config | Device configuration. |
|
delete |
|
noexcept |
|
inline |
Receives data using polling (busy-wait).
| size | Number of bytes to receive. |
| std::system_error | on failure. |
Definition at line 618 of file master.hpp.
References try_polling_receive(), and idfxx::unwrap().
|
inline |
Receives data using polling (busy-wait).
| rx_data | Buffer to receive into. |
| std::system_error | on failure. |
Definition at line 604 of file master.hpp.
References try_polling_receive(), and idfxx::unwrap().
|
inline |
Performs a full-duplex transfer using polling (busy-wait).
| tx_data | Data to transmit. |
| rx_data | Buffer for received data (must be same size as tx_data). |
| std::system_error | on failure. |
Definition at line 631 of file master.hpp.
References try_polling_transfer(), and idfxx::unwrap().
|
inline |
Executes a full-control transaction using polling (busy-wait).
| trans | Transaction descriptor (lengths in bits). |
| std::system_error | on failure. |
Definition at line 645 of file master.hpp.
References try_polling_transmit(), and idfxx::unwrap().
|
inline |
Transmits data using polling (busy-wait).
| tx_data | Data to transmit. |
| std::system_error | on failure. |
Definition at line 592 of file master.hpp.
References try_polling_transmit(), and idfxx::unwrap().
|
inline |
Queues a transaction for asynchronous execution.
Submits trans to the device and returns a future that signals completion. Blocks indefinitely if the device's transaction queue is full; the queue depth is set by config::queue_size.
| trans | Transaction descriptor. |
| std::system_error | on failure. |
Definition at line 736 of file master.hpp.
References try_queue_trans(), and idfxx::unwrap().
|
inline |
Queues a transaction for asynchronous execution with timeout.
| Rep | Duration arithmetic type. |
| Period | Duration period type. |
| trans | Transaction descriptor. |
| timeout | Maximum time to wait for space in the transaction queue. |
| std::system_error | on failure or timeout. |
Definition at line 753 of file master.hpp.
References idfxx::timeout, try_queue_trans(), and idfxx::unwrap().
|
inline |
Receives data from the device.
| size | Number of bytes to receive. |
| std::system_error | on failure. |
Definition at line 493 of file master.hpp.
References try_receive(), and idfxx::unwrap().
|
inline |
Receives data from the device into the provided buffer.
| rx_data | Buffer to receive into. |
| std::system_error | on failure. |
Definition at line 477 of file master.hpp.
References try_receive(), and idfxx::unwrap().
|
inline |
Performs a full-duplex transfer.
| tx_data | Data to transmit. |
| rx_data | Buffer for received data (must be same size as tx_data). |
| std::system_error | on failure. |
Definition at line 510 of file master.hpp.
References try_transfer(), and idfxx::unwrap().
|
inline |
Executes a full-control transaction.
| trans | Transaction descriptor (lengths in bits). |
| std::system_error | on failure. |
Definition at line 565 of file master.hpp.
References try_transmit(), and idfxx::unwrap().
|
inline |
Transmits data to the device.
| tx_data | Data to transmit. |
| std::system_error | on failure. |
Definition at line 467 of file master.hpp.
References try_transmit(), and idfxx::unwrap().
|
noexcept |
Tries to acquire exclusive access without waiting for the device.
Returns false immediately if another thread holds the device lock. On success, exclusive bus access is also acquired, which may briefly block while in-flight transactions of other devices on the bus complete.
| result< std::vector< uint8_t > > idfxx::spi::master_device::try_polling_receive | ( | size_t | size | ) |
Receives data using polling (busy-wait).
| size | Number of bytes to receive. |
| result< void > idfxx::spi::master_device::try_polling_receive | ( | std::span< uint8_t > | rx_data | ) |
Receives data using polling (busy-wait).
| rx_data | Buffer to receive into. |
Referenced by polling_receive(), and polling_receive().
| result< void > idfxx::spi::master_device::try_polling_transfer | ( | std::span< const uint8_t > | tx_data, |
| std::span< uint8_t > | rx_data | ||
| ) |
Performs a full-duplex transfer using polling (busy-wait).
| tx_data | Data to transmit. |
| rx_data | Buffer for received data (must be same size as tx_data). |
Referenced by polling_transfer().
| result< void > idfxx::spi::master_device::try_polling_transmit | ( | const transaction & | trans | ) |
Executes a full-control transaction using polling (busy-wait).
| trans | Transaction descriptor (lengths in bits). |
| result< void > idfxx::spi::master_device::try_polling_transmit | ( | std::span< const uint8_t > | tx_data | ) |
Transmits data using polling (busy-wait).
| tx_data | Data to transmit. |
Referenced by polling_transmit(), and polling_transmit().
| result< idfxx::future< void > > idfxx::spi::master_device::try_queue_trans | ( | const transaction & | trans | ) |
Queues a transaction for asynchronous execution.
Submits trans to the device and returns a future that signals completion. Blocks indefinitely if the device's transaction queue is full; the queue depth is set by config::queue_size.
| trans | Transaction descriptor. |
Referenced by queue_trans(), and queue_trans().
|
inline |
Queues a transaction for asynchronous execution with timeout.
| Rep | Duration arithmetic type. |
| Period | Duration period type. |
| trans | Transaction descriptor. |
| timeout | Maximum time to wait for space in the transaction queue. |
Definition at line 789 of file master.hpp.
References idfxx::timeout.
| result< std::vector< uint8_t > > idfxx::spi::master_device::try_receive | ( | size_t | size | ) |
Receives data from the device.
| size | Number of bytes to receive. |
| result< void > idfxx::spi::master_device::try_receive | ( | std::span< uint8_t > | rx_data | ) |
| result< void > idfxx::spi::master_device::try_transfer | ( | std::span< const uint8_t > | tx_data, |
| std::span< uint8_t > | rx_data | ||
| ) |
Performs a full-duplex transfer.
| tx_data | Data to transmit. |
| rx_data | Buffer for received data (must be same size as tx_data). |
Referenced by transfer().
| result< void > idfxx::spi::master_device::try_transmit | ( | const transaction & | trans | ) |
Executes a full-control transaction.
| trans | Transaction descriptor (lengths in bits). |
| result< void > idfxx::spi::master_device::try_transmit | ( | std::span< const uint8_t > | tx_data | ) |
Transmits data to the device.
| tx_data | Data to transmit. |
Referenced by transmit(), and transmit().
| void idfxx::spi::master_device::unlock | ( | ) | const |
Releases exclusive access acquired by lock() or try_lock().
Bus access is released when the outermost recursive lock is unlocked.