stm8l15x_usart.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**
  2. ********************************************************************************
  3. * @file stm8l15x_usart.h
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file contains all the functions prototypes for the USART firmware
  8. * library.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  13. *
  14. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15. * You may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at:
  17. *
  18. * http://www.st.com/software_license_agreement_liberty_v2
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. *
  26. ******************************************************************************
  27. */
  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __STM8L15x_USART_H
  30. #define __STM8L15x_USART_H
  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm8l15x.h"
  33. #include "stm8l15x_clk.h"
  34. /** @addtogroup STM8L15x_StdPeriph_Driver
  35. * @{
  36. */
  37. /** @addtogroup USART
  38. * @{
  39. */
  40. /* Exported types ------------------------------------------------------------*/
  41. /** @defgroup USART_Exported_Types
  42. * @{
  43. */
  44. /** @defgroup USART_Flags
  45. * @{
  46. */
  47. typedef enum
  48. {
  49. USART_FLAG_TXE = (uint16_t)0x0080, /*!< Transmit Data Register Empty flag */
  50. USART_FLAG_TC = (uint16_t)0x0040, /*!< Transmission Complete flag */
  51. USART_FLAG_RXNE = (uint16_t)0x0020, /*!< Read Data Register Not Empty flag */
  52. USART_FLAG_IDLE = (uint16_t)0x0010, /*!< Idle line detected flag */
  53. USART_FLAG_OR = (uint16_t)0x0008, /*!< OverRun error flag */
  54. USART_FLAG_NF = (uint16_t)0x0004, /*!< Noise error flag */
  55. USART_FLAG_FE = (uint16_t)0x0002, /*!< Framing Error flag */
  56. USART_FLAG_PE = (uint16_t)0x0001, /*!< Parity Error flag */
  57. USART_FLAG_SBK = (uint16_t)0x0101 /*!< Send Break characters Flag */
  58. } USART_FLAG_TypeDef;
  59. #define IS_USART_FLAG(Flag) \
  60. (((Flag) == USART_FLAG_TXE) || \
  61. ((Flag) == USART_FLAG_TC) || \
  62. ((Flag) == USART_FLAG_RXNE) || \
  63. ((Flag) == USART_FLAG_IDLE) || \
  64. ((Flag) == USART_FLAG_OR) || \
  65. ((Flag) == USART_FLAG_NF) || \
  66. ((Flag) == USART_FLAG_FE) || \
  67. ((Flag) == USART_FLAG_PE) || \
  68. ((Flag) == USART_FLAG_SBK))
  69. #define IS_USART_CLEAR_FLAG(Flag) (((Flag) == USART_FLAG_TC))
  70. /**
  71. * @}
  72. */
  73. /** @defgroup USART_Interrupts
  74. * @{
  75. */
  76. /**
  77. * @brief USART Interrupt definition
  78. * USART_IT possible values
  79. * Elements values convention: 0x0ZYX
  80. * X: Position of the corresponding Interrupt
  81. * Y: Flag position
  82. * Z: Register index
  83. */
  84. typedef enum
  85. {
  86. USART_IT_TXE = (uint16_t)0x0277, /*!< Transmit interrupt */
  87. USART_IT_TC = (uint16_t)0x0266, /*!< Transmission Complete interrupt */
  88. USART_IT_RXNE = (uint16_t)0x0255, /*!< Receive interrupt */
  89. USART_IT_IDLE = (uint16_t)0x0244, /*!< IDLE line interrupt */
  90. USART_IT_OR = (uint16_t)0x0235, /*!< Overrun Error interrupt */
  91. USART_IT_PE = (uint16_t)0x0100, /*!< Parity Error interrupt */
  92. USART_IT_ERR = (uint16_t)0x0500, /*!< Error interrupt */
  93. USART_IT_NF = (uint16_t)0x0102, /*!< Noise Error interrupt */
  94. USART_IT_FE = (uint16_t)0x0101 /*!< Frame Error interrupt */
  95. } USART_IT_TypeDef;
  96. #define IS_USART_CONFIG_IT(Interrupt) \
  97. (((Interrupt) == USART_IT_PE) || \
  98. ((Interrupt) == USART_IT_TXE) || \
  99. ((Interrupt) == USART_IT_TC) || \
  100. ((Interrupt) == USART_IT_RXNE) || \
  101. ((Interrupt) == USART_IT_OR) || \
  102. ((Interrupt) == USART_IT_ERR) || \
  103. ((Interrupt) == USART_IT_IDLE))
  104. #define IS_USART_GET_IT(ITPendingBit) \
  105. (((ITPendingBit) == USART_IT_TXE) || \
  106. ((ITPendingBit) == USART_IT_TC) || \
  107. ((ITPendingBit) == USART_IT_RXNE) || \
  108. ((ITPendingBit) == USART_IT_IDLE) || \
  109. ((ITPendingBit) == USART_IT_OR) || \
  110. ((ITPendingBit) == USART_IT_PE))
  111. #define IS_USART_CLEAR_IT(IT) (((IT) == USART_IT_TC) || ((IT) == USART_IT_RXNE))
  112. /**
  113. * @}
  114. */
  115. /** @defgroup USART_Wakeup_Modes
  116. * @{
  117. */
  118. typedef enum
  119. {
  120. USART_WakeUp_IdleLine = (uint8_t)0x00, /*!< 0x01 Idle Line wake up */
  121. USART_WakeUp_AddressMark = (uint8_t)0x08 /*!< 0x02 Address Mark wake up */
  122. } USART_WakeUp_TypeDef;
  123. #define IS_USART_WAKEUP(WakeUpMode)(((WakeUpMode) == USART_WakeUp_IdleLine) || \
  124. ((WakeUpMode) == USART_WakeUp_AddressMark))
  125. /**
  126. * @}
  127. */
  128. /** @defgroup USART_Stop_Bits
  129. * @{
  130. */
  131. typedef enum
  132. {
  133. USART_StopBits_1 = (uint8_t)0x00, /*!< One stop bit is transmitted at the end of frame*/
  134. USART_StopBits_2 = (uint8_t)0x20, /*!< Two stop bits are transmitted at the end of frame*/
  135. USART_StopBits_1_5 = (uint8_t)0x30 /*!< One and half stop bits*/
  136. } USART_StopBits_TypeDef;
  137. #define IS_USART_STOPBITS(StopBit)(((StopBit) == USART_StopBits_1) || \
  138. ((StopBit) == USART_StopBits_1_5) || \
  139. ((StopBit) == USART_StopBits_2))
  140. /**
  141. * @}
  142. */
  143. /** @defgroup USART_Parity
  144. * @{
  145. */
  146. typedef enum
  147. {
  148. USART_Parity_No = (uint8_t)0x00, /*!< No Parity*/
  149. USART_Parity_Even = (uint8_t)0x04, /*!< Even Parity*/
  150. USART_Parity_Odd = (uint8_t)0x06 /*!< Odd Parity*/
  151. } USART_Parity_TypeDef;
  152. #define IS_USART_PARITY(Parity)(((Parity) == USART_Parity_No) || \
  153. ((Parity) == USART_Parity_Even) || \
  154. ((Parity) == USART_Parity_Odd ))
  155. /**
  156. * @}
  157. */
  158. /** @defgroup USART_Lin_Break_Detection_Length
  159. * @{
  160. */
  161. typedef enum
  162. {
  163. USART_LINBreakDetectionLength_10BITS = (uint8_t)0x00, /*!< 10 bits Lin Break detection */
  164. USART_LINBreakDetectionLength_11BITS = (uint8_t)0x01 /*!< 11 bits Lin Break detection */
  165. } USART_LINBreakDetectionLength_TypeDef;
  166. /**
  167. * @}
  168. */
  169. /** @defgroup USART_Word_Length
  170. * @{
  171. */
  172. typedef enum
  173. {
  174. USART_WordLength_8b = (uint8_t)0x00, /*!< 8 bits Data */
  175. USART_WordLength_9b = (uint8_t)0x10 /*!< 9 bits Data */
  176. } USART_WordLength_TypeDef;
  177. #define IS_USART_WORDLENGTH(WordLength) (((WordLength) == USART_WordLength_8b) || \
  178. ((WordLength) == USART_WordLength_9b))
  179. /**
  180. * @}
  181. */
  182. /** @defgroup USART_Mode
  183. * @{
  184. */
  185. typedef enum
  186. {
  187. USART_Mode_Rx = (uint8_t)0x04, /*!< Receive Enable */
  188. USART_Mode_Tx = (uint8_t)0x08 /*!< Transmit Enable */
  189. } USART_Mode_TypeDef;
  190. #define IS_USART_MODE(MODE) ((((MODE) & (uint8_t)0xF3) == 0x00) && ((MODE) != (uint16_t)0x00))
  191. /**
  192. * @}
  193. */
  194. /** @defgroup USART_DMA_Requests
  195. * @{
  196. */
  197. typedef enum
  198. {
  199. USART_DMAReq_TX = (uint8_t)0x80, /*!< Receive DMA request Enable */
  200. USART_DMAReq_RX = (uint8_t)0x40 /*!< Transmit DMA request Enable */
  201. } USART_DMAReq_TypeDef;
  202. #define IS_USART_DMAREQ(DMAReq) ((((DMAReq) & (uint8_t)0x3F) == 0x00) && ((DMAReq) != (uint8_t)0x00))
  203. /**
  204. * @}
  205. */
  206. /** @defgroup USART_IrDA_Mode
  207. * @{
  208. */
  209. typedef enum
  210. {
  211. USART_IrDAMode_Normal = (uint8_t)0x00, /*!< IrDA Normal Mode */
  212. USART_IrDAMode_LowPower = (uint8_t)0x01 /*!< IrDA Low Power Mode */
  213. } USART_IrDAMode_TypeDef;
  214. #define IS_USART_IRDAMODE(IrDAMode) (((IrDAMode) == USART_IrDAMode_LowPower) || \
  215. ((IrDAMode) == USART_IrDAMode_Normal))
  216. /**
  217. * @}
  218. */
  219. /** @defgroup USART_Clock
  220. * @{
  221. */
  222. typedef enum
  223. {
  224. USART_Clock_Disable = (uint8_t)0x00, /*!< CK pin disabled */
  225. USART_Clock_Enable = (uint8_t)0x08 /*!< CK pin enabled */
  226. } USART_Clock_TypeDef;
  227. #define IS_USART_CLOCK(CLOCK) (((CLOCK) == USART_Clock_Disable) ||((CLOCK) == USART_Clock_Enable))
  228. /**
  229. * @}
  230. */
  231. /** @defgroup USART_Clock_Polarity
  232. * @{
  233. */
  234. typedef enum
  235. {
  236. USART_CPOL_Low = (uint8_t)0x00, /*!< CK to 0 when idle */
  237. USART_CPOL_High = (uint8_t)0x04 /*!< CK to 1 when idle.*/
  238. } USART_CPOL_TypeDef;
  239. #define IS_USART_CPOL(CPOL) (((CPOL) == USART_CPOL_Low) || ((CPOL) == USART_CPOL_High))
  240. /**
  241. * @}
  242. */
  243. /** @defgroup USART_Clock_Phase
  244. * @{
  245. */
  246. typedef enum
  247. {
  248. USART_CPHA_1Edge = (uint8_t)0x00, /*!< The first clock transition is the first data capture edge*/
  249. USART_CPHA_2Edge = (uint8_t)0x02 /*!< The second clock transition is the first data capture edge*/
  250. } USART_CPHA_TypeDef;
  251. #define IS_USART_CPHA(CPHA) (((CPHA) == USART_CPHA_1Edge) || ((CPHA) == USART_CPHA_2Edge))
  252. /**
  253. * @}
  254. */
  255. /** @defgroup USART_LastBit
  256. * @{
  257. */
  258. typedef enum
  259. {
  260. USART_LastBit_Disable = (uint8_t)0x00, /*!< The clock pulse of the last data bit is not output to the SCLK pin.*/
  261. USART_LastBit_Enable = (uint8_t)0x01 /*!< The clock pulse of the last data bit is output to the SCLK pin.*/
  262. } USART_LastBit_TypeDef;
  263. #define IS_USART_LASTBIT(LASTBIT) (((LASTBIT) == USART_LastBit_Disable) || \
  264. ((LASTBIT) == USART_LastBit_Enable))
  265. /**
  266. * @}
  267. */
  268. /**
  269. * @}
  270. */
  271. /* Exported constants --------------------------------------------------------*/
  272. /* Exported macros -----------------------------------------------------------*/
  273. /** @defgroupUSART_Exported_Macros
  274. * @{
  275. */
  276. /* BaudRate value should be < 625000 bps */
  277. #define IS_USART_BAUDRATE(NUM) ((NUM) <= (uint32_t)625000)
  278. #define USART_ADDRESS_MAX ((uint8_t)16)
  279. #define IS_USART_ADDRESS(address) ((address) < USART_ADDRESS_MAX)
  280. #define USART_DATA_9BITS_MAX ((uint16_t)0x1FF)
  281. #define IS_USART_DATA_9BITS(DATA) ((DATA) < USART_DATA_9BITS_MAX)
  282. /**
  283. * @}
  284. */
  285. /* Exported functions ------------------------------------------------------- */
  286. /* Function used to set the USART configuration to the default reset state ***/
  287. void USART_DeInit(USART_TypeDef* USARTx);
  288. /* Initialization and Configuration functions *********************************/
  289. void USART_Init(USART_TypeDef* USARTx, uint32_t BaudRate, USART_WordLength_TypeDef
  290. USART_WordLength, USART_StopBits_TypeDef USART_StopBits,
  291. USART_Parity_TypeDef USART_Parity, USART_Mode_TypeDef USART_Mode);
  292. void USART_ClockInit(USART_TypeDef* USARTx, USART_Clock_TypeDef USART_Clock,
  293. USART_CPOL_TypeDef USART_CPOL, USART_CPHA_TypeDef USART_CPHA,
  294. USART_LastBit_TypeDef USART_LastBit);
  295. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState);
  296. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler);
  297. void USART_SendBreak(USART_TypeDef* USARTx);
  298. /* Data transfers functions ***************************************************/
  299. void USART_SendData8(USART_TypeDef* USARTx, uint8_t Data);
  300. void USART_SendData9(USART_TypeDef* USARTx, uint16_t Data);
  301. uint8_t USART_ReceiveData8(USART_TypeDef* USARTx);
  302. uint16_t USART_ReceiveData9(USART_TypeDef* USARTx);
  303. /* Multi-Processor Communication functions ************************************/
  304. void USART_WakeUpConfig(USART_TypeDef* USARTx, USART_WakeUp_TypeDef USART_WakeUp);
  305. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  306. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address);
  307. /* Half-duplex mode function **************************************************/
  308. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  309. /* Smartcard mode functions ***************************************************/
  310. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  311. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState);
  312. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime);
  313. /* IrDA mode functions ********************************************************/
  314. void USART_IrDAConfig(USART_TypeDef* USARTx, USART_IrDAMode_TypeDef USART_IrDAMode);
  315. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState);
  316. /* DMA transfers management functions *****************************************/
  317. void USART_DMACmd(USART_TypeDef* USARTx, USART_DMAReq_TypeDef USART_DMAReq,
  318. FunctionalState NewState);
  319. /* Interrupts and flags management functions **********************************/
  320. void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT,
  321. FunctionalState NewState);
  322. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
  323. void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
  324. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
  325. void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
  326. #endif /* __STM8L15x_USART_H */
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */
  333. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/