📄 stm32f10x_flash.lst
字号:
423 FLASH->OPTKEYR = FLASH_KEY1;
424 FLASH->OPTKEYR = FLASH_KEY2;
425
426 /* Enables the Option Bytes Programming operation */
427 FLASH->CR |= CR_OPTPG_Set;
428 *(vu16*)Address = Data;
429
430 /* Wait for last operation to be completed */
431 status = FLASH_WaitForLastOperation(ProgramTimeout);
432
433 if(status != FLASH_BUSY)
434 {
435 /* if the program operation is completed, disable the OPTPG Bit */
436 FLASH->CR &= CR_OPTPG_Reset;
437 }
438 }
439 /* Return the Option Byte Data Program Status */
440 return status;
441 }
442
443 /*******************************************************************************
444 * Function Name : FLASH_EnableWriteProtection
445 * Description : Write protects the desired pages
446 * Input : - FLASH_Pages: specifies the address of the pages to be
447 * write protected. This parameter can be:
448 * - For STM32F10Xxx Medium-density devices (FLASH page size equal to 1 KB)
449 * - A value between FLASH_WRProt_Pages0to3 and
450 * FLASH_WRProt_Pages124to127
451 * - For STM32F10Xxx High-density devices (FLASH page size equal to 2 KB)
452 * - A value between FLASH_WRProt_Pages0to1 and
453 * FLASH_WRProt_Pages60to61 or FLASH_WRProt_Pages62to255
454 * - FLASH_WRProt_AllPages
455 * Output : None
456 * Return : FLASH Status: The returned value can be: FLASH_BUSY,
457 * FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
458 * FLASH_TIMEOUT.
459 *******************************************************************************/
460 FLASH_Status FLASH_EnableWriteProtection(u32 FLASH_Pages)
461 {
462 u16 WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
463
464 FLASH_Status status = FLASH_COMPLETE;
465
466 /* Check the parameters */
467 assert_param(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
468
469 FLASH_Pages = (u32)(~FLASH_Pages);
470 WRP0_Data = (vu16)(FLASH_Pages & WRP0_Mask);
471 WRP1_Data = (vu16)((FLASH_Pages & WRP1_Mask) >> 8);
472 WRP2_Data = (vu16)((FLASH_Pages & WRP2_Mask) >> 16);
473 WRP3_Data = (vu16)((FLASH_Pages & WRP3_Mask) >> 24);
474
475 /* Wait for last operation to be completed */
476 status = FLASH_WaitForLastOperation(ProgramTimeout);
477
478 if(status == FLASH_COMPLETE)
479 {
480 /* Authorizes the small information block programming */
481 FLASH->OPTKEYR = FLASH_KEY1;
482 FLASH->OPTKEYR = FLASH_KEY2;
483 FLASH->CR |= CR_OPTPG_Set;
484
485 if(WRP0_Data != 0xFF)
486 {
487 OB->WRP0 = WRP0_Data;
488
489 /* Wait for last operation to be completed */
490 status = FLASH_WaitForLastOperation(ProgramTimeout);
491 }
492 if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
493 {
494 OB->WRP1 = WRP1_Data;
495
496 /* Wait for last operation to be completed */
497 status = FLASH_WaitForLastOperation(ProgramTimeout);
498 }
499
500 if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
501 {
502 OB->WRP2 = WRP2_Data;
503
504 /* Wait for last operation to be completed */
505 status = FLASH_WaitForLastOperation(ProgramTimeout);
506 }
507
508 if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
509 {
510 OB->WRP3 = WRP3_Data;
511
512 /* Wait for last operation to be completed */
513 status = FLASH_WaitForLastOperation(ProgramTimeout);
514 }
515
516 if(status != FLASH_BUSY)
517 {
518 /* if the program operation is completed, disable the OPTPG Bit */
519 FLASH->CR &= CR_OPTPG_Reset;
520 }
521 }
522 /* Return the write protection operation Status */
523 return status;
524 }
525
526 /*******************************************************************************
527 * Function Name : FLASH_ReadOutProtection
528 * Description : Enables or disables the read out protection.
529 * If the user has already programmed the other option bytes before
530 * calling this function, he must re-program them since this
531 * function erases all option bytes.
532 * Input : - Newstate: new state of the ReadOut Protection.
533 * This parameter can be: ENABLE or DISABLE.
534 * Output : None
535 * Return : FLASH Status: The returned value can be: FLASH_BUSY,
536 * FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
537 * FLASH_TIMEOUT.
538 *******************************************************************************/
539 FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
540 {
541 FLASH_Status status = FLASH_COMPLETE;
542
543 /* Check the parameters */
544 assert_param(IS_FUNCTIONAL_STATE(NewState));
545
546 status = FLASH_WaitForLastOperation(EraseTimeout);
547
548 if(status == FLASH_COMPLETE)
549 {
550 /* Authorizes the small information block programming */
551 FLASH->OPTKEYR = FLASH_KEY1;
552 FLASH->OPTKEYR = FLASH_KEY2;
553
554 FLASH->CR |= CR_OPTER_Set;
555 FLASH->CR |= CR_STRT_Set;
556
557 /* Wait for last operation to be completed */
558 status = FLASH_WaitForLastOperation(EraseTimeout);
559
560 if(status == FLASH_COMPLETE)
561 {
562 /* if the erase operation is completed, disable the OPTER Bit */
563 FLASH->CR &= CR_OPTER_Reset;
564
565 /* Enable the Option Bytes Programming operation */
566 FLASH->CR |= CR_OPTPG_Set;
567
568 if(NewState != DISABLE)
569 {
570 OB->RDP = 0x00;
571 }
572 else
573 {
574 OB->RDP = RDP_Key;
575 }
576
577 /* Wait for last operation to be completed */
578 status = FLASH_WaitForLastOperation(EraseTimeout);
579
580 if(status != FLASH_BUSY)
581 {
582 /* if the program operation is completed, disable the OPTPG Bit */
583 FLASH->CR &= CR_OPTPG_Reset;
584 }
585 }
586 else
587 {
588 if(status != FLASH_BUSY)
589 {
590 /* Disable the OPTER Bit */
591 FLASH->CR &= CR_OPTER_Reset;
592 }
593 }
594 }
595 /* Return the protection operation Status */
596 return status;
597 }
598
599 /*******************************************************************************
600 * Function Name : FLASH_UserOptionByteConfig
601 * Description : Programs the FLASH User Option Byte: IWDG_SW / RST_STOP /
602 * RST_STDBY.
603 * Input : - OB_IWDG: Selects the IWDG mode
604 * This parameter can be one of the following values:
605 * - OB_IWDG_SW: Software IWDG selected
606 * - OB_IWDG_HW: Hardware IWDG selected
607 * - OB_STOP: Reset event when entering STOP mode.
608 * This parameter can be one of the following values:
609 * - OB_STOP_NoRST: No reset generated when entering in STOP
610 * - OB_STOP_RST: Reset generated when entering in STOP
611 * - OB_STDBY: Reset event when entering Standby mode.
612 * This parameter can be one of the following values:
613 * - OB_STDBY_NoRST: No reset generated when entering in STANDBY
614 * - OB_STDBY_RST: Reset generated when entering in STANDBY
615 * Output : None
616 * Return : FLASH Status: The returned value can be: FLASH_BUSY,
617 * FLASH_ERROR_PG, FLASH_ERROR_WRP, FLASH_COMPLETE or
618 * FLASH_TIMEOUT.
619 *******************************************************************************/
620 FLASH_Status FLASH_UserOptionByteConfig(u16 OB_IWDG, u16 OB_STOP, u16 OB_STDBY)
621 {
622 FLASH_Status status = FLASH_COMPLETE;
623
624 /* Check the parameters */
625 assert_param(IS_OB_IWDG_SOURCE(OB_IWDG));
626 assert_param(IS_OB_STOP_SOURCE(OB_STOP));
627 assert_param(IS_OB_STDBY_SOURCE(OB_STDBY));
628
629 /* Authorize the small information block programming */
630 FLASH->OPTKEYR = FLASH_KEY1;
631 FLASH->OPTKEYR = FLASH_KEY2;
632
633 /* Wait for last operation to be completed */
634 status = FLASH_WaitForLastOperation(ProgramTimeout);
635
636 if(status == FLASH_COMPLETE)
637 {
638 /* Enable the Option Bytes Programming operation */
639 FLASH->CR |= CR_OPTPG_Set;
640
641 OB->USER = ( OB_IWDG | OB_STOP |OB_STDBY) | (u16)0xF8;
642
643 /* Wait for last operation to be completed */
644 status = FLASH_WaitForLastOperation(ProgramTimeout);
645
646 if(status != FLASH_BUSY)
647 {
648 /* if the program operation is completed, disable the OPTPG Bit */
649 FLASH->CR &= CR_OPTPG_Reset;
650 }
651 }
652 /* Return the Option Byte program Status */
653 return status;
654 }
655
656 /*******************************************************************************
657 * Function Name : FLASH_GetUserOptionByte
658 * Description : Returns the FLASH User Option Bytes values.
659 * Input : None
660 * Output : None
661 * Return : The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
662 * and RST_STDBY(Bit2).
663 *******************************************************************************/
664 u32 FLASH_GetUserOptionByte(void)
665 {
666 /* Return the User Option Byte */
667 return (u32)(FLASH->OBR >> 2);
668 }
669
670 /*******************************************************************************
671 * Function Name : FLASH_GetWriteProtectionOptionByte
672 * Description : Returns the FLASH Write Protection Option Bytes Register value.
673 * Input : None
674 * Output : None
675 * Return : The FLASH Write Protection Option Bytes Register value
676 *******************************************************************************/
677 u32 FLASH_GetWriteProtectionOptionByte(void)
678 {
679 /* Return the Falsh write protection Register value */
680 return (u32)(FLASH->WRPR);
681 }
682
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -