EE445M RTOS
Taken at the University of Texas Spring 2015
uart.c
Go to the documentation of this file.
1 //*****************************************************************************
2 //
3 // uart.c - Driver for the UART.
4 //
5 // Copyright (c) 2005-2014 Texas Instruments Incorporated. All rights reserved.
6 // Software License Agreement
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the
18 // distribution.
19 //
20 // Neither the name of Texas Instruments Incorporated nor the names of
21 // its contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // This is part of revision 2.1.0.12573 of the Tiva Peripheral Driver Library.
37 //
38 //*****************************************************************************
39 
40 //*****************************************************************************
41 //
44 //
45 //*****************************************************************************
46 
47 #include <stdbool.h>
48 #include <stdint.h>
49 #include "inc/hw_ints.h"
50 #include "inc/hw_memmap.h"
51 #include "inc/hw_sysctl.h"
52 #include "inc/hw_types.h"
53 #include "inc/hw_uart.h"
54 #include "driverlib/debug.h"
55 #include "driverlib/interrupt.h"
56 #include "driverlib/uart.h"
57 
58 //*****************************************************************************
59 //
60 // The system clock divider defining the maximum baud rate supported by the
61 // UART.
62 //
63 //*****************************************************************************
64 #define UART_CLK_DIVIDER 8
65 
66 //*****************************************************************************
67 //
68 // A mapping of UART base address to interrupt number.
69 //
70 //*****************************************************************************
71 static const uint32_t g_ppui32UARTIntMap[][2] =
72 {
81 };
82 static const uint_fast8_t g_ui8UARTIntMapRows =
83  sizeof(g_ppui32UARTIntMap) / sizeof(g_ppui32UARTIntMap[0]);
84 static const uint32_t g_ppui32UARTIntMapSnowflake[][2] =
85 {
94 };
95 static const uint_fast8_t g_ui8UARTIntMapRowsSnowflake =
97  sizeof(g_ppui32UARTIntMapSnowflake[0]);
98 
99 //*****************************************************************************
100 //
110 //
111 //*****************************************************************************
112 #ifdef DEBUG
113 static bool
114 _UARTBaseValid(uint32_t ui32Base)
115 {
116  return((ui32Base == UART0_BASE) || (ui32Base == UART1_BASE) ||
117  (ui32Base == UART2_BASE) || (ui32Base == UART3_BASE) ||
118  (ui32Base == UART4_BASE) || (ui32Base == UART5_BASE) ||
119  (ui32Base == UART6_BASE) || (ui32Base == UART7_BASE));
120 }
121 #endif
122 
123 //*****************************************************************************
124 //
134 //
135 //*****************************************************************************
136 static uint32_t
137 _UARTIntNumberGet(uint32_t ui32Base)
138 {
139  uint_fast8_t ui8Idx, ui8Rows;
140  const uint32_t (*ppui32UARTIntMap)[2];
141 
142  //
143  // Default interrupt map.
144  //
145  ppui32UARTIntMap = g_ppui32UARTIntMap;
146  ui8Rows = g_ui8UARTIntMapRows;
147 
148  if(CLASS_IS_TM4C129)
149  {
150  ppui32UARTIntMap = g_ppui32UARTIntMapSnowflake;
152  }
153 
154  //
155  // Loop through the table that maps UART base addresses to interrupt
156  // numbers.
157  //
158  for(ui8Idx = 0; ui8Idx < ui8Rows; ui8Idx++)
159  {
160  //
161  // See if this base address matches.
162  //
163  if(ppui32UARTIntMap[ui8Idx][0] == ui32Base)
164  {
165  //
166  // Return the corresponding interrupt number.
167  //
168  return(ppui32UARTIntMap[ui8Idx][1]);
169  }
170  }
171 
172  //
173  // The base address could not be found, so return an error.
174  //
175  return(0);
176 }
177 
178 //*****************************************************************************
179 //
193 //
194 //*****************************************************************************
195 void
196 UARTParityModeSet(uint32_t ui32Base, uint32_t ui32Parity)
197 {
198  //
199  // Check the arguments.
200  //
201  ASSERT(_UARTBaseValid(ui32Base));
202  ASSERT((ui32Parity == UART_CONFIG_PAR_NONE) ||
203  (ui32Parity == UART_CONFIG_PAR_EVEN) ||
204  (ui32Parity == UART_CONFIG_PAR_ODD) ||
205  (ui32Parity == UART_CONFIG_PAR_ONE) ||
206  (ui32Parity == UART_CONFIG_PAR_ZERO));
207 
208  //
209  // Set the parity mode.
210  //
211  HWREG(ui32Base + UART_O_LCRH) = ((HWREG(ui32Base + UART_O_LCRH) &
213  UART_LCRH_PEN)) | ui32Parity);
214 }
215 
216 //*****************************************************************************
217 //
228 //
229 //*****************************************************************************
230 uint32_t
231 UARTParityModeGet(uint32_t ui32Base)
232 {
233  //
234  // Check the arguments.
235  //
236  ASSERT(_UARTBaseValid(ui32Base));
237 
238  //
239  // Return the current parity setting.
240  //
241  return(HWREG(ui32Base + UART_O_LCRH) &
243 }
244 
245 //*****************************************************************************
246 //
261 //
262 //*****************************************************************************
263 void
264 UARTFIFOLevelSet(uint32_t ui32Base, uint32_t ui32TxLevel,
265  uint32_t ui32RxLevel)
266 {
267  //
268  // Check the arguments.
269  //
270  ASSERT(_UARTBaseValid(ui32Base));
271  ASSERT((ui32TxLevel == UART_FIFO_TX1_8) ||
272  (ui32TxLevel == UART_FIFO_TX2_8) ||
273  (ui32TxLevel == UART_FIFO_TX4_8) ||
274  (ui32TxLevel == UART_FIFO_TX6_8) ||
275  (ui32TxLevel == UART_FIFO_TX7_8));
276  ASSERT((ui32RxLevel == UART_FIFO_RX1_8) ||
277  (ui32RxLevel == UART_FIFO_RX2_8) ||
278  (ui32RxLevel == UART_FIFO_RX4_8) ||
279  (ui32RxLevel == UART_FIFO_RX6_8) ||
280  (ui32RxLevel == UART_FIFO_RX7_8));
281 
282  //
283  // Set the FIFO interrupt levels.
284  //
285  HWREG(ui32Base + UART_O_IFLS) = ui32TxLevel | ui32RxLevel;
286 }
287 
288 //*****************************************************************************
289 //
304 //
305 //*****************************************************************************
306 void
307 UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel,
308  uint32_t *pui32RxLevel)
309 {
310  uint32_t ui32Temp;
311 
312  //
313  // Check the arguments.
314  //
315  ASSERT(_UARTBaseValid(ui32Base));
316 
317  //
318  // Read the FIFO level register.
319  //
320  ui32Temp = HWREG(ui32Base + UART_O_IFLS);
321 
322  //
323  // Extract the transmit and receive FIFO levels.
324  //
325  *pui32TxLevel = ui32Temp & UART_IFLS_TX_M;
326  *pui32RxLevel = ui32Temp & UART_IFLS_RX_M;
327 }
328 
329 //*****************************************************************************
330 //
365 //
366 //*****************************************************************************
367 void
368 UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
369  uint32_t ui32Baud, uint32_t ui32Config)
370 {
371  uint32_t ui32Div;
372 
373  //
374  // Check the arguments.
375  //
376  ASSERT(_UARTBaseValid(ui32Base));
377  ASSERT(ui32Baud != 0);
378  ASSERT(ui32UARTClk >= (ui32Baud * UART_CLK_DIVIDER));
379 
380  //
381  // Stop the UART.
382  //
383  UARTDisable(ui32Base);
384 
385  //
386  // Is the required baud rate greater than the maximum rate supported
387  // without the use of high speed mode?
388  //
389  if((ui32Baud * 16) > ui32UARTClk)
390  {
391  //
392  // Enable high speed mode.
393  //
394  HWREG(ui32Base + UART_O_CTL) |= UART_CTL_HSE;
395 
396  //
397  // Half the supplied baud rate to compensate for enabling high speed
398  // mode. This allows the following code to be common to both cases.
399  //
400  ui32Baud /= 2;
401  }
402  else
403  {
404  //
405  // Disable high speed mode.
406  //
407  HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_HSE);
408  }
409 
410  //
411  // Compute the fractional baud rate divider.
412  //
413  ui32Div = (((ui32UARTClk * 8) / ui32Baud) + 1) / 2;
414 
415  //
416  // Set the baud rate.
417  //
418  HWREG(ui32Base + UART_O_IBRD) = ui32Div / 64;
419  HWREG(ui32Base + UART_O_FBRD) = ui32Div % 64;
420 
421  //
422  // Set parity, data length, and number of stop bits.
423  //
424  HWREG(ui32Base + UART_O_LCRH) = ui32Config;
425 
426  //
427  // Clear the flags register.
428  //
429  HWREG(ui32Base + UART_O_FR) = 0;
430 
431  //
432  // Start the UART.
433  //
434  UARTEnable(ui32Base);
435 }
436 
437 //*****************************************************************************
438 //
464 //
465 //*****************************************************************************
466 void
467 UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk,
468  uint32_t *pui32Baud, uint32_t *pui32Config)
469 {
470  uint32_t ui32Int, ui32Frac;
471 
472  //
473  // Check the arguments.
474  //
475  ASSERT(_UARTBaseValid(ui32Base));
476 
477  //
478  // Compute the baud rate.
479  //
480  ui32Int = HWREG(ui32Base + UART_O_IBRD);
481  ui32Frac = HWREG(ui32Base + UART_O_FBRD);
482  *pui32Baud = (ui32UARTClk * 4) / ((64 * ui32Int) + ui32Frac);
483 
484  //
485  // See if high speed mode enabled.
486  //
487  if(HWREG(ui32Base + UART_O_CTL) & UART_CTL_HSE)
488  {
489  //
490  // High speed mode is enabled so the actual baud rate is actually
491  // double what was just calculated.
492  //
493  *pui32Baud *= 2;
494  }
495 
496  //
497  // Get the parity, data length, and number of stop bits.
498  //
499  *pui32Config = (HWREG(ui32Base + UART_O_LCRH) &
502 }
503 
504 //*****************************************************************************
505 //
513 //
514 //*****************************************************************************
515 void
516 UARTEnable(uint32_t ui32Base)
517 {
518  //
519  // Check the arguments.
520  //
521  ASSERT(_UARTBaseValid(ui32Base));
522 
523  //
524  // Enable the FIFO.
525  //
526  HWREG(ui32Base + UART_O_LCRH) |= UART_LCRH_FEN;
527 
528  //
529  // Enable RX, TX, and the UART.
530  //
531  HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_UARTEN | UART_CTL_TXE |
532  UART_CTL_RXE);
533 }
534 
535 //*****************************************************************************
536 //
545 //
546 //*****************************************************************************
547 void
548 UARTDisable(uint32_t ui32Base)
549 {
550  //
551  // Check the arguments.
552  //
553  ASSERT(_UARTBaseValid(ui32Base));
554 
555  //
556  // Wait for end of TX.
557  //
558  while(HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY)
559  {
560  }
561 
562  //
563  // Disable the FIFO.
564  //
565  HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
566 
567  //
568  // Disable the UART.
569  //
570  HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_UARTEN | UART_CTL_TXE |
571  UART_CTL_RXE);
572 }
573 
574 //*****************************************************************************
575 //
583 //
584 //*****************************************************************************
585 void
586 UARTFIFOEnable(uint32_t ui32Base)
587 {
588  //
589  // Check the arguments.
590  //
591  ASSERT(_UARTBaseValid(ui32Base));
592 
593  //
594  // Enable the FIFO.
595  //
596  HWREG(ui32Base + UART_O_LCRH) |= UART_LCRH_FEN;
597 }
598 
599 //*****************************************************************************
600 //
608 //
609 //*****************************************************************************
610 void
611 UARTFIFODisable(uint32_t ui32Base)
612 {
613  //
614  // Check the arguments.
615  //
616  ASSERT(_UARTBaseValid(ui32Base));
617 
618  //
619  // Disable the FIFO.
620  //
621  HWREG(ui32Base + UART_O_LCRH) &= ~(UART_LCRH_FEN);
622 }
623 
624 //*****************************************************************************
625 //
644 //
645 //*****************************************************************************
646 void
647 UARTEnableSIR(uint32_t ui32Base, bool bLowPower)
648 {
649  //
650  // Check the arguments.
651  //
652  ASSERT(_UARTBaseValid(ui32Base));
653 
654  //
655  // Enable SIR and SIRLP (if appropriate).
656  //
657  if(bLowPower)
658  {
659  HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_SIREN | UART_CTL_SIRLP);
660  }
661  else
662  {
663  HWREG(ui32Base + UART_O_CTL) |= (UART_CTL_SIREN);
664  }
665 }
666 
667 //*****************************************************************************
668 //
685 //
686 //*****************************************************************************
687 void
688 UARTDisableSIR(uint32_t ui32Base)
689 {
690  //
691  // Check the arguments.
692  //
693  ASSERT(_UARTBaseValid(ui32Base));
694 
695  //
696  // Disable SIR and SIRLP (if appropriate).
697  //
698  HWREG(ui32Base + UART_O_CTL) &= ~(UART_CTL_SIREN | UART_CTL_SIRLP);
699 }
700 
701 //*****************************************************************************
702 //
716 //
717 //*****************************************************************************
718 void
719 UARTSmartCardEnable(uint32_t ui32Base)
720 {
721  uint32_t ui32Val;
722 
723  //
724  // Check the arguments.
725  //
726  ASSERT(_UARTBaseValid(ui32Base));
727 
728  //
729  // Set 8-bit word length, even parity, 2 stop bits (note that although the
730  // STP2 bit is ignored when in smartcard mode, this code lets the caller
731  // read back the actual setting in use).
732  //
733  ui32Val = HWREG(ui32Base + UART_O_LCRH);
734  ui32Val &= ~(UART_LCRH_SPS | UART_LCRH_EPS | UART_LCRH_PEN |
738  HWREG(ui32Base + UART_O_LCRH) = ui32Val;
739 
740  //
741  // Enable SMART mode.
742  //
743  HWREG(ui32Base + UART_O_CTL) |= UART_CTL_SMART;
744 }
745 
746 //*****************************************************************************
747 //
760 //
761 //*****************************************************************************
762 void
763 UARTSmartCardDisable(uint32_t ui32Base)
764 {
765  //
766  // Check the arguments.
767  //
768  ASSERT(_UARTBaseValid(ui32Base));
769 
770  //
771  // Disable the SMART bit.
772  //
773  HWREG(ui32Base + UART_O_CTL) &= ~UART_CTL_SMART;
774 }
775 
776 //*****************************************************************************
777 //
797 //
798 //*****************************************************************************
799 void
800 UARTModemControlSet(uint32_t ui32Base, uint32_t ui32Control)
801 {
802  uint32_t ui32Temp;
803 
804  //
805  // Check the arguments.
806  //
807  ASSERT(ui32Base == UART1_BASE);
808  ASSERT((ui32Control & ~(UART_OUTPUT_RTS | UART_OUTPUT_DTR)) == 0);
809 
810  //
811  // Set the appropriate modem control output bits.
812  //
813  ui32Temp = HWREG(ui32Base + UART_O_CTL);
814  ui32Temp |= (ui32Control & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
815  HWREG(ui32Base + UART_O_CTL) = ui32Temp;
816 }
817 
818 //*****************************************************************************
819 //
839 //
840 //*****************************************************************************
841 void
842 UARTModemControlClear(uint32_t ui32Base, uint32_t ui32Control)
843 {
844  uint32_t ui32Temp;
845 
846  //
847  // Check the arguments.
848  //
849  ASSERT(ui32Base == UART1_BASE);
850  ASSERT((ui32Control & ~(UART_OUTPUT_RTS | UART_OUTPUT_DTR)) == 0);
851 
852  //
853  // Set the appropriate modem control output bits.
854  //
855  ui32Temp = HWREG(ui32Base + UART_O_CTL);
856  ui32Temp &= ~(ui32Control & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
857  HWREG(ui32Base + UART_O_CTL) = ui32Temp;
858 }
859 
860 //*****************************************************************************
861 //
877 //
878 //*****************************************************************************
879 uint32_t
880 UARTModemControlGet(uint32_t ui32Base)
881 {
882  //
883  // Check the arguments.
884  //
885  ASSERT(ui32Base == UART1_BASE);
886 
887  return(HWREG(ui32Base + UART_O_CTL) & (UART_OUTPUT_RTS | UART_OUTPUT_DTR));
888 }
889 
890 //*****************************************************************************
891 //
907 //
908 //*****************************************************************************
909 uint32_t
910 UARTModemStatusGet(uint32_t ui32Base)
911 {
912  //
913  // Check the arguments.
914  //
915  ASSERT(ui32Base == UART1_BASE);
916 
917  return(HWREG(ui32Base + UART_O_FR) & (UART_INPUT_RI | UART_INPUT_DCD |
919 }
920 
921 //*****************************************************************************
922 //
944 //
945 //*****************************************************************************
946 void
947 UARTFlowControlSet(uint32_t ui32Base, uint32_t ui32Mode)
948 {
949  //
950  // Check the arguments.
951  //
952  ASSERT(_UARTBaseValid(ui32Base));
953  ASSERT((ui32Mode & ~(UART_FLOWCONTROL_TX | UART_FLOWCONTROL_RX)) == 0);
954 
955  //
956  // Set the flow control mode as requested.
957  //
958  HWREG(ui32Base + UART_O_CTL) = ((HWREG(ui32Base + UART_O_CTL) &
960  UART_FLOWCONTROL_RX)) | ui32Mode);
961 }
962 
963 //*****************************************************************************
964 //
980 //
981 //*****************************************************************************
982 uint32_t
983 UARTFlowControlGet(uint32_t ui32Base)
984 {
985  //
986  // Check the arguments.
987  //
988  ASSERT(_UARTBaseValid(ui32Base));
989 
990  return(HWREG(ui32Base + UART_O_CTL) & (UART_FLOWCONTROL_TX |
992 }
993 
994 //*****************************************************************************
995 //
1017 //
1018 //*****************************************************************************
1019 void
1020 UARTTxIntModeSet(uint32_t ui32Base, uint32_t ui32Mode)
1021 {
1022  //
1023  // Check the arguments.
1024  //
1025  ASSERT(_UARTBaseValid(ui32Base));
1026  ASSERT((ui32Mode == UART_TXINT_MODE_EOT) ||
1027  (ui32Mode == UART_TXINT_MODE_FIFO));
1028 
1029  //
1030  // Set or clear the EOT bit of the UART control register as appropriate.
1031  //
1032  HWREG(ui32Base + UART_O_CTL) = ((HWREG(ui32Base + UART_O_CTL) &
1034  UART_TXINT_MODE_FIFO)) | ui32Mode);
1035 }
1036 
1037 //*****************************************************************************
1038 //
1056 //
1057 //*****************************************************************************
1058 uint32_t
1059 UARTTxIntModeGet(uint32_t ui32Base)
1060 {
1061  //
1062  // Check the arguments.
1063  //
1064  ASSERT(_UARTBaseValid(ui32Base));
1065 
1066  //
1067  // Return the current transmit interrupt mode.
1068  //
1069  return(HWREG(ui32Base + UART_O_CTL) & (UART_TXINT_MODE_EOT |
1071 }
1072 
1073 //*****************************************************************************
1074 //
1084 //
1085 //*****************************************************************************
1086 bool
1087 UARTCharsAvail(uint32_t ui32Base)
1088 {
1089  //
1090  // Check the arguments.
1091  //
1092  ASSERT(_UARTBaseValid(ui32Base));
1093 
1094  //
1095  // Return the availability of characters.
1096  //
1097  return((HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE) ? false : true);
1098 }
1099 
1100 //*****************************************************************************
1101 //
1111 //
1112 //*****************************************************************************
1113 bool
1114 UARTSpaceAvail(uint32_t ui32Base)
1115 {
1116  //
1117  // Check the arguments.
1118  //
1119  ASSERT(_UARTBaseValid(ui32Base));
1120 
1121  //
1122  // Return the availability of space.
1123  //
1124  return((HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF) ? false : true);
1125 }
1126 
1127 //*****************************************************************************
1128 //
1140 //
1141 //*****************************************************************************
1142 int32_t
1143 UARTCharGetNonBlocking(uint32_t ui32Base)
1144 {
1145  //
1146  // Check the arguments.
1147  //
1148  ASSERT(_UARTBaseValid(ui32Base));
1149 
1150  //
1151  // See if there are any characters in the receive FIFO.
1152  //
1153  if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE))
1154  {
1155  //
1156  // Read and return the next character.
1157  //
1158  return(HWREG(ui32Base + UART_O_DR));
1159  }
1160  else
1161  {
1162  //
1163  // There are no characters, so return a failure.
1164  //
1165  return(-1);
1166  }
1167 }
1168 
1169 //*****************************************************************************
1170 //
1181 //
1182 //*****************************************************************************
1183 int32_t
1184 UARTCharGet(uint32_t ui32Base)
1185 {
1186  //
1187  // Check the arguments.
1188  //
1189  ASSERT(_UARTBaseValid(ui32Base));
1190 
1191  //
1192  // Wait until a char is available.
1193  //
1194  while(HWREG(ui32Base + UART_O_FR) & UART_FR_RXFE)
1195  {
1196  }
1197 
1198  //
1199  // Now get the char.
1200  //
1201  return(HWREG(ui32Base + UART_O_DR));
1202 }
1203 
1204 //*****************************************************************************
1205 //
1219 //
1220 //*****************************************************************************
1221 bool
1222 UARTCharPutNonBlocking(uint32_t ui32Base, unsigned char ucData)
1223 {
1224  //
1225  // Check the arguments.
1226  //
1227  ASSERT(_UARTBaseValid(ui32Base));
1228 
1229  //
1230  // See if there is space in the transmit FIFO.
1231  //
1232  if(!(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF))
1233  {
1234  //
1235  // Write this character to the transmit FIFO.
1236  //
1237  HWREG(ui32Base + UART_O_DR) = ucData;
1238 
1239  //
1240  // Success.
1241  //
1242  return(true);
1243  }
1244  else
1245  {
1246  //
1247  // There is no space in the transmit FIFO, so return a failure.
1248  //
1249  return(false);
1250  }
1251 }
1252 
1253 //*****************************************************************************
1254 //
1265 //
1266 //*****************************************************************************
1267 void
1268 UARTCharPut(uint32_t ui32Base, unsigned char ucData)
1269 {
1270  //
1271  // Check the arguments.
1272  //
1273  ASSERT(_UARTBaseValid(ui32Base));
1274 
1275  //
1276  // Wait until space is available.
1277  //
1278  while(HWREG(ui32Base + UART_O_FR) & UART_FR_TXFF)
1279  {
1280  }
1281 
1282  //
1283  // Send the char.
1284  //
1285  HWREG(ui32Base + UART_O_DR) = ucData;
1286 }
1287 
1288 //*****************************************************************************
1289 //
1301 //
1302 //*****************************************************************************
1303 void
1304 UARTBreakCtl(uint32_t ui32Base, bool bBreakState)
1305 {
1306  //
1307  // Check the arguments.
1308  //
1309  ASSERT(_UARTBaseValid(ui32Base));
1310 
1311  //
1312  // Set the break condition as requested.
1313  //
1314  HWREG(ui32Base + UART_O_LCRH) =
1315  (bBreakState ?
1316  (HWREG(ui32Base + UART_O_LCRH) | UART_LCRH_BRK) :
1317  (HWREG(ui32Base + UART_O_LCRH) & ~(UART_LCRH_BRK)));
1318 }
1319 
1320 //*****************************************************************************
1321 //
1333 //
1334 //*****************************************************************************
1335 bool
1336 UARTBusy(uint32_t ui32Base)
1337 {
1338  //
1339  // Check the argument.
1340  //
1341  ASSERT(_UARTBaseValid(ui32Base));
1342 
1343  //
1344  // Determine if the UART is busy.
1345  //
1346  return((HWREG(ui32Base + UART_O_FR) & UART_FR_BUSY) ? true : false);
1347 }
1348 
1349 //*****************************************************************************
1350 //
1366 //
1367 //*****************************************************************************
1368 void
1369 UARTIntRegister(uint32_t ui32Base, void (*pfnHandler)(void))
1370 {
1371  uint32_t ui32Int;
1372 
1373  //
1374  // Check the arguments.
1375  //
1376  ASSERT(_UARTBaseValid(ui32Base));
1377 
1378  //
1379  // Determine the interrupt number based on the UART port.
1380  //
1381  ui32Int = _UARTIntNumberGet(ui32Base);
1382 
1383  ASSERT(ui32Int != 0);
1384 
1385  //
1386  // Register the interrupt handler.
1387  //
1388  IntRegister(ui32Int, pfnHandler);
1389 
1390  //
1391  // Enable the UART interrupt.
1392  //
1393  IntEnable(ui32Int);
1394 }
1395 
1396 //*****************************************************************************
1397 //
1411 //
1412 //*****************************************************************************
1413 void
1414 UARTIntUnregister(uint32_t ui32Base)
1415 {
1416  uint32_t ui32Int;
1417 
1418  //
1419  // Check the arguments.
1420  //
1421  ASSERT(_UARTBaseValid(ui32Base));
1422 
1423  //
1424  // Determine the interrupt number based on the UART port.
1425  //
1426  ui32Int = _UARTIntNumberGet(ui32Base);
1427 
1428  ASSERT(ui32Int != 0);
1429 
1430  //
1431  // Disable the interrupt.
1432  //
1433  IntDisable(ui32Int);
1434 
1435  //
1436  // Unregister the interrupt handler.
1437  //
1438  IntUnregister(ui32Int);
1439 }
1440 
1441 //*****************************************************************************
1442 //
1468 //
1469 //*****************************************************************************
1470 void
1471 UARTIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
1472 {
1473  //
1474  // Check the arguments.
1475  //
1476  ASSERT(_UARTBaseValid(ui32Base));
1477 
1478  //
1479  // Enable the specified interrupts.
1480  //
1481  HWREG(ui32Base + UART_O_IM) |= ui32IntFlags;
1482 }
1483 
1484 //*****************************************************************************
1485 //
1500 //
1501 //*****************************************************************************
1502 void
1503 UARTIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
1504 {
1505  //
1506  // Check the arguments.
1507  //
1508  ASSERT(_UARTBaseValid(ui32Base));
1509 
1510  //
1511  // Disable the specified interrupts.
1512  //
1513  HWREG(ui32Base + UART_O_IM) &= ~(ui32IntFlags);
1514 }
1515 
1516 //*****************************************************************************
1517 //
1530 //
1531 //*****************************************************************************
1532 uint32_t
1533 UARTIntStatus(uint32_t ui32Base, bool bMasked)
1534 {
1535  //
1536  // Check the arguments.
1537  //
1538  ASSERT(_UARTBaseValid(ui32Base));
1539 
1540  //
1541  // Return either the interrupt status or the raw interrupt status as
1542  // requested.
1543  //
1544  if(bMasked)
1545  {
1546  return(HWREG(ui32Base + UART_O_MIS));
1547  }
1548  else
1549  {
1550  return(HWREG(ui32Base + UART_O_RIS));
1551  }
1552 }
1553 
1554 //*****************************************************************************
1555 //
1578 //
1579 //*****************************************************************************
1580 void
1581 UARTIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
1582 {
1583  //
1584  // Check the arguments.
1585  //
1586  ASSERT(_UARTBaseValid(ui32Base));
1587 
1588  //
1589  // Clear the requested interrupt sources.
1590  //
1591  HWREG(ui32Base + UART_O_ICR) = ui32IntFlags;
1592 }
1593 
1594 //*****************************************************************************
1595 //
1614 //
1615 //*****************************************************************************
1616 void
1617 UARTDMAEnable(uint32_t ui32Base, uint32_t ui32DMAFlags)
1618 {
1619  //
1620  // Check the arguments.
1621  //
1622  ASSERT(_UARTBaseValid(ui32Base));
1623 
1624  //
1625  // Set the requested bits in the UART uDMA control register.
1626  //
1627  HWREG(ui32Base + UART_O_DMACTL) |= ui32DMAFlags;
1628 }
1629 
1630 //*****************************************************************************
1631 //
1646 //
1647 //*****************************************************************************
1648 void
1649 UARTDMADisable(uint32_t ui32Base, uint32_t ui32DMAFlags)
1650 {
1651  //
1652  // Check the arguments.
1653  //
1654  ASSERT(_UARTBaseValid(ui32Base));
1655 
1656  //
1657  // Clear the requested bits in the UART uDMA control register.
1658  //
1659  HWREG(ui32Base + UART_O_DMACTL) &= ~ui32DMAFlags;
1660 }
1661 
1662 //*****************************************************************************
1663 //
1677 //
1678 //*****************************************************************************
1679 uint32_t
1680 UARTRxErrorGet(uint32_t ui32Base)
1681 {
1682  //
1683  // Check the arguments.
1684  //
1685  ASSERT(_UARTBaseValid(ui32Base));
1686 
1687  //
1688  // Return the current value of the receive status register.
1689  //
1690  return(HWREG(ui32Base + UART_O_RSR) & 0x0000000F);
1691 }
1692 
1693 //*****************************************************************************
1694 //
1705 //
1706 //*****************************************************************************
1707 void
1708 UARTRxErrorClear(uint32_t ui32Base)
1709 {
1710  //
1711  // Check the arguments.
1712  //
1713  ASSERT(_UARTBaseValid(ui32Base));
1714 
1715  //
1716  // Any write to the Error Clear Register clears all bits which are
1717  // currently set.
1718  //
1719  HWREG(ui32Base + UART_O_ECR) = 0;
1720 }
1721 
1722 //*****************************************************************************
1723 //
1742 //
1743 //*****************************************************************************
1744 void
1745 UARTClockSourceSet(uint32_t ui32Base, uint32_t ui32Source)
1746 {
1747  //
1748  // Check the arguments.
1749  //
1750  ASSERT(_UARTBaseValid(ui32Base));
1751  ASSERT((ui32Source == UART_CLOCK_SYSTEM) ||
1752  (ui32Source == UART_CLOCK_PIOSC));
1753 
1754  //
1755  // Set the UART clock source.
1756  //
1757  HWREG(ui32Base + UART_O_CC) = ui32Source;
1758 }
1759 
1760 //*****************************************************************************
1761 //
1775 //
1776 //*****************************************************************************
1777 uint32_t
1778 UARTClockSourceGet(uint32_t ui32Base)
1779 {
1780  //
1781  // Check the arguments.
1782  //
1783  ASSERT(_UARTBaseValid(ui32Base));
1784 
1785  //
1786  // Return the UART clock source.
1787  //
1788  return(HWREG(ui32Base + UART_O_CC));
1789 }
1790 
1791 //*****************************************************************************
1792 //
1804 //
1805 //*****************************************************************************
1806 void
1807 UART9BitEnable(uint32_t ui32Base)
1808 {
1809  //
1810  // Check the arguments.
1811  //
1812  ASSERT(_UARTBaseValid(ui32Base));
1813 
1814  //
1815  // Enable 9-bit mode.
1816  //
1818 }
1819 
1820 //*****************************************************************************
1821 //
1833 //
1834 //*****************************************************************************
1835 void
1836 UART9BitDisable(uint32_t ui32Base)
1837 {
1838  //
1839  // Check the arguments.
1840  //
1841  ASSERT(_UARTBaseValid(ui32Base));
1842 
1843  //
1844  // Disable 9-bit mode.
1845  //
1846  HWREG(ui32Base + UART_O_9BITADDR) &= ~UART_9BITADDR_9BITEN;
1847 }
1848 
1849 //*****************************************************************************
1850 //
1868 //
1869 //*****************************************************************************
1870 void
1871 UART9BitAddrSet(uint32_t ui32Base, uint8_t ui8Addr,
1872  uint8_t ui8Mask)
1873 {
1874  //
1875  // Check the arguments.
1876  //
1877  ASSERT(_UARTBaseValid(ui32Base));
1878 
1879  //
1880  // Set the address and mask.
1881  //
1882  HWREG(ui32Base + UART_O_9BITADDR) = ui8Addr << UART_9BITADDR_ADDR_S;
1883  HWREG(ui32Base + UART_O_9BITAMASK) = ui8Mask << UART_9BITAMASK_MASK_S;
1884 }
1885 
1886 //*****************************************************************************
1887 //
1907 //
1908 //*****************************************************************************
1909 void
1910 UART9BitAddrSend(uint32_t ui32Base, uint8_t ui8Addr)
1911 {
1912  uint32_t ui32LCRH;
1913 
1914  //
1915  // Check the arguments.
1916  //
1917  ASSERT(_UARTBaseValid(ui32Base));
1918 
1919  //
1920  // Wait until the FIFO is empty and the UART is not busy.
1921  //
1922  while((HWREG(ui32Base + UART_O_FR) & (UART_FR_TXFE | UART_FR_BUSY)) !=
1923  UART_FR_TXFE)
1924  {
1925  }
1926 
1927  //
1928  // Force the address/data bit to 1 to indicate this is an address byte.
1929  //
1930  ui32LCRH = HWREG(ui32Base + UART_O_LCRH);
1931  HWREG(ui32Base + UART_O_LCRH) = ((ui32LCRH & ~UART_LCRH_EPS) |
1933 
1934  //
1935  // Send the address.
1936  //
1937  HWREG(ui32Base + UART_O_DR) = ui8Addr;
1938 
1939  //
1940  // Wait until the address has been sent.
1941  //
1942  while((HWREG(ui32Base + UART_O_FR) & (UART_FR_TXFE | UART_FR_BUSY)) !=
1943  UART_FR_TXFE)
1944  {
1945  }
1946 
1947  //
1948  // Restore the address/data setting.
1949  //
1950  HWREG(ui32Base + UART_O_LCRH) = ui32LCRH;
1951 }
1952 
1953 //*****************************************************************************
1954 //
1955 // Close the Doxygen group.
1957 //
1958 //*****************************************************************************
void UARTModemControlClear(uint32_t ui32Base, uint32_t ui32Control)
Definition: uart.c:842
#define INT_UART7_TM4C129
Definition: hw_ints.h:236
#define UART_CLK_DIVIDER
Definition: uart.c:64
void UARTFIFOEnable(uint32_t ui32Base)
Definition: uart.c:586
#define UART7_BASE
Definition: hw_memmap.h:68
uint32_t UARTClockSourceGet(uint32_t ui32Base)
Definition: uart.c:1778
void UARTSmartCardDisable(uint32_t ui32Base)
Definition: uart.c:763
#define UART_CONFIG_PAR_EVEN
Definition: uart.h:94
#define UART_LCRH_STP2
Definition: hw_uart.h:150
#define UART_O_FBRD
Definition: hw_uart.h:54
bool UARTCharsAvail(uint32_t ui32Base)
Definition: uart.c:1087
#define HWREG(x)
Definition: hw_types.h:48
#define UART_FLOWCONTROL_RX
Definition: uart.h:168
#define UART_FR_TXFE
Definition: hw_uart.h:105
#define INT_UART4_TM4C123
Definition: hw_ints.h:121
#define UART_9BITADDR_9BITEN
Definition: hw_uart.h:334
#define UART_LCRH_BRK
Definition: hw_uart.h:153
#define UART_FIFO_RX1_8
Definition: uart.h:117
#define UART_FLOWCONTROL_TX
Definition: uart.h:167
#define INT_UART7_TM4C123
Definition: hw_ints.h:124
#define UART_IFLS_TX_M
Definition: hw_uart.h:186
static uint32_t _UARTIntNumberGet(uint32_t ui32Base)
Definition: uart.c:137
void UARTConfigSetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk, uint32_t ui32Baud, uint32_t ui32Config)
Definition: uart.c:368
void UARTParityModeSet(uint32_t ui32Base, uint32_t ui32Parity)
Definition: uart.c:196
#define UART_O_LCRH
Definition: hw_uart.h:56
#define UART4_BASE
Definition: hw_memmap.h:65
#define INT_UART4_TM4C129
Definition: hw_ints.h:233
#define UART_IFLS_RX_M
Definition: hw_uart.h:179
#define UART_LCRH_FEN
Definition: hw_uart.h:149
#define UART_9BITADDR_ADDR_S
Definition: hw_uart.h:336
void UARTDMAEnable(uint32_t ui32Base, uint32_t ui32DMAFlags)
Definition: uart.c:1617
#define UART_O_9BITADDR
Definition: hw_uart.h:64
#define ASSERT(expr)
Definition: debug.h:67
void UARTFIFOLevelSet(uint32_t ui32Base, uint32_t ui32TxLevel, uint32_t ui32RxLevel)
Definition: uart.c:264
#define UART_O_DMACTL
Definition: hw_uart.h:63
void UARTIntRegister(uint32_t ui32Base, void(*pfnHandler)(void))
Definition: uart.c:1369
void UARTIntDisable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: uart.c:1503
#define UART_CTL_RXE
Definition: hw_uart.h:164
void UARTIntClear(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: uart.c:1581
#define UART_CTL_SIREN
Definition: hw_uart.h:171
#define UART_FR_BUSY
Definition: hw_uart.h:109
void UARTDisable(uint32_t ui32Base)
Definition: uart.c:548
#define INT_UART6_TM4C123
Definition: hw_ints.h:123
#define UART_O_MIS
Definition: hw_uart.h:61
void UARTBreakCtl(uint32_t ui32Base, bool bBreakState)
Definition: uart.c:1304
#define UART_CTL_SMART
Definition: hw_uart.h:169
#define UART_O_CC
Definition: hw_uart.h:67
void UARTModemControlSet(uint32_t ui32Base, uint32_t ui32Control)
Definition: uart.c:800
#define UART_INPUT_DSR
Definition: uart.h:158
#define UART_LCRH_EPS
Definition: hw_uart.h:151
bool UARTSpaceAvail(uint32_t ui32Base)
Definition: uart.c:1114
#define UART6_BASE
Definition: hw_memmap.h:67
#define UART_FIFO_RX2_8
Definition: uart.h:118
#define UART_FIFO_TX4_8
Definition: uart.h:107
void UARTSmartCardEnable(uint32_t ui32Base)
Definition: uart.c:719
void UARTRxErrorClear(uint32_t ui32Base)
Definition: uart.c:1708
bool UARTBusy(uint32_t ui32Base)
Definition: uart.c:1336
#define UART_FIFO_RX6_8
Definition: uart.h:120
#define INT_UART6_TM4C129
Definition: hw_ints.h:235
#define UART_O_9BITAMASK
Definition: hw_uart.h:65
#define UART_OUTPUT_DTR
Definition: uart.h:149
#define INT_UART5_TM4C123
Definition: hw_ints.h:122
uint32_t UARTModemStatusGet(uint32_t ui32Base)
Definition: uart.c:910
int32_t UARTCharGetNonBlocking(uint32_t ui32Base)
Definition: uart.c:1143
void UART9BitEnable(uint32_t ui32Base)
Definition: uart.c:1807
#define UART_9BITAMASK_MASK_S
Definition: hw_uart.h:345
#define UART1_BASE
Definition: hw_memmap.h:62
void UARTEnableSIR(uint32_t ui32Base, bool bLowPower)
Definition: uart.c:647
#define UART_FIFO_TX7_8
Definition: uart.h:109
void UART9BitDisable(uint32_t ui32Base)
Definition: uart.c:1836
#define UART_FIFO_TX2_8
Definition: uart.h:106
uint32_t UARTIntStatus(uint32_t ui32Base, bool bMasked)
Definition: uart.c:1533
#define UART_O_RIS
Definition: hw_uart.h:60
#define UART_O_IFLS
Definition: hw_uart.h:58
#define INT_UART0_TM4C129
Definition: hw_ints.h:181
bool UARTCharPutNonBlocking(uint32_t ui32Base, unsigned char ucData)
Definition: uart.c:1222
void UARTClockSourceSet(uint32_t ui32Base, uint32_t ui32Source)
Definition: uart.c:1745
#define UART_TXINT_MODE_FIFO
Definition: uart.h:177
#define INT_UART3_TM4C123
Definition: hw_ints.h:120
#define UART_LCRH_WLEN_8
Definition: hw_uart.h:148
#define INT_UART1_TM4C129
Definition: hw_ints.h:182
uint32_t UARTParityModeGet(uint32_t ui32Base)
Definition: uart.c:231
void UARTDisableSIR(uint32_t ui32Base)
Definition: uart.c:688
#define UART_CONFIG_PAR_ZERO
Definition: uart.h:97
#define UART_CLOCK_PIOSC
Definition: uart.h:187
uint32_t UARTRxErrorGet(uint32_t ui32Base)
Definition: uart.c:1680
#define INT_UART0_TM4C123
Definition: hw_ints.h:69
#define UART_O_DR
Definition: hw_uart.h:48
#define UART_LCRH_PEN
Definition: hw_uart.h:152
#define UART_FIFO_TX1_8
Definition: uart.h:105
#define UART5_BASE
Definition: hw_memmap.h:66
void UARTConfigGetExpClk(uint32_t ui32Base, uint32_t ui32UARTClk, uint32_t *pui32Baud, uint32_t *pui32Config)
Definition: uart.c:467
#define INT_UART2_TM4C129
Definition: hw_ints.h:209
#define UART_INPUT_CTS
Definition: uart.h:159
static const uint32_t g_ppui32UARTIntMap[][2]
Definition: uart.c:71
#define UART_O_CTL
Definition: hw_uart.h:57
static const uint_fast8_t g_ui8UARTIntMapRowsSnowflake
Definition: uart.c:95
#define UART_FIFO_RX7_8
Definition: uart.h:121
#define UART_CTL_UARTEN
Definition: hw_uart.h:172
int32_t UARTCharGet(uint32_t ui32Base)
Definition: uart.c:1184
#define UART_CONFIG_PAR_ODD
Definition: uart.h:95
void UART9BitAddrSend(uint32_t ui32Base, uint8_t ui8Addr)
Definition: uart.c:1910
#define UART_CLOCK_SYSTEM
Definition: uart.h:186
#define INT_UART5_TM4C129
Definition: hw_ints.h:234
void IntUnregister(uint32_t ui32Interrupt)
Definition: interrupt.c:381
#define UART_LCRH_SPS
Definition: hw_uart.h:143
uint32_t UARTFlowControlGet(uint32_t ui32Base)
Definition: uart.c:983
#define UART_LCRH_WLEN_M
Definition: hw_uart.h:144
void UART9BitAddrSet(uint32_t ui32Base, uint8_t ui8Addr, uint8_t ui8Mask)
Definition: uart.c:1871
uint32_t UARTTxIntModeGet(uint32_t ui32Base)
Definition: uart.c:1059
#define UART0_BASE
Definition: hw_memmap.h:61
void UARTFIFOLevelGet(uint32_t ui32Base, uint32_t *pui32TxLevel, uint32_t *pui32RxLevel)
Definition: uart.c:307
#define INT_UART2_TM4C123
Definition: hw_ints.h:98
#define UART_O_ICR
Definition: hw_uart.h:62
uint32_t UARTModemControlGet(uint32_t ui32Base)
Definition: uart.c:880
void UARTEnable(uint32_t ui32Base)
Definition: uart.c:516
#define INT_UART1_TM4C123
Definition: hw_ints.h:70
#define UART_FIFO_TX6_8
Definition: uart.h:108
#define UART_CTL_SIRLP
Definition: hw_uart.h:170
void UARTCharPut(uint32_t ui32Base, unsigned char ucData)
Definition: uart.c:1268
void UARTTxIntModeSet(uint32_t ui32Base, uint32_t ui32Mode)
Definition: uart.c:1020
#define UART_FR_RXFE
Definition: hw_uart.h:108
#define CLASS_IS_TM4C129
Definition: hw_types.h:99
void UARTIntEnable(uint32_t ui32Base, uint32_t ui32IntFlags)
Definition: uart.c:1471
#define UART3_BASE
Definition: hw_memmap.h:64
#define UART2_BASE
Definition: hw_memmap.h:63
#define UART_O_FR
Definition: hw_uart.h:51
void UARTFlowControlSet(uint32_t ui32Base, uint32_t ui32Mode)
Definition: uart.c:947
#define UART_CTL_HSE
Definition: hw_uart.h:167
#define UART_INPUT_DCD
Definition: uart.h:157
#define UART_OUTPUT_RTS
Definition: uart.h:148
#define UART_O_ECR
Definition: hw_uart.h:50
#define UART_O_IM
Definition: hw_uart.h:59
#define UART_CTL_TXE
Definition: hw_uart.h:165
static const uint32_t g_ppui32UARTIntMapSnowflake[][2]
Definition: uart.c:84
#define UART_CONFIG_PAR_ONE
Definition: uart.h:96
#define INT_UART3_TM4C129
Definition: hw_ints.h:232
static const uint_fast8_t g_ui8UARTIntMapRows
Definition: uart.c:82
#define UART_CONFIG_PAR_NONE
Definition: uart.h:93
void UARTIntUnregister(uint32_t ui32Base)
Definition: uart.c:1414
#define UART_TXINT_MODE_EOT
Definition: uart.h:178
void IntDisable(uint32_t ui32Interrupt)
Definition: interrupt.c:684
void IntRegister(uint32_t ui32Interrupt, void(*pfnHandler)(void))
Definition: interrupt.c:309
#define UART_FIFO_RX4_8
Definition: uart.h:119
#define UART_O_RSR
Definition: hw_uart.h:49
#define UART_INPUT_RI
Definition: uart.h:156
void IntEnable(uint32_t ui32Interrupt)
Definition: interrupt.c:610
void UARTDMADisable(uint32_t ui32Base, uint32_t ui32DMAFlags)
Definition: uart.c:1649
#define UART_O_IBRD
Definition: hw_uart.h:53
#define UART_FR_TXFF
Definition: hw_uart.h:107
void UARTFIFODisable(uint32_t ui32Base)
Definition: uart.c:611