2 * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * o Redistributions of source code must retain the above copyright notice, this list
9 * of conditions and the following disclaimer.
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.
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.
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.
30 #include "fsl_uart_hal.h"
32 /*******************************************************************************
34 ******************************************************************************/
35 /*******************************************************************************
36 * UART Common Configurations
37 ******************************************************************************/
38 /*FUNCTION**********************************************************************
40 * Function Name : UART_HAL_Init
41 * Description : This function initializes the module to a known state.
43 *END**************************************************************************/
44 void UART_HAL_Init(uint32_t baseAddr)
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);
57 HW_UART_C4_WR(baseAddr, 0U);
58 #if FSL_FEATURE_UART_HAS_DMA_ENABLE
59 HW_UART_C5_WR(baseAddr, 0U);
61 #if FSL_FEATURE_UART_HAS_MODEM_SUPPORT
62 HW_UART_MODEM_WR(baseAddr, 0U);
64 #if FSL_FEATURE_UART_HAS_IR_SUPPORT
65 HW_UART_IR_WR(baseAddr, 0U);
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);
76 /*FUNCTION**********************************************************************
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.
84 *END**************************************************************************/
85 uart_status_t UART_HAL_SetBaudRate(uint32_t baseAddr, uint32_t sourceClockInHz, uint32_t baudRate)
87 /* BaudRate = (SourceClkInHz)/[16 * (SBR + BRFA)]
88 * First, calculate SBR (integer part) then calculate the BRFA (fine adjust fractional field). */
91 /* calculate the baud rate modulo divisor, sbr*/
92 sbr = sourceClockInHz / (baudRate * 16);
94 /* check to see if sbr is out of range of register bits */
95 if ( (sbr > 0x1FFF) || (sbr < 1) )
97 /* unsupported baud rate for given source clock input*/
98 return kStatus_UART_BaudRateCalculationError;
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);
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;
110 /* write the brfa value to the register*/
111 BW_UART_C4_BRFA(baseAddr, brfa);
114 return kStatus_UART_Success;
117 /*FUNCTION**********************************************************************
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.
125 *END**************************************************************************/
126 void UART_HAL_SetBaudRateDivisor(uint32_t baseAddr, uint16_t baudRateDivisor)
128 /* check to see if baudRateDivisor is out of range of register bits */
129 assert( (baudRateDivisor < 0x1FFF) && (baudRateDivisor > 1) );
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);
136 /*FUNCTION**********************************************************************
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.
144 *END**************************************************************************/
145 void UART_HAL_SetTxRxInversionCmd(uint32_t baseAddr, bool rxInvertEnable, bool txInvertEnable)
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);
153 /*******************************************************************************
154 * UART Transfer Functions
155 ******************************************************************************/
156 /*FUNCTION**********************************************************************
158 * Function Name : UART_HAL_Putchar
159 * Description : This function allows the user to send an 8-bit character from the UART
162 *END**************************************************************************/
163 void UART_HAL_Putchar(uint32_t baseAddr, uint8_t data)
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);
175 /*FUNCTION**********************************************************************
177 * Function Name : UART_HAL_Putchar9
178 * Description : This function allows the user to send a 9-bit character from the UART
181 *END**************************************************************************/
182 void UART_HAL_Putchar9(uint32_t baseAddr, uint16_t data)
184 uint8_t ninthDataBit;
186 ninthDataBit = (data >> 8U) & 0x1U; /* isolate the ninth data bit*/
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);
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);
203 /*FUNCTION**********************************************************************
205 * Function Name : UART_HAL_Getchar
206 * Description : This function gets a received 8-bit character from the UART data register.
208 *END**************************************************************************/
209 void UART_HAL_Getchar(uint32_t baseAddr, uint8_t *readData)
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
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*/
223 /*FUNCTION**********************************************************************
225 * Function Name : UART_HAL_Getchar9
226 * Description : This function gets a received 9-bit character from the UART data register.
228 *END**************************************************************************/
229 void UART_HAL_Getchar9(uint32_t baseAddr, uint16_t *readData)
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;
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
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*/
253 /*******************************************************************************
254 * UART Interrupts and DMA
255 ******************************************************************************/
256 /*FUNCTION**********************************************************************
258 * Function Name : UART_HAL_ConfigureInterrupts
259 * Description : Configure the UART module interrupts to enable/disable various
262 *END**************************************************************************/
263 void UART_HAL_SetIntMode(uint32_t baseAddr, uart_interrupt_t interrupt, bool enable)
265 uint8_t reg = (uint32_t)interrupt >> UART_SHIFT;
266 uint32_t temp = 1U << (uint8_t)interrupt;
271 enable ? HW_UART_BDH_SET(baseAddr, temp) : HW_UART_BDH_CLR(baseAddr, temp);
274 enable ? HW_UART_C2_SET(baseAddr, temp) : HW_UART_C2_CLR(baseAddr, temp);
277 enable ? HW_UART_C3_SET(baseAddr, temp) : HW_UART_C3_CLR(baseAddr, temp);
279 #if FSL_FEATURE_UART_HAS_FIFO
281 enable ? HW_UART_CFIFO_SET(baseAddr, temp) : HW_UART_CFIFO_CLR(baseAddr, temp);
289 /*FUNCTION**********************************************************************
291 * Function Name : UART_HAL_GetIntMode
292 * Description : Return whether the UART module interrupts is enabled/disabled.
294 *END**************************************************************************/
295 bool UART_HAL_GetIntMode(uint32_t baseAddr, uart_interrupt_t interrupt)
297 uint8_t reg = (uint32_t)interrupt >> UART_SHIFT;
303 temp = HW_UART_BDH_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
306 temp = HW_UART_C2_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
309 temp = HW_UART_C3_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
311 #if FSL_FEATURE_UART_HAS_FIFO
313 temp = HW_UART_CFIFO_RD(baseAddr) >> (uint8_t)(interrupt) & 1U;
321 #if FSL_FEATURE_UART_HAS_DMA_SELECT
322 /*FUNCTION**********************************************************************
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.
331 *END**************************************************************************/
332 void UART_HAL_ConfigureDma(uint32_t baseAddr, bool txDmaConfig, bool rxDmaConfig)
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.
346 if (txDmaConfig == 1)
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 */
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 */
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.
369 if (rxDmaConfig == 1)
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 */
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 */
383 /*FUNCTION**********************************************************************
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.
389 *END**************************************************************************/
390 bool UART_HAL_IsTxdmaEnabled(uint32_t baseAddr)
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;
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.
407 if (BR_UART_C5_TDMAS(baseAddr) == 1)
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))
412 /* UART module is configured to generate TxDMA request*/
417 /* UART module is NOT configured to generate TxDMA request*/
423 /* UART module is NOT configured to generate TxDMA request*/
428 /*FUNCTION**********************************************************************
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.
434 *END**************************************************************************/
435 bool UART_HAL_IsRxdmaEnabled(uint32_t baseAddr)
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.
446 if (BR_UART_C5_RDMAS(baseAddr) == 1)
448 /* enable uart to generate receive DMA request*/
449 if (BR_UART_C2_RIE(baseAddr) == 1)
451 /* UART module is configured to generate RxDMA request*/
456 /* UART module is NOT configured to generate RxDMA request*/
462 /* UART module is NOT configured to generate RxDMA request*/
467 /*******************************************************************************
468 * UART UART Status Flags
469 ******************************************************************************/
470 /*FUNCTION**********************************************************************
472 * Function Name : UART_HAL_GetStatusFlag
473 * Description : Get UART status flag states.
475 *END**************************************************************************/
476 bool UART_HAL_GetStatusFlag(uint32_t baseAddr, uart_status_flag_t statusFlag)
478 uint8_t reg = (uint32_t)statusFlag >> UART_SHIFT;
484 temp = HW_UART_S1_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
487 temp = HW_UART_S2_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
489 #if FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS
491 temp = HW_UART_ED_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
494 #if FSL_FEATURE_UART_HAS_FIFO
496 temp = HW_UART_SFIFO_RD(baseAddr) >> (uint8_t)(statusFlag) & 1U;
505 /*FUNCTION**********************************************************************
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.
512 *END**************************************************************************/
513 uart_status_t UART_HAL_ClearStatusFlag(uint32_t baseAddr, uart_status_flag_t statusFlag)
515 uart_status_t returnCode; /* return code variable */
516 returnCode = kStatus_UART_Success; /* default return code, unless changed by error condition*/
518 /* clear the desired, individual status flag as passed in through statusFlag */
521 case kUartTxDataRegEmpty:
522 /* This flag is cleared automatically by other uart operations and
523 * cannot be manually cleared, return error code
525 returnCode = kStatus_UART_ClearStatusFlagError;
528 case kUartTxComplete:
529 /* This flag is cleared automatically by other uart operations and
530 * cannot be manually cleared, return error code
532 returnCode = kStatus_UART_ClearStatusFlagError;
535 case kUartRxDataRegFull:
536 /* This flag is cleared automatically by other uart operations and
537 * cannot be manually cleared, return error code
539 returnCode = kStatus_UART_ClearStatusFlagError;
542 case kUartIdleLineDetect:
543 /* to clear the status is a two-step process:
544 * first, read S1 register with the status flag set
546 HW_UART_S1_RD(baseAddr);
547 /* second, read the data register*/
548 HW_UART_D_RD(baseAddr);
552 /* to clear the status is a two-step process:
553 * first, read S1 register with the status flag set
555 HW_UART_S1_RD(baseAddr);
556 /* second, read the data register*/
557 HW_UART_D_RD(baseAddr);
560 case kUartNoiseDetect:
561 /* to clear the status is a two-step process:
562 * first, read S1 register with the status flag set
564 HW_UART_S1_RD(baseAddr);
565 /* second, read the data register*/
566 HW_UART_D_RD(baseAddr);
570 /* to clear the status is a two-step process:
571 * first, read S1 register with the status flag set
573 HW_UART_S1_RD(baseAddr);
574 /* second, read the data register*/
575 HW_UART_D_RD(baseAddr);
579 /* to clear the status is a two-step process:
580 * first, read S1 register with the status flag set
582 HW_UART_S1_RD(baseAddr);
583 /* second, read the data register*/
584 HW_UART_D_RD(baseAddr);
587 case kUartLineBreakDetect:
588 /* write one to clear status flag */
589 HW_UART_S2_SET(baseAddr, BM_UART_S2_LBKDIF);
592 case kUartRxActiveEdgeDetect:
593 /* write one to clear status flag */
594 HW_UART_S2_SET(baseAddr, BM_UART_S2_RXEDGIF);
598 /* This flag is cleared automatically by other uart operations and
599 * cannot be manually cleared, return error code
601 returnCode = kStatus_UART_ClearStatusFlagError;
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
609 returnCode = kStatus_UART_ClearStatusFlagError;
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
616 returnCode = kStatus_UART_ClearStatusFlagError;
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
624 returnCode = kStatus_UART_ClearStatusFlagError;
627 case kUartRxBuffEmpty:
628 /* This flag is not clearable, it simply reflects the current
629 * status of the buffer/FIFO
631 returnCode = kStatus_UART_ClearStatusFlagError;
634 case kUartTxBuffOverflow:
635 /* write one to clear status flag */
636 HW_UART_SFIFO_SET(baseAddr, BM_UART_SFIFO_TXOF);
639 case kUartRxBuffUnderflow:
640 /* write one to clear status flag */
641 HW_UART_SFIFO_SET(baseAddr, BM_UART_SFIFO_RXUF);
644 default: /* catch inputs that are not recognized*/
645 returnCode = kStatus_UART_ClearStatusFlagError;
652 /*FUNCTION**********************************************************************
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.
659 *END**************************************************************************/
660 void UART_HAL_ClearAllNonAutoclearStatusFlags(uint32_t baseAddr)
662 /* clear the status flags that can be manually cleared
663 * note, some flags are automatically cleared and cannot be cleared automatically
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);
678 /*******************************************************************************
679 * UART FIFO Configurations
680 ******************************************************************************/
681 #if FSL_FEATURE_UART_HAS_FIFO
682 /*FUNCTION**********************************************************************
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.
691 *END**************************************************************************/
692 uart_status_t UART_HAL_SetTxFifoCmd(uint32_t baseAddr, bool enable)
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);
700 if (txEnable || rxEnable)
702 return kStatus_UART_TxOrRxNotDisabled;
706 BW_UART_PFIFO_TXFE(baseAddr, enable);
707 return kStatus_UART_Success;
711 /*FUNCTION**********************************************************************
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.
720 *END**************************************************************************/
721 uart_status_t UART_HAL_SetRxFifoCmd(uint32_t baseAddr, bool enable)
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);
729 if (txEnable || rxEnable)
731 return kStatus_UART_TxOrRxNotDisabled;
735 BW_UART_PFIFO_RXFE(baseAddr, enable);
736 return kStatus_UART_Success;
740 /*FUNCTION**********************************************************************
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.
748 *END**************************************************************************/
749 uart_status_t UART_HAL_FlushTxFifo(uint32_t baseAddr)
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)
755 return kStatus_UART_TxNotDisabled;
759 /* Set the bit to flush fifo*/
760 BW_UART_CFIFO_TXFLUSH(baseAddr, 1U);
761 return kStatus_UART_Success;
765 /*FUNCTION**********************************************************************
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.
773 *END**************************************************************************/
774 uart_status_t UART_HAL_FlushRxFifo(uint32_t baseAddr)
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)
780 return kStatus_UART_RxNotDisabled;
784 /* Set the bit to flush fifo*/
785 BW_UART_CFIFO_RXFLUSH(baseAddr, 1U);
786 return kStatus_UART_Success;
790 /*FUNCTION**********************************************************************
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.
798 *END**************************************************************************/
799 uart_status_t UART_HAL_SetTxFifoWatermark(uint32_t baseAddr, uint8_t watermark)
801 /* in order to set the tx watermark, UARTx_C2[TE] (transmitter) must be disabled
802 * if not, return an error code
804 if (BR_UART_C2_TE(baseAddr) != 0)
806 return kStatus_UART_TxNotDisabled;
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;
818 /*FUNCTION**********************************************************************
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.
826 *END**************************************************************************/
827 uart_status_t UART_HAL_SetRxFifoWatermark(uint32_t baseAddr, uint8_t watermark)
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)
833 return kStatus_UART_RxNotDisabled;
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;
844 #endif /* FSL_FEATURE_UART_HAS_FIFO*/
846 /*******************************************************************************
847 * UART Special Feature Configurations
848 ******************************************************************************/
850 /*FUNCTION**********************************************************************
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
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.
864 *END**************************************************************************/
865 uart_status_t UART_HAL_PutReceiverInStandbyMode(uint32_t baseAddr)
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.
878 uart_wakeup_method_t rxWakeMethod;
879 bool uart_current_rx_state;
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);
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))
888 return kStatus_UART_RxStandbyModeError;
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;
898 /*FUNCTION**********************************************************************
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.
907 *END**************************************************************************/
908 void UART_HAL_ConfigIdleLineDetect(uint32_t baseAddr, uint8_t idleLine, uint8_t rxWakeIdleDetect)
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);
917 #if FSL_FEATURE_UART_HAS_ADDRESS_MATCHING
918 /*FUNCTION**********************************************************************
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.
927 *END**************************************************************************/
928 void UART_HAL_SetMatchAddress( uint32_t baseAddr, bool matchAddrMode1, bool matchAddrMode2,
929 uint8_t matchAddrValue1, uint8_t matchAddrValue2)
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 */
938 #if FSL_FEATURE_UART_HAS_IR_SUPPORT
939 /*FUNCTION**********************************************************************
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.
946 *END**************************************************************************/
947 void UART_HAL_SetInfraredOperation(uint32_t baseAddr, bool enable,
948 uart_ir_tx_pulsewidth_t pulseWidth)
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);
955 #endif /* FSL_FEATURE_UART_HAS_IR_SUPPORT */
958 /*******************************************************************************
960 ******************************************************************************/