12#ifndef CMSIS_PLUS_POSIX_DRIVER_DEVICE_SERIAL_BUFFERED_H_
13#define CMSIS_PLUS_POSIX_DRIVER_DEVICE_SERIAL_BUFFERED_H_
15#if defined(__cplusplus)
21#include <cmsis-plus/posix-io/device-char.h>
38#pragma GCC diagnostic push
40#pragma clang diagnostic ignored "-Wpadded"
41#elif defined(__GNUC__)
42#pragma GCC diagnostic ignored "-Wpadded"
51 template <
typename CS>
118 do_vopen (
const char* path,
int oflag, std::va_list args)
override;
124 do_read (
void* buf, std::size_t nbyte)
override;
127 do_write (
const void* buf, std::size_t nbyte)
override;
131 do_writev (
const struct iovec* iov,
int iovcnt)
override;
134 do_vioctl (
int request, std::va_list args)
override;
137 do_vfcntl (
int cmd, va_list args)
override;
166 std::size_t rx_count_ = 0;
167 bool volatile tx_busy_ =
false;
168 bool volatile is_connected_ =
false;
169 bool volatile is_opened_ =
false;
177#pragma GCC diagnostic pop
190 template <
typename CS>
192 const char* device_name,
196 device_char (device_name),
201 trace::printf (
"%s(\"%s\",%p,%p,%p) %p\n", __func__, device_name, driver,
202 rx_buf, tx_buf,
this);
204 assert (rx_buf !=
nullptr);
208 driver_->register_callback (
212 template <
typename CS>
218 is_connected_ =
false;
224#pragma GCC diagnostic push
225#if defined(__clang__)
226#elif defined(__GNUC__)
227#pragma GCC diagnostic ignored "-Wunused-parameter"
230 template <
typename CS>
256 if (tx_buf_ !=
nullptr)
263 result = driver_->configure (
293 capa = driver_->get_capabilities ();
303 status = driver_->get_modem_status ();
315 std::size_t nbyte = rx_buf_->back_contiguous_buffer (&pbuf);
317 result = driver_->receive (pbuf, nbyte);
324 is_connected_ =
true;
330 template <
typename CS>
337 template <
typename CS>
341 return is_connected_;
344 template <
typename CS>
353 if (tx_buf_ !=
nullptr)
357 if (tx_buf_->empty ())
385 is_connected_ =
false;
391 template <
typename CS>
404 count = rx_buf_->pop_front (
static_cast<uint8_t*
> (buf), nbyte);
422 template <
typename CS>
428 if (tx_buf_ !=
nullptr)
435 if (tx_buf_->below_high_water_mark ())
438 count = tx_buf_->push_back (
static_cast<const uint8_t*
> (buf),
450#pragma GCC diagnostic push
451#if defined(__clang__)
452#elif defined(__GNUC__)
453#pragma GCC diagnostic ignored "-Waggregate-return"
455 status = driver_->get_status ();
456#pragma GCC diagnostic pop
468 nb = tx_buf_->front_contiguous_buffer (&pbuf);
514 n = tx_buf_->push_back (
static_cast<const uint8_t*
> (buf)
535 status = driver_->get_status ();
553 status = driver_->get_status ();
560 count = driver_->get_tx_count ();
574 template<
typename CS>
582 template<
typename CS>
584 device_serial_buffered<CS>::do_vioctl (
int request, std::va_list args)
590 template<
typename CS>
592 device_serial_buffered<CS>::do_vfcntl (
int cmd, std::va_list args)
601 template <
typename CS>
606 if (!object->is_opened_)
617 std::size_t tmpCount =
object->driver_->get_rx_count ();
618 std::size_t count = tmpCount -
object->rx_count_;
619 object->rx_count_ = tmpCount;
620 std::size_t adjust =
object->rx_buf_->advance_back (count);
621 assert (count == adjust);
627 =
object->rx_buf_->back_contiguous_buffer (&pbuf);
632 object->rx_buf_->retreat_back ();
633 nbyte =
object->rx_buf_->back_contiguous_buffer (&pbuf);
639 status =
object->driver_->receive (pbuf, nbyte);
643 object->rx_count_ = 0;
648 object->rx_sem_.post ();
653 if (object->tx_buf_ !=
nullptr)
655 std::size_t count =
object->driver_->get_tx_count ();
656 std::size_t adjust =
object->tx_buf_->advance_front (count);
657 assert (count == adjust);
661 =
object->tx_buf_->front_contiguous_buffer (&pbuf);
665 status =
object->driver_->send (pbuf, nbyte);
671 object->tx_busy_ =
false;
673 if (object->tx_buf_->below_low_water_mark ())
676 object->tx_sem_.post ();
682 object->tx_sem_.post ();
688 status =
object->driver_->get_modem_status ();
691 object->is_connected_ = is_dcd_active;
695 object->open_sem_.post ();
700 object->rx_sem_.post ();
703 object->tx_sem_.post ();
716#pragma GCC diagnostic pop
Serial device driver capabilities.
bool dcd
DCD Line: false=not available, true=available.
bool tx_busy
Transmitter busy flag.
Circular buffer class template.<cmsis-plus/posix-driver/circular-buffer.h>
Buffered serial driver class template.<cmsis-plus/posix-driver/circular-buffer.h>
static void signal_event(device_serial_buffered *object, uint32_t event)
virtual ssize_t do_read(void *buf, std::size_t nbyte) override
virtual int do_close(void) override
virtual bool do_is_connected(void) override
virtual ~device_serial_buffered()
device_serial_buffered(const char *device_name, os::driver::Serial *driver, os::posix::circular_buffer_bytes *rx_buf, os::posix::circular_buffer_bytes *tx_buf)
virtual bool do_is_opened(void) override
virtual int do_vopen(const char *path, int oflag, std::va_list args) override
virtual ssize_t do_write(const void *buf, std::size_t nbyte) override
POSIX compliant binary semaphore.
int printf(const char *format,...)
Write a formatted string to the trace device.
constexpr config_t PARITY_NONE
No Parity = (default);.
constexpr config_t DATA_BITS_8
8 Data bits = (default);.
constexpr config_t STOP_BITS_1
1 Stop bit = (default);.
constexpr config_t MODE_ASYNCHRONOUS
UART = (Asynchronous);; arg = Baudrate.
constexpr config_t FLOW_CONTROL_NONE
No Flow Control = (default);.
@ abort_receive
Abort Serial::receive().
@ abort_send
Abort Serial::send().
@ disable_break
Disable Continuous Break transmission;.
@ disable_tx
Disable Transmitter.
@ enable_tx
Enable Transmitter.
@ enable_rx
Enable Receiver.
@ disable_rx
Disable Receiver.
@ cts
CTS state changed (optional).
@ rx_framing_error
Framing error detected on receive.
@ dsr
DSR state changed (optional).
@ dcd
DCD state changed (optional).
@ rx_timeout
Receive character timeout (optional).
@ receive_complete
Receive completed.
@ tx_complete
Transmit completed (optional).
constexpr return_t RETURN_OK
void(* signal_event_t)(const void *object, event_t event)
Single file µOS++ RTOS definitions.