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_lpuart_hal.h"
32 #ifndef MBED_NO_LPUART
34 /*******************************************************************************
36 ******************************************************************************/
37 /*FUNCTION**********************************************************************
39 * Function Name : LPUART_HAL_Init
40 * Description : Initializes the LPUART controller to known state.
42 *END**************************************************************************/
43 void LPUART_HAL_Init(uint32_t baseAddr)
45 HW_LPUART_BAUD_WR(baseAddr, 0x0F000004);
46 HW_LPUART_STAT_WR(baseAddr, 0xC01FC000);
47 HW_LPUART_CTRL_WR(baseAddr, 0x00000000);
48 HW_LPUART_MATCH_WR(baseAddr, 0x00000000);
49 HW_LPUART_MODIR_WR(baseAddr, 0x00000000);
52 /*FUNCTION**********************************************************************
54 * Function Name : LPUART_HAL_SetBaudRate
55 * Description : Configures the LPUART baud rate.
56 * In some LPUART instances the user must disable the transmitter/receiver
57 * before calling this function.
58 * Generally, this may be applied to all LPUARTs to ensure safe operation.
60 *END**************************************************************************/
61 lpuart_status_t LPUART_HAL_SetBaudRate(uint32_t baseAddr, uint32_t sourceClockInHz,
62 uint32_t desiredBaudRate)
64 uint16_t sbr, sbrTemp, i;
65 uint32_t osr, tempDiff, calculatedBaud, baudDiff;
67 /* This lpuart instantiation uses a slightly different baud rate calculation */
68 /* The idea is to use the best OSR (over-sampling rate) possible */
69 /* Note, osr is typically hard-set to 16 in other lpuart instantiations */
70 /* First calculate the baud rate using the minimum OSR possible (4) */
72 sbr = (sourceClockInHz/(desiredBaudRate * osr));
73 calculatedBaud = (sourceClockInHz / (osr * sbr));
75 if (calculatedBaud > desiredBaudRate)
77 baudDiff = calculatedBaud - desiredBaudRate;
81 baudDiff = desiredBaudRate - calculatedBaud;
84 /* loop to find the best osr value possible, one that generates minimum baudDiff */
85 /* iterate through the rest of the supported values of osr */
86 for (i = 5; i <= 32; i++)
88 /* calculate the temporary sbr value */
89 sbrTemp = (sourceClockInHz/(desiredBaudRate * i));
90 /* calculate the baud rate based on the temporary osr and sbr values */
91 calculatedBaud = (sourceClockInHz / (i * sbrTemp));
93 if (calculatedBaud > desiredBaudRate)
95 tempDiff = calculatedBaud - desiredBaudRate;
99 tempDiff = desiredBaudRate - calculatedBaud;
102 if (tempDiff <= baudDiff)
105 osr = i; /* update and store the best osr value calculated */
106 sbr = sbrTemp; /* update store the best sbr value calculated */
110 /* next, check to see if actual baud rate is within 3% of desired baud rate */
111 /* based on the best calculate osr value */
112 if (baudDiff < ((desiredBaudRate / 100) * 3))
114 /* Acceptable baud rate */
115 /* Check if osr is between 4x and 7x oversampling */
116 /* If so, then "BOTHEDGE" sampling must be turned on */
117 if ((osr > 3) && (osr < 8))
119 BW_LPUART_BAUD_BOTHEDGE(baseAddr, 1);
122 /* program the osr value (bit value is one less than actual value) */
123 BW_LPUART_BAUD_OSR(baseAddr, (osr-1));
125 /* write the sbr value to the BAUD registers */
126 BW_LPUART_BAUD_SBR(baseAddr, sbr);
130 /* Unacceptable baud rate difference of more than 3% */
131 return kStatus_LPUART_BaudRatePercentDiffExceeded;
134 return kStatus_LPUART_Success;
137 /*FUNCTION**********************************************************************
139 * Function Name : LPUART_HAL_SetBitCountPerChar
140 * Description : Configures the number of bits per character in the LPUART controller.
141 * In some LPUART instances, the user should disable the transmitter/receiver
142 * before calling this function.
143 * Generally, this may be applied to all LPUARTs to ensure safe operation.
145 *END**************************************************************************/
146 void LPUART_HAL_SetBitCountPerChar(uint32_t baseAddr, lpuart_bit_count_per_char_t bitCountPerChar)
148 if(bitCountPerChar == kLpuart10BitsPerChar)
150 BW_LPUART_BAUD_M10(baseAddr, 1); /* set M10 for 10-bit mode, M bit in C1 is don't care */
154 BW_LPUART_CTRL_M(baseAddr, bitCountPerChar); /* config 8- (M=0) or 9-bits (M=1) */
155 BW_LPUART_BAUD_M10(baseAddr, 0); /* clear M10 to make sure not 10-bit mode */
159 /*FUNCTION**********************************************************************
161 * Function Name : LPUART_HAL_SetParityMode
162 * Description : Configures parity mode in the LPUART controller.
163 * In some LPUART instances, the user should disable the transmitter/receiver
164 * before calling this function.
165 * Generally, this may be applied to all LPUARTs to ensure safe operation.
167 *END**************************************************************************/
168 void LPUART_HAL_SetParityMode(uint32_t baseAddr, lpuart_parity_mode_t parityModeType)
170 /* configure the parity enable/type */
172 if ((parityModeType) == kLpuartParityDisabled)
174 /* parity disabled, hence parity type is don't care */
175 BW_LPUART_CTRL_PE(baseAddr, 0);
180 BW_LPUART_CTRL_PE(baseAddr, 1);
181 /* parity odd/even depending on parity mode setting */
182 BW_LPUART_CTRL_PT(baseAddr, (parityModeType) & 0x1);
187 /*FUNCTION**********************************************************************
189 * Function Name : LPUART_HAL_SetTxRxInversionCmd
190 * Description : Configures the transmit and receive inversion control in the LPUART controller.
191 * This function should only be called when the LPUART is between transmit and receive packets.
193 *END**************************************************************************/
194 void LPUART_HAL_SetTxRxInversionCmd(uint32_t baseAddr, uint32_t rxInvert, uint32_t txInvert)
196 /* 0 - receive data not inverted, 1 - receive data inverted */
197 BW_LPUART_STAT_RXINV(baseAddr, rxInvert);
198 /* 0 - transmit data not inverted, 1 - transmit data inverted */
199 BW_LPUART_CTRL_TXINV(baseAddr, txInvert);
202 /*FUNCTION**********************************************************************
204 * Function Name : LPUART_HAL_EnableTransmitter
205 * Description : Enables the LPUART transmitter.
207 *END**************************************************************************/
208 void LPUART_HAL_EnableTransmitter(uint32_t baseAddr)
210 /* enable the transmitter based on the lpuart baseAddr */
212 /* for this lpuart baseAddr, there is a two step process to clear the transmit complete */
214 /* 1. Read the status register with the status bit set */
215 /* 2. enable the transmitter (change TE from 0 to 1) */
216 /* first read the status register */
218 /* no need to store the read value, it's assumed the status bit is set */
219 HW_LPUART_STAT_RD(baseAddr);
220 /* second, enable the transmitter */
221 BW_LPUART_CTRL_TE(baseAddr, 1);
224 /*FUNCTION**********************************************************************
226 * Function Name : LPUART_HAL_SetIntMode
227 * Description : Configures the LPUART module interrupts to enable/disable various interrupt sources.
229 *END**************************************************************************/
230 void LPUART_HAL_SetIntMode(uint32_t baseAddr, lpuart_interrupt_t interrupt, bool enable)
232 uint32_t reg = (uint32_t)(interrupt) >> LPUART_SHIFT;
233 uint32_t temp = 1U << (uint32_t)interrupt;
237 case LPUART_BAUD_REG_ID:
238 enable ? HW_LPUART_BAUD_SET(baseAddr, temp) : HW_LPUART_BAUD_CLR(baseAddr, temp);
240 case LPUART_STAT_REG_ID:
241 enable ? HW_LPUART_STAT_SET(baseAddr, temp) : HW_LPUART_STAT_CLR(baseAddr, temp);
243 case LPUART_CTRL_REG_ID:
244 enable ? HW_LPUART_CTRL_SET(baseAddr, temp) : HW_LPUART_CTRL_CLR(baseAddr, temp);
246 case LPUART_DATA_REG_ID:
247 enable ? HW_LPUART_DATA_SET(baseAddr, temp) : HW_LPUART_DATA_CLR(baseAddr, temp);
249 case LPUART_MATCH_REG_ID:
250 enable ? HW_LPUART_MATCH_SET(baseAddr, temp) : HW_LPUART_MATCH_CLR(baseAddr, temp);
252 case LPUART_MODIR_REG_ID:
253 enable ? HW_LPUART_MODIR_SET(baseAddr, temp) : HW_LPUART_MODIR_CLR(baseAddr, temp);
260 /*FUNCTION**********************************************************************
262 * Function Name : LPUART_HAL_GetIntMode
263 * Description : Returns whether the LPUART module interrupts is enabled/disabled.
265 *END**************************************************************************/
266 bool LPUART_HAL_GetIntMode(uint32_t baseAddr, lpuart_interrupt_t interrupt)
268 uint32_t reg = (uint32_t)(interrupt) >> LPUART_SHIFT;
273 case LPUART_BAUD_REG_ID:
274 retVal = HW_LPUART_BAUD_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
276 case LPUART_STAT_REG_ID:
277 retVal = HW_LPUART_STAT_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
279 case LPUART_CTRL_REG_ID:
280 retVal = HW_LPUART_CTRL_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
282 case LPUART_DATA_REG_ID:
283 retVal = HW_LPUART_DATA_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
285 case LPUART_MATCH_REG_ID:
286 retVal = HW_LPUART_MATCH_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
288 case LPUART_MODIR_REG_ID:
289 retVal = HW_LPUART_MODIR_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
298 #if FSL_FEATURE_LPUART_HAS_DMA_ENABLE
299 /*FUNCTION**********************************************************************
301 * Function Name : LPUART_HAL_ConfigureDma
302 * Description : LPUART configures DMA requests for Transmitter and Receiver.
304 *END**************************************************************************/
305 void LPUART_HAL_ConfigureDma(uint32_t baseAddr, bool txDmaConfig, bool rxDmaConfig)
307 /* TDMAE configures the transmit data register empty flag, S1[TDRE], */
308 /* to generate a DMA request. */
309 BW_LPUART_BAUD_TDMAE(baseAddr, txDmaConfig) ;/* set TDMAE to enable, clear to disable */
310 /* RDMAE configures the receive data register fell flag, S1[RDRF], */
311 /* to generate a DMA request. */
312 BW_LPUART_BAUD_RDMAE(baseAddr, rxDmaConfig); /* set RDMAE to enable, clear to disable */
316 /*FUNCTION**********************************************************************
318 * Function Name : LPUART_HAL_GetWaitModeOperationConfig
319 * Description : LPUART configures DMA requests for Transmitter and Receiver.
321 *END**************************************************************************/
322 lpuart_operation_config_t LPUART_HAL_GetWaitModeOperationConfig(uint32_t baseAddr)
324 /* get configuration lpuart operation in wait mode */
325 /* In CPU wait mode: 0 - lpuart clocks continue to run; 1 - lpuart clocks freeze */
326 if (BR_LPUART_CTRL_DOZEEN(baseAddr) == 0)
328 return kLpuartOperates;
337 /*FUNCTION**********************************************************************
339 * Function Name : LPUART_HAL_SedLoopbackCmd
340 * Description : Configures the LPUART loopback operation (enable/disable loopback operation)
341 * In some LPUART instances, the user should disable the transmitter/receiver
342 * before calling this function.
343 * Generally, this may be applied to all LPUARTs to ensure safe operation.
345 *END**************************************************************************/
346 void LPUART_HAL_SedLoopbackCmd(uint32_t baseAddr, bool enable)
348 /* configure lpuart to enable/disable operation in loopback mode */
350 /* configure LOOPS bit to enable(1)/disable(0) loopback mode, but also need to clear RSRC */
351 BW_LPUART_CTRL_LOOPS(baseAddr, enable);
353 /* clear RSRC for loopback mode, and if loopback disabled, */
354 /* this bit has no meaning but clear anyway */
355 /* to set it back to default value */
356 BW_LPUART_CTRL_RSRC(baseAddr, 0);
360 /*FUNCTION**********************************************************************
362 * Function Name : LPUART_HAL_SetSingleWireCmd
363 * Description : Configures the LPUART single-wire operation (enable/disable single-wire mode)
364 * In some LPUART instances, the user should disable the transmitter/receiver
365 * before calling this function.
366 * Generally, this may be applied to all LPUARTs to ensure safe operation.
368 *END**************************************************************************/
369 void LPUART_HAL_SetSingleWireCmd(uint32_t baseAddr, bool enable)
371 /* configure lpuart to enable/disable operation in single mode */
373 /* to enable single-wire mode, need both LOOPS and RSRC set, to disable, clear both */
374 BW_LPUART_CTRL_LOOPS(baseAddr, enable);
375 BW_LPUART_CTRL_RSRC(baseAddr, enable);
378 /*FUNCTION**********************************************************************
380 * Function Name : LPUART_HAL_PutReceiverInStandbyMode
381 * Description : Places the LPUART receiver in standby mode.
382 * In some LPUART instances,
383 * before placing LPUART in standby mode, first determine whether the receiver is set to
384 * wake on idle or whether it is already in idle state.
385 * NOTE that the RWU should only be set with C1[WAKE] = 0 (wakeup on idle) if the channel is currently
387 * This can be determined by the S2[RAF] flag. If it is set to wake up an IDLE event and the channel is
388 * already idle, it is possible that the LPUART will discard data since data must be received
389 * (or a LIN break detect) after an IDLE is detected and before IDLE is allowed to reasserted.
391 *END**************************************************************************/
392 lpuart_status_t LPUART_HAL_PutReceiverInStandbyMode(uint32_t baseAddr)
394 /* In some lpuart instances, there is a condition that must be met before placing */
395 /* rx in standby mode. */
396 /* Before placing lpuart in standby, need to first determine if receiver is set to */
397 /* wake on idle and if receiver is already in idle state. Per ref manual: */
398 /* NOTE: RWU should only be set with C1[WAKE] = 0 (wakeup on idle) if the channel is */
399 /* currently not idle. */
400 /* This can be determined by the STAT[RAF] flag. If set to wake up an IDLE event and */
401 /* the channel is already idle, it is possible that the LPUART will discard data since data */
402 /* must be received (or a LIN break detect) after an IDLE is detected before IDLE is */
403 /* allowed to reasserted. */
404 lpuart_wakeup_method_t rxWakeMethod;
405 bool lpuart_current_rx_state;
407 /* see if wake is set for idle or */
408 rxWakeMethod = LPUART_HAL_GetReceiverWakeupMethod(baseAddr);
409 lpuart_current_rx_state = LPUART_HAL_GetStatusFlag(baseAddr, kLpuartRxActive);
411 /* if both rxWakeMethod is set for idle and current rx state is idle, don't put in standy */
412 if ((rxWakeMethod == kLpuartIdleLineWake) && (lpuart_current_rx_state == 0))
414 return kStatus_LPUART_RxStandbyModeError;
418 /* set the RWU bit to place receiver into standby mode */
419 BW_LPUART_CTRL_RWU(baseAddr, 1);
420 return kStatus_LPUART_Success;
424 /*FUNCTION**********************************************************************
426 * Function Name : LPUART_HAL_GetReceiverWakeupMethod
427 * Description : Gets the LPUART receiver wakeup method (idle line or addr-mark) from standby mode.
429 *END**************************************************************************/
430 lpuart_wakeup_method_t LPUART_HAL_GetReceiverWakeupMethod(uint32_t baseAddr)
432 /* get configuration of the WAKE bit for idle line wake or address mark wake */
433 if(HW_LPUART_CTRL(baseAddr).B.WAKE == 1)
435 return kLpuartAddrMarkWake;
439 return kLpuartIdleLineWake;
443 /*FUNCTION**********************************************************************
445 * Function Name : LPUART_HAL_ConfigureIdleLineDetect
446 * Description : LPUART idle-line detect operation configuration (idle line bit-count start and wake
447 * up affect on IDLE status bit).
448 * In some LPUART instances, the user should disable the transmitter/receiver
449 * before calling this function.
450 * Generally, this may be applied to all LPUARTs to ensure safe operation.
452 *END**************************************************************************/
453 void LPUART_HAL_ConfigureIdleLineDetect(uint32_t baseAddr,
454 const lpuart_idle_line_config_t *config)
456 /* Configure the idle line detection configuration as follows: */
457 /* configure the ILT to bit count after start bit or stop bit */
458 /* configure RWUID to set or not set IDLE status bit upon detection of */
459 /* an idle character when recevier in standby */
460 BW_LPUART_CTRL_ILT(baseAddr, config->idleLineType);
461 BW_LPUART_STAT_RWUID(baseAddr, config->rxWakeIdleDetect);
464 /*FUNCTION**********************************************************************
466 * Function Name : LPUART_HAL_SetMatchAddressOperation
467 * Description : LPUART configures match address mode control (Note: Feature available on
468 * select LPUART instances)
470 *END**************************************************************************/
471 lpuart_status_t LPUART_HAL_SetMatchAddressOperation( uint32_t baseAddr,
472 bool matchAddrMode1, bool matchAddrMode2,
473 uint8_t matchAddrValue1, uint8_t matchAddrValue2, lpuart_match_config_t config)
475 BW_LPUART_BAUD_MAEN1(baseAddr, matchAddrMode1); /* Match Address Mode Enable 1 */
476 BW_LPUART_BAUD_MAEN2(baseAddr, matchAddrMode2); /* Match Address Mode Enable 2 */
477 BW_LPUART_MATCH_MA1(baseAddr, matchAddrValue1); /* match address register 1 */
478 BW_LPUART_MATCH_MA2(baseAddr, matchAddrValue2); /* match address register 2 */
479 BW_LPUART_BAUD_MATCFG(baseAddr, config); /* Match Configuration */
481 return kStatus_LPUART_Success;
484 #if FSL_FEATURE_LPUART_HAS_IR_SUPPORT
485 /*FUNCTION**********************************************************************
487 * Function Name : LPUART_HAL_SetInfraredOperation
488 * Description : Configures the LPUART infrared operation.
490 *END**************************************************************************/
491 void LPUART_HAL_SetInfraredOperation(uint32_t baseAddr, bool enable,
492 lpuart_ir_tx_pulsewidth_t pulseWidth)
494 /* enable or disable infrared */
495 BW_LPUART_MODIR_IREN(baseAddr, enable);
497 /* configure the narrow pulse width of the IR pulse */
498 BW_LPUART_MODIR_TNP(baseAddr, pulseWidth);
500 #endif /* FSL_FEATURE_LPUART_HAS_IR_SUPPORT */
502 /*FUNCTION**********************************************************************
504 * Function Name : LPUART_HAL_GetStatusFlag
505 * Description : LPUART get status flag by passing flag enum.
507 *END**************************************************************************/
508 bool LPUART_HAL_GetStatusFlag(uint32_t baseAddr, lpuart_status_flag_t statusFlag)
510 uint32_t reg = (uint32_t)(statusFlag) >> LPUART_SHIFT;
515 case LPUART_BAUD_REG_ID:
516 retVal = HW_LPUART_BAUD_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
518 case LPUART_STAT_REG_ID:
519 retVal = HW_LPUART_STAT_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
521 case LPUART_CTRL_REG_ID:
522 retVal = HW_LPUART_CTRL_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
524 case LPUART_DATA_REG_ID:
525 retVal = HW_LPUART_DATA_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
527 case LPUART_MATCH_REG_ID:
528 retVal = HW_LPUART_MATCH_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
530 case LPUART_MODIR_REG_ID:
531 retVal = HW_LPUART_MODIR_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
540 /*FUNCTION**********************************************************************
542 * Function Name : LPUART_HAL_ClearStatusFlag
543 * Description : LPUART clears an individual status flag
544 * (see lpuart_status_flag_t for list of status bits).
546 *END**************************************************************************/
547 lpuart_status_t LPUART_HAL_ClearStatusFlag(uint32_t baseAddr, lpuart_status_flag_t statusFlag)
549 lpuart_status_t returnCode = kStatus_LPUART_Success;
551 /* clear the desired, individual status flag as passed in through statusFlag */
554 case kLpuartTxDataRegEmpty:
555 /* This flag is cleared automatically by other lpuart operations */
556 /* and cannot be manually cleared, return error code */
557 returnCode = kStatus_LPUART_ClearStatusFlagError;
560 case kLpuartTxComplete:
561 /* This flag is cleared automatically by other lpuart operations */
562 /* and cannot be manually cleared, return error code */
563 returnCode = kStatus_LPUART_ClearStatusFlagError;
566 case kLpuartRxDataRegFull:
567 /* This flag is cleared automatically by other lpuart operations and */
568 /* cannot be manually cleared, return error code */
569 returnCode = kStatus_LPUART_ClearStatusFlagError;
572 case kLpuartIdleLineDetect:
573 /* write one to clear status flag */
574 BW_LPUART_STAT_IDLE(baseAddr, 1);
577 case kLpuartRxOverrun:
578 /* write one to clear status flag */
579 BW_LPUART_STAT_OR(baseAddr, 1);
582 case kLpuartNoiseDetect:
583 /* write one to clear status flag */
584 BW_LPUART_STAT_NF(baseAddr, 1);
587 case kLpuartFrameErr:
588 /* write one to clear status flag */
589 BW_LPUART_STAT_FE(baseAddr, 1);
592 case kLpuartParityErr:
593 /* write one to clear status flag */
594 BW_LPUART_STAT_PF(baseAddr, 1);
597 case kLpuartLineBreakDetect:
598 /* write one to clear status flag */
599 BW_LPUART_STAT_LBKDIF(baseAddr, 1);
602 case kLpuartRxActiveEdgeDetect:
603 /* write one to clear status flag */
604 BW_LPUART_STAT_RXEDGIF(baseAddr, (1U));
607 case kLpuartRxActive:
608 /* This flag is cleared automatically by other lpuart operations and */
609 /* cannot be manually cleared, return error code */
610 returnCode = kStatus_LPUART_ClearStatusFlagError;
613 #if FSL_FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS
614 case kLpuartNoiseInCurrentWord:
615 /* This flag is not clearable, it simply reflects the status in the */
616 /* current data word and changes with each new data word */
617 returnCode = kStatus_LPUART_ClearStatusFlagError;
620 case kLpuartParityErrInCurrentWord:
621 /* This flag is not clearable, it simply reflects the status in the */
622 /* current data word and changes with each new data word */
623 returnCode = kStatus_LPUART_ClearStatusFlagError;
627 #if FSL_FEATURE_LPUART_HAS_ADDRESS_MATCHING
628 case kLpuartMatchAddrOne:
629 /* write one to clear status flag */
630 BW_LPUART_STAT_MA1F(baseAddr, 1);
632 case kLpuartMatchAddrTwo:
633 /* write one to clear status flag */
634 BW_LPUART_STAT_MA2F(baseAddr, 1);
638 default: /* catch inputs that are not recognized */
639 returnCode = kStatus_LPUART_ClearStatusFlagError;
646 /*FUNCTION**********************************************************************
648 * Function Name : LPUART_HAL_ClearAllNonAutoclearStatusFlags
649 * Description : LPUART clears ALL status flags.
651 *END**************************************************************************/
652 void LPUART_HAL_ClearAllNonAutoclearStatusFlags(uint32_t baseAddr)
654 /* clear the status flags that can be manually cleared */
655 /* note, some flags are automatically cleared and cannot be cleared automatically */
656 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartIdleLineDetect);
657 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartRxOverrun);
658 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartNoiseDetect);
659 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartFrameErr);
660 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartParityErr);
661 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartLineBreakDetect);
662 LPUART_HAL_ClearStatusFlag(baseAddr, kLpuartRxActiveEdgeDetect);
665 /*FUNCTION**********************************************************************
667 * Function Name : LPUART_HAL_Putchar9
668 * Description : Sends the LPUART 9-bit character.
670 *END**************************************************************************/
671 void LPUART_HAL_Putchar9(uint32_t baseAddr, uint16_t data)
673 uint8_t ninthDataBit;
675 ninthDataBit = (data >> 8U) & 0x1U; /* isolate the ninth data bit */
677 /* put 9-bit data to transmit */
679 /* first, write to the ninth data bit (bit position T8, where T[0:7]=8-bits, T8=9th bit) */
680 BW_LPUART_CTRL_R9T8(baseAddr, ninthDataBit);
682 /* write to the data register last since this will trigger transmit complete status flag */
683 /* also typecast to uint8_t to match register type */
684 HW_LPUART_DATA_WR(baseAddr, (uint8_t)data);
687 /*FUNCTION**********************************************************************
689 * Function Name : LPUART_HAL_Putchar10
690 * Description : Sends the LPUART 10-bit character.
692 *END**************************************************************************/
693 lpuart_status_t LPUART_HAL_Putchar10(uint32_t baseAddr, uint16_t data)
695 uint8_t ninthDataBit;
696 uint8_t tenthDataBit;
698 /* put 10-bit data to transmit */
699 ninthDataBit = (data >> 8U) & 0x1U; /* isolate the ninth data bit */
700 tenthDataBit = (data >> 9U) & 0x1U; /* isolate the tenth data bit */
702 /* first, write to the tenth data bit (bit position T9, where T[0:7]=8-bits, */
703 /* T9=10th bit, T8=9th bit) */
704 BW_LPUART_CTRL_R8T9(baseAddr, tenthDataBit);
706 /* next, write to the ninth data bit (bit position T8, where T[0:7]=8-bits, */
707 /* T9=10th bit, T8=9th bit) */
708 BW_LPUART_CTRL_R9T8(baseAddr, ninthDataBit);
710 /* write to the data register last since this will trigger transmit complete status flag */
711 /* also typecast to uint8_t to match register type */
712 HW_LPUART_DATA_WR(baseAddr, (uint8_t)data);
714 return kStatus_LPUART_Success;
717 /*FUNCTION**********************************************************************
719 * Function Name : LPUART_HAL_Getchar
720 * Description : Gets the LPUART 8-bit character.
722 *END**************************************************************************/
723 void LPUART_HAL_Getchar(uint32_t baseAddr, uint8_t *readData)
725 /* get 8-bit data from the lpuart data register */
726 *readData = (uint8_t)HW_LPUART_DATA_RD(baseAddr); /* read 8-bit data from data register */
729 /*FUNCTION**********************************************************************
731 * Function Name : LPUART_HAL_Getchar9
732 * Description : Gets the LPUART 9-bit character.
734 *END**************************************************************************/
735 void LPUART_HAL_Getchar9(uint32_t baseAddr, uint16_t *readData)
739 /* get 9-bit data from the lpuart data register */
740 /* read ninth data bit and left shift to bit position R8 before reading */
741 /* the 8 other data bits R[7:0] */
742 temp = HW_LPUART_CTRL(baseAddr).B.R8T9; /* need this two step process to work around mishra rule */
743 *readData = temp << 8;
745 /* do last: get 8-bit data from the lpuart data register, will clear certain */
746 /* receive status bits once completed */
747 /* need to OR these 8-bits with the ninth bit value above */
748 *readData |= (uint8_t)HW_LPUART_DATA_RD(baseAddr); /* read 8-bit data from data register */
751 /*FUNCTION**********************************************************************
753 * Function Name : LPUART_HAL_Getchar10
754 * Description : Gets the LPUART 10-bit character.
756 *END**************************************************************************/
757 lpuart_status_t LPUART_HAL_Getchar10(uint32_t baseAddr, uint16_t *readData)
759 /* get 10-bit data from the lpuart data register, available only on supported lpuarts */
761 /* read tenth data bit and left shift to bit position R9 before reading the 9 other */
762 /* data bits: R8 and R[7:0] */
763 *readData = (uint16_t)((uint32_t)(HW_LPUART_CTRL(baseAddr).B.R9T8) << 9U);
765 /* read ninth data bit and left shift to bit position R8 before reading the 8 other */
766 /* data bits R[7:0] */
767 *readData |= (uint16_t)((uint32_t)(HW_LPUART_CTRL(baseAddr).B.R8T9) << 8U);
769 /* do last: get 8-bit data from the lpuart data register, will clear certain receive */
770 /* status bits once completed */
771 /* need to OR these 8-bits with the ninth bit value above */
772 *readData |= HW_LPUART_DATA_RD(baseAddr); /* read 8-bit data from data register */
774 return kStatus_LPUART_Success;
777 #endif /* MBED_NO_LPUART */
779 /*******************************************************************************
781 ******************************************************************************/