fpl011.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fpl011.h
  15. * Date: 2022-02-10 14:53:42
  16. * LastEditTime: 2022-02-18 09:07:38
  17. * Description:  This files is for uart functions
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. */
  23. #ifndef BSP_DRIVERS_SERIAL_PL011_UART_H
  24. #define BSP_DRIVERS_SERIAL_PL011_UART_H
  25. #ifdef __cplusplus
  26. extern "C"
  27. {
  28. #endif
  29. /***************************** Include Files *********************************/
  30. #include "ftypes.h"
  31. #include "fassert.h"
  32. #include "fpl011_hw.h"
  33. #include "sdkconfig.h"
  34. /************************** Constant Definitions *****************************/
  35. /**************************** Type Definitions *******************************/
  36. /***************** Macros (Inline Functions) Definitions *********************/
  37. #define FPL011_ERROR_PARAM FT_CODE_ERR(ErrModBsp, ErrBspUart, 0x1u)
  38. #define FPL011_BAUDRATE 115200U
  39. /* Config options */
  40. #define FPL011_OPTION_UARTEN 0x1U
  41. #define FPL011_OPTION_RXEN 0x2U
  42. #define FPL011_OPTION_TXEN 0x4U
  43. #define FPL011_OPTION_FIFOEN 0x8U
  44. #define FPL011_OPTION_CTS 0x10U
  45. #define FPL011_OPTION_RTS 0x20U
  46. #define FPL011_OPTION_DTR 0x40U
  47. #define FPL011_OPTION_RTSEN 0x80U
  48. #define FPL011_OPTION_CTSEN 0x100U
  49. #define FPL011_OPTION_TXDMAEN 0x200U
  50. #define FPL011_OPTION_RXDMAEN 0x400U
  51. /* Channel Operational Mode */
  52. #define FPL011_OPER_MODE_NORMAL (u8)0x00U /* Normal Mode */
  53. #define FPL011_OPER_MODE_LOCAL_LOOP (u8)0x01U /* Local Loop back Mode */
  54. /* Data format values */
  55. #define FPL011_FORMAT_WORDLENGTH_8BIT 0x3
  56. #define FPL011_FORMAT_WORDLENGTH_7BIT 0x2
  57. #define FPL011_FORMAT_WORDLENGTH_6BIT 0x1
  58. #define FPL011_FORMAT_WORDLENGTH_5BIT 0x0
  59. #define FPL011_FORMAT_NO_PARITY 0U /* No parity */
  60. #define FPL011_FORMAT_EN_PARITY 1U /* Enable parity */
  61. #define FPL011_FORMAT_EVEN_PARITY 2U /* Even parity */
  62. #define FPL011_FORMAT_ODD_PARITY 0U /* Odd parity */
  63. #define FPL011_FORMAT_EN_STICK_PARITY 4U /* Stick parity */
  64. #define FPL011_FORMAT_NO_STICK_PARITY 0U /* Stick parity */
  65. #define FPL011_FORMAT_PARITY_MASK 7U /* Format parity mask */
  66. #define FPL011_FORMAT_EVEN_PARITY_SHIFT 1U /* Even parity shift */
  67. #define FPL011_FORMAT_EN_STICK_PARITY_SHIFT 5U /* Stick parity shift */
  68. #define FPL011_FORMAT_2_STOP_BIT 1U
  69. #define FPL011_FORMAT_1_STOP_BIT 0U
  70. /* Callback events */
  71. #define FPL011_EVENT_RECV_DATA 1U /* Data receiving done */
  72. #define FPL011_EVENT_RECV_TOUT 2U /* A receive timeout occurred */
  73. #define FPL011_EVENT_SENT_DATA 3U /* Data transmission done */
  74. #define FPL011_EVENT_RECV_ERROR 4U /* A receive error detected */
  75. #define FPL011_EVENT_MODEM 5U /* Modem status changed */
  76. #define FPL011_EVENT_PARE_FRAME_BRKE 6U /* A receive parity, frame, break \
  77. * error detected */
  78. #define FPL011_EVENT_RECV_ORERR 7U /* A receive overrun error detected */
  79. /**************************** Type Definitions ******************************/
  80. /**
  81. * Keep track of data format setting of a device.
  82. */
  83. typedef struct
  84. {
  85. u32 baudrate ; /* In bps, ie 1200 */
  86. u32 data_bits ; /* Number of data bits */
  87. u32 parity ; /* Parity */
  88. u8 stopbits ; /* Number of stop bits */
  89. } FPl011Format ;
  90. typedef struct
  91. {
  92. u32 instance_id; /* Id of device*/
  93. u32 base_address;
  94. u32 ref_clock_hz;
  95. u32 irq_num;
  96. u32 baudrate;
  97. } FPl011Config;
  98. typedef struct
  99. {
  100. u8 *byte_p;
  101. u32 requested_bytes;
  102. u32 remaining_bytes;
  103. } FPl011Buffer;
  104. typedef void (*FPl011EventHandler)(void *args, u32 event, u32 event_data);
  105. typedef struct
  106. {
  107. FPl011Config config; /* Configuration data structure */
  108. u32 is_ready; /* Device is ininitialized and ready*/
  109. FPl011Buffer send_buffer;
  110. FPl011Buffer receive_buffer;
  111. FPl011EventHandler handler;
  112. void *args;
  113. uint8_t rxbs_error; /* An error occurs during receiving. 0 has no error and 1 has an error */
  114. } FPl011;
  115. /************************** Function Prototypes ******************************/
  116. /* FPl011_uart_sinit.c */
  117. const FPl011Config *FPl011LookupConfig(u32 instance_id);
  118. /* FPl011_uart.c */
  119. FError FPl011CfgInitialize(FPl011 *uart_p, FPl011Config *config);
  120. void FPl011BlockSend(FPl011 *uart_p, u8 *byte_p, u32 length);
  121. u32 FPl011Send(FPl011 *uart_p, u8 *byte_p, u32 length);
  122. u32 FPl011Receive(FPl011 *uart_p, u8 *byte_p, u32 length);
  123. u8 FPl011BlockReceive(FPl011 *uart_p);
  124. void FPl011ProgramCtlReg(FPl011 *uart_p, u32 ctrl_reg);
  125. /* FPl011_uart_options.c */
  126. void FPl011SetOperMode(FPl011 *uart_p, u8 operation_mode);
  127. void FPl011SetOptions(FPl011 *uart_p, u32 options);
  128. void FPl011SetSpecificOptions(FPl011 *uart_p, u32 options);
  129. void FPl011ClearSpecificOptions(FPl011 *uart_p, u32 options);
  130. FError FPl011SetBaudRate(FPl011 *uart_p, u32 baudrate) ;
  131. void FPl011GetDataFormat(FPl011 *uart_p, FPl011Format *format_p) ;
  132. FError FPl011SetDataFormat(FPl011 *uart_p, FPl011Format *format_p) ;
  133. void FPl011SetTxFifoThreadHold(FPl011 *uart_p, u8 trigger_level) ;
  134. void FPl011SetRxFifoThreadhold(FPl011 *uart_p, u8 trigger_level) ;
  135. /* FPl011_uart_intr.c */
  136. u32 FPl011GetInterruptMask(FPl011 *uart_p) ;
  137. void FPl011InterruptHandler(s32 vector, void *param);
  138. void FPl011SetHandler(FPl011 *uart_p, FPl011EventHandler fun_p, void *args);
  139. void FPl011SetInterruptMask(FPl011 *uart_p, u32 mask);
  140. #ifdef __cplusplus
  141. }
  142. #endif
  143. #endif // !