r_sci_uart.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /***********************************************************************************************************************
  2. * Copyright [2020-2021] 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. #ifndef R_SCI_UART_H
  21. #define R_SCI_UART_H
  22. /*******************************************************************************************************************//**
  23. * @addtogroup SCI_UART
  24. * @{
  25. **********************************************************************************************************************/
  26. /***********************************************************************************************************************
  27. * Includes
  28. **********************************************************************************************************************/
  29. #include "bsp_api.h"
  30. #include "r_uart_api.h"
  31. #include "r_sci_uart_cfg.h"
  32. /* Common macro for FSP header files. There is also a corresponding FSP_FOOTER macro at the end of this file. */
  33. FSP_HEADER
  34. /***********************************************************************************************************************
  35. * Macro definitions
  36. **********************************************************************************************************************/
  37. /**********************************************************************************************************************
  38. * Typedef definitions
  39. **********************************************************************************************************************/
  40. /** Enumeration for SCI clock source */
  41. typedef enum e_sci_clk_src
  42. {
  43. SCI_UART_CLOCK_INT, ///< Use internal clock for baud generation
  44. SCI_UART_CLOCK_INT_WITH_BAUDRATE_OUTPUT, ///< Use internal clock for baud generation and output on SCK
  45. SCI_UART_CLOCK_EXT8X, ///< Use external clock 8x baud rate
  46. SCI_UART_CLOCK_EXT16X ///< Use external clock 16x baud rate
  47. } sci_clk_src_t;
  48. /** UART flow control mode definition */
  49. typedef enum e_sci_uart_flow_control
  50. {
  51. SCI_UART_FLOW_CONTROL_RTS = 0U, ///< Use SCI pin for RTS
  52. SCI_UART_FLOW_CONTROL_CTS = 1U, ///< Use SCI pin for CTS
  53. SCI_UART_FLOW_CONTROL_CTSRTS = 3U, ///< Use SCI pin for CTS, external pin for RTS
  54. SCI_UART_FLOW_CONTROL_HARDWARE_CTSRTS = 8U, ///< Use CTSn_RTSn pin for RTS and CTSn pin for CTS. Available only for some channels on selected MCUs. See hardware manual for channel specific options
  55. } sci_uart_flow_control_t;
  56. /** UART instance control block. */
  57. typedef struct st_sci_uart_instance_ctrl
  58. {
  59. /* Parameters to control UART peripheral device */
  60. uint8_t fifo_depth; // FIFO depth of the UART channel
  61. uint8_t rx_transfer_in_progress; // Set to 1 if a receive transfer is in progress, 0 otherwise
  62. uint8_t data_bytes : 2; // 1 byte for 7 or 8 bit data, 2 bytes for 9 bit data
  63. uint8_t bitrate_modulation : 1; // 1 if bit rate modulation is enabled, 0 otherwise
  64. uint32_t open; // Used to determine if the channel is configured
  65. bsp_io_port_pin_t flow_pin;
  66. /* Source buffer pointer used to fill hardware FIFO from transmit ISR. */
  67. uint8_t const * p_tx_src;
  68. /* Size of source buffer pointer used to fill hardware FIFO from transmit ISR. */
  69. uint32_t tx_src_bytes;
  70. /* Destination buffer pointer used for receiving data. */
  71. uint8_t const * p_rx_dest;
  72. /* Size of destination buffer pointer used for receiving data. */
  73. uint32_t rx_dest_bytes;
  74. /* Pointer to the configuration block. */
  75. uart_cfg_t const * p_cfg;
  76. /* Base register for this channel */
  77. R_SCI0_Type * p_reg;
  78. void (* p_callback)(uart_callback_args_t *); // Pointer to callback that is called when a uart_event_t occurs.
  79. uart_callback_args_t * p_callback_memory; // Pointer to non-secure memory that can be used to pass arguments to a callback in non-secure memory.
  80. /* Pointer to context to be passed into callback function */
  81. void const * p_context;
  82. } sci_uart_instance_ctrl_t;
  83. /** Receive FIFO trigger configuration. */
  84. typedef enum e_sci_uart_rx_fifo_trigger
  85. {
  86. SCI_UART_RX_FIFO_TRIGGER_1 = 0x1, ///< Callback after each byte is received without buffering
  87. SCI_UART_RX_FIFO_TRIGGER_MAX = 0xF, ///< Callback when FIFO is full or after 15 bit times with no data (fewer interrupts)
  88. } sci_uart_rx_fifo_trigger_t;
  89. /** Asynchronous Start Bit Edge Detection configuration. */
  90. typedef enum e_sci_uart_start_bit_detect
  91. {
  92. SCI_UART_START_BIT_LOW_LEVEL = 0x0, ///< Detect low level on RXDn pin as start bit
  93. SCI_UART_START_BIT_FALLING_EDGE = 0x1, ///< Detect falling level on RXDn pin as start bit
  94. } sci_uart_start_bit_detect_t;
  95. /** Noise cancellation configuration. */
  96. typedef enum e_sci_uart_noise_cancellation
  97. {
  98. SCI_UART_NOISE_CANCELLATION_DISABLE = 0x0, ///< Disable noise cancellation
  99. SCI_UART_NOISE_CANCELLATION_ENABLE = 0x1, ///< Enable noise cancellation
  100. } sci_uart_noise_cancellation_t;
  101. /** Register settings to acheive a desired baud rate and modulation duty. */
  102. typedef struct st_baud_setting_t
  103. {
  104. union
  105. {
  106. uint8_t semr_baudrate_bits;
  107. struct
  108. {
  109. uint8_t : 2;
  110. uint8_t brme : 1; ///< Bit Rate Modulation Enable
  111. uint8_t abcse : 1; ///< Asynchronous Mode Extended Base Clock Select 1
  112. uint8_t abcs : 1; ///< Asynchronous Mode Base Clock Select
  113. uint8_t : 1;
  114. uint8_t bgdm : 1; ///< Baud Rate Generator Double-Speed Mode Select
  115. uint8_t : 1;
  116. };
  117. };
  118. uint8_t cks : 2; ///< CKS value to get divisor (CKS = N)
  119. uint8_t brr; ///< Bit Rate Register setting
  120. uint8_t mddr; ///< Modulation Duty Register setting
  121. } baud_setting_t;
  122. /** UART on SCI device Configuration */
  123. typedef struct st_sci_uart_extended_cfg
  124. {
  125. sci_clk_src_t clock; ///< The source clock for the baud-rate generator. If internal optionally output baud rate on SCK
  126. sci_uart_start_bit_detect_t rx_edge_start; ///< Start reception on falling edge
  127. sci_uart_noise_cancellation_t noise_cancel; ///< Noise cancellation setting
  128. baud_setting_t * p_baud_setting; ///< Register settings for a desired baud rate.
  129. sci_uart_rx_fifo_trigger_t rx_fifo_trigger; ///< Receive FIFO trigger level, unused if channel has no FIFO or if DTC is used.
  130. bsp_io_port_pin_t flow_control_pin; ///< UART Driver Enable pin
  131. sci_uart_flow_control_t flow_control; ///< CTS/RTS function of the SSn pin
  132. } sci_uart_extended_cfg_t;
  133. /**********************************************************************************************************************
  134. * Exported global variables
  135. **********************************************************************************************************************/
  136. /** @cond INC_HEADER_DEFS_SEC */
  137. /** Filled in Interface API structure for this Instance. */
  138. extern const uart_api_t g_uart_on_sci;
  139. /** @endcond */
  140. fsp_err_t R_SCI_UART_Open(uart_ctrl_t * const p_api_ctrl, uart_cfg_t const * const p_cfg);
  141. fsp_err_t R_SCI_UART_Read(uart_ctrl_t * const p_api_ctrl, uint8_t * const p_dest, uint32_t const bytes);
  142. fsp_err_t R_SCI_UART_Write(uart_ctrl_t * const p_api_ctrl, uint8_t const * const p_src, uint32_t const bytes);
  143. fsp_err_t R_SCI_UART_BaudSet(uart_ctrl_t * const p_api_ctrl, void const * const p_baud_setting);
  144. fsp_err_t R_SCI_UART_InfoGet(uart_ctrl_t * const p_api_ctrl, uart_info_t * const p_info);
  145. fsp_err_t R_SCI_UART_Close(uart_ctrl_t * const p_api_ctrl);
  146. fsp_err_t R_SCI_UART_Abort(uart_ctrl_t * const p_api_ctrl, uart_dir_t communication_to_abort);
  147. fsp_err_t R_SCI_UART_BaudCalculate(uint32_t baudrate,
  148. bool bitrate_modulation,
  149. uint32_t baud_rate_error_x_1000,
  150. baud_setting_t * const p_baud_setting);
  151. fsp_err_t R_SCI_UART_CallbackSet(uart_ctrl_t * const p_api_ctrl,
  152. void ( * p_callback)(uart_callback_args_t *),
  153. void const * const p_context,
  154. uart_callback_args_t * const p_callback_memory);
  155. fsp_err_t R_SCI_UART_ReadStop(uart_ctrl_t * const p_api_ctrl, uint32_t * remaining_bytes);
  156. /*******************************************************************************************************************//**
  157. * @} (end addtogroup SCI_UART)
  158. **********************************************************************************************************************/
  159. /* Common macro for FSP header files. There is also a corresponding FSP_HEADER macro at the top of this file. */
  160. FSP_FOOTER
  161. #endif