1 /******************************************************************************
3 * Copyright (C) 2010 - 2014 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.
50 *****************************************************************************/
52 /***************************** Include Files ********************************/
56 /************************** Constant Definitions ****************************/
58 /**************************** Type Definitions ******************************/
60 /***************** Macros (Inline Functions) Definitions ********************/
62 /************************** Variable Definitions ****************************/
64 * The following data type is a map from an option to the offset in the
65 * register to which it belongs as well as its bit mask in that register.
74 * Create the table which contains options which are to be processed to get/set
75 * the options. These options are table driven to allow easy maintenance and
76 * expansion of the options.
79 static Mapping OptionsTable[] = {
80 {XUARTPS_OPTION_SET_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STARTBRK},
81 {XUARTPS_OPTION_STOP_BREAK, XUARTPS_CR_OFFSET, XUARTPS_CR_STOPBRK},
82 {XUARTPS_OPTION_RESET_TMOUT, XUARTPS_CR_OFFSET, XUARTPS_CR_TORST},
83 {XUARTPS_OPTION_RESET_TX, XUARTPS_CR_OFFSET, XUARTPS_CR_TXRST},
84 {XUARTPS_OPTION_RESET_RX, XUARTPS_CR_OFFSET, XUARTPS_CR_RXRST},
85 {XUARTPS_OPTION_ASSERT_RTS, XUARTPS_MODEMCR_OFFSET,
87 {XUARTPS_OPTION_ASSERT_DTR, XUARTPS_MODEMCR_OFFSET,
89 {XUARTPS_OPTION_SET_FCM, XUARTPS_MODEMCR_OFFSET, XUARTPS_MODEMCR_FCM}
92 /* Create a constant for the number of entries in the table */
94 #define XUARTPS_NUM_OPTIONS (sizeof(OptionsTable) / sizeof(Mapping))
96 /************************** Function Prototypes *****************************/
98 /****************************************************************************/
101 * Gets the options for the specified driver instance. The options are
102 * implemented as bit masks such that multiple options may be enabled or
103 * disabled simulataneously.
105 * @param InstancePtr is a pointer to the XUartPs instance.
109 * The current options for the UART. The optionss are bit masks that are
110 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
114 *****************************************************************************/
115 u16 XUartPs_GetOptions(XUartPs *InstancePtr)
122 * Assert validates the input arguments
124 Xil_AssertNonvoid(InstancePtr != NULL);
125 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
128 * Loop thru the options table to map the physical options in the
129 * registers of the UART to the logical options to be returned
131 for (Index = 0; Index < XUARTPS_NUM_OPTIONS; Index++) {
132 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
137 * If the bit in the register which correlates to the option
138 * is set, then set the corresponding bit in the options,
139 * ignoring any bits which are zero since the options variable
140 * is initialized to zero
142 if (Register & OptionsTable[Index].Mask) {
143 Options |= OptionsTable[Index].Option;
150 /****************************************************************************/
153 * Sets the options for the specified driver instance. The options are
154 * implemented as bit masks such that multiple options may be enabled or
155 * disabled simultaneously.
157 * The GetOptions function may be called to retrieve the currently enabled
158 * options. The result is ORed in the desired new settings to be enabled and
159 * ANDed with the inverse to clear the settings to be disabled. The resulting
160 * value is then used as the options for the SetOption function call.
162 * @param InstancePtr is a pointer to the XUartPs instance.
163 * @param Options contains the options to be set which are bit masks
164 * contained in the file xuartps.h and named XUARTPS_OPTION_*.
170 *****************************************************************************/
171 void XUartPs_SetOptions(XUartPs *InstancePtr, u16 Options)
177 * Assert validates the input arguments
179 Xil_AssertVoid(InstancePtr != NULL);
180 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
183 * Loop thru the options table to map the logical options to the
184 * physical options in the registers of the UART.
186 for (Index = 0; Index < XUARTPS_NUM_OPTIONS; Index++) {
189 * Read the register which contains option so that the register
190 * can be changed without destoying any other bits of the
193 Register = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
198 * If the option is set in the input, then set the corresponding
199 * bit in the specified register, otherwise clear the bit in
202 if (Options & OptionsTable[Index].Option) {
203 Register |= OptionsTable[Index].Mask;
206 Register &= ~OptionsTable[Index].Mask;
209 /* Write the new value to the register to set the option */
210 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
211 OptionsTable[Index].RegisterOffset,
217 /****************************************************************************/
220 * This function gets the receive FIFO trigger level. The receive trigger
221 * level indicates the number of bytes in the receive FIFO that cause a receive
222 * data event (interrupt) to be generated.
224 * @param InstancePtr is a pointer to the XUartPs instance.
226 * @return The current receive FIFO trigger level. This is a value
231 *****************************************************************************/
232 u8 XUartPs_GetFifoThreshold(XUartPs *InstancePtr)
237 * Assert validates the input arguments
239 Xil_AssertNonvoid(InstancePtr != NULL);
240 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
243 * Read the value of the FIFO control register so that the threshold
244 * can be retrieved, this read takes special register processing
246 RtrigRegister = (u8) XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
247 XUARTPS_RXWM_OFFSET);
249 /* Return only the trigger level from the register value */
251 return (RtrigRegister & XUARTPS_RXWM_MASK);
254 /****************************************************************************/
257 * This functions sets the receive FIFO trigger level. The receive trigger
258 * level specifies the number of bytes in the receive FIFO that cause a receive
259 * data event (interrupt) to be generated.
261 * @param InstancePtr is a pointer to the XUartPs instance.
262 * @param TriggerLevel contains the trigger level to set.
268 *****************************************************************************/
269 void XUartPs_SetFifoThreshold(XUartPs *InstancePtr, u8 TriggerLevel)
274 * Assert validates the input arguments
276 Xil_AssertVoid(InstancePtr != NULL);
277 Xil_AssertVoid(TriggerLevel <= XUARTPS_RXWM_MASK);
278 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
280 RtrigRegister = TriggerLevel & XUARTPS_RXWM_MASK;
283 * Write the new value for the FIFO control register to it such that the
284 * threshold is changed
286 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
287 XUARTPS_RXWM_OFFSET, RtrigRegister);
291 /****************************************************************************/
294 * This function gets the modem status from the specified UART. The modem
295 * status indicates any changes of the modem signals. This function allows
296 * the modem status to be read in a polled mode. The modem status is updated
297 * whenever it is read such that reading it twice may not yield the same
300 * @param InstancePtr is a pointer to the XUartPs instance.
304 * The modem status which are bit masks that are contained in the file
305 * xuartps.h and named XUARTPS_MODEM_*.
309 * The bit masks used for the modem status are the exact bits of the modem
310 * status register with no abstraction.
312 *****************************************************************************/
313 u16 XUartPs_GetModemStatus(XUartPs *InstancePtr)
315 u32 ModemStatusRegister;
318 * Assert validates the input arguments
320 Xil_AssertNonvoid(InstancePtr != NULL);
321 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
323 /* Read the modem status register to return
325 ModemStatusRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
326 XUARTPS_MODEMSR_OFFSET);
327 return ModemStatusRegister;
330 /****************************************************************************/
333 * This function determines if the specified UART is sending data.
335 * @param InstancePtr is a pointer to the XUartPs instance.
338 * - TRUE if the UART is sending data
339 * - FALSE if UART is not sending data
343 *****************************************************************************/
344 u32 XUartPs_IsSending(XUartPs *InstancePtr)
346 u32 ChanStatRegister;
349 * Assert validates the input arguments
351 Xil_AssertNonvoid(InstancePtr != NULL);
352 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
355 * Read the channel status register to determine if the transmitter is
358 ChanStatRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
362 * If the transmitter is active, or the TX FIFO is not empty, then indicate
363 * that the UART is still sending some data
365 return ((XUARTPS_SR_TACTIVE == (ChanStatRegister &
366 XUARTPS_SR_TACTIVE)) ||
367 (XUARTPS_SR_TXEMPTY != (ChanStatRegister &
368 XUARTPS_SR_TXEMPTY)));
371 /****************************************************************************/
374 * This function gets the operational mode of the UART. The UART can operate
375 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
378 * @param InstancePtr is a pointer to the XUartPs instance.
382 * The operational mode is specified by constants defined in xuartps.h. The
383 * constants are named XUARTPS_OPER_MODE_*
387 *****************************************************************************/
388 u8 XUartPs_GetOperMode(XUartPs *InstancePtr)
394 * Assert validates the input arguments
396 Xil_AssertNonvoid(InstancePtr != NULL);
397 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
400 * Read the Mode register.
403 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
406 ModeRegister &= XUARTPS_MR_CHMODE_MASK;
408 * Return the constant
410 switch (ModeRegister) {
411 case XUARTPS_MR_CHMODE_NORM:
412 OperMode = XUARTPS_OPER_MODE_NORMAL;
414 case XUARTPS_MR_CHMODE_ECHO:
415 OperMode = XUARTPS_OPER_MODE_AUTO_ECHO;
417 case XUARTPS_MR_CHMODE_L_LOOP:
418 OperMode = XUARTPS_OPER_MODE_LOCAL_LOOP;
420 case XUARTPS_MR_CHMODE_R_LOOP:
421 OperMode = XUARTPS_OPER_MODE_REMOTE_LOOP;
424 OperMode = (u8) ((ModeRegister & XUARTPS_MR_CHMODE_MASK) >>
425 XUARTPS_MR_CHMODE_SHIFT);
431 /****************************************************************************/
434 * This function sets the operational mode of the UART. The UART can operate
435 * in one of four modes: Normal, Local Loopback, Remote Loopback, or automatic
438 * @param InstancePtr is a pointer to the XUartPs instance.
439 * @param OperationMode is the mode of the UART.
445 *****************************************************************************/
446 void XUartPs_SetOperMode(XUartPs *InstancePtr, u8 OperationMode)
451 * Assert validates the input arguments.
453 Xil_AssertVoid(InstancePtr != NULL);
454 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
455 Xil_AssertVoid(OperationMode <= XUARTPS_OPER_MODE_REMOTE_LOOP);
458 * Read the Mode register.
461 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
465 * Set the correct value by masking the bits, then ORing the const.
467 ModeRegister &= ~XUARTPS_MR_CHMODE_MASK;
469 switch (OperationMode) {
470 case XUARTPS_OPER_MODE_NORMAL:
471 ModeRegister |= XUARTPS_MR_CHMODE_NORM;
473 case XUARTPS_OPER_MODE_AUTO_ECHO:
474 ModeRegister |= XUARTPS_MR_CHMODE_ECHO;
476 case XUARTPS_OPER_MODE_LOCAL_LOOP:
477 ModeRegister |= XUARTPS_MR_CHMODE_L_LOOP;
479 case XUARTPS_OPER_MODE_REMOTE_LOOP:
480 ModeRegister |= XUARTPS_MR_CHMODE_R_LOOP;
484 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
489 /****************************************************************************/
492 * This function sets the Flow Delay.
493 * 0 - 3: Flow delay inactive
494 * 4 - 32: If Flow Control mode is enabled, UART_rtsN is deactivated when the
495 * receive FIFO fills to this level.
497 * @param InstancePtr is a pointer to the XUartPs instance.
501 * The Flow Delay is specified by constants defined in xuartps_hw.h. The
502 * constants are named XUARTPS_FLOWDEL*
506 *****************************************************************************/
507 u8 XUartPs_GetFlowDelay(XUartPs *InstancePtr)
512 * Assert validates the input arguments
514 Xil_AssertNonvoid(InstancePtr != NULL);
515 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
518 * Read the Mode register.
520 FdelRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
521 XUARTPS_FLOWDEL_OFFSET);
524 * Return the contents of the flow delay register
526 return (u8) (FdelRegister & XUARTPS_FLOWDEL_MASK);
529 /****************************************************************************/
532 * This function sets the Flow Delay.
533 * 0 - 3: Flow delay inactive
534 * 4 - 63: If Flow Control mode is enabled, UART_rtsN is deactivated when the
535 * receive FIFO fills to this level.
537 * @param InstancePtr is a pointer to the XUartPs instance.
538 * @param FlowDelayValue is the Setting for the flow delay.
544 *****************************************************************************/
545 void XUartPs_SetFlowDelay(XUartPs *InstancePtr, u8 FlowDelayValue)
550 * Assert validates the input arguments
552 Xil_AssertVoid(InstancePtr != NULL);
553 Xil_AssertVoid(FlowDelayValue > XUARTPS_FLOWDEL_MASK);
554 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
557 * Set the correct value by shifting the input constant, then masking
560 FdelRegister = (FlowDelayValue & XUARTPS_FLOWDEL_MASK);
562 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
563 XUARTPS_FLOWDEL_OFFSET, FdelRegister);
567 /****************************************************************************/
570 * This function gets the Receive Timeout of the UART.
572 * @param InstancePtr is a pointer to the XUartPs instance.
574 * @return The current setting for receive time out.
578 *****************************************************************************/
579 u8 XUartPs_GetRecvTimeout(XUartPs *InstancePtr)
584 * Assert validates the input arguments
586 Xil_AssertNonvoid(InstancePtr != NULL);
587 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
590 * Read the Recieve Timeout register.
592 RtoRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
593 XUARTPS_RXTOUT_OFFSET);
596 * Return the contents of the mode register shifted appropriately
598 return (RtoRegister & XUARTPS_RXTOUT_MASK);
601 /****************************************************************************/
604 * This function sets the Receive Timeout of the UART.
606 * @param InstancePtr is a pointer to the XUartPs instance.
607 * @param RecvTimeout setting allows the UART to detect an idle connection
608 * on the reciever data line.
609 * Timeout duration = RecvTimeout x 4 x Bit Period. 0 disables the
616 *****************************************************************************/
617 void XUartPs_SetRecvTimeout(XUartPs *InstancePtr, u8 RecvTimeout)
622 * Assert validates the input arguments
624 Xil_AssertVoid(InstancePtr != NULL);
625 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
628 * Set the correct value by masking the bits
630 RtoRegister = (RecvTimeout & XUARTPS_RXTOUT_MASK);
632 XUartPs_WriteReg(InstancePtr->Config.BaseAddress,
633 XUARTPS_RXTOUT_OFFSET, RtoRegister);
636 * Configure CR to restart the receiver timeout counter
639 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
641 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_CR_OFFSET,
642 (RtoRegister | XUARTPS_CR_TORST));
645 /****************************************************************************/
648 * Sets the data format for the device. The data format includes the
649 * baud rate, number of data bits, number of stop bits, and parity. It is the
650 * caller's responsibility to ensure that the UART is not sending or receiving
651 * data when this function is called.
653 * @param InstancePtr is a pointer to the XUartPs instance.
654 * @param FormatPtr is a pointer to a format structure containing the data
658 * - XST_SUCCESS if the data format was successfully set.
659 * - XST_UART_BAUD_ERROR indicates the baud rate could not be
660 * set because of the amount of error with the baud rate and
661 * the input clock frequency.
662 * - XST_INVALID_PARAM if one of the parameters was not valid.
666 * The data types in the format type, data bits and parity, are 32 bit fields
667 * to prevent a compiler warning.
668 * The asserts in this function will cause a warning if these fields are
672 *****************************************************************************/
673 int XUartPs_SetDataFormat(XUartPs *InstancePtr,
674 XUartPsFormat * FormatPtr)
679 Xil_AssertNonvoid(InstancePtr != NULL);
680 Xil_AssertNonvoid(FormatPtr != NULL);
681 Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
684 * Verify the inputs specified are valid
686 if ((FormatPtr->DataBits > XUARTPS_FORMAT_6_BITS) ||
687 (FormatPtr->StopBits > XUARTPS_FORMAT_2_STOP_BIT) ||
688 (FormatPtr->Parity > XUARTPS_FORMAT_NO_PARITY)) {
689 return XST_INVALID_PARAM;
693 * Try to set the baud rate and if it's not successful then don't
694 * continue altering the data format, this is done first to avoid the
695 * format from being altered when an error occurs
697 Status = XUartPs_SetBaudRate(InstancePtr, FormatPtr->BaudRate);
698 if (Status != XST_SUCCESS) {
703 XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
707 * Set the length of data (8,7,6) by first clearing out the bits
708 * that control it in the register, then set the length in the register
710 ModeRegister &= ~XUARTPS_MR_CHARLEN_MASK;
711 ModeRegister |= (FormatPtr->DataBits << XUARTPS_MR_CHARLEN_SHIFT);
714 * Set the number of stop bits in the mode register by first clearing
715 * out the bits that control it in the register, then set the number
716 * of stop bits in the register.
718 ModeRegister &= ~XUARTPS_MR_STOPMODE_MASK;
719 ModeRegister |= (FormatPtr->StopBits << XUARTPS_MR_STOPMODE_SHIFT);
722 * Set the parity by first clearing out the bits that control it in the
723 * register, then set the bits in the register, the default is no parity
724 * after clearing the register bits
726 ModeRegister &= ~XUARTPS_MR_PARITY_MASK;
727 ModeRegister |= (FormatPtr->Parity << XUARTPS_MR_PARITY_SHIFT);
730 * Update the mode register
732 XUartPs_WriteReg(InstancePtr->Config.BaseAddress, XUARTPS_MR_OFFSET,
738 /****************************************************************************/
741 * Gets the data format for the specified UART. The data format includes the
742 * baud rate, number of data bits, number of stop bits, and parity.
744 * @param InstancePtr is a pointer to the XUartPs instance.
745 * @param FormatPtr is a pointer to a format structure that will contain
746 * the data format after this call completes.
753 *****************************************************************************/
754 void XUartPs_GetDataFormat(XUartPs *InstancePtr, XUartPsFormat * FormatPtr)
760 * Assert validates the input arguments
762 Xil_AssertVoid(InstancePtr != NULL);
763 Xil_AssertVoid(FormatPtr != NULL);
764 Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);
767 * Get the baud rate from the instance, this is not retrieved from the
768 * hardware because it is only kept as a divisor such that it is more
769 * difficult to get back to the baud rate
771 FormatPtr->BaudRate = InstancePtr->BaudRate;
773 ModeRegister = XUartPs_ReadReg(InstancePtr->Config.BaseAddress,
777 * Get the length of data (8,7,6,5)
779 FormatPtr->DataBits =
780 (ModeRegister & XUARTPS_MR_CHARLEN_MASK) >>
781 XUARTPS_MR_CHARLEN_SHIFT;
784 * Get the number of stop bits
786 FormatPtr->StopBits =
787 (ModeRegister & XUARTPS_MR_STOPMODE_MASK) >>
788 XUARTPS_MR_STOPMODE_SHIFT;
791 * Determine what parity is
794 (ModeRegister & XUARTPS_MR_PARITY_MASK) >>
795 XUARTPS_MR_PARITY_SHIFT;