spi.c 37.3 KB
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
/*
 * Copyright (C) 2014 Hamburg University of Applied Sciences
 * Copyright (C) 2014 PHYTEC Messtechnik GmbH
 * Copyright (C) 2015 Eistec AB
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License v2.1. See the file LICENSE in the top level directory for more
 * details.
 */

#include <stdio.h>

#include "board.h"
#include "cpu.h"
#include "periph/spi.h"
#include "periph_conf.h"
#include "mutex.h"

#define ENABLE_DEBUG (0)
#include "debug.h"

#ifndef KINETIS_SPI_USE_HW_CS
#define KINETIS_SPI_USE_HW_CS   0
#endif

/**
 * @ingroup     cpu_kinetis_common_spi
 *
 * @{
 *
 * @file
 * @brief       Low-level SPI driver implementation
 *
 * @author      Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
 * @author      Johann Fischer <j.fischer@phytec.de>
 * @author      Joakim Nohlgรฅrd <joakim.nohlgard@eistec.se>
 *
 * @}
 */

/* guard this file in case no SPI device is defined */
#if SPI_NUMOF

#if SPI_0_EN
#ifdef SPI_0_PORT
#define SPI_0_SCK_PORT          SPI_0_PORT
#define SPI_0_SOUT_PORT         SPI_0_PORT
#define SPI_0_SIN_PORT          SPI_0_PORT
#define SPI_0_PCS0_PORT         SPI_0_PORT
#endif

#ifdef SPI_0_PORT_CLKEN
#define SPI_0_SCK_PORT_CLKEN    SPI_0_PORT_CLKEN
#define SPI_0_SOUT_PORT_CLKEN   SPI_0_PORT_CLKEN
#define SPI_0_SIN_PORT_CLKEN    SPI_0_PORT_CLKEN
#define SPI_0_PCS0_PORT_CLKEN   SPI_0_PORT_CLKEN
#endif

#ifdef SPI_0_AF
#define SPI_0_SCK_AF            SPI_0_AF
#define SPI_0_SOUT_AF           SPI_0_AF
#define SPI_0_SIN_AF            SPI_0_AF
#define SPI_0_PCS0_AF           SPI_0_AF
#endif

#ifndef SPI_0_TCSC_FREQ
#define SPI_0_TCSC_FREQ    (0)
#endif

#ifndef SPI_0_TASC_FREQ
#define SPI_0_TASC_FREQ    (0)
#endif

#ifndef SPI_0_TDT_FREQ
#define SPI_0_TDT_FREQ     (0)
#endif
#endif /* SPI_0_EN */

#if SPI_1_EN
#ifdef SPI_1_PORT
#define SPI_1_SCK_PORT          SPI_1_PORT
#define SPI_1_SOUT_PORT         SPI_1_PORT
#define SPI_1_SIN_PORT          SPI_1_PORT
#define SPI_1_PCS0_PORT         SPI_1_PORT
#endif

#ifdef SPI_1_PORT_CLKEN
#define SPI_1_SCK_PORT_CLKEN    SPI_1_PORT_CLKEN
#define SPI_1_SOUT_PORT_CLKEN   SPI_1_PORT_CLKEN
#define SPI_1_SIN_PORT_CLKEN    SPI_1_PORT_CLKEN
#define SPI_1_PCS0_PORT_CLKEN   SPI_1_PORT_CLKEN
#endif

#ifdef SPI_1_AF
#define SPI_1_SCK_AF            SPI_1_AF
#define SPI_1_SOUT_AF           SPI_1_AF
#define SPI_1_SIN_AF            SPI_1_AF
#define SPI_1_PCS0_AF           SPI_1_AF
#endif

#ifndef SPI_1_TCSC_FREQ
#define SPI_1_TCSC_FREQ    (0)
#endif

#ifndef SPI_1_TASC_FREQ
#define SPI_1_TASC_FREQ    (0)
#endif

#ifndef SPI_1_TDT_FREQ
#define SPI_1_TDT_FREQ     (0)
#endif
#endif /* SPI_1_EN */

#if SPI_2_EN
#ifdef SPI_2_PORT
#define SPI_2_SCK_PORT          SPI_2_PORT
#define SPI_2_SOUT_PORT         SPI_2_PORT
#define SPI_2_SIN_PORT          SPI_2_PORT
#define SPI_2_PCS0_PORT         SPI_2_PORT
#endif

#ifdef SPI_2_PORT_CLKEN
#define SPI_2_SCK_PORT_CLKEN    SPI_2_PORT_CLKEN
#define SPI_2_SOUT_PORT_CLKEN   SPI_2_PORT_CLKEN
#define SPI_2_SIN_PORT_CLKEN    SPI_2_PORT_CLKEN
#define SPI_2_PCS0_PORT_CLKEN   SPI_2_PORT_CLKEN
#endif

#ifdef SPI_2_AF
#define SPI_2_SCK_AF            SPI_2_AF
#define SPI_2_SOUT_AF           SPI_2_AF
#define SPI_2_SIN_AF            SPI_2_AF
#define SPI_2_PCS0_AF           SPI_2_AF
#endif

#ifndef SPI_2_TCSC_FREQ
#define SPI_2_TCSC_FREQ    (0)
#endif

#ifndef SPI_2_TASC_FREQ
#define SPI_2_TASC_FREQ    (0)
#endif

#ifndef SPI_2_TDT_FREQ
#define SPI_2_TDT_FREQ     (0)
#endif
#endif /* SPI_2_EN */

#if SPI_3_EN
#ifdef SPI_3_PORT
#define SPI_3_SCK_PORT          SPI_3_PORT
#define SPI_3_SOUT_PORT         SPI_3_PORT
#define SPI_3_SIN_PORT          SPI_3_PORT
#define SPI_3_PCS0_PORT         SPI_3_PORT
#endif

#ifdef SPI_3_PORT_CLKEN
#define SPI_3_SCK_PORT_CLKEN    SPI_3_PORT_CLKEN
#define SPI_3_SOUT_PORT_CLKEN   SPI_3_PORT_CLKEN
#define SPI_3_SIN_PORT_CLKEN    SPI_3_PORT_CLKEN
#define SPI_3_PCS0_PORT_CLKEN   SPI_3_PORT_CLKEN
#endif

#ifdef SPI_3_AF
#define SPI_3_SCK_AF            SPI_3_AF
#define SPI_3_SOUT_AF           SPI_3_AF
#define SPI_3_SIN_AF            SPI_3_AF
#define SPI_3_PCS0_AF           SPI_3_AF
#endif

#ifndef SPI_3_TCSC_FREQ
#define SPI_3_TCSC_FREQ    (0)
#endif

#ifndef SPI_3_TASC_FREQ
#define SPI_3_TASC_FREQ    (0)
#endif

#ifndef SPI_3_TDT_FREQ
#define SPI_3_TDT_FREQ     (0)
#endif
#endif /* SPI_3_EN */

#if SPI_4_EN
#ifdef SPI_4_PORT
#define SPI_4_SCK_PORT          SPI_4_PORT
#define SPI_4_SOUT_PORT         SPI_4_PORT
#define SPI_4_SIN_PORT          SPI_4_PORT
#define SPI_4_PCS0_PORT         SPI_4_PORT
#endif

#ifdef SPI_4_PORT_CLKEN
#define SPI_4_SCK_PORT_CLKEN    SPI_4_PORT_CLKEN
#define SPI_4_SOUT_PORT_CLKEN   SPI_4_PORT_CLKEN
#define SPI_4_SIN_PORT_CLKEN    SPI_4_PORT_CLKEN
#define SPI_4_PCS0_PORT_CLKEN   SPI_4_PORT_CLKEN
#endif

#ifdef SPI_4_AF
#define SPI_4_SCK_AF            SPI_4_AF
#define SPI_4_SOUT_AF           SPI_4_AF
#define SPI_4_SIN_AF            SPI_4_AF
#define SPI_4_PCS0_AF           SPI_4_AF
#endif

#ifndef SPI_4_TCSC_FREQ
#define SPI_4_TCSC_FREQ    (0)
#endif

#ifndef SPI_4_TASC_FREQ
#define SPI_4_TASC_FREQ    (0)
#endif

#ifndef SPI_4_TDT_FREQ
#define SPI_4_TDT_FREQ     (0)
#endif
#endif /* SPI_4_EN */

#if SPI_5_EN
#ifdef SPI_5_PORT
#define SPI_5_SCK_PORT          SPI_5_PORT
#define SPI_5_SOUT_PORT         SPI_5_PORT
#define SPI_5_SIN_PORT          SPI_5_PORT
#define SPI_5_PCS0_PORT         SPI_5_PORT
#endif

#ifdef SPI_5_PORT_CLKEN
#define SPI_5_SCK_PORT_CLKEN    SPI_5_PORT_CLKEN
#define SPI_5_SOUT_PORT_CLKEN   SPI_5_PORT_CLKEN
#define SPI_5_SIN_PORT_CLKEN    SPI_5_PORT_CLKEN
#define SPI_5_PCS0_PORT_CLKEN   SPI_5_PORT_CLKEN
#endif

#ifdef SPI_5_AF
#define SPI_5_SCK_AF            SPI_5_AF
#define SPI_5_SOUT_AF           SPI_5_AF
#define SPI_5_SIN_AF            SPI_5_AF
#define SPI_5_PCS0_AF           SPI_5_AF
#endif

#ifndef SPI_5_TCSC_FREQ
#define SPI_5_TCSC_FREQ    (0)
#endif

#ifndef SPI_5_TASC_FREQ
#define SPI_5_TASC_FREQ    (0)
#endif

#ifndef SPI_5_TDT_FREQ
#define SPI_5_TDT_FREQ     (0)
#endif
#endif /* SPI_5_EN */

#if SPI_6_EN
#ifdef SPI_6_PORT
#define SPI_6_SCK_PORT          SPI_6_PORT
#define SPI_6_SOUT_PORT         SPI_6_PORT
#define SPI_6_SIN_PORT          SPI_6_PORT
#define SPI_6_PCS0_PORT         SPI_6_PORT
#endif

#ifdef SPI_6_PORT_CLKEN
#define SPI_6_SCK_PORT_CLKEN    SPI_6_PORT_CLKEN
#define SPI_6_SOUT_PORT_CLKEN   SPI_6_PORT_CLKEN
#define SPI_6_SIN_PORT_CLKEN    SPI_6_PORT_CLKEN
#define SPI_6_PCS0_PORT_CLKEN   SPI_6_PORT_CLKEN
#endif

#ifdef SPI_6_AF
#define SPI_6_SCK_AF            SPI_6_AF
#define SPI_6_SOUT_AF           SPI_6_AF
#define SPI_6_SIN_AF            SPI_6_AF
#define SPI_6_PCS0_AF           SPI_6_AF
#endif

#ifndef SPI_6_TCSC_FREQ
#define SPI_6_TCSC_FREQ    (0)
#endif

#ifndef SPI_6_TASC_FREQ
#define SPI_6_TASC_FREQ    (0)
#endif

#ifndef SPI_6_TDT_FREQ
#define SPI_6_TDT_FREQ     (0)
#endif
#endif /* SPI_6_EN */

#if SPI_7_EN
#ifdef SPI_7_PORT
#define SPI_7_SCK_PORT          SPI_7_PORT
#define SPI_7_SOUT_PORT         SPI_7_PORT
#define SPI_7_SIN_PORT          SPI_7_PORT
#define SPI_7_PCS0_PORT         SPI_7_PORT
#endif

#ifdef SPI_7_PORT_CLKEN
#define SPI_7_SCK_PORT_CLKEN    SPI_7_PORT_CLKEN
#define SPI_7_SOUT_PORT_CLKEN   SPI_7_PORT_CLKEN
#define SPI_7_SIN_PORT_CLKEN    SPI_7_PORT_CLKEN
#define SPI_7_PCS0_PORT_CLKEN   SPI_7_PORT_CLKEN
#endif

#ifdef SPI_7_AF
#define SPI_7_SCK_AF            SPI_7_AF
#define SPI_7_SOUT_AF           SPI_7_AF
#define SPI_7_SIN_AF            SPI_7_AF
#define SPI_7_PCS0_AF           SPI_7_AF
#endif

#ifndef SPI_7_TCSC_FREQ
#define SPI_7_TCSC_FREQ    (0)
#endif

#ifndef SPI_7_TASC_FREQ
#define SPI_7_TASC_FREQ    (0)
#endif

#ifndef SPI_7_TDT_FREQ
#define SPI_7_TDT_FREQ     (0)
#endif
#endif /* SPI_7_EN */

#define KINETIS_CFG_SPI_IO(num)    \
            spi_dev = SPI_ ## num ## _DEV;\
            module_clock = SPI_ ## num ## _FREQ;\
            tcsc_freq = SPI_ ## num ## _TCSC_FREQ;\
            tasc_freq = SPI_ ## num ## _TASC_FREQ;\
            tdt_freq = SPI_ ## num ## _TDT_FREQ;\
            ctas = SPI_ ## num ## _CTAS;\
            /* enable clocks */\
            SPI_ ## num ## _CLKEN();\
            SPI_ ## num ## _SCK_PORT_CLKEN();\
            SPI_ ## num ## _SOUT_PORT_CLKEN();\
            SPI_ ## num ## _SIN_PORT_CLKEN();\
            /* Set PORT to AF mode */\
            SPI_ ## num ## _SCK_PORT->PCR[SPI_ ## num ## _SCK_PIN] =\
                PORT_PCR_MUX(SPI_ ## num ## _SCK_AF);\
            SPI_ ## num ## _SOUT_PORT->PCR[SPI_ ## num ## _SOUT_PIN] =\
                PORT_PCR_MUX(SPI_ ## num ## _SOUT_AF);\
            SPI_ ## num ## _SIN_PORT->PCR[SPI_ ## num ## _SIN_PIN] =\
                PORT_PCR_MUX(SPI_ ## num ## _SIN_AF);\
            if (KINETIS_SPI_USE_HW_CS) {\
                SPI_ ## num ## _PCS0_PORT_CLKEN();\
                SPI_ ## num ## _PCS0_PORT->PCR[SPI_ ## num ## _PCS0_PIN] =\
                    PORT_PCR_MUX(SPI_ ## num ## _PCS0_AF);\
            }

/**
 * @brief Array holding one pre-initialized mutex for each hardware SPI device
 */
/* We try to avoid adding duplicate entries with the same index by comparing the
 * SPI_x_INDEX macros, the #if statements become quite long though.
 *
 * If not checking for multiple initializations GCC will warn about it
 * but the binary still works. It does look strange in the preprocessed output, however.
 *
 * The warning message is:
 * warning: initialized field overwritten [-Woverride-init]
 * warning: (near initialization for โ€˜locks[0]โ€™) [-Woverride-init]
 *
 * Example of preprocessed source:
 * static mutex_t locks[] = {
 *     [0] = { 0, { ((void *)0) } },
 *     [1] = { 0, { ((void *)0) } },
 *     [0] = { 0, { ((void *)0) } }, // index [0] is given (the same, again) initial value.
 * };
 */

static mutex_t locks[] =  {
#if SPI_0_EN
    [SPI_0_INDEX] = MUTEX_INIT,
#endif
#if SPI_1_EN && (SPI_1_INDEX != SPI_0_INDEX)
    [SPI_1_INDEX] = MUTEX_INIT,
#endif
#if SPI_2_EN && (SPI_2_INDEX != SPI_0_INDEX) && (SPI_2_INDEX != SPI_1_INDEX)
    [SPI_2_INDEX] = MUTEX_INIT,
#endif
#if SPI_3_EN && (SPI_3_INDEX != SPI_0_INDEX) && (SPI_3_INDEX != SPI_1_INDEX) \
    && (SPI_3_INDEX != SPI_2_INDEX)
    [SPI_3_INDEX] = MUTEX_INIT,
#endif
#if SPI_4_EN && (SPI_4_INDEX != SPI_0_INDEX) && (SPI_4_INDEX != SPI_1_INDEX) \
    && (SPI_4_INDEX != SPI_2_INDEX) && (SPI_4_INDEX != SPI_3_INDEX)
    [SPI_4_INDEX] = MUTEX_INIT,
#endif
#if SPI_5_EN && (SPI_5_INDEX != SPI_0_INDEX) && (SPI_5_INDEX != SPI_1_INDEX) \
    && (SPI_5_INDEX != SPI_2_INDEX) && (SPI_5_INDEX != SPI_3_INDEX) \
    && (SPI_5_INDEX != SPI_4_INDEX)
    [SPI_5_INDEX] = MUTEX_INIT,
#endif
#if SPI_6_EN && (SPI_6_INDEX != SPI_0_INDEX) && (SPI_6_INDEX != SPI_1_INDEX) \
    && (SPI_6_INDEX != SPI_2_INDEX) && (SPI_6_INDEX != SPI_3_INDEX) \
    && (SPI_6_INDEX != SPI_4_INDEX) && (SPI_6_INDEX != SPI_5_INDEX)
    [SPI_6_INDEX] = MUTEX_INIT,
#endif
#if SPI_7_EN && (SPI_7_INDEX != SPI_0_INDEX) && (SPI_7_INDEX != SPI_1_INDEX) \
    && (SPI_7_INDEX != SPI_2_INDEX) && (SPI_7_INDEX != SPI_3_INDEX) \
    && (SPI_7_INDEX != SPI_4_INDEX) && (SPI_7_INDEX != SPI_5_INDEX) \
    && (SPI_7_INDEX != SPI_6_INDEX)
    [SPI_7_INDEX] = MUTEX_INIT,
#endif
};

/**
 * @brief Array of pointers that map RIOT SPI device number to actual hardware
 * module lock.
 *
 * Every RIOT device shares the bus lock with any other bus devices for the same
 * hardware module. This allows us to let two RIOT devices point to the same
 * hardware but using different CTAR registers for timing information.
 */
static mutex_t *locks_map[] = {
#if SPI_0_EN
    [SPI_0] = &locks[SPI_0_INDEX],
#endif
#if SPI_1_EN
    [SPI_1] = &locks[SPI_1_INDEX],
#endif
#if SPI_2_EN
    [SPI_2] = &locks[SPI_2_INDEX],
#endif
#if SPI_3_EN
    [SPI_3] = &locks[SPI_3_INDEX],
#endif
#if SPI_4_EN
    [SPI_4] = &locks[SPI_4_INDEX],
#endif
#if SPI_5_EN
    [SPI_5] = &locks[SPI_5_INDEX],
#endif
#if SPI_6_EN
    [SPI_6] = &locks[SPI_6_INDEX],
#endif
#if SPI_7_EN
    [SPI_7] = &locks[SPI_7_INDEX],
#endif
};

typedef struct {
    char(*cb)(char data);
} spi_state_t;

static inline void irq_handler_transfer(SPI_Type *spi, spi_t dev);

static spi_state_t spi_config[SPI_NUMOF];

#define SPI_IDLE_DATA (0xff)

/**
 * @brief Helper function for finding optimal baud rate scalers.
 *
 * Find the prescaler and scaler settings that will yield a clock frequency
 * as close as possible (but not above) the target frequency, given the module
 * runs at module_clock Hz.
 *
 * Hardware properties (Baud rate configuration):
 * Possible prescalers: 2, 3, 5, 7
 * Possible scalers: 2, 4, 6 (sic!), 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
 *
 * SCK baud rate = (f_BUS/PBR) x [(1+DBR)/BR]
 *
 * where PBR is the prescaler, BR is the scaler, DBR is the Double BaudRate bit.
 *
 * @note We are not using the DBR bit because it may affect the SCK duty cycle.
 *
 * @param module_clock Module clock frequency (e.g. F_BUS)
 * @param target_clock Desired baud rate
 * @param closest_prescaler pointer where to write the optimal prescaler index.
 * @param closest_scaler pointer where to write the optimal scaler index.
 *
 * @return The actual achieved frequency on success
 * @return Less than 0 on error.
 */
static long find_closest_baudrate_scalers(const uint32_t module_clock, const long target_clock,
        uint8_t *closest_prescaler, uint8_t *closest_scaler)
{
    uint8_t i;
    uint8_t k;
    long freq;
    static const uint8_t num_scalers = 16;
    static const uint8_t num_prescalers = 4;
    static const int br_scalers[16] = {
        2,     4,     6,     8,    16,    32,    64,   128,
        256,   512,  1024,  2048,  4096,  8192, 16384, 32768
    };
    static const int br_prescalers[4] = {2, 3, 5, 7};

    long closest_frequency = -1;

    /* Test all combinations until we arrive close to the target clock */
    for (i = 0; i < num_prescalers; ++i) {
        for (k = 0; k < num_scalers; ++k) {
            freq = module_clock / (br_scalers[k] * br_prescalers[i]);

            if (freq <= target_clock) {
                /* Found closest lower frequency at this prescaler setting,
                 * compare to the best result */
                if (closest_frequency < freq) {
                    closest_frequency = freq;
                    *closest_scaler = k;
                    *closest_prescaler = i;
                }

                break;
            }
        }
    }

    if (closest_frequency < 0) {
        /* Error, no solution found, this line is never reachable with current
         * hardware settings unless a _very_ low target clock is requested.
         * (scaler_max * prescaler_max) = 229376 => target_min@100MHz = 435 Hz*/
        return -1;
    }

    return closest_frequency;
}

/**
 * @brief Helper function for finding optimal delay scalers.
 *
 * Find the prescaler and scaler settings that will yield a delay timing
 * as close as possible (but not shorter than) the target delay, given the
 * module runs at module_clock Hz.
 *
 * Hardware properties (delay configuration):
 * Possible prescalers: 1, 3, 5, 7
 * Possible scalers: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536
 *
 * delay = (1/f_BUS) x prescaler x scaler
 *
 * Because we want to do this using only integers, the target_freq parameter is
 * the reciprocal of the delay time.
 *
 * @param module_clock Module clock frequency (e.g. F_BUS)
 * @param target_freq Reciprocal (i.e. 1/t [Hz], frequency) of the desired delay time.
 * @param closest_prescaler pointer where to write the optimal prescaler index.
 * @param closest_scaler pointer where to write the optimal scaler index.
 *
 * @return The actual achieved frequency on success
 * @return Less than 0 on error.
 */
static long find_closest_delay_scalers(const uint32_t module_clock, const long target_freq,
                                      uint8_t *closest_prescaler, uint8_t *closest_scaler)
{
    uint8_t i;
    uint8_t k;
    long freq;
    int prescaler;
    int scaler;
    static const uint8_t num_scalers = 16;
    static const uint8_t num_prescalers = 4;

    long closest_frequency = -1;

    /* Test all combinations until we arrive close to the target clock */
    for (i = 0; i < num_prescalers; ++i) {
        for (k = 0; k < num_scalers; ++k) {
            prescaler = (i * 2) + 1;
            scaler = (1 << (k + 1)); /* 2^(k+1) */
            freq = module_clock / (prescaler * scaler);

            if (freq <= target_freq) {
                /* Found closest lower frequency at this prescaler setting,
                 * compare to the best result */
                if (closest_frequency < freq) {
                    closest_frequency = freq;
                    *closest_scaler = k;
                    *closest_prescaler = i;
                }

                break;
            }
        }
    }

    if (closest_frequency < 0) {
        /* Error, no solution found, this line is never reachable with current
         * hardware settings unless a _very_ low target clock is requested.
         * (scaler_max * prescaler_max) = 458752 */
        return -1;
    }

    return closest_frequency;
}

int spi_init_master(spi_t dev, spi_conf_t conf, spi_speed_t speed)
{
    SPI_Type *spi_dev;
    uint8_t br_prescaler = 0xff;
    uint8_t br_scaler = 0xff;
    uint8_t prescaler_tmp = 0xff;
    uint8_t scaler_tmp = 0xff;
    uint32_t ctas = 0;
    uint32_t ctar = 0;
    uint32_t br_desired;
    uint32_t module_clock;
    uint32_t tcsc_freq;
    uint32_t tasc_freq;
    uint32_t tdt_freq;

    switch (speed) {
        case SPI_SPEED_100KHZ:
            br_desired = 100000;
            break;

        case SPI_SPEED_400KHZ:
            br_desired = 400000;
            break;

        case SPI_SPEED_1MHZ:
            br_desired = 1000000;
            break;

        case SPI_SPEED_5MHZ:
            br_desired = 5000000;
            break;

        case SPI_SPEED_10MHZ:
            br_desired = 10000000;
            break;

        default:
            return -2;
    }

    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            KINETIS_CFG_SPI_IO(0);
            break;
#endif /* SPI_0_EN */

#if SPI_1_EN

        case SPI_1:
            KINETIS_CFG_SPI_IO(1);
            break;
#endif /* SPI_1_EN */

#if SPI_2_EN

        case SPI_2:
            KINETIS_CFG_SPI_IO(2);
            break;
#endif /* SPI_2_EN */

#if SPI_3_EN

        case SPI_3:
            KINETIS_CFG_SPI_IO(3);
            break;
#endif /* SPI_3_EN */

#if SPI_4_EN

        case SPI_4:
            KINETIS_CFG_SPI_IO(4);
            break;
#endif /* SPI_4_EN */

#if SPI_5_EN

        case SPI_5:
            KINETIS_CFG_SPI_IO(5);
            break;
#endif /* SPI_5_EN */

#if SPI_6_EN

        case SPI_6:
            KINETIS_CFG_SPI_IO(6);
            break;
#endif /* SPI_6_EN */

#if SPI_7_EN

        case SPI_7:
            KINETIS_CFG_SPI_IO(7);
            break;
#endif /* SPI_7_EN */

        default:
            return -1;
    }

    /* Find baud rate scaler and prescaler settings */
    if (find_closest_baudrate_scalers(module_clock, br_desired,
                                      &br_prescaler, &br_scaler) < 0) {
        /* Desired baud rate is too low to be reachable at current module clock frequency. */
        return -2;
    }

    ctar |= SPI_CTAR_PBR(br_prescaler) | SPI_CTAR_BR(br_scaler);

    /* Find the other delay divisors */
    /* tCSC */
    if (tcsc_freq == 0) {
        /* Default to same as baud rate if set to zero. */
        tcsc_freq = br_desired;
    }

    if (find_closest_delay_scalers(module_clock, tcsc_freq,
                                   &prescaler_tmp, &scaler_tmp) < 0) {
        /* failed to find a solution */
        return -2;
    }

    ctar |= SPI_CTAR_PCSSCK(prescaler_tmp) | SPI_CTAR_CSSCK(scaler_tmp);

    /* tASC */
    if (tasc_freq == 0) {
        /* Default to same as baud rate if set to zero. */
        tasc_freq = br_desired;
    }

    if (find_closest_delay_scalers(module_clock, tasc_freq,
                                   &prescaler_tmp, &scaler_tmp) < 0) {
        /* failed to find a solution */
        return -2;
    }

    ctar |= SPI_CTAR_PASC(prescaler_tmp) | SPI_CTAR_ASC(scaler_tmp);

    /* tDT */
    if (tdt_freq == 0) {
        /* Default to same as baud rate if set to zero. */
        tdt_freq = br_desired;
    }

    if (find_closest_delay_scalers(module_clock, tdt_freq,
                                   &prescaler_tmp, &scaler_tmp) < 0) {
        /* failed to find a solution */
        return -2;
    }

    ctar |= SPI_CTAR_PDT(prescaler_tmp) | SPI_CTAR_DT(scaler_tmp);


    /* Set clock polarity and phase. */
    switch (conf) {
        case SPI_CONF_FIRST_RISING:
            break;

        case SPI_CONF_SECOND_RISING:
            ctar |= SPI_CTAR_CPHA_MASK;
            break;

        case SPI_CONF_FIRST_FALLING:
            ctar |= SPI_CTAR_CPOL_MASK;
            break;

        case SPI_CONF_SECOND_FALLING:
            ctar |= SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK;
            break;

        default:
            return -2;
    }

    /* Update CTAR register with new timing settings, 8-bit frame size. */
    spi_dev->CTAR[ctas] = SPI_CTAR_FMSZ(7) | ctar;

    /* enable SPI */
    spi_dev->MCR = SPI_MCR_MSTR_MASK
                   | SPI_MCR_DOZE_MASK
                   | SPI_MCR_CLR_TXF_MASK
                   | SPI_MCR_CLR_RXF_MASK;

    if (KINETIS_SPI_USE_HW_CS) {
        spi_dev->MCR |= SPI_MCR_PCSIS(1);
    }

    spi_dev->RSER = (uint32_t)0;

    return 0;
}

int spi_init_slave(spi_t dev, spi_conf_t conf, char(*cb)(char data))
{
    SPI_Type *spi_dev;

    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            spi_dev = SPI_0_DEV;
            /* enable clocks */
            SPI_0_CLKEN();
            SPI_0_PCS0_PORT_CLKEN();
            SPI_0_SCK_PORT_CLKEN();
            SPI_0_SOUT_PORT_CLKEN();
            SPI_0_SIN_PORT_CLKEN();
            /* Set PORT to AF mode */
            SPI_0_PCS0_PORT->PCR[SPI_0_PCS0_PIN] = PORT_PCR_MUX(SPI_0_PCS0_AF);
            SPI_0_SCK_PORT->PCR[SPI_0_SCK_PIN] = PORT_PCR_MUX(SPI_0_SCK_AF);
            SPI_0_SOUT_PORT->PCR[SPI_0_SOUT_PIN] = PORT_PCR_MUX(SPI_0_SOUT_AF);
            SPI_0_SIN_PORT->PCR[SPI_0_SIN_PIN] = PORT_PCR_MUX(SPI_0_SIN_AF);
            break;
#endif /* SPI_0_EN */

        default:
            return -1;
    }

    /* set frame size, slave mode always uses CTAR0 */
    spi_dev->CTAR[0] = SPI_CTAR_SLAVE_FMSZ(7);

    /* Set clock polarity and phase. */
    switch (conf) {
        case SPI_CONF_FIRST_RISING:
            spi_dev->CTAR[0] &= ~(SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK);
            break;

        case SPI_CONF_SECOND_RISING:
            spi_dev->CTAR[0] &= ~(SPI_CTAR_CPOL_MASK);
            spi_dev->CTAR[0] |= SPI_CTAR_CPHA_MASK;
            break;

        case SPI_CONF_FIRST_FALLING:
            spi_dev->CTAR[0] &= ~(SPI_CTAR_CPHA_MASK);
            spi_dev->CTAR[0] |= SPI_CTAR_CPOL_MASK;
            break;

        case SPI_CONF_SECOND_FALLING:
            spi_dev->CTAR[0] |= SPI_CTAR_CPHA_MASK | SPI_CTAR_CPOL_MASK;
            break;

        default:
            return -2;
    }

    /* enable SPI */
    spi_dev->MCR = SPI_MCR_DOZE_MASK
                   | SPI_MCR_PCSIS(SPI_0_PCS0_ACTIVE_LOW << 0)
                   | SPI_MCR_CLR_TXF_MASK
                   | SPI_MCR_CLR_RXF_MASK;

    spi_dev->RSER = (uint32_t)0;

    /* set callback */
    spi_config[dev].cb = cb;

    return 0;
}

int spi_acquire(spi_t dev)
{
    if ((unsigned int)dev >= SPI_NUMOF) {
        return -1;
    }

    mutex_lock(locks_map[dev]);
    return 0;
}

int spi_release(spi_t dev)
{
    if ((unsigned int)dev >= SPI_NUMOF) {
        return -1;
    }

    mutex_unlock(locks_map[dev]);
    return 0;
}

static inline uint8_t spi_transfer_internal(SPI_Type *spi_dev, uint32_t flags, uint8_t byte_out)
{
    /* Wait until there is space in the TXFIFO */
    while (!(spi_dev->SR & SPI_SR_TFFF_MASK));

#if KINETIS_SPI_USE_HW_CS
    spi_dev->PUSHR = flags | SPI_PUSHR_TXDATA(byte_out) | SPI_PUSHR_PCS(1);
#else
    spi_dev->PUSHR = flags | SPI_PUSHR_TXDATA(byte_out);
#endif

    /* Wait until we have received a byte */
    while (!(spi_dev->SR & SPI_SR_RXCTR_MASK));

    return (uint8_t)spi_dev->POPR;
}

int spi_transfer_byte(spi_t dev, char out, char *in)
{
    SPI_Type *spi_dev;
    uint8_t byte_in;
    uint32_t flags;

    /* The chip select lines are expected to be controlled via software in RIOT.
     * Don't set PCS bits in flags. */
    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            spi_dev = SPI_0_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_0_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_1_EN

        case SPI_1:
            spi_dev = SPI_1_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_1_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_2_EN

        case SPI_2:
            spi_dev = SPI_2_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_2_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_3_EN

        case SPI_3:
            spi_dev = SPI_3_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_3_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_4_EN

        case SPI_4:
            spi_dev = SPI_4_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_4_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_5_EN

        case SPI_5:
            spi_dev = SPI_5_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_5_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_6_EN

        case SPI_6:
            spi_dev = SPI_6_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_6_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

#if SPI_7_EN

        case SPI_7:
            spi_dev = SPI_7_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_7_CTAS) | SPI_PUSHR_EOQ_MASK);
            break;
#endif

        default:
            return -1;
    }

    byte_in = spi_transfer_internal(spi_dev, flags, out);

    /* Clear End-of-Queue status flag */
    spi_dev->SR = SPI_SR_EOQF_MASK;

    if (in != NULL) {
        *in = (char)byte_in;
    }

    return 1;
}

int spi_transfer_bytes(spi_t dev, char *out, char *in, unsigned int length)
{
    SPI_Type *spi_dev;
    uint8_t byte_in;
    uint8_t byte_out;
    uint32_t flags;
    int i;

    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            spi_dev = SPI_0_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_0_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_1_EN

        case SPI_1:
            spi_dev = SPI_1_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_1_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_2_EN

        case SPI_2:
            spi_dev = SPI_2_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_2_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_3_EN

        case SPI_3:
            spi_dev = SPI_3_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_3_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_4_EN

        case SPI_4:
            spi_dev = SPI_4_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_4_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_5_EN

        case SPI_5:
            spi_dev = SPI_5_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_5_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_6_EN

        case SPI_6:
            spi_dev = SPI_6_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_6_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_7_EN

        case SPI_7:
            spi_dev = SPI_7_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_7_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

        default:
            return -1;
    }

    /* Default: send idle data */
    byte_out = (uint8_t)SPI_IDLE_DATA;

    for (i = 0; i < (int)length; i++) {
        if (out != NULL) {
            /* Send given out data */
            byte_out = (uint8_t)out[i];
        }

        if (i >= (int)length - 1) {
            /* Last byte, set End-of-Queue flag, clear Continue flag. */
            flags &= ~(SPI_PUSHR_CONT_MASK);
            flags |= SPI_PUSHR_EOQ_MASK;
        }

        byte_in = spi_transfer_internal(spi_dev, flags, byte_out);

        if (in != NULL) {
            /* Save input byte to buffer */
            in[i] = (char)byte_in;
        }
    }

    /* Clear End-of-Queue status flag */
    spi_dev->SR = SPI_SR_EOQF_MASK;

    return i;
}

int spi_transfer_reg(spi_t dev, uint8_t reg, char out, char *in)
{
    SPI_Type *spi_dev;
    uint8_t byte_in;
    uint32_t flags;

    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            spi_dev = SPI_0_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_0_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_1_EN

        case SPI_1:
            spi_dev = SPI_1_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_1_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_2_EN

        case SPI_2:
            spi_dev = SPI_2_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_2_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_3_EN

        case SPI_3:
            spi_dev = SPI_3_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_3_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_4_EN

        case SPI_4:
            spi_dev = SPI_4_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_4_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_5_EN

        case SPI_5:
            spi_dev = SPI_5_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_5_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_6_EN

        case SPI_6:
            spi_dev = SPI_6_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_6_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_7_EN

        case SPI_7:
            spi_dev = SPI_7_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_7_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

        default:
            return -1;
    }

    /* Transfer the register address first. */
    spi_transfer_internal(spi_dev, flags, reg);

    /* Last byte, set End-of-Queue flag, clear Continue flag. */
    flags &= ~(SPI_PUSHR_CONT_MASK);
    flags |= SPI_PUSHR_EOQ_MASK;

    /* Transfer the value. */
    byte_in = spi_transfer_internal(spi_dev, flags, out);

    if (in != NULL) {
        /* Save input byte to buffer */
        *in = (char)byte_in;
    }

    /* Clear End-of-Queue status flag */
    spi_dev->SR = SPI_SR_EOQF_MASK;

    return 2;
}

int spi_transfer_regs(spi_t dev, uint8_t reg, char *out, char *in, unsigned int length)
{
    SPI_Type *spi_dev;
    uint8_t byte_in;
    uint8_t byte_out;
    uint32_t flags;
    int i;

    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            spi_dev = SPI_0_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_0_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_1_EN

        case SPI_1:
            spi_dev = SPI_1_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_1_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_2_EN

        case SPI_2:
            spi_dev = SPI_2_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_2_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_3_EN

        case SPI_3:
            spi_dev = SPI_3_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_3_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_4_EN

        case SPI_4:
            spi_dev = SPI_4_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_4_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_5_EN

        case SPI_5:
            spi_dev = SPI_5_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_5_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_6_EN

        case SPI_6:
            spi_dev = SPI_6_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_6_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

#if SPI_7_EN

        case SPI_7:
            spi_dev = SPI_7_DEV;
            flags = (SPI_PUSHR_CTAS(SPI_7_CTAS) | SPI_PUSHR_CONT_MASK);
            break;
#endif

        default:
            return -1;
    }

    byte_out = reg;

    /* Send register address */
    spi_transfer_internal(spi_dev, flags, byte_out);

    /* Default: send idle data */
    byte_out = (uint8_t)SPI_IDLE_DATA;

    for (i = 0; i < (int)length; i++) {
        if (out != NULL) {
            /* Send given out data */
            byte_out = (uint8_t)out[i];
        }

        if (i >= (int)length - 1) {
            /* Last byte, set End-of-Queue flag, clear Continue flag. */
            flags &= ~(SPI_PUSHR_CONT_MASK);
            flags |= SPI_PUSHR_EOQ_MASK;
        }

        byte_in = spi_transfer_internal(spi_dev, flags, byte_out);

        if (in != NULL) {
            /* Save input byte to buffer */
            in[i] = (char)byte_in;
        }
    }

    /* Clear End-of-Queue status flag */
    spi_dev->SR = SPI_SR_EOQF_MASK;

    return i;
}

void spi_transmission_begin(spi_t dev, char reset_val)
{

    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            SPI_0_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_0_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_1_EN

        case SPI_1:
            SPI_1_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_1_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_2_EN

        case SPI_2:
            SPI_2_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_2_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_3_EN

        case SPI_3:
            SPI_3_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_3_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_4_EN

        case SPI_4:
            SPI_4_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_4_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_5_EN

        case SPI_5:
            SPI_5_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_5_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_6_EN

        case SPI_6:
            SPI_6_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_6_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
#if SPI_7_EN

        case SPI_7:
            SPI_7_DEV->PUSHR = SPI_PUSHR_CTAS(SPI_7_CTAS)
                               | SPI_PUSHR_EOQ_MASK
                               | SPI_PUSHR_TXDATA(reset_val);
            break;
#endif
    }
}

void spi_poweron(spi_t dev)
{
    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            SPI_0_CLKEN();
            break;
#endif
#if SPI_1_EN

        case SPI_1:
            SPI_1_CLKEN();
            break;
#endif
#if SPI_2_EN

        case SPI_2:
            SPI_2_CLKEN();
            break;
#endif
#if SPI_3_EN

        case SPI_3:
            SPI_3_CLKEN();
            break;
#endif
#if SPI_4_EN

        case SPI_4:
            SPI_4_CLKEN();
            break;
#endif
#if SPI_5_EN

        case SPI_5:
            SPI_5_CLKEN();
            break;
#endif
#if SPI_6_EN

        case SPI_6:
            SPI_6_CLKEN();
            break;
#endif
#if SPI_7_EN

        case SPI_7:
            SPI_7_CLKEN();
            break;
#endif
    }
}

void spi_poweroff(spi_t dev)
{
    /* Wait until the last byte has been transmitted before turning off
     * the clock. */
    switch (dev) {
#if SPI_0_EN

        case SPI_0:
            while ((SPI_0_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_0_CLKDIS();
            break;
#endif
#if SPI_1_EN

        case SPI_1:
            while ((SPI_1_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_1_CLKDIS();
            break;
#endif
#if SPI_2_EN

        case SPI_2:
            while ((SPI_2_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_2_CLKDIS();
            break;
#endif
#if SPI_3_EN

        case SPI_3:
            while ((SPI_3_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_3_CLKDIS();
            break;
#endif
#if SPI_4_EN

        case SPI_4:
            while ((SPI_4_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_4_CLKDIS();
            break;
#endif
#if SPI_5_EN

        case SPI_5:
            while ((SPI_5_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_5_CLKDIS();
            break;
#endif
#if SPI_6_EN

        case SPI_6:
            while ((SPI_6_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_6_CLKDIS();
            break;
#endif
#if SPI_7_EN

        case SPI_7:
            while ((SPI_7_DEV->SR & SPI_SR_TXCTR_MASK) != 0);

            SPI_7_CLKDIS();
            break;
#endif
    }
}

static inline void irq_handler_transfer(SPI_Type *spi, spi_t dev)
{

    if (spi->SR & SPI_SR_RFDF_MASK) {
        char data;
        data = (char)spi->POPR;
        data = spi_config[dev].cb(data);
        spi->PUSHR = SPI_PUSHR_CTAS(0)
                     | SPI_PUSHR_EOQ_MASK
                     | SPI_PUSHR_TXDATA(data);
    }

    /* see if a thread with higher priority wants to run now */
    cortexm_isr_end();
}

#if SPI_0_EN
#ifdef SPI_0_IRQ_HANDLER
void SPI_0_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_0_DEV, SPI_0);
}
#endif
#endif

#if SPI_1_EN
#ifdef SPI_1_IRQ_HANDLER
void SPI_1_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_1_DEV, SPI_1);
}
#endif
#endif

#if SPI_2_EN
#ifdef SPI_2_IRQ_HANDLER
void SPI_2_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_2_DEV, SPI_2);
}
#endif
#endif

#if SPI_3_EN
#ifdef SPI_3_IRQ_HANDLER
void SPI_3_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_3_DEV, SPI_3);
}
#endif
#endif

#if SPI_4_EN
#ifdef SPI_4_IRQ_HANDLER
void SPI_4_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_4_DEV, SPI_4);
}
#endif
#endif

#if SPI_5_EN
#ifdef SPI_5_IRQ_HANDLER
void SPI_5_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_5_DEV, SPI_5);
}
#endif
#endif

#if SPI_6_EN
#ifdef SPI_6_IRQ_HANDLER
void SPI_6_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_6_DEV, SPI_6);
}
#endif
#endif

#if SPI_7_EN
#ifdef SPI_7_IRQ_HANDLER
void SPI_7_IRQ_HANDLER(void)
{
    irq_handler_transfer(SPI_7_DEV, SPI_7);
}
#endif
#endif

#endif /* SPI_NUMOF */