r_uart_api.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Copyright (c) 2020 - 2025 Renesas Electronics Corporation and/or its affiliates
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*******************************************************************************************************************//**
  7. * @ingroup RENESAS_CONNECTIVITY_INTERFACES
  8. * @defgroup UART_API UART Interface
  9. * @brief Interface for UART communications.
  10. *
  11. * @section UART_INTERFACE_SUMMARY Summary
  12. * The UART interface provides common APIs for UART HAL drivers. The UART interface supports the following features:
  13. * - Full-duplex UART communication
  14. * - Interrupt driven transmit/receive processing
  15. * - Callback function with returned event code
  16. * - Runtime baud-rate change
  17. * - Hardware resource locking during a transaction
  18. * - CTS/RTS hardware flow control support (with an associated IOPORT pin)
  19. *
  20. *
  21. * @{
  22. **********************************************************************************************************************/
  23. #ifndef R_UART_API_H
  24. #define R_UART_API_H
  25. /***********************************************************************************************************************
  26. * Includes
  27. **********************************************************************************************************************/
  28. /* Includes board and MCU related header files. */
  29. #include "bsp_api.h"
  30. #include "r_transfer_api.h"
  31. /* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
  32. FSP_HEADER
  33. /**********************************************************************************************************************
  34. * Macro definitions
  35. **********************************************************************************************************************/
  36. /**********************************************************************************************************************
  37. * Typedef definitions
  38. **********************************************************************************************************************/
  39. /** UART Event codes */
  40. #ifndef BSP_OVERRIDE_UART_EVENT_T
  41. typedef enum e_sf_event
  42. {
  43. UART_EVENT_RX_COMPLETE = (1UL << 0), /* /< Receive complete event */
  44. UART_EVENT_TX_COMPLETE = (1UL << 1), /* /< Transmit complete event */
  45. UART_EVENT_RX_CHAR = (1UL << 2), /* /< Character received */
  46. UART_EVENT_ERR_PARITY = (1UL << 3), /* /< Parity error event */
  47. UART_EVENT_ERR_FRAMING = (1UL << 4), /* /< Mode fault error event */
  48. UART_EVENT_ERR_OVERFLOW = (1UL << 5), /* /< FIFO Overflow error event */
  49. UART_EVENT_BREAK_DETECT = (1UL << 6), /* /< Break detect error event */
  50. UART_EVENT_TX_DATA_EMPTY = (1UL << 7), /* /< Last byte is transmitting, ready for more data */
  51. } uart_event_t;
  52. #endif
  53. #ifndef BSP_OVERRIDE_UART_DATA_BITS_T
  54. /** UART Data bit length definition */
  55. typedef enum e_uart_data_bits
  56. {
  57. UART_DATA_BITS_9 = 0U, /* /< Data bits 9-bit */
  58. UART_DATA_BITS_8 = 2U, /* /< Data bits 8-bit */
  59. UART_DATA_BITS_7 = 3U, /* /< Data bits 7-bit */
  60. } uart_data_bits_t;
  61. #endif
  62. #ifndef BSP_OVERRIDE_UART_PARITY_T
  63. /** UART Parity definition */
  64. typedef enum e_uart_parity
  65. {
  66. UART_PARITY_OFF = 0U, /* /< No parity */
  67. UART_PARITY_ZERO = 1U, /* /< Zero parity */
  68. UART_PARITY_EVEN = 2U, /* /< Even parity */
  69. UART_PARITY_ODD = 3U, /* /< Odd parity */
  70. } uart_parity_t;
  71. #endif
  72. /** UART Stop bits definition */
  73. typedef enum e_uart_stop_bits
  74. {
  75. UART_STOP_BITS_1 = 0U, /* /< Stop bit 1-bit */
  76. UART_STOP_BITS_2 = 1U, /* /< Stop bits 2-bit */
  77. } uart_stop_bits_t;
  78. /** UART transaction definition */
  79. typedef enum e_uart_dir
  80. {
  81. UART_DIR_RX_TX = 3U, /* /< Both RX and TX */
  82. UART_DIR_RX = 1U, /* /< Only RX */
  83. UART_DIR_TX = 2U, /* /< Only TX */
  84. } uart_dir_t;
  85. /** UART driver specific information */
  86. typedef struct st_uart_info
  87. {
  88. /** Maximum bytes that can be written at this time. Only applies if uart_cfg_t::p_transfer_tx is not NULL. */
  89. uint32_t write_bytes_max;
  90. /** Maximum bytes that are available to read at one time. Only applies if uart_cfg_t::p_transfer_rx is not NULL. */
  91. uint32_t read_bytes_max;
  92. } uart_info_t;
  93. /** UART Callback parameter definition */
  94. typedef struct st_uart_callback_arg
  95. {
  96. uint32_t channel; /* /< Device channel number */
  97. uart_event_t event; /* /< Event code */
  98. /** Contains the next character received for the events UART_EVENT_RX_CHAR, UART_EVENT_ERR_PARITY,
  99. * UART_EVENT_ERR_FRAMING, or UART_EVENT_ERR_OVERFLOW. Otherwise unused. */
  100. uint32_t data;
  101. void const * p_context; /* /< Context provided to user during callback */
  102. } uart_callback_args_t;
  103. /** UART Configuration */
  104. typedef struct st_uart_cfg
  105. {
  106. /* UART generic configuration */
  107. uint8_t channel; /* /< Select a channel corresponding to the channel number of the hardware. */
  108. uart_data_bits_t data_bits; /* /< Data bit length (8 or 7 or 9) */
  109. uart_parity_t parity; /* /< Parity type (none or odd or even) */
  110. uart_stop_bits_t stop_bits; /* /< Stop bit length (1 or 2) */
  111. uint8_t rxi_ipl; /* /< Receive interrupt priority */
  112. IRQn_Type rxi_irq; /* /< Receive interrupt IRQ number */
  113. uint8_t txi_ipl; /* /< Transmit interrupt priority */
  114. IRQn_Type txi_irq; /* /< Transmit interrupt IRQ number */
  115. uint8_t tei_ipl; /* /< Transmit end interrupt priority */
  116. IRQn_Type tei_irq; /* /< Transmit end interrupt IRQ number */
  117. uint8_t eri_ipl; /* /< Error interrupt priority */
  118. IRQn_Type eri_irq; /* /< Error interrupt IRQ number */
  119. /** Optional transfer instance used to receive multiple bytes without interrupts. Set to NULL if unused.
  120. * If NULL, the number of bytes allowed in the read API is limited to one byte at a time. */
  121. transfer_instance_t const * p_transfer_rx;
  122. /** Optional transfer instance used to send multiple bytes without interrupts. Set to NULL if unused.
  123. * If NULL, the number of bytes allowed in the write APIs is limited to one byte at a time. */
  124. transfer_instance_t const * p_transfer_tx;
  125. /* Configuration for UART Event processing */
  126. void (* p_callback)(uart_callback_args_t * p_args); /* /< Pointer to callback function */
  127. void const * p_context; /* /< User defined context passed into callback function */
  128. /* Pointer to UART peripheral specific configuration */
  129. void const * p_extend; /* /< UART hardware dependent configuration */
  130. } uart_cfg_t;
  131. /** UART control block. Allocate an instance specific control block to pass into the UART API calls.
  132. */
  133. typedef void uart_ctrl_t;
  134. /** Shared Interface definition for UART */
  135. typedef struct st_uart_api
  136. {
  137. /** Open UART device.
  138. *
  139. * @param[in,out] p_ctrl Pointer to the UART control block. Must be declared by user. Value set here.
  140. * @param[in] uart_cfg_t Pointer to UART configuration structure. All elements of this structure must be set by
  141. * user.
  142. */
  143. fsp_err_t (* open)(uart_ctrl_t * const p_ctrl, uart_cfg_t const * const p_cfg);
  144. /** Read from UART device. The read buffer is used until the read is complete. When a transfer is complete, the
  145. * callback is called with event UART_EVENT_RX_COMPLETE. Bytes received outside an active transfer are received in
  146. * the callback function with event UART_EVENT_RX_CHAR.
  147. * The maximum transfer size is reported by infoGet().
  148. *
  149. * @param[in] p_ctrl Pointer to the UART control block for the channel.
  150. * @param[in] p_dest Destination address to read data from.
  151. * @param[in] bytes Read data length.
  152. */
  153. fsp_err_t (* read)(uart_ctrl_t * const p_ctrl, uint8_t * const p_dest, uint32_t const bytes);
  154. /** Write to UART device. The write buffer is used until write is complete. Do not overwrite write buffer
  155. * contents until the write is finished. When the write is complete (all bytes are fully transmitted on the wire),
  156. * the callback called with event UART_EVENT_TX_COMPLETE.
  157. * The maximum transfer size is reported by infoGet().
  158. *
  159. * @param[in] p_ctrl Pointer to the UART control block.
  160. * @param[in] p_src Source address to write data to.
  161. * @param[in] bytes Write data length.
  162. */
  163. fsp_err_t (* write)(uart_ctrl_t * const p_ctrl, uint8_t const * const p_src, uint32_t const bytes);
  164. /** Change baud rate.
  165. * @warning Calling this API aborts any in-progress transmission and disables reception until the new baud
  166. * settings have been applied.
  167. *
  168. *
  169. * @param[in] p_ctrl Pointer to the UART control block.
  170. * @param[in] p_baudrate_info Pointer to module specific information for configuring baud rate.
  171. */
  172. fsp_err_t (* baudSet)(uart_ctrl_t * const p_ctrl, void const * const p_baudrate_info);
  173. /** Get the driver specific information.
  174. *
  175. * @param[in] p_ctrl Pointer to the UART control block.
  176. * @param[out] p_info Pointer to UART information structure.
  177. */
  178. fsp_err_t (* infoGet)(uart_ctrl_t * const p_ctrl, uart_info_t * const p_info);
  179. /**
  180. * Abort ongoing transfer.
  181. *
  182. * @param[in] p_ctrl Pointer to the UART control block.
  183. * @param[in] communication_to_abort Type of abort request.
  184. */
  185. fsp_err_t (* communicationAbort)(uart_ctrl_t * const p_ctrl, uart_dir_t communication_to_abort);
  186. /**
  187. * Specify callback function and optional context pointer and working memory pointer.
  188. *
  189. * @param[in] p_ctrl Pointer to the UART control block.
  190. * @param[in] p_callback Callback function
  191. * @param[in] p_context Pointer to send to callback function
  192. * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated.
  193. * Callback arguments allocated here are only valid during the callback.
  194. */
  195. fsp_err_t (* callbackSet)(uart_ctrl_t * const p_ctrl, void (* p_callback)(uart_callback_args_t *),
  196. void const * const p_context, uart_callback_args_t * const p_callback_memory);
  197. /** Close UART device.
  198. *
  199. * @param[in] p_ctrl Pointer to the UART control block.
  200. */
  201. fsp_err_t (* close)(uart_ctrl_t * const p_ctrl);
  202. /** Stop ongoing read and return the number of bytes remaining in the read.
  203. *
  204. * @param[in] p_ctrl Pointer to the UART control block.
  205. * @param[in,out] remaining_bytes Pointer to location to store remaining bytes for read.
  206. */
  207. fsp_err_t (* readStop)(uart_ctrl_t * const p_ctrl, uint32_t * remaining_bytes);
  208. } uart_api_t;
  209. /** This structure encompasses everything that is needed to use an instance of this interface. */
  210. typedef struct st_uart_instance
  211. {
  212. uart_ctrl_t * p_ctrl; /* /< Pointer to the control structure for this instance */
  213. uart_cfg_t const * p_cfg; /* /< Pointer to the configuration structure for this instance */
  214. uart_api_t const * p_api; /* /< Pointer to the API structure for this instance */
  215. } uart_instance_t;
  216. /** @} (end defgroup UART_API) */
  217. /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
  218. FSP_FOOTER
  219. #endif