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
37 * The implementation of the options functions for the XUartPs driver.
40 * MODIFICATION HISTORY:
42 * Ver Who Date Changes
43 * ----- ------ -------- -----------------------------------------------
44 * 1.00 drg/jz 01/13/10 First Release
45 * 1.00 sdm 09/27/11 Fixed a bug in XUartPs_SetFlowDelay where the input
46 * value was not being written to the register.
47 * 3.00 kvn 02/13/15 Modified code for MISRA-C:2012 compliance.
51 *****************************************************************************/
53 /***************************** Include Files ********************************/
57 /************************** Constant Definitions ****************************/
59 /**************************** Type Definitions ******************************/
61 /***************** Macros (Inline Functions) Definitions ********************/
63 /************************** Variable Definitions ****************************/
65 * The following data type is a map from an option to the offset in the
66 * register to which it belongs as well as its bit mask in that register.
75 * Create the table which contains options which are to be processed to get/set
76 * the options. These options are table driven to allow easy maintenance and
77 * expansion of the options.
80 static Mapping OptionsTable[] = {
81 {XUARTPS_OPTION_SET_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STARTBRK},
82 {XUARTPS_OPTION_STOP_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STOPBRK},
83 {XUARTPS_OPTION_RESET_TMOUT, XUARTPS_CR_OFFSET, XUARTPS_CR_TORST},
84 {XUARTPS_OPTION_RESET_TX, XUARTPS_CR_OFFSET, XUARTPS_CR_TXRST},
85 {XUARTPS_OPTION_RESET_RX, XUARTPS_CR_OFFSET, XUARTPS_CR_RXRST},
86 {XUARTPS_OPTION_ASSERT_RTS, XUARTPS_MODEMCR_OFFSET,
88 {XUARTPS_OPTION_ASSERT_DTR, XUARTPS_MODEMCR_OFFSET,
90 {XUARTPS_OPTION_SET_FCM, XUARTPS_MODEMCR_OFFSET, XUARTPS_MODEMCR_FCM}
93 /* Create a constant for the number of entries in the table */
95 #define XUARTPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(Mapping))
97 /************************** Function Prototypes *****************************/
99 /****************************************************************************/
102 * Gets the options for the specified driver instance. The options are
103 * implemented as bit masks such that multiple options may be enabled or
104 * disabled simulataneously.
106 * @param InstancePtr is a pointer to the XUartPs instance.
110 * The current options for the UART. The optionss are bit masks that are
111 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
115 *****************************************************************************/
116 u16 XUartPs_GetOptions(XUartPs *InstancePtr)
123 * 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)
178 * Assert validates the input arguments
180 Xil_AssertVoid(InstancePtr != NULL);
181 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
184 * Loop thru the options table to map the logical options to the
185 * physical options in the registers of the UART.
187 for (Index = 0U; Index < XUARTPS_NUM_OPTIONS; Index++) {
190 * Read the register which contains option so that the register
191 * can be changed without destoying any other bits of the
194 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
199 * If the option is set in the input, then set the corresponding
200 * bit in the specified register, otherwise clear the bit in
203 if ((Options & OptionsTable[Index].Option) != (u16)0) {
204 Register |= OptionsTable[Index].Mask;
207 Register &= ~OptionsTable[Index].Mask;
210 /* Write the new value to the register to set the option */
211 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
212 OptionsTable[Index].RegisterOffset,
218 /****************************************************************************/
221 * This function gets the receive FIFO trigger level. The receive trigger
222 * level indicates the number of bytes in the receive FIFO that cause a receive
223 * data event (interrupt) to be generated.
225 * @param InstancePtr is a pointer to the XUartPs instance.
227 * @return The current receive FIFO trigger level. This is a value
232 *****************************************************************************/
233 u8 XUartPs_GetFifoThreshold(XUartPs *InstancePtr)
238 * Assert validates the input arguments
240 Xil_AssertNonvoid(InstancePtr != NULL);
241 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
244 * Read the value of the FIFO control register so that the threshold
245 * can be retrieved, this read takes special register processing
247 RtrigRegister = (u8) XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
248 XUARTPS_RXWM_OFFSET);
250 /* Return only the trigger level from the register value */
252 RtrigRegister &= (u8)XUARTPS_RXWM_MASK;
253 return RtrigRegister;
256 /****************************************************************************/
259 * This functions sets the receive FIFO trigger level. The receive trigger
260 * level specifies the number of bytes in the receive FIFO that cause a receive
261 * data event (interrupt) to be generated.
263 * @param InstancePtr is a pointer to the XUartPs instance.
264 * @param TriggerLevel contains the trigger level to set.
270 *****************************************************************************/
271 void XUartPs_SetFifoThreshold(XUartPs *InstancePtr, u8 TriggerLevel)
276 * Assert validates the input arguments
278 Xil_AssertVoid(InstancePtr != NULL);
279 Xil_AssertVoid(TriggerLevel <= (u8)XUARTPS_RXWM_MASK);
280 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
282 RtrigRegister = ((u32)TriggerLevel) & (u32)XUARTPS_RXWM_MASK;
285 * Write the new value for the FIFO control register to it such that the
286 * threshold is changed
288 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
289 XUARTPS_RXWM_OFFSET, RtrigRegister);
293 /****************************************************************************/
296 * This function gets the modem status from the specified UART. The modem
297 * status indicates any changes of the modem signals. This function allows
298 * the modem status to be read in a polled mode. The modem status is updated
299 * whenever it is read such that reading it twice may not yield the same
302 * @param InstancePtr is a pointer to the XUartPs instance.
306 * The modem status which are bit masks that are contained in the file
307 * xuartps.h and named XUARTPS_MODEM_*.
311 * The bit masks used for the modem status are the exact bits of the modem
312 * status register with no abstraction.
314 *****************************************************************************/
315 u16 XUartPs_GetModemStatus(XUartPs *InstancePtr)
317 u32 ModemStatusRegister;
320 * Assert validates the input arguments
322 Xil_AssertNonvoid(InstancePtr != NULL);
323 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
325 /* Read the modem status register to return
327 ModemStatusRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
328 XUARTPS_MODEMSR_OFFSET);
329 TmpRegister = (u16)ModemStatusRegister;
333 /****************************************************************************/
336 * This function determines if the specified UART is sending data.
338 * @param InstancePtr is a pointer to the XUartPs instance.
341 * - TRUE if the UART is sending data
342 * - FALSE if UART is not sending data
346 *****************************************************************************/
347 u32 XUartPs_IsSending(XUartPs *InstancePtr)
349 u32 ChanStatRegister;
350 u32 ChanTmpSRegister;
355 * Assert validates the input arguments
357 Xil_AssertNonvoid(InstancePtr != NULL);
358 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
361 * Read the channel status register to determine if the transmitter is
364 ChanStatRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
368 * If the transmitter is active, or the TX FIFO is not empty, then indicate
369 * that the UART is still sending some data
371 ActiveResult = ChanStatRegister & ((u32)XUARTPS_SR_TACTIVE);
372 EmptyResult = ChanStatRegister & ((u32)XUARTPS_SR_TXEMPTY);
373 ChanTmpSRegister = (((u32)XUARTPS_SR_TACTIVE) == ActiveResult) ||
374 (((u32)XUARTPS_SR_TXEMPTY) != EmptyResult);
376 return ChanTmpSRegister;
379 /****************************************************************************/
382 * This function gets the operational mode of the UART. The UART can operate
383 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
386 * @param InstancePtr is a pointer to the XUartPs instance.
390 * The operational mode is specified by constants defined in xuartps.h. The
391 * constants are named XUARTPS_OPER_MODE_*
395 *****************************************************************************/
396 u8 XUartPs_GetOperMode(XUartPs *InstancePtr)
402 * Assert validates the input arguments
404 Xil_AssertNonvoid(InstancePtr != NULL);
405 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
408 * Read the Mode register.
411 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
414 ModeRegister &= (u32)XUARTPS_MR_CHMODE_MASK;
416 * Return the constant
418 switch (ModeRegister) {
419 case XUARTPS_MR_CHMODE_NORM:
420 OperMode = XUARTPS_OPER_MODE_NORMAL;
422 case XUARTPS_MR_CHMODE_ECHO:
423 OperMode = XUARTPS_OPER_MODE_AUTO_ECHO;
425 case XUARTPS_MR_CHMODE_L_LOOP:
426 OperMode = XUARTPS_OPER_MODE_LOCAL_LOOP;
428 case XUARTPS_MR_CHMODE_R_LOOP:
429 OperMode = XUARTPS_OPER_MODE_REMOTE_LOOP;
432 OperMode = (u8) ((ModeRegister & (u32)XUARTPS_MR_CHMODE_MASK) >>
433 XUARTPS_MR_CHMODE_SHIFT);
440 /****************************************************************************/
443 * This function sets the operational mode of the UART. The UART can operate
444 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
447 * @param InstancePtr is a pointer to the XUartPs instance.
448 * @param OperationMode is the mode of the UART.
454 *****************************************************************************/
455 void XUartPs_SetOperMode(XUartPs *InstancePtr, u8 OperationMode)
460 * Assert validates the input arguments.
462 Xil_AssertVoid(InstancePtr != NULL);
463 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
464 Xil_AssertVoid(OperationMode <= XUARTPS_OPER_MODE_REMOTE_LOOP);
467 * Read the Mode register.
470 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
474 * Set the correct value by masking the bits, then ORing the const.
476 ModeRegister &= (u32)(~XUARTPS_MR_CHMODE_MASK);
478 switch (OperationMode) {
479 case XUARTPS_OPER_MODE_NORMAL:
480 ModeRegister |= (u32)XUARTPS_MR_CHMODE_NORM;
482 case XUARTPS_OPER_MODE_AUTO_ECHO:
483 ModeRegister |= (u32)XUARTPS_MR_CHMODE_ECHO;
485 case XUARTPS_OPER_MODE_LOCAL_LOOP:
486 ModeRegister |= (u32)XUARTPS_MR_CHMODE_L_LOOP;
488 case XUARTPS_OPER_MODE_REMOTE_LOOP:
489 ModeRegister |= (u32)XUARTPS_MR_CHMODE_R_LOOP;
492 /* Default case made for MISRA-C Compliance. */
496 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
501 /****************************************************************************/
504 * This function sets the Flow Delay.
505 * 0 - 3: Flow delay inactive
506 * 4 - 32: If Flow Control mode is enabled, UART_rtsN is deactivated when the
507 * receive FIFO fills to this level.
509 * @param InstancePtr is a pointer to the XUartPs instance.
513 * The Flow Delay is specified by constants defined in xuartps_hw.h. The
514 * constants are named XUARTPS_FLOWDEL*
518 *****************************************************************************/
519 u8 XUartPs_GetFlowDelay(XUartPs *InstancePtr)
524 * Assert validates the input arguments
526 Xil_AssertNonvoid(InstancePtr != NULL);
527 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
530 * Read the Mode register.
532 FdelTmpRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
533 XUARTPS_FLOWDEL_OFFSET);
536 * Return the contents of the flow delay register
538 FdelTmpRegister = (u8)(FdelTmpRegister & (u32)XUARTPS_FLOWDEL_MASK);
539 return FdelTmpRegister;
542 /****************************************************************************/
545 * This function sets the Flow Delay.
546 * 0 - 3: Flow delay inactive
547 * 4 - 63: If Flow Control mode is enabled, UART_rtsN is deactivated when the
548 * receive FIFO fills to this level.
550 * @param InstancePtr is a pointer to the XUartPs instance.
551 * @param FlowDelayValue is the Setting for the flow delay.
557 *****************************************************************************/
558 void XUartPs_SetFlowDelay(XUartPs *InstancePtr, u8 FlowDelayValue)
563 * Assert validates the input arguments
565 Xil_AssertVoid(InstancePtr != NULL);
566 Xil_AssertVoid(FlowDelayValue > (u8)XUARTPS_FLOWDEL_MASK);
567 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
570 * Set the correct value by shifting the input constant, then masking
573 FdelRegister = ((u32)FlowDelayValue) & (u32)XUARTPS_FLOWDEL_MASK;
575 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
576 XUARTPS_FLOWDEL_OFFSET, FdelRegister);
580 /****************************************************************************/
583 * This function gets the Receive Timeout of the UART.
585 * @param InstancePtr is a pointer to the XUartPs instance.
587 * @return The current setting for receive time out.
591 *****************************************************************************/
592 u8 XUartPs_GetRecvTimeout(XUartPs *InstancePtr)
598 * Assert validates the input arguments
600 Xil_AssertNonvoid(InstancePtr != NULL);
601 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
604 * Read the Receive Timeout register.
606 RtoRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
607 XUARTPS_RXTOUT_OFFSET);
610 * Return the contents of the mode register shifted appropriately
612 RtoRTmpRegister = (u8)(RtoRegister & (u32)XUARTPS_RXTOUT_MASK);
613 return RtoRTmpRegister;
616 /****************************************************************************/
619 * This function sets the Receive Timeout of the UART.
621 * @param InstancePtr is a pointer to the XUartPs instance.
622 * @param RecvTimeout setting allows the UART to detect an idle connection
623 * on the reciever data line.
624 * Timeout duration = RecvTimeout x 4 x Bit Period. 0 disables the
631 *****************************************************************************/
632 void XUartPs_SetRecvTimeout(XUartPs *InstancePtr, u8 RecvTimeout)
637 * Assert validates the input arguments
639 Xil_AssertVoid(InstancePtr != NULL);
640 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
643 * Set the correct value by masking the bits
645 RtoRegister = ((u32)RecvTimeout & (u32)XUARTPS_RXTOUT_MASK);
647 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
648 XUARTPS_RXTOUT_OFFSET, RtoRegister);
651 * Configure CR to restart the receiver timeout counter
654 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
656 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
657 (RtoRegister | XUARTPS_CR_TORST));
660 /****************************************************************************/
663 * Sets the data format for the device. The data format includes the
664 * baud rate, number of data bits, number of stop bits, and parity. It is the
665 * caller's responsibility to ensure that the UART is not sending or receiving
666 * data when this function is called.
668 * @param InstancePtr is a pointer to the XUartPs instance.
669 * @param FormatPtr is a pointer to a format structure containing the data
673 * - XST_SUCCESS if the data format was successfully set.
674 * - XST_UART_BAUD_ERROR indicates the baud rate could not be
675 * set because of the amount of error with the baud rate and
676 * the input clock frequency.
677 * - XST_INVALID_PARAM if one of the parameters was not valid.
681 * The data types in the format type, data bits and parity, are 32 bit fields
682 * to prevent a compiler warning.
683 * The asserts in this function will cause a warning if these fields are
687 *****************************************************************************/
688 s32 XUartPs_SetDataFormat(XUartPs *InstancePtr,
689 XUartPsFormat * FormatPtr)
694 Xil_AssertNonvoid(InstancePtr != NULL);
695 Xil_AssertNonvoid(FormatPtr != NULL);
696 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
699 * Verify the inputs specified are valid
701 if ((FormatPtr->DataBits > ((u32)XUARTPS_FORMAT_6_BITS)) ||
702 (FormatPtr->StopBits > ((u8)XUARTPS_FORMAT_2_STOP_BIT)) ||
703 (FormatPtr->Parity > ((u32)XUARTPS_FORMAT_NO_PARITY))) {
704 Status = XST_INVALID_PARAM;
708 * Try to set the baud rate and if it's not successful then don't
709 * continue altering the data format, this is done first to avoid the
710 * format from being altered when an error occurs
712 Status = XUartPs_SetBaudRate(InstancePtr, FormatPtr->BaudRate);
713 if (Status != (s32)XST_SUCCESS) {
718 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
722 * Set the length of data (8,7,6) by first clearing out the bits
723 * that control it in the register, then set the length in the register
725 ModeRegister &= (u32)(~XUARTPS_MR_CHARLEN_MASK);
726 ModeRegister |= (FormatPtr->DataBits << XUARTPS_MR_CHARLEN_SHIFT);
729 * Set the number of stop bits in the mode register by first clearing
730 * out the bits that control it in the register, then set the number
731 * of stop bits in the register.
733 ModeRegister &= (u32)(~XUARTPS_MR_STOPMODE_MASK);
734 ModeRegister |= (((u32)FormatPtr->StopBits) << XUARTPS_MR_STOPMODE_SHIFT);
737 * Set the parity by first clearing out the bits that control it in the
738 * register, then set the bits in the register, the default is no parity
739 * after clearing the register bits
741 ModeRegister &= (u32)(~XUARTPS_MR_PARITY_MASK);
742 ModeRegister |= (FormatPtr->Parity << XUARTPS_MR_PARITY_SHIFT);
745 * Update the mode register
747 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
750 Status = XST_SUCCESS;
756 /****************************************************************************/
759 * Gets the data format for the specified UART. The data format includes the
760 * baud rate, number of data bits, number of stop bits, and parity.
762 * @param InstancePtr is a pointer to the XUartPs instance.
763 * @param FormatPtr is a pointer to a format structure that will contain
764 * the data format after this call completes.
771 *****************************************************************************/
772 void XUartPs_GetDataFormat(XUartPs *InstancePtr, XUartPsFormat * FormatPtr)
778 * Assert validates the input arguments
780 Xil_AssertVoid(InstancePtr != NULL);
781 Xil_AssertVoid(FormatPtr != NULL);
782 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
785 * Get the baud rate from the instance, this is not retrieved from the
786 * hardware because it is only kept as a divisor such that it is more
787 * difficult to get back to the baud rate
789 FormatPtr->BaudRate = InstancePtr->BaudRate;
791 ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
795 * Get the length of data (8,7,6,5)
797 FormatPtr->DataBits =
798 ((ModeRegister & (u32)XUARTPS_MR_CHARLEN_MASK) >>
799 XUARTPS_MR_CHARLEN_SHIFT);
802 * Get the number of stop bits
804 FormatPtr->StopBits =
805 (u8)((ModeRegister & (u32)XUARTPS_MR_STOPMODE_MASK) >>
806 XUARTPS_MR_STOPMODE_SHIFT);
809 * Determine what parity is
812 (u32)((ModeRegister & (u32)XUARTPS_MR_PARITY_MASK) >>
813 XUARTPS_MR_PARITY_SHIFT);