r_fdl_user_if.c 79.1 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
#ifdef ENABLE_QAC_TEST
    #pragma PRQA_MESSAGES_OFF 0292
#endif
/*********************************************************************************************************************
 * Library       : Data Flash Access Library for Renesas RH850 devices
 *
 * File Name     : $Source: r_fdl_user_if.c $
 * Lib. Version  : $RH850_FDL_LIB_VERSION_T01: V2.11 $
 * Mod. Revision : $Revision: 1.52 $
 * Mod. Date     : $Date: 2016/06/01 12:39:21JST $
 * Device(s)     : RV40 Flash based RH850 microcontroller
 * Description   : FDL user 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 11.3 (QAC message 0306)
 * 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.
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 14.1 (QAC message 1503)
 * Reason:       This function is not used within the library, it is only used by the user
 *               application.
 * Verification: This function is part of the library API and therefore essential on the one hand.
 *               On the other hand the QAC warning is correct, because this function is never
 *               called within the library. To solve this dilemma it is necessary and suitable
 *               to disable the message.
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * 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
 *********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 9.1 (QAC message 2962)
 * Reason:       Apparent using value of an uninitialized variable fcuCmd, used to convert API commands
 *              (see r_fdl_command_t enum) into commands passed to the hardware.
 * Verification: The variable fcuCmd is set properly for all commands except R_FDL_CMD_READ, for which is not used
 *               at all.
 *********************************************************************************************************************/

#define R_FDL_USERIF_C

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

/*********************************************************************************************************************
 * Module internal function prototypes
 *********************************************************************************************************************/
R_FDL_STATIC void           R_FDL_UFct_EndCurrentOperation             (r_fdl_status_t ret_enu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_UFct_FlashOpStart                    (r_fdl_request_t * request_pstr,
                                                                        r_fdl_status_t * tmpStat_penu,
                                                                        r_fdl_flag_t isCalledFromExecute_enu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_BusyAndSuspendPossible       (void);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_ChkResume                    (r_fdl_status_t * tmpStat_penu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_ReactOnFatalErrors           (r_fdl_status_t * tmpStat_penu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_StatusCheckAndSuspendOnReady (r_fdl_status_t * tmpStat_penu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_MultiOperation               (r_fdl_status_t * tmpStat_penu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_UFct_StateNotStandBy                 (void);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_OperationStates              (r_fdl_status_t * tmpStat_penu);
R_FDL_STATIC r_fdl_flag_t   R_FDL_Handler_CancelReq                    (r_fdl_status_t * tmpStat_penu);

/*********************************************************************************************************************
 * Global variables
 *********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   QAC message 2022
 * Reason:       Basically, global variables should be initialized. However, as the library is used for multiple
 *               compilers and not all compilers support explicit initialization for variables in not standard
 *               sections, this variable is initialized in the library initialization function rather than by the
 *               compiler.
 * Verification: test case
 *********************************************************************************************************************/
/* Note:
 * The define "R_FDL_COV_ENABLED" is a specific define for library testing purpose only.
 * The user application shall not set this define */
#if (!defined R_FDL_COV_ENABLED)
    #define R_FDL_START_SEC_VAR
    #include "r_fdl_mem_map.h"                                                                      /* PRQA S 5087 */

    R_FDL_NOINIT r_fdl_data_t g_fdl_str;                                                            /* PRQA S 2022 */

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

    #ifndef R_FDL_EXE_INIT_CODE_ON_STACK
        #define R_FDL_START_SEC_CODERAM
        #include "r_fdl_mem_map.h"                                                                  /* PRQA S 5087 */

        /* Code size calculation described in r_fdl_global.h */
        R_FDL_NOINIT uint16_t g_fdl_ramCodeBuf_au16[R_FDL_RAM_CODE_SIZE_HW];                        /* PRQA S 2022 */

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

#else /* #if (!defined R_FDL_COV_ENABLED) */
    R_FDL_NOINIT r_fdl_data_t g_fdl_str;                                                            /* PRQA S 2022 */
    #ifndef R_FDL_EXE_INIT_CODE_ON_STACK
        R_FDL_NOINIT uint16_t g_fdl_ramCodeBuf_au16[R_FDL_RAM_CODE_SIZE_HW];                        /* PRQA S 2022 */
    #endif
    
#endif /* #else #if (!defined R_FDL_COV_ENABLED) */


/*********************************************************************************************************************
 * Function name:  R_FDL_InitVariables
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Init function.
 * This function initializes the FDL internal variables and does the FDL configuration parameter checks.
 * The following parameter checks are done:
 * - Descriptor variable is defined
 * - FDL pool is defined
 * - EEL pool is within FDL pool
 *
 * In error case the library is set to not initialized (R_FDL_ISTAT_NOINIT)
 *
 * @param[in]  descriptor_pstr - Pointer to the FDL descriptor structure
 * @return     Initialization status when returned from function call:
 *             - R_FDL_OK                - Initialization was successful
 *             - R_FDL_ERR_CONFIGURATION - FDL configuration parameter error
 */
/*********************************************************************************************************************/
/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 8.8 (QAC message 3408)
 * Reason:       Library is delivered as source code and as a precompiled object.
 *               The file r_fdl_user_if_init.c is not included on generation of the precompiled object.
 *               It is delivered as a separate file and cannot include any other header files, except the
 *               defined interface header files. The function is a library internal function and its declaration
 *               is not part of the interface header files.
 * Verification: -
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_InitVariables (const r_fdl_descriptor_t * descriptor_pstr)                     /* PRQA S 3408 */
{
    r_fdl_status_t ret;
    uint32_t       fdAdd;
    uint32_t       i;
    uint16_t       blkEnd;

    /* Initially clear the complete r_fdl_data structure */
    fdAdd = (uint32_t)(&g_fdl_str);                                                                 /* PRQA S 0306 */
    for (i = 0; i < sizeof (g_fdl_str); i++)
    {
        R_FDL_IFct_WriteMemoryU08 (fdAdd, 0u);
        fdAdd++;
    }

    g_fdl_str.iStat_enu = R_FDL_ISTAT_NOINIT;
    ret = R_FDL_OK;

    /*  We have a descriptor at all */
    if (R_FDL_DESCRIPTOR_POINTER_UNDEFINED != descriptor_pstr)
    {
        /* Set pointer to FDL static configuration */
        g_fdl_str.RTCfg_pstr = descriptor_pstr;

        /* Parameter independent initializations */
        (g_fdl_str.reqSuspend_pstr)    = R_FDL_REQUEST_POINTER_UNDEFINED;
        (g_fdl_str.reqInt_pstr)        = R_FDL_REQUEST_POINTER_UNDEFINED;

        /* Reset misc flags */
        g_fdl_str.cancelRequest_enu    = R_FDL_FALSE;
        g_fdl_str.spdSpdRequest_enu    = R_FDL_FALSE;
        g_fdl_str.spdResRequest_enu    = R_FDL_FALSE;
        g_fdl_str.flashMode_u16        = R_FCU_MODE_USER;
        g_fdl_str.opStatus_enu         = R_FDL_OP_IDLE;

        /* FDL Pool is available */
        if (0u != g_fdl_str.RTCfg_pstr->fdlPoolSize_u16)
        {
            blkEnd =   (g_fdl_str.RTCfg_pstr->eelPoolStart_u16)
                     + (g_fdl_str.RTCfg_pstr->eelPoolSize_u16);
            
            /* EEL Pool fits into FDL pool */
            if (blkEnd >= (g_fdl_str.RTCfg_pstr->fdlPoolSize_u16 + 1u) )
            {
                ret = R_FDL_ERR_CONFIGURATION;
            }
        }
        else
        {
            ret = R_FDL_ERR_CONFIGURATION;
        }
    }

    /*  We don't have a descriptor at all */
    else
    {
        ret = R_FDL_ERR_CONFIGURATION;
    }

    /* Only if no error occurred, the library status is set to initialized */
    if (R_FDL_OK == ret)
    {
        g_fdl_str.iStat_enu = R_FDL_ISTAT_INIT;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Execute
 *********************************************************************************************************************/
/**
 * User interface function:
 * This function initiates a Flash operation (Erase/Write/Read/Prepare Environment).
 * After initiation, the function R_FDL_Handler need to be called regularly to finish the operation.
 *
 * @param[in, out] request_pstr   Pointer to the operation request structure.
 *                                - Structure input elements: \n
 *                                  command_enu, bufAddr_u32, idx_u32, cnt_u16, accessType_enu
 *                                - Structure output element: \n
 *                                  status_enu
 * @return      ---
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void  R_FDL_Execute (r_fdl_request_t * request_pstr)                                                /* PRQA S 1503 */
{
    r_fdl_status_t reqErr;

    reqErr = R_FDL_ERR_REJECTED;

    /* Request structure pointer parameter is not "0x00" */
    if (R_FDL_REQUEST_POINTER_UNDEFINED != request_pstr)
    {

        /* library internal request structure pointer is "0x00" (No operation ongoing) */
        if (R_FDL_REQUEST_POINTER_UNDEFINED == g_fdl_str.reqInt_pstr)
        {

            /* Library is initialized */
            if (R_FDL_ISTAT_INIT == g_fdl_str.iStat_enu)
            {

                /* Only Prepare command is allowed when teh library is in initialized status */
                if (R_FDL_CMD_PREPARE_ENV == request_pstr->command_enu)
                {
                    reqErr = R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_PE);
                    
                    /* No invalid FACI programming mode detected */
                    if (R_FDL_OK == reqErr)
                    {
                        reqErr = R_FDL_BUSY;
                        g_fdl_str.opStatus_enu = R_FDL_OP_EXECUTE_SYNC_ON;
                    }
                }
            }
            
            /* Library is prepared and not busy and no cancel request */
            else if ( (R_FDL_ISTAT_NORMALOP  == g_fdl_str.iStat_enu) ||
                      ( (R_FDL_ISTAT_SUSPENDED == g_fdl_str.iStat_enu) &&
                        (R_FDL_FALSE == g_fdl_str.cancelRequest_enu) ) )
            {
                /* Command selection */
                switch (request_pstr->command_enu)
                {
                    case R_FDL_CMD_BLANKCHECK:
                    {
                        (void)R_FDL_UFct_FlashOpStart (request_pstr, &reqErr, R_FDL_TRUE);
                        break;
                    }

        #if (defined ENABLE_CMD_WRITE16B)
                    case R_FDL_CMD_WRITE16B:
        #endif
                    case R_FDL_CMD_ERASE:
                    case R_FDL_CMD_WRITE:
                    {
                        /* Check FACI suspend flags points out that the command can be started */
                        if (R_FDL_TRUE == R_FDL_FCUFct_ChkStartable (request_pstr->command_enu) )
                        {
                            (void)R_FDL_UFct_FlashOpStart (request_pstr, &reqErr, R_FDL_TRUE);
                        }
                        break;
                    }

                    case R_FDL_CMD_READ:
                    {
                        (void)R_FDL_UFct_FlashOpStart (request_pstr, &reqErr, R_FDL_TRUE);
                        break;
                    }

                    case R_FDL_CMD_PREPARE_ENV:
                    {
                        reqErr = R_FDL_ERR_REJECTED;
                        break;
                    }

                    default:
                    {
                        reqErr = R_FDL_ERR_COMMAND;
                        break;
                    }
                } /* switch */
            }
            else
            {
                /* Nothing to do, added to fix QAC warning message 2004 */
            }
        }

        /* Command execution started */
        if (R_FDL_BUSY == reqErr)
        {
            /* set internal pointer to the request structure, required by the handler */
            g_fdl_str.reqInt_pstr = request_pstr;

        }
        
        /* Command execution rejected */
        else
        {
            /* At operations end (In error case or if the operation requires no handler calls), reset
               the access type */
            request_pstr->accessType_enu = R_FDL_ACCESS_NONE;

            g_fdl_str.opStatus_enu = R_FDL_OP_IDLE;
        }

        /* Set the return state */
        request_pstr->status_enu = reqErr;
    }

    return;
} /* R_FDL_Execute */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_CancelReq
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 * This function cancels an ongoing Flash operation if:
 * - cancel request flag set
 * - switch mode is performed
 *
 * @param[out] tmpStat_penu - Status return value. Is modified, if an operation is cancelled.
 * @return     handler function flow may continue after this function or handler need to exit
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_CancelReq (r_fdl_status_t * tmpStat_penu)
{
    r_fdl_flag_t    cont;
    r_fdl_status_t  res;
    
    cont = R_FDL_TRUE;

    /* Only do something is cancel request flag is not already set */
    if (R_FDL_TRUE == g_fdl_str.cancelRequest_enu)
    {
        if (R_FDL_OK == R_FDL_FCUFct_SwitchMode_Check() )
        {
            /* FACI in programming mode */
            if (R_FCU_MODE_PE == g_fdl_str.flashMode_u16)
            {
                res = R_FDL_FCUFct_ForcedStop();
                
                /* Timeout error in forced stop - internal error 
                   Note: If FACI remains busy, it might happen that a mode switch will not be performed. 
                         Thus, don't try to do it but finish with internal error immediately.
                         This means that DF is possibly not readable */
                if (R_FDL_OK != res)
                {
                    (*tmpStat_penu)         = R_FDL_ERR_INTERNAL;
                    cont                    = R_FDL_FALSE;
                }
                
                /* After forced stop, switch to user mode */
                else
                {
                    (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);
                    g_fdl_str.opResult_enu  = R_FDL_CANCELLED;
                    (*tmpStat_penu)         = R_FDL_BUSY;
                    g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
                }

            }
            
            /* FACI is idle */
            else
            {
                (*tmpStat_penu) = R_FDL_CANCELLED;
                cont = R_FDL_FALSE;
            }

            g_fdl_str.cancelRequest_enu = R_FDL_FALSE;
            g_fdl_str.spdSpdRequest_enu = R_FDL_FALSE;
            g_fdl_str.spdResRequest_enu = R_FDL_FALSE;
            g_fdl_str.iStat_enu         = R_FDL_ISTAT_NORMALOP;

            /* In case of suspended Flash operation, also the suspended operation's request 
               structure need to be updated */
            if (R_FDL_REQUEST_POINTER_UNDEFINED != g_fdl_str.reqSuspend_pstr)
            {
                g_fdl_str.reqSuspend_pstr->status_enu = R_FDL_CANCELLED;
                g_fdl_str.reqSuspend_pstr             = R_FDL_REQUEST_POINTER_UNDEFINED;
            }
        }
        else
        {
            cont = R_FDL_FALSE;
        }
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_BusyAndSuspendPossible
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 * This function suspends an ongoing Flash operation if:
 * - suspend request flag set
 * - function is suspendable
 *
 * @param[in,out]   -
 * @return          Handler function flow may continue afte this function or handler need to exit
 *                  - R_FDL_TRUE  - continue handler
 *                  - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_BusyAndSuspendPossible (void)
{
    r_fdl_flag_t cont;

    cont = R_FDL_TRUE;

    /* Handle a suspend request */
    if (R_FDL_TRUE == g_fdl_str.spdSpdRequest_enu)
    {

        /* FACI allows suspending */
        if (R_FDL_TRUE == R_FDL_FCUFct_ChkSuspendable() )
        {
            /* Must be called before changing g_fdl_str.iStat_enu because
               g_fdl_str.iStat_enu is checked in R_FDL_Standby! */
            R_FDL_FCUFct_Suspend();

            g_fdl_str.spdSpdRequest_enu = R_FDL_FALSE;
            g_fdl_str.iStat_enu         = R_FDL_ISTAT_SUSPEND_PR;
            cont                        = R_FDL_FALSE;
        }
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_ChkResume
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 * This function resumes a suspended Flash operation if:
 * - resume request flag set (can only be set if a function was suspended)
 * - No Flash operation is ongoing
 *
 * @param[out] tmpStat_penu - Status return value. Is modified, if an operation is resumed.
 * @return     handler function flow may continue after this function or handler need to exit:
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_ChkResume (r_fdl_status_t * tmpStat_penu)
{
    r_fdl_flag_t   cont;
    r_fdl_status_t res;

    cont = R_FDL_FALSE;

    /* Resume request flag set and no operation ongoing */
    if ( (R_FDL_REQUEST_POINTER_UNDEFINED == g_fdl_str.reqInt_pstr)
         && (R_FDL_TRUE == g_fdl_str.spdResRequest_enu) )
    {
        g_fdl_str.spdResRequest_enu = R_FDL_FALSE;

        /* We need to resume a Flash operation */
        if (R_FDL_BUSY == g_fdl_str.spdResStatus_enu)
        {
            res = R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_PE);
            /* protection error during mode switching */
            if (R_FDL_OK != res)
            {
                g_fdl_str.reqSuspend_pstr->status_enu = res;
            }
            
            /* Continue resuming */
            else
            {
                g_fdl_str.opStatus_enu  = R_FDL_OP_RESUME_SYNC_ON;
                cont                    = R_FDL_TRUE;
                g_fdl_str.iStat_enu     = R_FDL_ISTAT_NORMALOP;
            }
        }
        
        /* We need to resume to command execution end or to start of a new Flash operation */
        else
        {

            /* In order to support continuing Multi-Operations, we need to patch the
               R_FDL_OK to R_FDL_BUSY. Errors result in operation end (Write, Erase, BC errors) */
            if ( (R_FDL_OK == g_fdl_str.spdResStatus_enu)
                 && (g_fdl_str.spdMulOp_str.flashAdd_u32 < g_fdl_str.spdMulOp_str.flashAddEnd_u32) )
            {
                res = R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_PE);
                /* protection error during mode switching */
                if (R_FDL_OK != res)
                {
                    g_fdl_str.reqSuspend_pstr->status_enu = res;
                }
                
                /* Continue with next resume step */
                else
                {
                    g_fdl_str.opStatus_enu            = R_FDL_OP_RESUME_MULTI_SYNC_ON;
                    g_fdl_str.spdResStatus_enu        = R_FDL_BUSY;
                    cont                              = R_FDL_TRUE;
                    g_fdl_str.iStat_enu               = R_FDL_ISTAT_NORMALOP;
                }
            }
            
            /* Operation ends */
            else
            {
                g_fdl_str.iStat_enu = R_FDL_ISTAT_NORMALOP;
                (*tmpStat_penu) = g_fdl_str.spdResStatus_enu;
            }

        }

        /* on successful resume, restore the FDL operation data */
        if (R_FDL_ISTAT_SUSPENDED != g_fdl_str.iStat_enu)
        {
            /* Restore variables for resume */
            g_fdl_str.reqInt_pstr               = g_fdl_str.reqSuspend_pstr;
            /* if operation finished, the result will be set in the end of the handler function */
            g_fdl_str.reqInt_pstr->status_enu   = R_FDL_BUSY;
            g_fdl_str.reqSuspend_pstr           = R_FDL_REQUEST_POINTER_UNDEFINED;
            g_fdl_str.mulOp_str.flashAdd_u32    = g_fdl_str.spdMulOp_str.flashAdd_u32;
            g_fdl_str.mulOp_str.bufAdd_u32      = g_fdl_str.spdMulOp_str.bufAdd_u32;
            g_fdl_str.mulOp_str.flashAddEnd_u32 = g_fdl_str.spdMulOp_str.flashAddEnd_u32;
            g_fdl_str.mulOp_str.accessType_enu  = g_fdl_str.spdMulOp_str.accessType_enu;
        }
    }
    else
    {
        cont = R_FDL_TRUE;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_ReactOnFatalErrors
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 * Read out Flash operation error status after operation end and react on fatal errors.
 *
 * @param[out] tmpStat_penu - Status return value. Is set to internal error in case of detection
 *                            of a fatal HW error
 * @return     handler function flow may continue after this function or handler need to exit:
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_ReactOnFatalErrors (r_fdl_status_t * tmpStat_penu)
{
    r_fdl_flag_t   cont;
    r_fdl_status_t stat;

    cont = R_FDL_TRUE;

    /* Check for fatal error (protection  or internal error) */
    stat = R_FDL_FCUFct_CheckFatalError();

    /* Error case */
    if (R_FDL_OK != stat)
    {
        R_FDL_FCUFct_ClearStatus();
        (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);

        /* Switch mode function passed */
        if (R_FDL_BUSY == R_FDL_FCUFct_SwitchMode_Check() )
        {
            g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
            /* save status, will be returned on mode switch confirmation */
            g_fdl_str.opResult_enu = stat;
            (*tmpStat_penu)        = R_FDL_BUSY;
        }
        else
        {
            (*tmpStat_penu) = stat;
        }

        cont = R_FDL_FALSE;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_StatusCheckAndSuspendOnReady
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 * Get and judge Flash operation result (not fatal errors, just operation results) and
 * do further suspend processing in case of pending suspend request or already suspended
 * Flash operation.
 *
 * @param[out] tmpStat_penu - Status return value. Is set to operation result or to suspended in
 *                            case of suspend processing.
 * @return     handler function flow may continue after this function or handler need to exit:
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_StatusCheckAndSuspendOnReady (r_fdl_status_t * tmpStat_penu)
{
    r_fdl_flag_t   cont;
    r_fdl_status_t res;

    cont = R_FDL_TRUE;

    /* Get operation status after operation end and reset the hardware status */
    (*tmpStat_penu) = R_FDL_FCUFct_GetStat();
    R_FDL_FCUFct_ClearStatus();

    /* Suspend handling
       - suspend possible, part 2 (Flash operation is suspended now) -or-
       - suspend impossible (operation finished but suspend request still active) */
    if (R_FDL_TRUE == g_fdl_str.spdSpdRequest_enu)
    {
        /* suspend was impossible --> handle it now after operation completion */
        g_fdl_str.spdSpdRequest_enu = R_FDL_FALSE;
        g_fdl_str.iStat_enu = R_FDL_ISTAT_SUSPEND_PR;
    }
    
    if (R_FDL_ISTAT_SUSPEND_PR == g_fdl_str.iStat_enu)
    {
        g_fdl_str.spdResStatus_enu = (*tmpStat_penu);

        (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);
        res = R_FDL_FCUFct_SwitchMode_Check();

        /* Mode already switched */
        if (R_FDL_OK == res)
        {
            (*tmpStat_penu) = R_FDL_SUSPENDED;

            /* Backup variables for resume */
            g_fdl_str.reqSuspend_pstr = g_fdl_str.reqInt_pstr;

            g_fdl_str.iStat_enu = R_FDL_ISTAT_SUSPENDED;
        }
        
        /* Wait for mode switch confirmation */
        else
        {
            (*tmpStat_penu) = R_FDL_BUSY;
            
            g_fdl_str.opStatus_enu = R_FDL_OP_SUSPEND_SYNC_OFF;
        }

        g_fdl_str.spdMulOp_str.flashAdd_u32    = g_fdl_str.mulOp_str.flashAdd_u32;
        g_fdl_str.spdMulOp_str.bufAdd_u32      = g_fdl_str.mulOp_str.bufAdd_u32;
        g_fdl_str.spdMulOp_str.flashAddEnd_u32 = g_fdl_str.mulOp_str.flashAddEnd_u32;
        g_fdl_str.spdMulOp_str.accessType_enu  = g_fdl_str.mulOp_str.accessType_enu;

        cont = R_FDL_FALSE;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_MultiOperation
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 *
 * In case of multi-operations (erase multiple blocks or write multiple half words), after end
 * of a single Flash erase/write Flash operation, the next operation is started.
 *
 * @param[in, out] tmpStat_penu - current operation status. Used to judge previous operation result
 *                                and is updated in case of multi-operation requiring start of new
 *                                Flash operation
 * @return     handler function flow may continue after this function or handler need to exit:
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_MultiOperation (r_fdl_status_t * tmpStat_penu)
{
    r_fdl_flag_t    cont;
    uint32_t        cnt;
    r_fdl_command_t cmd;

    cont = R_FDL_TRUE;
    cmd  = g_fdl_str.reqInt_pstr->command_enu;

    /* Check if more data to handle:
       - erase, write, blank check operation
       - internal status = OK
       - more data to erase/write */
    if ( (R_FDL_OK == (*tmpStat_penu) )
         && (g_fdl_str.mulOp_str.flashAdd_u32 < g_fdl_str.mulOp_str.flashAddEnd_u32) )
    {
        /* Write command starts a new FACI write operation */
#if (defined ENABLE_CMD_WRITE16B)
        if ( (R_FDL_CMD_WRITE == cmd) || (R_FDL_CMD_WRITE16B == cmd) )                              /* PRQA S 0505 */
#else
        if (R_FDL_CMD_WRITE == cmd)
#endif
        {
#if (defined ENABLE_CMD_WRITE16B)
            if (R_FDL_CMD_WRITE == cmd)
            {
                cnt = R_WRITE_SIZE;
            }
            else
            {
                cnt = R_WRITE_SIZE_16B;
            }
#else
            cnt = R_WRITE_SIZE;
#endif

            (*tmpStat_penu) = R_FDL_FCUFct_StartWriteOperation (g_fdl_str.mulOp_str.bufAdd_u32,
                                                                g_fdl_str.mulOp_str.flashAdd_u32,
                                                                cnt,
                                                                g_fdl_str.mulOp_str.accessType_enu);

            g_fdl_str.mulOp_str.flashAdd_u32 += cnt;
            g_fdl_str.mulOp_str.bufAdd_u32   += cnt;
        }
        
        /* Erase command starts a new FACI erase operation */
        else if (R_FDL_CMD_ERASE == cmd)                                                            /* PRQA S 0505 */
        {
            (*tmpStat_penu) = R_FDL_FCUFct_StartBCEraseOperation (g_fdl_str.mulOp_str.flashAdd_u32,
                                                                  (g_fdl_str.mulOp_str.flashAdd_u32 + R_BLOCK_SIZE) - 1u,
                                                                  R_FCU_CMD_ERASE,
                                                                  g_fdl_str.mulOp_str.accessType_enu);

            g_fdl_str.mulOp_str.flashAdd_u32 += R_BLOCK_SIZE;
        }
        
        /* Blank Check command (exceeding 4kB boundary) starts a new FACI blank check operation */
        else
        {
            cnt = ( (g_fdl_str.mulOp_str.flashAddEnd_u32 + 1uL) - g_fdl_str.mulOp_str.flashAdd_u32);
            /* Limit count to max */
            R_FDL_IFct_CalcFOpUnitCnt_BC (&cnt, g_fdl_str.mulOp_str.flashAdd_u32);

            (*tmpStat_penu) = R_FDL_FCUFct_StartBCEraseOperation (g_fdl_str.mulOp_str.flashAdd_u32,
                                                                  ( (g_fdl_str.mulOp_str.flashAdd_u32 + cnt) - 1u),
                                                                  R_FCU_CMD_BLANKCHECK,
                                                                  g_fdl_str.mulOp_str.accessType_enu);

            g_fdl_str.mulOp_str.flashAdd_u32 += cnt;
        }

        /* In command execution end (OK or error) */
        if (R_FDL_BUSY != (*tmpStat_penu) )
        {
            (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);
        
            /* Mode already switched */
            if (R_FDL_BUSY == R_FDL_FCUFct_SwitchMode_Check() )
            {
                g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
                /* save status, will be returned on mode switch confirmation */
                g_fdl_str.opResult_enu = (*tmpStat_penu);
                (*tmpStat_penu) = R_FDL_BUSY;
            }
        }

        cont = R_FDL_FALSE;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler_OperationStates
 *********************************************************************************************************************/
/**
 * Part of the R_FDL_Handler function.
 *
 * This function checks if switch mode is performed.
 * If yes, it executes different operations depending on internal operation status.
 * Prepare environment is performed here also.
 * If not, do nothing
 *
 * @param[in, out] tmpStat_penu - current operation status. Used to judge previous operation result
 *                                and is updated in case of start of new Flash operation
 *
 * @return     handler function flow may continue after this function or handler need to exit:
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_Handler_OperationStates (r_fdl_status_t * tmpStat_penu)
{
    r_fdl_flag_t cont;
    r_fdl_status_t res;
    
    cont = R_FDL_TRUE;

    if ( (R_FDL_OP_IDLE != g_fdl_str.opStatus_enu) &&
         (R_FDL_OP_BUSY != g_fdl_str.opStatus_enu) )
    {
        (*tmpStat_penu) = R_FDL_FCUFct_SwitchMode_Check();

        /* Mode switch passed */
        if (R_FDL_OK == (*tmpStat_penu) )
        {
            
            /* Select current operation status */
            switch (g_fdl_str.opStatus_enu)
            {
                /* Next step after mode switch confirmation for normal operations and prepare */
                case R_FDL_OP_EXECUTE_SYNC_ON:
                {
                    if (R_FDL_CMD_PREPARE_ENV == g_fdl_str.reqInt_pstr->command_enu)
                    {
                        res = R_FDL_FCUFct_ForcedStop();

                        (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);

                        /* No timeout during forced stop processing */
                        if (R_FDL_OK == res)
                        {
                            g_fdl_str.opStatus_enu = R_FDL_OP_PREPARE_START;
                        }
                        else
                        {
                            g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
                            g_fdl_str.opResult_enu = R_FDL_ERR_INTERNAL;
                        }

                        cont                   = R_FDL_FALSE;
                        (*tmpStat_penu)        = R_FDL_BUSY;
                    }
                    else
                    {
                        cont = R_FDL_UFct_FlashOpStart (g_fdl_str.reqInt_pstr, tmpStat_penu, R_FDL_FALSE);
                    }
                    break;
                }

                /* Prepare operation 1st state */
                case R_FDL_OP_PREPARE_START:
                {
                    /* Disable FCU error interrupt which will occur always during FCU initialization from power up */
                    R_FDL_FCUFct_Switch_FAEINT (R_FDL_FALSE);
                    
                    /* Initialize the FCU code RAM */
                    (*tmpStat_penu) = R_FDL_FCUFct_InitRAM();
                    
                    /* No timeout error during RAM initialization */
                    if (R_FDL_OK == (*tmpStat_penu) )
                    {
                        (*tmpStat_penu) = R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_PE);
                        
                        /* No protection error during mode switch */
                        if (R_FDL_OK == (*tmpStat_penu) )
                        {
                            (*tmpStat_penu)        = R_FDL_BUSY;
                            g_fdl_str.opStatus_enu = R_FDL_OP_PREPARE_RST_FCU;
                        }
                    }
                    
                    /* In error case we need to activate the FAEINT again as the operation will immediately finish */
                    if (R_FDL_BUSY != (*tmpStat_penu) )
                    {
                        R_FDL_FCUFct_Switch_FAEINT (R_FDL_TRUE);
                    }

                    cont = R_FDL_FALSE;
                    break;
                }

                /* Prepare operation 2nd state */
                case R_FDL_OP_PREPARE_RST_FCU:
                {
                    /* Reset FCU again to clear any possible ECC errors */
                    res = R_FDL_FCUFct_ForcedStop();
                    R_FDL_FCUFct_ClearStatus();

                    (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);
                    
                    /* Enable FCU error interrupt again, now we expect no more ECC error */
                    R_FDL_FCUFct_Switch_FAEINT (R_FDL_TRUE);
                    
                    /* No timeout during forced stop processing */
                    if (R_FDL_OK == res)
                    {
                        g_fdl_str.opStatus_enu = R_FDL_OP_PREPARE_END;
                    }
                    else
                    {
                        g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
                        g_fdl_str.opResult_enu = R_FDL_ERR_INTERNAL;
                    }

                    (*tmpStat_penu)        = R_FDL_BUSY;
                    cont                   = R_FDL_FALSE;
                    break;
                }

                /* Prepare operation 3rd state */
                case R_FDL_OP_PREPARE_END:
                {
                    (*tmpStat_penu) = R_FDL_FCUFct_VerifyChecksum();

                    /* Checksum check passed */
                    if (R_FDL_BUSY == (*tmpStat_penu) )
                    {
                        (*tmpStat_penu) = R_FDL_FCUFct_GetDFSize (&(g_fdl_str.dfSize_u32) );
                        #ifdef R_FDL_F1L_PATCH_DF_OVERSIZE
                            g_fdl_str.dfSize_u32 *= 2uL;
                        #endif

                        if (R_FDL_OK == (*tmpStat_penu) )
                        {
                            /* FDL pool size does not exceed Data FLash size */
                            if (g_fdl_str.RTCfg_pstr->fdlPoolSize_u16 <= (g_fdl_str.dfSize_u32 >> R_BLOCK_SIZE_2N) )
                            {
                                /* Configure the FCU frequency */
                                (*tmpStat_penu) = R_FDL_FCUFct_SetFrequency();
                            }
                            else
                            {
                                (*tmpStat_penu) = R_FDL_ERR_CONFIGURATION;
                            }
                        }
                    }

                    if (R_FDL_OK == (*tmpStat_penu) )
                    {
                        g_fdl_str.iStat_enu = R_FDL_ISTAT_NORMALOP;
                    }

                    cont = R_FDL_FALSE;
                    break;
                }

                /* Resume command execution (FACI resume) */
                case R_FDL_OP_RESUME_SYNC_ON:
                {
                    R_FDL_FCUFct_Resume();
                    g_fdl_str.opStatus_enu = R_FDL_OP_BUSY;
                    (*tmpStat_penu)        = R_FDL_BUSY;
                    break;
                }

                /* Resume command execution (Start new operation) */
                case R_FDL_OP_RESUME_MULTI_SYNC_ON:
                {
                    g_fdl_str.opStatus_enu = R_FDL_OP_BUSY;
                    (*tmpStat_penu)        = R_FDL_BUSY;
                    break;
                }

                /* Command execution end */
                case R_FDL_OP_END_SYNC_OFF:
                {
                    g_fdl_str.opStatus_enu = R_FDL_OP_IDLE;
                    (*tmpStat_penu)        = g_fdl_str.opResult_enu;
                    cont                   = R_FDL_FALSE;
                    break;
                }

                /* suspend processing end */
                case R_FDL_OP_SUSPEND_SYNC_OFF:
                {
                    (*tmpStat_penu)                        = R_FDL_SUSPENDED;
                    /* Backup variables for resume */
                    g_fdl_str.reqSuspend_pstr              = g_fdl_str.reqInt_pstr;
                    g_fdl_str.iStat_enu                    = R_FDL_ISTAT_SUSPENDED;
                    cont                                   = R_FDL_FALSE;
                    break;
                }

                default:
                {
                    /* Nothing to do */
                    break;
                }
            } /* switch */
        }
        else
        {
            cont = R_FDL_FALSE;
        }
    }
    return (cont);
} /* R_FDL_Handler_OperationStates */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_Handler
 *********************************************************************************************************************/
/**
 * User interface function:
 * This function forwards an initiated Flash operation until it is finished. At operation end the
 * result is set in the request structure.
 * After initiation by R_FDL_Execute, the function R_FDL_Handler need to be called regularly to finish
 * the operation.

 * @param[in,out]  -
 * @return         ---
 *
 * The function returns the operation result in the status variable, passed to
 * the R_FDL_Execute variable (request.status_enu)
 */
/*********************************************************************************************************************/

/*********************************************************************************************************************
 * MISRA Rule:   MISRA-C 2004 rule 1.2 (QAC message 0505)
 * Reason:       QAC claims referencing a possibly undefined pointer.
 * Verification: Reviewed, that this is not possible. The pointer is always defined
 *********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
void R_FDL_Handler (void)                                                                           /* PRQA S 1503 */
{
    r_fdl_flag_t   contHdrFct;
    r_fdl_status_t tmpStatus;
    r_fdl_status_t res;

    tmpStatus = R_FDL_BUSY;

    /* Check if the handler has something to do, block on not initialized library and on StandBy processing */
    if ( (R_FDL_TRUE == R_FDL_UFct_StateNotStandBy() ) &&
         ( (R_FDL_REQUEST_POINTER_UNDEFINED != g_fdl_str.reqInt_pstr) ||
         (R_FDL_TRUE == g_fdl_str.spdResRequest_enu) ||
           (R_FDL_TRUE == g_fdl_str.cancelRequest_enu) ) )
    {
        /* Cancel Request management */
        contHdrFct = R_FDL_Handler_CancelReq (&tmpStatus);

        /* continue handler execution: Suspend handling (suspend possible, part 1)*/
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_Handler_BusyAndSuspendPossible();
        }

        /* continue handler execution: Resume handling */
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_Handler_ChkResume (&tmpStatus);
        }

        /* continue handler execution: command processing execution */
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_Handler_OperationStates (&tmpStatus);
        }

        /* continue handler execution: check if the FCU is busy */
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_FCUFct_ChkReady();
        }

        /* continue handler execution: Fatal error evaluation */
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_Handler_ReactOnFatalErrors (&tmpStatus);
        }

        /* continue handler execution: judge operation status and do suspend handling part 2 */
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_Handler_StatusCheckAndSuspendOnReady (&tmpStatus);
        }

        /* continue handler execution: multi operation (Write/Erase) state machine */
        if (R_FDL_TRUE == contHdrFct)
        {
            contHdrFct = R_FDL_Handler_MultiOperation (&tmpStatus);
        }

        /* continue handler execution: normal operation end */
        if (R_FDL_TRUE == contHdrFct)
        {
            (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);
            res = R_FDL_FCUFct_SwitchMode_Check();
            if (R_FDL_BUSY == res)
            {
                g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
                /* save status, will be returned on mode switch confirmation */
                g_fdl_str.opResult_enu = tmpStatus;
                tmpStatus = R_FDL_BUSY;
            }
        }

        /* Operation end, return status to user */
        if (R_FDL_BUSY != tmpStatus)
        {
            R_FDL_UFct_EndCurrentOperation (tmpStatus);
        }

        #ifdef PATCH_TO_SIMULATE_ERRORS
            tstData_str.simCntHdr_u32++;
        #endif

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


/*********************************************************************************************************************
 * Function name:  R_FDL_GetVersionString
 *********************************************************************************************************************/
/**
 * User interface function:
 * Returns the pointer to the library version string, stores in the library code section.
 *
 * @param[in,out]   -
 * @return          Pointer to the version string in Flash
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_CONST
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
const uint8_t *R_FDL_GetVersionString (void)                                                        /* PRQA S 1503 */
{
    static const uint8_t r_fdl_vStr[] = R_FDL_VERSION_STRING;

    return (&r_fdl_vStr[0]);
}
#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 */

/*********************************************************************************************************************
 * Function name:  R_FDL_SuspendRequest
 *********************************************************************************************************************/
/**
 * User interface function:
 * Sets the suspend request flag for the write or erase operation
 *
 * Note:
 * The operation is not immediately suspended. Suspend handling is controlled by R_FDL_Handler.
 * The suspend status is returned by the status variable of the request structure (Set in
 * R_FDL_Handler).
 *
 * Suspend request handling:
 * - Reject if
 *     - we have no active Flash operation
 *     - we have already a Flash operation suspended
 *     - we have a cancel request
 * - Reset resume request if set (then we have a not yet handled resume request)
 * - Set suspend request if we have an operation ongoing and we are not already in suspend
 *   processing
 *
 * @param[in,out]   -
 * @return          result of the suspend request
 *                  - R_FDL_OK           - Request accepted
 *                  - R_FDL_ERR_REJECTED - Request rejected
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_SuspendRequest (void)                                                          /* PRQA S 1503 */
{
    r_fdl_status_t ret;

    ret = R_FDL_ERR_REJECTED;

    /* Block on:
          - not initialized library
          - StandBy processing       */
    if (R_FDL_TRUE == R_FDL_UFct_StateNotStandBy() )
    {
    
        /* Set the request flag only if we are busy and not in suspend state or suspend processing */
        if ( (R_FDL_REQUEST_POINTER_UNDEFINED != g_fdl_str.reqInt_pstr)
            && (R_FDL_REQUEST_POINTER_UNDEFINED == g_fdl_str.reqSuspend_pstr)
            && (R_FDL_ISTAT_NORMALOP == g_fdl_str.iStat_enu)
             && (R_FDL_FALSE == g_fdl_str.cancelRequest_enu) )
        {
            ret = R_FDL_OK;
            g_fdl_str.spdSpdRequest_enu = R_FDL_TRUE;
        }
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_ResumeRequest
 *********************************************************************************************************************/
/**
 * User interface function:
 * Sets the resume request flag for the write or erase operation
 *
 * Note:
 * The operation is not immediately resumed. Resume handling is controlled by R_FDL_Handler.
 * The status is returned by the status variable of the request structure (Set in
 * R_FDL_Handler).
 *
 * Resume request handling:
 * - Reset suspend request if set (then we have a not yet handled suspend request)
 * - Set resume request if we have a suspended operation (reqSuspend_pstr defined)
 *   and we have not already a resume request
 * - Reject all other conditions
 *
 * @param[in,out]   -
 * @return          result of the resume request
 *                  - R_FDL_OK           - Request accepted
 *                  - R_FDL_ERR_REJECTED - Request rejected
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_ResumeRequest (void)                                                           /* PRQA S 1503 */
{
    r_fdl_status_t ret;

    ret = R_FDL_OK;

    /* Block on not initialized library and on StandBy processing */
    if (R_FDL_TRUE == R_FDL_UFct_StateNotStandBy() )
    {
    
        /* Set the request flag only if we are already suspended */
        if ( (R_FDL_ISTAT_SUSPENDED == g_fdl_str.iStat_enu)
             && (R_FDL_FALSE == g_fdl_str.cancelRequest_enu) )
        {
            g_fdl_str.spdResRequest_enu = R_FDL_TRUE;
        }
        else
        {
            ret = R_FDL_ERR_REJECTED;
        }
    }
    else
    {
        ret = R_FDL_ERR_REJECTED;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_Standby
 *********************************************************************************************************************/
/**
 * User interface function:
 * Prepares the HW for fast standby entry:
 * - on write end erase
 * - blank check is not suspendable, here the function is without effect
 *
 * This function can be called asynchronous (e.g. interrupt function) and so need to take
 * care of the current hardware status in order to restore it later on during wakeup
 *
 * Note:
 * the function need to be called continuously until the result is != R_FDL_BUSY
 *
 * @param[in,out]   -
 * @return          result of the standby operation
 *                  - R_FDL_OK           - Flash hardware is in standby or ready (no more Flash operation)
 *                  - R_FDL_BUSY         - Flash hardware is still busy. Repeat calling the function
 *                  - R_FDL_ERR_REJECTED - Rejected, as the library is not initialized
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_StandBy (void)                                                                 /* PRQA S 1503 */
{
    r_fdl_status_t ret;

    /* Allow execution in each initialized state except StandBy and Init */
    if ( ( (R_FDL_TRUE == R_FDL_UFct_StateNotStandBy() ) &&
           (R_FDL_ISTAT_INIT != g_fdl_str.iStat_enu) )
         || (R_FDL_ISTAT_STANDBY_PR == g_fdl_str.iStat_enu) )
    {

        /* On Standby processing start, backup the internal Status */
        if (R_FDL_ISTAT_STANDBY_PR != g_fdl_str.iStat_enu)
        {
            g_fdl_str.stByIStatBackUp_enu = g_fdl_str.iStat_enu;

            /* Set to standby processing. By that, other EEL operations are blocked */
            g_fdl_str.iStat_enu = R_FDL_ISTAT_STANDBY_PR;
        }

        /* only operate standby, if any Flash operation is ongoing */
        if (R_FDL_FALSE == R_FDL_FCUFct_ChkReady() )
        {
            /* hardware is still busy */
            ret = R_FDL_BUSY;

            /* only operate standby if the Flash operation is suspendable
               additionally check if we are  not already suspended or in suspend
               processing */
            if (R_FDL_TRUE == R_FDL_FCUFct_ChkSuspendable() )
            {
                R_FDL_FCUFct_Suspend();
            }
        }

        /* hardware is no longer busy, set library into standby */
        else
        {
            g_fdl_str.iStat_enu = R_FDL_ISTAT_STANDBY;
            ret = R_FDL_OK;
        }
    }
    else
    {
        /* library is not initialized */
        ret = R_FDL_ERR_REJECTED;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_WakeUp
 *********************************************************************************************************************/
/**
 * User interface function:
 * Wake up the hardware from standby
 *
 * @param[in,out]   -
 * @return          result of the initialization check
 *                  - R_FDL_OK           - Wakeup succeeded
 *                  - R_FDL_ERR_REJECTED - Rejected, as the library is not initialized
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_WakeUp (void)                                                                  /* PRQA S 1503 */
{
    r_fdl_status_t ret;

    /* We can only wakeup if we have been in standby before */
    if (R_FDL_ISTAT_STANDBY == g_fdl_str.iStat_enu)
    {
        ret = R_FDL_OK;

        g_fdl_str.iStat_enu = g_fdl_str.stByIStatBackUp_enu;

        /* check if we need to wakeup */
        if (R_FDL_TRUE == R_FDL_FCUFct_ResumeChkNeed() )
        {
            R_FDL_FCUFct_Resume();
        }
    }
    else
    {
        ret = R_FDL_ERR_REJECTED;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_CancelRequest
 *********************************************************************************************************************/
/**
 * User interface function:
 * Sets the cancel request flag for an ongoing operation (exception Prepare environment cmd)
 *
 * Note:
 * The operation is not immediately cancelled. Cancel handling is controlled by R_FDL_Handler.
 * The status is returned by the status variable of the request structure (Set in
 * R_FDL_Handler).
 *
 * Cancel request handling:
 * - Set cancel request if we do not have a standby operation
 *   and we have not already a cancel request
 * - Reject all other conditions
 *
 * @param[in,out]   -
 * @return          result of the cancel request
 *                  - R_FDL_OK           - Request accepted
 *                  - R_FDL_ERR_REJECTED - Request rejected
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
r_fdl_status_t R_FDL_CancelRequest (void)                                                           /* PRQA S 1503 */
{
    r_fdl_status_t ret;

    ret = R_FDL_OK;

    /* Block on not initialized library and on StandBy processing */
    if ( (R_FDL_TRUE ==  R_FDL_UFct_StateNotStandBy() ) &&
        (R_FDL_ISTAT_INIT != g_fdl_str.iStat_enu)      &&
         (R_FDL_FALSE == g_fdl_str.cancelRequest_enu) )
    {

        /* Library is in suspended state, no operation on-going */
        if ( (R_FDL_ISTAT_SUSPENDED == g_fdl_str.iStat_enu) &&
             (R_FDL_REQUEST_POINTER_UNDEFINED == g_fdl_str.reqInt_pstr) )
        {
            ret = R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_PE);
        }
        
        /* No operation ongoing */
        else if ( (R_FDL_ISTAT_NORMALOP == g_fdl_str.iStat_enu) &&
                  (R_FDL_REQUEST_POINTER_UNDEFINED == g_fdl_str.reqInt_pstr) )
        {
            ret = R_FDL_ERR_REJECTED;
        }
        else
        {
            /* Nothing to do, added to fix QAC warning message 2004 */
        }

        /* All conditions fulfilled for cancel */
        if (R_FDL_OK == ret)
        {
            g_fdl_str.cancelRequest_enu = R_FDL_TRUE;
        }
    }
    else
    {
        ret = R_FDL_ERR_REJECTED;
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_UFct_FlashOpStart
 *********************************************************************************************************************/
/**
 * Extract information required for a Flash operation and call the function which starts the operation.
 *
 * @param[in]  request_pstr - pointer to the operation request structure
 * @param[out] tmpStat_penu - Status return value. Is set to operation result. Possible values:
 *                              - R_FDL_BUSY
 *                              - R_FDL_ERR_PARAMETER
 *                              - R_FDL_ERR_PROTECTION
 *                              - R_FDL_OK (Read operation)
 *                              - R_FDL_ERR_ECC_SED (Read operation)
 *                              - R_FDL_ERR_ECC_DED (Read operation)
 * @param[in]  isCalledFromExecute_enu - flag to distinguish if the function is called from:
 *                                       - R_FDL_Execute: value R_FDL_TRUE
 *                                       - R_FDL_Handler: value R_FDL_FALSE
 *
 * @return     handler function flow may continue after this function or handler need to exit:
 *             - R_FDL_TRUE  - continue handler
 *             - R_FDL_FALSE - exit handler
 *             In case the function is called from R_FDL_Execute, the return value can be ignored.
 */
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_UFct_FlashOpStart (r_fdl_request_t * request_pstr, 
                                                   r_fdl_status_t  * tmpStat_penu, 
                                                   r_fdl_flag_t      isCalledFromExecute_enu)
{
    uint32_t           addFlash;
    uint32_t           cntTotal;
    uint32_t           addBuf;
    uint32_t           granularityCurOp;
    uint32_t           cntCurOp;
    uint32_t           addEndCurOp;
    r_fdl_command_t    cmd;
    r_fdl_accessType_t accType;
    r_fdl_flag_t       cont;
    uint8_t            fcuCmd;

    addFlash         = (request_pstr->idx_u32);
    cntTotal         = (request_pstr->cnt_u16);
    cmd              = (request_pstr->command_enu);
    accType          = (request_pstr->accessType_enu);
    addBuf           = (request_pstr->bufAddr_u32);
    /* granularityCurOp has the default value for all commands except WRITE_16B */
    granularityCurOp = R_WRITE_SIZE;
    cont             = R_FDL_TRUE;
    (*tmpStat_penu)  = R_FDL_BUSY;


    if (R_FDL_CMD_ERASE == cmd)
    {
        addFlash <<= R_BLOCK_SIZE_2N;
        cntTotal <<= R_BLOCK_SIZE_2N;

        /* granularityCurOp has the default value */
        cntCurOp = (1 << R_BLOCK_SIZE_2N);

        /* Set buffer address to a valid address */
        addBuf   = 0x00000001uL;

        fcuCmd   = R_FCU_CMD_ERASE;
    }
    else if (R_FDL_CMD_BLANKCHECK == cmd)
    {
        cntTotal *= R_WRITE_SIZE;
        cntCurOp = cntTotal;

        /* Limit count to max */
        R_FDL_IFct_CalcFOpUnitCnt_BC (&cntCurOp, addFlash);

        /* Set buffer address to a valid address */
        addBuf   = 0x00000001uL;

        fcuCmd   = R_FCU_CMD_BLANKCHECK;
    }

    /* write 4 bytes (1 words) at once */
    else if (R_FDL_CMD_WRITE == cmd)
    {
        cntCurOp = R_WRITE_SIZE;
        cntTotal *= R_WRITE_SIZE;

        fcuCmd   = R_FCU_CMD_WRITE;
    }
#if (defined ENABLE_CMD_WRITE16B)

    /* write 16 bytes (4 words) at once */
    else if (R_FDL_CMD_WRITE16B == cmd)
    {
        cntCurOp = R_WRITE_SIZE_16B;
        cntTotal *= R_WRITE_SIZE_16B;

        granularityCurOp = cntCurOp;

        fcuCmd   = R_FCU_CMD_WRITE;
    }
#endif

    /* Read command */
    else
    {
        cntTotal *= R_WRITE_SIZE;
        cntCurOp = cntTotal;

        /* for the read cmd there is not equivalent FCU cmd, it is initialized here to fix QAC error message */
        fcuCmd = 0x00;
    }

    addEndCurOp = (addFlash + cntCurOp) - 1uL;

    /* Check against invalid buffer address (R_FDL_CMD_WRITE, R_FDL_CMD_WRITE16B, R_FDL_CMD_READ)
       - Only if function is called by R_FDL_Execute */
    if (R_FDL_TRUE == isCalledFromExecute_enu)
    {
        if (R_FDL_NULL == addBuf)
        {
            (*tmpStat_penu) = R_FDL_ERR_PARAMETER;
        }
        else
        {
            (*tmpStat_penu) = R_FDL_IFct_ChkAccessBoundaries (addFlash, cntTotal, accType, granularityCurOp);
        }

        /* Reset flag; it is updated later on in case the switch mode to PE is performed correctly */
        cont = R_FDL_FALSE;
    }

    if (R_FDL_BUSY == (*tmpStat_penu) )
    {
        /* Read command is treated only in R_FDL_Execute,
           no need to check given parameter isCalledFromExecute_enu */
        if (R_FDL_CMD_READ == cmd)
        {
            /* user destination buffer must be 4 bytes aligned */
            if (0 == (addBuf & R_U32_ALIGNED) )
            {
                (*tmpStat_penu) = R_FDL_FCUFct_ReadOperation ( (volatile uint32_t *)(&addFlash),
                                                              addBuf,
                                                               (cntTotal / R_WRITE_SIZE) );
                request_pstr->idx_u32 = addFlash;   /* Store possible fail address to request structure */
            }
            else
            {
                (*tmpStat_penu) = R_FDL_ERR_PARAMETER;
            }
        }
        
        /* all commands except Read */
        else
        {
            /* Switch mode only required once */
            if (R_FDL_TRUE == isCalledFromExecute_enu)
            {
                (*tmpStat_penu) = R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_PE);
                if (R_FDL_OK == (*tmpStat_penu) )
                {
                    (*tmpStat_penu) = R_FDL_FCUFct_SwitchMode_Check();
                    if (R_FDL_OK == (*tmpStat_penu) )
                    {
                        cont = R_FDL_TRUE;
                    }
                    else
                    {
                        g_fdl_str.opStatus_enu = R_FDL_OP_EXECUTE_SYNC_ON;
                    }
                }
            }

            /* continue if mode switch passed (either immediately after the mode switch 
               start or if the function is later on called by the handler */
            if (R_FDL_TRUE == cont)
            {
                if ( (R_FDL_CMD_ERASE == cmd) || (R_FDL_CMD_BLANKCHECK == cmd) )
                {
                    (*tmpStat_penu) = R_FDL_FCUFct_StartBCEraseOperation (addFlash, addEndCurOp, fcuCmd, accType);
                }
                
                /* Write command */
                else
                {
                    (*tmpStat_penu) = R_FDL_FCUFct_StartWriteOperation (addBuf, addFlash, cntCurOp, accType);

                    g_fdl_str.mulOp_str.bufAdd_u32 = addBuf + cntCurOp;          /* Next Source Address */
                }

                /* command can be executed */
                if (R_FDL_BUSY == (*tmpStat_penu) )
                {
                    g_fdl_str.opStatus_enu = R_FDL_OP_BUSY;

                    /* Prepare for multi-word write */
                    g_fdl_str.mulOp_str.flashAdd_u32     = addEndCurOp + 1uL;
                    g_fdl_str.mulOp_str.flashAddEnd_u32  = (addFlash + cntTotal) - 1uL;   /* Source End Address */
                    g_fdl_str.mulOp_str.accessType_enu   = accType;
                }
                
                /* error --> switch back to user mode */
                else
                {
                    (void)R_FDL_FCUFct_SwitchMode_Start (R_FCU_MODE_USER);

                    /* save operation status and return it after switch mode confirmation */
                    g_fdl_str.opResult_enu = (*tmpStat_penu);
                    g_fdl_str.opStatus_enu = R_FDL_OP_END_SYNC_OFF;
                    cont = R_FDL_FALSE;
                    (*tmpStat_penu) = R_FDL_BUSY;
                }
            }
        }
    }

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


/*********************************************************************************************************************
 * Function name:  R_FDL_UFct_EndCurrentOperation
 *********************************************************************************************************************/
/**
 * At the end of a Flash operation, this function updates the request structure elements and
 * resets the library internal pointer to this structure.
 *
 * @param[in]  ret_enu - status return value
 * @return     ---
 *
 * The function modifies the global structure g_fdl_str.reqInt_pstr to set the operation
 * status
*/
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC void R_FDL_UFct_EndCurrentOperation (r_fdl_status_t ret_enu)
{
    (g_fdl_str.reqInt_pstr)->status_enu = ret_enu;

    /* For Blank Check, return fail address too */
    if ( (R_FDL_CMD_BLANKCHECK == (g_fdl_str.reqInt_pstr)->command_enu) && (R_FDL_ERR_BLANKCHECK == ret_enu) )
    {
        (g_fdl_str.reqInt_pstr)->idx_u32 = g_fdl_str.opFailAddr_u32;
    }
    (g_fdl_str.reqInt_pstr)->accessType_enu = R_FDL_ACCESS_NONE;
    (g_fdl_str.reqInt_pstr)                 = R_FDL_REQUEST_POINTER_UNDEFINED;
     g_fdl_str.opStatus_enu                 = R_FDL_OP_IDLE;
    
    /* In case of internal error enforce re-initialization. No more command execution will be allowed */
    if (R_FDL_ERR_INTERNAL == ret_enu)
    {
        g_fdl_str.iStat_enu = R_FDL_ISTAT_NOINIT;
    }
} /* R_FDL_UFct_EndCurrentOperation */
#define R_FDL_STOP_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */


/*********************************************************************************************************************
 * Function name:  R_FDL_UFct_StateNotStandBy
 *********************************************************************************************************************/
/**
 * Check if the library is initialized and not in StandBy or StandBy processing
 *
 * @param[in,out]   -
 * @return          - R_FDL_TRUE   Library is initialized
 *                  - R_FDL_FALSE  Library is not initialized
*/
/*********************************************************************************************************************/
#define R_FDL_START_SEC_PUBLIC_CODE
#include "r_fdl_mem_map.h"                                                                          /* PRQA S 5087 */
R_FDL_STATIC r_fdl_flag_t R_FDL_UFct_StateNotStandBy (void)
{
    r_fdl_flag_t ret;

    /* Any states except StandBy related states or uninitialized state */
    if ( (R_FDL_ISTAT_NORMALOP == g_fdl_str.iStat_enu)
        || (R_FDL_ISTAT_INIT == g_fdl_str.iStat_enu)
         || ( (R_FDL_ISTAT_SUSPEND_PR == g_fdl_str.iStat_enu)
              || (R_FDL_ISTAT_SUSPENDED == g_fdl_str.iStat_enu) ) )
    {
        ret = R_FDL_TRUE;
    }
    /* StandBy related state or uninitialized state */
    else
    {
        ret = R_FDL_FALSE;
    }


    return (ret);
} /* R_FDL_UFct_StateNotStandBy */

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

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