1 /******************************************************************************
2 * @brief providing common UART API.
3 *
4 ******************************************************************************/
5 #include "uart.h"
6 
7 /******************************************************************************
8 * Local variables
9 ******************************************************************************/
10 UART_CallbackType UART_Callback = NULL;
11 /******************************************************************************
12 * Local function prototypes
13 ******************************************************************************/
14 
15 /******************************************************************************
16 * Local functions
17 *****************************************************************************/
18 
19 /******************************************************************************
20 * Global functions
21 ******************************************************************************/
22 
23 /******************************************************************************
24 * define UART APIs
25 *
26 *//*! @addtogroup uart_api_list
27 * @{
28 *******************************************************************************/
29 
30 /*****************************************************************************//*!
31 *
32 * @brief initialize the UART, interrupts disabled, and no hardware flow-control.
33 *
34 * @param[in] pUART       base of UART port
35 * @param[in] pConfig     pointer to UART configuration structure
36 *
37 * @return none
38 *
39 * @ Pass/ Fail criteria: none
40 *****************************************************************************/
UART_Init(UART_Type * pUART,UART_ConfigType * pConfig)41 void UART_Init(UART_Type *pUART, UART_ConfigType *pConfig)
42 {
43     uint16_t u16Sbr;
44     uint8_t u8Temp;
45     uint32_t u32SysClk = pConfig->u32SysClkHz;
46     uint32_t u32Baud = pConfig->u32Baudrate;
47 
48     /* Sanity check */
49     ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
50 
51 	/* Enable the clock to the selected UART */
52     if (pUART == UART0)
53 	{
54 		SIM->SCGC |= SIM_SCGC_UART0_MASK;
55 	}
56 #if defined(CPU_NV32)  | defined(CPU_NV326)
57 	else if (pUART == UART1)
58 	{
59         SIM->SCGC |= SIM_SCGC_UART1_MASK;
60 	}
61     else
62 	{
63         SIM->SCGC |= SIM_SCGC_UART2_MASK;
64 	}
65 #endif
66     /* Make sure that the transmitter and receiver are disabled while we
67      * change settings.
68      */
69     pUART->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
70 
71     /* Configure the UART for 8-bit mode, no parity */
72     pUART->C1 = 0;
73 
74     /* Calculate baud settings */
75     u16Sbr = (((u32SysClk)>>4) + (u32Baud>>1))/u32Baud;
76 
77     /* Save off the current value of the UARTx_BDH except for the SBR field */
78     u8Temp = pUART->BDH & ~(UART_BDH_SBR_MASK);
79 
80     pUART->BDH = u8Temp |  UART_BDH_SBR(u16Sbr >> 8);
81     pUART->BDL = (uint8_t)(u16Sbr & UART_BDL_SBR_MASK);
82 
83     /* Enable receiver and transmitter */
84     pUART->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
85 }
86 
87 /*****************************************************************************//*!
88 *
89 * @brief receive a character.
90 *
91 * @param[in] pUART       base of UART port
92 *
93 * @return unsigned char
94 *
95 *****************************************************************************/
UART_GetChar(UART_Type * pUART)96 uint8_t UART_GetChar(UART_Type *pUART)
97 {
98 
99     /* Sanity check */
100     ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
101 
102     /* Wait until character has been received */
103     while (!(pUART->S1 & UART_S1_RDRF_MASK));
104 
105     /* Return the 8-bit data from the receiver */
106     return pUART->D;
107 }
108 /*****************************************************************************//*!
109 *
110 * @brief send a character.
111 *
112 * @param[in] pUART       base of UART port
113 * @param[in] u8Char      char to send
114 *
115 * @return none
116 *
117 *****************************************************************************/
UART_PutChar(UART_Type * pUART,uint8_t u8Char)118 void UART_PutChar(UART_Type *pUART, uint8_t u8Char)
119 {
120     /* Wait until space is available in the FIFO */
121     while (!(pUART->S1 & UART_S1_TDRE_MASK));
122 
123     /* Send the character */
124     pUART->D = (uint8_t)u8Char;
125 }
126 
127 /*****************************************************************************//*!
128 *
129 * @brief set baudrate.
130 *
131 * @param[in] pUART       base of UART port
132 * @param[in] pConfig     baudrate config parameters
133 *
134 * @return none
135 *
136 * @ Pass/ Fail criteria:
137 *****************************************************************************/
UART_SetBaudrate(UART_Type * pUART,UART_ConfigBaudrateType * pConfig)138 void UART_SetBaudrate(UART_Type *pUART, UART_ConfigBaudrateType *pConfig)
139 {
140     uint8_t u8Temp;
141     uint16_t u16Sbr;
142     uint32_t u32SysClk    = pConfig->u32SysClkHz;
143     uint32_t u32baud       = pConfig->u32Baudrate;
144 
145     /* Sanity check */
146     ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
147 
148     /* Calculate baud settings */
149     u16Sbr = (((u32SysClk)>>4) + (u32baud>>1))/u32baud;
150 
151     /* Save off the current value of the UARTx_BDH except for the SBR field */
152     u8Temp = pUART->BDH & ~(UART_BDH_SBR_MASK);
153 
154     pUART->BDH = u8Temp |  UART_BDH_SBR(u16Sbr >> 8);
155     pUART->BDL = (uint8_t)(u16Sbr & UART_BDL_SBR_MASK);
156 
157     /* Enable receiver and transmitter */
158     pUART->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
159 
160 }
161 
162 /*****************************************************************************//*!
163 *
164 * @brief enable interrupt.
165 *
166 * @param[in] pUART          base of UART port
167 * @param[in] InterruptType  interrupt type
168 *
169 * @return none
170 *
171 * @ Pass/ Fail criteria:
172 *****************************************************************************/
UART_EnableInterrupt(UART_Type * pUART,UART_InterruptType InterruptType)173 void UART_EnableInterrupt(UART_Type *pUART, UART_InterruptType InterruptType)
174 {
175 
176     /* Sanity check */
177     ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
178 
179     if (InterruptType == UART_TxBuffEmptyInt)
180     {
181         pUART->C2 |= UART_C2_TIE_MASK;
182     }
183     else if (InterruptType == UART_TxCompleteInt)
184     {
185         pUART->C2 |= UART_C2_TCIE_MASK;
186     }
187     else if (InterruptType == UART_RxBuffFullInt)
188     {
189         pUART->C2 |= UART_C2_RIE_MASK;
190     }
191     else if (InterruptType == UART_IdleLineInt)
192     {
193         pUART->C2 |= UART_C2_ILIE_MASK;
194     }
195     else if (InterruptType == UART_RxOverrunInt)
196     {
197         pUART->C3 |= UART_C3_ORIE_MASK;
198     }
199     else if (InterruptType == UART_NoiseErrorInt)
200     {
201         pUART->C3 |= UART_C3_NEIE_MASK;
202     }
203     else if (InterruptType == UART_FramingErrorInt)
204     {
205         pUART->C3 |= UART_C3_FEIE_MASK;
206     }
207     else if (InterruptType == UART_ParityErrorInt)
208     {
209         pUART->C3 |= UART_C3_FEIE_MASK;
210     }
211     else
212     {
213         /* un-supported Interrupt type */
214     }
215 }
216 
217 /*****************************************************************************//*!
218 *
219 * @brief disable interrupt.
220 *
221 * @param[in] pUART base of UART port
222 * @param[in] InterruptType interrupt type
223 *
224 * @return none
225 *
226 * @ Pass/ Fail criteria:
227 *****************************************************************************/
UART_DisableInterrupt(UART_Type * pUART,UART_InterruptType InterruptType)228 void UART_DisableInterrupt(UART_Type *pUART, UART_InterruptType InterruptType)
229 {
230     /* Sanity check */
231     ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
232 
233 
234     if (InterruptType == UART_TxBuffEmptyInt)
235     {
236         pUART->C2 &= (~UART_C2_TIE_MASK);
237     }
238     else if (InterruptType == UART_TxCompleteInt)
239     {
240         pUART->C2 &= (~UART_C2_TCIE_MASK);
241     }
242     else if (InterruptType == UART_RxBuffFullInt)
243     {
244         pUART->C2 &= (~UART_C2_RIE_MASK);
245     }
246     else if (InterruptType == UART_IdleLineInt)
247     {
248         pUART->C2 &= (~UART_C2_ILIE_MASK);
249     }
250     else if (InterruptType == UART_RxOverrunInt)
251     {
252         pUART->C3 &= (~UART_C3_ORIE_MASK);
253     }
254     else if (InterruptType == UART_NoiseErrorInt)
255     {
256         pUART->C3 &= (~UART_C3_NEIE_MASK);
257     }
258     else if (InterruptType == UART_FramingErrorInt)
259     {
260         pUART->C3 &= (~UART_C3_FEIE_MASK);
261     }
262     else if (InterruptType == UART_ParityErrorInt)
263     {
264         pUART->C3 &= (~UART_C3_FEIE_MASK);
265     }
266     else
267     {
268         /* un-supported interrupt type */
269     }
270 }
271 
272 
273 /*****************************************************************************//*!
274 *
275 * @brief get flags from 2 UART status registers.
276 *
277 * @param[in] pUART  base of UART port
278 *
279 * @return       16-bit flags
280 *
281 * @ Pass/ Fail criteria:
282 *****************************************************************************/
UART_GetFlags(UART_Type * pUART)283 uint16_t UART_GetFlags(UART_Type *pUART)
284 {
285     uint16_t u16StatusFlags = 0;
286 
287     u16StatusFlags = pUART->S2;
288     u16StatusFlags = (u16StatusFlags<<8)| pUART->S1;
289 
290     return u16StatusFlags;
291 }
292 /*****************************************************************************//*!
293 *
294 * @brief check whether the specified flag is set.
295 *
296 * @param[in] pUART      base of UART port
297 * @param[in] FlagType   flag type
298 *
299 * @return
300 *               1, flag is set
301 *               0, flag is clear
302 *
303 * @ Pass/ Fail criteria: none
304 *****************************************************************************/
UART_CheckFlag(UART_Type * pUART,UART_FlagType FlagType)305 uint8_t UART_CheckFlag(UART_Type *pUART, UART_FlagType FlagType)
306 {
307     uint16_t u16StatusFlags = 0;
308 
309     u16StatusFlags = UART_GetFlags(pUART);
310 
311     return (u16StatusFlags & (1<<FlagType));
312 }
313 
314 /*****************************************************************************//*!
315 *
316 * @brief send a series of charecters using polling mode.
317 *
318 * @param[in] pUART      base of UART port
319 * @param[in] pSendBuff  pointer of charecters to send
320 * @param[in] u32Length  number of charecters
321 *
322 * @return       none
323 *
324 * @ Pass/ Fail criteria:
325 *****************************************************************************/
UART_SendWait(UART_Type * pUART,uint8_t * pSendBuff,uint32_t u32Length)326 void UART_SendWait(UART_Type *pUART, uint8_t *pSendBuff, uint32_t u32Length)
327 {
328     uint8_t u8TxChar;
329     uint32_t  i;
330 
331     for (i = 0; i < u32Length; i++)
332     {
333         u8TxChar = pSendBuff[i];
334         while (!UART_IsTxBuffEmpty(pUART))
335         {
336             #if defined(ENABLE_WDOG)
337                 WDOG_Feed();
338             #endif
339         }
340         UART_WriteDataReg(pUART, u8TxChar);
341     }
342 }
343 
344 /*****************************************************************************//*!
345 *
346 * @brief receive a series of charecters using polling mode.
347 *
348 * @param[in] pUART          base of UART port
349 * @param[in] pReceiveBuff   pointer of charecters to receive
350 * @param[in] u32Length      number of charecters
351 *
352 * @return       none
353 *
354 * @ Pass/ Fail criteria:
355 *****************************************************************************/
UART_ReceiveWait(UART_Type * pUART,uint8_t * pReceiveBuff,uint32_t u32Length)356 void UART_ReceiveWait(UART_Type *pUART, uint8_t *pReceiveBuff, uint32_t u32Length)
357 {
358     uint8_t u8RxChar;
359     uint32_t i;
360 
361     for (i = 0; i < u32Length; i++)
362     {
363         while (!UART_IsRxBuffFull(pUART))
364         {
365             #if defined(ENABLE_WDOG)
366                 WDOG_Feed();
367             #endif
368         }
369         u8RxChar = UART_ReadDataReg(pUART);
370         pReceiveBuff[i] = u8RxChar;
371     }
372 }
373 
374 /*****************************************************************************//*!
375 *
376 * @brief wait tx complete.
377 *
378 * @param[in] pUART      base of UART port
379 *
380 * @return       none
381 *
382 * @ Pass/ Fail criteria: none*****************************************************************************/
UART_WaitTxComplete(UART_Type * pUART)383 void UART_WaitTxComplete(UART_Type *pUART)
384 {
385     while (!UART_IsTxComplete(pUART));
386 }
387 
388 /*****************************************************************************//*!
389 *
390 * @brief set up UART callback routines to be called by interrupt service routine.
391 *
392 * @param[in]  pUART         pointer to an UART register base
393 * @param[in]  pfnCallback   callback routine
394 *
395 * @return none
396 *
397 * @ Pass/ Fail criteria: none
398 *****************************************************************************/
UART_SetCallback(UART_CallbackType pfnCallback)399 void UART_SetCallback(UART_CallbackType pfnCallback)
400 {
401     //uint8_t    u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
402     UART_Callback = pfnCallback;
403 }
404 
405 
406 /*! @} End of uart_api_list */
407 
408 
409 /*****************************************************************************//*!
410 *
411 * @brief uart0 interrupt service routine.
412 *
413 * @param        none
414 *
415 * @return       none
416 *
417 * @ Pass/ Fail criteria:
418 *****************************************************************************/
UART0_Isr(void)419 void UART0_Isr(void)
420 {
421     UART_Callback(UART0);
422 }
423 
424 
425 #if defined(CPU_NV32) | defined(CPU_NV326)
426 /*****************************************************************************//*!
427 *
428 * @brief uart1 interrupt service routine.
429 *
430 * @param        none
431 *
432 * @return       none
433 *
434 * @ Pass/ Fail criteria:
435 *****************************************************************************/
UART1_Isr(void)436 void UART1_Isr(void)
437 {
438     UART_Callback(UART1);
439 }
440 /*****************************************************************************//*!
441 *
442 * @brief uart2 interrupt service routine.
443 *
444 * @param        none
445 *
446 * @return       none
447 *
448 * @ Pass/ Fail criteria:
449 *****************************************************************************/
UART2_Isr(void)450 void UART2_Isr(void)
451 {
452     UART_Callback(UART2);
453 }
454 
455 
456 #endif
457 
458 
459 
460