em_leuart.h 9.2 KB

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