Core interfaces for UART communication. Main docs: https://pigweed.dev/pw_uart.
|
StatusWithSize | pw::uart::Uart::ReadAtLeast (ByteSpan rx_buffer, size_t min_bytes) |
|
StatusWithSize | pw::uart::Uart::ReadExactly (ByteSpan rx_buffer) |
|
Status | pw::uart::Uart::Read (ByteSpan rx_buffer) |
|
StatusWithSize | pw::uart::Uart::TryReadAtLeastFor (ByteSpan rx_buffer, size_t min_bytes, chrono::SystemClock::duration timeout) |
|
StatusWithSize | pw::uart::Uart::TryReadExactlyFor (ByteSpan rx_buffer, chrono::SystemClock::duration timeout) |
|
StatusWithSize | pw::uart::Uart::TryReadFor (ByteSpan rx_buffer, chrono::SystemClock::duration timeout) |
|
Status | pw::uart::Uart::Write (ConstByteSpan tx_buffer) |
|
StatusWithSize | pw::uart::Uart::TryWriteFor (ConstByteSpan tx_buffer, chrono::SystemClock::duration timeout) |
|
Status | pw::uart::Uart::FlushOutput () |
|
virtual StatusWithSize | pw::uart::Uart::DoTryReadFor (ByteSpan rx_buffer, std::optional< chrono::SystemClock::duration > timeout) |
|
virtual StatusWithSize | pw::uart::Uart::DoTryReadFor (ByteSpan, size_t, std::optional< chrono::SystemClock::duration >) |
|
virtual StatusWithSize | pw::uart::Uart::DoTryWriteFor (ConstByteSpan tx_buffer, std::optional< chrono::SystemClock::duration > timeout)=0 |
| Writes data from a provided buffer to the UART with an optional timeout.
|
|
Status | pw::uart::UartBase::Enable () |
|
Status | pw::uart::UartBase::Disable () |
|
Status | pw::uart::UartBase::SetBaudRate (uint32_t baud_rate) |
|
Status | pw::uart::UartBase::SetFlowControl (bool enable) |
|
size_t | pw::uart::UartBase::ConservativeReadAvailable () |
|
Status | pw::uart::UartBase::ClearPendingReceiveBytes () |
|
Status | pw::uart::UartNonBlocking::ReadExactly (ByteSpan rx_buffer, Function< void(Status status, ConstByteSpan buffer)> &&callback) |
|
Status | pw::uart::UartNonBlocking::ReadAtLeast (ByteSpan rx_buffer, size_t min_bytes, Function< void(Status status, ConstByteSpan buffer)> &&callback) |
|
bool | pw::uart::UartNonBlocking::CancelRead () |
|
Status | pw::uart::UartNonBlocking::Write (ConstByteSpan tx_buffer, Function< void(StatusWithSize status)> &&callback) |
|
bool | pw::uart::UartNonBlocking::CancelWrite () |
|
Status | pw::uart::UartNonBlocking::FlushOutput (Function< void(Status status)> &&callback) |
|
bool | pw::uart::UartNonBlocking::CancelFlushOutput () |
|
virtual Status | pw::uart::UartNonBlocking::DoRead (ByteSpan rx_buffer, size_t min_bytes, Function< void(Status status, ConstByteSpan buffer)> &&callback)=0 |
|
virtual bool | pw::uart::UartNonBlocking::DoCancelRead ()=0 |
|
virtual Status | pw::uart::UartNonBlocking::DoWrite (ConstByteSpan tx_buffer, Function< void(StatusWithSize status)> &&callback)=0 |
|
virtual bool | pw::uart::UartNonBlocking::DoCancelWrite ()=0 |
|
virtual Status | pw::uart::UartNonBlocking::DoFlushOutput (Function< void(Status status)> &&) |
|
virtual bool | pw::uart::UartNonBlocking::DoCancelFlushOutput () |
|
◆ CancelFlushOutput()
bool pw::uart::UartNonBlocking::CancelFlushOutput |
( |
| ) |
|
|
inline |
Cancels a pending FlushOutput() operation.
This function will cancel an output flush in progress. The FlushOutput callback will be called with status=CANCELLED.
- Returns
embed:rst:leading-asterisk
*
* true: The operation was successfully canceled a transaction in progress
* and the callback will be invoked with status=CANCELLED.
* false: There were no transactions in progress and nothing was
* cancelled. No callback will be invoked.
*
*
◆ CancelRead()
bool pw::uart::UartNonBlocking::CancelRead |
( |
| ) |
|
|
inline |
Cancel a current read in progress.
This function will cancel a read in progress. The read's callback will be invoked with status=CANCELLED.
- Returns
embed:rst:leading-asterisk
*
* true: The operation was successfully canceled a transaction in progress
* and the callback will be invoked with status=CANCELLED.
* false: There were no transactions in progress and nothing was
* cancelled. No callback will be invoked.
*
*
◆ CancelWrite()
bool pw::uart::UartNonBlocking::CancelWrite |
( |
| ) |
|
|
inline |
Cancel a current write in progress.
This function will cancel a write in progress. The write's callback will be called with status=CANCELLED.
- Returns
embed:rst:leading-asterisk
*
* true: The operation was successfully canceled a transaction in progress
* and the callback will be invoked with status=CANCELLED.
* false: There were no transactions in progress and nothing was
* cancelled. No callback will be invoked.
*
*
◆ ClearPendingReceiveBytes()
Status pw::uart::UartBase::ClearPendingReceiveBytes |
( |
| ) |
|
|
inline |
Empties the UART's receive buffer and discards any unread data.
This function removes all data from the receive buffer, resetting the buffer to an empty state. This is useful for situations where you want to disregard any previously received data and resynchronize.
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful.
*
* May return other implementation-specific status codes.
*
*
◆ ConservativeReadAvailable()
size_t pw::uart::UartBase::ConservativeReadAvailable |
( |
| ) |
|
|
inline |
Returns the number of bytes currently available for reading.
This function checks the receive buffer to determine how many bytes of data are ready to be read.
- Returns
- The number of bytes available for reading. When no data is available or in case of an error this function returns 0.
◆ Disable()
Status pw::uart::UartBase::Disable |
( |
| ) |
|
|
inline |
Disables the UART module. Disabling the UART shuts down communication and prevents the microcontroller from sending or receiving data through the UART port.
This is usually done to save power. Interrupt handlers should also be disabled.
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The UART module has been successfully initialized.
*
* INTERNAL: Internal errors within the hardware abstraction layer.
*
*
◆ DoCancelFlushOutput()
virtual bool pw::uart::UartNonBlocking::DoCancelFlushOutput |
( |
| ) |
|
|
inlineprivatevirtual |
Cancels a pending FlushOutput() operation.
This function will cancel an output flush in progress. The DoFlushOutput callback will be called with status=CANCELLED.
- Returns
embed:rst:leading-asterisk
*
* true: The operation was successfully canceled a transaction in progress
* and the callback will be invoked with status=CANCELLED.
* false: There were no transactions in progress and nothing was
* cancelled. No callback will be invoked.
*
*
◆ DoCancelRead()
virtual bool pw::uart::UartNonBlocking::DoCancelRead |
( |
| ) |
|
|
privatepure virtual |
Cancels a current read in progress.
This virtual function will cancel a read in progress. The read's callback will be called with status=CANCELLED.
- Returns
embed:rst:leading-asterisk
*
* true: The operation was successfully canceled a transaction in progress
* and the callback will be invoked with status=CANCELLED.
* false: There were no transactions in progress and nothing was
* cancelled. No callback will be invoked.
*
* May return other implementation-specific status codes.
*
*
◆ DoCancelWrite()
virtual bool pw::uart::UartNonBlocking::DoCancelWrite |
( |
| ) |
|
|
privatepure virtual |
Cancel a current write in progress.
This virtual function will cancel a write in progress. The write's callback will be invoked with status=CANCELLED.
- Returns
embed:rst:leading-asterisk
*
* true: The operation was successfully canceled a transaction in progress
* and the callback will be invoked with status=CANCELLED.
* false: There were no transactions in progress and nothing was
* cancelled. No callback will be invoked.
*
* May return other implementation-specific status codes.
*
*
◆ DoFlushOutput()
Ensures all queued data in the UART has been transmitted and the hardware FIFO is empty.
This function ensures that all data enqueued before calling this function has been transmitted. Any data enqueued after this function completes will be transmitted immediately.
- Parameters
-
callback | A callback to invoke when the flush is completed. |
status | OK: The operation was successful and the transmit FIFO is empty. CANCELLED: The operation was cancelled via CancelFlushOutput(). May return other implementation-specific status codes. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successfully started.
* UNAVAILABLE: Another Write() or FlushOutput() operation is currently
* in progress.
*
* May return other implementation-specific status codes.
*
*
◆ DoRead()
Reads at least min_bytes
and at most rx_buffer.size()
bytes from the UART into the provided buffer.
This virtual function attempts to read data into the provided byte buffer (rx_buffer
). This virtual function will return immediately. callback
will be invoked when the buffer has been filled with at least min_bytes
, or an error occurs. Implementation Notes:
- The callback may be called in ISR context.
- The callback must be moved and stored prior to its invocation.
- Do not hold a lock when invoking the callback.
- Parameters
-
rx_buffer | The buffer to read data into. |
min_bytes | Minimum bytes to read. |
callback | A callback to invoke when the transaction is completed. |
status | OK: The operation was successful and the buffer has been filled with at least min_bytes with data. CANCELLED: The operation was cancelled via CancelRead(). May return other implementation-specific status codes. |
buffer | buffer.size() returns the number of bytes successfully read into the buffer. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful started.
* UNAVAILABLE: Another Read() transaction is currently in progress.
*
* May return other implementation-specific status codes.
*
*
◆ DoTryReadFor() [1/2]
Reads data from the UART into a provided buffer with an optional timeout provided.
This virtual function attempts to read data into the provided byte buffer (rx_buffer
). The operation will continue until either the buffer is full, an error occurs, or the optional timeout duration expires.
- Parameters
-
rx_buffer | The buffer to read data into. |
timeout | An optional timeout duration. If specified, the function will block for no longer than this duration. If zero, the function will immediately return with at least one hardware read operation attempt. If not specified, the function blocks until the buffer is full. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been
* filled with data.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be filled.
*
* May return other implementation-specific status codes.
*
*
◆ DoTryReadFor() [2/2]
Reads data from the UART into a provided buffer with an optional timeout provided.
This virtual function attempts to read data into the provided byte buffer (rx_buffer
). The operation will continue until either min_bytes
have been read into the buffer, an error occurs, or the optional timeout duration expires.
- Parameters
-
rx_buffer | The buffer to read data into. |
min_bytes | The minimum number of bytes to read before returning. |
timeout | An optional timeout duration. If specified, the function will block for no longer than this duration. If zero, the function will immediately return with at least one hardware read operation attempt. If not specified, the function blocks until the buffer is full. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been
* filled with data.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be filled.
*
* May return other implementation-specific status codes.
*
*
Reimplemented in pw::uart::UartBlockingAdapter.
◆ DoTryWriteFor()
Writes data from a provided buffer to the UART with an optional timeout.
This virtual function attempts to write data from the provided byte buffer (tx_buffer
) to the UART. The operation will continue until either the buffer is empty, an error occurs, or the optional timeout duration expires.
- Parameters
-
tx_buffer | The buffer containing data to be written. |
timeout | An optional timeout duration. If specified, the function will block for no longer than this duration. If zero, the function will immediately return after at least one hardware write operation attempt. If not specified, the function blocks until the buffer is empty. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been
* written.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be written.
*
* May return other implementation-specific status codes.
*
*
Implemented in pw::uart::UartBlockingAdapter.
◆ DoWrite()
Writes data from a provided buffer to the UART.
This virtual function attempts to write data from the provided byte buffer (tx_buffer
). This virtual function will return immediately. callback
will be invoked when either the buffer fully written, or an error occurs. Implementation Notes:
- The callback may be called in ISR context.
- The callback must be moved and stored prior to its invocation.
- Do not hold a lock when invoking the callback.
- Parameters
-
tx_buffer | The buffer to write to the UART. |
callback | A callback to invoke when the transaction is completed. |
status | status.size() returns the number of bytes successfully written from tx_buffer . |
status | OK: The operation was successful and the entire buffer has been written. CANCELLED: The operation was cancelled via CancelWrite(). May return other implementation-specific status codes. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful started.
* UNAVAILABLE: Another Write() transaction is currently in progress.
*
* May return other implementation-specific status codes.
*
*
◆ Enable()
Status pw::uart::UartBase::Enable |
( |
| ) |
|
|
inline |
Initializes the UART module, sets it into the default state as determined by the concrete UART implementation. This function should be a no-op if the UART module is in an enabled state.
This may change the power state of the UART module, configure the interface parameters, enable the associated pins, setup the internal TX and RX buffers, etc...
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The UART module has been successfully initialized.
*
* INTERNAL: Internal errors within the hardware abstraction layer.
*
*
◆ FlushOutput() [1/2]
Status pw::uart::Uart::FlushOutput |
( |
| ) |
|
|
inline |
Blocks until all queued data in the UART has been transmitted and the FIFO is empty.
This function ensures that all data enqueued before calling this function has been transmitted. Any data enqueued after calling this function will be transmitted immediately.
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful.
*
* May return other implementation-specific status codes.
*
*
◆ FlushOutput() [2/2]
Ensures all queued data in the UART has been transmitted and the hardware FIFO is empty.
This function ensures that all data enqueued before calling this function has been transmitted. Any data enqueued after this function completes will be transmitted immediately.
- Parameters
-
callback | A callback to invoke when the flush is completed. |
status | OK: The operation was successful and the transmit FIFO is empty. CANCELLED: The operation was cancelled via CancelFlushOutput(). May return other implementation-specific status codes. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successfully started.
* UNAVAILABLE: Another Write() or FlushOutput() operation is currently
* in progress.
*
* May return other implementation-specific status codes.
*
*
◆ Read()
Deprecated: Prefer ReadExactly in new code.
Reads data from the UART into a provided buffer.
This function blocks until the entire buffer has been filled.
- Parameters
-
rx_buffer | The buffer to read data into. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful.
*
* May return other implementation-specific status codes.
*
*
◆ ReadAtLeast() [1/2]
Reads data from the UART into a provided buffer.
This function blocks until min_bytes
have been read into rx_buffer
.
- Parameters
-
rx_buffer | The buffer to read data into. |
min_bytes | The minimum number of bytes to read before returning. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful.
*
* May return other implementation-specific status codes.
*
*
◆ ReadAtLeast() [2/2]
Reads at least min_bytes
and at most rx_buffer.size()
bytes from the UART into the provided buffer.
This function calls callback
after rx_buffer
is filled with at least min_bytes
of data. This may be called from interrupt context. The callback may be called in ISR context. It is not safe to call any Uart methods from the callback context.
- Parameters
-
rx_buffer | The buffer to read data into. |
min_bytes | Minimum bytes to read. |
callback | A callback to invoke when the transaction is completed. |
status | OK: The operation was successful and the buffer has been filled with at least min_bytes with data. CANCELLED: The operation was cancelled via CancelRead(). May return other implementation-specific status codes. |
buffer | buffer.size() returns the number of bytes successfully read into the buffer. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successfully started.
* UNAVAILABLE: Another Read() transaction is currently in progress.
*
* May return other implementation-specific status codes.
*
*
◆ ReadExactly() [1/2]
Reads data from the UART into a provided buffer.
This function blocks until the entire buffer has been filled.
- Parameters
-
rx_buffer | The buffer to read data into. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful.
*
* May return other implementation-specific status codes.
*
*
◆ ReadExactly() [2/2]
Reads exactly rx_buffer.size()
bytes from the UART into the provided buffer.
This function calls callback
after the entirety of rx_buffer
is filled with data. This may be called from interrupt context. The callback may be called in ISR context. It is not safe to call any Uart methods from the callback context.
- Parameters
-
rx_buffer | The buffer to read data into. |
callback | A callback to invoke when the transaction is completed. |
status | OK: The operation was successful and the entire buffer has been filled with data. CANCELLED: The operation was cancelled via CancelRead(). May return other implementation-specific status codes. |
buffer | buffer.size() returns the number of bytes successfully read into the buffer. If status =OK, the buffer is identical to rx_buffer |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successfully started.
* UNAVAILABLE: Another Read() transaction is currently in progress.
*
* May return other implementation-specific status codes.
*
*
◆ SetBaudRate()
Status pw::uart::UartBase::SetBaudRate |
( |
uint32_t |
baud_rate | ) |
|
|
inline |
Configures the UART communication baud rate.
This function sets the communication speed (baud rate) for the UART. Whether the baud rate can be changed while the UART is enabled depends on the specific implementation.
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The UART has been successfully initialized.
*
* FAILED_PRECONDITION: The device is enabled and does not support
* changing settings on the fly.
*
* INTERNAL: Internal errors within the hardware abstraction layer.
*
*
◆ SetFlowControl()
Status pw::uart::UartBase::SetFlowControl |
( |
bool |
enable | ) |
|
|
inline |
Configures the UART hardware flow control enable.
This function sets the hardware flow control enable for the UART. Whether the flow control setting rate can be changed while the UART is enabled depends on the specific implementation.
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The UART has been successfully initialized.
*
* FAILED_PRECONDITION: The device is enabled and does not support
* changing settings on the fly.
*
* UNIMPLEMENTED: The device does not support flow control.
*
* INTERNAL: Internal errors within the hardware abstraction layer.
*
*
◆ TryReadAtLeastFor()
Reads data from the UART into a provided buffer.
This function blocks until either min_bytes
have been read into buffer or the specified timeout has elapsed, whichever occurs first.
- Parameters
-
rx_buffer | The buffer to read data into. |
min_bytes | The minimum number of bytes to read before returning. |
timeout | The maximum time to wait for data to be read. If zero, the function will immediately return with at least one hardware read operation attempt. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been filled
* with data.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be filled.
*
* May return other implementation-specific status codes.
*
*
◆ TryReadExactlyFor()
Reads data from the UART into a provided buffer.
This function blocks until either rx_buffer.size()
bytes have been read into buffer or the specified timeout has elapsed, whichever occurs first.
- Parameters
-
rx_buffer | The buffer to read data into. |
timeout | The maximum time to wait for data to be read. If zero, the function will immediately return with at least one hardware read operation attempt. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been filled
* with data.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be filled.
*
* May return other implementation-specific status codes.
*
*
◆ TryReadFor()
Deprecated: Prefer TryReadExactlyFor in new code. Reads data from the UART into a provided buffer.
This function blocks until either rx_buffer.size()
bytes have been read into buffer or the specified timeout has elapsed, whichever occurs first.
- Parameters
-
rx_buffer | The buffer to read data into. |
timeout | The maximum time to wait for data to be read. If zero, the function will immediately return with at least one hardware read operation attempt. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been filled
* with data.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be filled.
*
* May return other implementation-specific status codes.
*
*
◆ TryWriteFor()
Writes data from the provided buffer to the UART. The function blocks until either the entire buffer has been written or the specified timeout has elapsed, whichever occurs first.
- Parameters
-
tx_buffer | The buffer to write data from. |
timeout | The maximum time to wait for data to be written. If zero, the function will immediately return with at least one hardware write operation attempt. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful and the entire buffer has been
* written.
*
* DEADLINE_EXCEEDED: The operation timed out before the entire buffer
* could be written.
*
* May return other implementation-specific status codes.
*
*
◆ Write() [1/2]
Writes data from the provided buffer to the UART. The function blocks until the entire buffer has been written.
- Parameters
-
tx_buffer | - The buffer to write data from. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successful.
*
* May return other implementation-specific status codes.
*
*
◆ Write() [2/2]
Writes data from a provided buffer to the UART.
This function calls callback
after the entirety of tx_buffer
is written to the UART. This may be called from interrupt context. The callback may be called in ISR context. It is not safe to call any Uart methods from the callback context.
- Parameters
-
tx_buffer | The buffer to write to the UART. |
callback | A callback to invoke when the transaction is completed. |
status | status.size() returns the number of bytes successfully written from tx_buffer . OK: The operation was successful and the entire buffer has been written. CANCELLED: The operation was cancelled via CancelWrite(). May return other implementation-specific status codes. |
- Returns
embed:rst:leading-asterisk
*
* .. pw-status-codes::
*
* OK: The operation was successfully started.
* UNAVAILABLE: Another Write() transaction is currently in progress.
*
* May return other implementation-specific status codes.
*
*