Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 | /* * Copyright (c) 2015-2016, Freescale Semiconductor, Inc. * Copyright 2016-2017 NXP * All rights reserved. * * SPDX-License-Identifier: BSD-3-Clause */ #include "fsl_uart.h" /******************************************************************************* * Definitions ******************************************************************************/ /* Component ID definition, used by tools. */ #ifndef FSL_COMPONENT_ID #define FSL_COMPONENT_ID "platform.drivers.uart" #endif /* UART transfer state. */ enum _uart_tansfer_states { kUART_TxIdle, /* TX idle. */ kUART_TxBusy, /* TX busy. */ kUART_RxIdle, /* RX idle. */ kUART_RxBusy, /* RX busy. */ kUART_RxFramingError, /* Rx framing error */ kUART_RxParityError /* Rx parity error */ }; /* Typedef for interrupt handler. */ typedef void (*uart_isr_t)(UART_Type *base, uart_handle_t *handle); /******************************************************************************* * Prototypes ******************************************************************************/ /*! * @brief Check whether the RX ring buffer is full. * * @param handle UART handle pointer. * @retval true RX ring buffer is full. * @retval false RX ring buffer is not full. */ static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle); /*! * @brief Read RX register using non-blocking method. * * This function reads data from the TX register directly, upper layer must make * sure the RX register is full or TX FIFO has data before calling this function. * * @param base UART peripheral base address. * @param data Start addresss of the buffer to store the received data. * @param length Size of the buffer. */ static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length); /*! * @brief Write to TX register using non-blocking method. * * This function writes data to the TX register directly, upper layer must make * sure the TX register is empty or TX FIFO has empty room before calling this function. * * @note This function does not check whether all the data has been sent out to bus, * so before disable TX, check kUART_TransmissionCompleteFlag to ensure the TX is * finished. * * @param base UART peripheral base address. * @param data Start address of the data to write. * @param length Size of the buffer to be sent. */ static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length); /******************************************************************************* * Variables ******************************************************************************/ /* Array of UART handle. */ #if (defined(UART5)) #define UART_HANDLE_ARRAY_SIZE 6 #else /* UART5 */ #if (defined(UART4)) #define UART_HANDLE_ARRAY_SIZE 5 #else /* UART4 */ #if (defined(UART3)) #define UART_HANDLE_ARRAY_SIZE 4 #else /* UART3 */ #if (defined(UART2)) #define UART_HANDLE_ARRAY_SIZE 3 #else /* UART2 */ #if (defined(UART1)) #define UART_HANDLE_ARRAY_SIZE 2 #else /* UART1 */ #if (defined(UART0)) #define UART_HANDLE_ARRAY_SIZE 1 #else /* UART0 */ #error No UART instance. #endif /* UART 0 */ #endif /* UART 1 */ #endif /* UART 2 */ #endif /* UART 3 */ #endif /* UART 4 */ #endif /* UART 5 */ static uart_handle_t *s_uartHandle[UART_HANDLE_ARRAY_SIZE]; /* Array of UART peripheral base address. */ static UART_Type *const s_uartBases[] = UART_BASE_PTRS; /* Array of UART IRQ number. */ static const IRQn_Type s_uartIRQ[] = UART_RX_TX_IRQS; #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) /* Array of UART clock name. */ static const clock_ip_name_t s_uartClock[] = UART_CLOCKS; #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ /* UART ISR for transactional APIs. */ static uart_isr_t s_uartIsr; /******************************************************************************* * Code ******************************************************************************/ /*! * brief Get the UART instance from peripheral base address. * * param base UART peripheral base address. * return UART instance. */ uint32_t UART_GetInstance(UART_Type *base) { uint32_t instance; uint32_t uartArrayCount = (sizeof(s_uartBases) / sizeof(s_uartBases[0])); /* Find the instance index from base address mappings. */ for (instance = 0; instance < uartArrayCount; instance++) { if (s_uartBases[instance] == base) { break; } } assert(instance < uartArrayCount); return instance; } /*! * brief Get the length of received data in RX ring buffer. * * param handle UART handle pointer. * return Length of received data in RX ring buffer. */ size_t UART_TransferGetRxRingBufferLength(uart_handle_t *handle) { assert(handle); size_t size; if (handle->rxRingBufferTail > handle->rxRingBufferHead) { size = (size_t)(handle->rxRingBufferHead + handle->rxRingBufferSize - handle->rxRingBufferTail); } else { size = (size_t)(handle->rxRingBufferHead - handle->rxRingBufferTail); } return size; } static bool UART_TransferIsRxRingBufferFull(uart_handle_t *handle) { assert(handle); bool full; if (UART_TransferGetRxRingBufferLength(handle) == (handle->rxRingBufferSize - 1U)) { full = true; } else { full = false; } return full; } /*! * brief Initializes a UART instance with a user configuration structure and peripheral clock. * * This function configures the UART module with the user-defined settings. The user can configure the configuration * structure and also get the default configuration by using the UART_GetDefaultConfig() function. * The example below shows how to use this API to configure UART. * code * uart_config_t uartConfig; * uartConfig.baudRate_Bps = 115200U; * uartConfig.parityMode = kUART_ParityDisabled; * uartConfig.stopBitCount = kUART_OneStopBit; * uartConfig.txFifoWatermark = 0; * uartConfig.rxFifoWatermark = 1; * UART_Init(UART1, &uartConfig, 20000000U); * endcode * * param base UART peripheral base address. * param config Pointer to the user-defined configuration structure. * param srcClock_Hz UART clock source frequency in HZ. * retval kStatus_UART_BaudrateNotSupport Baudrate is not support in current clock source. * retval kStatus_Success Status UART initialize succeed */ status_t UART_Init(UART_Type *base, const uart_config_t *config, uint32_t srcClock_Hz) { assert(config); assert(config->baudRate_Bps); #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO assert(FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->txFifoWatermark); assert(FSL_FEATURE_UART_FIFO_SIZEn(base) >= config->rxFifoWatermark); #endif uint16_t sbr = 0; uint8_t temp = 0; uint32_t baudDiff = 0; /* Calculate the baud rate modulo divisor, sbr*/ sbr = srcClock_Hz / (config->baudRate_Bps * 16); /* set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate */ if (sbr == 0) { sbr = 1; } #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT /* Determine if a fractional divider is needed to fine tune closer to the * desired baud, each value of brfa is in 1/32 increments, * hence the multiply-by-32. */ uint32_t tempBaud = 0; uint16_t brfa = (2 * srcClock_Hz / (config->baudRate_Bps)) - 32 * sbr; /* Calculate the baud rate based on the temporary SBR values and BRFA */ tempBaud = (srcClock_Hz * 2 / ((sbr * 32 + brfa))); baudDiff = (tempBaud > config->baudRate_Bps) ? (tempBaud - config->baudRate_Bps) : (config->baudRate_Bps - tempBaud); #else /* Calculate the baud rate based on the temporary SBR values */ baudDiff = (srcClock_Hz / (sbr * 16)) - config->baudRate_Bps; /* Select the better value between sbr and (sbr + 1) */ if (baudDiff > (config->baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1))))) { baudDiff = config->baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1))); sbr++; } #endif /* next, check to see if actual baud rate is within 3% of desired baud rate * based on the calculate SBR value */ if (baudDiff > ((config->baudRate_Bps / 100) * 3)) { /* Unacceptable baud rate difference of more than 3%*/ return kStatus_UART_BaudrateNotSupport; } #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) /* Enable uart clock */ CLOCK_EnableClock(s_uartClock[UART_GetInstance(base)]); #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ /* Disable UART TX RX before setting. */ base->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK); /* Write the sbr value to the BDH and BDL registers*/ base->BDH = (base->BDH & ~UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8); base->BDL = (uint8_t)sbr; #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT /* Write the brfa value to the register*/ base->C4 = (base->C4 & ~UART_C4_BRFA_MASK) | (brfa & UART_C4_BRFA_MASK); #endif /* Set bit count/parity mode/idle type. */ temp = base->C1 & ~(UART_C1_PE_MASK | UART_C1_PT_MASK | UART_C1_M_MASK | UART_C1_ILT_MASK); temp |= UART_C1_ILT(config->idleType); if (kUART_ParityDisabled != config->parityMode) { temp |= (UART_C1_M_MASK | (uint8_t)config->parityMode); } base->C1 = temp; #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT /* Set stop bit per char */ base->BDH = (base->BDH & ~UART_BDH_SBNS_MASK) | UART_BDH_SBNS((uint8_t)config->stopBitCount); #endif #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Set tx/rx FIFO watermark Note: Take care of the RX FIFO, RX interrupt request only assert when received bytes equal or more than RX water mark, there is potential issue if RX water mark larger than 1. For example, if RX FIFO water mark is 2, upper layer needs 5 bytes and 5 bytes are received. the last byte will be saved in FIFO but not trigger RX interrupt because the water mark is 2. */ base->TWFIFO = config->txFifoWatermark; base->RWFIFO = config->rxFifoWatermark; /* Enable tx/rx FIFO */ base->PFIFO |= (UART_PFIFO_TXFE_MASK | UART_PFIFO_RXFE_MASK); /* Flush FIFO */ base->CFIFO |= (UART_CFIFO_TXFLUSH_MASK | UART_CFIFO_RXFLUSH_MASK); #endif #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT if (config->enableRxRTS) { /* Enable receiver RTS(request-to-send) function. */ base->MODEM |= UART_MODEM_RXRTSE_MASK; } if (config->enableTxCTS) { /* Enable transmitter CTS(clear-to-send) function. */ base->MODEM |= UART_MODEM_TXCTSE_MASK; } #endif /* Enable TX/RX base on configure structure. */ temp = base->C2; if (config->enableTx) { temp |= UART_C2_TE_MASK; } if (config->enableRx) { temp |= UART_C2_RE_MASK; } base->C2 = temp; return kStatus_Success; } /*! * brief Deinitializes a UART instance. * * This function waits for TX complete, disables TX and RX, and disables the UART clock. * * param base UART peripheral base address. */ void UART_Deinit(UART_Type *base) { #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Wait tx FIFO send out*/ while (0 != base->TCFIFO) { } #endif /* Wait last char shoft out */ while (0 == (base->S1 & UART_S1_TC_MASK)) { } /* Disable the module. */ base->C2 = 0; #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) /* Disable uart clock */ CLOCK_DisableClock(s_uartClock[UART_GetInstance(base)]); #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */ } /*! * brief Gets the default configuration structure. * * This function initializes the UART configuration structure to a default value. The default * values are as follows. * uartConfig->baudRate_Bps = 115200U; * uartConfig->bitCountPerChar = kUART_8BitsPerChar; * uartConfig->parityMode = kUART_ParityDisabled; * uartConfig->stopBitCount = kUART_OneStopBit; * uartConfig->txFifoWatermark = 0; * uartConfig->rxFifoWatermark = 1; * uartConfig->idleType = kUART_IdleTypeStartBit; * uartConfig->enableTx = false; * uartConfig->enableRx = false; * * param config Pointer to configuration structure. */ void UART_GetDefaultConfig(uart_config_t *config) { assert(config); /* Initializes the configure structure to zero. */ memset(config, 0, sizeof(*config)); config->baudRate_Bps = 115200U; config->parityMode = kUART_ParityDisabled; #if defined(FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT) && FSL_FEATURE_UART_HAS_STOP_BIT_CONFIG_SUPPORT config->stopBitCount = kUART_OneStopBit; #endif #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO config->txFifoWatermark = 0; config->rxFifoWatermark = 1; #endif #if defined(FSL_FEATURE_UART_HAS_MODEM_SUPPORT) && FSL_FEATURE_UART_HAS_MODEM_SUPPORT config->enableRxRTS = false; config->enableTxCTS = false; #endif config->idleType = kUART_IdleTypeStartBit; config->enableTx = false; config->enableRx = false; } /*! * brief Sets the UART instance baud rate. * * This function configures the UART module baud rate. This function is used to update * the UART module baud rate after the UART module is initialized by the UART_Init. * code * UART_SetBaudRate(UART1, 115200U, 20000000U); * endcode * * param base UART peripheral base address. * param baudRate_Bps UART baudrate to be set. * param srcClock_Hz UART clock source freqency in Hz. * retval kStatus_UART_BaudrateNotSupport Baudrate is not support in the current clock source. * retval kStatus_Success Set baudrate succeeded. */ status_t UART_SetBaudRate(UART_Type *base, uint32_t baudRate_Bps, uint32_t srcClock_Hz) { assert(baudRate_Bps); uint16_t sbr = 0; uint32_t baudDiff = 0; uint8_t oldCtrl; /* Calculate the baud rate modulo divisor, sbr*/ sbr = srcClock_Hz / (baudRate_Bps * 16); /* set sbrTemp to 1 if the sourceClockInHz can not satisfy the desired baud rate */ if (sbr == 0) { sbr = 1; } #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT /* Determine if a fractional divider is needed to fine tune closer to the * desired baud, each value of brfa is in 1/32 increments, * hence the multiply-by-32. */ uint32_t tempBaud = 0; uint16_t brfa = (2 * srcClock_Hz / (baudRate_Bps)) - 32 * sbr; /* Calculate the baud rate based on the temporary SBR values and BRFA */ tempBaud = (srcClock_Hz * 2 / ((sbr * 32 + brfa))); baudDiff = (tempBaud > baudRate_Bps) ? (tempBaud - baudRate_Bps) : (baudRate_Bps - tempBaud); #else /* Calculate the baud rate based on the temporary SBR values */ baudDiff = (srcClock_Hz / (sbr * 16)) - baudRate_Bps; /* Select the better value between sbr and (sbr + 1) */ if (baudDiff > (baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1))))) { baudDiff = baudRate_Bps - (srcClock_Hz / (16 * (sbr + 1))); sbr++; } #endif /* next, check to see if actual baud rate is within 3% of desired baud rate * based on the calculate SBR value */ if (baudDiff < ((baudRate_Bps / 100) * 3)) { /* Store C2 before disable Tx and Rx */ oldCtrl = base->C2; /* Disable UART TX RX before setting. */ base->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK); /* Write the sbr value to the BDH and BDL registers*/ base->BDH = (base->BDH & ~UART_BDH_SBR_MASK) | (uint8_t)(sbr >> 8); base->BDL = (uint8_t)sbr; #if defined(FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT) && FSL_FEATURE_UART_HAS_BAUD_RATE_FINE_ADJUST_SUPPORT /* Write the brfa value to the register*/ base->C4 = (base->C4 & ~UART_C4_BRFA_MASK) | (brfa & UART_C4_BRFA_MASK); #endif /* Restore C2. */ base->C2 = oldCtrl; return kStatus_Success; } else { /* Unacceptable baud rate difference of more than 3%*/ return kStatus_UART_BaudrateNotSupport; } } /*! * brief Enables UART interrupts according to the provided mask. * * This function enables the UART interrupts according to the provided mask. The mask * is a logical OR of enumeration members. See ref _uart_interrupt_enable. * For example, to enable TX empty interrupt and RX full interrupt, do the following. * code * UART_EnableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable); * endcode * * param base UART peripheral base address. * param mask The interrupts to enable. Logical OR of ref _uart_interrupt_enable. */ void UART_EnableInterrupts(UART_Type *base, uint32_t mask) { mask &= kUART_AllInterruptsEnable; /* The interrupt mask is combined by control bits from several register: ((CFIFO<<24) | (C3<<16) | (C2<<8) |(BDH)) */ base->BDH |= mask; base->C2 |= (mask >> 8); base->C3 |= (mask >> 16); #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO base->CFIFO |= (mask >> 24); #endif } /*! * brief Disables the UART interrupts according to the provided mask. * * This function disables the UART interrupts according to the provided mask. The mask * is a logical OR of enumeration members. See ref _uart_interrupt_enable. * For example, to disable TX empty interrupt and RX full interrupt do the following. * code * UART_DisableInterrupts(UART1,kUART_TxDataRegEmptyInterruptEnable | kUART_RxDataRegFullInterruptEnable); * endcode * * param base UART peripheral base address. * param mask The interrupts to disable. Logical OR of ref _uart_interrupt_enable. */ void UART_DisableInterrupts(UART_Type *base, uint32_t mask) { mask &= kUART_AllInterruptsEnable; /* The interrupt mask is combined by control bits from several register: ((CFIFO<<24) | (C3<<16) | (C2<<8) |(BDH)) */ base->BDH &= ~mask; base->C2 &= ~(mask >> 8); base->C3 &= ~(mask >> 16); #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO base->CFIFO &= ~(mask >> 24); #endif } /*! * brief Gets the enabled UART interrupts. * * This function gets the enabled UART interrupts. The enabled interrupts are returned * as the logical OR value of the enumerators ref _uart_interrupt_enable. To check * a specific interrupts enable status, compare the return value with enumerators * in ref _uart_interrupt_enable. * For example, to check whether TX empty interrupt is enabled, do the following. * code * uint32_t enabledInterrupts = UART_GetEnabledInterrupts(UART1); * * if (kUART_TxDataRegEmptyInterruptEnable & enabledInterrupts) * { * ... * } * endcode * * param base UART peripheral base address. * return UART interrupt flags which are logical OR of the enumerators in ref _uart_interrupt_enable. */ uint32_t UART_GetEnabledInterrupts(UART_Type *base) { uint32_t temp; temp = base->BDH | ((uint32_t)(base->C2) << 8) | ((uint32_t)(base->C3) << 16); #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO temp |= ((uint32_t)(base->CFIFO) << 24); #endif return temp & kUART_AllInterruptsEnable; } /*! * brief Gets UART status flags. * * This function gets all UART status flags. The flags are returned as the logical * OR value of the enumerators ref _uart_flags. To check a specific status, * compare the return value with enumerators in ref _uart_flags. * For example, to check whether the TX is empty, do the following. * code * if (kUART_TxDataRegEmptyFlag & UART_GetStatusFlags(UART1)) * { * ... * } * endcode * * param base UART peripheral base address. * return UART status flags which are ORed by the enumerators in the _uart_flags. */ uint32_t UART_GetStatusFlags(UART_Type *base) { uint32_t status_flag; status_flag = base->S1 | ((uint32_t)(base->S2) << 8); #if defined(FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS) && FSL_FEATURE_UART_HAS_EXTENDED_DATA_REGISTER_FLAGS status_flag |= ((uint32_t)(base->ED) << 16); #endif #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO status_flag |= ((uint32_t)(base->SFIFO) << 24); #endif return status_flag; } /*! * brief Clears status flags with the provided mask. * * This function clears UART status flags with a provided mask. An automatically cleared flag * can't be cleared by this function. * These flags can only be cleared or set by hardware. * kUART_TxDataRegEmptyFlag, kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag, * kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag, kUART_ParityErrorInRxDataRegFlag, * kUART_TxFifoEmptyFlag,kUART_RxFifoEmptyFlag * Note that this API should be called when the Tx/Rx is idle. Otherwise it has no effect. * * param base UART peripheral base address. * param mask The status flags to be cleared; it is logical OR value of ref _uart_flags. * retval kStatus_UART_FlagCannotClearManually The flag can't be cleared by this function but * it is cleared automatically by hardware. * retval kStatus_Success Status in the mask is cleared. */ status_t UART_ClearStatusFlags(UART_Type *base, uint32_t mask) { uint8_t reg = base->S2; status_t status; #if defined(FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT) && FSL_FEATURE_UART_HAS_LIN_BREAK_DETECT reg &= ~(UART_S2_RXEDGIF_MASK | UART_S2_LBKDIF_MASK); #else reg &= ~UART_S2_RXEDGIF_MASK; #endif base->S2 = reg | (uint8_t)(mask >> 8); #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO base->SFIFO = (uint8_t)(mask >> 24); #endif if (mask & (kUART_IdleLineFlag | kUART_NoiseErrorFlag | kUART_FramingErrorFlag | kUART_ParityErrorFlag)) { /* Read base->D to clear the flags. */ (void)base->S1; (void)base->D; } if (mask & kUART_RxOverrunFlag) { /* Read base->D to clear the flags and Flush all data in FIFO. */ (void)base->S1; (void)base->D; #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */ base->CFIFO |= UART_CFIFO_RXFLUSH_MASK; #endif } /* If some flags still pending. */ if (mask & UART_GetStatusFlags(base)) { /* Some flags can only clear or set by the hardware itself, these flags are: kUART_TxDataRegEmptyFlag, kUART_TransmissionCompleteFlag, kUART_RxDataRegFullFlag, kUART_RxActiveFlag, kUART_NoiseErrorInRxDataRegFlag, kUART_ParityErrorInRxDataRegFlag, kUART_TxFifoEmptyFlag, kUART_RxFifoEmptyFlag. */ status = kStatus_UART_FlagCannotClearManually; } else { status = kStatus_Success; } return status; } /*! * brief Writes to the TX register using a blocking method. * * This function polls the TX register, waits for the TX register to be empty or for the TX FIFO * to have room and writes data to the TX buffer. * * note This function does not check whether all data is sent out to the bus. * Before disabling the TX, check kUART_TransmissionCompleteFlag to ensure that the TX is * finished. * * param base UART peripheral base address. * param data Start address of the data to write. * param length Size of the data to write. */ void UART_WriteBlocking(UART_Type *base, const uint8_t *data, size_t length) { /* This API can only ensure that the data is written into the data buffer but can't ensure all data in the data buffer are sent into the transmit shift buffer. */ while (length--) { while (!(base->S1 & UART_S1_TDRE_MASK)) { } base->D = *(data++); } } static void UART_WriteNonBlocking(UART_Type *base, const uint8_t *data, size_t length) { assert(data); size_t i; /* The Non Blocking write data API assume user have ensured there is enough space in peripheral to write. */ for (i = 0; i < length; i++) { base->D = data[i]; } } /*! * brief Read RX data register using a blocking method. * * This function polls the RX register, waits for the RX register to be full or for RX FIFO to * have data, and reads data from the TX register. * * param base UART peripheral base address. * param data Start address of the buffer to store the received data. * param length Size of the buffer. * retval kStatus_UART_RxHardwareOverrun Receiver overrun occurred while receiving data. * retval kStatus_UART_NoiseError A noise error occurred while receiving data. * retval kStatus_UART_FramingError A framing error occurred while receiving data. * retval kStatus_UART_ParityError A parity error occurred while receiving data. * retval kStatus_Success Successfully received all data. */ status_t UART_ReadBlocking(UART_Type *base, uint8_t *data, size_t length) { assert(data); uint32_t statusFlag; while (length--) { #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO while (!base->RCFIFO) #else while (!(base->S1 & UART_S1_RDRF_MASK)) #endif { statusFlag = UART_GetStatusFlags(base); if (statusFlag & kUART_RxOverrunFlag) { return kStatus_UART_RxHardwareOverrun; } if (statusFlag & kUART_NoiseErrorFlag) { return kStatus_UART_NoiseError; } if (statusFlag & kUART_FramingErrorFlag) { return kStatus_UART_FramingError; } if (statusFlag & kUART_ParityErrorFlag) { return kStatus_UART_ParityError; } } *(data++) = base->D; } return kStatus_Success; } static void UART_ReadNonBlocking(UART_Type *base, uint8_t *data, size_t length) { assert(data); size_t i; /* The Non Blocking read data API assume user have ensured there is enough space in peripheral to write. */ for (i = 0; i < length; i++) { data[i] = base->D; } } /*! * brief Initializes the UART handle. * * This function initializes the UART handle which can be used for other UART * transactional APIs. Usually, for a specified UART instance, * call this API once to get the initialized handle. * * param base UART peripheral base address. * param handle UART handle pointer. * param callback The callback function. * param userData The parameter of the callback function. */ void UART_TransferCreateHandle(UART_Type *base, uart_handle_t *handle, uart_transfer_callback_t callback, void *userData) { assert(handle); uint32_t instance; /* Zero the handle. */ memset(handle, 0, sizeof(*handle)); /* Set the TX/RX state. */ handle->rxState = kUART_RxIdle; handle->txState = kUART_TxIdle; /* Set the callback and user data. */ handle->callback = callback; handle->userData = userData; /* Get instance from peripheral base address. */ instance = UART_GetInstance(base); /* Save the handle in global variables to support the double weak mechanism. */ s_uartHandle[instance] = handle; s_uartIsr = UART_TransferHandleIRQ; /* Enable interrupt in NVIC. */ EnableIRQ(s_uartIRQ[instance]); } /*! * brief Sets up the RX ring buffer. * * This function sets up the RX ring buffer to a specific UART handle. * * When the RX ring buffer is used, data received are stored into the ring buffer even when the * user doesn't call the UART_TransferReceiveNonBlocking() API. If data is already received * in the ring buffer, the user can get the received data from the ring buffer directly. * * note When using the RX ring buffer, one byte is reserved for internal use. In other * words, if p ringBufferSize is 32, only 31 bytes are used for saving data. * * param base UART peripheral base address. * param handle UART handle pointer. * param ringBuffer Start address of the ring buffer for background receiving. Pass NULL to disable the ring buffer. * param ringBufferSize Size of the ring buffer. */ void UART_TransferStartRingBuffer(UART_Type *base, uart_handle_t *handle, uint8_t *ringBuffer, size_t ringBufferSize) { assert(handle); assert(ringBuffer); /* Setup the ringbuffer address */ handle->rxRingBuffer = ringBuffer; handle->rxRingBufferSize = ringBufferSize; handle->rxRingBufferHead = 0U; handle->rxRingBufferTail = 0U; /* Enable the interrupt to accept the data when user need the ring buffer. */ UART_EnableInterrupts( base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable); /* Enable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_EnableInterrupts(base, kUART_ParityErrorInterruptEnable); } } /*! * brief Aborts the background transfer and uninstalls the ring buffer. * * This function aborts the background transfer and uninstalls the ring buffer. * * param base UART peripheral base address. * param handle UART handle pointer. */ void UART_TransferStopRingBuffer(UART_Type *base, uart_handle_t *handle) { assert(handle); if (handle->rxState == kUART_RxIdle) { UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable); /* Disable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable); } } handle->rxRingBuffer = NULL; handle->rxRingBufferSize = 0U; handle->rxRingBufferHead = 0U; handle->rxRingBufferTail = 0U; } /*! * brief Transmits a buffer of data using the interrupt method. * * This function sends data using an interrupt method. This is a non-blocking function, which * returns directly without waiting for all data to be written to the TX register. When * all data is written to the TX register in the ISR, the UART driver calls the callback * function and passes the ref kStatus_UART_TxIdle as status parameter. * * note The kStatus_UART_TxIdle is passed to the upper layer when all data is written * to the TX register. However, it does not ensure that all data is sent out. Before disabling the TX, * check the kUART_TransmissionCompleteFlag to ensure that the TX is finished. * * param base UART peripheral base address. * param handle UART handle pointer. * param xfer UART transfer structure. See #uart_transfer_t. * retval kStatus_Success Successfully start the data transmission. * retval kStatus_UART_TxBusy Previous transmission still not finished; data not all written to TX register yet. * retval kStatus_InvalidArgument Invalid argument. */ status_t UART_TransferSendNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer) { assert(handle); assert(xfer); assert(xfer->dataSize); assert(xfer->data); status_t status; /* Return error if current TX busy. */ if (kUART_TxBusy == handle->txState) { status = kStatus_UART_TxBusy; } else { handle->txData = xfer->data; handle->txDataSize = xfer->dataSize; handle->txDataSizeAll = xfer->dataSize; handle->txState = kUART_TxBusy; /* Enable transmitter interrupt. */ UART_EnableInterrupts(base, kUART_TxDataRegEmptyInterruptEnable); status = kStatus_Success; } return status; } /*! * brief Aborts the interrupt-driven data transmit. * * This function aborts the interrupt-driven data sending. The user can get the remainBytes to find out * how many bytes are not sent out. * * param base UART peripheral base address. * param handle UART handle pointer. */ void UART_TransferAbortSend(UART_Type *base, uart_handle_t *handle) { assert(handle); UART_DisableInterrupts(base, kUART_TxDataRegEmptyInterruptEnable | kUART_TransmissionCompleteInterruptEnable); handle->txDataSize = 0; handle->txState = kUART_TxIdle; } /*! * brief Gets the number of bytes written to the UART TX register. * * This function gets the number of bytes written to the UART TX * register by using the interrupt method. * * param base UART peripheral base address. * param handle UART handle pointer. * param count Send bytes count. * retval kStatus_NoTransferInProgress No send in progress. * retval kStatus_InvalidArgument The parameter is invalid. * retval kStatus_Success Get successfully through the parameter \p count; */ status_t UART_TransferGetSendCount(UART_Type *base, uart_handle_t *handle, uint32_t *count) { assert(handle); assert(count); if (kUART_TxIdle == handle->txState) { return kStatus_NoTransferInProgress; } *count = handle->txDataSizeAll - handle->txDataSize; return kStatus_Success; } /*! * brief Receives a buffer of data using an interrupt method. * * This function receives data using an interrupt method. This is a non-blocking function, which * returns without waiting for all data to be received. * If the RX ring buffer is used and not empty, the data in the ring buffer is copied and * the parameter p receivedBytes shows how many bytes are copied from the ring buffer. * After copying, if the data in the ring buffer is not enough to read, the receive * request is saved by the UART driver. When the new data arrives, the receive request * is serviced first. When all data is received, the UART driver notifies the upper layer * through a callback function and passes the status parameter ref kStatus_UART_RxIdle. * For example, the upper layer needs 10 bytes but there are only 5 bytes in the ring buffer. * The 5 bytes are copied to the xfer->data and this function returns with the * parameter p receivedBytes set to 5. For the left 5 bytes, newly arrived data is * saved from the xfer->data[5]. When 5 bytes are received, the UART driver notifies the upper layer. * If the RX ring buffer is not enabled, this function enables the RX and RX interrupt * to receive data to the xfer->data. When all data is received, the upper layer is notified. * * param base UART peripheral base address. * param handle UART handle pointer. * param xfer UART transfer structure, see #uart_transfer_t. * param receivedBytes Bytes received from the ring buffer directly. * retval kStatus_Success Successfully queue the transfer into transmit queue. * retval kStatus_UART_RxBusy Previous receive request is not finished. * retval kStatus_InvalidArgument Invalid argument. */ status_t UART_TransferReceiveNonBlocking(UART_Type *base, uart_handle_t *handle, uart_transfer_t *xfer, size_t *receivedBytes) { assert(handle); assert(xfer); assert(xfer->data); assert(xfer->dataSize); uint32_t i; status_t status; /* How many bytes to copy from ring buffer to user memory. */ size_t bytesToCopy = 0U; /* How many bytes to receive. */ size_t bytesToReceive; /* How many bytes currently have received. */ size_t bytesCurrentReceived; /* How to get data: 1. If RX ring buffer is not enabled, then save xfer->data and xfer->dataSize to uart handle, enable interrupt to store received data to xfer->data. When all data received, trigger callback. 2. If RX ring buffer is enabled and not empty, get data from ring buffer first. If there are enough data in ring buffer, copy them to xfer->data and return. If there are not enough data in ring buffer, copy all of them to xfer->data, save the xfer->data remained empty space to uart handle, receive data to this empty space and trigger callback when finished. */ if (kUART_RxBusy == handle->rxState) { status = kStatus_UART_RxBusy; } else { bytesToReceive = xfer->dataSize; bytesCurrentReceived = 0U; /* If RX ring buffer is used. */ if (handle->rxRingBuffer) { /* Disable UART RX IRQ, protect ring buffer. */ UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable); /* How many bytes in RX ring buffer currently. */ bytesToCopy = UART_TransferGetRxRingBufferLength(handle); if (bytesToCopy) { bytesToCopy = MIN(bytesToReceive, bytesToCopy); bytesToReceive -= bytesToCopy; /* Copy data from ring buffer to user memory. */ for (i = 0U; i < bytesToCopy; i++) { xfer->data[bytesCurrentReceived++] = handle->rxRingBuffer[handle->rxRingBufferTail]; /* Wrap to 0. Not use modulo (%) because it might be large and slow. */ if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize) { handle->rxRingBufferTail = 0U; } else { handle->rxRingBufferTail++; } } } /* If ring buffer does not have enough data, still need to read more data. */ if (bytesToReceive) { /* No data in ring buffer, save the request to UART handle. */ handle->rxData = xfer->data + bytesCurrentReceived; handle->rxDataSize = bytesToReceive; handle->rxDataSizeAll = bytesToReceive; handle->rxState = kUART_RxBusy; } /* Enable UART RX IRQ if previously enabled. */ UART_EnableInterrupts(base, kUART_RxDataRegFullInterruptEnable); /* Call user callback since all data are received. */ if (0 == bytesToReceive) { if (handle->callback) { handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData); } } } /* Ring buffer not used. */ else { handle->rxData = xfer->data + bytesCurrentReceived; handle->rxDataSize = bytesToReceive; handle->rxDataSizeAll = bytesToReceive; handle->rxState = kUART_RxBusy; /* Enable RX/Rx overrun/framing error/idle line interrupt. */ UART_EnableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable); /* Enable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_EnableInterrupts(base, kUART_ParityErrorInterruptEnable); } } /* Return the how many bytes have read. */ if (receivedBytes) { *receivedBytes = bytesCurrentReceived; } status = kStatus_Success; } return status; } /*! * brief Aborts the interrupt-driven data receiving. * * This function aborts the interrupt-driven data receiving. The user can get the remainBytes to know * how many bytes are not received yet. * * param base UART peripheral base address. * param handle UART handle pointer. */ void UART_TransferAbortReceive(UART_Type *base, uart_handle_t *handle) { assert(handle); /* Only abort the receive to handle->rxData, the RX ring buffer is still working. */ if (!handle->rxRingBuffer) { /* Disable RX interrupt. */ UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable); /* Disable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable); } } handle->rxDataSize = 0U; handle->rxState = kUART_RxIdle; } /*! * brief Gets the number of bytes that have been received. * * This function gets the number of bytes that have been received. * * param base UART peripheral base address. * param handle UART handle pointer. * param count Receive bytes count. * retval kStatus_NoTransferInProgress No receive in progress. * retval kStatus_InvalidArgument Parameter is invalid. * retval kStatus_Success Get successfully through the parameter \p count; */ status_t UART_TransferGetReceiveCount(UART_Type *base, uart_handle_t *handle, uint32_t *count) { assert(handle); assert(count); if (kUART_RxIdle == handle->rxState) { return kStatus_NoTransferInProgress; } if (!count) { return kStatus_InvalidArgument; } *count = handle->rxDataSizeAll - handle->rxDataSize; return kStatus_Success; } /*! * brief UART IRQ handle function. * * This function handles the UART transmit and receive IRQ request. * * param base UART peripheral base address. * param handle UART handle pointer. */ void UART_TransferHandleIRQ(UART_Type *base, uart_handle_t *handle) { assert(handle); uint8_t count; uint8_t tempCount; uint32_t status = UART_GetStatusFlags(base); /* If RX framing error */ if (kUART_FramingErrorFlag & status) { /* Read base->D to clear framing error flag, otherwise the RX does not work. */ while (base->S1 & UART_S1_RDRF_MASK) { (void)base->D; } #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */ base->CFIFO |= UART_CFIFO_RXFLUSH_MASK; #endif handle->rxState = kUART_RxFramingError; handle->rxDataSize = 0U; /* Trigger callback. */ if (handle->callback) { handle->callback(base, handle, kStatus_UART_FramingError, handle->userData); } } /* If RX parity error */ if (kUART_ParityErrorFlag & status) { /* Read base->D to clear parity error flag, otherwise the RX does not work. */ while (base->S1 & UART_S1_RDRF_MASK) { (void)base->D; } #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */ base->CFIFO |= UART_CFIFO_RXFLUSH_MASK; #endif handle->rxState = kUART_RxParityError; handle->rxDataSize = 0U; /* Trigger callback. */ if (handle->callback) { handle->callback(base, handle, kStatus_UART_ParityError, handle->userData); } } /* If RX overrun. */ if (kUART_RxOverrunFlag & status) { /* Read base->D to clear overrun flag, otherwise the RX does not work. */ while (base->S1 & UART_S1_RDRF_MASK) { (void)base->D; } #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */ base->CFIFO |= UART_CFIFO_RXFLUSH_MASK; #endif /* Trigger callback. */ if (handle->callback) { handle->callback(base, handle, kStatus_UART_RxHardwareOverrun, handle->userData); } } /* If IDLE line was detected. */ if ((kUART_IdleLineFlag & status) && (UART_C2_ILIE_MASK & base->C2)) { #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* If still some data in the FIFO, read out these data to user data buffer. */ count = base->RCFIFO; /* If handle->rxDataSize is not 0, first save data to handle->rxData. */ while ((count) && (handle->rxDataSize)) { tempCount = MIN(handle->rxDataSize, count); /* Using non block API to read the data from the registers. */ UART_ReadNonBlocking(base, handle->rxData, tempCount); handle->rxData += tempCount; handle->rxDataSize -= tempCount; count -= tempCount; /* If all the data required for upper layer is ready, trigger callback. */ if (!handle->rxDataSize) { handle->rxState = kUART_RxIdle; /* Disable RX interrupt/overrun interrupt/fram error/idle line detected interrupt */ UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable); /* Disable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable); } if (handle->callback) { handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData); } } } #endif /* To clear IDLE, read UART status S1 with IDLE set and then read D.*/ while (UART_S1_IDLE_MASK & base->S1) { (void)base->D; } #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO /* Flush FIFO date, otherwise FIFO pointer will be in unknown state. */ base->CFIFO |= UART_CFIFO_RXFLUSH_MASK; #endif /* If rxDataSize is 0, disable idle line interrupt.*/ if (!(handle->rxDataSize)) { UART_DisableInterrupts(base, kUART_IdleLineInterruptEnable); } /* If callback is not NULL and rxDataSize is not 0. */ if ((handle->callback) && (handle->rxDataSize)) { handle->callback(base, handle, kStatus_UART_IdleLineDetected, handle->userData); } } /* Receive data register full */ if ((kUART_RxDataRegFullFlag & status) && (UART_C2_RIE_MASK & base->C2)) { /* Get the size that can be stored into buffer for this interrupt. */ #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO count = base->RCFIFO; #else count = 1; #endif /* If handle->rxDataSize is not 0, first save data to handle->rxData. */ while ((count) && (handle->rxDataSize)) { #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO tempCount = MIN(handle->rxDataSize, count); #else tempCount = 1; #endif /* Using non block API to read the data from the registers. */ UART_ReadNonBlocking(base, handle->rxData, tempCount); handle->rxData += tempCount; handle->rxDataSize -= tempCount; count -= tempCount; /* If all the data required for upper layer is ready, trigger callback. */ if (!handle->rxDataSize) { handle->rxState = kUART_RxIdle; if (handle->callback) { handle->callback(base, handle, kStatus_UART_RxIdle, handle->userData); } } } /* If use RX ring buffer, receive data to ring buffer. */ if (handle->rxRingBuffer) { while (count--) { /* If RX ring buffer is full, trigger callback to notify over run. */ if (UART_TransferIsRxRingBufferFull(handle)) { if (handle->callback) { handle->callback(base, handle, kStatus_UART_RxRingBufferOverrun, handle->userData); } } /* If ring buffer is still full after callback function, the oldest data is overrided. */ if (UART_TransferIsRxRingBufferFull(handle)) { /* Increase handle->rxRingBufferTail to make room for new data. */ if (handle->rxRingBufferTail + 1U == handle->rxRingBufferSize) { handle->rxRingBufferTail = 0U; } else { handle->rxRingBufferTail++; } } /* Read data. */ handle->rxRingBuffer[handle->rxRingBufferHead] = base->D; /* Increase handle->rxRingBufferHead. */ if (handle->rxRingBufferHead + 1U == handle->rxRingBufferSize) { handle->rxRingBufferHead = 0U; } else { handle->rxRingBufferHead++; } } } else if (!handle->rxDataSize) { /* Disable RX interrupt/overrun interrupt/fram error/idle line detected interrupt */ UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable); /* Disable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable); } } else { } } /* If framing error or parity error happened, stop the RX interrupt when ues no ring buffer */ if (((handle->rxState == kUART_RxFramingError) || (handle->rxState == kUART_RxParityError)) && (!handle->rxRingBuffer)) { UART_DisableInterrupts(base, kUART_RxDataRegFullInterruptEnable | kUART_RxOverrunInterruptEnable | kUART_FramingErrorInterruptEnable | kUART_IdleLineInterruptEnable); /* Disable parity error interrupt when parity mode is enable*/ if (UART_C1_PE_MASK & base->C1) { UART_DisableInterrupts(base, kUART_ParityErrorInterruptEnable); } } /* Send data register empty and the interrupt is enabled. */ if ((kUART_TxDataRegEmptyFlag & status) && (base->C2 & UART_C2_TIE_MASK)) { /* Get the bytes that available at this moment. */ #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO count = FSL_FEATURE_UART_FIFO_SIZEn(base) - base->TCFIFO; #else count = 1; #endif while ((count) && (handle->txDataSize)) { #if defined(FSL_FEATURE_UART_HAS_FIFO) && FSL_FEATURE_UART_HAS_FIFO tempCount = MIN(handle->txDataSize, count); #else tempCount = 1; #endif /* Using non block API to write the data to the registers. */ UART_WriteNonBlocking(base, handle->txData, tempCount); handle->txData += tempCount; handle->txDataSize -= tempCount; count -= tempCount; /* If all the data are written to data register, TX finished. */ if (!handle->txDataSize) { handle->txState = kUART_TxIdle; /* Disable TX register empty interrupt. */ base->C2 = (base->C2 & ~UART_C2_TIE_MASK); /* Trigger callback. */ if (handle->callback) { handle->callback(base, handle, kStatus_UART_TxIdle, handle->userData); } } } } } /*! * brief UART Error IRQ handle function. * * This function handles the UART error IRQ request. * * param base UART peripheral base address. * param handle UART handle pointer. */ void UART_TransferHandleErrorIRQ(UART_Type *base, uart_handle_t *handle) { /* To be implemented by User. */ } #if defined(UART0) #if ((!(defined(FSL_FEATURE_SOC_LPSCI_COUNT))) || \ ((defined(FSL_FEATURE_SOC_LPSCI_COUNT)) && (FSL_FEATURE_SOC_LPSCI_COUNT == 0))) void UART0_DriverIRQHandler(void) { s_uartIsr(UART0, s_uartHandle[0]); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } void UART0_RX_TX_DriverIRQHandler(void) { UART0_DriverIRQHandler(); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } #endif #endif #if defined(UART1) void UART1_DriverIRQHandler(void) { s_uartIsr(UART1, s_uartHandle[1]); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } void UART1_RX_TX_DriverIRQHandler(void) { UART1_DriverIRQHandler(); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } #endif #if defined(UART2) void UART2_DriverIRQHandler(void) { s_uartIsr(UART2, s_uartHandle[2]); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } void UART2_RX_TX_DriverIRQHandler(void) { UART2_DriverIRQHandler(); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } #endif #if defined(UART3) void UART3_DriverIRQHandler(void) { s_uartIsr(UART3, s_uartHandle[3]); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } void UART3_RX_TX_DriverIRQHandler(void) { UART3_DriverIRQHandler(); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } #endif #if defined(UART4) void UART4_DriverIRQHandler(void) { s_uartIsr(UART4, s_uartHandle[4]); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } void UART4_RX_TX_DriverIRQHandler(void) { UART4_DriverIRQHandler(); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } #endif #if defined(UART5) void UART5_DriverIRQHandler(void) { s_uartIsr(UART5, s_uartHandle[5]); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } void UART5_RX_TX_DriverIRQHandler(void) { UART5_DriverIRQHandler(); /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping exception return operation might vector to incorrect interrupt */ #if defined __CORTEX_M && (__CORTEX_M == 4U) __DSB(); #endif } #endif |