r_fdl_hw_access.c 121 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743
#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_OFF 0292
#endif
/*********************************************************************************************************************
 * Library       : Data Flash Access Library for Renesas RH850 devices
 *
 * File Name     : $Source: r_fdl_hw_access.c $
 * Lib. Version  : $RH850_FDL_LIB_VERSION_T01: V2.11 $
 * Mod. Revision : $Revision: 1.70 $
 * Mod. Date     : $Date: 2016/06/01 12:39:20JST $
 * Device(s)     : RV40 Flash based RH850 microcontroller
 * Description   : FDL hardware interface functions
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only  intended for use with Renesas products.
 * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
 * applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS
 * AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
 * REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability of
 * this software. By using this software, you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer
 *
 * Copyright (C) 2015-2016 Renesas Electronics Corporation. All rights reserved.
 *********************************************************************************************************************/

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_ON 0292
#endif

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 0292)
 * Reason:       To support automatic insertion of revision, module name etc. by the source revision control system
 *               it is necessary to violate the rule, because the system uses non basic characters as placeholders.
 * Verification: The placeholders are used in commentaries only. Therefore rule violation cannot influence code
 *               compilation.
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 19.1 (QAC message 5087)
 * Reason:       The section mapping concept (Mapping code, constants and data to specific linker sections) bases on
 *               a central include file containing all section mapping  defines and pragmas. This need to be included
 *               multiple times within the code. The file itself only contains those defines and pragmas.
 * Verification: This is the standard concept defined for AUTOSAR implementations
 *********************************************************************************************************************/



/*********************************************************************************************************************
 * FDL header files include
 *********************************************************************************************************************/
#include "r_fdl_global.h"

/*********************************************************************************************************************
 * local function prototypes
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 8.8 (QAC message 3447)
 * Reason:       Function declared as external usable function, but is only used locally
 * Verification: Function must be declared global in order to avoid function inlining. The function contains assembler 
 *               code and relies on calling conventions. GHS may (in case of precompiled library creation) optimize by
 *               inlining. As the parameters are used in assbembler only, GHS will optimize call parameter preparation 
 *               away.
 *********************************************************************************************************************/ 
R_FDL_STATIC r_fdl_status_t   R_FDL_IFct_ChkAccessRight (r_fdl_accessType_t accType_enu,
                                                         uint32_t granularity_u32);
R_FDL_STATIC r_fdl_status_t   R_FDL_IFct_GetFWParam     (uint32_t addr_u32, uint32_t * buf);
#ifndef R_FDL_NO_BFA_SWITCH
R_FDL_STATIC void         R_FDL_FCUFct_InitRAM_Asm  (uint32_t * param_pu32);
R_FDL_STATIC void         R_FDL_IFct_GetFWParam_Asm (uint32_t * param_pu32);
R_FDL_STATIC uint32_t     R_FDL_IFct_GetBWCAdd      (void);
R_FDL_STATIC void         R_FDL_IFct_ExeCodeInRAM   (r_fdl_pFct_ExeInRAM pFct,
                                                     uint32_t *          param_pu32);
#endif
R_FDL_STATIC uint8_t          R_FDL_IFct_ReadMemoryU08  (uint32_t addr_u32);
R_FDL_STATIC uint16_t         R_FDL_IFct_ReadMemoryU16  (uint32_t addr_u32);
R_FDL_STATIC uint32_t         R_FDL_IFct_ReadMemoryU32  (uint32_t addr_u32);
R_FDL_STATIC void             R_FDL_IFct_WriteMemoryU16 (uint32_t addr_u32, uint16_t val_u16);
R_FDL_STATIC void             R_FDL_IFct_WriteMemoryU32 (uint32_t addr_u32, uint32_t val_u32);
             void             R_FDL_FCUFct_ChkReg_Asm   (uint32_t regAdd, uint32_t valMask,         /* PRQA S 3447 */
                                                         uint32_t to, uint32_t * ret);
/*********************************************************************************************************************
 * FDL internal section mapping definitions
 *********************************************************************************************************************/
#define R_FDL_START_SEC_CONST
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

#if R_FDL_COMPILER == R_FDL_COMP_REC
    #define asm _asm
#endif


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_ReadMemoryU32
 *********************************************************************************************************************/
/**
 * Function to read a 32-bit IO register or memory
 *
 * @param[in]  addr_u32 - source address
 * @return     32-bit register contents
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303)
 * Reason:       For effective embedded programming, integer to pointer conversions are used
 * Verification: The converted addresses are essential for complete code execution. Incorrect
 *               conversion would result in test fails.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC uint32_t  R_FDL_IFct_ReadMemoryU32 (uint32_t addr_u32)
{
    #if (defined TA_USE_TEST_CALLBACKS)
    return (test_cb_read_u32 (addr_u32) );
    #else
    return (*( (volatile uint32_t *)(addr_u32) ) );                                                 /* PRQA S 0303 */
    #endif
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_ReadMemoryU08
 *********************************************************************************************************************/
/**
 * Function to read a 8-bit IO register or memory
 *
 * @param[in]  addr_u32 - source address
 * @return     8-bit register contents
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
   MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303)
   Reason:       For effective embedded programming, integer to pointer conversions are used
   Verification: The converted addresses are essential for complete code execution. Incorrect
                 conversion would result in test fails.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC uint8_t  R_FDL_IFct_ReadMemoryU08 (uint32_t addr_u32)
{
    #if (defined TA_USE_TEST_CALLBACKS)
    return (test_cb_read_u8 (addr_u32) );
    #else
    return (*( (volatile uint8_t *)(addr_u32) ) );                                                  /* PRQA S 0303 */
    #endif
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_ReadMemoryU16
 *********************************************************************************************************************/
/**
 * Function to read a 16-bit IO register or memory
 *
 * @param[in]  addr_u32 - source address
 * @return     16-bit register contents
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303)
 * Reason:       For effective embedded programming, integer to pointer conversions are used
 * Verification: The converted addresses are essential for complete code execution. Incorrect
 *               conversion would result in test fails.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC uint16_t  R_FDL_IFct_ReadMemoryU16 (uint32_t addr_u32)
{
    #if (defined TA_USE_TEST_CALLBACKS)
    return (test_cb_read_u16 (addr_u32) );
    #else
    return (*( (volatile uint16_t *)(addr_u32) ) );                                                 /* PRQA S 0303 */
    #endif
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_WriteMemoryU08
 *********************************************************************************************************************/
/**
 * Function to write a 8-bit IO register or memory
 *
 * @param[in]  addr_u32 - write destination address
 * @param[in]  val_u08  - 8-bit write data
 * @return     ---
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303)
 * Reason:       For effective embedded programming, integer to pointer conversions are used
 * Verification: The converted addresses are essential for complete code execution. Incorrect
 *               conversion would result in test fails.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_IFct_WriteMemoryU08 (uint32_t addr_u32, uint8_t val_u08)
{
    #if (defined TA_USE_TEST_CALLBACKS)
    test_cb_write_u8 (addr_u32, val_u08);
    #else
    (*( (volatile uint8_t *)(addr_u32) ) ) = val_u08;                                               /* PRQA S 0303 */
    #endif
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_WriteMemoryU16
 *********************************************************************************************************************/
/**
 * Function to write a 16-bit IO register or memory
 *
 * @param[in]  addr_u32 - write destination address
 * @param[in]  val_u16  - 16-bit write data
 * @return     ---
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303)
 * Reason:       For effective embedded programming, integer to pointer conversions are used
 * Verification: The converted addresses are essential for complete code execution. Incorrect
 *               conversion would result in test fails.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC void R_FDL_IFct_WriteMemoryU16 (uint32_t addr_u32, uint16_t val_u16)
{
    #if (defined TA_USE_TEST_CALLBACKS)
    test_cb_write_u16 (addr_u32, val_u16);
    #else
    (*( (volatile uint16_t *)(addr_u32) ) ) = val_u16;                                              /* PRQA S 0303 */
    #endif
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_WriteMemoryU32
 *********************************************************************************************************************/
/**
 * Function to write a 32-bit IO register or memory
 *
 * @param[in]  addr_u32 - write destination address
 * @param[in]  val_u32  - 32-bit write data
 * @return     ---
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303)
 * Reason:       For effective embedded programming, integer to pointer conversions are used
 * Verification: The converted addresses are essential for complete code execution. Incorrect
 *               conversion would result in test fails.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC void R_FDL_IFct_WriteMemoryU32 (uint32_t addr_u32, uint32_t val_u32)
{
    #if (defined TA_USE_TEST_CALLBACKS)
    test_cb_write_u32 (addr_u32, val_u32);
    #else
    (*( (volatile uint32_t *)(addr_u32) ) ) = val_u32;                                              /* PRQA S 0303 */
    #endif
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_CalcFOpUnitCnt_BC
 *********************************************************************************************************************/
/**
 * This function calculates the correct unit count for Flash operations.
 * Calculation basics: Blank Check may not overlap Flash macros
 * Smallest possible Flash macros are assumed. So, also operations within physical macros are cut, when a minimum
 * theoretical macro boundary is hit.
 *
 * @param[in,out]  cnt_pu32    - number of operation units. The input value is cut down to match the boundary conditions
 * @param[in]      addr_u32    - operation start address
 * @return         ---
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_IFct_CalcFOpUnitCnt_BC (uint32_t * cnt_pu32, uint32_t addr_u32)
{
    uint32_t cntTmp;
    uint32_t addMax;
    uint32_t cntTmpMax;

    cntTmp = (*cnt_pu32);

    /* Consider macro boundaries */
    addMax = ( (addr_u32 / R_MACROSIZE_MIN) * R_MACROSIZE_MIN); /* Calculate macro boundary */
    addMax += R_MACROSIZE_MIN;
    cntTmpMax = (addMax - addr_u32);                  /* Calculate remaining operations in the macro */

    /* Limit count to the macro maximum */
    if (cntTmpMax < cntTmp)
    {
        cntTmp = cntTmpMax;
    }

    *cnt_pu32 = cntTmp;

} /* R_FDL_IFct_CalcFOpUnitCnt_BC */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_ChkAccessBoundaries
 *********************************************************************************************************************/
/**
 * This function checks if the Flash access boundaries are correct. Parameter error if:
 * - addStart granularity is not correct
 * - end address calc. wrap around
 * - end address overflow
 * - bCnt = 0
 * - selected block range is not in the correct pool (User/EEL)
 *
 * @param[in]  addrStart_u32   - Start address of the range to access
 * @param[in]  bCnt_u32        - Number of bytes to access
 * @param[in]  accType_enu     - type of FDL access (user or EEL)
 * @param[in]  granularity_u32 - Granularity for each command
 * @return     Returns the access check result
 *             - R_FDL_BUSY (check was OK)
 *             - R_FDL_ERR_PARAMETER (parameter error detected)
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 21.1 (QAC message 2912)
 * Reason:       Wraparound in unsigned arithmetic operation, used to detect parameter error.
 * Verification: A check against wraparound is performed immediately after the arithmetic operation in cause.
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_IFct_ChkAccessBoundaries (uint32_t addrStart_u32,
                                               uint32_t bCnt_u32,
                                               r_fdl_accessType_t accType_enu,
                                               uint32_t granularity_u32)
{
    uint32_t       eelPoolStart;
    uint32_t       eelPoolSize;
    uint32_t       eelPoolEnd;
    uint32_t       fdlPoolEnd;
    uint32_t       addEnd;
    r_fdl_status_t ret;
    r_fdl_flag_t   inEelRange;

    ret = R_FDL_BUSY;

    eelPoolStart = (g_fdl_str.RTCfg_pstr->eelPoolStart_u16) << R_BLOCK_SIZE_2N;
    eelPoolSize  = (g_fdl_str.RTCfg_pstr->eelPoolSize_u16) << R_BLOCK_SIZE_2N;
    fdlPoolEnd   = ( (g_fdl_str.RTCfg_pstr->fdlPoolSize_u16) << R_BLOCK_SIZE_2N) - 1uL;

    inEelRange = R_FDL_FALSE;

    /* Byte count = 0 is parameter error */
    if (0uL == bCnt_u32)
    {
        ret = R_FDL_ERR_PARAMETER;
    }
    
    /* Byte count != 0 */
    else
    {
        addEnd = (addrStart_u32 + bCnt_u32) - 1uL;                                                 /* PRQA S 2912 */

        /* Start and end address checks against Data Flash size and Start address granularity */
        if ( (addrStart_u32 > addEnd) ||                /* end address calc. wrap around */
             ( (addEnd >= g_fdl_str.dfSize_u32) ||      /* end address overflow */
                                                        /* cnt parameter error.
                                                           Granularity is correct by default (user IF bases on block/word
                                                           count, lib. internally changed to byte cnt */
               ( (addrStart_u32 & (granularity_u32 - 1uL) ) != 0x00000000uL) ) )  /* address granularity */
        {
            ret = R_FDL_ERR_PARAMETER;
        }
        
        /* Addresses match to Data Flash, now check against EEL and FDL pool boundaries */
        else
        {

            /* EEL pool defined? */
            if (eelPoolSize > 0u)
            {
                eelPoolEnd   = (eelPoolStart + eelPoolSize) - 1u;

                /* Address range in EEL pool? */
                if ( (addrStart_u32 >= eelPoolStart) && (addEnd <= eelPoolEnd) )
                {
                    inEelRange = R_FDL_TRUE;
                }
                
                /* Should be outside EEL pool */
                else
                {
                    /* Are we overlapping boundaries? */
                    if (!( (addEnd < eelPoolStart) || (addrStart_u32 > eelPoolEnd) ) )
                    {
                        ret = R_FDL_ERR_PARAMETER;
                    }
                }
            }

            /* As EEL we must remain in the EEL range */
            if (R_FDL_ACCESS_EEL == accType_enu)
            {
                if (R_FDL_FALSE == inEelRange)
                {
                    ret = R_FDL_ERR_PARAMETER;
                }
            }
            
            /* As user ... */
            else if (R_FDL_ACCESS_USER == accType_enu)
            {
                /* we must remain in the Data Flash but outside EEL range */
                if ( (addEnd > fdlPoolEnd) || (R_FDL_TRUE == inEelRange) )
                {
                    ret = R_FDL_ERR_PARAMETER;
                }
            }
            else
            {
                ret = R_FDL_ERR_PARAMETER;
            }
        }
    }
    return (ret);
} /* R_FDL_IFct_ChkAccessBoundaries */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_ChkAccessRight
 *********************************************************************************************************************/
/**
 * The check is a robustness feature against modification from outside the library as the parameter
 * check during command initiation should have detected any parameter error already. This check
 * shall block wrong settings directly before starting the Flash hardware. So,
 * on violation, the returned error is a protection error, not a parameter error. \n
 * The function reads back the access parameters from the FCU for the check and compares them
 * against the access rights defined by the descriptor.
 *
 * @param[in]  accType_enu - type of FDL access (user or EEL)
 * @param[in]  granularity_u32 - Granularity for each command, used to determine if start address is aligned properly
 *
 * @return     Returns the access check result
 *             - R_FDL_BUSY (check was OK)
 *             - R_FDL_ERR_INTERNAL (parameter detected)
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_status_t R_FDL_IFct_ChkAccessRight (r_fdl_accessType_t accType_enu,
                                                       uint32_t granularity_u32)
{
    r_fdl_status_t ret;
    uint32_t       addStart, bCnt;

    addStart = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSADR_U32);
    bCnt     = (R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FEADR_U32) - addStart) + R_WRITE_SIZE;

    #ifdef PATCH_TO_SIMULATE_ERRORS
    R_FDL_COV_SAVEOFF
    if (tstData_str.simError_enu == R_FDL_TRUE)
    {
        if (R_FDL_SIM_ERROR_PROTECTION == tstData_str.simErrorType_enu)
        {
            /* bCnt = 0 must raise a protection error */
            bCnt = 0;
            tstData_str.simError_enu = R_FDL_FALSE;
        }
    }
    R_FDL_COV_RESTORE
    #endif


    ret = R_FDL_IFct_ChkAccessBoundaries (addStart, bCnt, accType_enu, granularity_u32);
    /* R_FDL_IFct_ChkAccessBoundaries returns PARAMETER error as it is used in R_FDL_Execute for
       parameter check. If we check directly before Flash operation start, we want to return
       R_FDL_ERR_INTERNAL in error case. So we patch the return value here */
    if (R_FDL_ERR_PARAMETER == ret)
    {
        ret = R_FDL_ERR_INTERNAL;
    }

    return (ret);
} /* R_FDL_IFct_ChkAccessRight */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ChkReg_Asm
 *********************************************************************************************************************/
/**
 * Assembler function to read a register and wait till the target value is reached.
 * The register check loop is timeout supervised
 * Note: Function must be declared global (See QAC comment below)
 *
 * @param[in]     regAdd: register address (r6)
 * @param[in]     valMask: target value mask (r7)
 * @param[in]     to: timeout value (r8)
 * @param[out]    ret: timeout check result (r9)
 *                     0: No error, 1: Timeout error
 * @return        ---
 */
/*********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 3006)
 * Reason:       This function contains a mixture of in-line assembler statements and C statements.
 *               The function is copied to RAM into a memory range of fix size. So, the function itself must have a
 *               fix size which cannot be realized by a c-compiler. So, the function is written in Assembler
 * Verification: Review of the c-interface (only temporary registers are used and the stack size is unchanged
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 1006)
 * Reason:       In-line assembler construct is a language extension. The code has been ignored.
 * Verification: -
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 3206)
 * Reason:       function parameter seems to be not used in the function as no c-code relates on
 *               it, but the assembler code uses the parameter
 * Verification: -
 *********************************************************************************************************************/
 /*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 8.10 (QAC message 1505)
 * Reason:       Function declared as external usable function, but is only used locally
 * Verification: Function must be declared global in order to avoid function inlining. The function contains assembler 
 *               code and relies on calling conventions. GHS may (in case of precompiled library creation) optimize by
 *               inlining. As the parameters are used in assbembler only, GHS will optimize call parameter preparation 
 *               away.
 *********************************************************************************************************************/ 

#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

R_FDL_COV_SAVEOFF

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_OFF 1006
#endif

#if R_FDL_COMPILER == R_FDL_COMP_GHS
void R_FDL_FCUFct_ChkReg_Asm (uint32_t regAdd, uint32_t valMask, uint32_t to, uint32_t * ret) /*PRQA S 3006,3206,1505*/
{
    /* Used registers: r6 ~ r10  */
    asm (" _R_FDL_FCUFct_ChkReg_Loop:           ");
    asm (" ld.w 0[r6], r10                      "); /* read register value */
    asm (" and     r7, r10                      "); /* mask the value */
    asm (" cmp     r10, r7                      "); /* equal target value? */
    asm (" bz      _R_FDL_FCUFct_ChkReg_Pass    ");
    asm (" add     -1, r8                       "); /* timeout check */
    asm (" bnz     _R_FDL_FCUFct_ChkReg_Loop    ");

    asm (" mov     1, r10                       "); /* timeout detected, return 1 */
    asm (" st.w    r10, 0[r9]                   ");
    asm (" br      _R_FDL_FCUFct_ChkReg_End     ");

    asm (" _R_FDL_FCUFct_ChkReg_Pass:           ");
    asm (" st.w    r0, 0[r9]                    "); /* timeout detected, return 0 */

    asm (" _R_FDL_FCUFct_ChkReg_End:            ");

} /* R_FDL_IFct_GetFWParam_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_GHS */

#if R_FDL_COMPILER == R_FDL_COMP_IAR
void R_FDL_FCUFct_ChkReg_Asm (uint32_t regAdd, uint32_t valMask, uint32_t to, uint32_t * ret) /*PRQA S 3006,3206,1505*/
{
    /* IAR compiler uses with rh850 compiler onwards the same calling conventions as GHS */
    __asm (
        "_R_FDL_FCUFct_ChkReg_Loop:          \n"
        " ld.w 0[r6], r10                    \n"
        " and     r7, r10                    \n"
        " cmp     r10, r7                    \n"
        " bz      _R_FDL_FCUFct_ChkReg_Pass  \n"
        " add     -1, r8                     \n"
        " bnz     _R_FDL_FCUFct_ChkReg_Loop  \n"
        "                                    \n"
        " mov     1, r10                     \n"
        " st.w    r10, 0[r9]                 \n"
        " br      _R_FDL_FCUFct_ChkReg_End   \n"
        "                                    \n"
        "_R_FDL_FCUFct_ChkReg_Pass:          \n"
        " st.w    r0, 0[r9]                  \n"
        "                                    \n"
        "_R_FDL_FCUFct_ChkReg_End:           \n"
        );

} /* R_FDL_IFct_GetFWParam_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_IAR */

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_ON 1006
#endif

#if R_FDL_COMPILER == R_FDL_COMP_REC
#pragma inline_asm R_FDL_FCUFct_ChkReg_Asm
void R_FDL_FCUFct_ChkReg_Asm (uint32_t regAdd, uint32_t valMask, uint32_t to, uint32_t * ret) /*PRQA S 3006,3206,1505*/
{
    .local _R_FDL_FCUFct_ChkReg_Loop
    .local _R_FDL_FCUFct_ChkReg_Pass
    .local _R_FDL_FCUFct_ChkReg_End

_R_FDL_FCUFct_ChkReg_Loop:
    ld.w    0[r6], r10
    and r7, r10
    cmp r10, r7
    bz _R_FDL_FCUFct_ChkReg_Pass
    add -1, r8
    bnz _R_FDL_FCUFct_ChkReg_Loop

    mov     1, r10
    st.w r10, 0[r9]
    br _R_FDL_FCUFct_ChkReg_End

_R_FDL_FCUFct_ChkReg_Pass:
    st.w r0, 0[r9]

_R_FDL_FCUFct_ChkReg_End:
} /* R_FDL_FCUFct_ChkReg_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_REC */

R_FDL_COV_RESTORE

#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ForcedStop
 *********************************************************************************************************************/
/**
 * Reset FCU and FACI registers
 *
 * @param[in,out] -
 * @return        Returns timeout check result
 *                - R_FDL_OK:             function passed
 *                - R_FDL_ERR_INTERNAL:   timeout error
  */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_ForcedStop (void)
{
    r_fdl_status_t ret;
    uint32_t       res;

    R_FDL_IFct_WriteMemoryU08 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_FORCED_STOP);

    /* Register check loop */
    R_FDL_FCUFct_ChkReg_Asm (R_FCU_REGADD_FSTATR_U32,
                             R_FCU_REGBIT_FSTATR_FRDY,
                             R_FDL_TIMEOUT_CHKREG,
                             &res);

    /* Patch the check loop result: 0 --> check passed */
    if (0x00000000uL == res)
    {
        ret = R_FDL_OK;
    }
    /* != 0 --> timeout in check loop */
    else
    {
        ret = R_FDL_ERR_INTERNAL;
    }

    return (ret);
} /* R_FDL_FCUFct_ForcedStop */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ClearStatus
 *********************************************************************************************************************/
/**
 * Clear (error) status of the sequencer by the clear status FCU command
 *
 * @param[in,out]   -
 * @return          ---
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_FCUFct_ClearStatus (void)
{
    uint32_t res32;
    uint8_t  res8;

    res32 = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

    if (R_FCU_REGBIT_FSTATR_ILGERR == (R_FCU_REGBIT_FSTATR_ILGERR & res32) )
    {
        res8 = R_FDL_IFct_ReadMemoryU08 (R_FCU_REGADD_FASTAT_U08);
        /* Only CLDLK bit may be set, others have to be cleared */
        if (R_FCU_REGBIT_FASTAT_CMDLK != res8)
        {
            R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_FASTAT_U08, R_FCU_REGBIT_FASTAT_CMDLK);
        }
    }

    R_FDL_IFct_WriteMemoryU08 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_CLEARSTAT);
} /* R_FDL_FCUFct_ClearStatus */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_Switch_FAEINT
 *********************************************************************************************************************/
/**
 * Switch On/OFF FAEINT
 *
 * @param[in,out]   on_t    - R_FDL_TRUE:  Switch interrupt on
 *                          - R_FDL_FALSE: Switch interrupt off
 * @return          ---
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_FCUFct_Switch_FAEINT (r_fdl_flag_t on_t)
{
    if (R_FDL_FALSE == on_t)
    {
        R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_FAEINT_U08, R_FCU_REGVAL_FAEINT_DISABLE);
    }
    else
    {
        R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_FAEINT_U08, R_FCU_REGVAL_FAEINT_ENABLE);
    }

} /* R_FDL_FCUFct_Switch_FAEINT */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_GetBWCAdd
 *********************************************************************************************************************/
/**
 * Use intrinsic functions to evaluate BWC address from PID register
 *
 * @param      -
 * @return     BWC register address, 0x00000000uL  if no BWC available
 */
/*********************************************************************************************************************/
#ifdef R_FDL_NO_BFA_SWITCH
#else

#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC uint32_t R_FDL_IFct_GetBWCAdd (void)
{
    uint32_t bwc;
    uint32_t pid;

    /* regID 6, selID 1 */
    #if R_FDL_COMPILER == R_FDL_COMP_GHS
    pid = __STSR (R_SYSTEM_REGISTER_PID);
    #elif R_FDL_COMPILER == R_FDL_COMP_IAR
    pid = __STSR (R_SYSTEM_REGISTER_PID);
    #elif R_FDL_COMPILER == R_FDL_COMP_REC
    pid = __stsr_rh (R_SYSTEM_REGISTER_PID);
    #endif

    pid &= R_PID_CORE_MASK;    /* Extract bits 5~7 */

    /* G3K core */
    if (R_PID_CORE_G3K == pid)
    {
        bwc = R_BWCBUF_G3K_ADD;
    }
    
    /* G3KH core */
    else if (R_PID_CORE_G3KH == pid)
    {
        bwc = R_BWCBUF_G3KH_ADD;
    }
    
    /* No G3K code --> no BWC buffer */
    else
    {
        bwc = 0x00000000uL;
    }

    return (bwc);
} /* R_FDL_IFct_GetBWCAdd */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

#endif /* ifdef R_FDL_NO_BFA_SWITCH */

/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_GetFWParam
 *********************************************************************************************************************/
/**
 * Call the assembler function to read out a firmware parameter from the Extra area
 *
 * @param[in]  addr_u32 - Read address
 * @param[out] buf - write buffer for the result value
 * @return     Returns timeout check result
 *             - R_FDL_OK:             function passed
 *             - R_FDL_ERR_INTERNAL:   timeout error
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_status_t R_FDL_IFct_GetFWParam (uint32_t addr_u32, uint32_t * buf)
{
    r_fdl_status_t ret;

    #ifdef R_FDL_NO_BFA_SWITCH
    ret = R_FDL_OK;
    (*buf) = R_FDL_IFct_ReadMemoryU32 (addr_u32);

    #else
    uint32_t getFWParam[5];

    /* Initialize the RAM function parameters */
    getFWParam[0] = addr_u32;                       /* Read address */
    getFWParam[1] = R_FDL_IFct_GetBWCAdd();         /* BWC buffer address */
    getFWParam[2] = 0x00000000uL;                   /* Reset result (only updated in case of no function error) */
    getFWParam[3] = R_FDL_TIMEOUT_CC;               /* Timeout loop count */
    getFWParam[4] = 0x00000000uL;                   /* Clear function call error */

    /* Execute Copy routine in RAM as BFA need to be temporarily switched on */
    R_FDL_IFct_ExeCodeInRAM (&R_FDL_IFct_GetFWParam_Asm, (uint32_t *)(&getFWParam[0]) );

    *buf = getFWParam[2];

    /* No timeout error */
    if (0x00000000uL == getFWParam[4])
    {
        ret = R_FDL_OK;
    }
    else
    {
        ret = R_FDL_ERR_INTERNAL;
    }
    #endif /* ifdef R_FDL_NO_BFA_SWITCH */

    return (ret);
} /* R_FDL_IFct_GetFWParam */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_GetFWParam_Asm
 *********************************************************************************************************************/
/**
 * Assembler function to read out a firmware parameter from the Extra area
 * The cache clear check loop is timeout supervised
 *
 * Note: the code for GHS, IAR and CC-RL is basically equal, only the style is compiler dependent and so, different.
 *       GHS code is fully commented, while the IAR and CC-RL code is partly or not commented. Refer to GHS comments
 *       to understand the code.
 *
 * @param[in,out]  param_pu32 \n
 *                 in  - Address to read \n
 *                 out - Read value
 * @return     ---
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 3006)
 * Reason:       This function contains a mixture of in-line assembler statements and C statements.
 *               The function is copied to RAM into a memory range of fix size. So, the function itself must have a
 *               fix size which cannot be realized by a c-compiler. So, the function is written in Assembler
 * Verification: Review of the c-interface (only temporary registers are used and the stack size is unchanged
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 1006)
 * Reason:       In-line assembler construct is a language extension. The code has been ignored.
 * Verification: -
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 3206)
 * Reason:       function parameter seems to be not used in the function as no c-code relates on
 *               it, but the assembler code uses the parameter
 * Verification: -
 *********************************************************************************************************************/
#ifdef R_FDL_NO_BFA_SWITCH
#else

#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

R_FDL_COV_SAVEOFF

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_OFF 1006
#endif

#if R_FDL_COMPILER == R_FDL_COMP_GHS
R_FDL_STATIC void R_FDL_IFct_GetFWParam_Asm (uint32_t * param_pu32)                             /* PRQA S 3006,3206 */
{
    /* GHS compilers use r6~r9 as scratch registers.
       r6: parameter address
       r7, r9: work register
       r8: value for switch BFA on/off
    */

    /* ----- Switch ON BFA ----- */
    asm (" mov 0x01, r8                         ");     /* Hard coded BFASEL value to switch on EXA3*/
    asm (" br _R_FDL_IFct_GetFWParam_Asm_SWBFA  ");

    asm (" _R_FDL_IFct_GetFWParam_Asm_Read:     ");

    /* Read requested data as one aligned word */
    asm (" ld.w 0[r6], r7                       ");
    asm (" ld.w 0[r7], r9                       ");
    asm (" st.w r9, 8[r6]                       ");

    /* ----- Switch OFF BFA ----- */
    asm (" mov r0, r8                           ");     /* Hard coded BFASEL value to switch off EXA3*/

    /* Switch BFA and clear the cache */
    /* & line buffer (Called twice) */
    asm (" _R_FDL_IFct_GetFWParam_Asm_SWBFA:    ");

    /* Switch EXA3 */
    asm (" syncp                                ");     /* sync to peripheral access */
    asm (" mov 0xffc59008, r7                   ");     /* Hard coded address of BFASEL */
    asm (" st.b r8, 0[r7]                       ");
    asm (" ld.b 0[r7], r7                       ");     /* dummy read from FCU register to wait one APB access cycle */
    asm (" syncp                                ");     /* sync to peripheral access */
    asm (" synci                                ");

    /* Clear the cache */
    asm (" stsr 24, r7, 4                       ");     /* system register 24, 4 is ICCTRL */
    asm (" ori 0x0100, r7, r7                   ");     /* set cache clear bit 8 */
    asm (" ldsr r7, 24, 4                       ");

    asm (" ld.w 12[r6], r9                      ");     /* read timeout value */
    asm (" _R_FDL_IFct_GetFWParam_Asm_Polling:  ");
    asm (" stsr 24, r7, 4                       ");     /* system register 24, 4 is ICCTRL */
    asm (" andi 0x0100, r7, r7");
    asm (" bz _R_FDL_IFct_GetFWParam_Asm_Polling_Pass"); /* exit loop when Cache is cleared (bit is 0) */
    asm (" add -1, r9");                                /* check timeout */
    asm (" bnz _R_FDL_IFct_GetFWParam_Asm_Polling");
    asm (" mov 1, r9");                                 /* store timeout error and continue*/
    asm (" st.w    r9, 16[r6]                   ");
    asm (" _R_FDL_IFct_GetFWParam_Asm_Polling_Pass:");

    asm (" synci                                ");

    /* Check the core - BWC buffer address == 0 --> clear cache
                        BWC buffer address != 0 --> clear BWC buffer */
    asm (" ld.w 4[r6], r9                       ");     /* get BWCBUFEN address */
    asm (" cmp r9, r0                           ");     /* BWCBUFEN address not available on G3M cores */
    asm (" be _R_FDL_IFct_GetFWParam_Asm_G3M    ");

    /* G3K core: Clear BWC */
    asm (" mov 0x01, r7                         ");     /* BWCBUFCLR bit */
    asm (" st.b r0, 0[r9]                       ");     /* BWCBUFCLR = 0 */
    asm (" st.b r7, 0[r9]                       ");     /* BWCBUFCLR = 1 */
    asm (" st.b r0, 0[r9]                       ");     /* BWCBUFCLR = 0 */
    asm (" ld.b 0[r9], r7                       ");
    asm (" syncp                                ");     /* sync to peripheral access */
    asm (" synci                                ");
    asm (" br _R_FDL_IFct_GetFWParam_Asm_CCEnd  ");

    /* G3M core: clear sub-cache */
    asm (" _R_FDL_IFct_GetFWParam_Asm_G3M:      ");
    asm (" stsr 24, r7, 13                      ");     /* system register 24, 13 is CDBCR */
    asm (" ori 0x02, r7, r7                     ");     /* set cache clear bit 1 */
    asm (" ldsr r7, 24, 13                      ");
    asm (" stsr 24, r7, 13                      ");     /* Dummy read to system register to complete the operation */

    asm (" _R_FDL_IFct_GetFWParam_Asm_CCEnd:    ");

    /* Check where to continue */
    asm (" cmp r8, r0                           ");     /* we check here against the hard coded BFASEL value */
    asm (" bnz _R_FDL_IFct_GetFWParam_Asm_Read  ");
} /* R_FDL_IFct_GetFWParam_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_GHS */

#if R_FDL_COMPILER == R_FDL_COMP_IAR
R_FDL_STATIC void R_FDL_IFct_GetFWParam_Asm (uint32_t * param_pu32)                             /* PRQA S 3006,3206 */
{
    /* IAR compiler uses with rh850 compiler onwards the same calling conventions as GHS */
    __asm ("                                         \n"
           " mov 0x01, r8                            \n"
           " br _R_FDL_IFct_GetFWParam_Asm_SWBFA     \n"
           "                                         \n"
           "_R_FDL_IFct_GetFWParam_Asm_Read:         \n"
           "                                         \n"
           " /* Read requested data as one aligned word */ \n"
           " ld.w 0[r6], r7                          \n"
           " ld.w 0[r7], r9                          \n"
           " st.w r9, 8[r6]                          \n"
           "                                         \n"
           " /* ----- Switch OFF BFA ----- */        \n"
           " mov r0, r8                              \n"
           "                                         \n"
           " /* Switch BFA and clear the cache */    \n"
           " /* & line buffer (Called twice) */      \n"
           "_R_FDL_IFct_GetFWParam_Asm_SWBFA:        \n"
           "                                         \n"
           " /* Switch EXA3 */                       \n"
           " syncp                                   \n"
           " mov 0xffc59008, r7                      \n"
           " st.b r8, 0[r7]                          \n"
           " ld.b 0[r7], r7                          \n"
           " syncp                                   \n"
           " synci                                   \n"
           "                                         \n"
           " /* Clear the cache */                   \n"
           " stsr 24, r7, 4                          \n"
           " ori 0x0100, r7, r7                      \n"
           " ldsr r7, 24, 4                          \n"
           "                                         \n"
           " ld.w 12[r6], r9                         \n"
           "_R_FDL_IFct_GetFWParam_Asm_Polling:      \n"
           " stsr 24, r7, 4                          \n"
           " andi 0x0100, r7, r7                     \n"
           " bz _R_FDL_IFct_GetFWParam_Asm_Polling_Pass \n"
           " add -1, r9                              \n"
           " bnz _R_FDL_IFct_GetFWParam_Asm_Polling  \n"
           " mov 1, r9                               \n"
           " st.w    r9, 16[r6]                      \n"
           "_R_FDL_IFct_GetFWParam_Asm_Polling_Pass: \n"
           "                                         \n"
           " synci                                   \n"
           "                                         \n"
           " /* Check the core ... */                \n"
           " ld.w 4[r6], r9                          \n"
           " cmp r9, r0                              \n"
           " be _R_FDL_IFct_GetFWParam_Asm_G3M       \n"
           "                                         \n"
           " /* G3K core: Clear BWC */               \n"
           " mov 0x01, r7                            \n"
           " st.b r0, 0[r9]                          \n"
           " st.b r7, 0[r9]                          \n"
           " st.b r0, 0[r9]                          \n"
           " ld.b 0[r9], r7                          \n"
           " syncp                                   \n"
           " synci                                   \n"
           " br _R_FDL_IFct_GetFWParam_Asm_CCEnd     \n"
           "                                         \n"
           " /* G3M core: clear sub-cache */         \n"
           "_R_FDL_IFct_GetFWParam_Asm_G3M:          \n"
           " stsr 24, r7, 13                         \n"
           " ori 0x02, r7, r7                        \n"
           " ldsr r7, 24, 13                         \n"
           " stsr 24, r7, 13                         \n"
           "                                         \n"
           "_R_FDL_IFct_GetFWParam_Asm_CCEnd:        \n"
           "                                         \n"
           " /* Check where to continue */           \n"
           " cmp r8, r0                              \n"
           " bnz _R_FDL_IFct_GetFWParam_Asm_Read     \n"
           );

} /* R_FDL_IFct_GetFWParam_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_IAR */

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_ON 1006
#endif

#if R_FDL_COMPILER == R_FDL_COMP_REC
#pragma inline_asm R_FDL_IFct_GetFWParam_Asm
R_FDL_STATIC void R_FDL_IFct_GetFWParam_Asm (uint32_t * param_pu32)
{
    mov 0x01, r8
    br _R_FDL_IFct_GetFWParam_Asm_SWBFA

_R_FDL_IFct_GetFWParam_Asm_Read:

    ld.w 0[r6], r7
    ld.w 0[r7], r9
    st.w r9, 8[r6]

    mov r0, r8

_R_FDL_IFct_GetFWParam_Asm_SWBFA:

    syncp
    mov 0xffc59008, r7
    st.b r8, 0[r7]
    ld.b 0[r7], r7
    syncp
    synci

    stsr 24, r7, 4
    ori 0x0100, r7, r7
    ldsr r7, 24, 4

    ld.w 12[r6], r9
_R_FDL_IFct_GetFWParam_Asm_Polling:
    stsr 24, r7, 4
    andi 0x0100, r7, r7
    bz _R_FDL_IFct_GetFWParam_Asm_Polling_Pass
    add -1, r9
    bnz _R_FDL_IFct_GetFWParam_Asm_Polling
    mov 1, r9
    st.w r9, 16[r6]
_R_FDL_IFct_GetFWParam_Asm_Polling_Pass:

    synci

    ld.w 4[r6], r9
    cmp r9, r0
    be _R_FDL_IFct_GetFWParam_Asm_G3M

    mov 0x01, r7
    st.b r0, 0[r9]
    st.b r7, 0[r9]
    st.b r0, 0[r9]
    ld.b 0[r9], r7
    syncp
    synci
    br _R_FDL_IFct_GetFWParam_Asm_CCEnd

_R_FDL_IFct_GetFWParam_Asm_G3M:
    stsr 24, r7, 13
    ori 0x02, r7, r7
    ldsr r7, 24, 13
    stsr 24, r7, 13

_R_FDL_IFct_GetFWParam_Asm_CCEnd:

    cmp r8, r0
    bnz _R_FDL_IFct_GetFWParam_Asm_Read
} /* R_FDL_IFct_GetFWParam_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_REC */

R_FDL_COV_RESTORE

#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

#endif /* ifdef R_FDL_NO_BFA_SWITCH */

/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_GetDFSize
 *********************************************************************************************************************/
/**
 * Get Data Flash size \n
 *
 * @param[out]      size - Size in Bytes
 * @return          Operation result
 *                  - R_FDL_OK: Operation successfully completed
 *                  - R_FDL_ERR_INTERNAL: Timeout during parameter read
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_GetDFSize (uint32_t * size)
{
    uint32_t       data;
    uint32_t       dataAddr;
    r_fdl_status_t ret;

    ret = R_FDL_IFct_GetFWParam (R_EXTRA3_SCDSADD, &dataAddr);    /* Entry tells where to find the PRD* information */

    if (R_FDL_OK == ret)
    {
        data = R_FDL_IFct_ReadMemoryU32 (dataAddr + R_PRDSEL3_OFFSET);
        data &= 0x0000FFFFuL;       /* Lower 16Byte are the DF size in kB. So, multiply accordingly */
        *size = data * 1024;
    }

    return (ret);
} /* R_FDL_FCUFct_GetDFSize */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_SetFrequency
 *********************************************************************************************************************/
/**
 * Set the FCU frequency. \n
 * APBFrequency - FCU frequency, set in MHz taken from descriptor is needed
 * The function may return an error if the frequency could not be set (Timeout of command error).
 *
 * @param[in,out]   -
 * @return     configuration result
 *             - R_FDL_OK                - Frequency set successfully
 *             - R_FDL_ERR_CONFIGURATION - Frequency parameter out of range
 *             - R_FDL_ERR_INTERNAL      - timeout during parameter read
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 9.1 (QAC message 2962)
 * Reason:       Possible use of uninitialized variable. 
 * Verification: Claimed variables initialization is done conditionally if iErr is false. However usage of the variable
 *               is also bound to this condition. So, no issue
 *********************************************************************************************************************/

#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_SetFrequency (void)
{
    r_fdl_status_t ret;
    uint16_t       fCpu;
    uint16_t       fFaci;
    uint32_t       fwVal;
    uint8_t        fwVer, fDivider;
    uint32_t       fMin, fMax, fPClk;
    uint32_t       dataAddr, data;

    fCpu = g_fdl_str.RTCfg_pstr->cpuFrequencyMHz_u16;

    /* Get firmware parameters */
    /* FW version is 1 Byte on not word aligned address, so we read word aligned and calculate the correct byte */
    ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FWVER & (~0x00000003uL), &fwVal);
    fwVer    = (uint8_t)( (fwVal >> 8) & 0xFFu);

    /* Firmware version is > 1 (version 1 is marked with 0xFF)
       and no error during parameter read */
    if (    (0xFF != fwVer)
            && (R_FDL_OK == ret) )
    {
        /* Frequency is 32bit value in Hz. We need to calculate MHz from that */
        ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FMIN, &fwVal);
        fMin     = fwVal / 1000000uL;

        if (R_FDL_OK == ret)
        {
            ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FMAX, &fwVal);
            fMax     = fwVal / 1000000uL;
        }

        /* versions 2/3 and no error during parameter read*/
        if (fwVer < R_FCU_FWVER_04)
        {
            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                /* Divider 1 Byte only but on a word aligned address,
                   so we read word aligned and calculate the correct byte */
                ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FDIV_FWVER_03, &fwVal);
                fDivider = (uint8_t)(fwVal & 0xFFu);
            }

            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                /* PCLK Frequency is 32bit value in Hz. We need to calculate MHz from that */
                ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FPCLK_FWVER_03, &fwVal);
                fPClk    = fwVal / 1000000uL;
            }

            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                /* Entry tells where to find the PRD* information */
                ret = R_FDL_IFct_GetFWParam (R_EXTRA3_SCDSADD, &dataAddr);
            }

            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                data = R_FDL_IFct_ReadMemoryU32 (dataAddr + R_PRDNAME2_OFFSET);                     /* PRQA S 2962 */

                /* for devices F1x and R1x */
                if (R_PRDNAME_010x == (data & 0x00FFFFFFu) )
                {
                    g_fdl_str.baseAddrECC_u32 = R_DECC_BASE_F1X;
                }
                else
                {
                    g_fdl_str.baseAddrECC_u32 = R_DECC_BASE_E1X;
                }
            }
        }
        
        /* version 4 and upper */
        else
        {
            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                /* Divider 1 Byte only on a byte aligned address,
                   so we read word aligned and calculate the correct byte */
                ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FDIV_FWVER_04 & (~0x00000003uL), &fwVal);
                fDivider = (uint8_t)( (fwVal >> 8) & 0xFFu);
            }

            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                /* PCLK Frequency is 32bit value in Hz. We need to calculate MHz from that */
                ret = R_FDL_IFct_GetFWParam (R_EXTRA3_FPCLK_FWVER_04, &fwVal);
                fPClk    = fwVal / 1000000uL;
            }

            if (R_FDL_OK == ret)    /* no error during parameter read */
            {
                ret = R_FDL_IFct_GetFWParam (R_EXTRA3_ECCADDR, &(g_fdl_str.baseAddrECC_u32) );
            }
        }

        if (R_FDL_OK == ret)    /* no error during parameter read */
        {
            /* CPU frequency is within boundaries */
            if ( (fCpu >= fMin) && (fCpu <= fMax) )                                                 /* PRQA S 2962 */
            {
                /* FCU frequency is fix and independent from the CPU frequency */
                if (0xFF == fDivider)                                                               /* PRQA S 2962 */
                {
                    fFaci = (uint16_t)fPClk;                                                        /* PRQA S 2962 */
                }
                
                /* FCU frequency calculation (including frequency round up) */
                else
                {
                    fFaci = (fCpu + fDivider) - 1;
                    fFaci = fFaci / fDivider;
                }

                /* Set frequency */
                R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_PCKAR_U16, R_FCU_REGBIT_PCKAR_KEY + fFaci);
            }
            
            /* CPU frequency is out of bounds */
            else
            {
                ret = R_FDL_ERR_CONFIGURATION;
            }
        }
    }
    return (ret);
} /* R_FDL_FCUFct_SetFrequency */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_VerifyChecksum
 *********************************************************************************************************************/
/**
 * Function to verify the FCURAM checksum. \n
 *
 * @param[in,out]   -
 * @return
 *                  - R_FDL_BUSY
 *                  - R_FDL_ERR_INTERNAL (code in FCU RAM not OK)
 *
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_VerifyChecksum (void)
{
    r_fdl_status_t ret;

    #ifdef R_FDL_NO_BFA_SWITCH
    ret = R_FDL_BUSY;

    #else
    uint32_t add;
    uint32_t addEnd;
    uint32_t chkSum;

    /* Set FCU RAM to RW */
    R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_FCURAME_U16, R_FCU_REGBIT_FCURAME_FCRME + R_FCU_REGBIT_FCURAME_KEY);

    /* Calculate and compare FCU RAM checksum */
    addEnd = g_fdl_str.chksumEndAddr_u32;
    addEnd <<= 2;
    addEnd += R_FCU_RAM_ADD;
    chkSum = 0x00000000uL;

    /* Calculate the checksum over the FCU RAM */
    for (add = R_FCU_RAM_ADD; add < addEnd; add += 2)
    {
        chkSum += (uint32_t)(R_FDL_IFct_ReadMemoryU16 (add) );
    }

    /* Checksum correct */
    if (chkSum == g_fdl_str.chksumVal_u32)
    {
        ret = R_FDL_BUSY;
    }
        
    /* Checksum error */
    else
    {
        ret = R_FDL_ERR_INTERNAL;
    }

    /* Deactivate FCU RAM access */
    R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_FCURAME_U16,
                               R_FCU_REGBIT_FCURAME_RESET + R_FCU_REGBIT_FCURAME_KEY);
    #endif /* ifdef R_FDL_NO_BFA_SWITCH */

    return (ret);
} /* R_FDL_FCUFct_VerifyChecksum */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_InitRAM
 *********************************************************************************************************************/
/**
 * Function to copy the firmware to the FCU RAM. \n
 * As BFLASH need to be activated to copy code from device firmware to FCU RAM, some code needs to
 * be executed from RAM. \n
 * This function:
 * - prepares the parameter structure for the function to be executed in RAM
 * - calls the function to copy and execute the code in RAM
 * - saves the checksum related values for later processing
 * - clear the cache
 *
 * @param[in,out]   -
 * @return     Returns timeout check result
 *             - R_FDL_OK:             function passed
 *             - R_FDL_ERR_INTERNAL:   timeout error
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_InitRAM (void)
{
    r_fdl_status_t ret;

    #ifdef R_FDL_NO_BFA_SWITCH
    ret = R_FDL_OK;

    #else
    uint32_t initRamParam[8];

    /* Initialize the RAM function parameters */
    initRamParam[0] = R_FCU_RAM_SRC_ADD;
    initRamParam[1] = R_FCU_RAM_ADD;
    initRamParam[2] = R_FCU_RAM_SIZE;
    initRamParam[3] = R_FCU_RAM_ADD_CHKSUM_END;
    initRamParam[4] = R_FCU_RAM_ADD_CHKSUM;
    initRamParam[5] = R_FDL_IFct_GetBWCAdd();
    initRamParam[6] = R_FDL_TIMEOUT_CC;                 /* Timeout loop count */
    initRamParam[7] = 0x00000000uL;                     /* Reset function return value (Only set in case of an error) */

    /* Activate FCU RAM access */
    R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_FCURAME_U16,   R_FCU_REGBIT_FCURAME_FCRME + R_FCU_REGBIT_FCURAME_FRAMTRAN
                               + R_FCU_REGBIT_FCURAME_KEY);

    /* Execute Copy routine in RAM as BFA need to be temporarily switched on */
    R_FDL_IFct_ExeCodeInRAM (&R_FDL_FCUFct_InitRAM_Asm, (uint32_t *)(&initRamParam[0]) );

    /* Save checksum values, needed for later processing */
    g_fdl_str.chksumEndAddr_u32 = initRamParam[0];
    g_fdl_str.chksumVal_u32     = initRamParam[1];

    /* Deactivate FCU RAM access */
    R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_FCURAME_U16, R_FCU_REGBIT_FCURAME_RESET + R_FCU_REGBIT_FCURAME_KEY);

    /* No timeout error */
    if (0x00000000uL == initRamParam[7])
    {
        ret = R_FDL_OK;
    }
    else
    {
        ret = R_FDL_ERR_INTERNAL;
    }
    #endif /* ifdef R_FDL_NO_BFA_SWITCH */

    return (ret);


} /* R_FDL_FCUFct_InitRAM */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_InitRAM_Asm
 *********************************************************************************************************************/
/**
 * Copy the firmware to the FCU RAM --> This function is executed in RAM \n
 * Sequence is:
 * - Switch on BFA
 * - Copy code to FCU
 * - Switch off BFA
 * - Clear Cache
 * The cache clear check loop is timeout supervised
 *
 * Note: the code for GHS, IAR and CC-RL is basically equal, only the style is compiler dependent and so, different.
 *       GHS code is fully commented, while the IAR and CC-RL code is partly or not commented. Refer to GHS comments
 *       to understand the code.
 *
* @param[in]  param_pu32 - Parameter array containing addresses and data for the assembler
 *                          function
 * @return     ---
 */
/*********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 3006)
 * Reason:       This function contains a mixture of in-line assembler statements and C statements.
 *               The function is copied to RAM into a memory range of fix size. So, the function itself must have a
 *               fix size which cannot be realized by a c-compiler. So, the function is written in Assembler
 * Verification: Review of the c-interface (only temporary registers are used and the stack size is unchanged
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 1006)
 * Reason:       In-line assembler construct is a language extension. The code has been ignored.
 * Verification: -
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 3206)
 * Reason:       function parameter seems to be not used in the function as no c-code relates on
 *               it, but the assembler code uses the parameter
 * Verification: -
 *********************************************************************************************************************/

#ifdef R_FDL_NO_BFA_SWITCH
#else

#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

R_FDL_COV_SAVEOFF

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_OFF 1006
#endif

#if R_FDL_COMPILER == R_FDL_COMP_GHS
R_FDL_STATIC void R_FDL_FCUFct_InitRAM_Asm (uint32_t * param_pu32)                              /* PRQA S 3006,3206 */
{
    /* GHS compilers use r6~r10 as scratch registers. This function requires r6~r9 */

    /* ----- Switch ON BFA ----- */
    asm (" movea 0x01, r0, r8                   ");     /* Hard coded BFASEL value to switch on EXA3*/
    asm (" br _R_FDL_FCUFct_InitRAM_Asm_SWBFA   ");

    asm (" _R_FDL_FCUFct_InitRAM_Asm_Copy:      ");

    /* Copy FW to RAM */
    /* r7: src, r8: dest, r9: cnt, */
    /* r10: read buffer */
    asm (" ld.w 0[r6], r7                       ");
    asm (" ld.w 4[r6], r8                       ");
    asm (" ld.w 8[r6], r9                       ");

    asm (" _R_FDL_FCUFct_InitRAM_Asm_Loop:      ");
    asm (" ld.w 0[r7], r10                      ");
    asm (" add 4, r7                            ");
    asm (" add 4, r8                            ");
    asm (" add -4, r9                           ");
    asm (" st.w r10, -4[r8]                     ");
    asm (" bnz _R_FDL_FCUFct_InitRAM_Asm_Loop   ");

    /* Copy end address and checksum */
    asm (" ld.w 12[r6], r7                      ");
    asm (" ld.w 0[r7], r10                      ");
    asm (" st.w r10, 0[r6]                      ");
    asm (" ld.w 16[r6], r7                      ");
    asm (" ld.w 0[r7], r10                      ");
    asm (" st.w r10, 4[r6]                      ");

    /* ----- Switch OFF BFA ----- */
    asm (" mov r0, r8                           ");     /* Hard coded BFASEL value to switch off EXA3*/

    /* Switch BFA and clear the cache */
    /* & line buffer (Called twice) */
    asm (" _R_FDL_FCUFct_InitRAM_Asm_SWBFA:     ");

    /* Switch EXA3 */
    asm (" syncp                                ");     /* sync to peripheral access */
    asm (" mov 0xffc59008, r7                   ");     /* Hard coded address of BFASEL */
    asm (" st.b r8, 0[r7]                       ");
    asm (" ld.b 0[r7], r7                       ");     /* Dummy read from FCU register to wait one APB access cycle */
    asm (" syncp                                ");     /* sync to peripheral access */
    asm (" synci                                ");

    /* Clear the cache */
    asm (" stsr 24, r7, 4                       ");     /* system register 24, 4 is ICCTRL */
    asm (" ori 0x0100, r7, r7                   ");     /* set cache clear bit 8 */
    asm (" ldsr r7, 24, 4                       ");

    asm (" ld.w 24[r6], r9                      ");     /* read timeout value */

    asm ("_R_FDL_IFct_InitRAM_Asm_Polling:      ");
    asm (" stsr 24, r7, 4                       ");     /* system register 24, 4 is ICCTRL */
    asm (" andi 0x0100, r7, r7                  ");
    asm (" bz _R_FDL_IFct_InitRAM_Asm_Polling_Pass");   /* exit loop when Cache is cleared (bit is 0) */
    asm (" add -1, r9                           ");     /* check timeout */
    asm (" bnz _R_FDL_IFct_InitRAM_Asm_Polling  ");

    asm (" mov 1, r9                            ");     /* store timeout error and continue*/
    asm (" st.w r9, 28[r6]                      ");
    asm (" _R_FDL_IFct_InitRAM_Asm_Polling_Pass: ");

    asm (" synci                                ");

    /* Check the core - BWC buffer address == 0 --> clear cache
                        BWC buffer address != 0 --> clear BWC buffer */
    asm (" ld.w 20[r6], r9                      ");     /* get BWCBUFEN address */
    asm (" cmp r9, r0                           ");     /* BWCBUFEN address not available on G3M cores */
    asm (" be _R_FDL_FCUFct_InitRAM_Asm_G3M     ");

    /* G3K core: Clear BWC */
    asm (" mov 0x01, r7                         ");     /* BWCBUFCLR bit */
    asm (" st.b r0, 0[r9]                       ");     /* BWCBUFCLR = 0 */
    asm (" st.b r7, 0[r9]                       ");     /* BWCBUFCLR = 1 */
    asm (" st.b r0, 0[r9]                       ");     /* BWCBUFCLR = 0 */
    asm (" ld.b 0[r9], r7                       ");
    asm (" syncp                                ");     /* sync to peripheral access */
    asm (" synci                                ");
    asm (" br _R_FDL_FCUFct_InitRAM_Asm_CCEnd   ");

    /* G3M core: clear sub-cache */
    asm (" _R_FDL_FCUFct_InitRAM_Asm_G3M:       ");
    asm (" stsr 24, r7, 13                      ");     /* system register 24, 13 is CDBCR */
    asm (" ori 0x02, r7, r7                     ");     /* set cache clear bit 1 */
    asm (" ldsr r7, 24, 13                      ");
    asm (" stsr 24, r7, 13                      ");     /* Dummy read to system register to complete the operation */

    asm (" _R_FDL_FCUFct_InitRAM_Asm_CCEnd:     ");

    /* Check where to continue */
    asm (" cmp r8, r0                           ");     /* we check here against the hard coded BFASEL value */
    asm (" bnz _R_FDL_FCUFct_InitRAM_Asm_Copy   ");
} /* R_FDL_FCUFct_InitRAM_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_GHS */

#if R_FDL_COMPILER == R_FDL_COMP_IAR
R_FDL_STATIC void R_FDL_FCUFct_InitRAM_Asm (uint32_t * param_pu32)                              /* PRQA S 3006,3206 */
/* IAR compiler uses with rh850 compiler onwards the same calling conventions as GHS */
{
    __asm ("                                         \n"
           " /* ----- Switch ON BFA ----- */         \n"
           " movea 0x01, r0, r8                      \n"
           " br _R_FDL_FCUFct_InitRAM_Asm_SWBFA      \n"
           "                                         \n"
           "_R_FDL_FCUFct_InitRAM_Asm_Copy:          \n"
           "                                         \n"
           " /* Copy FW to RAM */                    \n"
           " /* r7: src, r8: dest, r9: cnt, */       \n"
           " /* r10: read buffer */                  \n"
           " ld.w 0[r6], r7                          \n"
           " ld.w 4[r6], r8                          \n"
           " ld.w 8[r6], r9                          \n"
           "                                         \n"
           "_R_FDL_FCUFct_InitRAM_Asm_Loop:          \n"
           " ld.w 0[r7], r10                         \n"
           " add 4, r7                               \n"
           " add 4, r8                               \n"
           " add -4, r9                              \n"
           " st.w r10, -4[r8]                        \n"
           " bnz _R_FDL_FCUFct_InitRAM_Asm_Loop      \n"
           "                                         \n"
           " /* Copy end address and checksum */     \n"
           " ld.w 12[r6], r7                         \n"
           " ld.w 0[r7], r10                         \n"
           " st.w r10, 0[r6]                         \n"
           " ld.w 16[r6], r7                         \n"
           " ld.w 0[r7], r10                         \n"
           " st.w r10, 4[r6]                         \n"
           "                                         \n"
           " /* ----- Switch OFF BFA ----- */        \n"
           " mov r0, r8                              \n"
           "                                         \n"
           " /* Switch BFA and clear the cache */    \n"
           " /* & line buffer (Called twice) */      \n"
           "_R_FDL_FCUFct_InitRAM_Asm_SWBFA:         \n"
           "                                         \n"
           " /* Switch EXA3 */                       \n"
           " syncp                                   \n"
           " mov 0xffc59008, r7                      \n"
           " st.b r8, 0[r7]                          \n"
           " ld.b 0[r7], r7                          \n"
           " syncp                                   \n"
           " synci                                   \n"
           "                                         \n"
           " /* Clear the cache */                   \n"
           " stsr 24, r7, 4                          \n"
           " ori 0x0100, r7, r7                      \n"
           " ldsr r7, 24, 4                          \n"
           "                                         \n"
           " ld.w 24[r6], r9                         \n"
           "_R_FDL_IFct_InitRAM_Asm_Polling:         \n"
           " stsr 24, r7, 4                          \n"
           " andi 0x0100, r7, r7                     \n"
           " bz _R_FDL_IFct_InitRAM_Asm_Polling_Pass \n"
           " add -1, r9                              \n"
           " bnz _R_FDL_IFct_InitRAM_Asm_Polling     \n"
           "                                         \n"
           " mov 1, r9                               \n"
           " st.w r9, 28[r6]                         \n"
           "                                         \n"
           "_R_FDL_IFct_InitRAM_Asm_Polling_Pass:    \n"
           " synci                                   \n"
           "                                         \n"
           " /* Check the core ... */                \n"
           " ld.w 20[r6], r9                         \n"
           " cmp r9, r0                              \n"
           " be _R_FDL_FCUFct_InitRAM_Asm_G3M        \n"
           "                                         \n"
           " /* G3K core: Clear BWC */               \n"
           " mov 0x01, r7                            \n"
           " st.b r0, 0[r9]                          \n"
           " st.b r7, 0[r9]                          \n"
           " st.b r0, 0[r9]                          \n"
           " ld.b 0[r9], r7                          \n"
           " syncp                                   \n"
           " synci                                   \n"
           " br _R_FDL_FCUFct_InitRAM_Asm_CCEnd      \n"
           "                                         \n"
           " /* G3M core: clear sub-cache */         \n"
           "_R_FDL_FCUFct_InitRAM_Asm_G3M:           \n"
           " stsr 24, r7, 13                         \n"
           " ori 0x02, r7, r7                        \n"
           " ldsr r7, 24, 13                         \n"
           " stsr 24, r7, 13                         \n"
           "                                         \n"
           "_R_FDL_FCUFct_InitRAM_Asm_CCEnd:         \n"
           "                                         \n"
           " /* Check where to continue */           \n"
           " cmp r8, r0                              \n"
           " bnz _R_FDL_FCUFct_InitRAM_Asm_Copy      \n"
           );
} /* R_FDL_IFct_GetFWParam_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_IAR */

#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_ON 1006
#endif

#if R_FDL_COMPILER == R_FDL_COMP_REC
#pragma inline_asm R_FDL_FCUFct_InitRAM_Asm
R_FDL_STATIC void R_FDL_FCUFct_InitRAM_Asm (uint32_t * param_pu32)
{
    movea 0x01, r0, r8
    br _R_FDL_FCUFct_InitRAM_Asm_SWBFA

_R_FDL_FCUFct_InitRAM_Asm_Copy:

    ld.w 0[r6], r7
    ld.w 4[r6], r8
    ld.w 8[r6], r9

_R_FDL_FCUFct_InitRAM_Asm_Loop:
    ld.w 0[r7], r10
    add 4, r7
    add 4, r8
    add -4, r9                              
    st.w r10, -4[r8]
    bnz _R_FDL_FCUFct_InitRAM_Asm_Loop

    ld.w 12[r6], r7
    ld.w 0[r7], r10
    st.w r10, 0[r6]
    ld.w 16[r6], r7
    ld.w 0[r7], r10
    st.w r10, 4[r6]

    mov r0, r8

_R_FDL_FCUFct_InitRAM_Asm_SWBFA:

    syncp
    mov 0xffc59008, r7
    st.b r8, 0[r7]
    ld.b 0[r7], r7
    syncp
    synci

    stsr 24, r7, 4
    ori 0x0100, r7, r7
    ldsr r7, 24, 4

    ld.w 24[r6], r9
_R_FDL_IFct_InitRAM_Asm_Polling:
    stsr 24, r7, 4
    andi 0x0100, r7, r7
    bz _R_FDL_IFct_InitRAM_Asm_Polling_Pass
    add -1, r9
    bnz _R_FDL_IFct_InitRAM_Asm_Polling
    mov 1, r9
    st.w r9, 28[r6]
_R_FDL_IFct_InitRAM_Asm_Polling_Pass:

    synci

    ld.w 20[r6], r9
    cmp r9, r0
    be _R_FDL_FCUFct_InitRAM_Asm_G3M

    mov 0x01, r7
    st.b r0, 0[r9]
    st.b r7, 0[r9]
    st.b r0, 0[r9]
    ld.b 0[r9], r7
    syncp
    synci
    br _R_FDL_FCUFct_InitRAM_Asm_CCEnd

_R_FDL_FCUFct_InitRAM_Asm_G3M:
    stsr 24, r7, 13
    ori 0x02, r7, r7
    ldsr r7, 24, 13
    stsr 24, r7, 13

_R_FDL_FCUFct_InitRAM_Asm_CCEnd:

    cmp r8, r0
    bnz _R_FDL_FCUFct_InitRAM_Asm_Copy
} /* R_FDL_FCUFct_InitRAM_Asm */
#endif /* if R_FDL_COMPILER == R_FDL_COMP_REC */

R_FDL_COV_RESTORE

#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

#endif /* ifdef R_FDL_NO_BFA_SWITCH */

/*********************************************************************************************************************
 * Function name:  R_FDL_IFct_ExeCodeInRAM
 *********************************************************************************************************************/
/**
 * Execute a function in RAM. \n
 * This function copies a function to the RAM and jumps to the RAM.
 * Stack is used as storage location for the function to be copied.
 *
 * @param[in]  pFct         - function to execute
 * @param[in]  param_pu32   - parameter structure that will be used by the function in RAM. The
 *                            structure pointer is just passed to the function.
 * @return     ---
 */
/*********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 0310)
 * Reason:       Casting to different object pointer type is used because function code need
 *               to be copied to RAM.
 * Verification: Copy destination is g_fdl_str.
 *              - This is re-initialized afterwards in R_FDL_Init
 *              - Copy size is limited by "sizeof"
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 11.3 (QAC message 0303, 0305, 0306)
 * Reason:       Casts between a pointer and other data types are used because function
 *               code need to be copied to RAM and finally a jump to RAM is done
 * Verification: None as complete system would crash if this does not work.
 *********************************************************************************************************************/

/********************************************************************************************************************
* MISRA Rule:   MISRA-C 2004 rule 3.1 (QAC message 0491)
* Reason:       Array subscripting applied to an object of pointer type required to copy data to
*               RAM location
* Verification: Check copy boundaries and address
*********************************************************************************************************************/
#ifdef R_FDL_NO_BFA_SWITCH
#else

#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC void R_FDL_IFct_ExeCodeInRAM (r_fdl_pFct_ExeInRAM pFct, uint32_t * param_pu32)
{
    uint32_t size,
             i,
             add,
             addAl;

    volatile uint16_t * pSrc;
    r_fdl_pFct_ExeInRAM pFctExe;

#ifdef R_FDL_EXE_INIT_CODE_ON_STACK
    /* Code size calculation described in r_fdl_global.h */
    volatile uint16_t g_fdl_ramCodeBuf_au16[R_FDL_RAM_CODE_SIZE_HW];
#endif

    /* Copy code to the 16byte aligned buffer address */
    add   = (uint32_t)(&g_fdl_ramCodeBuf_au16[0]);                                                  /* PRQA S 0306 */
    addAl = ( ( (add + 15uL) >> 4) << 4);
    pSrc  = (uint16_t *)( (uint32_t)pFct - (addAl - add) );                                     /* PRQA S 0305, 0306 */
    size  = sizeof (g_fdl_ramCodeBuf_au16) >> 1;

    for (i = 0; i < size; i++)
    {
        g_fdl_ramCodeBuf_au16[i] = pSrc[i];                                                         /* PRQA S 0491 */
    }

    pFctExe = (r_fdl_pFct_ExeInRAM)(addAl);                                                         /* PRQA S 0305 */

    /* Critical section start - disable all interrupts and exceptions */
    FDL_CRITICAL_SECTION_BEGIN
    /* Execute code in RAM */
    pFctExe (param_pu32);

    /* Critical section end - enable interrupts and exceptions again */
    FDL_CRITICAL_SECTION_END
} /* R_FDL_IFct_ExeCodeInRAM */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

#endif /* ifdef R_FDL_NO_BFA_SWITCH */

/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_SwitchMode_Start
 *********************************************************************************************************************/
/**
 * Switch FCU mode to Programming/User mode
 *
 * @param      mode_u16 - Mode (R_FCU_MODE_PE / R_FCU_MODE_CPE / R_FCU_MODE_USER)
 * @return
 * @li         R_FDL_OK
 * @li         R_FDL_ERR_PROTECTION
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_SwitchMode_Start (uint16_t mode_u16)
{
    volatile uint16_t regFENTRYR;
    uint32_t          regFSTATR;
    uint32_t          cmp;
    r_fdl_status_t    ret;

    ret = R_FDL_OK;

    /* switch target is user mode */
    if (R_FCU_MODE_USER == mode_u16)
    {
        /* Clear FCU errors */
        regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);
        cmp = R_FCU_REGBIT_FSTATR_ILGERR + R_FCU_REGBIT_FSTATR_ERSERR + R_FCU_REGBIT_FSTATR_PRGERR;
        if (R_FCU_REGBIT_FSTATR_RESET != (regFSTATR & cmp) )
        {
            R_FDL_FCUFct_ClearStatus();
        }
        R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_FENTRYR_U16, mode_u16 + R_FCU_REGBIT_FENTRY_KEY);
        g_fdl_str.flashMode_u16 = mode_u16;

    }
    
    /* switch target is PE mode */
    else
    {
        /* Check if any Code flash operation is on-going (try to switch from Code Flash PE mode) */
        regFENTRYR =  R_FDL_IFct_ReadMemoryU16 (R_FCU_REGADD_FENTRYR_U16);
        if ( (regFENTRYR & R_FCU_MODE_CPE) == R_FCU_MODE_CPE)
        {
            ret = R_FDL_ERR_PROTECTION;
        }
        
        /* switch from read mode */
        else
        {
            /* Check if the mode is already set. If yes, setting it again would toggle the mode.
               So, don't set it again */
            if (mode_u16 != regFENTRYR)
            {
                R_FDL_IFct_WriteMemoryU16 (R_FCU_REGADD_FENTRYR_U16, mode_u16 + R_FCU_REGBIT_FENTRY_KEY);

                /* also clear FSADDRR and FEADDRR when entering P/E mode in order to allow access address
                   checking by R_FDL_IFct_ChkAccessRight */
                R_FDL_IFct_WriteMemoryU32 (R_FCU_REGADD_FSADR_U32, 0x00000000uL);
                R_FDL_IFct_WriteMemoryU32 (R_FCU_REGADD_FEADR_U32, 0x00000000uL);
            }
            g_fdl_str.flashMode_u16 = mode_u16;
        }
    }

    return (ret);
} /* R_FDL_FCUFct_SwitchMode_Start */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_SwitchMode_Check
 *********************************************************************************************************************/
/**
 * This function checks if the Flash sequencer operation mode switch is performed correctly
 *
 * @param[in,out]   -
 * @return
 * @li              R_FDL_OK
 * @li              R_FDL_BUSY
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_SwitchMode_Check (void)
{
    volatile uint16_t regFENTRYR;
    r_fdl_status_t    ret;

    ret = R_FDL_BUSY;

    /* We need a dummy read as the mode is not changed immediately on some devices */
    /* this is required because in automatic switch mode acceptance, the switch mode check function
       may only be called once to ensure correct operation for interrupt support */
    regFENTRYR =  R_FDL_IFct_ReadMemoryU16 (R_FCU_REGADD_FENTRYR_U16);
    regFENTRYR =  R_FDL_IFct_ReadMemoryU16 (R_FCU_REGADD_FENTRYR_U16);

    if (g_fdl_str.flashMode_u16 == regFENTRYR)
    {
        ret = R_FDL_OK;
    }

    return (ret);
} /* R_FDL_FCUFct_SwitchMode_Check */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_GetStat
 *********************************************************************************************************************/
/**
 * Return FCU Flash operation result
 *
 * @param[in,out]   -
 * @return          Operation result:
 *                  - R_FDL_ERR_BLANKCHECK
 *                  - R_FDL_ERR_WRITE
 *                  - R_FDL_ERR_ERASE
 *                  - R_FDL_BUSY
 *                  - R_FDL_OK
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_GetStat (void)
{
    r_fdl_status_t    stat;
    volatile uint32_t regFSTATR;
    volatile uint8_t  regFBCSTAT;

    /* For BC we need to check the command too because the bit cannot be reset after BC execution
       and so stays valid until next BC */
    regFBCSTAT = R_FDL_IFct_ReadMemoryU08 (R_FCU_REGADD_FBCSTAT_U08);

    /* Blank check command and blank error detected */
    if ( (R_FCU_REGBIT_FBCSTAT_BCST == (regFBCSTAT & R_FCU_REGBIT_FBCSTAT_BCST) ) &&
         (R_FDL_CMD_BLANKCHECK == g_fdl_str.reqInt_pstr->command_enu) )
    {
        stat = R_FDL_ERR_BLANKCHECK;
        g_fdl_str.opFailAddr_u32 = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FPSADDR_U32);
    }
    
    /* No Blank Check error or other command the Blank Check */
    else
    {
        regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

        #ifdef PATCH_TO_SIMULATE_ERRORS
        R_FDL_COV_SAVEOFF
        if (tstData_str.simError_enu == R_FDL_TRUE)
        {
            switch (tstData_str.simErrorType_enu)
            {
                case R_FDL_SIM_ERROR_ERASE:
                {
                    regFSTATR |= R_FCU_REGBIT_FSTATR_ERSERR;
                    tstData_str.simError_enu = R_FDL_FALSE;
                    break;
                }

                case R_FDL_SIM_ERROR_WRITE:
                {
                    regFSTATR |= R_FCU_REGBIT_FSTATR_PRGERR;
                    tstData_str.simError_enu = R_FDL_FALSE;
                    break;
                }

                default:
                {
                    break;
                }
            }     /* switch */

        }
        R_FDL_COV_RESTORE
        #endif /* ifdef PATCH_TO_SIMULATE_ERRORS */

        /* Detected programming error */
        if (R_FCU_REGBIT_FSTATR_PRGERR == (regFSTATR & R_FCU_REGBIT_FSTATR_PRGERR) )
        {
            stat = R_FDL_ERR_WRITE;
        }
        else
        {

            /* Detected Erase error */
            if (R_FCU_REGBIT_FSTATR_ERSERR == (regFSTATR & R_FCU_REGBIT_FSTATR_ERSERR) )
            {
                stat = R_FDL_ERR_ERASE;
            }
            else
            {

                /* In case of program or erase suspend bit set, returned status remains busy as
                   this is the resume status */
                if ( ( (R_FCU_REGBIT_FSTATR_PRGSPD == (regFSTATR & R_FCU_REGBIT_FSTATR_PRGSPD) ) &&
                       (R_FDL_CMD_WRITE == g_fdl_str.reqInt_pstr->command_enu) ) ||
                     ( (R_FCU_REGBIT_FSTATR_ERSSPD == (regFSTATR & R_FCU_REGBIT_FSTATR_ERSSPD) ) &&
                       (R_FDL_CMD_ERASE == g_fdl_str.reqInt_pstr->command_enu) ) )
                {
                    stat = R_FDL_BUSY;
                }
                else
                {
                    stat = R_FDL_OK;
                }
            }
        }
    }

    return (stat);
} /* R_FDL_FCUFct_GetStat */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_StartWriteOperation
 *********************************************************************************************************************/
/**
 * This function configures the Flash programming sequencer and starts the operation
 *
 * @param[in]  addSrc_u32  - source data address
 * @param[in]  addDest_u32 - EEP Flash write address, relative to EEP Flash base address
 * @param[in]  cnt_u32     - Number of words to write. Allowed is 1 or 4 words
 * @param[in]  accType_enu - User/EEL access (check of access rights)
 *
 * @return     Parameter check result:
 *             - R_FDL_BUSY
 *             - R_FDL_ERR_PROTECTION
 *             - R_FDL_ERR_INTERNAL
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_StartWriteOperation (uint32_t addSrc_u32,
                                                 uint32_t addDest_u32,
                                                 uint32_t cnt_u32,
                                                 r_fdl_accessType_t accType_enu)
{
    volatile uint32_t regFSTATR;
    uint32_t          i;
    r_fdl_status_t    ret;
    r_fdl_status_t    res;
    uint16_t          data;

    ret = R_FDL_BUSY;

    R_FDL_IFct_WriteMemoryU32 (R_FCU_REGADD_FSADR_U32, addDest_u32);

    /* We just write the end address to this register for the access rights check because the
       hardware does not store the write end address in a readable register
       Note: Writing the register is only possible before the write command is issued afterwards
             it is no longer possible*/
    R_FDL_IFct_WriteMemoryU32 (R_FCU_REGADD_FEADR_U32, addDest_u32 + (cnt_u32 - 1uL) );

#ifdef R_FDL_TST_WA_F1L_RESETBLOCKING
    {
        uint32_t loop;
        EEL_ROBUSTNESSTEST_DISABLE_RESET
        for (loop = 0; loop < 10000; loop++)
        {
        }
    }
#endif

    R_FDL_IFct_WriteMemoryU16 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_WRITE);

    /* copy to FCU is 2 bytes at once */
    cnt_u32 /= R_FCU_DATA_TRANSFERSIZE;

    R_FDL_IFct_WriteMemoryU16 (R_FCU_DFLASH_CMD_ADD, (uint16_t)cnt_u32);

    /* Transfer write data to FACI */
    for (i = 0; i < cnt_u32; ++i)
    {
        /* assume reading of unaligned source buffer */
        data = (uint16_t)R_FDL_IFct_ReadMemoryU08 (addSrc_u32 + 1);
        data <<= 8;
        data += (uint16_t)R_FDL_IFct_ReadMemoryU08 (addSrc_u32);

        addSrc_u32 += 2;
        R_FDL_IFct_WriteMemoryU16 (R_FCU_DFLASH_CMD_ADD, data);

        /* DBFull signal may never occur */
        regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);
        if (R_FCU_REGBIT_FSTATR_DBFULL == (regFSTATR & R_FCU_REGBIT_FSTATR_DBFULL) )
        {
            ret = R_FDL_ERR_INTERNAL;
        }
    }

    if (R_FDL_BUSY == ret)
    {
        ret = R_FDL_IFct_ChkAccessRight (accType_enu, (cnt_u32 * R_FCU_DATA_TRANSFERSIZE) );
    }

    /* Check if an error occurred. Then, the library shall exit the P/E mode and return to read mode
      (possible if FRDY = 1) */
    if (R_FDL_BUSY == ret)
    {
        R_FDL_IFct_WriteMemoryU16 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_EXE);
    }
    else
    {
        /* At this point the FRDY = 0, and by calling the ForcedStop, the FCU generates an intentional illegal error
           and FRDY bit becomes 1 */
        res = R_FDL_FCUFct_ForcedStop();
        if (R_FDL_OK != res)
        {
            ret = res; /* Internal error overrules a potential protection error */
        }
    }

#ifdef R_FDL_TST_WA_F1L_RESETBLOCKING
    EEL_ROBUSTNESSTEST_ENABLE_RESET
#endif

    return (ret);
} /* R_FDL_FCUFct_StartWriteOperation */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_StartBCEraseOperation
 *********************************************************************************************************************/
/**
 * This function configures the Flash programming sequencer and starts the operation
 *
 * @param[in]  addStart_u32 - Start address (1st block address for erase, 1st address for BC)
 * @param[in]  addEnd_u32   - End address (last block address for erase, last address for BC)
 * @param[in]  fcuCmd_u08   - Type of command. Erase / BC
 * @param[in]  accType_enu  - User/EEL access (check of access rights)
 *
 * @return status of the operation to start
 *         - R_FDL_BUSY - operation started
 *         - R_FDL_ERR_PROTECTION - operation blocked due to protection
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_StartBCEraseOperation (uint32_t addStart_u32,
                                                   uint32_t addEnd_u32,
                                                   uint8_t fcuCmd_u08,
                                                   r_fdl_accessType_t accType_enu)
{
    r_fdl_status_t ret;
    r_fdl_status_t res;

    R_FDL_IFct_WriteMemoryU32 (R_FCU_REGADD_FSADR_U32, addStart_u32);
    R_FDL_IFct_WriteMemoryU32 (R_FCU_REGADD_FEADR_U32, addEnd_u32);

    R_FDL_IFct_WriteMemoryU08 (R_FCU_DFLASH_CMD_ADD, fcuCmd_u08);

    ret = R_FDL_IFct_ChkAccessRight (accType_enu, R_WRITE_SIZE);

    /* Check if an error occurred. Then, the library shall exit the P/E mode and return to read mode
      (possible if FRDY = 1) */
    if (R_FDL_BUSY == ret)
    {
        R_FDL_IFct_WriteMemoryU08 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_EXE);
    }
    else
    {
        /* At this point the FRDY = 0, and by calling the ForcedStop, the FCU generates an intentional illegal error
           and FRDY bit becomes 1 */
        res = R_FDL_FCUFct_ForcedStop();
        if (R_FDL_OK != res)
        {
            ret = res; /* Internal error overrules a potential protection error */
        }
    }

    return (ret);
} /* R_FDL_FCUFct_StartBCEraseOperation */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ReadOperation
 *********************************************************************************************************************/
/**
 * This function reads data from the Data Flash. Doing so, it disables the ECC error interrupts to
 * avoid interrupt generation on accepted ECC errors when reading e.g. blank or partly written
 * Flash words. Instead, the error and fail address is returned to the calling function.
 * Note: Detecting a single bit error does not stop reading the Data Flash. Instead, the error
 *       is returned together with the fail address and the user application / EEL can still
 *       judge if it trusts the data. If later on a double bit error is detected, the function
 *       stop reading the data and the single bit error indication and address is overwritten
 *       with the double bit error indication and address
 *
 * @param[in,out]  pAddSrc_u32 \n
 *                 in:  Data Flash src address (byte index) to read. \n
 *                 out: In ECC error case the fail address is returned
 * @param[in]      addDest_u32 - destination buffer address
 * @param[in]      cnt_u32 - number of words to read
 *
 * @return     Parameter check result:
 *             - R_FDL_OK
 *             - R_FDL_ERR_ECC_SED (Single bit error detected during read)
 *             - R_FDL_ERR_ECC_DED (Double bit error detected during read)
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_ReadOperation (volatile uint32_t * pAddSrc_u32,
                                           uint32_t addDest_u32,
                                           uint32_t cnt_u32)
{
    r_fdl_status_t ret;
    uint32_t       addEnd;
    uint32_t       add;
    uint32_t       data;
    uint8_t        regDFERINT;
    uint32_t       regDFERSTR;


    ret     = R_FDL_OK;
    add     = (*pAddSrc_u32) + R_FCU_DFLASH_READ_ADD;
    addEnd  = add + (cnt_u32 * R_WRITE_SIZE);

    /* Clear ECC errors */
    R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_DFERSTC_U08, R_FCU_REGBIT_DFERSTC_ERRCLR);

    /* Backup and Disable ECC error interrupts */
    regDFERINT = R_FDL_IFct_ReadMemoryU08 (R_FCU_REGADD_DFERINT_U08);
    R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_DFERINT_U08, R_FCU_REGVAL_DFERINT_NOINT);

    /* Loop over all addresses to read */
    do
    {
        data = R_FDL_IFct_ReadMemoryU32 (add);
        R_FDL_IFct_WriteMemoryU32 (addDest_u32, data);

        regDFERSTR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_DFERSTR_U32);
        #ifdef PATCH_TO_SIMULATE_ERRORS
        R_FDL_COV_SAVEOFF
        if (tstData_str.simError_enu == R_FDL_TRUE)
        {
            if (R_FDL_SIM_ERROR_BITCHECK == tstData_str.simErrorType_enu)
            {
                if (2 == tstData_str.simErrorVal_u32)     /*simulate double bit error*/
                {
                    regDFERSTR |= R_FCU_REGBIT_DFERSTR_DEDF;
                    tstData_str.simError_enu = R_FDL_FALSE;
                }
                else     /*simulate single bit error*/
                {
                    regDFERSTR |= R_FCU_REGBIT_DFERSTR_SEDF;
                    tstData_str.simError_enu = R_FDL_FALSE;
                }
            }
        }
        R_FDL_COV_RESTORE
        #endif /* ifdef PATCH_TO_SIMULATE_ERRORS */


        /* Do we have an ECC error? */
        if (R_FCU_REGVAL_DFERSTR_NOERR != regDFERSTR)
        {

            /* Double error detected */
            if (R_FCU_REGBIT_DFERSTR_DEDF == (R_FCU_REGBIT_DFERSTR_DEDF & regDFERSTR) )
            {
                (*pAddSrc_u32) = add - R_FCU_DFLASH_READ_ADD;
                ret = R_FDL_ERR_ECC_DED;
            }
            
            /* On single bit error ... */
            else
            {
                /* ... note first error occurrence address */
                if (R_FDL_OK == ret)
                {
                    (*pAddSrc_u32) = add - R_FCU_DFLASH_READ_ADD;
                    ret = R_FDL_ERR_ECC_SED;
                }
            }

            /* Clear ECC errors */
            R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_DFERSTC_U08, R_FCU_REGBIT_DFERSTC_ERRCLR);
        }
        add += R_WRITE_SIZE;
        addDest_u32 += R_WRITE_SIZE;
    }
    while ( (add < addEnd) && (R_FDL_ERR_ECC_DED != ret) );

    /* Restore the ECC error interrupts */
    R_FDL_IFct_WriteMemoryU08 (R_FCU_REGADD_DFERINT_U08, regDFERINT);

    return (ret);
} /* R_FDL_FCUFct_ReadOperation */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ChkStartable
 *********************************************************************************************************************/
/**
 * function to check if write or erase may be started. BC or bit error check may always be started
 * so this test is not necessary there
 * - Erase may only start when no operation is suspended
 * - Write may only start if no write is suspended
 *
 * @param[in]  cmd_enu - command to be executed (erase/write/BC)
 * @return     check result
 *             - R_FDL_TRUE  - Flash operation is startable
 *             - R_FDL_FALSE - Flash operation is not startable
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_flag_t R_FDL_FCUFct_ChkStartable (r_fdl_command_t cmd_enu)
{
    volatile uint32_t regFSTATR;
    r_fdl_flag_t      ret;

    ret = R_FDL_TRUE;

    regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

    /* For the write command ... */
#if (defined ENABLE_CMD_WRITE16B)
    if ( (R_FDL_CMD_WRITE == cmd_enu) || (R_FDL_CMD_WRITE16B == cmd_enu) )
#else
    if (R_FDL_CMD_WRITE == cmd_enu)
#endif
    {
        /* check if the write suspend flag is set */
        if (0x00000000uL != (regFSTATR & R_FCU_REGBIT_FSTATR_PRGSPD) )
        {
            ret = R_FDL_FALSE;
        }
    }
    
    /* For the erase command ... */
    else
    {
        /* check if the write or erase suspend flag is set */
        if (0x00000000uL != (regFSTATR & (R_FCU_REGBIT_FSTATR_ERSSPD + R_FCU_REGBIT_FSTATR_PRGSPD) ) )
        {
            ret = R_FDL_FALSE;
        }
    }

    return (ret);
} /* R_FDL_FCUFct_ChkStartable */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ChkSuspendable
 *********************************************************************************************************************/
/**
 * Checks if a Flash operation may be suspended. \n
 * A Flash operation may be suspeded if the SUSRDY bit in FSTATR is set
 *
 * @param[in,out]   -
 * @return     check result
 *             - R_FDL_TRUE  - Flash operation is suspendable
 *             - R_FDL_FALSE - Flash operation is not suspendable
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_flag_t R_FDL_FCUFct_ChkSuspendable (void)
{
    volatile uint32_t regFSTATR;
    r_fdl_flag_t      ret;

    regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

    /* Check if the operation can be suspended */
    if (R_FCU_REGBIT_FSTATR_SUSRDY == (regFSTATR & R_FCU_REGBIT_FSTATR_SUSRDY) )
    {
        ret = R_FDL_TRUE;
        #if (defined ENABLE_CMD_WRITE16B)
        if (R_FDL_CMD_WRITE16B == g_fdl_str.reqInt_pstr->command_enu)
        {
            ret = R_FDL_FALSE;
        }
        #endif
    }
    else
    {
        ret = R_FDL_FALSE;
    }

    return (ret);
} /* R_FDL_FCUFct_ChkSuspendable */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_Suspend
 *********************************************************************************************************************/
/**
 * Suspends an ongoing Flash Flash operation
 *
 * @param[in,out]   -
 * @return          ---
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_FCUFct_Suspend (void)
{
    /* Suspend command */
    R_FDL_IFct_WriteMemoryU16 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_SUSPEND);
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ResumeChkNeed
 *********************************************************************************************************************/
/**
 * Checks if a formerly suspended Flash erase operation need to be resumed (not finished yet). \n
 * This is done by checking the PRGSPD and ERSSPD bits in FSTATR
 *
 * @param[in,out]   -
 * @return          check result
 *                  - R_FDL_TRUE  - Need to resume
 *                  - R_FDL_FALSE - Need no resume
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_flag_t R_FDL_FCUFct_ResumeChkNeed (void)
{
    volatile uint32_t regFSTATR;
    r_fdl_flag_t      ret;


    regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

    /* Any suspend flag set in the FACI? */
    if (0x00000000uL == (regFSTATR & (R_FCU_REGBIT_FSTATR_PRGSPD + R_FCU_REGBIT_FSTATR_ERSSPD) ) )
    {
        ret = R_FDL_FALSE;
    }
    else
    {
        ret = R_FDL_TRUE;
    }

    return (ret);
} /* R_FDL_FCUFct_ResumeChkNeed */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_Resume
 *********************************************************************************************************************/
/**
 * Resumes a formerly suspended Flash operation
 *
 * @param[in,out]   -
 * @return          ---
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_FCUFct_Resume (void)
{
    /* Resume command */
    R_FDL_IFct_WriteMemoryU16 (R_FCU_DFLASH_CMD_ADD, R_FCU_CMD_EXE);
}
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_ChkReady
 *********************************************************************************************************************/
/**
 * Returns sequencer busy/Ready status \n
 * Ready if FSTATR.FRDY = 1
 *
 * @param[in,out]   -
 * @return          Status of the sequencer
 *                  - R_FDL_TRUE  - Sequencer is not busy with a Flash operation
 *                  - R_FDL_FALSE - Sequencer is busy with a Flash operation
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_flag_t R_FDL_FCUFct_ChkReady (void)
{
    r_fdl_flag_t      ret;
    volatile uint32_t regFSTATR;

    regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

    #ifdef PATCH_TO_SIMULATE_ERRORS
    R_FDL_COV_SAVEOFF
    if ( (tstData_str.simError_enu == R_FDL_TRUE) &&
         (R_FDL_SIM_FCU_TIMEOUT == tstData_str.simErrorType_enu) )
    {
        regFSTATR &= (~R_FCU_REGBIT_FSTATR_FRDY);
    }
    R_FDL_COV_RESTORE
    #endif

    /* Check if the FACI is ready */
    if (R_FCU_REGBIT_FSTATR_FRDY == (regFSTATR & R_FCU_REGBIT_FSTATR_FRDY) )
    {
        ret = R_FDL_TRUE;

    #ifdef PATCH_TO_SIMULATE_ERRORS
        R_FDL_COV_SAVEOFF
        if ( (tstData_str.simError_enu == R_FDL_TRUE) &&
             (tstData_str.simErrorType_enu == R_FDL_SIM_ERROR_FATALERROR) )
        {
            tstData_str.simErrorCnt_u32++;
        }
        R_FDL_COV_RESTORE
    #endif
    }
    else
    {
        ret = R_FDL_FALSE;
    }

    return (ret);
} /* R_FDL_FCUFct_ChkReady */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_FCUFct_CheckFatalError
 *********************************************************************************************************************/
/**
 * Returns information if a fatal error occurred in the FCU (e.g. double bit ECC errors, FCU internal errors,
 * protection error ...).\n
 *
 * @param[in,out]   -
 * @return          check result
 *                  - R_FDL_OK               - No fatal error in the FCU
 *                  - R_FDL_ERR_PROTECTION   - Protection error caused by FHVE
 *                  - R_FDL_ERR_INTERNAL     - Other fatal FCU errors
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_FCUFct_CheckFatalError (void)
{
    r_fdl_status_t    ret;
    volatile uint32_t regFSTATR;
    uint32_t          checkVal;

    regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);

    #ifdef PATCH_TO_SIMULATE_ERRORS
    R_FDL_COV_SAVEOFF
    if (tstData_str.simError_enu == R_FDL_TRUE)
    {
        if (R_FDL_SIM_ERROR_FATALERROR == tstData_str.simErrorType_enu)
        {
            if ( (tstData_str.simErrorCntSet_u32 == 0)
                 || ( (tstData_str.simErrorCntSet_u32 != 0)
                      && (tstData_str.simErrorCntSet_u32 == tstData_str.simErrorCnt_u32) ) )
            {

                while ( (regFSTATR & R_FCU_REGBIT_FSTATR_FRDY) != R_FCU_REGBIT_FSTATR_FRDY)
                {
                    regFSTATR = R_FDL_IFct_ReadMemoryU32 (R_FCU_REGADD_FSTATR_U32);
                }
                regFSTATR |= ( (uint16_t)tstData_str.simErrorVal_u32);
                tstData_str.simError_enu = R_FDL_FALSE;
            }
        }
        else if (R_FDL_SIM_ERROR_FHVE == tstData_str.simErrorType_enu)
        {
            regFSTATR |= ( (uint16_t)tstData_str.simErrorVal_u32);
            tstData_str.simError_enu = R_FDL_FALSE;
        }
    }
    R_FDL_COV_RESTORE
    #endif /* ifdef PATCH_TO_SIMULATE_ERRORS */

    ret = R_FDL_OK;
    checkVal = ( ( ( ( ( R_FCU_REGBIT_FSTATR_FRDTCT
                       + R_FCU_REGBIT_FSTATR_TBLDTCT )
                       + R_FCU_REGBIT_FSTATR_CFGDTCT )
                       + R_FCU_REGBIT_FSTATR_FCUERR )
                       + R_FCU_REGBIT_FSTATR_ILGERR )
                       + R_FCU_REGBIT_FSTATR_OTPDTCT );

    /* Any detected error flag results in internal error (fatal error) */
    if ( (checkVal & regFSTATR) != 0x00000000u)
    {
        ret = R_FDL_ERR_INTERNAL;
    }
    else
    {
        checkVal = R_FCU_REGBIT_FSTATR_FHVEERR;

        /* Check FV`HVE protection error */
        if ( (checkVal & regFSTATR) != 0x00000000u)
        {
            ret = R_FDL_ERR_PROTECTION;
        }
    }

    return (ret);
} /* R_FDL_FCUFct_CheckFatalError */

/*********************************************************************************************************************/
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

#define R_FDL_STOP_SEC_CONST
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */

/*********************************************************************************************************************/