efm32_usart.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Universal synchronous/asynchronous receiver/transmitter (USART/UART)
  4. * peripheral API for EFM32.
  5. * @author Energy Micro AS
  6. * @version 2.3.2
  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. * @brief Universal Synchronous/Asynchronous Receiver/Transmitter (USART) peripheral API for EFM32
  43. * @{
  44. ******************************************************************************/
  45. /*******************************************************************************
  46. ******************************** ENUMS ************************************
  47. ******************************************************************************/
  48. /** Databit selection. */
  49. typedef enum
  50. {
  51. usartDatabits4 = USART_FRAME_DATABITS_FOUR, /**< 4 databits (not available for UART). */
  52. usartDatabits5 = USART_FRAME_DATABITS_FIVE, /**< 5 databits (not available for UART). */
  53. usartDatabits6 = USART_FRAME_DATABITS_SIX, /**< 6 databits (not available for UART). */
  54. usartDatabits7 = USART_FRAME_DATABITS_SEVEN, /**< 7 databits (not available for UART). */
  55. usartDatabits8 = USART_FRAME_DATABITS_EIGHT, /**< 8 databits. */
  56. usartDatabits9 = USART_FRAME_DATABITS_NINE, /**< 9 databits. */
  57. usartDatabits10 = USART_FRAME_DATABITS_TEN, /**< 10 databits (not available for UART). */
  58. usartDatabits11 = USART_FRAME_DATABITS_ELEVEN, /**< 11 databits (not available for UART). */
  59. usartDatabits12 = USART_FRAME_DATABITS_TWELVE, /**< 12 databits (not available for UART). */
  60. usartDatabits13 = USART_FRAME_DATABITS_THIRTEEN, /**< 13 databits (not available for UART). */
  61. usartDatabits14 = USART_FRAME_DATABITS_FOURTEEN, /**< 14 databits (not available for UART). */
  62. usartDatabits15 = USART_FRAME_DATABITS_FIFTEEN, /**< 15 databits (not available for UART). */
  63. usartDatabits16 = USART_FRAME_DATABITS_SIXTEEN /**< 16 databits (not available for UART). */
  64. } USART_Databits_TypeDef;
  65. /** Enable selection. */
  66. typedef enum
  67. {
  68. /** Disable both receiver and transmitter. */
  69. usartDisable = 0x0,
  70. /** Enable receiver only, transmitter disabled. */
  71. usartEnableRx = USART_CMD_RXEN,
  72. /** Enable transmitter only, receiver disabled. */
  73. usartEnableTx = USART_CMD_TXEN,
  74. /** Enable both receiver and transmitter. */
  75. usartEnable = (USART_CMD_RXEN | USART_CMD_TXEN)
  76. } USART_Enable_TypeDef;
  77. /** Oversampling selection, used for asynchronous operation. */
  78. typedef enum
  79. {
  80. usartOVS16 = USART_CTRL_OVS_X16, /**< 16x oversampling (normal). */
  81. usartOVS8 = USART_CTRL_OVS_X8, /**< 8x oversampling. */
  82. usartOVS6 = USART_CTRL_OVS_X6, /**< 6x oversampling. */
  83. usartOVS4 = USART_CTRL_OVS_X4 /**< 4x oversampling. */
  84. } USART_OVS_TypeDef;
  85. /** Parity selection, mainly used for asynchronous operation. */
  86. typedef enum
  87. {
  88. usartNoParity = USART_FRAME_PARITY_NONE, /**< No parity. */
  89. usartEvenParity = USART_FRAME_PARITY_EVEN, /**< Even parity. */
  90. usartOddParity = USART_FRAME_PARITY_ODD /**< Odd parity. */
  91. } USART_Parity_TypeDef;
  92. /** Stopbits selection, used for asynchronous operation. */
  93. typedef enum
  94. {
  95. usartStopbits0p5 = USART_FRAME_STOPBITS_HALF, /**< 0.5 stopbits. */
  96. usartStopbits1 = USART_FRAME_STOPBITS_ONE, /**< 1 stopbits. */
  97. usartStopbits1p5 = USART_FRAME_STOPBITS_ONEANDAHALF, /**< 1.5 stopbits. */
  98. usartStopbits2 = USART_FRAME_STOPBITS_TWO /**< 2 stopbits. */
  99. } USART_Stopbits_TypeDef;
  100. /** Clock polarity/phase mode. */
  101. typedef enum
  102. {
  103. /** Clock idle low, sample on rising edge. */
  104. usartClockMode0 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLELEADING,
  105. /** Clock idle low, sample on falling edge. */
  106. usartClockMode1 = USART_CTRL_CLKPOL_IDLELOW | USART_CTRL_CLKPHA_SAMPLETRAILING,
  107. /** Clock idle high, sample on falling edge. */
  108. usartClockMode2 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLELEADING,
  109. /** Clock idle high, sample on rising edge. */
  110. usartClockMode3 = USART_CTRL_CLKPOL_IDLEHIGH | USART_CTRL_CLKPHA_SAMPLETRAILING
  111. } USART_ClockMode_TypeDef;
  112. /** Pulse width selection for IrDA mode. */
  113. typedef enum
  114. {
  115. /** IrDA pulse width is 1/16 for OVS=0 and 1/8 for OVS=1 */
  116. usartIrDAPwONE = USART_IRCTRL_IRPW_ONE,
  117. /** IrDA pulse width is 2/16 for OVS=0 and 2/8 for OVS=1 */
  118. usartIrDAPwTWO = USART_IRCTRL_IRPW_TWO,
  119. /** IrDA pulse width is 3/16 for OVS=0 and 3/8 for OVS=1 */
  120. usartIrDAPwTHREE = USART_IRCTRL_IRPW_THREE,
  121. /** IrDA pulse width is 4/16 for OVS=0 and 4/8 for OVS=1 */
  122. usartIrDAPwFOUR = USART_IRCTRL_IRPW_FOUR
  123. } USART_IrDAPw_Typedef;
  124. /** PRS channel selection for IrDA mode. */
  125. typedef enum
  126. {
  127. usartIrDAPrsCh0 = USART_IRCTRL_IRPRSSEL_PRSCH0, /**< PRS channel 0 */
  128. usartIrDAPrsCh1 = USART_IRCTRL_IRPRSSEL_PRSCH1, /**< PRS channel 1 */
  129. usartIrDAPrsCh2 = USART_IRCTRL_IRPRSSEL_PRSCH2, /**< PRS channel 2 */
  130. usartIrDAPrsCh3 = USART_IRCTRL_IRPRSSEL_PRSCH3, /**< PRS channel 3 */
  131. usartIrDAPrsCh4 = USART_IRCTRL_IRPRSSEL_PRSCH4, /**< PRS channel 4 */
  132. usartIrDAPrsCh5 = USART_IRCTRL_IRPRSSEL_PRSCH5, /**< PRS channel 5 */
  133. usartIrDAPrsCh6 = USART_IRCTRL_IRPRSSEL_PRSCH6, /**< PRS channel 6 */
  134. usartIrDAPrsCh7 = USART_IRCTRL_IRPRSSEL_PRSCH7 /**< PRS channel 7 */
  135. } USART_IrDAPrsSel_Typedef;
  136. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  137. /** I2S format selection. */
  138. typedef enum
  139. {
  140. usartI2sFormatW32D32 = USART_I2SCTRL_FORMAT_W32D32, /**< 32-bit word, 32-bit data */
  141. usartI2sFormatW32D24M = USART_I2SCTRL_FORMAT_W32D24M, /**< 32-bit word, 32-bit data with 8 lsb masked */
  142. usartI2sFormatW32D24 = USART_I2SCTRL_FORMAT_W32D24, /**< 32-bit word, 24-bit data */
  143. usartI2sFormatW32D16 = USART_I2SCTRL_FORMAT_W32D16, /**< 32-bit word, 16-bit data */
  144. usartI2sFormatW32D8 = USART_I2SCTRL_FORMAT_W32D8, /**< 32-bit word, 8-bit data */
  145. usartI2sFormatW16D16 = USART_I2SCTRL_FORMAT_W16D16, /**< 16-bit word, 16-bit data */
  146. usartI2sFormatW16D8 = USART_I2SCTRL_FORMAT_W16D8, /**< 16-bit word, 8-bit data */
  147. usartI2sFormatW8D8 = USART_I2SCTRL_FORMAT_W8D8 /**< 8-bit word, 8-bit data */
  148. } USART_I2sFormat_TypeDef;
  149. /** I2S frame data justify. */
  150. typedef enum
  151. {
  152. usartI2sJustifyLeft = USART_I2SCTRL_JUSTIFY_LEFT, /**< Data is left-justified within the frame */
  153. usartI2sJustifyRight = USART_I2SCTRL_JUSTIFY_RIGHT /**< Data is right-justified within the frame */
  154. } USART_I2sJustify_TypeDef;
  155. /** USART Rx input PRS selection. */
  156. typedef enum
  157. {
  158. usartPrsRxCh0 = USART_INPUT_RXPRSSEL_PRSCH0, /**< PRSCH0 selected as USART_INPUT */
  159. usartPrsRxCh1 = USART_INPUT_RXPRSSEL_PRSCH1, /**< PRSCH1 selected as USART_INPUT */
  160. usartPrsRxCh2 = USART_INPUT_RXPRSSEL_PRSCH2, /**< PRSCH2 selected as USART_INPUT */
  161. usartPrsRxCh3 = USART_INPUT_RXPRSSEL_PRSCH3, /**< PRSCH3 selected as USART_INPUT */
  162. usartPrsRxCh4 = USART_INPUT_RXPRSSEL_PRSCH4, /**< PRSCH4 selected as USART_INPUT */
  163. usartPrsRxCh5 = USART_INPUT_RXPRSSEL_PRSCH5, /**< PRSCH5 selected as USART_INPUT */
  164. usartPrsRxCh6 = USART_INPUT_RXPRSSEL_PRSCH6, /**< PRSCH6 selected as USART_INPUT */
  165. #if defined(_EFM32_TINY_FAMILY)
  166. usartPrsRxCh7 = USART_INPUT_RXPRSSEL_PRSCH7 /**< PRSCH7 selected as USART_INPUT */
  167. #elif defined(_EFM32_GIANT_FAMILY)
  168. usartPrsRxCh7 = USART_INPUT_RXPRSSEL_PRSCH7, /**< PRSCH7 selected as USART_INPUT */
  169. usartPrsRxCh8 = USART_INPUT_RXPRSSEL_PRSCH8, /**< PRSCH8 selected as USART_INPUT */
  170. usartPrsRxCh9 = USART_INPUT_RXPRSSEL_PRSCH9, /**< PRSCH9 selected as USART_INPUT */
  171. usartPrsRxCh10 = USART_INPUT_RXPRSSEL_PRSCH10, /**< PRSCH10 selected as USART_INPUT */
  172. usartPrsRxCh11 = USART_INPUT_RXPRSSEL_PRSCH11 /**< PRSCH11 selected as USART_INPUT */
  173. #else
  174. #error Unknown EFM32 family.
  175. #endif
  176. } USART_PrsRxCh_TypeDef;
  177. #endif
  178. #if defined (_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  179. /** USART PRS Transmit Trigger Channels */
  180. typedef enum
  181. {
  182. usartPrsTriggerCh0 = USART_TRIGCTRL_TSEL_PRSCH0, /**< PRSCH0 selected as USART Trigger */
  183. usartPrsTriggerCh1 = USART_TRIGCTRL_TSEL_PRSCH1, /**< PRSCH0 selected as USART Trigger */
  184. usartPrsTriggerCh2 = USART_TRIGCTRL_TSEL_PRSCH2, /**< PRSCH0 selected as USART Trigger */
  185. usartPrsTriggerCh3 = USART_TRIGCTRL_TSEL_PRSCH3, /**< PRSCH0 selected as USART Trigger */
  186. usartPrsTriggerCh4 = USART_TRIGCTRL_TSEL_PRSCH4, /**< PRSCH0 selected as USART Trigger */
  187. usartPrsTriggerCh5 = USART_TRIGCTRL_TSEL_PRSCH5, /**< PRSCH0 selected as USART Trigger */
  188. usartPrsTriggerCh6 = USART_TRIGCTRL_TSEL_PRSCH6, /**< PRSCH0 selected as USART Trigger */
  189. usartPrsTriggerCh7 = USART_TRIGCTRL_TSEL_PRSCH7, /**< PRSCH0 selected as USART Trigger */
  190. } USART_PrsTriggerCh_TypeDef;
  191. #endif
  192. /*******************************************************************************
  193. ******************************* STRUCTS ***********************************
  194. ******************************************************************************/
  195. /** Asynchronous mode init structure. */
  196. typedef struct
  197. {
  198. /** Specifies whether TX and/or RX shall be enabled when init completed. */
  199. USART_Enable_TypeDef enable;
  200. /**
  201. * USART/UART reference clock assumed when configuring baudrate setup. Set
  202. * it to 0 if currently configurated reference clock shall be used.
  203. */
  204. uint32_t refFreq;
  205. /** Desired baudrate. */
  206. uint32_t baudrate;
  207. /** Oversampling used. */
  208. USART_OVS_TypeDef oversampling;
  209. /** Number of databits in frame. Notice that UART modules only support 8 or
  210. * 9 databits. */
  211. USART_Databits_TypeDef databits;
  212. /** Parity mode to use. */
  213. USART_Parity_TypeDef parity;
  214. /** Number of stopbits to use. */
  215. USART_Stopbits_TypeDef stopbits;
  216. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  217. /** Majority Vote Disable for 16x, 8x and 6x oversampling modes. */
  218. bool mvdis;
  219. /** Enable USART Rx via PRS. */
  220. bool prsRxEnable;
  221. /** Select PRS channel for USART Rx. (Only valid if prsRxEnable is true). */
  222. USART_PrsRxCh_TypeDef prsRxCh;
  223. #endif
  224. } USART_InitAsync_TypeDef;
  225. #if defined(_EFM32_TINY_FAMILY) || defined(_EFM32_GIANT_FAMILY)
  226. /** USART PRS trigger enable */
  227. typedef struct
  228. {
  229. #if defined(_EFM32_GIANT_FAMILY)
  230. /** Enable AUTOTX */
  231. bool autoTxTriggerEnable;
  232. #endif
  233. /** Trigger receive via PRS channel */
  234. bool rxTriggerEnable;
  235. /** Trigger transmit via PRS channel */
  236. bool txTriggerEnable;
  237. /** PRS channel to be used to trigger auto transmission */
  238. USART_PrsTriggerCh_TypeDef prsTriggerChannel;
  239. } USART_PrsTriggerInit_TypeDef;
  240. #endif
  241. /** Default config for USART async init structure. */
  242. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  243. #define USART_INITASYNC_DEFAULT \
  244. { usartEnable, /* Enable RX/TX when init completed. */ \
  245. 0, /* Use current configured reference clock for configuring baudrate. */ \
  246. 115200, /* 115200 bits/s. */ \
  247. usartOVS16, /* 16x oversampling. */ \
  248. usartDatabits8, /* 8 databits. */ \
  249. usartNoParity, /* No parity. */ \
  250. usartStopbits1, /* 1 stopbit. */ \
  251. false, /* Do not disable majority vote. */ \
  252. false, /* Not USART PRS input mode. */ \
  253. usartPrsRxCh0 /* PRS channel 0. */ \
  254. }
  255. #else
  256. #define USART_INITASYNC_DEFAULT \
  257. { usartEnable, /* Enable RX/TX when init completed. */ \
  258. 0, /* Use current configured reference clock for configuring baudrate. */ \
  259. 115200, /* 115200 bits/s. */ \
  260. usartOVS16, /* 16x oversampling. */ \
  261. usartDatabits8, /* 8 databits. */ \
  262. usartNoParity, /* No parity. */ \
  263. usartStopbits1 /* 1 stopbit. */ \
  264. }
  265. #endif
  266. /** Synchronous mode init structure. */
  267. typedef struct
  268. {
  269. /** Specifies whether TX and/or RX shall be enabled when init completed. */
  270. USART_Enable_TypeDef enable;
  271. /**
  272. * USART/UART reference clock assumed when configuring baudrate setup. Set
  273. * it to 0 if currently configurated reference clock shall be used.
  274. */
  275. uint32_t refFreq;
  276. /** Desired baudrate. */
  277. uint32_t baudrate;
  278. /** Number of databits in frame. */
  279. USART_Databits_TypeDef databits;
  280. /** Select if to operate in master or slave mode. */
  281. bool master;
  282. /** Select if to send most or least significant bit first. */
  283. bool msbf;
  284. /** Clock polarity/phase mode. */
  285. USART_ClockMode_TypeDef clockMode;
  286. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  287. /** Enable USART Rx via PRS. */
  288. bool prsRxEnable;
  289. /** Select PRS channel for USART Rx. (Only valid if prsRxEnable is true). */
  290. USART_PrsRxCh_TypeDef prsRxCh;
  291. /** Enable AUTOTX mode. Transmits as long as RX is not full.
  292. * If TX is empty, underflows are generated. */
  293. bool autoTx;
  294. #endif
  295. } USART_InitSync_TypeDef;
  296. /** Default config for USART sync init structure. */
  297. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  298. #define USART_INITSYNC_DEFAULT \
  299. { usartEnable, /* Enable RX/TX when init completed. */ \
  300. 0, /* Use current configured reference clock for configuring baudrate. */ \
  301. 1000000, /* 1 Mbits/s. */ \
  302. usartDatabits8, /* 8 databits. */ \
  303. true, /* Master mode. */ \
  304. false, /* Send least significant bit first. */ \
  305. usartClockMode0, /* Clock idle low, sample on rising edge. */ \
  306. false, /* Not USART PRS input mode. */ \
  307. usartPrsRxCh0, /* PRS channel 0. */ \
  308. false /* No AUTOTX mode. */ \
  309. }
  310. #else
  311. #define USART_INITSYNC_DEFAULT \
  312. { usartEnable, /* Enable RX/TX when init completed. */ \
  313. 0, /* Use current configured reference clock for configuring baudrate. */ \
  314. 1000000, /* 1 Mbits/s. */ \
  315. usartDatabits8, /* 8 databits. */ \
  316. true, /* Master mode. */ \
  317. false, /* Send least significant bit first. */ \
  318. usartClockMode0 /* Clock idle low, sample on rising edge. */ \
  319. }
  320. #endif
  321. /** IrDA mode init structure. Inherited from asynchronous mode init structure */
  322. typedef struct
  323. {
  324. /** General Async initialization structure. */
  325. USART_InitAsync_TypeDef async;
  326. /** Set to invert Rx signal before IrDA demodulator. */
  327. bool irRxInv;
  328. /** Set to enable filter on IrDA demodulator. */
  329. bool irFilt;
  330. /** Configure the pulse width generated by the IrDA modulator as a fraction
  331. * of the configured USART bit period. */
  332. USART_IrDAPw_Typedef irPw;
  333. /** Enable the PRS channel selected by irPrsSel as input to IrDA module
  334. * instead of TX. */
  335. bool irPrsEn;
  336. /** A PRS can be used as input to the pulse modulator instead of TX.
  337. * This value selects the channel to use. */
  338. USART_IrDAPrsSel_Typedef irPrsSel;
  339. } USART_InitIrDA_TypeDef;
  340. /** Default config for IrDA mode init structure. */
  341. #define USART_INITIRDA_DEFAULT \
  342. { \
  343. { usartEnable, /* Enable RX/TX when init completed. */ \
  344. 0, /* Use current configured reference clock for configuring baudrate. */ \
  345. 115200, /* 115200 bits/s. */ \
  346. usartOVS16, /* 16x oversampling. */ \
  347. usartDatabits8, /* 8 databits. */ \
  348. usartEvenParity, /* Even parity. */ \
  349. usartStopbits1 /* 1 stopbit. */ \
  350. }, \
  351. false, /* Rx invert disabled. */ \
  352. false, /* Filtering disabled. */ \
  353. usartIrDAPwTHREE, /* Pulse width is set to ONE. */ \
  354. false, /* Routing to PRS is disabled. */ \
  355. usartIrDAPrsCh0 /* PRS channel 0. */ \
  356. }
  357. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  358. /** I2S mode init structure. Inherited from synchronous mode init structure */
  359. typedef struct
  360. {
  361. /** General Sync initialization structure. */
  362. USART_InitSync_TypeDef sync;
  363. /** I2S mode. */
  364. USART_I2sFormat_TypeDef format;
  365. /** Delay on I2S data. Set to add a one-cycle delay between a transition
  366. * on the word-clock and the start of the I2S word.
  367. * Should be set for standard I2S format. */
  368. bool delay;
  369. /** Separate DMA Request For Left/Right Data. */
  370. bool dmaSplit;
  371. /** Justification of I2S data within the frame */
  372. USART_I2sJustify_TypeDef justify;
  373. /** Stero or Mono, set to true for mono. */
  374. bool mono;
  375. } USART_InitI2s_TypeDef;
  376. /** Default config for I2S mode init structure. */
  377. #define USART_INITI2S_DEFAULT \
  378. { \
  379. { usartEnableTx, /* Enable TX when init completed. */ \
  380. 0, /* Use current configured reference clock for configuring baudrate. */ \
  381. 1000000, /* Baudrate 1M bits/s. */ \
  382. usartDatabits16, /* 16 databits. */ \
  383. true, /* Operate as I2S master. */ \
  384. true, /* Most significant bit first. */ \
  385. usartClockMode0, /* Clock idle low, sample on rising edge. */ \
  386. false, /* Don't enable USARTRx via PRS. */ \
  387. usartPrsRxCh0, /* PRS channel selection (dummy). */ \
  388. false /* Disable AUTOTX mode. */ \
  389. }, \
  390. usartI2sFormatW16D16, /* 16-bit word, 16-bit data */ \
  391. true, /* Delay on I2S data. */ \
  392. false, /* No DMA split. */ \
  393. usartI2sJustifyLeft, /* Data is left-justified within the frame */ \
  394. false /* Stereo mode. */ \
  395. }
  396. #endif
  397. /*******************************************************************************
  398. ***************************** PROTOTYPES **********************************
  399. ******************************************************************************/
  400. void USART_BaudrateAsyncSet(USART_TypeDef *usart,
  401. uint32_t refFreq,
  402. uint32_t baudrate,
  403. USART_OVS_TypeDef ovs);
  404. uint32_t USART_BaudrateCalc(uint32_t refFreq,
  405. uint32_t clkdiv,
  406. bool syncmode,
  407. USART_OVS_TypeDef ovs);
  408. uint32_t USART_BaudrateGet(USART_TypeDef *usart);
  409. void USART_BaudrateSyncSet(USART_TypeDef *usart,
  410. uint32_t refFreq,
  411. uint32_t baudrate);
  412. void USART_Enable(USART_TypeDef *usart, USART_Enable_TypeDef enable);
  413. void USART_InitAsync(USART_TypeDef *usart, const USART_InitAsync_TypeDef *init);
  414. void USART_InitSync(USART_TypeDef *usart, const USART_InitSync_TypeDef *init);
  415. void USART_InitIrDA(const USART_InitIrDA_TypeDef *init);
  416. #if defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_TINY_FAMILY)
  417. void USART_InitI2s(USART_TypeDef *usart, USART_InitI2s_TypeDef *init);
  418. void USART_InitPrsTrigger(USART_TypeDef *usart, const USART_PrsTriggerInit_TypeDef *init);
  419. #endif
  420. /***************************************************************************//**
  421. * @brief
  422. * Clear one or more pending USART interrupts.
  423. *
  424. * @param[in] usart
  425. * Pointer to USART/UART peripheral register block.
  426. *
  427. * @param[in] flags
  428. * Pending USART/UART interrupt source(s) to clear. Use one or more valid
  429. * interrupt flags for the USART module (USART_IF_nnn) OR'ed together.
  430. ******************************************************************************/
  431. static __INLINE void USART_IntClear(USART_TypeDef *usart, uint32_t flags)
  432. {
  433. usart->IFC = flags;
  434. }
  435. /***************************************************************************//**
  436. * @brief
  437. * Disable one or more USART interrupts.
  438. *
  439. * @param[in] usart
  440. * Pointer to USART/UART peripheral register block.
  441. *
  442. * @param[in] flags
  443. * USART/UART interrupt source(s) to disable. Use one or more valid
  444. * interrupt flags for the USART module (USART_IF_nnn) OR'ed together.
  445. ******************************************************************************/
  446. static __INLINE void USART_IntDisable(USART_TypeDef *usart, uint32_t flags)
  447. {
  448. usart->IEN &= ~(flags);
  449. }
  450. /***************************************************************************//**
  451. * @brief
  452. * Enable one or more USART interrupts.
  453. *
  454. * @note
  455. * Depending on the use, a pending interrupt may already be set prior to
  456. * enabling the interrupt. Consider using USART_IntClear() prior to enabling
  457. * if such a pending interrupt should be ignored.
  458. *
  459. * @param[in] usart
  460. * Pointer to USART/UART peripheral register block.
  461. *
  462. * @param[in] flags
  463. * USART/UART interrupt source(s) to enable. Use one or more valid
  464. * interrupt flags for the USART module (USART_IF_nnn) OR'ed together.
  465. ******************************************************************************/
  466. static __INLINE void USART_IntEnable(USART_TypeDef *usart, uint32_t flags)
  467. {
  468. usart->IEN |= flags;
  469. }
  470. /***************************************************************************//**
  471. * @brief
  472. * Get pending USART interrupt flags.
  473. *
  474. * @note
  475. * The event bits are not cleared by the use of this function.
  476. *
  477. * @param[in] usart
  478. * Pointer to USART/UART peripheral register block.
  479. *
  480. * @return
  481. * USART/UART interrupt source(s) pending. Returns one or more valid
  482. * interrupt flags for the USART module (USART_IF_nnn) OR'ed together.
  483. ******************************************************************************/
  484. static __INLINE uint32_t USART_IntGet(USART_TypeDef *usart)
  485. {
  486. return usart->IF;
  487. }
  488. /***************************************************************************//**
  489. * @brief
  490. * Get enabled and pending USART interrupt flags.
  491. * Useful for handling more interrupt sources in the same interrupt handler.
  492. *
  493. * @param[in] usart
  494. * Pointer to USART/UART peripheral register block.
  495. *
  496. * @note
  497. * Interrupt flags are not cleared by the use of this function.
  498. *
  499. * @return
  500. * Pending and enabled USART interrupt sources.
  501. * The return value is the bitwise AND combination of
  502. * - the OR combination of enabled interrupt sources in USARTx_IEN_nnn
  503. * register (USARTx_IEN_nnn) and
  504. * - the OR combination of valid interrupt flags of the USART module
  505. * (USARTx_IF_nnn).
  506. ******************************************************************************/
  507. static __INLINE uint32_t USART_IntGetEnabled(USART_TypeDef *usart)
  508. {
  509. uint32_t tmp;
  510. /* Store USARTx->IEN in temporary variable in order to define explicit order
  511. * of volatile accesses. */
  512. tmp = usart->IEN;
  513. /* Bitwise AND of pending and enabled interrupts */
  514. return usart->IF & tmp;
  515. }
  516. /***************************************************************************//**
  517. * @brief
  518. * Set one or more pending USART interrupts from SW.
  519. *
  520. * @param[in] usart
  521. * Pointer to USART/UART peripheral register block.
  522. *
  523. * @param[in] flags
  524. * USART/UART interrupt source(s) to set to pending. Use one or more valid
  525. * interrupt flags for the USART module (USART_IF_nnn) OR'ed together.
  526. ******************************************************************************/
  527. static __INLINE void USART_IntSet(USART_TypeDef *usart, uint32_t flags)
  528. {
  529. usart->IFS = flags;
  530. }
  531. void USART_Reset(USART_TypeDef *usart);
  532. uint8_t USART_Rx(USART_TypeDef *usart);
  533. uint16_t USART_RxDouble(USART_TypeDef *usart);
  534. uint32_t USART_RxDoubleExt(USART_TypeDef *usart);
  535. uint16_t USART_RxExt(USART_TypeDef *usart);
  536. void USART_Tx(USART_TypeDef *usart, uint8_t data);
  537. void USART_TxDouble(USART_TypeDef *usart, uint16_t data);
  538. void USART_TxDoubleExt(USART_TypeDef *usart, uint32_t data);
  539. void USART_TxExt(USART_TypeDef *usart, uint16_t data);
  540. /** @} (end addtogroup USART) */
  541. /** @} (end addtogroup EFM32_Library) */
  542. #ifdef __cplusplus
  543. }
  544. #endif
  545. #endif /* __EFM32_USART_H */