1 //*****************************************************************************
3 // uart.c - Driver for the UART.
5 // Copyright (c) 2005,2006 Luminary Micro, Inc. All rights reserved.
7 // Software License Agreement
9 // Luminary Micro, Inc. (LMI) is supplying this software for use solely and
10 // exclusively on LMI's Stellaris Family of microcontroller products.
12 // The software is owned by LMI and/or its suppliers, and is protected under
13 // applicable copyright laws. All rights are reserved. Any use in violation
14 // of the foregoing restrictions may subject the user to criminal sanctions
15 // under applicable laws, as well as to civil liability for the breach of the
16 // terms and conditions of this license.
18 // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
19 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
21 // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
22 // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
24 // This is part of revision 991 of the Stellaris Driver Library.
26 //*****************************************************************************
28 //*****************************************************************************
30 //! \addtogroup uart_api
33 //*****************************************************************************
35 #include "../hw_ints.h"
36 #include "../hw_memmap.h"
37 #include "../hw_types.h"
38 #include "../hw_uart.h"
40 #include "interrupt.h"
44 //*****************************************************************************
46 //! Sets the type of parity.
48 //! \param ulBase is the base address of the UART port.
49 //! \param ulParity specifies the type of parity to use.
51 //! Sets the type of parity to use for transmitting and expect when receiving.
52 //! The \e ulParity parameter must be one of \b UART_CONFIG_PAR_NONE,
53 //! \b UART_CONFIG_PAR_EVEN, \b UART_CONFIG_PAR_ODD, \b UART_CONFIG_PAR_ONE,
54 //! or \b UART_CONFIG_PAR_ZERO. The last two allow direct control of the
55 //! parity bit; it will always be either be one or zero based on the mode.
59 //*****************************************************************************
60 #if defined(GROUP_paritymodeset) || defined(BUILD_ALL) || defined(DOXYGEN)
62 UARTParityModeSet(unsigned long ulBase, unsigned long ulParity)
65 // Check the arguments.
67 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
68 ASSERT((ulParity == UART_CONFIG_PAR_NONE) ||
69 (ulParity == UART_CONFIG_PAR_EVEN) ||
70 (ulParity == UART_CONFIG_PAR_ODD) ||
71 (ulParity == UART_CONFIG_PAR_ONE) ||
72 (ulParity == UART_CONFIG_PAR_ZERO));
75 // Set the parity mode.
77 HWREG(ulBase + UART_O_LCR_H) = ((HWREG(ulBase + UART_O_LCR_H) &
78 ~(UART_LCR_H_SPS | UART_LCR_H_EPS |
79 UART_LCR_H_PEN)) | ulParity);
83 //*****************************************************************************
85 //! Gets the type of parity currently being used.
87 //! \param ulBase is the base address of the UART port.
89 //! \return The current parity settings, specified as one of
90 //! \b UART_CONFIG_PAR_NONE, \b UART_CONFIG_PAR_EVEN, \b UART_CONFIG_PAR_ODD,
91 //! \b UART_CONFIG_PAR_ONE, or \b UART_CONFIG_PAR_ZERO.
93 //*****************************************************************************
94 #if defined(GROUP_paritymodeget) || defined(BUILD_ALL) || defined(DOXYGEN)
96 UARTParityModeGet(unsigned long ulBase)
99 // Check the arguments.
101 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
104 // Return the current parity setting.
106 return(HWREG(ulBase + UART_O_LCR_H) &
107 (UART_LCR_H_SPS | UART_LCR_H_EPS | UART_LCR_H_PEN));
111 //*****************************************************************************
113 //! Sets the configuration of a UART.
115 //! \param ulBase is the base address of the UART port.
116 //! \param ulBaud is the desired baud rate.
117 //! \param ulConfig is the data format for the port (number of data bits,
118 //! number of stop bits, and parity).
120 //! This function will configure the UART for operation in the specified data
121 //! format. The baud rate is provided in the \e ulBaud parameter and the
122 //! data format in the \e ulConfig parameter.
124 //! The \e ulConfig parameter is the logical OR of three values: the number of
125 //! data bits, the number of stop bits, and the parity. \b UART_CONFIG_WLEN_8,
126 //! \b UART_CONFIG_WLEN_7, \b UART_CONFIG_WLEN_6, and \b UART_CONFIG_WLEN_5
127 //! select from eight to five data bits per byte (respectively).
128 //! \b UART_CONFIG_STOP_ONE and \b UART_CONFIG_STOP_TWO select one or two stop
129 //! bits (respectively). \b UART_CONFIG_PAR_NONE, \b UART_CONFIG_PAR_EVEN,
130 //! \b UART_CONFIG_PAR_ODD, \b UART_CONFIG_PAR_ONE, and \b UART_CONFIG_PAR_ZERO
131 //! select the parity mode (no parity bit, even parity bit, odd parity bit,
132 //! parity bit always one, and parity bit always zero, respectively).
134 //! The baud rate is dependent upon the system clock rate returned by
135 //! SysCtlClockGet(); if it does not return the correct system clock rate then
136 //! the baud rate will be incorrect.
140 //*****************************************************************************
141 #if defined(GROUP_configset) || defined(BUILD_ALL) || defined(DOXYGEN)
143 UARTConfigSet(unsigned long ulBase, unsigned long ulBaud,
144 unsigned long ulConfig)
146 unsigned long ulUARTClk, ulInt, ulFrac;
149 // Check the arguments.
151 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
159 // Determine the UART clock rate.
161 ulUARTClk = SysCtlClockGet();
164 // Compute the fractional baud rate divider.
166 ulInt = ulUARTClk / (16 * ulBaud);
167 ulFrac = ulUARTClk % (16 * ulBaud);
168 ulFrac = ((((2 * ulFrac * 4) / ulBaud) + 1) / 2);
171 // Set the baud rate.
173 HWREG(ulBase + UART_O_IBRD) = ulInt;
174 HWREG(ulBase + UART_O_FBRD) = ulFrac;
177 // Set parity, data length, and number of stop bits.
179 HWREG(ulBase + UART_O_LCR_H) = ulConfig;
182 // Clear the flags register.
184 HWREG(ulBase + UART_O_FR) = 0;
193 //*****************************************************************************
195 //! Gets the current configuration of a UART.
197 //! \param ulBase is the base address of the UART port.
198 //! \param pulBaud is a pointer to storage for the baud rate.
199 //! \param pulConfig is a pointer to storage for the data format.
201 //! The baud rate and data format for the UART is determined. The returned
202 //! baud rate is the actual baud rate; it may not be the exact baud rate
203 //! requested or an ``official'' baud rate. The data format returned in
204 //! \e pulConfig is enumerated the same as the \e ulConfig parameter of
207 //! The baud rate is dependent upon the system clock rate returned by
208 //! SysCtlClockGet(); if it does not return the correct system clock rate then
209 //! the baud rate will be computed incorrectly.
213 //*****************************************************************************
214 #if defined(GROUP_configget) || defined(BUILD_ALL) || defined(DOXYGEN)
216 UARTConfigGet(unsigned long ulBase, unsigned long *pulBaud,
217 unsigned long *pulConfig)
220 unsigned long ulInt, ulFrac;
223 // Check the arguments.
225 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
228 // Compute the baud rate.
230 ulInt = HWREG(ulBase + UART_O_IBRD);
231 ulFrac = HWREG(ulBase + UART_O_FBRD);
232 *pulBaud = (SysCtlClockGet() * 4) / ((64 * ulInt) + ulFrac);
235 // Get the parity, data length, and number of stop bits.
237 *pulConfig = (HWREG(ulBase + UART_O_LCR_H) &
238 (UART_LCR_H_SPS | UART_LCR_H_WLEN | UART_LCR_H_STP2 |
239 UART_LCR_H_EPS | UART_LCR_H_PEN));
243 //*****************************************************************************
245 //! Enables transmitting and receiving.
247 //! \param ulBase is the base address of the UART port.
249 //! Sets the UARTEN, TXE, and RXE bits, and enables the transmit and receive
254 //*****************************************************************************
255 #if defined(GROUP_enable) || defined(BUILD_ALL) || defined(DOXYGEN)
257 UARTEnable(unsigned long ulBase)
260 // Check the arguments.
262 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
267 HWREG(ulBase + UART_O_LCR_H) |= UART_LCR_H_FEN;
270 // Enable RX, TX, and the UART.
272 HWREG(ulBase + UART_O_CTL) |= (UART_CTL_UARTEN | UART_CTL_TXE |
277 //*****************************************************************************
279 //! Disables transmitting and receiving.
281 //! \param ulBase is the base address of the UART port.
283 //! Clears the UARTEN, TXE, and RXE bits, then waits for the end of
284 //! transmission of the current character, and flushes the transmit FIFO.
288 //*****************************************************************************
289 #if defined(GROUP_disable) || defined(BUILD_ALL) || defined(DOXYGEN)
291 UARTDisable(unsigned long ulBase)
294 // Check the arguments.
296 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
299 // Wait for end of TX.
301 while(HWREG(ulBase + UART_O_FR) & UART_FR_BUSY)
308 HWREG(ulBase + UART_O_LCR_H) &= ~(UART_LCR_H_FEN);
313 HWREG(ulBase + UART_O_CTL) &= ~(UART_CTL_UARTEN | UART_CTL_TXE |
318 //*****************************************************************************
320 //! Determines if there are any characters in the receive FIFO.
322 //! \param ulBase is the base address of the UART port.
324 //! This function returns a flag indicating whether or not there is data
325 //! available in the receive FIFO.
327 //! \return Returns \b true if there is data in the receive FIFO, and \b false
328 //! if there is no data in the receive FIFO.
330 //*****************************************************************************
331 #if defined(GROUP_charsavail) || defined(BUILD_ALL) || defined(DOXYGEN)
333 UARTCharsAvail(unsigned long ulBase)
336 // Check the arguments.
338 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
341 // Return the availability of characters.
343 return((HWREG(ulBase + UART_O_FR) & UART_FR_RXFE) ? false : true);
347 //*****************************************************************************
349 //! Determines if there is any space in the transmit FIFO.
351 //! \param ulBase is the base address of the UART port.
353 //! This function returns a flag indicating whether or not there is space
354 //! available in the transmit FIFO.
356 //! \return Returns \b true if there is space available in the transmit FIFO,
357 //! and \b false if there is no space available in the transmit FIFO.
359 //*****************************************************************************
360 #if defined(GROUP_spaceavail) || defined(BUILD_ALL) || defined(DOXYGEN)
362 UARTSpaceAvail(unsigned long ulBase)
365 // Check the arguments.
367 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
370 // Return the availability of space.
372 return((HWREG(ulBase + UART_O_FR) & UART_FR_TXFF) ? false : true);
376 //*****************************************************************************
378 //! Receives a character from the specified port.
380 //! \param ulBase is the base address of the UART port.
382 //! Gets a character from the receive FIFO for the specified port.
384 //! \return Returns the character read from the specified port, cast as a
385 //! \e long. A \b -1 will be returned if there are no characters present in
386 //! the receive FIFO. The UARTCharsAvail() function should be called before
387 //! attempting to call this function.
389 //*****************************************************************************
390 #if defined(GROUP_charnonblockingget) || defined(BUILD_ALL) || defined(DOXYGEN)
392 UARTCharNonBlockingGet(unsigned long ulBase)
395 // Check the arguments.
397 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
400 // See if there are any characters in the receive FIFO.
402 if(!(HWREG(ulBase + UART_O_FR) & UART_FR_RXFE))
405 // Read and return the next character.
407 return(HWREG(ulBase + UART_O_DR));
412 // There are no characters, so return a failure.
419 //*****************************************************************************
421 //! Waits for a character from the specified port.
423 //! \param ulBase is the base address of the UART port.
425 //! Gets a character from the receive FIFO for the specified port. If there
426 //! are no characters available, this function will wait until a character is
427 //! received before returning.
429 //! \return Returns the character read from the specified port, cast as an
432 //*****************************************************************************
433 #if defined(GROUP_charget) || defined(BUILD_ALL) || defined(DOXYGEN)
435 UARTCharGet(unsigned long ulBase)
438 // Check the arguments.
440 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
443 // Wait until a char is available.
445 while(HWREG(ulBase + UART_O_FR) & UART_FR_RXFE)
452 return(HWREG(ulBase + UART_O_DR));
456 //*****************************************************************************
458 //! Sends a character to the specified port.
460 //! \param ulBase is the base address of the UART port.
461 //! \param ucData is the character to be transmitted.
463 //! Writes the character \e ucData to the transmit FIFO for the specified port.
464 //! This function does not block, so if there is no space available, then a
465 //! \b false is returned, and the application will have to retry the function
468 //! \return Returns \b true if the character was successfully placed in the
469 //! transmit FIFO, and \b false if there was no space available in the transmit
472 //*****************************************************************************
473 #if defined(GROUP_charnonblockingput) || defined(BUILD_ALL) || defined(DOXYGEN)
475 UARTCharNonBlockingPut(unsigned long ulBase, unsigned char ucData)
478 // Check the arguments.
480 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
483 // See if there is space in the transmit FIFO.
485 if(!(HWREG(ulBase + UART_O_FR) & UART_FR_TXFF))
488 // Write this character to the transmit FIFO.
490 HWREG(ulBase + UART_O_DR) = ucData;
500 // There is no space in the transmit FIFO, so return a failure.
507 //*****************************************************************************
509 //! Waits to send a character from the specified port.
511 //! \param ulBase is the base address of the UART port.
512 //! \param ucData is the character to be transmitted.
514 //! Sends the character \e ucData to the transmit FIFO for the specified port.
515 //! If there is no space available in the transmit FIFO, this function will
516 //! wait until there is space available before returning.
520 //*****************************************************************************
521 #if defined(GROUP_charput) || defined(BUILD_ALL) || defined(DOXYGEN)
523 UARTCharPut(unsigned long ulBase, unsigned char ucData)
526 // Check the arguments.
528 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
531 // Wait until space is available.
533 while(HWREG(ulBase + UART_O_FR) & UART_FR_TXFF)
540 HWREG(ulBase + UART_O_DR) = ucData;
544 //*****************************************************************************
546 //! Causes a BREAK to be sent.
548 //! \param ulBase is the base address of the UART port.
549 //! \param bBreakState controls the output level.
551 //! Calling this function with \e bBreakState set to \b true will assert a
552 //! break condition on the UART. Calling this function with \e bBreakState set
553 //! to \b false will remove the break condition. For proper transmission of a
554 //! break command, the break must be asserted for at least two complete frames.
558 //*****************************************************************************
559 #if defined(GROUP_breakctl) || defined(BUILD_ALL) || defined(DOXYGEN)
561 UARTBreakCtl(unsigned long ulBase, tBoolean bBreakState)
564 // Check the arguments.
566 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
569 // Set the break condition as requested.
571 HWREG(ulBase + UART_O_LCR_H) =
573 (HWREG(ulBase + UART_O_LCR_H) | UART_LCR_H_BRK) :
574 (HWREG(ulBase + UART_O_LCR_H) & ~(UART_LCR_H_BRK)));
578 //*****************************************************************************
580 //! Registers an interrupt handler for a UART interrupt.
582 //! \param ulBase is the base address of the UART port.
583 //! \param pfnHandler is a pointer to the function to be called when the
584 //! UART interrupt occurs.
586 //! This function does the actual registering of the interrupt handler. This
587 //! will enable the global interrupt in the interrupt controller; specific UART
588 //! interrupts must be enabled via UARTIntEnable(). It is the interrupt
589 //! handler's responsibility to clear the interrupt source.
591 //! \sa IntRegister() for important information about registering interrupt
596 //*****************************************************************************
597 #if defined(GROUP_intregister) || defined(BUILD_ALL) || defined(DOXYGEN)
599 UARTIntRegister(unsigned long ulBase, void (*pfnHandler)(void))
604 // Check the arguments.
606 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
609 // Determine the interrupt number based on the UART port.
611 ulInt = (ulBase == UART0_BASE) ? INT_UART0 : INT_UART1;
614 // Register the interrupt handler.
616 IntRegister(ulInt, pfnHandler);
619 // Enable the UART interrupt.
625 //*****************************************************************************
627 //! Unregisters an interrupt handler for a UART interrupt.
629 //! \param ulBase is the base address of the UART port.
631 //! This function does the actual unregistering of the interrupt handler. It
632 //! will clear the handler to be called when a UART interrupt occurs. This
633 //! will also mask off the interrupt in the interrupt controller so that the
634 //! interrupt handler no longer is called.
636 //! \sa IntRegister() for important information about registering interrupt
641 //*****************************************************************************
642 #if defined(GROUP_intunregister) || defined(BUILD_ALL) || defined(DOXYGEN)
644 UARTIntUnregister(unsigned long ulBase)
649 // Check the arguments.
651 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
654 // Determine the interrupt number based on the UART port.
656 ulInt = (ulBase == UART0_BASE) ? INT_UART0 : INT_UART1;
659 // Disable the interrupt.
664 // Unregister the interrupt handler.
666 IntUnregister(ulInt);
670 //*****************************************************************************
672 //! Enables individual UART interrupt sources.
674 //! \param ulBase is the base address of the UART port.
675 //! \param ulIntFlags is the bit mask of the interrupt sources to be enabled.
677 //! Enables the indicated UART interrupt sources. Only the sources that are
678 //! enabled can be reflected to the processor interrupt; disabled sources have
679 //! no effect on the processor.
681 //! The parameter \e ulIntFlags is the logical OR of any of the following:
683 //! - UART_INT_OE - Overrun Error interrupt
684 //! - UART_INT_BE - Break Error interrupt
685 //! - UART_INT_PE - Parity Error interrupt
686 //! - UART_INT_FE - Framing Error interrupt
687 //! - UART_INT_RT - Receive Timeout interrupt
688 //! - UART_INT_TX - Transmit interrupt
689 //! - UART_INT_RX - Receive interrupt
693 //*****************************************************************************
694 #if defined(GROUP_intenable) || defined(BUILD_ALL) || defined(DOXYGEN)
696 UARTIntEnable(unsigned long ulBase, unsigned long ulIntFlags)
699 // Check the arguments.
701 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
704 // Enable the specified interrupts.
706 HWREG(ulBase + UART_O_IM) |= ulIntFlags;
710 //*****************************************************************************
712 //! Disables individual UART interrupt sources.
714 //! \param ulBase is the base address of the UART port.
715 //! \param ulIntFlags is the bit mask of the interrupt sources to be disabled.
717 //! Disables the indicated UART interrupt sources. Only the sources that are
718 //! enabled can be reflected to the processor interrupt; disabled sources have
719 //! no effect on the processor.
721 //! The parameter \e ulIntFlags has the same definition as the same parameter
722 //! to UARTIntEnable().
726 //*****************************************************************************
727 #if defined(GROUP_intdisable) || defined(BUILD_ALL) || defined(DOXYGEN)
729 UARTIntDisable(unsigned long ulBase, unsigned long ulIntFlags)
732 // Check the arguments.
734 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
737 // Disable the specified interrupts.
739 HWREG(ulBase + UART_O_IM) &= ~(ulIntFlags);
743 //*****************************************************************************
745 //! Gets the current interrupt status.
747 //! \param ulBase is the base address of the UART port.
748 //! \param bMasked is false if the raw interrupt status is required and true
749 //! if the masked interrupt status is required.
751 //! This returns the interrupt status for the specified UART. Either the raw
752 //! interrupt status or the status of interrupts that are allowed to reflect to
753 //! the processor can be returned.
755 //! \return The current interrupt status, enumerated as a bit field of
756 //! values described in UARTIntEnable().
758 //*****************************************************************************
759 #if defined(GROUP_intstatus) || defined(BUILD_ALL) || defined(DOXYGEN)
761 UARTIntStatus(unsigned long ulBase, tBoolean bMasked)
764 // Check the arguments.
766 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
769 // Return either the interrupt status or the raw interrupt status as
774 return(HWREG(ulBase + UART_O_MIS));
778 return(HWREG(ulBase + UART_O_RIS));
783 //*****************************************************************************
785 //! Clears UART interrupt sources.
787 //! \param ulBase is the base address of the UART port.
788 //! \param ulIntFlags is a bit mask of the interrupt sources to be cleared.
790 //! The specified UART interrupt sources are cleared, so that they no longer
791 //! assert. This must be done in the interrupt handler to keep it from being
792 //! called again immediately upon exit.
794 //! The parameter \e ulIntFlags has the same definition as the same parameter
795 //! to UARTIntEnable().
799 //*****************************************************************************
800 #if defined(GROUP_intclear) || defined(BUILD_ALL) || defined(DOXYGEN)
802 UARTIntClear(unsigned long ulBase, unsigned long ulIntFlags)
805 // Check the arguments.
807 ASSERT((ulBase == UART0_BASE) || (ulBase == UART1_BASE));
810 // Clear the requested interrupt sources.
812 HWREG(ulBase + UART_O_ICR) = ulIntFlags;
816 //*****************************************************************************
818 // Close the Doxygen group.
821 //*****************************************************************************