em_usart.h 28 KB

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