1 /******************************************************************************
3 * Copyright (C) 2010 - 2015 Xilinx, Inc. All rights reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * Use of the Software is limited solely to applications:
16 * (a) running on a Xilinx device, or
17 * (b) that interact with a Xilinx device through a bus or interconnect.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
24 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * Except as contained in this notice, the name of the Xilinx shall not be used
28 * in advertising or otherwise to promote the sale, use or other dealings in
29 * this Software without prior written authorization from Xilinx.
31 ******************************************************************************/
32 /****************************************************************************/
35 * @file xuartps_options.c
36 * @addtogroup uartps_v3_1
39 * The implementation of the options functions for the XUartPs driver.
42 * MODIFICATION HISTORY:
44 * Ver Who Date Changes
45 * ----- ------ -------- -----------------------------------------------
46 * 1.00 drg/jz 01/13/10 First Release
47 * 1.00 sdm 09/27/11 Fixed a bug in XUartPs_SetFlowDelay where the input
48 * value was not being written to the register.
49 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
53 *****************************************************************************/
55 /***************************** Include Files ********************************/
59 /************************** Constant Definitions ****************************/
61 /**************************** Type Definitions ******************************/
63 /***************** Macros (Inline Functions) Definitions ********************/
65 /************************** Variable Definitions ****************************/
67 * The following data type is a map from an option to the offset in the
68 * register to which it belongs as well as its bit mask in that register.
77 * Create the table which contains options which are to be processed to get/set
78 * the options. These options are table driven to allow easy maintenance and
79 * expansion of the options.
82 static Mapping OptionsTable[] = {
83 {XUARTPS_OPTION_SET_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STARTBRK},
84 {XUARTPS_OPTION_STOP_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STOPBRK},
85 {XUARTPS_OPTION_RESET_TMOUT, XUARTPS_CR_OFFSET, XUARTPS_CR_TORST},
86 {XUARTPS_OPTION_RESET_TX, XUARTPS_CR_OFFSET, XUARTPS_CR_TXRST},
87 {XUARTPS_OPTION_RESET_RX, XUARTPS_CR_OFFSET, XUARTPS_CR_RXRST},
88 {XUARTPS_OPTION_ASSERT_RTS, XUARTPS_MODEMCR_OFFSET,
90 {XUARTPS_OPTION_ASSERT_DTR, XUARTPS_MODEMCR_OFFSET,
92 {XUARTPS_OPTION_SET_FCM, XUARTPS_MODEMCR_OFFSET, XUARTPS_MODEMCR_FCM}
95 /* Create a constant for the number of entries in the table */
97 #define XUARTPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(Mapping))
99 /************************** Function Prototypes *****************************/
101 /****************************************************************************/
104 * Gets the options for the specified driver instance. The options are
105 * implemented as bit masks such that multiple options may be enabled or
106 * disabled simulataneously.
108 * @param InstancePtr is a pointer to the XUartPs instance.
112 * The current options for the UART. The optionss are bit masks that are
113 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
117 *****************************************************************************/
118 u16 XUartPs_GetOptions(XUartPs *InstancePtr)
124 /* Assert validates the input arguments */
125 Xil_AssertNonvoid(InstancePtr != NULL);
126 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
129 * Loop thru the options table to map the physical options in the
130 * registers of the UART to the logical options to be returned
132 for (Index = 0U; Index < XUARTPS_NUM_OPTIONS; Index++) {
133 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
138 * If the bit in the register which correlates to the option
139 * is set, then set the corresponding bit in the options,
140 * ignoring any bits which are zero since the options variable
141 * is initialized to zero
143 if ((Register & OptionsTable[Index].Mask) != (u32)0) {
144 Options |= OptionsTable[Index].Option;
151 /****************************************************************************/
154 * Sets the options for the specified driver instance. The options are
155 * implemented as bit masks such that multiple options may be enabled or
156 * disabled simultaneously.
158 * The GetOptions function may be called to retrieve the currently enabled
159 * options. The result is ORed in the desired new settings to be enabled and
160 * ANDed with the inverse to clear the settings to be disabled. The resulting
161 * value is then used as the options for the SetOption function call.
163 * @param InstancePtr is a pointer to the XUartPs instance.
164 * @param Options contains the options to be set which are bit masks
165 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
171 *****************************************************************************/
172 void XUartPs_SetOptions(XUartPs *InstancePtr, u16 Options)
177 /* Assert validates the input arguments */
178 Xil_AssertVoid(InstancePtr != NULL);
179 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
182 * Loop thru the options table to map the logical options to the
183 * physical options in the registers of the UART.
185 for (Index = 0U; Index < XUARTPS_NUM_OPTIONS; Index++) {
188 * Read the register which contains option so that the register
189 * can be changed without destoying any other bits of the
192 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
197 * If the option is set in the input, then set the corresponding
198 * bit in the specified register, otherwise clear the bit in
201 if ((Options & OptionsTable[Index].Option) != (u16)0) {
202 Register |= OptionsTable[Index].Mask;
205 Register &= ~OptionsTable[Index].Mask;
208 /* Write the new value to the register to set the option */
209 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
210 OptionsTable[Index].RegisterOffset,
216 /****************************************************************************/
219 * This function gets the receive FIFO trigger level. The receive trigger
220 * level indicates the number of bytes in the receive FIFO that cause a receive
221 * data event (interrupt) to be generated.
223 * @param InstancePtr is a pointer to the XUartPs instance.
225 * @return The current receive FIFO trigger level. This is a value
230 *****************************************************************************/
231 u8 XUartPs_GetFifoThreshold(XUartPs *InstancePtr)
235 /* Assert validates the input arguments */
236 Xil_AssertNonvoid(InstancePtr != NULL);
237 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
240 * Read the value of the FIFO control register so that the threshold
241 * can be retrieved, this read takes special register processing
243 RtrigRegister = (u8) XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
244 XUARTPS_RXWM_OFFSET);
246 /* Return only the trigger level from the register value */
248 RtrigRegister &= (u8)XUARTPS_RXWM_MASK;
249 return RtrigRegister;
252 /****************************************************************************/
255 * This functions sets the receive FIFO trigger level. The receive trigger
256 * level specifies the number of bytes in the receive FIFO that cause a receive
257 * data event (interrupt) to be generated.
259 * @param InstancePtr is a pointer to the XUartPs instance.
260 * @param TriggerLevel contains the trigger level to set.
266 *****************************************************************************/
267 void XUartPs_SetFifoThreshold(XUartPs *InstancePtr, u8 TriggerLevel)
271 /* Assert validates the input arguments */
272 Xil_AssertVoid(InstancePtr != NULL);
273 Xil_AssertVoid(TriggerLevel <= (u8)XUARTPS_RXWM_MASK);
274 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
276 RtrigRegister = ((u32)TriggerLevel) & (u32)XUARTPS_RXWM_MASK;
279 * Write the new value for the FIFO control register to it such that the
280 * threshold is changed
282 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
283 XUARTPS_RXWM_OFFSET, RtrigRegister);
287 /****************************************************************************/
290 * This function gets the modem status from the specified UART. The modem
291 * status indicates any changes of the modem signals. This function allows
292 * the modem status to be read in a polled mode. The modem status is updated
293 * whenever it is read such that reading it twice may not yield the same
296 * @param InstancePtr is a pointer to the XUartPs instance.
300 * The modem status which are bit masks that are contained in the file
301 * xuartps.h and named XUARTPS_MODEM_*.
305 * The bit masks used for the modem status are the exact bits of the modem
306 * status register with no abstraction.
308 *****************************************************************************/
309 u16 XUartPs_GetModemStatus(XUartPs *InstancePtr)
311 u32 ModemStatusRegister;
313 /* Assert validates the input arguments */
314 Xil_AssertNonvoid(InstancePtr != NULL);
315 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
317 /* Read the modem status register to return
319 ModemStatusRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
320 XUARTPS_MODEMSR_OFFSET);
321 TmpRegister = (u16)ModemStatusRegister;
325 /****************************************************************************/
328 * This function determines if the specified UART is sending data.
330 * @param InstancePtr is a pointer to the XUartPs instance.
333 * - TRUE if the UART is sending data
334 * - FALSE if UART is not sending data
338 *****************************************************************************/
339 u32 XUartPs_IsSending(XUartPs *InstancePtr)
341 u32 ChanStatRegister;
342 u32 ChanTmpSRegister;
346 /* Assert validates the input arguments */
347 Xil_AssertNonvoid(InstancePtr != NULL);
348 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
351 * Read the channel status register to determine if the transmitter is
354 ChanStatRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
358 * If the transmitter is active, or the TX FIFO is not empty, then indicate
359 * that the UART is still sending some data
361 ActiveResult = ChanStatRegister & ((u32)XUARTPS_SR_TACTIVE);
362 EmptyResult = ChanStatRegister & ((u32)XUARTPS_SR_TXEMPTY);
363 ChanTmpSRegister = (((u32)XUARTPS_SR_TACTIVE) == ActiveResult) ||
364 (((u32)XUARTPS_SR_TXEMPTY) != EmptyResult);
366 return ChanTmpSRegister;
369 /****************************************************************************/
372 * This function gets the operational mode of the UART. The UART can operate
373 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
376 * @param InstancePtr is a pointer to the XUartPs instance.
380 * The operational mode is specified by constants defined in xuartps.h. The
381 * constants are named XUARTPS_OPER_MODE_*
385 *****************************************************************************/
386 u8 XUartPs_GetOperMode(XUartPs *InstancePtr)
391 /* Assert validates the input arguments */
392 Xil_AssertNonvoid(InstancePtr != NULL);
393 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
395 /* Read the Mode register. */
397 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
400 ModeRegister &= (u32)XUARTPS_MR_CHMODE_MASK;
401 /* Return the constant */
402 switch (ModeRegister) {
403 case XUARTPS_MR_CHMODE_NORM:
404 OperMode = XUARTPS_OPER_MODE_NORMAL;
406 case XUARTPS_MR_CHMODE_ECHO:
407 OperMode = XUARTPS_OPER_MODE_AUTO_ECHO;
409 case XUARTPS_MR_CHMODE_L_LOOP:
410 OperMode = XUARTPS_OPER_MODE_LOCAL_LOOP;
412 case XUARTPS_MR_CHMODE_R_LOOP:
413 OperMode = XUARTPS_OPER_MODE_REMOTE_LOOP;
416 OperMode = (u8) ((ModeRegister & (u32)XUARTPS_MR_CHMODE_MASK) >>
417 XUARTPS_MR_CHMODE_SHIFT);
424 /****************************************************************************/
427 * This function sets the operational mode of the UART. The UART can operate
428 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
431 * @param InstancePtr is a pointer to the XUartPs instance.
432 * @param OperationMode is the mode of the UART.
438 *****************************************************************************/
439 void XUartPs_SetOperMode(XUartPs *InstancePtr, u8 OperationMode)
443 /* Assert validates the input arguments. */
444 Xil_AssertVoid(InstancePtr != NULL);
445 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
446 Xil_AssertVoid(OperationMode <= XUARTPS_OPER_MODE_REMOTE_LOOP);
448 /* Read the Mode register. */
450 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
453 /* Set the correct value by masking the bits, then ORing the const. */
454 ModeRegister &= (u32)(~XUARTPS_MR_CHMODE_MASK);
456 switch (OperationMode) {
457 case XUARTPS_OPER_MODE_NORMAL:
458 ModeRegister |= (u32)XUARTPS_MR_CHMODE_NORM;
460 case XUARTPS_OPER_MODE_AUTO_ECHO:
461 ModeRegister |= (u32)XUARTPS_MR_CHMODE_ECHO;
463 case XUARTPS_OPER_MODE_LOCAL_LOOP:
464 ModeRegister |= (u32)XUARTPS_MR_CHMODE_L_LOOP;
466 case XUARTPS_OPER_MODE_REMOTE_LOOP:
467 ModeRegister |= (u32)XUARTPS_MR_CHMODE_R_LOOP;
470 /* Default case made for MISRA-C Compliance. */
474 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
479 /****************************************************************************/
482 * This function sets the Flow Delay.
483 * 0 - 3: Flow delay inactive
484 * 4 - 32: If Flow Control mode is enabled, UART_rtsN is deactivated when the
485 * receive FIFO fills to this level.
487 * @param InstancePtr is a pointer to the XUartPs instance.
491 * The Flow Delay is specified by constants defined in xuartps_hw.h. The
492 * constants are named XUARTPS_FLOWDEL*
496 *****************************************************************************/
497 u8 XUartPs_GetFlowDelay(XUartPs *InstancePtr)
501 /* Assert validates the input arguments */
502 Xil_AssertNonvoid(InstancePtr != NULL);
503 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
505 /* Read the Mode register. */
506 FdelTmpRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
507 XUARTPS_FLOWDEL_OFFSET);
509 /* Return the contents of the flow delay register */
510 FdelTmpRegister = (u8)(FdelTmpRegister & (u32)XUARTPS_FLOWDEL_MASK);
511 return FdelTmpRegister;
514 /****************************************************************************/
517 * This function sets the Flow Delay.
518 * 0 - 3: Flow delay inactive
519 * 4 - 63: If Flow Control mode is enabled, UART_rtsN is deactivated when the
520 * receive FIFO fills to this level.
522 * @param InstancePtr is a pointer to the XUartPs instance.
523 * @param FlowDelayValue is the Setting for the flow delay.
529 *****************************************************************************/
530 void XUartPs_SetFlowDelay(XUartPs *InstancePtr, u8 FlowDelayValue)
534 /* Assert validates the input arguments */
535 Xil_AssertVoid(InstancePtr != NULL);
536 Xil_AssertVoid(FlowDelayValue > (u8)XUARTPS_FLOWDEL_MASK);
537 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
540 * Set the correct value by shifting the input constant, then masking
543 FdelRegister = ((u32)FlowDelayValue) & (u32)XUARTPS_FLOWDEL_MASK;
545 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
546 XUARTPS_FLOWDEL_OFFSET, FdelRegister);
550 /****************************************************************************/
553 * This function gets the Receive Timeout of the UART.
555 * @param InstancePtr is a pointer to the XUartPs instance.
557 * @return The current setting for receive time out.
561 *****************************************************************************/
562 u8 XUartPs_GetRecvTimeout(XUartPs *InstancePtr)
567 /* Assert validates the input arguments */
568 Xil_AssertNonvoid(InstancePtr != NULL);
569 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
571 /* Read the Receive Timeout register. */
572 RtoRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
573 XUARTPS_RXTOUT_OFFSET);
575 /* Return the contents of the mode register shifted appropriately */
576 RtoRTmpRegister = (u8)(RtoRegister & (u32)XUARTPS_RXTOUT_MASK);
577 return RtoRTmpRegister;
580 /****************************************************************************/
583 * This function sets the Receive Timeout of the UART.
585 * @param InstancePtr is a pointer to the XUartPs instance.
586 * @param RecvTimeout setting allows the UART to detect an idle connection
587 * on the reciever data line.
588 * Timeout duration = RecvTimeout x 4 x Bit Period. 0 disables the
595 *****************************************************************************/
596 void XUartPs_SetRecvTimeout(XUartPs *InstancePtr, u8 RecvTimeout)
600 /* Assert validates the input arguments */
601 Xil_AssertVoid(InstancePtr != NULL);
602 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
604 /* Set the correct value by masking the bits */
605 RtoRegister = ((u32)RecvTimeout & (u32)XUARTPS_RXTOUT_MASK);
607 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
608 XUARTPS_RXTOUT_OFFSET, RtoRegister);
610 /* Configure CR to restart the receiver timeout counter */
612 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
614 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
615 (RtoRegister | XUARTPS_CR_TORST));
618 /****************************************************************************/
621 * Sets the data format for the device. The data format includes the
622 * baud rate, number of data bits, number of stop bits, and parity. It is the
623 * caller's responsibility to ensure that the UART is not sending or receiving
624 * data when this function is called.
626 * @param InstancePtr is a pointer to the XUartPs instance.
627 * @param FormatPtr is a pointer to a format structure containing the data
631 * - XST_SUCCESS if the data format was successfully set.
632 * - XST_UART_BAUD_ERROR indicates the baud rate could not be
633 * set because of the amount of error with the baud rate and
634 * the input clock frequency.
635 * - XST_INVALID_PARAM if one of the parameters was not valid.
639 * The data types in the format type, data bits and parity, are 32 bit fields
640 * to prevent a compiler warning.
641 * The asserts in this function will cause a warning if these fields are
645 *****************************************************************************/
646 s32 XUartPs_SetDataFormat(XUartPs *InstancePtr,
647 XUartPsFormat * FormatPtr)
652 Xil_AssertNonvoid(InstancePtr != NULL);
653 Xil_AssertNonvoid(FormatPtr != NULL);
654 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
656 /* Verify the inputs specified are valid */
657 if ((FormatPtr->DataBits > ((u32)XUARTPS_FORMAT_6_BITS)) ||
658 (FormatPtr->StopBits > ((u8)XUARTPS_FORMAT_2_STOP_BIT)) ||
659 (FormatPtr->Parity > ((u32)XUARTPS_FORMAT_NO_PARITY))) {
660 Status = XST_INVALID_PARAM;
664 * Try to set the baud rate and if it's not successful then don't
665 * continue altering the data format, this is done first to avoid the
666 * format from being altered when an error occurs
668 Status = XUartPs_SetBaudRate(InstancePtr, FormatPtr->BaudRate);
669 if (Status != (s32)XST_SUCCESS) {
674 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
678 * Set the length of data (8,7,6) by first clearing out the bits
679 * that control it in the register, then set the length in the register
681 ModeRegister &= (u32)(~XUARTPS_MR_CHARLEN_MASK);
682 ModeRegister |= (FormatPtr->DataBits << XUARTPS_MR_CHARLEN_SHIFT);
685 * Set the number of stop bits in the mode register by first clearing
686 * out the bits that control it in the register, then set the number
687 * of stop bits in the register.
689 ModeRegister &= (u32)(~XUARTPS_MR_STOPMODE_MASK);
690 ModeRegister |= (((u32)FormatPtr->StopBits) << XUARTPS_MR_STOPMODE_SHIFT);
693 * Set the parity by first clearing out the bits that control it in the
694 * register, then set the bits in the register, the default is no parity
695 * after clearing the register bits
697 ModeRegister &= (u32)(~XUARTPS_MR_PARITY_MASK);
698 ModeRegister |= (FormatPtr->Parity << XUARTPS_MR_PARITY_SHIFT);
700 /* Update the mode register */
701 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
704 Status = XST_SUCCESS;
710 /****************************************************************************/
713 * Gets the data format for the specified UART. The data format includes the
714 * baud rate, number of data bits, number of stop bits, and parity.
716 * @param InstancePtr is a pointer to the XUartPs instance.
717 * @param FormatPtr is a pointer to a format structure that will contain
718 * the data format after this call completes.
725 *****************************************************************************/
726 void XUartPs_GetDataFormat(XUartPs *InstancePtr, XUartPsFormat * FormatPtr)
731 /* Assert validates the input arguments */
732 Xil_AssertVoid(InstancePtr != NULL);
733 Xil_AssertVoid(FormatPtr != NULL);
734 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
737 * Get the baud rate from the instance, this is not retrieved from the
738 * hardware because it is only kept as a divisor such that it is more
739 * difficult to get back to the baud rate
741 FormatPtr->BaudRate = InstancePtr->BaudRate;
743 ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
746 /* Get the length of data (8,7,6,5) */
747 FormatPtr->DataBits =
748 ((ModeRegister & (u32)XUARTPS_MR_CHARLEN_MASK) >>
749 XUARTPS_MR_CHARLEN_SHIFT);
751 /* Get the number of stop bits */
752 FormatPtr->StopBits =
753 (u8)((ModeRegister & (u32)XUARTPS_MR_STOPMODE_MASK) >>
754 XUARTPS_MR_STOPMODE_SHIFT);
756 /* Determine what parity is */
758 (u32)((ModeRegister & (u32)XUARTPS_MR_PARITY_MASK) >>
759 XUARTPS_MR_PARITY_SHIFT);