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