r_uart_api.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /***********************************************************************************************************************
  2. * Copyright [2020-2023] Renesas Electronics Corporation and/or its affiliates. All Rights Reserved.
  3. *
  4. * This software and documentation are supplied by Renesas Electronics America Inc. and may only be used with products
  5. * of Renesas Electronics Corp. and its affiliates ("Renesas"). No other uses are authorized. Renesas products are
  6. * sold pursuant to Renesas terms and conditions of sale. Purchasers are solely responsible for the selection and use
  7. * of Renesas products and Renesas assumes no liability. No license, express or implied, to any intellectual property
  8. * right is granted by Renesas. This software is protected under all applicable laws, including copyright laws. Renesas
  9. * reserves the right to change or discontinue this software and/or this documentation. THE SOFTWARE AND DOCUMENTATION
  10. * IS DELIVERED TO YOU "AS IS," AND RENESAS MAKES NO REPRESENTATIONS OR WARRANTIES, AND TO THE FULLEST EXTENT
  11. * PERMISSIBLE UNDER APPLICABLE LAW, DISCLAIMS ALL WARRANTIES, WHETHER EXPLICITLY OR IMPLICITLY, INCLUDING WARRANTIES
  12. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT, WITH RESPECT TO THE SOFTWARE OR
  13. * DOCUMENTATION. RENESAS SHALL HAVE NO LIABILITY ARISING OUT OF ANY SECURITY VULNERABILITY OR BREACH. TO THE MAXIMUM
  14. * EXTENT PERMITTED BY LAW, IN NO EVENT WILL RENESAS BE LIABLE TO YOU IN CONNECTION WITH THE SOFTWARE OR DOCUMENTATION
  15. * (OR ANY PERSON OR ENTITY CLAIMING RIGHTS DERIVED FROM YOU) FOR ANY LOSS, DAMAGES, OR CLAIMS WHATSOEVER, INCLUDING,
  16. * WITHOUT LIMITATION, ANY DIRECT, CONSEQUENTIAL, SPECIAL, INDIRECT, PUNITIVE, OR INCIDENTAL DAMAGES; ANY LOST PROFITS,
  17. * OTHER ECONOMIC DAMAGE, PROPERTY DAMAGE, OR PERSONAL INJURY; AND EVEN IF RENESAS HAS BEEN ADVISED OF THE POSSIBILITY
  18. * OF SUCH LOSS, DAMAGES, CLAIMS OR COSTS.
  19. **********************************************************************************************************************/
  20. /*******************************************************************************************************************//**
  21. * @ingroup RENESAS_INTERFACES
  22. * @defgroup UART_API UART Interface
  23. * @brief Interface for UART communications.
  24. *
  25. * @section UART_INTERFACE_SUMMARY Summary
  26. * The UART interface provides common APIs for UART HAL drivers. The UART interface supports the following features:
  27. * - Full-duplex UART communication
  28. * - Interrupt driven transmit/receive processing
  29. * - Callback function with returned event code
  30. * - Runtime baud-rate change
  31. * - Hardware resource locking during a transaction
  32. * - CTS/RTS hardware flow control support (with an associated IOPORT pin)
  33. *
  34. *
  35. * @{
  36. **********************************************************************************************************************/
  37. #ifndef R_UART_API_H
  38. #define R_UART_API_H
  39. /***********************************************************************************************************************
  40. * Includes
  41. **********************************************************************************************************************/
  42. /* Includes board and MCU related header files. */
  43. #include "bsp_api.h"
  44. #include "r_transfer_api.h"
  45. /* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
  46. FSP_HEADER
  47. /**********************************************************************************************************************
  48. * Macro definitions
  49. **********************************************************************************************************************/
  50. /**********************************************************************************************************************
  51. * Typedef definitions
  52. **********************************************************************************************************************/
  53. /** UART Event codes */
  54. #ifndef BSP_OVERRIDE_UART_EVENT_T
  55. typedef enum e_sf_event
  56. {
  57. UART_EVENT_RX_COMPLETE = (1UL << 0), ///< Receive complete event
  58. UART_EVENT_TX_COMPLETE = (1UL << 1), ///< Transmit complete event
  59. UART_EVENT_RX_CHAR = (1UL << 2), ///< Character received
  60. UART_EVENT_ERR_PARITY = (1UL << 3), ///< Parity error event
  61. UART_EVENT_ERR_FRAMING = (1UL << 4), ///< Mode fault error event
  62. UART_EVENT_ERR_OVERFLOW = (1UL << 5), ///< FIFO Overflow error event
  63. UART_EVENT_BREAK_DETECT = (1UL << 6), ///< Break detect error event
  64. UART_EVENT_TX_DATA_EMPTY = (1UL << 7), ///< Last byte is transmitting, ready for more data
  65. } uart_event_t;
  66. #endif
  67. #ifndef BSP_OVERRIDE_UART_DATA_BITS_T
  68. /** UART Data bit length definition */
  69. typedef enum e_uart_data_bits
  70. {
  71. UART_DATA_BITS_9 = 0U, ///< Data bits 9-bit
  72. UART_DATA_BITS_8 = 2U, ///< Data bits 8-bit
  73. UART_DATA_BITS_7 = 3U, ///< Data bits 7-bit
  74. } uart_data_bits_t;
  75. #endif
  76. #ifndef BSP_OVERRIDE_UART_PARITY_T
  77. /** UART Parity definition */
  78. typedef enum e_uart_parity
  79. {
  80. UART_PARITY_OFF = 0U, ///< No parity
  81. UART_PARITY_EVEN = 2U, ///< Even parity
  82. UART_PARITY_ODD = 3U, ///< Odd parity
  83. } uart_parity_t;
  84. #endif
  85. /** UART Stop bits definition */
  86. typedef enum e_uart_stop_bits
  87. {
  88. UART_STOP_BITS_1 = 0U, ///< Stop bit 1-bit
  89. UART_STOP_BITS_2 = 1U, ///< Stop bits 2-bit
  90. } uart_stop_bits_t;
  91. /** UART transaction definition */
  92. typedef enum e_uart_dir
  93. {
  94. UART_DIR_RX_TX = 3U, ///< Both RX and TX
  95. UART_DIR_RX = 1U, ///< Only RX
  96. UART_DIR_TX = 2U, ///< Only TX
  97. } uart_dir_t;
  98. /** UART driver specific information */
  99. typedef struct st_uart_info
  100. {
  101. /** Maximum bytes that can be written at this time. Only applies if uart_cfg_t::p_transfer_tx is not NULL. */
  102. uint32_t write_bytes_max;
  103. /** Maximum bytes that are available to read at one time. Only applies if uart_cfg_t::p_transfer_rx is not NULL. */
  104. uint32_t read_bytes_max;
  105. } uart_info_t;
  106. /** UART Callback parameter definition */
  107. typedef struct st_uart_callback_arg
  108. {
  109. uint32_t channel; ///< Device channel number
  110. uart_event_t event; ///< Event code
  111. /** Contains the next character received for the events UART_EVENT_RX_CHAR, UART_EVENT_ERR_PARITY,
  112. * UART_EVENT_ERR_FRAMING, or UART_EVENT_ERR_OVERFLOW. Otherwise unused. */
  113. uint32_t data;
  114. void const * p_context; ///< Context provided to user during callback
  115. } uart_callback_args_t;
  116. /** UART Configuration */
  117. typedef struct st_uart_cfg
  118. {
  119. /* UART generic configuration */
  120. uint8_t channel; ///< Select a channel corresponding to the channel number of the hardware.
  121. uart_data_bits_t data_bits; ///< Data bit length (8 or 7 or 9)
  122. uart_parity_t parity; ///< Parity type (none or odd or even)
  123. uart_stop_bits_t stop_bits; ///< Stop bit length (1 or 2)
  124. uint8_t rxi_ipl; ///< Receive interrupt priority
  125. IRQn_Type rxi_irq; ///< Receive interrupt IRQ number
  126. uint8_t txi_ipl; ///< Transmit interrupt priority
  127. IRQn_Type txi_irq; ///< Transmit interrupt IRQ number
  128. uint8_t tei_ipl; ///< Transmit end interrupt priority
  129. IRQn_Type tei_irq; ///< Transmit end interrupt IRQ number
  130. uint8_t eri_ipl; ///< Error interrupt priority
  131. IRQn_Type eri_irq; ///< Error interrupt IRQ number
  132. /** Optional transfer instance used to receive multiple bytes without interrupts. Set to NULL if unused.
  133. * If NULL, the number of bytes allowed in the read API is limited to one byte at a time. */
  134. transfer_instance_t const * p_transfer_rx;
  135. /** Optional transfer instance used to send multiple bytes without interrupts. Set to NULL if unused.
  136. * If NULL, the number of bytes allowed in the write APIs is limited to one byte at a time. */
  137. transfer_instance_t const * p_transfer_tx;
  138. /* Configuration for UART Event processing */
  139. void (* p_callback)(uart_callback_args_t * p_args); ///< Pointer to callback function
  140. void const * p_context; ///< User defined context passed into callback function
  141. /* Pointer to UART peripheral specific configuration */
  142. void const * p_extend; ///< UART hardware dependent configuration
  143. } uart_cfg_t;
  144. /** UART control block. Allocate an instance specific control block to pass into the UART API calls.
  145. */
  146. typedef void uart_ctrl_t;
  147. /** Shared Interface definition for UART */
  148. typedef struct st_uart_api
  149. {
  150. /** Open UART device.
  151. *
  152. * @param[in,out] p_ctrl Pointer to the UART control block. Must be declared by user. Value set here.
  153. * @param[in] uart_cfg_t Pointer to UART configuration structure. All elements of this structure must be set by
  154. * user.
  155. */
  156. fsp_err_t (* open)(uart_ctrl_t * const p_ctrl, uart_cfg_t const * const p_cfg);
  157. /** Read from UART device. The read buffer is used until the read is complete. When a transfer is complete, the
  158. * callback is called with event UART_EVENT_RX_COMPLETE. Bytes received outside an active transfer are received in
  159. * the callback function with event UART_EVENT_RX_CHAR.
  160. * The maximum transfer size is reported by infoGet().
  161. *
  162. * @param[in] p_ctrl Pointer to the UART control block for the channel.
  163. * @param[in] p_dest Destination address to read data from.
  164. * @param[in] bytes Read data length.
  165. */
  166. fsp_err_t (* read)(uart_ctrl_t * const p_ctrl, uint8_t * const p_dest, uint32_t const bytes);
  167. /** Write to UART device. The write buffer is used until write is complete. Do not overwrite write buffer
  168. * contents until the write is finished. When the write is complete (all bytes are fully transmitted on the wire),
  169. * the callback called with event UART_EVENT_TX_COMPLETE.
  170. * The maximum transfer size is reported by infoGet().
  171. *
  172. * @param[in] p_ctrl Pointer to the UART control block.
  173. * @param[in] p_src Source address to write data to.
  174. * @param[in] bytes Write data length.
  175. */
  176. fsp_err_t (* write)(uart_ctrl_t * const p_ctrl, uint8_t const * const p_src, uint32_t const bytes);
  177. /** Change baud rate.
  178. * @warning Calling this API aborts any in-progress transmission and disables reception until the new baud
  179. * settings have been applied.
  180. *
  181. *
  182. * @param[in] p_ctrl Pointer to the UART control block.
  183. * @param[in] p_baudrate_info Pointer to module specific information for configuring baud rate.
  184. */
  185. fsp_err_t (* baudSet)(uart_ctrl_t * const p_ctrl, void const * const p_baudrate_info);
  186. /** Get the driver specific information.
  187. *
  188. * @param[in] p_ctrl Pointer to the UART control block.
  189. * @param[in] baudrate Baud rate in bps.
  190. */
  191. fsp_err_t (* infoGet)(uart_ctrl_t * const p_ctrl, uart_info_t * const p_info);
  192. /**
  193. * Abort ongoing transfer.
  194. *
  195. * @param[in] p_ctrl Pointer to the UART control block.
  196. * @param[in] communication_to_abort Type of abort request.
  197. */
  198. fsp_err_t (* communicationAbort)(uart_ctrl_t * const p_ctrl, uart_dir_t communication_to_abort);
  199. /**
  200. * Specify callback function and optional context pointer and working memory pointer.
  201. *
  202. * @param[in] p_ctrl Pointer to the UART control block.
  203. * @param[in] p_callback Callback function
  204. * @param[in] p_context Pointer to send to callback function
  205. * @param[in] p_working_memory Pointer to volatile memory where callback structure can be allocated.
  206. * Callback arguments allocated here are only valid during the callback.
  207. */
  208. fsp_err_t (* callbackSet)(uart_ctrl_t * const p_ctrl, void (* p_callback)(uart_callback_args_t *),
  209. void const * const p_context, uart_callback_args_t * const p_callback_memory);
  210. /** Close UART device.
  211. *
  212. * @param[in] p_ctrl Pointer to the UART control block.
  213. */
  214. fsp_err_t (* close)(uart_ctrl_t * const p_ctrl);
  215. /** Stop ongoing read and return the number of bytes remaining in the read.
  216. *
  217. * @param[in] p_ctrl Pointer to the UART control block.
  218. * @param[in,out] remaining_bytes Pointer to location to store remaining bytes for read.
  219. */
  220. fsp_err_t (* readStop)(uart_ctrl_t * const p_ctrl, uint32_t * remaining_bytes);
  221. } uart_api_t;
  222. /** This structure encompasses everything that is needed to use an instance of this interface. */
  223. typedef struct st_uart_instance
  224. {
  225. uart_ctrl_t * p_ctrl; ///< Pointer to the control structure for this instance
  226. uart_cfg_t const * p_cfg; ///< Pointer to the configuration structure for this instance
  227. uart_api_t const * p_api; ///< Pointer to the API structure for this instance
  228. } uart_instance_t;
  229. /** @} (end defgroup UART_API) */
  230. /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
  231. FSP_FOOTER
  232. #endif