S32 SDK
lpuart_hal.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
7  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
9  * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
10  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
11  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
12  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
14  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
15  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
16  * THE POSSIBILITY OF SUCH DAMAGE.
17  */
18 
48 #include "lpuart_hal.h"
49 
50 
51 /*******************************************************************************
52  * Code
53  ******************************************************************************/
54 /*FUNCTION**********************************************************************
55  *
56  * Function Name : LPUART_HAL_Init
57  * Description : Initializes the LPUART controller to known state, using
58  * register reset values defined in the reference manual.
59  *
60  * Implements : LPUART_HAL_Init_Activity
61  *END**************************************************************************/
63 {
64  /* Set the default oversampling ratio (16) and baud-rate divider (4) */
65  base->BAUD = ((uint32_t)((FEATURE_LPUART_DEFAULT_OSR << LPUART_BAUD_OSR_SHIFT) | \
67  /* Clear the error/interrupt flags */
69  /* Reset all features/interrupts by default */
70  base->CTRL = 0x00000000;
71  /* Reset match addresses */
72  base->MATCH = 0x00000000;
73 #if FEATURE_LPUART_HAS_MODEM_SUPPORT
74  /* Reset IrDA modem features */
75  base->MODIR = 0x00000000;
76 #endif
77 #if FEATURE_LPUART_FIFO_SIZE > 0U
78  /* Reset FIFO feature */
80  /* Reset FIFO Watermark values */
81  base->WATER = 0x00000000;
82 #endif
83 }
84 
85 /*FUNCTION**********************************************************************
86  *
87  * Function Name : LPUART_HAL_SetBaudRate
88  * Description : Configures the LPUART baud rate.
89  * In some LPUART instances the user must disable the transmitter/receiver
90  * before calling this function.
91  * Generally, this may be applied to all LPUARTs to ensure safe operation.
92  *
93  * Implements : LPUART_HAL_SetBaudRate_Activity
94  *END**************************************************************************/
96  uint32_t sourceClockInHz,
97  uint32_t desiredBaudRate)
98 {
99  uint16_t sbr, sbrTemp, i;
100  uint32_t osr, tempDiff, calculatedBaud, baudDiff, baudRegValTemp;
101 
102  /* This lpuart instantiation uses a slightly different baud rate calculation
103  * The idea is to use the best OSR (over-sampling rate) possible
104  * Note, osr is typically hard-set to 16 in other lpuart instantiations
105  * First calculate the baud rate using the minimum OSR possible (4) */
106  osr = 4;
107  sbr = (uint16_t)(sourceClockInHz / (desiredBaudRate * osr));
108  calculatedBaud = (sourceClockInHz / (osr * sbr));
109 
110  if (calculatedBaud > desiredBaudRate)
111  {
112  baudDiff = calculatedBaud - desiredBaudRate;
113  }
114  else
115  {
116  baudDiff = desiredBaudRate - calculatedBaud;
117  }
118 
119  /* loop to find the best osr value possible, one that generates minimum baudDiff
120  * iterate through the rest of the supported values of osr */
121  for (i = 5U; i <= 32U; i++)
122  {
123  /* calculate the temporary sbr value */
124  sbrTemp = (uint16_t)(sourceClockInHz / (desiredBaudRate * i));
125  /* calculate the baud rate based on the temporary osr and sbr values */
126  calculatedBaud = (sourceClockInHz / (i * sbrTemp));
127 
128  if (calculatedBaud > desiredBaudRate)
129  {
130  tempDiff = calculatedBaud - desiredBaudRate;
131  }
132  else
133  {
134  tempDiff = desiredBaudRate - calculatedBaud;
135  }
136 
137  if (tempDiff <= baudDiff)
138  {
139  baudDiff = tempDiff;
140  osr = i; /* update and store the best osr value calculated */
141  sbr = sbrTemp; /* update store the best sbr value calculated */
142  }
143  }
144 
145  /* Check to see if actual baud rate is within 3% of desired baud rate
146  * based on the best calculate osr value */
147  if (baudDiff < ((desiredBaudRate / 100U) * 3U))
148  {
149  /* Acceptable baud rate, check if osr is between 4x and 7x oversampling.
150  * If so, then "BOTHEDGE" sampling must be turned on */
151  if ((osr > 3U) && (osr < 8U))
152  {
153  base->BAUD = (base->BAUD & ~LPUART_BAUD_BOTHEDGE_MASK) | ((uint32_t)1U << LPUART_BAUD_BOTHEDGE_SHIFT);
154  }
155 
156  /* program the osr value (bit value is one less than actual value) */
157  baudRegValTemp = base->BAUD;
158  baudRegValTemp &= ~(LPUART_BAUD_OSR_MASK);
159  baudRegValTemp |= LPUART_BAUD_OSR(osr - 1U);
160  base->BAUD = baudRegValTemp;
161 
162  /* write the sbr value to the BAUD registers */
163  baudRegValTemp = base->BAUD;
164  baudRegValTemp &= ~(LPUART_BAUD_SBR_MASK);
165  baudRegValTemp |= LPUART_BAUD_SBR(sbr);
166  base->BAUD = baudRegValTemp;
167  }
168  else
169  {
170  /* Unacceptable baud rate difference of more than 3% */
171  return STATUS_ERROR;
172  }
173 
174  return STATUS_SUCCESS;
175 }
176 
177 /*FUNCTION**********************************************************************
178  *
179  * Function Name : LPUART_HAL_SetBitCountPerChar
180  * Description : Configures the number of bits per char in LPUART controller.
181  * In some LPUART instances, the user should disable the transmitter/receiver
182  * before calling this function.
183  * Generally, this may be applied to all LPUARTs to ensure safe operation.
184  *
185  * Implements : LPUART_HAL_SetBitCountPerChar_Activity
186  *END**************************************************************************/
188  lpuart_bit_count_per_char_t bitCountPerChar)
189 {
190  if (bitCountPerChar == LPUART_10_BITS_PER_CHAR)
191  {
192  base->BAUD = (base->BAUD & ~LPUART_BAUD_M10_MASK) | ((uint32_t)1U << LPUART_BAUD_M10_SHIFT);
193  }
194  else
195  {
196  /* config 8-bit (M=0) or 9-bits (M=1) */
197  base->CTRL = (base->CTRL & ~LPUART_CTRL_M_MASK) | ((uint32_t)bitCountPerChar << LPUART_CTRL_M_SHIFT);
198  /* clear M10 to make sure not 10-bit mode */
199  base->BAUD &= ~LPUART_BAUD_M10_MASK;
200  }
201 }
202 
203 /*FUNCTION**********************************************************************
204  *
205  * Function Name : LPUART_HAL_SetParityMode
206  * Description : Configures parity mode in the LPUART controller.
207  * In some LPUART instances, the user should disable the transmitter/receiver
208  * before calling this function.
209  * Generally, this may be applied to all LPUARTs to ensure safe operation.
210  *
211  * Implements : LPUART_HAL_SetParityMode_Activity
212  *END**************************************************************************/
214 {
215  base->CTRL = (base->CTRL & ~LPUART_CTRL_PE_MASK) | (((uint32_t)parityModeType >> 1U) << LPUART_CTRL_PE_SHIFT);
216  base->CTRL = (base->CTRL & ~LPUART_CTRL_PT_MASK) | (((uint32_t)parityModeType & 1U) << LPUART_CTRL_PT_SHIFT);
217 }
218 
219 /*FUNCTION**********************************************************************
220  *
221  * Function Name : LPUART_HAL_Putchar9
222  * Description : Sends the LPUART 9-bit character.
223  *
224  * Implements : LPUART_HAL_Putchar9_Activity
225  *END**************************************************************************/
226 void LPUART_HAL_Putchar9(LPUART_Type * base, uint16_t data)
227 {
228  uint8_t ninthDataBit;
229  volatile uint8_t * dataRegBytes = (volatile uint8_t *)(&(base->DATA));
230 
231 
232  ninthDataBit = (uint8_t)((data >> 8U) & 0x1U);
233 
234  /* write to ninth data bit T8(where T[0:7]=8-bits, T8=9th bit) */
235  base->CTRL = (base->CTRL & ~LPUART_CTRL_R9T8_MASK) | ((uint32_t)(ninthDataBit) << LPUART_CTRL_R9T8_SHIFT);
236 
237  /* write 8-bits to the data register*/
238  dataRegBytes[0] = (uint8_t)data;
239 }
240 
241 /*FUNCTION**********************************************************************
242  *
243  * Function Name : LPUART_HAL_Putchar10
244  * Description : Sends the LPUART 10-bit character.
245  *
246  * Implements : LPUART_HAL_Putchar10_Activity
247  *END**************************************************************************/
248 void LPUART_HAL_Putchar10(LPUART_Type * base, uint16_t data)
249 {
250  uint8_t ninthDataBit, tenthDataBit;
251  uint32_t ctrlRegVal;
252  volatile uint8_t * dataRegBytes = (volatile uint8_t *)(&(base->DATA));
253 
254  ninthDataBit = (uint8_t)((data >> 8U) & 0x1U);
255  tenthDataBit = (uint8_t)((data >> 9U) & 0x1U);
256 
257  /* write to ninth/tenth data bit (T[0:7]=8-bits, T8=9th bit, T9=10th bit) */
258  ctrlRegVal = base->CTRL;
259  ctrlRegVal = (ctrlRegVal & ~LPUART_CTRL_R9T8_MASK) | ((uint32_t)ninthDataBit << LPUART_CTRL_R9T8_SHIFT);
260  ctrlRegVal = (ctrlRegVal & ~LPUART_CTRL_R8T9_MASK) | ((uint32_t)tenthDataBit << LPUART_CTRL_R8T9_SHIFT);
261  base->CTRL = ctrlRegVal;
262 
263  /* write to 8-bits to the data register */
264  dataRegBytes[0] = (uint8_t)data;
265 }
266 
267 /*FUNCTION**********************************************************************
268  *
269  * Function Name : LPUART_HAL_Getchar9
270  * Description : Gets the LPUART 9-bit character.
271  *
272  * Implements : LPUART_HAL_Getchar9_Activity
273  *END**************************************************************************/
274 void LPUART_HAL_Getchar9(const LPUART_Type * base, uint16_t *readData)
275 {
276  DEV_ASSERT(readData != NULL);
277 
278  /* get ninth bit from lpuart data register */
279  *readData = (uint16_t)(((base->CTRL >> LPUART_CTRL_R8T9_SHIFT) & 1U) << 8);
280 
281  /* get 8-bit data from the lpuart data register */
282  *readData |= (uint8_t)base->DATA;
283 }
284 
285 /*FUNCTION**********************************************************************
286  *
287  * Function Name : LPUART_HAL_Getchar10
288  * Description : Gets the LPUART 10-bit character, available only on
289  * supported lpuarts
290  *
291  * Implements : LPUART_HAL_Getchar10_Activity
292  *END**************************************************************************/
293 void LPUART_HAL_Getchar10(const LPUART_Type * base, uint16_t *readData)
294 {
295  DEV_ASSERT(readData != NULL);
296 
297  /* read tenth data bit */
298  *readData = (uint16_t)(((base->CTRL >> LPUART_CTRL_R9T8_SHIFT) & 1U) << 9);
299  /* read ninth data bit */
300  *readData |= (uint16_t)(((base->CTRL >> LPUART_CTRL_R8T9_SHIFT) & 1U) << 8);
301 
302  /* get 8-bit data */
303  *readData |= (uint8_t)base->DATA;
304 }
305 
306 /*FUNCTION**********************************************************************
307  *
308  * Function Name : LPUART_HAL_SendDataPolling
309  * Description : Send out multiple bytes of data using polling method.
310  * This function only supports 8-bit transaction.
311  *
312  * Implements : LPUART_HAL_SendDataPolling_Activity
313  *END**************************************************************************/
315  const uint8_t *txBuff,
316  uint32_t txSize)
317 {
318  DEV_ASSERT(txBuff != NULL);
319 
320  while (txSize-- > 0U)
321  {
322  while (((base->STAT >> LPUART_STAT_TDRE_SHIFT) & 1U) == 0U)
323  {}
324 
325  LPUART_HAL_Putchar(base, *txBuff++);
326  }
327 }
328 
329 /*FUNCTION**********************************************************************
330  *
331  * Function Name : LPUART_HAL_ReceiveDataPolling
332  * Description : Receive multiple bytes of data using polling method.
333  * This function only supports 8-bit transaction.
334  *
335  * Implements : LPUART_HAL_ReceiveDataPolling_Activity
336  *END**************************************************************************/
338  uint8_t *rxBuff,
339  uint32_t rxSize)
340 {
341  DEV_ASSERT(rxBuff != NULL);
342 
343  status_t retVal = STATUS_SUCCESS;
344 
345  while (rxSize-- > 0U)
346  {
347  while (((base->STAT >> LPUART_STAT_RDRF_SHIFT) & 1U) == 0U)
348  {}
349 
350  LPUART_HAL_Getchar(base, rxBuff++);
351 
352  /* Clear the Overrun flag since it will block receiving */
353  if (((base->STAT >> LPUART_STAT_OR_SHIFT) & 1U) > 0U)
354  {
356  retVal = STATUS_UART_RX_OVERRUN;
357  }
358  }
359 
360  return retVal;
361 }
362 
363 /*FUNCTION**********************************************************************
364  *
365  * Function Name : LPUART_HAL_SetIntMode
366  * Description : Configures the LPUART module interrupts to enable/disable
367  * various interrupt sources.
368  *
369  * Implements : LPUART_HAL_SetIntMode_Activity
370  *END**************************************************************************/
371 void LPUART_HAL_SetIntMode(LPUART_Type * base, lpuart_interrupt_t intSrc, bool enable)
372 {
373  uint32_t reg = (uint32_t)(intSrc) >> LPUART_SHIFT;
374  uint32_t intRegOffset = (uint16_t)(intSrc);
375 
376  switch (reg)
377  {
378  case LPUART_BAUD_REG_ID:
379  base->BAUD = (base->BAUD & ~(1UL << intRegOffset)) | ((enable ? 1U : 0U) << intRegOffset);
380  break;
381  case LPUART_STAT_REG_ID:
382  base->STAT = (base->STAT & (~FEATURE_LPUART_STAT_REG_FLAGS_MASK & ~(1UL << intRegOffset))) | \
383  ((enable ? 1U : 0U) << intRegOffset);
384  break;
385  case LPUART_CTRL_REG_ID:
386  base->CTRL = (base->CTRL & ~(1UL << intRegOffset)) | ((enable ? 1U : 0U) << intRegOffset);
387  break;
388  case LPUART_DATA_REG_ID:
389  base->DATA = (base->DATA & ~(1UL << intRegOffset)) | ((enable ? 1U : 0U) << intRegOffset);
390  break;
391  case LPUART_MATCH_REG_ID:
392  base->MATCH = (base->MATCH & ~(1UL << intRegOffset)) | ((enable ? 1U : 0U) << intRegOffset);
393  break;
394 #if FEATURE_LPUART_HAS_MODEM_SUPPORT
395  case LPUART_MODIR_REG_ID:
396  base->MODIR = (base->MODIR & ~(1UL << intRegOffset)) | ((enable ? 1U : 0U) << intRegOffset);
397  break;
398 #endif
399 #if FEATURE_LPUART_FIFO_SIZE > 0U
400  case LPUART_FIFO_REG_ID:
401  base->FIFO = (base->FIFO & (~FEATURE_LPUART_FIFO_REG_FLAGS_MASK & ~(1UL << intRegOffset))) | \
402  ((enable ? 1U : 0U) << intRegOffset);
403  break;
404 #endif
405  default :
406  /* Invalid parameter: return */
407  break;
408  }
409 }
410 
411 /*FUNCTION**********************************************************************
412  *
413  * Function Name : LPUART_HAL_GetIntMode
414  * Description : Returns whether LPUART module interrupt is enabled/disabled.
415  *
416  * Implements : LPUART_HAL_GetIntMode_Activity
417  *END**************************************************************************/
419 {
420  uint32_t reg = (uint32_t)(intSrc) >> LPUART_SHIFT;
421  bool retVal = false;
422 
423  switch ( reg )
424  {
425  case LPUART_BAUD_REG_ID:
426  retVal = (((base->BAUD >> (uint16_t)(intSrc)) & 1U) > 0U);
427  break;
428  case LPUART_STAT_REG_ID:
429  retVal = (((base->STAT >> (uint16_t)(intSrc)) & 1U) > 0U);
430  break;
431  case LPUART_CTRL_REG_ID:
432  retVal = (((base->CTRL >> (uint16_t)(intSrc)) & 1U) > 0U);
433  break;
434  case LPUART_DATA_REG_ID:
435  retVal = (((base->DATA >> (uint16_t)(intSrc)) & 1U) > 0U);
436  break;
437  case LPUART_MATCH_REG_ID:
438  retVal = (((base->MATCH >> (uint16_t)(intSrc)) & 1U) > 0U);
439  break;
440 #if FEATURE_LPUART_HAS_MODEM_SUPPORT
441  case LPUART_MODIR_REG_ID:
442  retVal = (((base->MODIR >> (uint16_t)(intSrc)) & 1U) > 0U);
443  break;
444 #endif
445 #if FEATURE_LPUART_FIFO_SIZE > 0U
446  case LPUART_FIFO_REG_ID:
447  retVal = (((base->FIFO >> (uint16_t)(intSrc)) & 1U) > 0U);
448  break;
449 #endif
450  default :
451  /* Invalid parameter: return */
452  break;
453  }
454 
455  return retVal;
456 }
457 
458 /*FUNCTION**********************************************************************
459  *
460  * Function Name : LPUART_HAL_SetLoopbackCmd
461  * Description : Configures the LPUART loopback operation (enable/disable
462  * loopback operation)
463  * In some LPUART instances, the user should disable the transmitter/receiver
464  * before calling this function.
465  * Generally, this may be applied to all LPUARTs to ensure safe operation.
466  *
467  * Implements : LPUART_HAL_SetLoopbackCmd_Activity
468  *END**************************************************************************/
469 void LPUART_HAL_SetLoopbackCmd(LPUART_Type * base, bool enable)
470 {
471  /* configure LOOPS bit to enable(1)/disable(0) loopback mode, but also need
472  * to clear RSRC */
473  base->CTRL = (base->CTRL & ~LPUART_CTRL_LOOPS_MASK) | ((enable ? 1U : 0U) << LPUART_CTRL_LOOPS_SHIFT);
474 
475  /* clear RSRC for loopback mode, and if loopback disabled, */
476  /* this bit has no meaning but clear anyway */
477  /* to set it back to default value */
478  base->CTRL &= ~LPUART_CTRL_RSRC_MASK;
479 }
480 
481 /*FUNCTION**********************************************************************
482  *
483  * Function Name : LPUART_HAL_SetSingleWireCmd
484  * Description : Configures the LPUART single-wire operation (enable/disable
485  * single-wire mode)
486  * In some LPUART instances, the user should disable the transmitter/receiver
487  * before calling this function.
488  * Generally, this may be applied to all LPUARTs to ensure safe operation.
489  *
490  * Implements : LPUART_HAL_SetSingleWireCmd_Activity
491  *END**************************************************************************/
492 void LPUART_HAL_SetSingleWireCmd(LPUART_Type * base, bool enable)
493 {
494  /* to enable single-wire mode, need both LOOPS and RSRC set,
495  * to enable or clear both */
496  base->CTRL = (base->CTRL & ~LPUART_CTRL_LOOPS_MASK) | ((enable ? 1U : 0U) << LPUART_CTRL_LOOPS_SHIFT);
497  base->CTRL = (base->CTRL & ~LPUART_CTRL_RSRC_MASK) | ((enable ? 1U : 0U) << LPUART_CTRL_RSRC_SHIFT);
498 }
499 
500 /*FUNCTION**********************************************************************
501  *
502  * Function Name : LPUART_HAL_SetReceiverInStandbyMode
503  * Description : Places the LPUART receiver in standby mode.
504  * In some LPUART instances, before placing LPUART in standby mode, first
505  * determine whether the receiver is set to wake on idle or whether it is
506  * already in idle state.
507  *
508  * Implements : LPUART_HAL_SetReceiverInStandbyMode_Activity
509  *END**************************************************************************/
511 {
512  lpuart_wakeup_method_t rxWakeMethod;
513  bool lpuart_current_rx_state;
514 
515  rxWakeMethod = LPUART_HAL_GetReceiverWakeupMode(base);
516  lpuart_current_rx_state = LPUART_HAL_GetStatusFlag(base, LPUART_RX_ACTIVE);
517 
518  /* if both rxWakeMethod is set for idle and current rx state is idle,
519  * don't put in standby */
520  if ((rxWakeMethod == LPUART_IDLE_LINE_WAKE) && (lpuart_current_rx_state == false))
521  {
522  return STATUS_ERROR;
523  }
524  else
525  {
526  /* set the RWU bit to place receiver into standby mode */
527  base->CTRL = (base->CTRL & ~LPUART_CTRL_RWU_MASK) | ((uint32_t)1U << LPUART_CTRL_RWU_SHIFT);
528  return STATUS_SUCCESS;
529  }
530 }
531 
532 /*FUNCTION**********************************************************************
533  *
534  * Function Name : LPUART_HAL_SetIdleLineDetect
535  * Description : LPUART idle-line detect operation configuration (idle line
536  * bit-count start and wake up affect on IDLE status bit).
537  * In some LPUART instances, the user should disable the transmitter/receiver
538  * before calling this function.
539  * Generally, this may be applied to all LPUARTs to ensure safe operation.
540  *
541  * Implements : LPUART_HAL_SetIdleLineDetect_Activity
542  *END**************************************************************************/
544  const lpuart_idle_line_config_t *config)
545 {
546  DEV_ASSERT(config != NULL);
547 
548  /* Configure the idle line detection configuration as follows:
549  * configure the ILT to bit count after start bit or stop bit
550  * configure RWUID to set or not set IDLE status bit upon detection of
551  * an idle character when receiver in standby */
552  base->CTRL = (base->CTRL & ~LPUART_CTRL_ILT_MASK) | ((uint32_t)config->idleLineType << LPUART_CTRL_ILT_SHIFT);
554  ((uint32_t)config->rxWakeIdleDetect << LPUART_STAT_RWUID_SHIFT);
555 }
556 
557 /*FUNCTION**********************************************************************
558  *
559  * Function Name : LPUART_HAL_SetMatchAddressReg1
560  * Description : Configures match address register 1
561  *
562  * Implements : LPUART_HAL_SetMatchAddressReg1_Activity
563  *END**************************************************************************/
564 void LPUART_HAL_SetMatchAddressReg1(LPUART_Type * base, bool enable, uint8_t value)
565 {
566  uint32_t matchRegValTemp;
567 
568  /* The MAEN bit must be cleared before configuring MA value */
569  base->BAUD &= ~LPUART_BAUD_MAEN1_MASK;
570  if (enable)
571  {
572  matchRegValTemp = base->MATCH;
573  matchRegValTemp &= ~(LPUART_MATCH_MA1_MASK);
574  matchRegValTemp |= LPUART_MATCH_MA1(value);
575  base->MATCH = matchRegValTemp;
576 
577  base->BAUD = (base->BAUD & ~LPUART_BAUD_MAEN1_MASK) | ((uint32_t)1U << LPUART_BAUD_MAEN1_SHIFT);
578  }
579 }
580 
581 /*FUNCTION**********************************************************************
582  *
583  * Function Name : LPUART_HAL_SetMatchAddressReg2
584  * Description : Configures match address register 2
585  *
586  * Implements : LPUART_HAL_SetMatchAddressReg2_Activity
587  *END**************************************************************************/
588 void LPUART_HAL_SetMatchAddressReg2(LPUART_Type * base, bool enable, uint8_t value)
589 {
590  uint32_t matchRegValTemp;
591 
592  /* The MAEN bit must be cleared before configuring MA value */
593  base->BAUD &= ~LPUART_BAUD_MAEN2_MASK;
594  if (enable)
595  {
596  matchRegValTemp = base->MATCH;
597  matchRegValTemp &= ~(LPUART_MATCH_MA2_MASK);
598  matchRegValTemp |= LPUART_MATCH_MA2(value);
599  base->MATCH = matchRegValTemp;
600 
601  base->BAUD = (base->BAUD & ~LPUART_BAUD_MAEN2_MASK) | ((uint32_t)1U << LPUART_BAUD_MAEN2_SHIFT);
602  }
603 }
604 
605 #if FEATURE_LPUART_HAS_IR_SUPPORT
606 /*FUNCTION**********************************************************************
607  *
608  * Function Name : LPUART_HAL_SetInfrared
609  * Description : Configures the LPUART infrared operation.
610  *
611  * Implements : LPUART_HAL_SetInfrared_Activity
612  *END**************************************************************************/
613 void LPUART_HAL_SetInfrared(LPUART_Type * base, bool enable,
614  lpuart_ir_tx_pulsewidth_t pulseWidth)
615 {
616  uint32_t modirRegValTemp;
617 
618  /* enable or disable infrared */
619  base->MODIR = (base->MODIR & ~LPUART_MODIR_IREN_MASK) | ((enable ? 1UL : 0UL) << LPUART_MODIR_IREN_SHIFT);
620 
621  /* configure the narrow pulse width of the IR pulse */
622  modirRegValTemp = base->MODIR;
623  modirRegValTemp &= ~(LPUART_MODIR_TNP_MASK);
624  modirRegValTemp |= LPUART_MODIR_TNP(pulseWidth);
625  base->MODIR = modirRegValTemp;
626 }
627 #endif /* FEATURE_LPUART_HAS_IR_SUPPORT */
628 
629 /*FUNCTION**********************************************************************
630  *
631  * Function Name : LPUART_HAL_GetStatusFlag
632  * Description : LPUART get status flag by passing flag enum.
633  *
634  * Implements : LPUART_HAL_GetStatusFlag_Activity
635  *END**************************************************************************/
637 {
638  uint32_t reg = (uint32_t)(statusFlag) >> LPUART_SHIFT;
639  bool retVal = false;
640 
641  switch ( reg )
642  {
643  case LPUART_BAUD_REG_ID:
644  retVal = (((base->BAUD >> (uint16_t)(statusFlag)) & 1U) > 0U);
645  break;
646  case LPUART_STAT_REG_ID:
647  retVal = (((base->STAT >> (uint16_t)(statusFlag)) & 1U) > 0U);
648  break;
649  case LPUART_CTRL_REG_ID:
650  retVal = (((base->CTRL >> (uint16_t)(statusFlag)) & 1U) > 0U);
651  break;
652  case LPUART_DATA_REG_ID:
653  retVal = (((base->DATA >> (uint16_t)(statusFlag)) & 1U) > 0U);
654  break;
655  case LPUART_MATCH_REG_ID:
656  retVal = (((base->MATCH >> (uint16_t)(statusFlag)) & 1U) > 0U);
657  break;
658 #if FEATURE_LPUART_HAS_MODEM_SUPPORT
659  case LPUART_MODIR_REG_ID:
660  retVal = (((base->MODIR >> (uint16_t)(statusFlag)) & 1U) > 0U);
661  break;
662 #endif
663 #if FEATURE_LPUART_FIFO_SIZE > 0U
664  case LPUART_FIFO_REG_ID:
665  retVal = (((base->FIFO >> (uint16_t)(statusFlag)) & 1U) > 0U);
666  break;
667 #endif
668  default:
669  /* Invalid parameter: return */
670  break;
671  }
672 
673  return retVal;
674 }
675 
676 /*FUNCTION**********************************************************************
677  *
678  * Function Name : LPUART_HAL_ClearStatusFlag
679  * Description : LPUART clears an individual status flag
680  * (see lpuart_status_flag_t for list of status bits).
681  *
682  * Implements : LPUART_HAL_ClearStatusFlag_Activity
683  *END**************************************************************************/
685  lpuart_status_flag_t statusFlag)
686 {
687  status_t returnCode = STATUS_SUCCESS;
688 
689  switch(statusFlag)
690  {
691  /* These flags are cleared automatically by other lpuart operations
692  * and cannot be manually cleared, return error code */
694  case LPUART_TX_COMPLETE:
696  case LPUART_RX_ACTIVE:
697 #if FEATURE_LPUART_HAS_EXTENDED_DATA_REGISTER_FLAGS
700 #endif
701  returnCode = STATUS_ERROR;
702  break;
703 
706  break;
707 
708  case LPUART_RX_OVERRUN:
710  break;
711 
712  case LPUART_NOISE_DETECT:
714  break;
715 
716  case LPUART_FRAME_ERR:
718  break;
719 
720  case LPUART_PARITY_ERR:
722  break;
723 
726  break;
727 
730  break;
731 
732 #if FEATURE_LPUART_HAS_ADDRESS_MATCHING
735  break;
738  break;
739 #endif
740 #if FEATURE_LPUART_FIFO_SIZE > 0U
741  case LPUART_FIFO_TX_OF:
743  break;
744  case LPUART_FIFO_RX_UF:
746  break;
747 #endif
748  default:
749  returnCode = STATUS_ERROR;
750  break;
751  }
752 
753  return (returnCode);
754 }
755 
756 /*******************************************************************************
757  * EOF
758  ******************************************************************************/
759 
#define LPUART_CTRL_LOOPS_SHIFT
Definition: S32K144.h:6911
#define LPUART_STAT_RWUID_SHIFT
Definition: S32K144.h:6862
#define LPUART_MODIR_TNP(x)
Definition: S32K144.h:7096
#define LPUART_STAT_TDRE_SHIFT
Definition: S32K144.h:6846
#define LPUART_STAT_RDRF_SHIFT
Definition: S32K144.h:6838
__IO uint32_t BAUD
Definition: S32K144.h:6672
#define LPUART_MATCH_MA2(x)
Definition: S32K144.h:7063
#define LPUART_STAT_MA1F_MASK
Definition: S32K144.h:6813
#define LPUART_STAT_OR_MASK
Definition: S32K144.h:6829
#define LPUART_BAUD_BOTHEDGE_MASK
Definition: S32K144.h:6772
__IO uint32_t MODIR
Definition: S32K144.h:6677
#define LPUART_CTRL_PT_SHIFT
Definition: S32K144.h:6883
void LPUART_HAL_Putchar9(LPUART_Type *base, uint16_t data)
Sends the LPUART 9-bit character.
Definition: lpuart_hal.c:226
#define LPUART_MODIR_IREN_MASK
Definition: S32K144.h:7097
#define LPUART_STAT_RXEDGIF_MASK
Definition: S32K144.h:6873
void LPUART_HAL_SendDataPolling(LPUART_Type *base, const uint8_t *txBuff, uint32_t txSize)
Send out multiple bytes of data using polling method.
Definition: lpuart_hal.c:314
status_t LPUART_HAL_SetReceiverInStandbyMode(LPUART_Type *base)
Places the LPUART receiver in standby mode.
Definition: lpuart_hal.c:510
#define LPUART_BAUD_OSR_MASK
Definition: S32K144.h:6792
#define LPUART_STAT_LBKDIF_MASK
Definition: S32K144.h:6877
#define LPUART_CTRL_PE_SHIFT
Definition: S32K144.h:6887
#define LPUART_BAUD_MAEN2_MASK
Definition: S32K144.h:6800
#define LPUART_BAUD_REG_ID
Definition: lpuart_hal.h:55
#define LPUART_BAUD_SBR_SHIFT
Definition: S32K144.h:6753
#define LPUART_STAT_FE_MASK
Definition: S32K144.h:6821
static lpuart_wakeup_method_t LPUART_HAL_GetReceiverWakeupMode(const LPUART_Type *base)
Gets the LPUART receiver wakeup method from standby mode.
Definition: lpuart_hal.h:1147
#define LPUART_MATCH_MA1(x)
Definition: S32K144.h:7059
#define LPUART_CTRL_PE_MASK
Definition: S32K144.h:6886
#define LPUART_FIFO_REG_ID
Definition: lpuart_hal.h:61
status_t LPUART_HAL_ReceiveDataPolling(LPUART_Type *base, uint8_t *rxBuff, uint32_t rxSize)
Receive multiple bytes of data using polling method.
Definition: lpuart_hal.c:337
#define LPUART_MATCH_MA1_MASK
Definition: S32K144.h:7056
#define LPUART_BAUD_MAEN2_SHIFT
Definition: S32K144.h:6801
#define LPUART_DATA_REG_ID
Definition: lpuart_hal.h:58
#define LPUART_CTRL_LOOPS_MASK
Definition: S32K144.h:6910
#define LPUART_STAT_PF_MASK
Definition: S32K144.h:6817
#define FEATURE_LPUART_FIFO_REG_FLAGS_MASK
void LPUART_HAL_SetParityMode(LPUART_Type *base, lpuart_parity_mode_t parityModeType)
Configures parity mode in the LPUART controller.
Definition: lpuart_hal.c:213
#define LPUART_STAT_RWUID_MASK
Definition: S32K144.h:6861
#define FEATURE_LPUART_DEFAULT_SBR
#define LPUART_MODIR_REG_ID
Definition: lpuart_hal.h:60
lpuart_status_flag_t
LPUART status flags.
Definition: lpuart_hal.h:229
#define LPUART_SHIFT
Definition: lpuart_hal.h:54
#define DEV_ASSERT(x)
Definition: devassert.h:78
#define LPUART_STAT_NF_MASK
Definition: S32K144.h:6825
#define LPUART_STAT_OR_SHIFT
Definition: S32K144.h:6830
#define FEATURE_LPUART_STAT_REG_FLAGS_MASK
#define LPUART_BAUD_M10_MASK
Definition: S32K144.h:6796
#define LPUART_STAT_REG_ID
Definition: lpuart_hal.h:56
#define LPUART_CTRL_M_MASK
Definition: S32K144.h:6898
#define LPUART_FIFO_RXUF_MASK
Definition: S32K144.h:7138
#define LPUART_BAUD_BOTHEDGE_SHIFT
Definition: S32K144.h:6773
#define LPUART_CTRL_R9T8_MASK
Definition: S32K144.h:6986
__IO uint32_t STAT
Definition: S32K144.h:6673
bool LPUART_HAL_GetIntMode(const LPUART_Type *base, lpuart_interrupt_t intSrc)
Returns LPUART module interrupts state.
Definition: lpuart_hal.c:418
#define LPUART_CTRL_PT_MASK
Definition: S32K144.h:6882
#define LPUART_STAT_MA2F_MASK
Definition: S32K144.h:6809
#define LPUART_CTRL_REG_ID
Definition: lpuart_hal.h:57
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
Definition: status.h:31
#define LPUART_CTRL_ILT_MASK
Definition: S32K144.h:6890
#define LPUART_CTRL_RSRC_MASK
Definition: S32K144.h:6902
void LPUART_HAL_SetInfrared(LPUART_Type *base, bool enable, lpuart_ir_tx_pulsewidth_t pulseWidth)
Configures the LPUART infrared operation.
#define LPUART_BAUD_OSR(x)
Definition: S32K144.h:6795
__IO uint32_t DATA
Definition: S32K144.h:6675
lpuart_parity_mode_t
LPUART parity mode.
Definition: lpuart_hal.h:78
__IO uint32_t CTRL
Definition: S32K144.h:6674
#define LPUART_CTRL_R8T9_SHIFT
Definition: S32K144.h:6991
void LPUART_HAL_SetSingleWireCmd(LPUART_Type *base, bool enable)
Configures the LPUART single-wire operation (enable/disable single-wire mode).
Definition: lpuart_hal.c:492
lpuart_bit_count_per_char_t
LPUART number of bits in a character.
Definition: lpuart_hal.h:89
#define LPUART_CTRL_RSRC_SHIFT
Definition: S32K144.h:6903
#define LPUART_CTRL_R8T9_MASK
Definition: S32K144.h:6990
Structure for idle line configuration settings.
Definition: lpuart_hal.h:211
#define LPUART_CTRL_RWU_SHIFT
Definition: S32K144.h:6935
static void LPUART_HAL_Putchar(LPUART_Type *base, uint8_t data)
Sends the LPUART 8-bit character.
Definition: lpuart_hal.h:710
#define LPUART_CTRL_RWU_MASK
Definition: S32K144.h:6934
lpuart_wakeup_method_t
LPUART wakeup from standby method constants.
Definition: lpuart_hal.h:110
#define LPUART_MODIR_TNP_MASK
Definition: S32K144.h:7093
void LPUART_HAL_SetLoopbackCmd(LPUART_Type *base, bool enable)
Configures the LPUART loopback operation (enable/disable loopback operation)
Definition: lpuart_hal.c:469
#define LPUART_STAT_IDLE_MASK
Definition: S32K144.h:6833
void LPUART_HAL_SetMatchAddressReg1(LPUART_Type *base, bool enable, uint8_t value)
Configures address match register 1.
Definition: lpuart_hal.c:564
__IO uint32_t FIFO
Definition: S32K144.h:6678
status_t LPUART_HAL_ClearStatusFlag(LPUART_Type *base, lpuart_status_flag_t statusFlag)
LPUART clears an individual status flag.
Definition: lpuart_hal.c:684
#define LPUART_CTRL_ILT_SHIFT
Definition: S32K144.h:6891
void LPUART_HAL_Getchar9(const LPUART_Type *base, uint16_t *readData)
Gets the LPUART 9-bit character.
Definition: lpuart_hal.c:274
#define LPUART_CTRL_M_SHIFT
Definition: S32K144.h:6899
lpuart_ir_tx_pulsewidth_t
LPUART infra-red transmitter pulse width options.
Definition: lpuart_hal.h:159
#define FEATURE_LPUART_DEFAULT_OSR
#define LPUART_BAUD_MAEN1_SHIFT
Definition: S32K144.h:6805
void LPUART_HAL_Getchar10(const LPUART_Type *base, uint16_t *readData)
Gets the LPUART 10-bit character.
Definition: lpuart_hal.c:293
#define LPUART_MATCH_MA2_MASK
Definition: S32K144.h:7060
status_t LPUART_HAL_SetBaudRate(LPUART_Type *base, uint32_t sourceClockInHz, uint32_t desiredBaudRate)
Configures the LPUART baud rate.
Definition: lpuart_hal.c:95
void LPUART_HAL_SetMatchAddressReg2(LPUART_Type *base, bool enable, uint8_t value)
Configures address match register 2.
Definition: lpuart_hal.c:588
#define LPUART_CTRL_R9T8_SHIFT
Definition: S32K144.h:6987
__IO uint32_t MATCH
Definition: S32K144.h:6676
#define LPUART_FIFO_TXOF_MASK
Definition: S32K144.h:7142
void LPUART_HAL_Putchar10(LPUART_Type *base, uint16_t data)
Sends the LPUART 10-bit character (Note: Feature available on select LPUART instances).
Definition: lpuart_hal.c:248
__IO uint32_t WATER
Definition: S32K144.h:6679
void LPUART_HAL_SetBitCountPerChar(LPUART_Type *base, lpuart_bit_count_per_char_t bitCountPerChar)
Configures the number of bits per character in the LPUART controller.
Definition: lpuart_hal.c:187
#define LPUART_BAUD_MAEN1_MASK
Definition: S32K144.h:6804
void LPUART_HAL_SetIntMode(LPUART_Type *base, lpuart_interrupt_t intSrc, bool enable)
Configures the LPUART module interrupts.
Definition: lpuart_hal.c:371
#define LPUART_BAUD_SBR(x)
Definition: S32K144.h:6755
bool LPUART_HAL_GetStatusFlag(const LPUART_Type *base, lpuart_status_flag_t statusFlag)
LPUART get status flag.
Definition: lpuart_hal.c:636
#define LPUART_BAUD_OSR_SHIFT
Definition: S32K144.h:6793
#define LPUART_BAUD_M10_SHIFT
Definition: S32K144.h:6797
void LPUART_HAL_SetIdleLineDetect(LPUART_Type *base, const lpuart_idle_line_config_t *config)
LPUART idle-line detect operation configuration.
Definition: lpuart_hal.c:543
void LPUART_HAL_Init(LPUART_Type *base)
Initializes the LPUART controller.
Definition: lpuart_hal.c:62
#define LPUART_MATCH_REG_ID
Definition: lpuart_hal.h:59
#define LPUART_MODIR_IREN_SHIFT
Definition: S32K144.h:7098
lpuart_interrupt_t
LPUART interrupt configuration structure, default settings are 0 (disabled)
Definition: lpuart_hal.h:292
#define LPUART_BAUD_SBR_MASK
Definition: S32K144.h:6752
static void LPUART_HAL_Getchar(const LPUART_Type *base, uint8_t *readData)
Gets the LPUART 8-bit character.
Definition: lpuart_hal.h:748