apm32f10x_fmc.c 18.5 KB
Newer Older
A
abbcc 已提交
1
/*!
2
 * @file        apm32f10x_fmc.c
A
abbcc 已提交
3
 *
4
 * @brief       This file provides all the FMC firmware functions
A
abbcc 已提交
5
 *
6
 * @version     V1.0.4
A
abbcc 已提交
7
 *
8
 * @date        2022-12-01
A
abbcc 已提交
9
 *
10 11 12 13 14 15 16 17
 * @attention
 *
 *  Copyright (C) 2020-2022 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
 *
 *  The program is only for reference, which is distributed in the hope
18
 *  that it will be useful and instructional for customers to develop
19 20 21 22 23
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
A
abbcc 已提交
24 25
 */

26
/* Includes */
A
abbcc 已提交
27 28 29
#include "apm32f10x_fmc.h"
#include "apm32f10x_rcm.h"

30
/** @addtogroup APM32F10x_StdPeriphDriver
A
abbcc 已提交
31 32 33 34 35 36 37
  @{
*/

/** @addtogroup FMC_Driver FMC Driver
  @{
*/

38
/** @defgroup FMC_Functions Functions
A
abbcc 已提交
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
  @{
*/

/*!
 * @brief     Configs the code latency value.
 *
 * @param     latency: the FMC Latency value.
 *
 * @retval    None
 */
void FMC_ConfigLatency(FMC_LATENCY_T latency)
{
    FMC->CTRL1_B.WS = latency;
}

/*!
 * @brief     Enables the Half cycle flash access.
 *
 * @param     None
 *
 * @retval    None
 */
void FMC_EnableHalfCycleAccess(void)
{
    FMC->CTRL1_B.HCAEN = BIT_SET;
}

/*!
 * @brief     Disable the Half cycle flash access.
 *
 * @param     None
 *
 * @retval    None
 */
void FMC_DisableHalfCycleAccess(void)
{
    FMC->CTRL1_B.HCAEN = BIT_RESET;
}

/*!
 * @brief     Enables the Prefetch Buffer.
 *
 * @param     None
 *
 * @retval    None
 */
void FMC_EnablePrefetchBuffer(void)
{
    FMC->CTRL1_B.PBEN = ENABLE;
}

/*!
 * @brief     Disables the Prefetch Buffer.
 *
 * @param     None
 *
 * @retval    None
 */
void FMC_DisablePrefetchBuffer(void)
{
    FMC->CTRL1_B.PBEN = DISABLE;
}

/*!
 * @brief     Unlocks the FMC Program Erase Controller
 *
 * @param     None
 *
 * @retval    None
 */
void FMC_Unlock(void)
{
    FMC->KEY = 0x45670123;
    FMC->KEY = 0xCDEF89AB;
}

/*!
 * @brief     Locks the FMC Program Erase Controller.
 *
 * @param     None
 *
 * @retval    None
 */
void FMC_Lock(void)
{
    FMC->CTRL2_B.LOCK = BIT_SET;
}

/*!
 * @brief     Erases a specified FMC page.
 *
 * @param     pageAddr: The page address to be erased.
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_BUSY
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_ErasePage(uint32_t pageAddr)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    status = FMC_WaitForLastOperation(0x000B0000);
A
Aligagago 已提交
144
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
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
    {
        FMC->CTRL2_B.PAGEERA = BIT_SET;
        FMC->ADDR = pageAddr;
        FMC->CTRL2_B.STA = BIT_SET;

        status = FMC_WaitForLastOperation(0x000B0000);
        FMC->CTRL2_B.PAGEERA = BIT_RESET;
    }
    return status;
}

/*!
 * @brief     Erases all FMC pages.
 *
 * @param     None
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_EraseAllPage(void)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    status = FMC_WaitForLastOperation(0x000B0000);
A
Aligagago 已提交
172
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
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
    {
        FMC->CTRL2_B.MASSERA = BIT_SET;
        FMC->CTRL2_B.STA = BIT_SET;

        status = FMC_WaitForLastOperation(0x000B0000);
        FMC->CTRL2_B.MASSERA = BIT_RESET;
    }
    return status;
}

/*!
 * @brief     Erases the FMC option bytes.
 *
 * @param     None
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_EraseOptionBytes(void)
{
    uint16_t rdtemp = 0x00A5;
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

A
Aligagago 已提交
199
    if (FMC_GetReadProtectionStatus() != RESET)
A
abbcc 已提交
200 201 202 203
    {
        rdtemp = 0x00;
    }
    status = FMC_WaitForLastOperation(0x000B0000);
A
Aligagago 已提交
204
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
205 206 207 208 209 210 211 212 213
    {
        FMC->OBKEY = 0x45670123;
        FMC->OBKEY = 0xCDEF89AB;

        FMC->CTRL2_B.OBE = BIT_SET;
        FMC->CTRL2_B.STA = BIT_SET;

        status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
214
        if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
215 216 217 218 219
        {
            FMC->CTRL2_B.OBE = BIT_RESET;
            FMC->CTRL2_B.OBP = BIT_SET;
            OB->RDP = rdtemp;
            status = FMC_WaitForLastOperation(0x000B0000);
A
Aligagago 已提交
220
            if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
221 222 223 224
            {
                FMC->CTRL2_B.OBP = BIT_RESET;
            }
        }
A
Aligagago 已提交
225
        else if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
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
        {
            FMC->CTRL2_B.OBP = BIT_RESET;
        }
    }
    return status;
}

/*!
 * @brief     Programs a word at a specified address.
 *
 * @param     address:the address to be programmed.
 *
 * @param     data: the data to be programmed.
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_ProgramWord(uint32_t address, uint32_t data)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;
    __IOM uint32_t temp = 0;

A
Aligagago 已提交
251 252 253
#ifdef APM32F10X_HD
    __set_PRIMASK(1);
#endif
A
abbcc 已提交
254 255 256

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
257
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
258 259 260
    {
        FMC->CTRL2_B.PG = BIT_SET;

261
        *(__IOM uint16_t*)address = data;
A
abbcc 已提交
262 263 264

        status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
265
        if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
266 267 268
        {
            temp = address + 2;

269
            *(__IOM uint16_t*) temp = data >> 16;
A
abbcc 已提交
270 271 272 273 274 275 276 277 278 279

            status = FMC_WaitForLastOperation(0x000B0000);
            FMC->CTRL2_B.PG = BIT_RESET;
        }
        else
        {
            FMC->CTRL2_B.PG = BIT_RESET;
        }
    }

A
Aligagago 已提交
280 281 282
#ifdef APM32F10X_HD
    __set_PRIMASK(0);
#endif
A
abbcc 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

    return status;
}

/*!
 * @brief     Programs a half word at a specified address.
 *
 * @param     address:the address to be programmed.
 *
 * @param     data: the data to be programmed.
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_ProgramHalfWord(uint32_t address, uint16_t data)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

A
Aligagago 已提交
304 305 306
#ifdef APM32F10X_HD
    __set_PRIMASK(1);
#endif
A
abbcc 已提交
307 308 309

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
310
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
311 312
    {
        FMC->CTRL2_B.PG = BIT_SET;
313
        *(__IOM uint16_t*)address = data;
A
abbcc 已提交
314 315 316 317
        status = FMC_WaitForLastOperation(0x000B0000);
        FMC->CTRL2_B.PG = BIT_RESET;
    }

A
Aligagago 已提交
318 319 320
#ifdef APM32F10X_HD
    __set_PRIMASK(0);
#endif
A
abbcc 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

    return status;
}

/*!
 * @brief     Programs a half word at a specified Option Byte Data address.
 *
 * @param     address:the address to be programmed.
 *
 * @param     data: the data to be programmed.
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_ProgramOptionByteData(uint32_t address, uint8_t data)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
344
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
345 346 347 348 349
    {
        FMC->OBKEY = 0x45670123;
        FMC->OBKEY = 0xCDEF89AB;

        FMC->CTRL2_B.OBP = BIT_SET;
350
        *(__IOM uint16_t*)address = data;
A
abbcc 已提交
351
        status = FMC_WaitForLastOperation(0x000B0000);
A
Aligagago 已提交
352
        if (status == FMC_STATUS_TIMEOUT)
A
abbcc 已提交
353 354 355 356 357 358 359 360 361 362 363 364
        {
            FMC->CTRL2_B.OBP = BIT_RESET;
        }
    }
    return status;
}

/*!
 * @brief     Write protects the desired pages
 *
 * @param     page:the address of the pages to be write protection
 *                This parameter can be any combination of the following values:
365
 *                 for APM32F10X_LD :
A
abbcc 已提交
366
 *                    @arg FLASH_WRP_PAGE_0_3 to FLASH_WRP_PAGE_28_31
367
 *                 for APM32F10X_MD :
A
abbcc 已提交
368
 *                    @arg FLASH_WRP_PAGE_0_3 to FLASH_WRP_PAGE_124_127
369
 *                 for APM32F10X_HD :
A
abbcc 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
 *                    @arg FLASH_WRP_PAGE_0_1 to FLASH_WRP_PAGE_60_61 or FLASH_WRP_PAGE_62_127
 *                 @arg FMC_WRP_PAGE_ALL
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_EnableWriteProtection(uint32_t page)
{
    uint16_t WPP0_Data = 0xFFFF, WPP1_Data = 0xFFFF, WPP2_Data = 0xFFFF, WPP3_Data = 0xFFFF;
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    page = ~page;
    WPP0_Data = (page & 0x000000FF);
    WPP1_Data = (page & 0x0000FF00) >> 8;
    WPP2_Data = (page & 0x00FF0000) >> 16;
    WPP3_Data = (page & 0xFF000000) >> 24;

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
392
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
393 394 395 396 397
    {
        FMC->OBKEY = 0x45670123;
        FMC->OBKEY = 0xCDEF89AB;
        FMC->CTRL2_B.OBP = BIT_SET;

A
Aligagago 已提交
398
        if (WPP0_Data != 0xFF)
A
abbcc 已提交
399 400 401 402
        {
            OB->WRP0 = WPP0_Data;
            status = FMC_WaitForLastOperation(0x000B0000);
        }
A
Aligagago 已提交
403
        if ((status == FMC_STATUS_COMPLETE) && (WPP1_Data != 0xFF))
A
abbcc 已提交
404 405 406 407
        {
            OB->WRP1 = WPP1_Data;
            status = FMC_WaitForLastOperation(0x000B0000);
        }
A
Aligagago 已提交
408
        if ((status == FMC_STATUS_COMPLETE) && (WPP2_Data != 0xFF))
A
abbcc 已提交
409 410 411 412
        {
            OB->WRP2 = WPP2_Data;
            status = FMC_WaitForLastOperation(0x000B0000);
        }
A
Aligagago 已提交
413
        if ((status == FMC_STATUS_COMPLETE) && (WPP3_Data != 0xFF))
A
abbcc 已提交
414 415 416 417 418
        {
            OB->WRP3 = WPP3_Data;
            status = FMC_WaitForLastOperation(0x000B0000);
        }

A
Aligagago 已提交
419
        if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
        {
            FMC->CTRL2_B.OBP = BIT_RESET;
        }
    }
    return status;
}

/*!
 * @brief     Enables the read out protection.
 *
 * @param     None
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_EnableReadOutProtection(void)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
444
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
445 446 447 448 449 450 451 452 453
    {
        FMC->OBKEY = 0x45670123;
        FMC->OBKEY = 0xCDEF89AB;

        FMC->CTRL2_B.OBE = BIT_SET;
        FMC->CTRL2_B.STA = BIT_SET;

        status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
454
        if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
455 456 457
        {
            FMC->CTRL2_B.OBE = BIT_RESET;
            FMC->CTRL2_B.OBP = BIT_SET;
458
            OB->RDP = 0x00;
A
abbcc 已提交
459 460 461

            status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
462
            if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
463 464 465 466
            {
                FMC->CTRL2_B.OBP = BIT_RESET;
            }
        }
A
Aligagago 已提交
467
        else if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
        {
            FMC->CTRL2_B.OBE = BIT_RESET;
        }
    }
    return status;
}

/*!
 * @brief     Disables the read out protection.
 *
 * @param     None
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_DisableReadOutProtection(void)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
492
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
493 494 495 496 497 498 499 500
    {
        FMC->OBKEY = 0x45670123;
        FMC->OBKEY = 0xCDEF89AB;
        FMC->CTRL2_B.OBE = BIT_SET;
        FMC->CTRL2_B.STA = BIT_SET;

        status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
501
        if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
502 503 504
        {
            FMC->CTRL2_B.OBE = BIT_RESET;
            FMC->CTRL2_B.OBP = BIT_SET;
505
            OB->RDP = 0xA5;
A
abbcc 已提交
506 507 508

            status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
509
            if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
510 511 512 513
            {
                FMC->CTRL2_B.OBP = BIT_RESET;
            }
        }
A
Aligagago 已提交
514
        else if (status != FMC_STATUS_TIMEOUT)
A
abbcc 已提交
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
        {
            FMC->CTRL2_B.OBE = BIT_RESET;
        }
    }
    return status;
}

/*!
 * @brief     Programs the FMC User Option Byte.
 *
 * @param     userConfig: Point to a FMC_UserConfig_T structure.
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
533
FMC_STATUS_T FMC_ConfigUserOptionByte(FMC_UserConfig_T* userConfig)
A
abbcc 已提交
534 535 536 537 538 539 540 541
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    FMC->OBKEY = 0x45670123;
    FMC->OBKEY = 0xCDEF89AB;

    status = FMC_WaitForLastOperation(0x000B0000);

A
Aligagago 已提交
542
    if (status == FMC_STATUS_COMPLETE)
A
abbcc 已提交
543 544
    {
        FMC->CTRL2_B.OBP = BIT_SET;
545 546 547
        OB->USER = (uint32_t)userConfig->iwdtSet | \
                   (uint32_t)userConfig->stopSet | \
                   (uint32_t)userConfig->stdbySet | 0xF8;
A
abbcc 已提交
548
        status = FMC_WaitForLastOperation(0x000B0000);
A
Aligagago 已提交
549
        if (status == FMC_STATUS_TIMEOUT)
A
abbcc 已提交
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
        {
            FMC->CTRL2_B.OBP = BIT_RESET;
        }
    }
    return status;
}

/*!
 * @brief     Read the FMC User Option Bytes values.
 *
 * @param     None
 *
 * @retval    Returns User Option Bytes values
 */
uint32_t FMC_ReadUserOptionByte(void)
{
    return (FMC->OBCS_B.UOB >> 2);
}

/*!
 * @brief     Read the FMC Write Protection Option Bytes Register value.
 *
 * @param     None
 *
 * @retval    Returns the value of Option Bytes Write Protection Register.
 */
uint32_t FMC_ReadOptionByteWriteProtection(void)
{
    return FMC->WRTPROT;
}

/*!
 * @brief     Get the FMC Read Out Protection Status is set or not.
 *
 * @param     None
 *
 * @retval    status : set or reset.
 */
uint8_t FMC_GetReadProtectionStatus(void)
{
    uint8_t flagstatus = RESET;

A
Aligagago 已提交
592
    if (FMC->OBCS_B.READPROT != RESET)
A
abbcc 已提交
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
    {
        flagstatus = SET;
    }
    else
    {
        flagstatus = RESET;
    }
    return flagstatus;
}

/*!
 * @brief     FMC Prefetch Buffer status is set or not.
 *
 * @param     None
 *
 * @retval    status : set or reset.
 */
uint8_t FMC_ReadPrefetchBufferStatus(void)
{
    return FMC->CTRL1_B.PBSF;
}

/*!
 * @brief     Enables the specified FMC interrupts.
 *
 * @param     interrupt: Select the FMC interrupt sources
 *                       This parameter can be one of the following values:
 *                       @arg FMC_INT_ERR :  Error Interrupt
 *                       @arg FMC_INT_OC  :  Operation Complete Interrupt
 *
 * @retval    None
 */
void FMC_EnableInterrupt(FMC_INT_T interrupt)
{
A
Aligagago 已提交
627
    if (interrupt == FMC_INT_ERR)
A
abbcc 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    {
        FMC->CTRL2_B.ERRIE = ENABLE;
    }
    else
    {
        FMC->CTRL2_B.OCIE = ENABLE;
    }
}

/*!
 * @brief     Disable the specified FMC interrupts.
 *
 * @param     interrupt: Select the FMC interrupt sources
 *                       This parameter can be one of the following values:
 *                       @arg FMC_INT_ERR :  Error Interrupt
 *                       @arg FMC_INT_OC  :  Operation Complete Interrupt
 *
 * @retval    None
 */
void FMC_DisableInterrupt(FMC_INT_T interrupt)
{
A
Aligagago 已提交
649
    if (interrupt == FMC_INT_ERR)
A
abbcc 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    {
        FMC->CTRL2_B.ERRIE = DISABLE;
    }
    else
    {
        FMC->CTRL2_B.OCIE = DISABLE;
    }
}

/*!
 * @brief     Read FMC flag is set or not
 *
 * @param     flag: status flag of FMC
 *                  This parameter can be one of the following values:
 *                  @arg FMC_FLAG_BUSY : FMC Busy flag
 *                  @arg FMC_FLAG_OC   : FMC Operation Complete flag
 *                  @arg FMC_FLAG_PE   : FMC Program error flag
 *                  @arg FMC_FLAG_WPE  : FMC Write protected error flag
 *                  @arg FMC_FLAG_OBE  : FMC Option Byte error flag
 *
 * @retval    flag status : set or reset
 */
uint8_t FMC_ReadStatusFlag(FMC_FLAG_T flag)
{
A
Aligagago 已提交
674
    if (flag == FMC_FLAG_OBE)
A
abbcc 已提交
675 676 677
    {
        return FMC->OBCS_B.OBE;
    }
A
Aligagago 已提交
678
    else if ((FMC->STS & flag) != RESET)
A
abbcc 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    {
        return SET;
    }
    return RESET;
}

/*!
 * @brief     Clears the FMC's flag.
 *
 * @param     flag: status flag of FMC
 *                  This parameter can be any combination of the following values:
 *                  @arg FMC_FLAG_OC   : FMC Operation Complete flag
 *                  @arg FMC_FLAG_PE   : FMC Program error flag
 *                  @arg FMC_FLAG_WPE  : FMC Write protected error flag
 *
 * @retval    None
 *
 */
697
void FMC_ClearStatusFlag(uint32_t flag)
A
abbcc 已提交
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
{
    FMC->STS = flag;
}

/*!
 * @brief     Read the FMC Status.
 *
 * @param     None
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_BUSY
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 */
FMC_STATUS_T  FMC_ReadStatus(void)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

A
Aligagago 已提交
717
    if (FMC->STS_B.BUSYF == BIT_SET)
A
abbcc 已提交
718 719 720
    {
        status = FMC_STATUS_BUSY;
    }
A
Aligagago 已提交
721
    else if (FMC->STS_B.PEF == BIT_SET)
A
abbcc 已提交
722 723 724
    {
        status = FMC_STATUS_ERROR_PG;
    }
A
Aligagago 已提交
725
    else if (FMC->STS_B.WPEF == BIT_SET)
A
abbcc 已提交
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
    {
        status = FMC_STATUS_ERROR_WRP;
    }
    else
    {
        status = FMC_STATUS_COMPLETE;
    }
    return status;
}

/*!
 * @brief     Waits for a Flash operation to complete or a TIMEOUT to occur.
 *
 * @param     timeOut:FMC programming timeout value.
 *
 * @retval    Returns the flash state.It can be one of value:
 *                 @arg FMC_STATUS_ERROR_PG
 *                 @arg FMC_STATUS_ERROR_WRP
 *                 @arg FMC_STATUS_COMPLETE
 *                 @arg FMC_STATUS_TIMEOUT
 */
FMC_STATUS_T FMC_WaitForLastOperation(uint32_t timeOut)
{
    FMC_STATUS_T status = FMC_STATUS_COMPLETE;

    /** Check for the Flash Status */
    status = FMC_ReadStatus();

    /** Wait for a Flash operation to complete or a TIMEOUT to occur */
A
Aligagago 已提交
755
    while ((status == FMC_STATUS_BUSY) && (timeOut != 0))
A
abbcc 已提交
756 757 758 759
    {
        status = FMC_ReadStatus();
        timeOut--;
    }
A
Aligagago 已提交
760
    if (timeOut == 0x00)
A
abbcc 已提交
761 762 763 764 765 766
    {
        status = FMC_STATUS_TIMEOUT;
    }
    return status;
}

767
/**@} end of group FMC_Functions*/
A
abbcc 已提交
768
/**@} end of group FMC_Driver*/
769
/**@} end of group APM32F10x_StdPeriphDriver*/