efm32_leuart.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Low Energy Universal Asynchronous Receiver/Transmitter (LEUART)
  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_LEUART_H
  30. #define __EFM32_LEUART_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 LEUART
  42. * @{
  43. ******************************************************************************/
  44. /*******************************************************************************
  45. ******************************** ENUMS ************************************
  46. ******************************************************************************/
  47. /** Databit selection. */
  48. typedef enum
  49. {
  50. leuartDatabits8 = LEUART_CTRL_DATABITS_EIGHT, /**< 8 databits. */
  51. leuartDatabits9 = LEUART_CTRL_DATABITS_NINE /**< 9 databits. */
  52. } LEUART_Databits_TypeDef;
  53. /** Enable selection. */
  54. typedef enum
  55. {
  56. /** Disable both receiver and transmitter. */
  57. leuartDisable = 0x0,
  58. /** Enable receiver only, transmitter disabled. */
  59. leuartEnableRx = LEUART_CMD_RXEN,
  60. /** Enable transmitter only, receiver disabled. */
  61. leuartEnableTx = LEUART_CMD_TXEN,
  62. /** Enable both receiver and transmitter. */
  63. leuartEnable = (LEUART_CMD_RXEN | LEUART_CMD_TXEN)
  64. } LEUART_Enable_TypeDef;
  65. /** Parity selection. */
  66. typedef enum
  67. {
  68. leuartNoParity = LEUART_CTRL_PARITY_NONE, /**< No parity. */
  69. leuartEvenParity = LEUART_CTRL_PARITY_EVEN, /**< Even parity. */
  70. leuartOddParity = LEUART_CTRL_PARITY_ODD /**< Odd parity. */
  71. } LEUART_Parity_TypeDef;
  72. /** Stopbits selection. */
  73. typedef enum
  74. {
  75. leuartStopbits1 = LEUART_CTRL_STOPBITS_ONE, /**< 1 stopbits. */
  76. leuartStopbits2 = LEUART_CTRL_STOPBITS_TWO /**< 2 stopbits. */
  77. } LEUART_Stopbits_TypeDef;
  78. /*******************************************************************************
  79. ******************************* STRUCTS ***********************************
  80. ******************************************************************************/
  81. /** Init structure. */
  82. typedef struct
  83. {
  84. /** Specifies whether TX and/or RX shall be enabled when init completed. */
  85. LEUART_Enable_TypeDef enable;
  86. /**
  87. * LEUART reference clock assumed when configuring baudrate setup. Set
  88. * it to 0 if currently configurated reference clock shall be used.
  89. */
  90. uint32_t refFreq;
  91. /** Desired baudrate. */
  92. uint32_t baudrate;
  93. /** Number of databits in frame. */
  94. LEUART_Databits_TypeDef databits;
  95. /** Parity mode to use. */
  96. LEUART_Parity_TypeDef parity;
  97. /** Number of stopbits to use. */
  98. LEUART_Stopbits_TypeDef stopbits;
  99. } LEUART_Init_TypeDef;
  100. /** Default config for LEUART init structure. */
  101. #define LEUART_INIT_DEFAULT \
  102. { leuartEnable, /* Enable RX/TX when init completed. */ \
  103. 0, /* Use current configured reference clock for configuring baudrate. */ \
  104. 9600, /* 9600 bits/s. */ \
  105. leuartDatabits8, /* 8 databits. */ \
  106. leuartNoParity, /* No parity. */ \
  107. leuartStopbits1 /* 1 stopbit. */ \
  108. }
  109. /*******************************************************************************
  110. ***************************** PROTOTYPES **********************************
  111. ******************************************************************************/
  112. uint32_t LEUART_BaudrateCalc(uint32_t refFreq, uint32_t clkdiv);
  113. uint32_t LEUART_BaudrateGet(LEUART_TypeDef *leuart);
  114. void LEUART_BaudrateSet(LEUART_TypeDef *leuart,
  115. uint32_t refFreq,
  116. uint32_t baudrate);
  117. void LEUART_Enable(LEUART_TypeDef *leuart, LEUART_Enable_TypeDef enable);
  118. void LEUART_FreezeEnable(LEUART_TypeDef *leuart, bool enable);
  119. void LEUART_Init(LEUART_TypeDef *leuart, LEUART_Init_TypeDef *init);
  120. /***************************************************************************//**
  121. * @brief
  122. * Clear one or more pending LEUART interrupts.
  123. *
  124. * @param[in] leuart
  125. * Pointer to LEUART peripheral register block.
  126. *
  127. * @param[in] flags
  128. * Pending LEUART interrupt source to clear. Use a logical OR combination
  129. * of valid interrupt flags for the LEUART module (LEUART_IF_nnn).
  130. ******************************************************************************/
  131. static __INLINE void LEUART_IntClear(LEUART_TypeDef *leuart, uint32_t flags)
  132. {
  133. leuart->IFC = flags;
  134. }
  135. /***************************************************************************//**
  136. * @brief
  137. * Disable one or more LEUART interrupts.
  138. *
  139. * @param[in] leuart
  140. * Pointer to LEUART peripheral register block.
  141. *
  142. * @param[in] flags
  143. * LEUART interrupt sources to disable. Use a logical OR combination of
  144. * valid interrupt flags for the LEUART module (LEUART_IF_nnn).
  145. ******************************************************************************/
  146. static __INLINE void LEUART_IntDisable(LEUART_TypeDef *leuart, uint32_t flags)
  147. {
  148. leuart->IEN &= ~(flags);
  149. }
  150. /***************************************************************************//**
  151. * @brief
  152. * Enable one or more LEUART interrupts.
  153. *
  154. * @note
  155. * Depending on the use, a pending interrupt may already be set prior to
  156. * enabling the interrupt. Consider using LEUART_IntClear() prior to enabling
  157. * if such a pending interrupt should be ignored.
  158. *
  159. * @param[in] leuart
  160. * Pointer to LEUART peripheral register block.
  161. *
  162. * @param[in] flags
  163. * LEUART interrupt sources to enable. Use a logical OR combination of
  164. * valid interrupt flags for the LEUART module (LEUART_IF_nnn).
  165. ******************************************************************************/
  166. static __INLINE void LEUART_IntEnable(LEUART_TypeDef *leuart, uint32_t flags)
  167. {
  168. leuart->IEN |= flags;
  169. }
  170. /***************************************************************************//**
  171. * @brief
  172. * Get pending LEUART interrupt flags.
  173. *
  174. * @note
  175. * The event bits are not cleared by the use of this function.
  176. *
  177. * @param[in] leuart
  178. * Pointer to LEUART peripheral register block.
  179. *
  180. * @return
  181. * LEUART interrupt sources pending. A logical OR combination of valid
  182. * interrupt flags for the LEUART module (LEUART_IF_nnn).
  183. ******************************************************************************/
  184. static __INLINE uint32_t LEUART_IntGet(LEUART_TypeDef *leuart)
  185. {
  186. return(leuart->IF);
  187. }
  188. /***************************************************************************//**
  189. * @brief
  190. * Set one or more pending LEUART interrupts from SW.
  191. *
  192. * @param[in] leuart
  193. * Pointer to LEUART peripheral register block.
  194. *
  195. * @param[in] flags
  196. * LEUART interrupt sources to set to pending. Use a logical OR combination
  197. * of valid interrupt flags for the LEUART module (LEUART_IF_nnn).
  198. ******************************************************************************/
  199. static __INLINE void LEUART_IntSet(LEUART_TypeDef *leuart, uint32_t flags)
  200. {
  201. leuart->IFS = flags;
  202. }
  203. void LEUART_Reset(LEUART_TypeDef *leuart);
  204. uint8_t LEUART_Rx(LEUART_TypeDef *leuart);
  205. uint16_t LEUART_RxExt(LEUART_TypeDef *leuart);
  206. void LEUART_Tx(LEUART_TypeDef *leuart, uint8_t data);
  207. void LEUART_TxExt(LEUART_TypeDef *leuart, uint16_t data);
  208. /** @} (end addtogroup LEUART) */
  209. /** @} (end addtogroup EFM32_Library) */
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif /* __EFM32_LEUART_H */