]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/lpuart/fsl_lpuart_hal.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_Freescale / TARGET_KPSDK_MCUS / TARGET_KPSDK_CODE / hal / lpuart / fsl_lpuart_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_lpuart_hal.h"
31
32 #ifndef MBED_NO_LPUART
33
34 /*******************************************************************************
35  * Code
36  ******************************************************************************/
37 /*FUNCTION**********************************************************************
38  *
39  * Function Name : LPUART_HAL_Init
40  * Description   : Initializes the LPUART controller to known state.
41  *
42  *END**************************************************************************/
43 void LPUART_HAL_Init(uint32_t baseAddr)
44 {
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);    
50 }
51
52 /*FUNCTION**********************************************************************
53  *
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.
59  *
60  *END**************************************************************************/
61 lpuart_status_t LPUART_HAL_SetBaudRate(uint32_t baseAddr, uint32_t sourceClockInHz,
62                                 uint32_t desiredBaudRate)
63 {
64     uint16_t sbr, sbrTemp, i;
65     uint32_t osr, tempDiff, calculatedBaud, baudDiff;
66
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) */
71     osr = 4;
72     sbr = (sourceClockInHz/(desiredBaudRate * osr));
73     calculatedBaud = (sourceClockInHz / (osr * sbr));
74
75     if (calculatedBaud > desiredBaudRate)
76     {
77         baudDiff = calculatedBaud - desiredBaudRate;
78     }
79     else
80     {
81         baudDiff = desiredBaudRate - calculatedBaud;
82     }
83
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++)
87     {
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));
92
93         if (calculatedBaud > desiredBaudRate)
94         {
95             tempDiff = calculatedBaud - desiredBaudRate;
96         }
97         else
98         {
99             tempDiff = desiredBaudRate - calculatedBaud;
100         }
101
102         if (tempDiff <= baudDiff)
103         {
104             baudDiff = tempDiff;
105             osr = i;  /* update and store the best osr value calculated */
106             sbr = sbrTemp;  /* update store the best sbr value calculated */
107         }
108     }
109
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))
113     {
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))
118         {
119             BW_LPUART_BAUD_BOTHEDGE(baseAddr, 1);
120         }
121
122         /* program the osr value (bit value is one less than actual value) */
123         BW_LPUART_BAUD_OSR(baseAddr, (osr-1));
124
125         /* write the sbr value to the BAUD registers */
126         BW_LPUART_BAUD_SBR(baseAddr, sbr);
127     }
128     else
129     {
130         /* Unacceptable baud rate difference of more than 3% */
131         return kStatus_LPUART_BaudRatePercentDiffExceeded;
132     }
133
134     return kStatus_LPUART_Success;
135 }
136
137 /*FUNCTION**********************************************************************
138  *
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.
144  *
145  *END**************************************************************************/
146 void LPUART_HAL_SetBitCountPerChar(uint32_t baseAddr, lpuart_bit_count_per_char_t bitCountPerChar)
147 {
148     if(bitCountPerChar == kLpuart10BitsPerChar)
149     {
150         BW_LPUART_BAUD_M10(baseAddr, 1); /* set M10 for 10-bit mode, M bit in C1 is don't care */
151     }
152     else
153     {
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 */
156     }
157 }
158
159 /*FUNCTION**********************************************************************
160  *
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.
166  *
167  *END**************************************************************************/
168 void LPUART_HAL_SetParityMode(uint32_t baseAddr, lpuart_parity_mode_t parityModeType)
169 {
170     /* configure the parity enable/type    */
171
172     if ((parityModeType) == kLpuartParityDisabled)
173     {
174         /* parity disabled, hence parity type is don't care */
175         BW_LPUART_CTRL_PE(baseAddr, 0);
176     }
177     else
178     {
179         /* parity enabled */
180         BW_LPUART_CTRL_PE(baseAddr, 1);
181         /* parity odd/even depending on parity mode setting */
182         BW_LPUART_CTRL_PT(baseAddr, (parityModeType) & 0x1);
183     }
184
185 }
186
187 /*FUNCTION**********************************************************************
188  *
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.
192  *
193  *END**************************************************************************/
194 void LPUART_HAL_SetTxRxInversionCmd(uint32_t baseAddr, uint32_t rxInvert, uint32_t txInvert)
195 {
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);
200 }
201
202 /*FUNCTION**********************************************************************
203  *
204  * Function Name : LPUART_HAL_EnableTransmitter
205  * Description   : Enables the LPUART transmitter.
206  *
207  *END**************************************************************************/
208 void LPUART_HAL_EnableTransmitter(uint32_t baseAddr)
209 {
210     /* enable the transmitter based on the lpuart baseAddr */
211
212     /* for this lpuart baseAddr, there is a two step process to clear the transmit complete */
213     /* status flag: */
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 */
217
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);
222 }
223
224 /*FUNCTION**********************************************************************
225  *
226  * Function Name : LPUART_HAL_SetIntMode
227  * Description   : Configures the LPUART module interrupts to enable/disable various interrupt sources.
228  *
229  *END**************************************************************************/
230 void LPUART_HAL_SetIntMode(uint32_t baseAddr, lpuart_interrupt_t interrupt, bool enable)
231 {
232     uint32_t reg = (uint32_t)(interrupt) >> LPUART_SHIFT;
233     uint32_t temp = 1U << (uint32_t)interrupt;
234
235     switch ( reg )
236     {
237         case LPUART_BAUD_REG_ID:
238             enable ? HW_LPUART_BAUD_SET(baseAddr, temp) : HW_LPUART_BAUD_CLR(baseAddr, temp);
239             break;
240         case LPUART_STAT_REG_ID:
241             enable ? HW_LPUART_STAT_SET(baseAddr, temp) : HW_LPUART_STAT_CLR(baseAddr, temp);
242             break;
243         case LPUART_CTRL_REG_ID:
244             enable ? HW_LPUART_CTRL_SET(baseAddr, temp) : HW_LPUART_CTRL_CLR(baseAddr, temp);
245             break;
246         case LPUART_DATA_REG_ID:
247             enable ? HW_LPUART_DATA_SET(baseAddr, temp) : HW_LPUART_DATA_CLR(baseAddr, temp);
248             break;
249         case LPUART_MATCH_REG_ID:
250             enable ? HW_LPUART_MATCH_SET(baseAddr, temp) : HW_LPUART_MATCH_CLR(baseAddr, temp);
251             break;
252         case LPUART_MODIR_REG_ID:
253             enable ? HW_LPUART_MODIR_SET(baseAddr, temp) : HW_LPUART_MODIR_CLR(baseAddr, temp);
254             break;
255         default :
256             break;
257     }
258 }
259
260 /*FUNCTION**********************************************************************
261  *
262  * Function Name : LPUART_HAL_GetIntMode
263  * Description   : Returns whether the LPUART module interrupts is enabled/disabled.
264  *
265  *END**************************************************************************/
266 bool LPUART_HAL_GetIntMode(uint32_t baseAddr, lpuart_interrupt_t interrupt)
267 {
268     uint32_t reg = (uint32_t)(interrupt) >> LPUART_SHIFT;
269           bool retVal = false;
270
271     switch ( reg )
272     {
273         case LPUART_BAUD_REG_ID:
274             retVal = HW_LPUART_BAUD_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
275             break;
276         case LPUART_STAT_REG_ID:
277             retVal = HW_LPUART_STAT_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
278             break;
279         case LPUART_CTRL_REG_ID:
280             retVal = HW_LPUART_CTRL_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
281             break;
282         case LPUART_DATA_REG_ID:
283             retVal = HW_LPUART_DATA_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
284             break;
285         case LPUART_MATCH_REG_ID:
286             retVal = HW_LPUART_MATCH_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
287             break;
288         case LPUART_MODIR_REG_ID:
289             retVal = HW_LPUART_MODIR_RD(baseAddr) >> (uint32_t)(interrupt) & 1U;
290             break;
291         default :
292             break;
293     }
294
295     return retVal;
296 }
297
298 #if FSL_FEATURE_LPUART_HAS_DMA_ENABLE 
299 /*FUNCTION**********************************************************************
300  *
301  * Function Name : LPUART_HAL_ConfigureDma
302  * Description   : LPUART configures DMA requests for Transmitter and Receiver.
303  *
304  *END**************************************************************************/
305 void LPUART_HAL_ConfigureDma(uint32_t baseAddr, bool txDmaConfig, bool  rxDmaConfig)
306 {
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  */
313 }
314 #endif
315
316 /*FUNCTION**********************************************************************
317  *
318  * Function Name : LPUART_HAL_GetWaitModeOperationConfig
319  * Description   : LPUART configures DMA requests for Transmitter and Receiver.
320  *
321  *END**************************************************************************/
322 lpuart_operation_config_t LPUART_HAL_GetWaitModeOperationConfig(uint32_t baseAddr)
323 {
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)
327     {
328          return kLpuartOperates;
329     }
330     else
331     {
332          return kLpuartStops;
333     }
334
335 }
336
337 /*FUNCTION**********************************************************************
338  *
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.
344  *
345  *END**************************************************************************/
346 void LPUART_HAL_SedLoopbackCmd(uint32_t baseAddr, bool enable)
347 {
348     /* configure lpuart to enable/disable operation in loopback mode */
349
350     /* configure LOOPS bit to enable(1)/disable(0) loopback mode, but also need to clear RSRC */
351     BW_LPUART_CTRL_LOOPS(baseAddr, enable);
352
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);
357
358 }
359
360 /*FUNCTION**********************************************************************
361  *
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.
367  *
368  *END**************************************************************************/
369 void LPUART_HAL_SetSingleWireCmd(uint32_t baseAddr, bool enable)
370 {
371     /* configure lpuart to enable/disable operation in single mode */
372
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);
376 }
377
378 /*FUNCTION**********************************************************************
379  *
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
386  * not idle.
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.
390  *
391  *END**************************************************************************/
392 lpuart_status_t LPUART_HAL_PutReceiverInStandbyMode(uint32_t baseAddr)
393 {
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;
406
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);
410
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))
413     {
414         return kStatus_LPUART_RxStandbyModeError;
415     }
416     else
417     {
418         /* set the RWU bit to place receiver into standby mode */
419         BW_LPUART_CTRL_RWU(baseAddr, 1);
420         return kStatus_LPUART_Success;
421     }
422 }
423
424 /*FUNCTION**********************************************************************
425  *
426  * Function Name : LPUART_HAL_GetReceiverWakeupMethod
427  * Description   : Gets the LPUART receiver wakeup method (idle line or addr-mark) from standby mode.
428  *
429  *END**************************************************************************/
430 lpuart_wakeup_method_t LPUART_HAL_GetReceiverWakeupMethod(uint32_t baseAddr)
431 {
432     /* get configuration of the WAKE bit for idle line wake or address mark wake */
433     if(HW_LPUART_CTRL(baseAddr).B.WAKE == 1)
434     {
435         return kLpuartAddrMarkWake;
436     }
437     else
438     {
439         return kLpuartIdleLineWake;
440     }
441 }
442
443 /*FUNCTION**********************************************************************
444  *
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.
451  *
452  *END**************************************************************************/
453 void LPUART_HAL_ConfigureIdleLineDetect(uint32_t baseAddr,
454                                          const lpuart_idle_line_config_t *config)
455 {
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);
462 }
463
464 /*FUNCTION**********************************************************************
465  *
466  * Function Name : LPUART_HAL_SetMatchAddressOperation
467  * Description   : LPUART configures match address mode control (Note: Feature available on
468  *                 select LPUART instances)
469  *
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)
474 {
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 */
480
481     return kStatus_LPUART_Success;
482 }
483
484 #if FSL_FEATURE_LPUART_HAS_IR_SUPPORT
485 /*FUNCTION**********************************************************************
486  *
487  * Function Name : LPUART_HAL_SetInfraredOperation
488  * Description   : Configures the LPUART infrared operation.
489  *
490  *END**************************************************************************/
491 void LPUART_HAL_SetInfraredOperation(uint32_t baseAddr, bool enable,
492                                            lpuart_ir_tx_pulsewidth_t pulseWidth)
493 {
494     /* enable or disable infrared */
495     BW_LPUART_MODIR_IREN(baseAddr, enable);
496
497     /* configure the narrow pulse width of the IR pulse */
498     BW_LPUART_MODIR_TNP(baseAddr, pulseWidth);
499 }
500 #endif  /* FSL_FEATURE_LPUART_HAS_IR_SUPPORT */
501
502 /*FUNCTION**********************************************************************
503  *
504  * Function Name : LPUART_HAL_GetStatusFlag
505  * Description   : LPUART get status flag by passing flag enum.
506  *
507  *END**************************************************************************/
508 bool LPUART_HAL_GetStatusFlag(uint32_t baseAddr, lpuart_status_flag_t statusFlag)
509 {
510     uint32_t reg = (uint32_t)(statusFlag) >> LPUART_SHIFT;
511           bool retVal = false;
512
513     switch ( reg )
514     {
515         case LPUART_BAUD_REG_ID:
516             retVal = HW_LPUART_BAUD_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
517             break;
518         case LPUART_STAT_REG_ID:
519             retVal = HW_LPUART_STAT_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
520             break;
521         case LPUART_CTRL_REG_ID:
522             retVal = HW_LPUART_CTRL_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
523             break;
524         case LPUART_DATA_REG_ID:
525             retVal = HW_LPUART_DATA_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
526             break;
527         case LPUART_MATCH_REG_ID:
528             retVal = HW_LPUART_MATCH_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
529             break;
530         case LPUART_MODIR_REG_ID:
531             retVal = HW_LPUART_MODIR_RD(baseAddr) >> (uint32_t)(statusFlag) & 1U;
532             break;
533         default:
534             break;
535     }
536
537     return retVal;
538 }
539
540 /*FUNCTION**********************************************************************
541  *
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).
545  *
546  *END**************************************************************************/
547 lpuart_status_t LPUART_HAL_ClearStatusFlag(uint32_t baseAddr, lpuart_status_flag_t statusFlag)
548 {
549     lpuart_status_t returnCode = kStatus_LPUART_Success;
550
551     /* clear the desired, individual status flag as passed in through statusFlag  */
552     switch(statusFlag)
553     {
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;
558             break;
559
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;
564             break;
565
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;
570             break;
571
572         case kLpuartIdleLineDetect:
573             /* write one to clear status flag */
574             BW_LPUART_STAT_IDLE(baseAddr, 1);
575             break;
576
577         case kLpuartRxOverrun:
578             /* write one to clear status flag */
579             BW_LPUART_STAT_OR(baseAddr, 1);
580             break;
581
582         case kLpuartNoiseDetect:
583             /* write one to clear status flag */
584             BW_LPUART_STAT_NF(baseAddr, 1);
585             break;
586
587         case kLpuartFrameErr:
588             /* write one to clear status flag */
589             BW_LPUART_STAT_FE(baseAddr, 1);
590             break;
591
592         case kLpuartParityErr:
593             /* write one to clear status flag */
594             BW_LPUART_STAT_PF(baseAddr, 1);
595             break;
596
597         case kLpuartLineBreakDetect:
598             /* write one to clear status flag */
599             BW_LPUART_STAT_LBKDIF(baseAddr, 1);
600             break;
601
602         case kLpuartRxActiveEdgeDetect:
603             /* write one to clear status flag */
604             BW_LPUART_STAT_RXEDGIF(baseAddr, (1U));
605             break;
606
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;
611             break;
612
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;
618             break;
619
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;
624             break;
625 #endif
626
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);
631             break;
632         case kLpuartMatchAddrTwo:
633             /* write one to clear status flag */
634             BW_LPUART_STAT_MA2F(baseAddr, 1);
635             break;
636 #endif
637
638         default:  /* catch inputs that are not recognized */
639             returnCode = kStatus_LPUART_ClearStatusFlagError;
640             break;
641     }
642
643     return (returnCode);
644 }
645
646 /*FUNCTION**********************************************************************
647  *
648  * Function Name : LPUART_HAL_ClearAllNonAutoclearStatusFlags
649  * Description   : LPUART clears ALL status flags.
650  *
651  *END**************************************************************************/
652 void LPUART_HAL_ClearAllNonAutoclearStatusFlags(uint32_t baseAddr)
653 {
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);
663 }
664
665 /*FUNCTION**********************************************************************
666  *
667  * Function Name : LPUART_HAL_Putchar9
668  * Description   : Sends the LPUART 9-bit character.
669  *
670  *END**************************************************************************/
671 void LPUART_HAL_Putchar9(uint32_t baseAddr, uint16_t data)
672 {
673     uint8_t ninthDataBit;
674
675     ninthDataBit = (data >> 8U) & 0x1U;  /* isolate the ninth data bit */
676
677     /* put 9-bit data to transmit */
678
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);
681
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);
685 }
686
687 /*FUNCTION**********************************************************************
688  *
689  * Function Name : LPUART_HAL_Putchar10
690  * Description   : Sends the LPUART 10-bit character.
691  *
692  *END**************************************************************************/
693 lpuart_status_t LPUART_HAL_Putchar10(uint32_t baseAddr, uint16_t data)
694 {
695     uint8_t ninthDataBit;
696     uint8_t tenthDataBit;
697
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 */
701
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);
705
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);
709
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);
713
714     return kStatus_LPUART_Success;
715 }
716
717 /*FUNCTION**********************************************************************
718  *
719  * Function Name : LPUART_HAL_Getchar
720  * Description   : Gets the LPUART 8-bit character.
721  *
722  *END**************************************************************************/
723 void LPUART_HAL_Getchar(uint32_t baseAddr, uint8_t *readData)
724 {
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 */
727 }
728
729 /*FUNCTION**********************************************************************
730  *
731  * Function Name : LPUART_HAL_Getchar9
732  * Description   : Gets the LPUART 9-bit character.
733  *
734  *END**************************************************************************/
735 void  LPUART_HAL_Getchar9(uint32_t baseAddr, uint16_t *readData)
736 {
737     uint16_t temp;
738
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;
744
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  */
749 }
750
751 /*FUNCTION**********************************************************************
752  *
753  * Function Name : LPUART_HAL_Getchar10
754  * Description   : Gets the LPUART 10-bit character.
755  *
756  *END**************************************************************************/
757 lpuart_status_t LPUART_HAL_Getchar10(uint32_t baseAddr, uint16_t *readData)
758 {
759     /* get 10-bit data from the lpuart data register, available only on supported lpuarts */
760
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);
764
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);
768
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 */
773
774     return kStatus_LPUART_Success;
775 }
776
777 #endif /* MBED_NO_LPUART */
778
779 /*******************************************************************************
780  * EOF
781  ******************************************************************************/
782