efm32_usart.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Universal synchronous/asynchronous receiver/transmitter (USART/UART)
  4. * peripheral API for EFM32.
  5. * @author Energy Micro AS
  6. * @version 1.3.0
  7. *******************************************************************************
  8. * @section License
  9. * <b>(C) Copyright 2010 Energy Micro AS, http://www.energymicro.com</b>
  10. *******************************************************************************
  11. *
  12. * This source code is the property of Energy Micro AS. The source and compiled
  13. * code may only be used on Energy Micro "EFM32" microcontrollers.
  14. *
  15. * This copyright notice may not be removed from the source code nor changed.
  16. *
  17. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  18. * obligation to support this Software. Energy Micro AS is providing the
  19. * Software "AS IS", with no express or implied warranties of any kind,
  20. * including, but not limited to, any implied warranties of merchantability
  21. * or fitness for any particular purpose or warranties against infringement
  22. * of any proprietary rights of a third party.
  23. *
  24. * Energy Micro AS will not be liable for any consequential, incidental, or
  25. * special damages, or any other relief, or for any claim by any third party,
  26. * arising from your use of this Software.
  27. *
  28. ******************************************************************************/
  29. #ifndef __EFM32_USART_H
  30. #define __EFM32_USART_H
  31. #include <stdbool.h>
  32. #include "efm32.h"
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /***************************************************************************//**
  37. * @addtogroup EFM32_Library
  38. * @{
  39. ******************************************************************************/
  40. /***************************************************************************//**
  41. * @addtogroup USART
  42. * @{
  43. ******************************************************************************/
  44. /*******************************************************************************
  45. ******************************** ENUMS ************************************
  46. ******************************************************************************/
  47. /** Databit selection. */
  48. typedef enum
  49. {
  50. usartDatabits4 = USART_FRAME_DATABITS_FOUR, /**< 4 databits (not available for UART). */
  51. usartDatabits5 = USART_FRAME_DATABITS_FIVE, /**< 5 databits (not available for UART). */
  52. usartDatabits6 = USART_FRAME_DATABITS_SIX, /**< 6 databits (not available for UART). */
  53. usartDatabits7 = USART_FRAME_DATABITS_SEVEN, /**< 7 databits (not available for UART). */
  54. usartDatabits8 = USART_FRAME_DATABITS_EIGHT, /**< 8 databits. */
  55. usartDatabits9 = USART_FRAME_DATABITS_NINE, /**< 9 databits. */
  56. usartDatabits10 = USART_FRAME_DATABITS_TEN, /**< 10 databits (not available for UART). */
  57. usartDatabits11 = USART_FRAME_DATABITS_ELEVEN, /**< 11 databits (not available for UART). */
  58. usartDatabits12 = USART_FRAME_DATABITS_TWELVE, /**< 12 databits (not available for UART). */
  59. usartDatabits13 = USART_FRAME_DATABITS_THIRTEEN, /**< 13 databits (not available for UART). */
  60. usartDatabits14 = USART_FRAME_DATABITS_FOURTEEN, /**< 14 databits (not available for UART). */
  61. usartDatabits15 = USART_FRAME_DATABITS_FIFTEEN, /**< 15 databits (not available for UART). */
  62. usartDatabits16 = USART_FRAME_DATABITS_SIXTEEN /**< 16 databits (not available for UART). */
  63. } USART_Databits_TypeDef;
  64. /** Enable selection. */
  65. typedef enum
  66. {
  67. /** Disable both receiver and transmitter. */
  68. usartDisable = 0x0,
  69. /** Enable receiver only, transmitter disabled. */
  70. usartEnableRx = USART_CMD_RXEN,
  71. /** Enable transmitter only, receiver disabled. */
  72. usartEnableTx = USART_CMD_TXEN,
  73. /** Enable both receiver and transmitter. */
  74. usartEnable = (USART_CMD_RXEN | USART_CMD_TXEN)
  75. } USART_Enable_TypeDef;
  76. /** Oversampling selection, used for asynchronous operation. */
  77. typedef enum
  78. {
  79. usartOVS16 = USART_CTRL_OVS_X16, /**< 16x oversampling (normal). */
  80. usartOVS8 = USART_CTRL_OVS_X8, /**< 8x oversampling. */
  81. usartOVS6 = USART_CTRL_OVS_X6, /**< 6x oversampling. */
  82. usartOVS4 = USART_CTRL_OVS_X4 /**< 4x oversampling. */
  83. } USART_OVS_TypeDef;
  84. /** Parity selection, mainly used for asynchronous operation. */
  85. typedef enum
  86. {
  87. usartNoParity = USART_FRAME_PARITY_NONE, /**< No parity. */
  88. usartEvenParity = USART_FRAME_PARITY_EVEN, /**< Even parity. */
  89. usartOddParity = USART_FRAME_PARITY_ODD /**< Odd parity. */
  90. } USART_Parity_TypeDef;
  91. /** Stopbits selection, used for asynchronous operation. */
  92. typedef enum
  93. {
  94. usartStopbits0p5 = USART_FRAME_STOPBITS_HALF, /**< 0.5 stopbits. */
  95. usartStopbits1 = USART_FRAME_STOPBITS_ONE, /**< 1 stopbits. */
  96. usartStopbits1p5 = USART_FRAME_STOPBITS_ONEANDAHALF, /**< 1.5 stopbits. */
  97. usartStopbits2 = USART_FRAME_STOPBITS_TWO /**< 2 stopbits. */
  98. } USART_Stopbits_TypeDef;
  99. /** Clock polarity/phase mode. */
  100. typedef enum
  101. {
  102. /** Clock idle low, sample on rising edge. */
  103. usartClockMode0 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLELEADING,
  104. /** Clock idle low, sample on falling edge. */
  105. usartClockMode1 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLETRAILING,
  106. /** Clock idle high, sample on falling edge. */
  107. usartClockMode2 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLELEADING,
  108. /** Clock idle high, sample on rising edge. */
  109. usartClockMode3 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLETRAILING
  110. } USART_ClockMode_TypeDef;
  111. /** Pulse width selection for IrDA mode. */
  112. typedef enum
  113. {
  114. /** IrDA pulse width is 1/16 for OVS=0 and 1/8 for OVS=1 */
  115. usartIrDAPwONE = USART_IRCTRL_IRPW_ONE,
  116. /** IrDA pulse width is 2/16 for OVS=0 and 2/8 for OVS=1 */
  117. usartIrDAPwTWO = USART_IRCTRL_IRPW_TWO,
  118. /** IrDA pulse width is 3/16 for OVS=0 and 3/8 for OVS=1 */
  119. usartIrDAPwTHREE = USART_IRCTRL_IRPW_THREE,
  120. /** IrDA pulse width is 4/16 for OVS=0 and 4/8 for OVS=1 */
  121. usartIrDAPwFOUR = USART_IRCTRL_IRPW_FOUR
  122. } USART_IrDAPw_Typedef;
  123. /** PRS channel selection for IrDA mode. */
  124. typedef enum
  125. {
  126. usartIrDAPrsCh0 = USART_IRCTRL_IRPRSSEL_PRSCH0, /**< PRS channel 0 */
  127. usartIrDAPrsCh1 = USART_IRCTRL_IRPRSSEL_PRSCH1, /**< PRS channel 1 */
  128. usartIrDAPrsCh2 = USART_IRCTRL_IRPRSSEL_PRSCH2, /**< PRS channel 2 */
  129. usartIrDAPrsCh3 = USART_IRCTRL_IRPRSSEL_PRSCH3, /**< PRS channel 3 */
  130. usartIrDAPrsCh4 = USART_IRCTRL_IRPRSSEL_PRSCH4, /**< PRS channel 4 */
  131. usartIrDAPrsCh5 = USART_IRCTRL_IRPRSSEL_PRSCH5, /**< PRS channel 5 */
  132. usartIrDAPrsCh6 = USART_IRCTRL_IRPRSSEL_PRSCH6, /**< PRS channel 6 */
  133. usartIrDAPrsCh7 = USART_IRCTRL_IRPRSSEL_PRSCH7 /**< PRS channel 7 */
  134. } USART_IrDAPrsSel_Typedef;
  135. /*******************************************************************************
  136. ******************************* STRUCTS ***********************************
  137. ******************************************************************************/
  138. /** Asynchronous mode init structure. */
  139. typedef struct
  140. {
  141. /** Specifies whether TX and/or RX shall be enabled when init completed. */
  142. USART_Enable_TypeDef enable;
  143. /**
  144. * USART/UART reference clock assumed when configuring baudrate setup. Set
  145. * it to 0 if currently configurated reference clock shall be used.
  146. */
  147. uint32_t refFreq;
  148. /** Desired baudrate. */
  149. uint32_t baudrate;
  150. /** Oversampling used. */
  151. USART_OVS_TypeDef oversampling;
  152. /** Number of databits in frame. Notice that UART modules only support 8 or
  153. * 9 databits. */
  154. USART_Databits_TypeDef databits;
  155. /** Parity mode to use. */
  156. USART_Parity_TypeDef parity;
  157. /** Number of stopbits to use. */
  158. USART_Stopbits_TypeDef stopbits;
  159. } USART_InitAsync_TypeDef;
  160. /** Default config for USART async init structure. */
  161. #define USART_INITASYNC_DEFAULT \
  162. { usartEnable, /* Enable RX/TX when init completed. */ \
  163. 0, /* Use current configured reference clock for configuring baudrate. */ \
  164. 115200, /* 115200 bits/s. */ \
  165. usartOVS16, /* 16x oversampling. */ \
  166. usartDatabits8, /* 8 databits. */ \
  167. usartNoParity, /* No parity. */ \
  168. usartStopbits1 /* 1 stopbit. */ \
  169. }
  170. /** Synchronous mode init structure. */
  171. typedef struct
  172. {
  173. /** Specifies whether TX and/or RX shall be enabled when init completed. */
  174. USART_Enable_TypeDef enable;
  175. /**
  176. * USART/UART reference clock assumed when configuring baudrate setup. Set
  177. * it to 0 if currently configurated reference clock shall be used.
  178. */
  179. uint32_t refFreq;
  180. /** Desired baudrate. */
  181. uint32_t baudrate;
  182. /** Number of databits in frame. */
  183. USART_Databits_TypeDef databits;
  184. /** Select if to operate in master or slave mode. */
  185. bool master;
  186. /** Select if to send most or least significant bit first. */
  187. bool msbf;
  188. /** Clock polarity/phase mode. */
  189. USART_ClockMode_TypeDef clockMode;
  190. } USART_InitSync_TypeDef;
  191. /** Default config for USART sync init structure. */
  192. #define USART_INITSYNC_DEFAULT \
  193. { usartEnable, /* Enable RX/TX when init completed. */ \
  194. 0, /* Use current configured reference clock for configuring baudrate. */ \
  195. 1000000, /* 1 Mbits/s. */ \
  196. usartDatabits8, /* 8 databits. */ \
  197. true, /* Master mode. */ \
  198. false, /* Send least significant bit first. */ \
  199. usartClockMode0 /* Clock idle low, sample on rising edge. */ \
  200. }
  201. /** IrDA mode init structure. Inherited from asynchronous mode init structure */
  202. typedef struct
  203. {
  204. /** General Async initialization structure. */
  205. USART_InitAsync_TypeDef async;
  206. /** Set to invert Rx signal before IrDA demodulator. */
  207. bool irRxInv;
  208. /** Set to enable filter on IrDA demodulator. */
  209. bool irFilt;
  210. /** Configure the pulse width generated by the IrDA modulator as a fraction
  211. * of the configured USART bit period. */
  212. USART_IrDAPw_Typedef irPw;
  213. /** Enable the PRS channel selected by irPrsSel as input to IrDA module
  214. * instead of TX. */
  215. bool irPrsEn;
  216. /** A PRS can be used as input to the pulse modulator instead of TX.
  217. * This value selects the channel to use. */
  218. USART_IrDAPrsSel_Typedef irPrsSel;
  219. } USART_InitIrDA_TypeDef;
  220. /** Default config for IrDA mode init structure. */
  221. #define USART_INITIRDA_DEFAULT \
  222. { \
  223. { usartEnable, /* Enable RX/TX when init completed. */ \
  224. 0, /* Use current configured reference clock for configuring baudrate. */ \
  225. 115200, /* 115200 bits/s. */ \
  226. usartOVS16, /* 16x oversampling. */ \
  227. usartDatabits8, /* 8 databits. */ \
  228. usartEvenParity, /* Even parity. */ \
  229. usartStopbits1 /* 1 stopbit. */ \
  230. }, \
  231. false, /* Rx invert disabled. */ \
  232. false, /* Filtering disabled. */ \
  233. usartIrDAPwTHREE, /* Pulse width is set to ONE. */ \
  234. false, /* Routing to PRS is disabled. */ \
  235. usartIrDAPrsCh0 /* PRS channel 0. */ \
  236. }
  237. /*******************************************************************************
  238. ***************************** PROTOTYPES **********************************
  239. ******************************************************************************/
  240. void USART_BaudrateAsyncSet(USART_TypeDef *usart,
  241. uint32_t refFreq,
  242. uint32_t baudrate,
  243. USART_OVS_TypeDef ovs);
  244. uint32_t USART_BaudrateCalc(uint32_t refFreq,
  245. uint32_t clkdiv,
  246. bool syncmode,
  247. USART_OVS_TypeDef ovs);
  248. uint32_t USART_BaudrateGet(USART_TypeDef *usart);
  249. void USART_BaudrateSyncSet(USART_TypeDef *usart,
  250. uint32_t refFreq,
  251. uint32_t baudrate);
  252. void USART_Enable(USART_TypeDef *usart, USART_Enable_TypeDef enable);
  253. void USART_InitAsync(USART_TypeDef *usart, USART_InitAsync_TypeDef *init);
  254. void USART_InitSync(USART_TypeDef *usart, USART_InitSync_TypeDef *init);
  255. void USART_InitIrDA(USART_InitIrDA_TypeDef *init);
  256. /***************************************************************************//**
  257. * @brief
  258. * Clear one or more pending USART interrupts.
  259. *
  260. * @param[in] usart
  261. * Pointer to USART/UART peripheral register block.
  262. *
  263. * @param[in] flags
  264. * Pending USART/UART interrupt source to clear. Use a logical OR combination
  265. * of valid interrupt flags for the USART module (USART_IF_nnn).
  266. ******************************************************************************/
  267. static __INLINE void USART_IntClear(USART_TypeDef *usart, uint32_t flags)
  268. {
  269. usart->IFC = flags;
  270. }
  271. /***************************************************************************//**
  272. * @brief
  273. * Disable one or more USART interrupts.
  274. *
  275. * @param[in] usart
  276. * Pointer to USART/UART peripheral register block.
  277. *
  278. * @param[in] flags
  279. * USART/UART interrupt sources to disable. Use a logical OR combination of
  280. * valid interrupt flags for the USART module (USART_IF_nnn).
  281. ******************************************************************************/
  282. static __INLINE void USART_IntDisable(USART_TypeDef *usart, uint32_t flags)
  283. {
  284. usart->IEN &= ~(flags);
  285. }
  286. /***************************************************************************//**
  287. * @brief
  288. * Enable one or more USART interrupts.
  289. *
  290. * @note
  291. * Depending on the use, a pending interrupt may already be set prior to
  292. * enabling the interrupt. Consider using USART_IntClear() prior to enabling
  293. * if such a pending interrupt should be ignored.
  294. *
  295. * @param[in] usart
  296. * Pointer to USART/UART peripheral register block.
  297. *
  298. * @param[in] flags
  299. * USART/UART interrupt sources to enable. Use a logical OR combination of
  300. * valid interrupt flags for the USART module (USART_IF_nnn).
  301. ******************************************************************************/
  302. static __INLINE void USART_IntEnable(USART_TypeDef *usart, uint32_t flags)
  303. {
  304. usart->IEN |= flags;
  305. }
  306. /***************************************************************************//**
  307. * @brief
  308. * Get pending USART interrupt flags.
  309. *
  310. * @note
  311. * The event bits are not cleared by the use of this function.
  312. *
  313. * @param[in] usart
  314. * Pointer to USART/UART peripheral register block.
  315. *
  316. * @return
  317. * USART/UART interrupt sources pending. A logical OR combination of valid
  318. * interrupt flags for the USART module (USART_IF_nnn).
  319. ******************************************************************************/
  320. static __INLINE uint32_t USART_IntGet(USART_TypeDef *usart)
  321. {
  322. return usart->IF;
  323. }
  324. /***************************************************************************//**
  325. * @brief
  326. * Set one or more pending USART interrupts from SW.
  327. *
  328. * @param[in] usart
  329. * Pointer to USART/UART peripheral register block.
  330. *
  331. * @param[in] flags
  332. * USART/UART interrupt sources to set to pending. Use a logical OR combination
  333. * of valid interrupt flags for the USART module (USART_IF_nnn).
  334. ******************************************************************************/
  335. static __INLINE void USART_IntSet(USART_TypeDef *usart, uint32_t flags)
  336. {
  337. usart->IFS = flags;
  338. }
  339. void USART_Reset(USART_TypeDef *usart);
  340. uint8_t USART_Rx(USART_TypeDef *usart);
  341. uint16_t USART_RxDouble(USART_TypeDef *usart);
  342. uint32_t USART_RxDoubleExt(USART_TypeDef *usart);
  343. uint16_t USART_RxExt(USART_TypeDef *usart);
  344. void USART_Tx(USART_TypeDef *usart, uint8_t data);
  345. void USART_TxDouble(USART_TypeDef *usart, uint16_t data);
  346. void USART_TxDoubleExt(USART_TypeDef *usart, uint32_t data);
  347. void USART_TxExt(USART_TypeDef *usart, uint16_t data);
  348. /** @} (end addtogroup USART) */
  349. /** @} (end addtogroup EFM32_Library) */
  350. #ifdef __cplusplus
  351. }
  352. #endif
  353. #endif /* __EFM32_USART_H */