lib_uart.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. ******************************************************************************
  3. * @file lib_uart.h
  4. * @author Application Team
  5. * @version V4.4.0
  6. * @date 2018-09-27
  7. * @brief UART library.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. ******************************************************************************
  12. */
  13. #ifndef __LIB_UART_H
  14. #define __LIB_UART_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include "target.h"
  19. //UART Init struct
  20. typedef struct
  21. {
  22. uint32_t Mode;
  23. uint32_t Parity;
  24. uint32_t WordLen;
  25. uint32_t FirstBit;
  26. uint32_t Baudrate;
  27. } UART_InitType;
  28. //Mode
  29. #define UART_MODE_RX UART_CTRL_RXEN
  30. #define UART_MODE_TX UART_CTRL_TXEN
  31. #define UART_MODE_OFF 0
  32. #define UART_MODE_Msk (UART_CTRL_RXEN | UART_CTRL_TXEN)
  33. //Parity
  34. #define UART_PARITY_EVEN UART_CTRL2_PMODE_EVEN
  35. #define UART_PARITY_ODD UART_CTRL2_PMODE_ODD
  36. #define UART_PARITY_0 UART_CTRL2_PMODE_0
  37. #define UART_PARITY_1 UART_CTRL2_PMODE_1
  38. #define UART_PARITY_NONE 0
  39. //WordLen
  40. #define UART_WORDLEN_8B 0
  41. #define UART_WORDLEN_9B UART_CTRL2_MODE
  42. //FirstBit
  43. #define UART_FIRSTBIT_LSB 0
  44. #define UART_FIRSTBIT_MSB UART_CTRL2_MSB
  45. //UART Configration Information struct
  46. typedef struct
  47. {
  48. uint32_t Mode_Transmit :1; //1: TX Enable; 0: TX Disable
  49. uint32_t Mode_Receive :1; //1: RX Enable; 0: RX Disable
  50. uint32_t Baudrate; //The value of current budrate
  51. uint8_t Parity; //0: parity bit=0; 1: parity bit=1; 2: Even parity; 3:Odd parity
  52. uint8_t WordLen; //8: data bits=8; 9: data bits=9
  53. uint8_t FirstBit; //0: LSB transmit first; 1: MSB transmit first
  54. } UART_ConfigINFOType;
  55. //status
  56. #define UART_FLAG_RXPARITY UART_STATE_RXPSTS
  57. #define UART_FLAG_TXDONE UART_STATE_TXDONE
  58. #define UART_FLAG_RXPE UART_STATE_RXPE
  59. #define UART_FLAG_RXOV UART_STATE_RXOV
  60. #define UART_FLAG_TXOV UART_STATE_TXOV
  61. #define UART_FLAG_RXFULL UART_STATE_RXFULL
  62. #define UART_FLAG_RCMsk (UART_FLAG_TXDONE \
  63. |UART_FLAG_RXPE \
  64. |UART_FLAG_RXOV \
  65. |UART_STATE_RXFULL\
  66. |UART_FLAG_TXOV)
  67. //interrupt
  68. #define UART_INT_TXDONE UART_CTRL_TXDONEIE
  69. #define UART_INT_RXPE UART_CTRL_RXPEIE
  70. #define UART_INT_RXOV UART_CTRL_RXOVIE
  71. #define UART_INT_TXOV UART_CTRL_TXOVIE
  72. #define UART_INT_RX UART_CTRL_RXIE
  73. #define UART_INT_Msk (UART_INT_TXDONE \
  74. |UART_INT_RXPE \
  75. |UART_INT_RXOV \
  76. |UART_INT_TXOV \
  77. |UART_INT_RX)
  78. //INTStatus
  79. #define UART_INTSTS_TXDONE UART_INTSTS_TXDONEIF
  80. #define UART_INTSTS_RXPE UART_INTSTS_RXPEIF
  81. #define UART_INTSTS_RXOV UART_INTSTS_RXOVIF
  82. #define UART_INTSTS_TXOV UART_INTSTS_TXOVIF
  83. #define UART_INTSTS_RX UART_INTSTS_RXIF
  84. #define UART_INTSTS_Msk (UART_INTSTS_TXDONE \
  85. |UART_INTSTS_RXPE \
  86. |UART_INTSTS_RXOV \
  87. |UART_INTSTS_TXOV \
  88. |UART_INTSTS_RX)
  89. /* Private macros ------------------------------------------------------------*/
  90. #define IS_UART_MODE(__MODE__) (((((__MODE__) & UART_MODE_Msk) != 0U) && (((__MODE__) & ~UART_MODE_Msk) == 0U)) ||\
  91. ((__MODE__) == UART_MODE_OFF))
  92. #define IS_UART_PARITY(__PARITY__) (((__PARITY__) == UART_PARITY_EVEN) ||\
  93. ((__PARITY__) == UART_PARITY_ODD) ||\
  94. ((__PARITY__) == UART_PARITY_0) ||\
  95. ((__PARITY__) == UART_PARITY_1) ||\
  96. ((__PARITY__) == UART_PARITY_NONE))
  97. #define IS_UART_WORDLEN(__WORDLEN__) (((__WORDLEN__) == UART_WORDLEN_8B) ||\
  98. ((__WORDLEN__) == UART_WORDLEN_9B))
  99. #define IS_UART_FIRSTBIT(__FIRSTBIT__) (((__FIRSTBIT__) == UART_FIRSTBIT_LSB) ||\
  100. ((__FIRSTBIT__) == UART_FIRSTBIT_MSB))
  101. #define IS_UART_BAUDRATE(__BAUDRATE__) ((__BAUDRATE__) < 0x100000UL)
  102. #define IS_UART_FLAGR(__FLAGR__) (((__FLAGR__) == UART_FLAG_RXPARITY) ||\
  103. ((__FLAGR__) == UART_FLAG_TXDONE) ||\
  104. ((__FLAGR__) == UART_FLAG_RXPE) ||\
  105. ((__FLAGR__) == UART_FLAG_RXOV) ||\
  106. ((__FLAGR__) == UART_FLAG_TXOV) ||\
  107. ((__FLAGR__) == UART_FLAG_RXFULL))
  108. #define IS_UART_FLAGC(__FLAGC__) ((((__FLAGC__) & UART_FLAG_RCMsk) != 0U) &&\
  109. (((__FLAGC__) & ~UART_FLAG_RCMsk) == 0U))
  110. #define IS_UART_INT(__INT__) ((((__INT__) & UART_INT_Msk) != 0U) &&\
  111. (((__INT__) & ~UART_INT_Msk) == 0U))
  112. #define IS_UART_INTFLAGR(__INTFLAGR__) (((__INTFLAGR__) == UART_INTSTS_TXDONE) ||\
  113. ((__INTFLAGR__) == UART_INTSTS_RXPE) ||\
  114. ((__INTFLAGR__) == UART_INTSTS_RXOV) ||\
  115. ((__INTFLAGR__) == UART_INTSTS_TXOV) ||\
  116. ((__INTFLAGR__) == UART_INTSTS_RX))
  117. #define IS_UART_INTFLAGC(__INTFLAGC__) ((((__INTFLAGC__) & UART_INTSTS_Msk) != 0U) &&\
  118. (((__INTFLAGC__) & ~UART_INTSTS_Msk) == 0U))
  119. /* Exported Functions ------------------------------------------------------- */
  120. /* UART Exported Functions Group1:
  121. Initialization and functions --------------*/
  122. void UART_DeInit(UART_TypeDef *UARTx);
  123. void UART_Init(UART_TypeDef *UARTx, UART_InitType *InitStruct);
  124. void UART_StructInit(UART_InitType *InitStruct);
  125. /* UART Exported Functions Group2:
  126. (Interrupt) Flag --------------------------*/
  127. uint8_t UART_GetFlag(UART_TypeDef *UARTx, uint32_t FlagMask);
  128. void UART_ClearFlag(UART_TypeDef *UARTx, uint32_t FlagMask);
  129. void UART_INTConfig(UART_TypeDef *UARTx, uint32_t INTMask, uint8_t NewState);
  130. uint8_t UART_GetINTStatus(UART_TypeDef *UARTx, uint32_t INTMask);
  131. void UART_ClearINTStatus(UART_TypeDef *UARTx, uint32_t INTMask);
  132. /* UART Exported Functions Group3:
  133. Transfer datas ----------------------------*/
  134. void UART_SendData(UART_TypeDef *UARTx, uint8_t ch);
  135. uint8_t UART_ReceiveData(UART_TypeDef *UARTx);
  136. /* UART Exported Functions Group4:
  137. MISC Configuration ------------------------*/
  138. void UART_BaudrateConfig(UART_TypeDef *UARTx, uint32_t BaudRate);
  139. void UART_Cmd(UART_TypeDef *UARTx, uint32_t Mode, uint32_t NewState);
  140. void UART_GetConfigINFO(UART_TypeDef *UARTx, UART_ConfigINFOType *ConfigInfo);
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif /* __LIB_UART_H */
  145. /*********************************** END OF FILE ******************************/