uart.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef KBF_UART_H
  2. #define KBF_UART_H
  3. #include <string>
  4. #include <mutex>
  5. #include <freertos/FreeRTOS.h>
  6. #include <freertos/queue.h>
  7. #include "task.h"
  8. using std::string;
  9. namespace kbf {
  10. /**
  11. * @brief Universal Asynchronous Receiver-Transmitter functions.
  12. */
  13. class UART {
  14. public:
  15. /** @brief Tag used for logging */
  16. static constexpr const char *TAG = "kbf::uart";
  17. /** @brief RX buffer size. */
  18. static constexpr const int BUFFER_SIZE = 2048; // TODO use Kconfig or provide runtime config option?
  19. /** @brief pass this to setPins to leave a pin unchanged */
  20. static constexpr const int NO_CHANGE = -1;
  21. /** @brief maximum number of RTOS ticks to wait in destructor for TX FIFO */
  22. static constexpr const int WAIT_ON_DESTROY = 100;
  23. /**
  24. * @brief Installs and initializes the UART driver and starts the event handler task.
  25. *
  26. * @note This function will abort if the driver has already been initialized. Use the #isDriverInstalled()
  27. * static function to check in advance
  28. *
  29. * @param port UART port to use; 0 is used for programming / logging, so you probably want >0; default is 1
  30. * @param baudRate Baud rate to use; default is 115200
  31. */
  32. explicit UART(int port = 1, int baudRate = 115200);
  33. /**
  34. * @brief Uninstalls the UART driver.
  35. *
  36. * @note May wait #WAIT_ON_DESTROY RTOS ticks for the TX FIFO to finish.
  37. */
  38. ~UART();
  39. /**
  40. * @brief Sets the GPIO pins used by UART for the current port.
  41. *
  42. * @param rx RX pin
  43. * @param tx TX pin
  44. * @param rts RTS pin
  45. * @param cts CTS pin
  46. */
  47. void setPins(int rx, int tx, int rts, int cts) const;
  48. /**
  49. * @brief Checks whether the UART driver is installed for a given port.
  50. *
  51. * @param port UART port to use; default is 1
  52. * @return true if the UART driver is already installed; false otherwise
  53. */
  54. static bool isDriverInstalled(int port = 1);
  55. /**
  56. * @brief Sends data.
  57. *
  58. * @param data data to be sent
  59. */
  60. void write(const string &data) const;
  61. /**
  62. * @brief Reads data, blocking execution.
  63. *
  64. * @note For filtering data, see #onRead(). It will be called before this function returns.
  65. *
  66. * @return read data
  67. */
  68. [[nodiscard]] string read();
  69. /**
  70. * @brief Called when data is received.
  71. *
  72. * @note This function will be called before #read() or #waitFor() returns, thus enabling
  73. * data filtering by modifying the contents of the first argument, &data.
  74. *
  75. * @param data data being read
  76. * @param uartInstance UART class instance
  77. */
  78. void (*onRead)(string &data, UART &uartInstance) = nullptr;
  79. /**
  80. * @brief Blocks execution until the specified signal is received.
  81. *
  82. * @note #onRead() will be called before this function returns.
  83. *
  84. * @param signal data to wait for
  85. */
  86. void waitFor(const string &signal);
  87. /** @brief UART port; default is 1 (set by ctor) */
  88. const int port;
  89. private:
  90. string buffer{};
  91. std::mutex mutex;
  92. QueueHandle_t queue{};
  93. kbf::Task *task;
  94. [[noreturn]] static void taskFunc(void *uartInstance);
  95. };
  96. }
  97. #endif //KBF_UART_H