]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/uart/fsl_uart_hal.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[qmk_firmware.git] / tmk_core / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / hal / uart / fsl_uart_hal.c
1 /*
2  * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  *   of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  *   list of conditions and the following disclaimer in the documentation and/or
13  *   other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  *   contributors may be used to endorse or promote products derived from this
17  *   software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "fsl_uart_hal.h"
31
32 /*******************************************************************************
33  * Code
34  ******************************************************************************/
35 /*******************************************************************************
36  * UART Common Configurations
37  ******************************************************************************/
38 /*FUNCTION**********************************************************************
39  *
40  * Function Name : UART_HAL_Init
41  * Description   : This function initializes the module to a known state.
42  *
43  *END**************************************************************************/
44 void UART_HAL_Init(uint32_t baseAddr)
45 {
46     HW_UART_BDH_WR(baseAddr, 0U);
47     HW_UART_BDL_WR(baseAddr, 4U);
48     HW_UART_C1_WR(baseAddr, 0U);
49     HW_UART_C2_WR(baseAddr, 0U);
50     HW_UART_S2_WR(baseAddr, 0U);
51     HW_UART_C3_WR(baseAddr, 0U);
52     HW_UART_D_WR(baseAddr, 0U);
53 #if FSL_FEATURE_UART_HAS_ADDRESS_MATCHING
54     HW_UART_MA1_WR(baseAddr, 0U);
55     HW_UART_MA2_WR(baseAddr, 0U);
56 #endif
57     HW_UART_C4_WR(baseAddr, 0U);
58 #if FSL_FEATURE_UART_HAS_DMA_ENABLE
59     HW_UART_C5_WR(baseAddr, 0U);
60 #endif
61 #if FSL_FEATURE_UART_HAS_MODEM_SUPPORT
62     HW_UART_MODEM_WR(baseAddr, 0U);
63 #endif
64 #if FSL_FEATURE_UART_HAS_IR_SUPPORT
65     HW_UART_IR_WR(baseAddr, 0U);
66 #endif
67 #if FSL_FEATURE_UART_HAS_FIFO
68     HW_UART_PFIFO_WR(baseAddr, 0U);
69     HW_UART_CFIFO_WR(baseAddr, 0U);
70     HW_UART_SFIFO_WR(baseAddr, 0xC0U);
71     HW_UART_TWFIFO_WR(baseAddr, 0U);
72     HW_UART_RWFIFO_WR(baseAddr, 1U);
73 #endif
74 }
75
76 /*FUNCTION**********************************************************************
77  *
78  * Function Name : UART_HAL_SetBaudRate
79  * Description   : Configure the UART baud rate.
80  * This function programs the UART baud rate to the desired value passed in by the
81  * user. The user must also pass in the module source clock so that the function can
82  * calculate the baud rate divisors to their appropriate values.
83  *
84  *END**************************************************************************/
85 uart_status_t UART_HAL_SetBaudRate(uint32_t baseAddr, uint32_t sourceClockInHz, uint32_t baudRate)
86 {
87     /* BaudRate = (SourceClkInHz)/[16 * (SBR +  BRFA)]
88      * First, calculate SBR (integer part) then calculate the BRFA (fine adjust fractional field). */
89     uint16_t brfa, sbr;
90
91     /* calculate the baud rate modulo divisor, sbr*/
92     sbr = sourceClockInHz / (baudRate * 16);
93
94     /* check to see if sbr is out of range of register bits */
95     if ( (sbr > 0x1FFF) || (sbr < 1) )
96     {
97         /* unsupported baud rate for given source clock input*/
98         return kStatus_UART_BaudRateCalculationError;
99     }
100
101     /* write the sbr value to the BDH and BDL registers*/
102     BW_UART_BDH_SBR(baseAddr, (uint8_t)(sbr >> 8));
103     BW_UART_BDL_SBR(baseAddr, (uint8_t)sbr);
104
105 #if FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT
106     /* determine if a fractional divider is needed to fine tune closer to the desired baud
107      * each value of brfa is in 1/32 increments, hence the multiply-by-32. */
108     brfa = (32*sourceClockInHz/(baudRate*16)) - 32*sbr;
109
110     /* write the brfa value to the register*/
111     BW_UART_C4_BRFA(baseAddr, brfa);
112 #endif
113
114     return kStatus_UART_Success;
115 }
116
117 /*FUNCTION**********************************************************************
118  *
119  * Function Name : UART_HAL_SetBaudRateDivisor
120  * Description   : Set the UART baud rate modulo divisor value.
121  * This function allows the user to program the baud rate divisor directly in
122  * situations where the divisor value is known. In this case, the user may not want to
123  * call the UART_HAL_SetBaudRate() function as the divisor is already known to them.
124  *
125  *END**************************************************************************/
126 void UART_HAL_SetBaudRateDivisor(uint32_t baseAddr, uint16_t baudRateDivisor)
127 {
128     /* check to see if baudRateDivisor is out of range of register bits */
129     assert( (baudRateDivisor < 0x1FFF) && (baudRateDivisor > 1) );
130
131     /* program the sbr (baudRateDivisor) value to the BDH and BDL registers*/
132     BW_UART_BDH_SBR(baseAddr, (uint8_t)(baudRateDivisor >> 8));
133     BW_UART_BDL_SBR(baseAddr, (uint8_t)baudRateDivisor);
134 }
135
136 /*FUNCTION**********************************************************************
137  *
138  * Function Name : UART_HAL_SetTxRxInversionCmd
139  * Description   : Configure the transmit and receive inversion control in UART
140  * controller. This function allows the user to invert the transmit and receive
141  * signals, independently.  This function should only be called when the UART is
142  * between transmit and receive packets.
143  *
144  *END**************************************************************************/
145 void UART_HAL_SetTxRxInversionCmd(uint32_t baseAddr, bool rxInvertEnable, bool txInvertEnable)
146 {
147     /* 0 - receive data not inverted, 1 - receive data inverted */
148     BW_UART_S2_RXINV(baseAddr, (uint8_t)rxInvertEnable);
149     /* 0 - transmit data not inverted, 1 - transmit data inverted*/
150     BW_UART_C3_TXINV(baseAddr, (uint8_t)txInvertEnable);
151 }
152
153 /*******************************************************************************
154  * UART Transfer Functions
155  ******************************************************************************/
156 /*FUNCTION**********************************************************************
157  *
158  * Function Name : UART_HAL_Putchar
159  * Description   : This function allows the user to send an 8-bit character from the UART
160  *                 data register.
161  *
162  *END**************************************************************************/
163 void UART_HAL_Putchar(uint32_t baseAddr, uint8_t data)
164 {
165     /* put 8-bit data into the uart data register*/
166     /* in addition to sending a char, this function also clears the transmit status flags
167      * for this uart baseAddr, there is a two step process to clear the
168      * transmit status flags:
169      * 1. Read the status register with the status bit set
170      * 2. write to the data register */
171     HW_UART_S1_RD(baseAddr);
172     HW_UART_D_WR(baseAddr, data);
173 }
174
175 /*FUNCTION**********************************************************************
176  *
177  * Function Name : UART_HAL_Putchar9
178  * Description   : This function allows the user to send a 9-bit character from the UART
179  *                 data register.
180  *
181  *END**************************************************************************/
182 void UART_HAL_Putchar9(uint32_t baseAddr, uint16_t data)
183 {
184     uint8_t ninthDataBit;
185
186     ninthDataBit = (data >> 8U) & 0x1U;  /* isolate the ninth data bit*/
187
188     /* put 9-bit data to transmit*/
189     /* first, write to the ninth data bit (bit position T8, where T[0:7]=8-bits, T8=9th bit)*/
190     BW_UART_C3_T8(baseAddr, ninthDataBit);
191
192     /* in addition to sending a char, this function also clears the transmit status flags
193      * for this uart baseAddr, there is a two step process to clear the
194      * transmit status flags:
195      * 1. Read the status register with the status bit set
196      * 2. write to the data register */
197     HW_UART_S1_RD(baseAddr);
198     /* write to the data register last since this will trigger transmit complete status flags
199      * also typecast to uint8_t to match register type */
200     HW_UART_D_WR(baseAddr, (uint8_t)data);
201 }
202
203 /*FUNCTION**********************************************************************
204  *
205  * Function Name : UART_HAL_Getchar
206  * Description   : This function gets a received 8-bit character from the UART data register.
207  *
208  *END**************************************************************************/
209 void UART_HAL_Getchar(uint32_t baseAddr, uint8_t *readData)
210 {
211     /* get 8-bit data from the uart data register*/
212     /* in addition to getting a char, this function also clears the receive status flag RDRF
213      * along with IDLE, OR, NF, FE, and PF (these can also be cleared in separate functions)
214      * for this uart baseAddr, there is a two step process to clear the receive
215      * status flag:
216      * 1. Read the status register with the status bit set
217      * 2. read from the data register */
218     HW_UART_S1_RD(baseAddr);
219     /* second, perform a read from the data register */
220     *readData = HW_UART_D_RD(baseAddr);  /* read 8-bit data from data register*/
221 }
222
223 /*FUNCTION**********************************************************************
224  *
225  * Function Name : UART_HAL_Getchar9
226  * Description   : This function gets a received 9-bit character from the UART data register.
227  *
228  *END**************************************************************************/
229 void  UART_HAL_Getchar9(uint32_t baseAddr, uint16_t *readData)
230 {
231     uint16_t temp;
232
233     /* get 9-bit data from the uart data register*/
234     /* read ninth data bit and left shift to bit position R8 before reading
235      * the 8 other data bits R[7:0]
236      * *readData = (HW_UART_C3(baseAddr).B.R8) << 8; */
237     temp = (HW_UART_C3(baseAddr).B.R8);
238     *readData = temp << 8;
239
240     /* in addition to getting a char, this function also clears the receive status flag RDRF
241      * along with IDLE, OR, NF, FE, and PF (these can also be cleared in separate functions)
242      * for this uart baseAddr, there is a two step process to clear the receive
243      * status flag:
244      * 1. Read the status register with the status bit set
245      * 2. read from the data register */
246     HW_UART_S1_RD(baseAddr);
247     /* do last: get 8-bit data from the uart data register,
248      * will clear certain receive status bits once completed
249      * need to OR these 8-bits with the ninth bit value above. */
250     *readData |= HW_UART_D_RD(baseAddr);  /* read 8-bit data from data register*/
251 }
252
253 /*******************************************************************************
254  * UART Interrupts and DMA
255  ******************************************************************************/
256 /*FUNCTION**********************************************************************
257  *
258  * Function Name : UART_HAL_ConfigureInterrupts
259  * Description   : Configure the UART module interrupts to enable/disable various
260  * interrupt sources.
261  *
262  *END**************************************************************************/
263 void UART_HAL_SetIntMode(uint32_t baseAddr, uart_interrupt_t interrupt, bool enable)
264 {
265     uint8_t reg = (uint32_t)interrupt >> UART_SHIFT;
266     uint32_t temp = 1U << (uint8_t)interrupt;
267
268     switch ( reg )
269     {
270         case 0 :
271             enable ? HW_UART_BDH_SET(baseAddr, temp) : HW_UART_BDH_CLR(baseAddr, temp);
272             break;
273         case 1 :
274             enable ? HW_UART_C2_SET(baseAddr, temp) : HW_UART_C2_CLR(baseAddr, temp);
275             break;
276         case 2 :
277             enable ? HW_UART_C3_SET(baseAddr, temp) : HW_UART_C3_CLR(baseAddr, temp);
278             break;
279 #if FSL_FEATURE_UART_HAS_FIFO
280         case 3 :
281             enable ? HW_UART_CFIFO_SET(baseAddr, temp) : HW_UART_CFIFO_CLR(baseAddr, temp);
282             break;
283 #endif
284         default :
285             break;
286     }
287 }
288
289 /*FUNCTION**********************************************************************
290  *
291  * Function Name : UART_HAL_GetIntMode
292  * Description   : Return whether the UART module interrupts is enabled/disabled.
293  *
294  *END**************************************************************************/
295 bool UART_HAL_GetIntMode(uint32_t baseAddr, uart_interrupt_t interrupt)
296 {
297     uint8_t reg = (uint32_t)interrupt >> UART_SHIFT;
298     uint8_t temp = 0;
299
300     switch ( reg )
301     {
302         case 0 :
303             temp = HW_UART_BDH_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
304             break;
305         case 1 :
306             temp = HW_UART_C2_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
307             break;
308         case 2 :
309             temp = HW_UART_C3_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
310             break;
311 #if FSL_FEATURE_UART_HAS_FIFO
312         case 3 :
313             temp = HW_UART_CFIFO_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
314             break;
315 #endif
316         default :
317             break;
318     }
319     return (bool)temp;
320 }
321 #if FSL_FEATURE_UART_HAS_DMA_SELECT 
322 /*FUNCTION**********************************************************************
323  *
324  * Function Name : UART_HAL_ConfigureDma
325  * Description   : Configure the UART DMA requests for the Transmitter and Receiver.
326  * This function allows the user to configure the transmit data register empty flag to
327  * generate an interrupt request (default) or a DMA request.  Similarly, this function
328  * allows the user to conigure the receive data register full flag to generate an interrupt
329  * request (default) or a DMA request.
330  *
331  *END**************************************************************************/
332 void UART_HAL_ConfigureDma(uint32_t baseAddr, bool txDmaConfig, bool  rxDmaConfig)
333 {
334
335     /* TDMAS configures the transmit data register empty flag, TDRE, to generate interrupt
336      * or DMA requests if TIE is set.
337      * NOTE: If UART_C2[TIE] is cleared, TDRE DMA and TDRE interrupt request signals are
338      * not asserted when the TDRE flag is set, regardless of the state of TDMAS.
339      * If UART_C2[TIE] and TDMAS are both set, then UART_C2[TCIE] must be cleared, and UART_D
340      * must not be written outside of servicing of a DMA request.
341      * 0 If TIE is set and the TDRE flag is set, the TDRE interrupt request signal is asserted
342      * to request interrupt service.
343      * 1 If TIE is set and the TDRE flag is set, the TDRE DMA request signal is asserted
344      * to request a DMA transfer.
345      */
346     if (txDmaConfig == 1)
347     {
348         /* enable uart to generate transmit DMA request*/
349         BW_UART_C5_TDMAS(baseAddr, 1U); /* set TDMAS */
350         BW_UART_C2_TCIE(baseAddr, 0U); /* clear TCIE */
351         BW_UART_C2_TIE(baseAddr, 1U); /* set TIE */
352     }
353     else
354     {
355         /* disable uart transmit DMA request*/
356         BW_UART_C2_TIE(baseAddr, 0U); /* clear TIE to disable */
357         BW_UART_C5_TDMAS(baseAddr, 0U); /* clear TDMAS to disable */
358     }
359
360     /* RDMAS configures the receiver data register full flag, RDRF, to generate interrupt or
361      * DMA requests if RIEis set.
362      * NOTE: If RIE is cleared, the RDRF DMA and RDRF interrupt request signals are not
363      * asserted when the RDRF flag is set, regardless of the state of RDMAS.
364      * 0 If RIE is set and the RDRF flag is set, the RDRF interrupt request signal is
365      * asserted to request interrupt service.
366      * 1 If RIE is set and the RDRF flag is set, the RDRF DMA request signal is asserted
367      * to request a DMA transfer.
368      */
369     if (rxDmaConfig == 1)
370     {
371         /* enable uart to generate receive DMA request*/
372         BW_UART_C5_RDMAS(baseAddr, 1U); /* set RDMAS */
373         BW_UART_C2_RIE(baseAddr, 1U); /* set RIE */
374     }
375     else
376     {
377         /* disable uart receive DMA request*/
378         BW_UART_C2_RIE(baseAddr, 0U); /* clear RIE to disable */
379         BW_UART_C5_RDMAS(baseAddr, 0U); /* clear RDMAS to disable */
380     }
381 }
382
383 /*FUNCTION**********************************************************************
384  *
385  * Function Name : UART_HAL_IsTxdmaEnabled
386  * Description   : Get the UART Transmit DMA request configuration setting.
387  * This function returns to the user the configuration setting of the Transmit DMA request.
388  *
389  *END**************************************************************************/
390 bool UART_HAL_IsTxdmaEnabled(uint32_t baseAddr)
391 {
392     /* create variable for this to work around MISRA rule 12.4 since this is a volatile value*/
393     uint32_t tcieBitStatus;
394     tcieBitStatus = HW_UART_C2(baseAddr).B.TCIE;
395
396     /* TDMAS configures the transmit data register empty flag, TDRE, to generate interrupt or
397      * DMA requests if TIE is set.
398      * NOTE: If UART_C2[TIE] is cleared, TDRE DMA and TDRE interrupt request signals are
399      * not asserted when the TDRE flag is set, regardless of the state of TDMAS.
400      * If UART_C2[TIE] and TDMAS are both set, then UART_C2[TCIE] must be cleared, and UART_D
401      * must not be written outside of servicing of a DMA request.
402      * 0 If TIE is set and the TDRE flag is set, the TDRE interrupt request signal is asserted
403      * to request interrupt service.
404      * 1 If TIE is set and the TDRE flag is set, the TDRE DMA request signal is asserted to
405      * request a DMA transfer.
406      */
407     if (BR_UART_C5_TDMAS(baseAddr) == 1)
408     {
409         /* in order to enable transmit DMA request, TIE must be set and TCIE must be cleared*/
410         if ((BR_UART_C2_TIE(baseAddr) == 1) && (tcieBitStatus == 0))
411         {
412             /* UART module is configured to generate TxDMA request*/
413             return 1;
414         }
415         else
416         {
417             /* UART module is NOT configured to generate TxDMA request*/
418             return 0;
419         }
420     }
421     else
422     {
423         /* UART module is NOT configured to generate TxDMA request*/
424         return 0;
425     }
426 }
427
428 /*FUNCTION**********************************************************************
429  *
430  * Function Name : UART_HAL_IsRxdmaEnabled
431  * Description   : Get the UART Receive DMA request configuration setting.
432  * This function returns to the user the configuration setting of the Receive DMA request.
433  *
434  *END**************************************************************************/
435 bool UART_HAL_IsRxdmaEnabled(uint32_t baseAddr)
436 {
437     /* RDMAS configures the receiver data register full flag, RDRF, to generate interrupt or
438      * DMA requests if RIE is set.
439      * NOTE: If RIE is cleared, the RDRF DMA and RDRF interrupt request signals are not
440      * asserted when the RDRF flag is set, regardless of the state of RDMAS.
441      * 0 If RIE is set and the RDRF flag is set, the RDRF interrupt request signal is asserted
442      * to requestinterrupt service.
443      * 1 If RIE is set and the RDRF flag is set, the RDRF DMA request signal is asserted to
444      * request a DMA transfer.
445      */
446     if (BR_UART_C5_RDMAS(baseAddr) == 1)
447     {
448         /* enable uart to generate receive DMA request*/
449         if (BR_UART_C2_RIE(baseAddr) == 1)
450         {
451             /* UART module is configured to generate RxDMA request*/
452             return 1;
453         }
454         else
455         {
456             /* UART module is NOT configured to generate RxDMA request*/
457             return 0;
458         }
459     }
460     else
461     {
462         /* UART module is NOT configured to generate RxDMA request*/
463         return 0;
464     }
465 }
466 #endif
467 /*******************************************************************************
468  * UART UART Status Flags
469  ******************************************************************************/
470 /*FUNCTION**********************************************************************
471  *
472  * Function Name : UART_HAL_GetStatusFlag
473  * Description   : Get UART status flag states.
474  *
475  *END**************************************************************************/
476 bool UART_HAL_GetStatusFlag(uint32_t baseAddr, uart_status_flag_t statusFlag)
477 {
478     uint8_t reg = (uint32_t)statusFlag >> UART_SHIFT;
479     uint8_t temp = 0;
480
481     switch ( reg )
482     {
483         case 0 :
484             temp = HW_UART_S1_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
485             break;
486         case 1 :
487             temp = HW_UART_S2_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
488             break;
489 #if FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
490         case 2 :
491             temp = HW_UART_ED_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
492             break;
493 #endif
494 #if FSL_FEATURE_UART_HAS_FIFO
495         case 3 :
496             temp = HW_UART_SFIFO_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
497             break;
498 #endif
499         default :
500             break;
501     }
502     return (bool)temp;
503 }
504
505 /*FUNCTION**********************************************************************
506  *
507  * Function Name : UART_HAL_ClearStatusFlag
508  * Description   : Clear an individual and specific UART status flag.
509  * This function allows the user to clear an individual and specific UART status flag. Refer to
510  * structure definition uart_status_flag_t for list of status bits.
511  *
512  *END**************************************************************************/
513 uart_status_t UART_HAL_ClearStatusFlag(uint32_t baseAddr, uart_status_flag_t statusFlag)
514 {
515     uart_status_t returnCode;       /* return code variable */
516     returnCode = kStatus_UART_Success;  /* default return code, unless changed by error condition*/
517
518     /* clear the desired, individual status flag as passed in through statusFlag  */
519     switch(statusFlag)
520     {
521         case kUartTxDataRegEmpty:
522             /* This flag is cleared automatically by other uart operations and
523              * cannot be manually cleared, return error code
524              */
525             returnCode = kStatus_UART_ClearStatusFlagError;
526             break;
527
528         case kUartTxComplete:
529             /* This flag is cleared automatically by other uart operations and
530              * cannot be manually cleared, return error code
531              */
532             returnCode = kStatus_UART_ClearStatusFlagError;
533             break;
534
535         case kUartRxDataRegFull:
536             /* This flag is cleared automatically by other uart operations and
537              * cannot be manually cleared, return error code
538              */
539             returnCode = kStatus_UART_ClearStatusFlagError;
540             break;
541
542         case kUartIdleLineDetect:
543             /* to clear the status is a two-step process:
544              * first, read S1 register with the status flag set
545              */
546             HW_UART_S1_RD(baseAddr);
547             /* second, read the data register*/
548             HW_UART_D_RD(baseAddr);
549             break;
550
551         case kUartRxOverrun:
552             /* to clear the status is a two-step process:
553              * first, read S1 register with the status flag set
554              */
555             HW_UART_S1_RD(baseAddr);
556             /* second, read the data register*/
557             HW_UART_D_RD(baseAddr);
558             break;
559
560         case kUartNoiseDetect:
561             /* to clear the status is a two-step process:
562              * first, read S1 register with the status flag set
563              */
564             HW_UART_S1_RD(baseAddr);
565             /* second, read the data register*/
566             HW_UART_D_RD(baseAddr);
567             break;
568
569         case kUartFrameErr:
570             /* to clear the status is a two-step process:
571              * first, read S1 register with the status flag set
572              */
573             HW_UART_S1_RD(baseAddr);
574             /* second, read the data register*/
575             HW_UART_D_RD(baseAddr);
576             break;
577
578         case kUartParityErr:
579             /* to clear the status is a two-step process:
580              * first, read S1 register with the status flag set
581              */
582             HW_UART_S1_RD(baseAddr);
583             /* second, read the data register*/
584             HW_UART_D_RD(baseAddr);
585             break;
586
587         case kUartLineBreakDetect:
588             /* write one to clear status flag */
589             HW_UART_S2_SET(baseAddr, BM_UART_S2_LBKDIF);
590             break;
591
592         case kUartRxActiveEdgeDetect:
593             /* write one to clear status flag */
594             HW_UART_S2_SET(baseAddr, BM_UART_S2_RXEDGIF);
595             break;
596
597         case kUartRxActive:
598             /* This flag is cleared automatically by other uart operations and
599              * cannot be manually cleared, return error code
600              */
601             returnCode = kStatus_UART_ClearStatusFlagError;
602             break;
603
604 #if FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
605         case kUartNoiseInCurrentWord:
606             /* This flag is not clearable, it simply reflects the status in the
607              * current data word and changes with each new data word
608              */
609             returnCode = kStatus_UART_ClearStatusFlagError;
610             break;
611
612         case kUartParityErrInCurrentWord:
613             /* This flag is not clearable, it simply reflects the status in the
614              * current data word and changes with each new data word
615              */
616             returnCode = kStatus_UART_ClearStatusFlagError;
617             break;
618 #endif
619 #if FSL_FEATURE_UART_HAS_FIFO
620         case kUartTxBuffEmpty:
621             /* This flag is not clearable, it simply reflects the current
622              * status of the buffer/FIFO
623              */
624             returnCode = kStatus_UART_ClearStatusFlagError;
625             break;
626
627         case kUartRxBuffEmpty:
628             /* This flag is not clearable, it simply reflects the current
629              * status of the buffer/FIFO
630              */
631             returnCode = kStatus_UART_ClearStatusFlagError;
632             break;
633
634         case kUartTxBuffOverflow:
635             /* write one to clear status flag */
636             HW_UART_SFIFO_SET(baseAddr, BM_UART_SFIFO_TXOF);
637             break;
638
639         case kUartRxBuffUnderflow:
640             /* write one to clear status flag */
641             HW_UART_SFIFO_SET(baseAddr, BM_UART_SFIFO_RXUF);
642             break;
643 #endif
644         default:  /* catch inputs that are not recognized*/
645             returnCode = kStatus_UART_ClearStatusFlagError;
646             break;
647     }
648
649     return (returnCode);
650 }
651
652 /*FUNCTION**********************************************************************
653  *
654  * Function Name : UART_HAL_ClearAllNonAutoclearStatusFlags
655  * Description   : Clear ALL of the UART status flags.
656  * This function tries to clear all of the UART status flags.  In some cases, some of the status
657  * flags may not get cleared because of the condition that set the flag may still exist.
658  *
659  *END**************************************************************************/
660 void UART_HAL_ClearAllNonAutoclearStatusFlags(uint32_t baseAddr)
661 {
662     /* clear the status flags that can be manually cleared
663      * note, some flags are automatically cleared and cannot be cleared automatically
664      */
665     UART_HAL_ClearStatusFlag(baseAddr, kUartIdleLineDetect);
666     UART_HAL_ClearStatusFlag(baseAddr, kUartRxOverrun);
667     UART_HAL_ClearStatusFlag(baseAddr, kUartNoiseDetect);
668     UART_HAL_ClearStatusFlag(baseAddr, kUartFrameErr);
669     UART_HAL_ClearStatusFlag(baseAddr, kUartParityErr);
670     UART_HAL_ClearStatusFlag(baseAddr, kUartLineBreakDetect);
671     UART_HAL_ClearStatusFlag(baseAddr, kUartRxActiveEdgeDetect);
672 #if FSL_FEATURE_UART_HAS_FIFO
673     UART_HAL_ClearStatusFlag(baseAddr, kUartTxBuffOverflow);
674     UART_HAL_ClearStatusFlag(baseAddr, kUartRxBuffUnderflow);
675 #endif
676 }
677
678 /*******************************************************************************
679  * UART FIFO Configurations
680  ******************************************************************************/
681 #if FSL_FEATURE_UART_HAS_FIFO
682 /*FUNCTION**********************************************************************
683  *
684  * Function Name : UART_HAL_SetTxFifo
685  * Description   : Enable or disable the UART transmit FIFO.
686  * This function allows the user to enable or disable the UART transmit FIFO.
687  * It is required that the transmitter/receiver should be disabled before calling this
688  * function and when the FIFO is empty. Additionally, TXFLUSH and RXFLUSH commands
689  * should be issued after calling this function.
690  *
691  *END**************************************************************************/
692 uart_status_t UART_HAL_SetTxFifoCmd(uint32_t baseAddr, bool enable)
693 {
694     /* before enabling the tx fifo, UARTx_C2[TE] (transmitter) and
695      * UARTx_C2[RE] (receiver) must be disabled
696      * if not, return an error code */
697     uint8_t txEnable = BR_UART_C2_TE(baseAddr);
698     uint8_t rxEnable = BR_UART_C2_RE(baseAddr);
699
700     if (txEnable || rxEnable)
701     {
702         return kStatus_UART_TxOrRxNotDisabled;
703     }
704     else
705     {
706         BW_UART_PFIFO_TXFE(baseAddr, enable);
707         return kStatus_UART_Success;
708     }
709 }
710
711 /*FUNCTION**********************************************************************
712  *
713  * Function Name : UART_HAL_SetRxFifoCmd
714  * Description   : Enable or disable the UART receive FIFO.
715  * This function allows the user to enable or disable the UART receive FIFO.
716  * It is required that the transmitter/receiver should be disabled before calling
717  * this function and when the FIFO is empty. Additionally, TXFLUSH and RXFLUSH
718  * commands should be issued after calling this function.
719  *
720  *END**************************************************************************/
721 uart_status_t UART_HAL_SetRxFifoCmd(uint32_t baseAddr, bool enable)
722 {
723     /* before enabling the rx fifo, UARTx_C2[TE] (transmitter) and
724      * UARTx_C2[RE] (receiver) must be disabled
725      * if not, return an error code */
726     uint8_t txEnable = BR_UART_C2_TE(baseAddr);
727     uint8_t rxEnable = BR_UART_C2_RE(baseAddr);
728
729     if (txEnable || rxEnable)
730     {
731         return kStatus_UART_TxOrRxNotDisabled;
732     }
733     else
734     {
735         BW_UART_PFIFO_RXFE(baseAddr, enable);
736         return kStatus_UART_Success;
737     }
738 }
739
740 /*FUNCTION**********************************************************************
741  *
742  * Function Name : UART_HAL_FlushTxFifo
743  * Description   : Flush the UART transmit FIFO.
744  * This function allows you to flush the UART transmit FIFO for a particular modulei
745  * baseAddr. Flushing the FIFO may result in data loss. It is recommended that the
746  * transmitter should be disabled before calling this function.
747  *
748  *END**************************************************************************/
749 uart_status_t UART_HAL_FlushTxFifo(uint32_t baseAddr)
750 {
751     /* in order to flush the tx fifo, UARTx_C2[TE] (transmitter) must be disabled
752      * if not, return an error code */
753     if (BR_UART_C2_TE(baseAddr) != 0)
754     {
755         return kStatus_UART_TxNotDisabled;
756     }
757     else
758     {
759         /* Set the bit to flush fifo*/
760         BW_UART_CFIFO_TXFLUSH(baseAddr, 1U);
761         return kStatus_UART_Success;
762     }
763 }
764
765 /*FUNCTION**********************************************************************
766  *
767  * Function Name : UART_HAL_FlushRxFifo
768  * Description   : Flush the UART receive FIFO.
769  * This function allows you to flush the UART receive FIFO for a particular module
770  * baseAddr. Flushing the FIFO may result in data loss. It is recommended that the
771  * receiver should be disabled before calling this function.
772  *
773  *END**************************************************************************/
774 uart_status_t UART_HAL_FlushRxFifo(uint32_t baseAddr)
775 {
776     /* in order to flush the rx fifo, UARTx_C2[RE] (receiver) must be disabled
777      * if not, return an error code. */
778     if (BR_UART_C2_RE(baseAddr) != 0)
779     {
780         return kStatus_UART_RxNotDisabled;
781     }
782     else
783     {
784         /* Set the bit to flush fifo*/
785         BW_UART_CFIFO_RXFLUSH(baseAddr, 1U);
786         return kStatus_UART_Success;
787     }
788 }
789
790 /*FUNCTION**********************************************************************
791  *
792  * Function Name : UART_HAL_SetTxFifoWatermark
793  * Description   : Set the UART transmit FIFO watermark value.
794  * Programming the transmit watermark should be done when UART the transmitter is
795  * disabled and the value must be set less than the size obtained from
796  * UART_HAL_GetTxFifoSize.
797  *
798  *END**************************************************************************/
799 uart_status_t UART_HAL_SetTxFifoWatermark(uint32_t baseAddr, uint8_t watermark)
800 {
801     /* in order to set the tx watermark, UARTx_C2[TE] (transmitter) must be disabled
802      * if not, return an error code
803      */
804     if (BR_UART_C2_TE(baseAddr) != 0)
805     {
806         return kStatus_UART_TxNotDisabled;
807     }
808     else
809     {
810         /* Programming the transmit watermark should be done when the transmitter is
811          * disabled and the value must be set less than the size given in
812          * PFIFO[TXFIFOSIZE] */
813         HW_UART_TWFIFO_WR(baseAddr, watermark);
814         return kStatus_UART_Success;
815     }
816 }
817
818 /*FUNCTION**********************************************************************
819  *
820  * Function Name : UART_HAL_SetRxFifoWatermark
821  * Description   : Set the UART receive FIFO watermark value.
822  * Programming the receive watermark should be done when the receiver is disabled
823  * and the value must be set less than the size obtained from UART_HAL_GetRxFifoSize
824  * and greater than zero.
825  *
826  *END**************************************************************************/
827 uart_status_t UART_HAL_SetRxFifoWatermark(uint32_t baseAddr, uint8_t watermark)
828 {
829     /* in order to set the rx watermark, UARTx_C2[RE] (receiver) must be disabled
830      * if not, return an error code. */
831     if (BR_UART_C2_RE(baseAddr) != 0)
832     {
833         return kStatus_UART_RxNotDisabled;
834     }
835     else
836     {
837         /* Programming the receive watermark should be done when the receiver is
838          * disabled and the value must be set less than the size given in
839          * PFIFO[RXFIFOSIZE] and greater than zero.  */
840         HW_UART_RWFIFO_WR(baseAddr, watermark);
841         return kStatus_UART_Success;
842     }
843 }
844 #endif  /* FSL_FEATURE_UART_HAS_FIFO*/
845
846 /*******************************************************************************
847  * UART Special Feature Configurations
848  ******************************************************************************/
849
850 /*FUNCTION**********************************************************************
851  *
852  * Function Name : UART_HAL_PutReceiverInStandbyMode
853  * Description   : Place the UART receiver in standby mode.
854  * This function, when called, will place the UART receiver into standby mode.
855  * In some UART baseAddrs, there is a condition that must be met before placing rx in standby mode.
856  * Before placing UART in standby, you need to first determine if receiver is set to
857  * wake on idle and if receiver is already in idle state. Per ref manual:
858  * NOTE: RWU should only be set with C1[WAKE] = 0 (wakeup on idle) if the channel is currently
859  * not idle.
860  * This can be determined by the S2[RAF] flag. If set to wake up FROM an IDLE event and the channel
861  * is already idle, it is possible that the UART will discard data since data must be received
862  * (or a LIN break detect) after an IDLE is detected before IDLE is allowed to reasserted.
863  *
864  *END**************************************************************************/
865 uart_status_t UART_HAL_PutReceiverInStandbyMode(uint32_t baseAddr)
866 {
867     /* In some uart baseAddrs, there is a condition that must be met before placing
868      * rx in standby mode.
869      * Before placing uart in standby, need to first determine if receiver is set to
870      * wake on idle and if receiver is already in idle state. Per ref manual:
871      * NOTE: RWU should only be set with C1[WAKE] = 0 (wakeup on idle) if the channel is
872      * currently not idle.
873      * This can be determined by the S2[RAF] flag. If set to wake up an IDLE event and
874      * the channel is already idle, it is possible that the UART will discard data since data
875      * must be received (or a LIN break detect) after an IDLE is detected before IDLE is
876      * allowed to reasserted.
877      */
878     uart_wakeup_method_t rxWakeMethod;
879     bool uart_current_rx_state;
880
881     /* see if wake is set for idle or */
882     rxWakeMethod = UART_HAL_GetReceiverWakeupMethod(baseAddr);
883     uart_current_rx_state = UART_HAL_GetStatusFlag(baseAddr, kUartRxActive);
884
885     /* if both rxWakeMethod is set for idle and current rx state is idle, don't put in standy*/
886     if ((rxWakeMethod == kUartIdleLineWake) && (uart_current_rx_state == 0))
887     {
888         return kStatus_UART_RxStandbyModeError;
889     }
890     else
891     {
892         /* set the RWU bit to place receiver into standby mode*/
893         HW_UART_C2_SET(baseAddr, BM_UART_C2_RWU);
894         return kStatus_UART_Success;
895     }
896 }
897
898 /*FUNCTION**********************************************************************
899  *
900  * Function Name : UART_HAL_ConfigIdleLineDetect
901  * Description   : Configure the operation options of the UART idle line detect.
902  * This function allows the user to configure the UART idle-line detect operation. There are two
903  * separate operations for the user to configure, the idle line bit-count start and the receive
904  * wake up affect on IDLE status bit. The user will pass in a stucture of type
905  * uart_idle_line_config_t.
906  *
907  *END**************************************************************************/
908 void UART_HAL_ConfigIdleLineDetect(uint32_t baseAddr, uint8_t idleLine, uint8_t rxWakeIdleDetect)
909 {
910     /* Configure the idle line detection configuration as follows:
911      * configure the ILT to bit count after start bit or stop bit
912      * configure RWUID to set or not set IDLE status bit upon detection of
913      * an idle character when recevier in standby */
914     BW_UART_C1_ILT(baseAddr, idleLine);
915     BW_UART_S2_RWUID(baseAddr, rxWakeIdleDetect);
916 }
917 #if FSL_FEATURE_UART_HAS_ADDRESS_MATCHING
918 /*FUNCTION**********************************************************************
919  *
920  * Function Name : UART_HAL_SetMatchAddress
921  * Description   : Configure the UART match address mode control operation. (Note: Feature
922  *                 available on select UART baseAddrs)
923  * The function allows the user to configure the UART match address control operation. The user
924  * has the option to enable the match address mode and to program the match address value. There
925  * are two match address modes, each with it's own enable and programmable match address value.
926  *
927  *END**************************************************************************/
928 void UART_HAL_SetMatchAddress( uint32_t baseAddr, bool matchAddrMode1, bool matchAddrMode2,
929                                uint8_t matchAddrValue1, uint8_t matchAddrValue2)
930 {
931     BW_UART_C4_MAEN1(baseAddr, matchAddrMode1); /* Match Address Mode Enable 1 */
932     BW_UART_C4_MAEN2(baseAddr, matchAddrMode2); /* Match Address Mode Enable 2 */
933     HW_UART_MA1_WR(baseAddr, matchAddrValue1); /* match address register 1 */
934     HW_UART_MA2_WR(baseAddr, matchAddrValue2); /* match address register 2 */
935 }
936 #endif
937
938 #if FSL_FEATURE_UART_HAS_IR_SUPPORT
939 /*FUNCTION**********************************************************************
940  *
941  * Function Name : UART_HAL_SetInfraredOperation
942  * Description   : Configure the UART infrared operation.
943  * The function allows the user to enable or disable the UART infrared (IR) operation
944  * and to configure the IR pulse width.
945  *
946  *END**************************************************************************/
947 void UART_HAL_SetInfraredOperation(uint32_t baseAddr, bool enable,
948                                            uart_ir_tx_pulsewidth_t pulseWidth)
949 {
950     /* enable or disable infrared */
951     BW_UART_IR_IREN(baseAddr, enable);
952     /* configure the narrow pulse width of the IR pulse */
953     BW_UART_IR_TNP(baseAddr, pulseWidth);
954 }
955 #endif  /* FSL_FEATURE_UART_HAS_IR_SUPPORT */
956
957
958 /*******************************************************************************
959  * EOF
960  ******************************************************************************/
961