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 | /***************************************************************************//**
* @file em_usart.c
* @brief Universal synchronous/asynchronous receiver/transmitter (USART/UART)
* Peripheral API
* @version 5.6.0
*******************************************************************************
* # License
* <b>Copyright 2016 Silicon Laboratories, Inc. www.silabs.com</b>
*******************************************************************************
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
* obligation to support this Software. Silicon Labs is providing the
* Software "AS IS", with no express or implied warranties of any kind,
* including, but not limited to, any implied warranties of merchantability
* or fitness for any particular purpose or warranties against infringement
* of any proprietary rights of a third party.
*
* Silicon Labs will not be liable for any consequential, incidental, or
* special damages, or any other relief, or for any claim by any third party,
* arising from your use of this Software.
*
******************************************************************************/
#include "em_usart.h"
#if defined(USART_COUNT) && (USART_COUNT > 0)
#include "em_cmu.h"
#include "em_bus.h"
#include "em_assert.h"
#if defined(USART_CTRLX_CTSEN)
#include "em_gpio.h"
#endif
/***************************************************************************//**
* @addtogroup emlib
* @{
******************************************************************************/
/***************************************************************************//**
* @addtogroup USART
* @{
******************************************************************************/
/*******************************************************************************
******************************* DEFINES ***********************************
******************************************************************************/
/** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
/** Validation of USART register block pointer reference for assert statements. */
#if (USART_COUNT == 1) && defined(USART0)
#define USART_REF_VALID(ref) ((ref) == USART0)
#elif (USART_COUNT == 1) && defined(USART1)
#define USART_REF_VALID(ref) ((ref) == USART1)
#elif (USART_COUNT == 2) && defined(USART2)
#define USART_REF_VALID(ref) (((ref) == USART1) || ((ref) == USART2))
#elif (USART_COUNT == 2)
#define USART_REF_VALID(ref) (((ref) == USART0) || ((ref) == USART1))
#elif (USART_COUNT == 3)
#define USART_REF_VALID(ref) (((ref) == USART0) || ((ref) == USART1) \
|| ((ref) == USART2))
#elif (USART_COUNT == 4)
#define USART_REF_VALID(ref) (((ref) == USART0) || ((ref) == USART1) \
|| ((ref) == USART2) || ((ref) == USART3))
#elif (USART_COUNT == 5)
#define USART_REF_VALID(ref) (((ref) == USART0) || ((ref) == USART1) \
|| ((ref) == USART2) || ((ref) == USART3) \
|| ((ref) == USART4))
#elif (USART_COUNT == 6)
#define USART_REF_VALID(ref) (((ref) == USART0) || ((ref) == USART1) \
|| ((ref) == USART2) || ((ref) == USART3) \
|| ((ref) == USART4) || ((ref) == USART5))
#else
#error "Undefined number of USARTs."
#endif
#if defined(USARTRF_COUNT) && (USARTRF_COUNT > 0)
#if (USARTRF_COUNT == 1) && defined(USARTRF0)
#define USARTRF_REF_VALID(ref) ((ref) == USARTRF0)
#elif (USARTRF_COUNT == 1) && defined(USARTRF1)
#define USARTRF_REF_VALID(ref) ((ref) == USARTRF1)
#else
#define USARTRF_REF_VALID(ref) (0)
#endif
#else
#define USARTRF_REF_VALID(ref) (0)
#endif
#if (_SILICON_LABS_32B_SERIES == 2)
#define USART_IRDA_VALID(ref) USART_REF_VALID(ref)
#elif defined(_SILICON_LABS_32B_SERIES_1)
#if defined(_SILICON_LABS_GECKO_INTERNAL_SDID_100) || defined(_SILICON_LABS_GECKO_INTERNAL_SDID_103)
// If GG11 or TG11
#define USART_IRDA_VALID(ref) (((ref) == USART0) || ((ref) == USART2))
#elif defined(USART3)
#define USART_IRDA_VALID(ref) (((ref) == USART0) || ((ref) == USART1) || ((ref) == USART2) || ((ref) == USART3))
#elif defined(USART2)
#define USART_IRDA_VALID(ref) (((ref) == USART0) || ((ref) == USART1) || ((ref) == USART2))
#else
#define USART_IRDA_VALID(ref) (((ref) == USART0) || ((ref) == USART1))
#endif
#elif defined(_SILICON_LABS_32B_SERIES_0)
#if defined(_EZR32_HAPPY_FAMILY)
#define USART_IRDA_VALID(ref) ((ref) == USART0)
#elif defined(_EFM32_HAPPY_FAMILY)
#define USART_IRDA_VALID(ref) (((ref) == USART0) || ((ref) == USART1))
#elif defined(USART0)
#define USART_IRDA_VALID(ref) ((ref) == USART0)
#elif (USART_COUNT == 1) && defined(USART1)
#define USART_IRDA_VALID(ref) ((ref) == USART1)
#elif defined(USARTRF0)
#define USART_IRDA_VALID(ref) ((ref) == USARTRF0)
#else
#define USART_IRDA_VALID(ref) (0)
#endif
#endif
#if (_SILICON_LABS_32B_SERIES == 2)
#define USART_I2S_VALID(ref) USART_REF_VALID(ref)
#elif defined(_SILICON_LABS_32B_SERIES_1)
#if defined(USART4)
#define USART_I2S_VALID(ref) (((ref) == USART1) || ((ref) == USART3) || ((ref) == USART4))
#elif defined(USART3)
#define USART_I2S_VALID(ref) (((ref) == USART1) || ((ref) == USART3))
#else
#define USART_I2S_VALID(ref) ((ref) == USART1)
#endif
#elif defined(_SILICON_LABS_32B_SERIES_0)
#if defined(_EZR32_HAPPY_FAMILY)
#define USART_I2S_VALID(ref) ((ref) == USART0)
#elif defined(_EFM32_HAPPY_FAMILY)
#define USART_I2S_VALID(ref) (((ref) == USART0) || ((ref) == USART1))
#elif defined(_EFM32_TINY_FAMILY) || defined(_EFM32_ZERO_FAMILY)
#define USART_I2S_VALID(ref) ((ref) == USART1)
#elif defined(_EFM32_GIANT_FAMILY) || defined(_EFM32_WONDER_FAMILY)
#define USART_I2S_VALID(ref) (((ref) == USART1) || ((ref) == USART2))
#endif
#endif
#if (UART_COUNT == 1)
#define UART_REF_VALID(ref) ((ref) == UART0)
#elif (UART_COUNT == 2)
#define UART_REF_VALID(ref) (((ref) == UART0) || ((ref) == UART1))
#else
#define UART_REF_VALID(ref) (0)
#endif
#if defined(_USART_CLKDIV_DIVEXT_MASK)
#define CLKDIV_MASK (_USART_CLKDIV_DIV_MASK | _USART_CLKDIV_DIVEXT_MASK)
#else
#define CLKDIV_MASK _USART_CLKDIV_DIV_MASK
#endif
/** @endcond */
/*******************************************************************************
************************** LOCAL FUNCTIONS ********************************
******************************************************************************/
#if !defined(_EFM32_GECKO_FAMILY)
/***************************************************************************//**
* @brief
* Configure a PRS channel as USART Rx input
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] ch
* PRS channel.
******************************************************************************/
static void prsRxInput(USART_TypeDef *usart, USART_PRS_Channel_t ch)
{
#if defined(_USART_INPUT_MASK)
usart->INPUT = ((uint32_t)ch << _USART_INPUT_RXPRSSEL_SHIFT)
| USART_INPUT_RXPRS;
#elif defined(USART_CTRLX_RXPRSEN)
if (usart == USART0) {
PRS->CONSUMER_USART0_RX = ch;
} else if (usart == USART1) {
PRS->CONSUMER_USART1_RX = ch;
} else if (usart == USART2) {
PRS->CONSUMER_USART2_RX = ch;
}
usart->CTRLX |= USART_CTRLX_RXPRSEN;
#endif
}
#endif
/***************************************************************************//**
* @brief
* Configure a PRS channel as USART Ir input
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] ch
* PRS channel.
******************************************************************************/
static void prsIrInput(USART_TypeDef *usart, USART_PRS_Channel_t ch)
{
#if defined(_USART_IRCTRL_IRPRSSEL_MASK)
usart->IRCTRL |= ((uint32_t)ch << _USART_IRCTRL_IRPRSSEL_SHIFT)
| USART_IRCTRL_IRPRSEN;
#else
if (usart == USART0) {
PRS->CONSUMER_USART0_IR = ch;
} else if (usart == USART1) {
PRS->CONSUMER_USART1_IR = ch;
} else if (usart == USART2) {
PRS->CONSUMER_USART2_IR = ch;
}
usart->IRCTRL |= USART_IRCTRL_IRPRSEN;
#endif
}
/***************************************************************************//**
* @brief
* Configure a PRS channel as USART Trigger input
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] ch
* PRS channel.
******************************************************************************/
static void prsTriggerInput(USART_TypeDef *usart, USART_PRS_Channel_t ch)
{
#if defined(_USART_IRCTRL_IRPRSSEL_MASK)
usart->TRIGCTRL = (usart->TRIGCTRL & ~_USART_TRIGCTRL_TSEL_MASK)
| (ch << _USART_TRIGCTRL_TSEL_SHIFT);
#else
if (usart == USART0) {
PRS->CONSUMER_USART0_TRIGGER = ch;
} else if (usart == USART1) {
PRS->CONSUMER_USART1_TRIGGER = ch;
} else if (usart == USART2) {
PRS->CONSUMER_USART2_TRIGGER = ch;
}
#endif
}
/*******************************************************************************
************************** GLOBAL FUNCTIONS *******************************
******************************************************************************/
/***************************************************************************//**
* @brief
* Configure USART/UART operating in asynchronous mode to use a given
* baudrate (or as close as possible to a specified baudrate).
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] refFreq
* USART/UART reference clock frequency in Hz. If set to 0,
* the currently configured reference clock is assumed.
*
* @param[in] baudrate
* Baudrate to try to achieve for USART/UART.
*
* @param[in] ovs
* Oversampling to be used. Normal is 16x oversampling but lower oversampling
* may be used to achieve higher rates or better baudrate accuracy in some
* cases. Notice that lower oversampling frequency makes the channel more
* vulnerable to bit faults during reception due to clock inaccuracies
* compared to the link partner.
******************************************************************************/
void USART_BaudrateAsyncSet(USART_TypeDef *usart,
uint32_t refFreq,
uint32_t baudrate,
USART_OVS_TypeDef ovs)
{
uint32_t clkdiv;
uint32_t oversample;
/* Inhibit divide by 0 */
EFM_ASSERT(baudrate);
/*
* Use integer division to avoid forcing in float division
* utils and yet keep rounding effect errors to a minimum.
*
* CLKDIV in asynchronous mode is given by:
*
* CLKDIV = 256 * (fHFPERCLK/(oversample * br) - 1)
* or
* CLKDIV = (256 * fHFPERCLK)/(oversample * br) - 256
*
* The basic problem with integer division in the above formula is that
* the dividend (256 * fHFPERCLK) may become higher than max 32 bit
* integer. Yet, we want to evaluate the dividend first before dividing
* to get as small rounding effects as possible.
* Too harsh restrictions on maximum fHFPERCLK value should not be made.
*
* It is possible to factorize 256 and oversample/br. However,
* since the last 6 or 3 bits of CLKDIV are don't care, base the
* integer arithmetic on the below formula
*
* CLKDIV / 64 = (4 * fHFPERCLK)/(oversample * br) - 4 (3 bits dont care)
* or
* CLKDIV / 8 = (32 * fHFPERCLK)/(oversample * br) - 32 (6 bits dont care)
*
* and calculate 1/64 of CLKDIV first. This allows for fHFPERCLK
* up to 1 GHz without overflowing a 32 bit value.
*/
/* HFPERCLK used to clock all USART/UART peripheral modules. */
if (!refFreq) {
#if defined(_SILICON_LABS_32B_SERIES_2)
refFreq = CMU_ClockFreqGet(cmuClock_PCLK);
#else
refFreq = CMU_ClockFreqGet(cmuClock_HFPER);
#endif
}
/* Map oversampling. */
switch (ovs) {
case usartOVS16:
EFM_ASSERT(baudrate <= (refFreq / 16));
oversample = 16;
break;
case usartOVS8:
EFM_ASSERT(baudrate <= (refFreq / 8));
oversample = 8;
break;
case usartOVS6:
EFM_ASSERT(baudrate <= (refFreq / 6));
oversample = 6;
break;
case usartOVS4:
EFM_ASSERT(baudrate <= (refFreq / 4));
oversample = 4;
break;
default:
/* Invalid input */
EFM_ASSERT(0);
return;
}
/* Calculate and set CLKDIV with fractional bits.
* The added (oversample*baudrate)/2 in the first line is to round the
* divisor to the nearest fractional divisor. */
#if defined(_SILICON_LABS_32B_SERIES_0) && !defined(_EFM32_HAPPY_FAMILY)
/* Devices with 2 fractional bits. CLKDIV[7:6] */
clkdiv = 4 * refFreq + (oversample * baudrate) / 2;
clkdiv /= oversample * baudrate;
clkdiv -= 4;
clkdiv *= 64;
#else
/* Devices with 5 fractional bits. CLKDIV[7:3] */
clkdiv = 32 * refFreq + (oversample * baudrate) / 2;
clkdiv /= oversample * baudrate;
clkdiv -= 32;
clkdiv *= 8;
#endif
/* Verify that the resulting clock divider is within limits. */
EFM_ASSERT(clkdiv <= CLKDIV_MASK);
/* Make sure that reserved bits are not written to. */
clkdiv &= CLKDIV_MASK;
usart->CTRL &= ~_USART_CTRL_OVS_MASK;
usart->CTRL |= ovs;
usart->CLKDIV = clkdiv;
}
/***************************************************************************//**
* @brief
* Calculate baudrate for USART/UART given reference frequency, clock division,
* and oversampling rate (if async mode).
*
* @details
* This function returns the baudrate that a USART/UART module will use if
* configured with the given frequency, clock divisor, and mode. Notice that
* this function will not use the hardware configuration. It can be used
* to determine if a given configuration is sufficiently accurate for the
* application.
*
* @param[in] refFreq
* USART/UART HF peripheral frequency used.
*
* @param[in] clkdiv
* A clock division factor to be used.
*
* @param[in] syncmode
* @li True - synchronous mode operation.
* @li False - asynchronous mode operation.
*
* @param[in] ovs
* Oversampling used if in asynchronous mode. Not used if @p syncmode is true.
*
* @return
* Baudrate with given settings.
******************************************************************************/
uint32_t USART_BaudrateCalc(uint32_t refFreq,
uint32_t clkdiv,
bool syncmode,
USART_OVS_TypeDef ovs)
{
uint32_t oversample;
uint64_t divisor;
uint64_t factor;
uint64_t remainder;
uint64_t quotient;
uint32_t br;
/* Out of bound clkdiv. */
EFM_ASSERT(clkdiv <= CLKDIV_MASK);
/* Mask out unused bits */
clkdiv &= CLKDIV_MASK;
/* Use integer division to avoid forcing in float division */
/* utils and yet keep rounding effect errors to a minimum. */
/* Baudrate calculation depends on if synchronous or asynchronous mode. */
if (syncmode) {
/*
* Baudrate is given by:
*
* br = fHFPERCLK/(2 * (1 + (CLKDIV / 256)))
*
* which can be rewritten to
*
* br = (128 * fHFPERCLK)/(256 + CLKDIV)
*/
oversample = 1; /* Not used in sync mode, i.e., 1 */
factor = 128;
} else {
/*
* Baudrate in asynchronous mode is given by:
*
* br = fHFPERCLK/(oversample * (1 + (CLKDIV / 256)))
*
* which can be rewritten to
*
* br = (256 * fHFPERCLK)/(oversample * (256 + CLKDIV))
*
* 256 factor of the dividend is reduced with a
* (part of) oversample part of the divisor.
*/
switch (ovs) {
case usartOVS16:
oversample = 1;
factor = 256 / 16;
break;
case usartOVS8:
oversample = 1;
factor = 256 / 8;
break;
case usartOVS6:
oversample = 3;
factor = 256 / 2;
break;
default:
oversample = 1;
factor = 256 / 4;
break;
}
}
/*
* The basic problem with integer division in the above formula is that
* the dividend (factor * fHFPERCLK) may become larger than a 32 bit
* integer. Yet we want to evaluate the dividend first before dividing
* to get as small rounding effects as possible. Too harsh restrictions
* should not be made on the maximum fHFPERCLK value either.
*
* For division a/b,
*
* a = qb + r
*
* where q is the quotient and r is the remainder, both integers.
*
* The original baudrate formula can be rewritten as
*
* br = xa / b = x(qb + r)/b = xq + xr/b
*
* where x is 'factor', a is 'refFreq' and b is 'divisor', referring to
* variable names.
*/
/*
* The divisor will never exceed max 32 bit value since
* clkdiv <= _USART_CLKDIV_DIV_MASK (currently 0x1FFFC0 or 0x7FFFF8)
* and 'oversample' has been reduced to <= 3.
*/
divisor = oversample * (256 + clkdiv);
quotient = refFreq / divisor;
remainder = refFreq % divisor;
/* The factor <= 128 and since divisor >= 256, the below cannot exceed the maximum */
/* 32 bit value. However, factor * remainder can become larger than 32-bit */
/* because of the size of _USART_CLKDIV_DIV_MASK on some families. */
br = (uint32_t)(factor * quotient);
/*
* The factor <= 128 and remainder < (oversample*(256 + clkdiv)), which
* means dividend (factor * remainder) worst case is
* 128 * (3 * (256 + _USART_CLKDIV_DIV_MASK)) = 0x1_8001_7400.
*/
br += (uint32_t)((factor * remainder) / divisor);
return br;
}
/***************************************************************************//**
* @brief
* Get the current baudrate for USART/UART.
*
* @details
* This function returns the actual baudrate (not considering oscillator
* inaccuracies) used by a USART/UART peripheral.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @return
* The current baudrate.
******************************************************************************/
uint32_t USART_BaudrateGet(USART_TypeDef *usart)
{
uint32_t freq;
USART_OVS_TypeDef ovs;
bool syncmode;
if (usart->CTRL & USART_CTRL_SYNC) {
syncmode = true;
} else {
syncmode = false;
}
/* HFPERCLK used to clock all USART/UART peripheral modules. */
#if defined(_SILICON_LABS_32B_SERIES_2)
freq = CMU_ClockFreqGet(cmuClock_PCLK);
#else
freq = CMU_ClockFreqGet(cmuClock_HFPER);
#endif
ovs = (USART_OVS_TypeDef)(usart->CTRL & _USART_CTRL_OVS_MASK);
return USART_BaudrateCalc(freq, usart->CLKDIV, syncmode, ovs);
}
/***************************************************************************//**
* @brief
* Configure the USART operating in synchronous mode to use a given baudrate
* (or as close as possible to a specified baudrate).
*
* @details
* The configuration will be set to use a baudrate <= the specified baudrate
* to ensure that the baudrate does not exceed the specified value.
*
* The fractional clock division is suppressed, although the hardware design allows it.
* It could cause half clock cycles to exceed a specified limit and thus
* potentially violate specifications for the slave device. In some special
* situations, a fractional clock division may be useful even in synchronous
* mode, but in those cases it must be directly adjusted, possibly assisted
* by USART_BaudrateCalc():
*
* @param[in] usart
* A pointer to the USART peripheral register block. (Cannot be used on UART
* modules.)
*
* @param[in] refFreq
* A USART reference clock frequency in Hz that will be used. If set to 0,
* the currently-configured reference clock is assumed.
*
* @param[in] baudrate
* Baudrate to try to achieve for USART.
******************************************************************************/
void USART_BaudrateSyncSet(USART_TypeDef *usart, uint32_t refFreq, uint32_t baudrate)
{
uint32_t clkdiv;
/* Prevent dividing by 0. */
EFM_ASSERT(baudrate);
/*
* CLKDIV in synchronous mode is given by:
*
* CLKDIV = 256 * (fHFPERCLK/(2 * br) - 1)
*/
/* HFPERCLK used to clock all USART/UART peripheral modules. */
if (!refFreq) {
#if defined(_SILICON_LABS_32B_SERIES_2)
refFreq = CMU_ClockFreqGet(cmuClock_PCLK);
#else
refFreq = CMU_ClockFreqGet(cmuClock_HFPER);
#endif
}
clkdiv = (refFreq - 1) / (2 * baudrate);
clkdiv = clkdiv << 8;
/* Verify that resulting clock divider is within limits. */
EFM_ASSERT(!(clkdiv & ~CLKDIV_MASK));
usart->CLKDIV = clkdiv;
}
/***************************************************************************//**
* @brief
* Enable/disable USART/UART receiver and/or transmitter.
*
* @details
* Notice that this function does not do any configuration. Enabling should
* normally be done after initialization (if not enabled as part
* of initialization).
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] enable
* Select the status for the receiver/transmitter.
******************************************************************************/
void USART_Enable(USART_TypeDef *usart, USART_Enable_TypeDef enable)
{
uint32_t tmp;
/* Make sure the module exists on the selected chip. */
EFM_ASSERT(USART_REF_VALID(usart)
|| USARTRF_REF_VALID(usart)
|| UART_REF_VALID(usart));
#if defined(USART_EN_EN)
usart->EN_SET = USART_EN_EN;
#endif
/* Disable as specified. */
tmp = ~((uint32_t)enable);
tmp &= _USART_CMD_RXEN_MASK | _USART_CMD_TXEN_MASK;
usart->CMD = tmp << 1;
/* Enable as specified. */
usart->CMD = (uint32_t)enable;
#if defined(USART_EN_EN)
if (enable == usartDisable) {
usart->EN_CLR = USART_EN_EN;
}
#endif
}
/***************************************************************************//**
* @brief
* Initialize USART/UART for normal asynchronous mode.
*
* @details
* This function will configure basic settings to operate in normal
* asynchronous mode.
*
* A special control setup not covered by this function must be done after
* using this function by direct modification of the CTRL register.
*
* Notice that pins used by the USART/UART module must be properly configured
* by the user explicitly for the USART/UART to work as intended.
* (When configuring pins, remember to consider the sequence of
* configuration to avoid unintended pulses/glitches on output
* pins.)
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] init
* A pointer to the initialization structure used to configure the basic async setup.
******************************************************************************/
void USART_InitAsync(USART_TypeDef *usart, const USART_InitAsync_TypeDef *init)
{
/* Make sure the module exists on the selected chip. */
EFM_ASSERT(USART_REF_VALID(usart)
|| USARTRF_REF_VALID(usart)
|| UART_REF_VALID(usart));
/* Initialize USART registers to hardware reset state. */
USART_Reset(usart);
#if defined(USART_EN_EN)
usart->EN_SET = USART_EN_EN;
#endif
#if defined(USART_CTRL_MVDIS)
/* Disable the majority vote if specified. */
if (init->mvdis) {
usart->CTRL |= USART_CTRL_MVDIS;
}
#endif
#if !defined(_EFM32_GECKO_FAMILY)
/* Configure the PRS input mode. */
if (init->prsRxEnable) {
prsRxInput(usart, init->prsRxCh);
}
#endif
/* Configure databits, stopbits, and parity. */
usart->FRAME = (uint32_t)init->databits
| (uint32_t)init->stopbits
| (uint32_t)init->parity;
/* Configure baudrate. */
USART_BaudrateAsyncSet(usart, init->refFreq, init->baudrate, init->oversampling);
if (init->autoCsEnable) {
usart->CTRL |= USART_CTRL_AUTOCS;
}
#if defined(_USART_TIMING_CSHOLD_MASK)
usart->TIMING = ((init->autoCsHold << _USART_TIMING_CSHOLD_SHIFT)
& _USART_TIMING_CSHOLD_MASK)
| ((init->autoCsSetup << _USART_TIMING_CSSETUP_SHIFT)
& _USART_TIMING_CSSETUP_MASK);
#endif
#if defined(_USART_ROUTEPEN_RTSPEN_MASK) && defined(_USART_ROUTEPEN_CTSPEN_MASK)
usart->ROUTEPEN &= ~(_USART_ROUTEPEN_RTSPEN_MASK | _USART_ROUTEPEN_CTSPEN_MASK);
usart->ROUTEPEN |= init->hwFlowControl;
#elif defined(USART_CTRLX_CTSEN)
if ((init->hwFlowControl == usartHwFlowControlRts)
|| (init->hwFlowControl == usartHwFlowControlCtsAndRts)) {
GPIO->USARTROUTE_SET[USART_NUM(usart)].ROUTEEN = GPIO_USART_ROUTEEN_RTSPEN;
} else {
GPIO->USARTROUTE_CLR[USART_NUM(usart)].ROUTEEN = GPIO_USART_ROUTEEN_RTSPEN;
}
if ((init->hwFlowControl == usartHwFlowControlCts)
|| (init->hwFlowControl == usartHwFlowControlCtsAndRts)) {
usart->CTRLX_SET = USART_CTRLX_CTSEN;
} else {
usart->CTRLX_CLR = USART_CTRLX_CTSEN;
}
#endif
/* Finally, enable (as specified). */
usart->CMD = (uint32_t)init->enable;
}
/***************************************************************************//**
* @brief
* Initialize USART for synchronous mode.
*
* @details
* This function will configure basic settings to operate in
* synchronous mode.
*
* A special control setup not covered by this function must be done after
* using this function by direct modification of the CTRL register.
*
* Notice that pins used by the USART module must be properly configured
* by the user explicitly for the USART to work as intended.
* (When configuring pins remember to consider the sequence of
* configuration to avoid unintended pulses/glitches on output
* pins.)
*
* @param[in] usart
* A pointer to the USART peripheral register block. (UART does not support this
* mode.)
*
* @param[in] init
* A pointer to the initialization structure used to configure basic async setup.
******************************************************************************/
void USART_InitSync(USART_TypeDef *usart, const USART_InitSync_TypeDef *init)
{
/* Make sure the module exists on the selected chip. */
EFM_ASSERT(USART_REF_VALID(usart) || USARTRF_REF_VALID(usart) );
/* Initialize USART registers to hardware reset state. */
USART_Reset(usart);
#if defined(USART_EN_EN)
usart->EN_SET = USART_EN_EN;
#endif
/* Set bits for synchronous mode. */
usart->CTRL |= (USART_CTRL_SYNC)
| (uint32_t)init->clockMode
| (init->msbf ? USART_CTRL_MSBF : 0);
#if defined(_USART_CTRL_AUTOTX_MASK)
usart->CTRL |= init->autoTx ? USART_CTRL_AUTOTX : 0;
#endif
#if !defined(_EFM32_GECKO_FAMILY)
if (init->prsRxEnable) {
prsRxInput(usart, init->prsRxCh);
}
#endif
/* Configure databits, leave stopbits and parity at reset default (not used). */
usart->FRAME = (uint32_t)init->databits
| USART_FRAME_STOPBITS_DEFAULT
| USART_FRAME_PARITY_DEFAULT;
/* Configure the baudrate. */
USART_BaudrateSyncSet(usart, init->refFreq, init->baudrate);
/* Finally, enable (as specified). */
if (init->master) {
usart->CMD = USART_CMD_MASTEREN;
}
if (init->autoCsEnable) {
usart->CTRL |= USART_CTRL_AUTOCS;
}
#if defined(_USART_TIMING_CSHOLD_MASK)
usart->TIMING = ((init->autoCsHold << _USART_TIMING_CSHOLD_SHIFT)
& _USART_TIMING_CSHOLD_MASK)
| ((init->autoCsSetup << _USART_TIMING_CSSETUP_SHIFT)
& _USART_TIMING_CSSETUP_MASK);
#endif
usart->CMD = (uint32_t)init->enable;
}
/***************************************************************************//**
* @brief
* Initialize USART for asynchronous IrDA mode.
*
* @details
* This function will configure basic settings to operate in
* asynchronous IrDA mode.
*
* A special control setup not covered by this function must be done after
* using this function by direct modification of the CTRL and IRCTRL
* registers.
*
* Notice that pins used by the USART/UART module must be properly configured
* by the user explicitly for the USART/UART to work as intended.
* (When configuring pins, remember to consider the sequence of
* configuration to avoid unintended pulses/glitches on output
* pins.)
*
* @param[in] usart
* A pointer to the USART peripheral register block.
*
* @param[in] init
* A pointer to the initialization structure used to configure async IrDA setup.
*
* @note
* Not all USART instances support IrDA. See the data sheet for your device.
*
******************************************************************************/
void USARTn_InitIrDA(USART_TypeDef *usart, const USART_InitIrDA_TypeDef *init)
{
EFM_ASSERT(USART_IRDA_VALID(usart));
/* Initialize USART as an async device. */
USART_InitAsync(usart, &(init->async));
/* Set IrDA modulation to RZI (return-to-zero-inverted). */
usart->CTRL |= USART_CTRL_TXINV;
/* Invert the Rx signal before the demodulator if enabled. */
if (init->irRxInv) {
usart->CTRL |= USART_CTRL_RXINV;
}
/* Configure IrDA. */
usart->IRCTRL = (uint32_t)init->irPw
| ((init->irFilt ? 1UL : 0UL) << _USART_IRCTRL_IRFILT_SHIFT);
if (init->irPrsEn) {
prsIrInput(usart, init->irPrsSel);
}
/* Enable IrDA. */
usart->IRCTRL |= USART_IRCTRL_IREN;
}
#if defined(_USART_I2SCTRL_MASK)
/***************************************************************************//**
* @brief
* Initialize USART for I2S mode.
*
* @details
* This function will configure basic settings to operate in I2S
* mode.
*
* A special control setup not covered by this function must be done after
* using this function by direct modification of the CTRL and I2SCTRL
* registers.
*
* Notice that pins used by the USART module must be properly configured
* by the user explicitly for the USART to work as intended.
* (When configuring pins, remember to consider the sequence of
* configuration to avoid unintended pulses/glitches on output
* pins.)
*
* @param[in] usart
* A pointer to the USART peripheral register block. (UART does not support this
* mode.)
*
* @param[in] init
* A pointer to the initialization structure used to configure the basic I2S setup.
*
* @note
* This function does not apply to all USART's. See the chip Reference Manual.
*
******************************************************************************/
void USART_InitI2s(USART_TypeDef *usart, USART_InitI2s_TypeDef *init)
{
USART_Enable_TypeDef enable;
/* Make sure the module exists on the selected chip. */
EFM_ASSERT(USART_I2S_VALID(usart));
/* Override the enable setting. */
enable = init->sync.enable;
init->sync.enable = usartDisable;
/* Initialize USART as a sync device. */
USART_InitSync(usart, &init->sync);
/* Configure and enable I2CCTRL register according to the selected mode. */
usart->I2SCTRL = (uint32_t)init->format
| (uint32_t)init->justify
| (init->delay ? USART_I2SCTRL_DELAY : 0)
| (init->dmaSplit ? USART_I2SCTRL_DMASPLIT : 0)
| (init->mono ? USART_I2SCTRL_MONO : 0)
| USART_I2SCTRL_EN;
if (enable != usartDisable) {
USART_Enable(usart, enable);
}
}
#endif
/***************************************************************************//**
* @brief
* Initialize the automatic transmissions using PRS channel as a trigger.
* @note
* Initialize USART with USART_Init() before setting up the PRS configuration.
*
* @param[in] usart A pointer to USART to configure.
* @param[in] init A pointer to the initialization structure.
******************************************************************************/
void USART_InitPrsTrigger(USART_TypeDef *usart, const USART_PrsTriggerInit_TypeDef *init)
{
uint32_t trigctrl;
prsTriggerInput(usart, init->prsTriggerChannel);
/* Clear values that will be reconfigured. */
trigctrl = usart->TRIGCTRL & ~(_USART_TRIGCTRL_RXTEN_MASK
| _USART_TRIGCTRL_TXTEN_MASK
#if defined(USART_TRIGCTRL_AUTOTXTEN)
| _USART_TRIGCTRL_AUTOTXTEN_MASK
#endif
);
#if defined(USART_TRIGCTRL_AUTOTXTEN)
if (init->autoTxTriggerEnable) {
trigctrl |= USART_TRIGCTRL_AUTOTXTEN;
}
#endif
if (init->txTriggerEnable) {
trigctrl |= USART_TRIGCTRL_TXTEN;
}
if (init->rxTriggerEnable) {
trigctrl |= USART_TRIGCTRL_RXTEN;
}
usart->TRIGCTRL = trigctrl;
}
/***************************************************************************//**
* @brief
* Reset USART/UART to the same state that it was in after a hardware reset.
*
* @param[in] usart
* A pointer to USART/UART peripheral register block.
******************************************************************************/
void USART_Reset(USART_TypeDef *usart)
{
/* Make sure the module exists on the selected chip. */
EFM_ASSERT(USART_REF_VALID(usart)
|| USARTRF_REF_VALID(usart)
|| UART_REF_VALID(usart) );
#if defined(USART_EN_EN)
usart->EN_SET = USART_EN_EN;
/* Make sure disabled first, before resetting other registers. */
usart->CMD = USART_CMD_RXDIS | USART_CMD_TXDIS | USART_CMD_MASTERDIS
| USART_CMD_RXBLOCKDIS | USART_CMD_TXTRIDIS | USART_CMD_CLEARTX
| USART_CMD_CLEARRX;
usart->CTRL = _USART_CTRL_RESETVALUE;
usart->CTRLX = _USART_CTRLX_RESETVALUE;
usart->FRAME = _USART_FRAME_RESETVALUE;
usart->TRIGCTRL = _USART_TRIGCTRL_RESETVALUE;
usart->CLKDIV = _USART_CLKDIV_RESETVALUE;
usart->IEN = _USART_IEN_RESETVALUE;
usart->IF_CLR = _USART_IF_MASK;
usart->TIMING = _USART_TIMING_RESETVALUE;
if (USART_IRDA_VALID(usart)) {
usart->IRCTRL = _USART_IRCTRL_RESETVALUE;
}
if (USART_I2S_VALID(usart)) {
usart->I2SCTRL = _USART_I2SCTRL_RESETVALUE;
}
usart->EN_CLR = USART_EN_EN;
#else
/* Make sure disabled first, before resetting other registers */
usart->CMD = USART_CMD_RXDIS | USART_CMD_TXDIS | USART_CMD_MASTERDIS
| USART_CMD_RXBLOCKDIS | USART_CMD_TXTRIDIS | USART_CMD_CLEARTX
| USART_CMD_CLEARRX;
usart->CTRL = _USART_CTRL_RESETVALUE;
usart->FRAME = _USART_FRAME_RESETVALUE;
usart->TRIGCTRL = _USART_TRIGCTRL_RESETVALUE;
usart->CLKDIV = _USART_CLKDIV_RESETVALUE;
usart->IEN = _USART_IEN_RESETVALUE;
usart->IFC = _USART_IFC_MASK;
#if defined(_USART_TIMING_MASK)
usart->TIMING = _USART_TIMING_RESETVALUE;
#endif
#if defined(_USART_ROUTEPEN_MASK) || defined(_UART_ROUTEPEN_MASK)
usart->ROUTEPEN = _USART_ROUTEPEN_RESETVALUE;
usart->ROUTELOC0 = _USART_ROUTELOC0_RESETVALUE;
usart->ROUTELOC1 = _USART_ROUTELOC1_RESETVALUE;
#else
usart->ROUTE = _USART_ROUTE_RESETVALUE;
#endif
if (USART_IRDA_VALID(usart)) {
usart->IRCTRL = _USART_IRCTRL_RESETVALUE;
}
#if defined(_USART_INPUT_RESETVALUE)
usart->INPUT = _USART_INPUT_RESETVALUE;
#endif
#if defined(_USART_I2SCTRL_RESETVALUE)
if (USART_I2S_VALID(usart)) {
usart->I2SCTRL = _USART_I2SCTRL_RESETVALUE;
}
#endif
#endif
}
/***************************************************************************//**
* @brief
* Receive one 4-8 bit frame, (or part of 10-16 bit frame).
*
* @details
* This function is normally used to receive one frame when operating with
* frame length 4-8 bits. See @ref USART_RxExt() for reception of
* 9 bit frames.
*
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is empty until data is received.
* Alternatively, the user can explicitly check whether data is available.
* If data is available, call @ref USART_RxDataGet() to read the RXDATA
* register directly.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @return
* Data received.
******************************************************************************/
uint8_t USART_Rx(USART_TypeDef *usart)
{
while (!(usart->STATUS & USART_STATUS_RXDATAV)) {
}
return (uint8_t)usart->RXDATA;
}
/***************************************************************************//**
* @brief
* Receive two 4-8 bit frames or one 10-16 bit frame.
*
* @details
* This function is normally used to receive one frame when operating with
* frame length 10-16 bits. See @ref USART_RxDoubleExt() for
* reception of two 9 bit frames.
*
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is empty until data is received.
* Alternatively, the user can explicitly check whether data is available.
* If data is available, call @ref USART_RxDoubleGet() to read the RXDOUBLE
* register directly.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @return
* Data received.
******************************************************************************/
uint16_t USART_RxDouble(USART_TypeDef *usart)
{
while (!(usart->STATUS & USART_STATUS_RXFULL)) {
}
return (uint16_t)usart->RXDOUBLE;
}
/***************************************************************************//**
* @brief
* Receive two 4-9 bit frames, or one 10-16 bit frame with extended
* information.
*
* @details
* This function is normally used to receive one frame when operating with
* frame length 10-16 bits and additional RX status information is required.
*
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if buffer is empty until data is received.
* Alternatively, the user can explicitly check whether data is available.
* If data is available, call @ref USART_RxDoubleXGet() to read the RXDOUBLEX
* register directly.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @return
* Data received.
******************************************************************************/
uint32_t USART_RxDoubleExt(USART_TypeDef *usart)
{
while (!(usart->STATUS & USART_STATUS_RXFULL)) {
}
return usart->RXDOUBLEX;
}
/***************************************************************************//**
* @brief
* Receive one 4-9 bit frame (or part of 10-16 bit frame) with extended
* information.
*
* @details
* This function is normally used to receive one frame when operating with
* frame length 4-9 bits and additional RX status information is required.
*
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is empty until data is received.
* Alternatively, the user can explicitly check whether data is available.
* If data is available, call @ref USART_RxDataXGet() to read the RXDATAX
* register directly.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @return
* Data received.
******************************************************************************/
uint16_t USART_RxExt(USART_TypeDef *usart)
{
while (!(usart->STATUS & USART_STATUS_RXDATAV)) {
}
return (uint16_t)usart->RXDATAX;
}
/***************************************************************************//**
* @brief
* Perform one 8 bit frame SPI transfer.
*
* @note
* This function will stall if the transmit buffer is full. When a transmit
* buffer becomes available, data is written and the function will wait until
* data is fully transmitted. The SPI return value is then read out and
* returned.
*
* @param[in] usart
* A pointer to the USART peripheral register block.
*
* @param[in] data
* Data to transmit.
*
* @return
* Data received.
******************************************************************************/
uint8_t USART_SpiTransfer(USART_TypeDef *usart, uint8_t data)
{
while (!(usart->STATUS & USART_STATUS_TXBL)) {
}
usart->TXDATA = (uint32_t)data;
while (!(usart->STATUS & USART_STATUS_TXC)) {
}
return (uint8_t)usart->RXDATA;
}
/***************************************************************************//**
* @brief
* Transmit one 4-9 bit frame.
*
* @details
* Depending on the frame length configuration, 4-8 (least significant) bits from
* @p data are transmitted. If the frame length is 9, 8 bits are transmitted from
* @p data and one bit as specified by CTRL register, BIT8DV field.
* See USART_TxExt() for transmitting 9 bit frame with full control of
* all 9 bits.
*
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is full until the buffer becomes available.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] data
* Data to transmit. See details above for more information.
******************************************************************************/
void USART_Tx(USART_TypeDef *usart, uint8_t data)
{
/* Check that transmit buffer is empty */
while (!(usart->STATUS & USART_STATUS_TXBL)) {
}
usart->TXDATA = (uint32_t)data;
}
/***************************************************************************//**
* @brief
* Transmit two 4-9 bit frames or one 10-16 bit frame.
*
* @details
* Depending on the frame length configuration, 4-8 (least significant) bits from
* each byte in @p data are transmitted. If frame length is 9, 8 bits are
* transmitted from each byte in @p data adding one bit as specified by the CTRL
* register, BIT8DV field, to each byte. See USART_TxDoubleExt()
* for transmitting two 9 bit frames with full control of all 9 bits.
*
* If the frame length is 10-16, 10-16 (least significant) bits from @p data
* are transmitted.
*
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is full until the buffer becomes available.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] data
* Data to transmit, the least significant byte holds the frame transmitted
* first. See details above for more info.
******************************************************************************/
void USART_TxDouble(USART_TypeDef *usart, uint16_t data)
{
/* Check that transmit buffer is empty */
while (!(usart->STATUS & USART_STATUS_TXBL)) {
}
usart->TXDOUBLE = (uint32_t)data;
}
/***************************************************************************//**
* @brief
* Transmit two 4-9 bit frames or one 10-16 bit frame with extended control.
*
* @details
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is full until the buffer becomes available.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] data
* Data to transmit with extended control. Contains two 16 bit words
* concatenated. Least significant word holds the frame transmitted first. If the frame
* length is 4-9, two frames with 4-9 least significant bits from each 16 bit
* word are transmitted.
* @par
* If the frame length is 10-16 bits, 8 data bits are taken from the least
* significant 16 bit word and the remaining bits from the other 16 bit word.
* @par
* Additional control bits are available as documented in the reference
* manual (set to 0 if not used). For 10-16 bit frame length, these control
* bits are taken from the most significant 16 bit word.
******************************************************************************/
void USART_TxDoubleExt(USART_TypeDef *usart, uint32_t data)
{
/* Check that transmit buffer is empty. */
while (!(usart->STATUS & USART_STATUS_TXBL)) {
}
usart->TXDOUBLEX = data;
}
/***************************************************************************//**
* @brief
* Transmit one 4-9 bit frame with extended control.
*
* @details
* Notice that possible parity/stop bits in asynchronous mode are not
* considered part of a specified frame bit length.
*
* @note
* This function will stall if the buffer is full until the buffer becomes available.
*
* @param[in] usart
* A pointer to the USART/UART peripheral register block.
*
* @param[in] data
* Data to transmit with extended control. Least significant bit contains
* frame bits. Additional control bits are available as documented in
* the reference manual (set to 0 if not used).
******************************************************************************/
void USART_TxExt(USART_TypeDef *usart, uint16_t data)
{
/* Check that the transmit buffer is empty. */
while (!(usart->STATUS & USART_STATUS_TXBL)) {
}
usart->TXDATAX = (uint32_t)data;
}
/** @} (end addtogroup USART) */
/** @} (end addtogroup emlib) */
#endif /* defined(USART_COUNT) && (USART_COUNT > 0) */
|