stm32f10x_flash.c
Upload User: goodway329
Upload Date: 2016-01-03
Package Size: 312k
Code Size: 31k
Development Platform:

C/C++

  1. /******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
  2. * File Name          : stm32f10x_flash.c
  3. * Author             : MCD Application Team
  4. * Date First Issued  : 05/21/2007
  5. * Description        : This file provides all the FLASH firmware functions.
  6. ********************************************************************************
  7. * History:
  8. * 05/21/2007: V0.3
  9. ********************************************************************************
  10. * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  11. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
  12. * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
  13. * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
  14. * CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
  15. * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  16. *******************************************************************************/
  17. /* Includes ------------------------------------------------------------------*/
  18. #include "stm32f10x_flash.h"
  19. /* Private typedef -----------------------------------------------------------*/
  20. /* Private define ------------------------------------------------------------*/
  21. /* Flash Access Control Register bits */
  22. #define ACR_LATENCY_Mask         ((u32)0x00000038)
  23. #define ACR_HLFCYA_Mask          ((u32)0xFFFFFFF7)
  24. #define ACR_PRFTBE_Mask          ((u32)0xFFFFFFEF)
  25. #ifdef _FLASH_PROG
  26. /* Flash Access Control Register bits */
  27. #define ACR_PRFTBS_Mask          ((u32)0x00000020) 
  28. /* Flash Control Register bits */
  29. #define CR_PG_Set                ((u32)0x00000001)
  30. #define CR_PG_Reset              ((u32)0x00001FFE) 
  31. #define CR_PER_Set               ((u32)0x00000002)
  32. #define CR_PER_Reset             ((u32)0x00001FFD)
  33. #define CR_MER_Set               ((u32)0x00000004)
  34. #define CR_MER_Reset             ((u32)0x00001FFB)
  35. #define CR_OPTPG_Set             ((u32)0x00000010)
  36. #define CR_OPTPG_Reset           ((u32)0x00001FEF)
  37. #define CR_OPTER_Set             ((u32)0x00000020)
  38. #define CR_OPTER_Reset           ((u32)0x00001FDF)
  39. #define CR_STRT_Set              ((u32)0x00000040)
  40.  
  41. #define CR_LOCK_Set              ((u32)0x00000080)
  42. /* FLASH Mask */
  43. #define RDPRT_Mask               ((u32)0x00000002)
  44. #define WRP0_Mask                ((u32)0x000000FF)
  45. #define WRP1_Mask                ((u32)0x0000FF00)
  46. #define WRP2_Mask                ((u32)0x00FF0000)
  47. #define WRP3_Mask                ((u32)0xFF000000)
  48. /* FLASH Keys */
  49. #define RDP_Key                  ((u16)0x00A5)
  50. #define FLASH_KEY1               ((u32)0x45670123)
  51. #define FLASH_KEY2               ((u32)0xCDEF89AB)
  52. /* Delay definition */   
  53. #define EraseTimeout             ((u32)0x00000FFF)
  54. #define ProgramTimeout           ((u32)0x0000000F)
  55. #endif
  56. /* Private macro -------------------------------------------------------------*/
  57. /* Private variables ---------------------------------------------------------*/
  58. /* Private function prototypes -----------------------------------------------*/
  59. #ifdef _FLASH_PROG
  60. static void delay(void);
  61. #endif
  62. /* Private functions ---------------------------------------------------------*/
  63. /*******************************************************************************
  64. * Function Name  : FLASH_SetLatency
  65. * Description    : Sets the code latency value.
  66. * Input          : - FLASH_Latency: specifies the FLASH Latency value.
  67. *                    This parameter can be one of the following values:
  68. *                       - FLASH_Latency_0: FLASH Zero Latency cycle
  69. *                       - FLASH_Latency_1: FLASH One Latency cycle
  70. *                       - FLASH_Latency_2: FLASH Two Latency cycles
  71. * Output         : None
  72. * Return         : None
  73. *******************************************************************************/
  74. void FLASH_SetLatency(u32 FLASH_Latency)
  75. {
  76.   /* Check the parameters */
  77.   assert(IS_FLASH_LATENCY(FLASH_Latency));
  78.   
  79.   /* Sets the Latency value */
  80.   FLASH->ACR &= ACR_LATENCY_Mask;
  81.   FLASH->ACR |= FLASH_Latency;
  82. }
  83. /*******************************************************************************
  84. * Function Name  : FLASH_HalfCycleAccessCmd
  85. * Description    : Enables or disables the Half cycle flash access.
  86. * Input          : - FLASH_HalfCycle: specifies the FLASH Half cycle Access mode.
  87. *                    This parameter can be one of the following values:
  88. *                       - FLASH_HalfCycleAccess_Enable: FLASH Half Cycle Enable
  89. *                       - FLASH_HalfCycleAccess_Disable: FLASH Half Cycle Disable
  90. * Output         : None
  91. * Return         : None
  92. *******************************************************************************/
  93. void FLASH_HalfCycleAccessCmd(u32 FLASH_HalfCycleAccess)
  94. {
  95.   /* Check the parameters */
  96.   assert(IS_FLASH_HALFCYCLEACCESS_STATE(FLASH_HalfCycleAccess));
  97.   
  98.   /* Enable or disable the Half cycle access */
  99.   FLASH->ACR &= ACR_HLFCYA_Mask;
  100.   FLASH->ACR |= FLASH_HalfCycleAccess;
  101. }
  102. /*******************************************************************************
  103. * Function Name  : FLASH_PrefetchBufferCmd
  104. * Description    : Enables or disables the Prefetch Buffer.
  105. * Input          : - FLASH_PrefetchBuffer: specifies the Prefetch buffer status.
  106. *                    This parameter can be one of the following values:
  107. *                       - FLASH_PrefetchBuffer_Enable: FLASH Prefetch Buffer Enable
  108. *                       - FLASH_PrefetchBuffer_Disable: FLASH Prefetch Buffer Disable
  109. * Output         : None
  110. * Return         : None
  111. *******************************************************************************/
  112. void FLASH_PrefetchBufferCmd(u32 FLASH_PrefetchBuffer)
  113. {
  114.   /* Check the parameters */
  115.   assert(IS_FLASH_PREFETCHBUFFER_STATE(FLASH_PrefetchBuffer));
  116.   
  117.   /* Enable or disable the Prefetch Buffer */
  118.   FLASH->ACR &= ACR_PRFTBE_Mask;
  119.   FLASH->ACR |= FLASH_PrefetchBuffer;
  120. }
  121. #ifdef _FLASH_PROG
  122. /*******************************************************************************
  123. * Function Name  : FLASH_Unlock
  124. * Description    : Unlocks the FLASH Program Erase Controller.
  125. * Input          : None
  126. * Output         : None
  127. * Return         : None
  128. *******************************************************************************/
  129. void FLASH_Unlock(void)
  130. {
  131.   /* Authorize the FPEC Access */
  132.   FLASH->KEYR = FLASH_KEY1;
  133.   FLASH->KEYR = FLASH_KEY2;
  134. }
  135. /*******************************************************************************
  136. * Function Name  : FLASH_Lock
  137. * Description    : Locks the FLASH Program Erase Controller.
  138. * Input          : None
  139. * Output         : None
  140. * Return         : None
  141. *******************************************************************************/
  142. void FLASH_Lock(void)
  143. {
  144.   /* Set the Lock Bit to lock the FPEC and the FCR */
  145.   FLASH->CR |= CR_LOCK_Set;
  146. }
  147. /*******************************************************************************
  148. * Function Name  : FLASH_ErasePage
  149. * Description    : Erases a specified FLASH page.
  150. * Input          : - Page_Address: The page address to be erased.
  151. * Output         : None
  152. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  153. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  154. *                  FLASH_TIMEOUT.
  155. *******************************************************************************/
  156. FLASH_Status FLASH_ErasePage(u32 Page_Address)
  157. {
  158.   FLASH_Status status = FLASH_COMPLETE;
  159.   /* Check the parameters */
  160.   assert(IS_FLASH_ADDRESS(Page_Address));
  161.   /* Wait for last operation to be completed */
  162.   status = FLASH_WaitForLastOperation(EraseTimeout);
  163.   
  164.   if(status == FLASH_COMPLETE)
  165.   { 
  166.     /* if the previous operation is completed, proceed to erase the page */
  167.     FLASH->CR|= CR_PER_Set;
  168.     FLASH->AR = Page_Address; 
  169.     FLASH->CR|= CR_STRT_Set;
  170.     
  171.     /* Wait for last operation to be completed */
  172.     status = FLASH_WaitForLastOperation(EraseTimeout);
  173.     if(status != FLASH_BUSY)
  174.     {
  175.       /* if the erase operation is completed, disable the PER Bit */
  176.       FLASH->CR &= CR_PER_Reset;
  177.     }
  178.   }
  179.   /* Return the Erase Status */
  180.   return status;
  181. }
  182. /*******************************************************************************
  183. * Function Name  : FLASH_EraseAllPages
  184. * Description    : Erases all FLASH pages.
  185. * Input          : None
  186. * Output         : None
  187. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  188. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  189. *                  FLASH_TIMEOUT.
  190. *******************************************************************************/
  191. FLASH_Status FLASH_EraseAllPages(void)
  192. {
  193.   FLASH_Status status = FLASH_COMPLETE;
  194.   /* Wait for last operation to be completed */
  195.   status = FLASH_WaitForLastOperation(EraseTimeout);
  196.   
  197.   if(status == FLASH_COMPLETE)
  198.   {
  199.     /* if the previous operation is completed, proceed to erase all pages */
  200.      FLASH->CR |= CR_MER_Set;
  201.      FLASH->CR |= CR_STRT_Set;
  202.     
  203.     /* Wait for last operation to be completed */
  204.     status = FLASH_WaitForLastOperation(EraseTimeout);
  205.     if(status != FLASH_BUSY)
  206.     {
  207.       /* if the erase operation is completed, disable the MER Bit */
  208.       FLASH->CR &= CR_MER_Reset;
  209.     }
  210.   }    
  211.   /* Return the Erase Status */
  212.   return status;
  213. }
  214. /*******************************************************************************
  215. * Function Name  : FLASH_EraseOptionBytes
  216. * Description    : Erases the FLASH option bytes.
  217. * Input          : None
  218. * Output         : None
  219. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  220. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  221. *                  FLASH_TIMEOUT.
  222. *******************************************************************************/
  223. FLASH_Status FLASH_EraseOptionBytes(void)
  224. {
  225.   FLASH_Status status = FLASH_COMPLETE;
  226.   
  227.   /* Wait for last operation to be completed */
  228.   status = FLASH_WaitForLastOperation(EraseTimeout);
  229.   if(status == FLASH_COMPLETE)
  230.   {
  231.     /* Authorize the small information block programming */
  232.     FLASH->OPTKEYR = FLASH_KEY1;
  233.     FLASH->OPTKEYR = FLASH_KEY2;
  234.     
  235.     /* if the previous operation is completed, proceed to erase the option bytes */
  236.     FLASH->CR |= CR_OPTER_Set;
  237.     FLASH->CR |= CR_STRT_Set;
  238.     /* Wait for last operation to be completed */
  239.     status = FLASH_WaitForLastOperation(EraseTimeout);
  240.     
  241.     if(status == FLASH_COMPLETE)
  242.     {
  243.       /* if the erase operation is completed, disable the OPTER Bit */
  244.       FLASH->CR &= CR_OPTER_Reset;
  245.        
  246.       /* Enable the Option Bytes Programming operation */
  247.       FLASH->CR |= CR_OPTPG_Set;
  248.       /* Enable the readout access */
  249.       OB->RDP= RDP_Key; 
  250.       /* Wait for last operation to be completed */
  251.       status = FLASH_WaitForLastOperation(ProgramTimeout);
  252.  
  253.       if(status != FLASH_BUSY)
  254.       {
  255.         /* if the program operation is completed, disable the OPTPG Bit */
  256.         FLASH->CR &= CR_OPTPG_Reset;
  257.       }
  258.     }
  259.     else
  260.     {
  261.       if (status != FLASH_BUSY)
  262.       {
  263.         /* Disable the OPTPG Bit */
  264.         FLASH->CR &= CR_OPTPG_Reset;
  265.       }
  266.     }  
  267.   }
  268.   /* Return the erase status */
  269.   return status;
  270. }
  271. /*******************************************************************************
  272. * Function Name  : FLASH_ProgramWord
  273. * Description    : Programs a word at a specified address.
  274. * Input          : - Address: specifies the address to be programmed.
  275. *                  - Data: specifies the data to be programmed.
  276. * Output         : None
  277. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  278. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  279. *                  FLASH_TIMEOUT. 
  280. *******************************************************************************/
  281. FLASH_Status FLASH_ProgramWord(u32 Address, u32 Data)
  282. {
  283.   FLASH_Status status = FLASH_COMPLETE;
  284.   /* Check the parameters */
  285.   assert(IS_FLASH_ADDRESS(Address));
  286.   /* Wait for last operation to be completed */
  287.   status = FLASH_WaitForLastOperation(ProgramTimeout);
  288.   
  289.   if(status == FLASH_COMPLETE)
  290.   {
  291.     /* if the previous operation is completed, proceed to program the new first 
  292.     half word */
  293.     FLASH->CR |= CR_PG_Set;
  294.   
  295.     *(vu16*)Address = (u16)Data;
  296.     /* Wait for last operation to be completed */
  297.     status = FLASH_WaitForLastOperation(ProgramTimeout);
  298.  
  299.     if(status == FLASH_COMPLETE)
  300.     {
  301.       /* if the previous operation is completed, proceed to program the new second 
  302.       half word */
  303.       *(vu16*)(Address + 2) = Data >> 16;
  304.     
  305.       /* Wait for last operation to be completed */
  306.       status = FLASH_WaitForLastOperation(ProgramTimeout);
  307.         
  308.       if(status != FLASH_BUSY)
  309.       {
  310.         /* Disable the PG Bit */
  311.         FLASH->CR &= CR_PG_Reset;
  312.       }
  313.     }
  314.     else
  315.     {
  316.       if (status != FLASH_BUSY)
  317.       {
  318.         /* Disable the PG Bit */
  319.         FLASH->CR &= CR_PG_Reset;
  320.       }
  321.      }
  322.   }
  323.   /* Return the Program Status */
  324.   return status;
  325. }
  326. /*******************************************************************************
  327. * Function Name  : FLASH_ProgramHalfWord
  328. * Description    : Programs a half word at a specified address.
  329. * Input          : - Address: specifies the address to be programmed.
  330. *                  - Data: specifies the data to be programmed.
  331. * Output         : None
  332. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  333. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  334. *                  FLASH_TIMEOUT. 
  335. *******************************************************************************/
  336. FLASH_Status FLASH_ProgramHalfWord(u32 Address, u16 Data)
  337. {
  338.   FLASH_Status status = FLASH_COMPLETE;
  339.   /* Check the parameters */
  340.   assert(IS_FLASH_ADDRESS(Address));
  341.   /* Wait for last operation to be completed */
  342.   status = FLASH_WaitForLastOperation(ProgramTimeout);
  343.   
  344.   if(status == FLASH_COMPLETE)
  345.   {
  346.     /* if the previous operation is completed, proceed to program the new data */
  347.     FLASH->CR |= CR_PG_Set;
  348.   
  349.     *(vu16*)Address = Data;
  350.     /* Wait for last operation to be completed */
  351.     status = FLASH_WaitForLastOperation(ProgramTimeout);
  352.     if(status != FLASH_BUSY)
  353.     {
  354.       /* if the program operation is completed, disable the PG Bit */
  355.       FLASH->CR &= CR_PG_Reset;
  356.     }
  357.   } 
  358.   /* Return the Program Status */
  359.   return status;
  360. }
  361. /*******************************************************************************
  362. * Function Name  : FLASH_ProgramOptionByteData
  363. * Description    : Programs a half word at a specified Option Byte Data address.
  364. * Input          : - Address: specifies the address to be programmed.
  365. *                    This parameter can be 0x1FFFF804 or 0x1FFFF806. 
  366. *                  - Data: specifies the data to be programmed.
  367. * Output         : None
  368. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  369. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  370. *                  FLASH_TIMEOUT. 
  371. *******************************************************************************/
  372. FLASH_Status FLASH_ProgramOptionByteData(u32 Address, u8 Data)
  373. {
  374.   FLASH_Status status = FLASH_COMPLETE;
  375.   /* Check the parameters */
  376.   assert(IS_OB_DATA_ADDRESS(Address));
  377.   status = FLASH_WaitForLastOperation(ProgramTimeout);
  378.   if(status == FLASH_COMPLETE)
  379.   {
  380.     /* Authorize the small information block programming */
  381.     FLASH->OPTKEYR = FLASH_KEY1;
  382.     FLASH->OPTKEYR = FLASH_KEY2;
  383.     /* Enables the Option Bytes Programming operation */
  384.     FLASH->CR |= CR_OPTPG_Set; 
  385.     *(vu16*)Address = Data;
  386.     
  387.     /* Wait for last operation to be completed */
  388.     status = FLASH_WaitForLastOperation(ProgramTimeout);
  389.     if(status != FLASH_BUSY)
  390.     {
  391.       /* if the program operation is completed, disable the OPTPG Bit */
  392.       FLASH->CR &= CR_OPTPG_Reset;
  393.     }
  394.   }    
  395.   /* Return the Option Byte Data Program Status */
  396.   return status;      
  397. }
  398. /*******************************************************************************
  399. * Function Name  : FLASH_EnableWriteProtection
  400. * Description    : Write protects the desired pages
  401. * Input          : - FLASH_Pages: specifies the address of the pages to be 
  402. *                    write protected. This parameter can be:
  403. *                    - A value between FLASH_WRProt_Pages0to3 and 
  404. *                      FLASH_WRProt_Pages124to127 
  405. *                    - FLASH_WRProt_AllPages
  406. * Output         : None
  407. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  408. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  409. *                  FLASH_TIMEOUT.
  410. *******************************************************************************/
  411. FLASH_Status FLASH_EnableWriteProtection(u32 FLASH_Pages)
  412. {
  413.   u16 WRP0_Data = 0xFFFF, WRP1_Data = 0xFFFF, WRP2_Data = 0xFFFF, WRP3_Data = 0xFFFF;
  414.   
  415.   FLASH_Status status = FLASH_COMPLETE;
  416.   
  417.   /* Check the parameters */
  418.   assert(IS_FLASH_WRPROT_PAGE(FLASH_Pages));
  419.   
  420.   FLASH_Pages = (u32)(~FLASH_Pages);
  421.   WRP0_Data = (vu16)(FLASH_Pages & WRP0_Mask);
  422.   WRP1_Data = (vu16)((FLASH_Pages & WRP1_Mask) >> 8);
  423.   WRP2_Data = (vu16)((FLASH_Pages & WRP2_Mask) >> 16);
  424.   WRP3_Data = (vu16)((FLASH_Pages & WRP3_Mask) >> 24);
  425.   
  426.   /* Wait for last operation to be completed */
  427.   status = FLASH_WaitForLastOperation(ProgramTimeout);
  428.   
  429.   if(status == FLASH_COMPLETE)
  430.   {
  431.     /* Authorizes the small information block programming */
  432.     FLASH->OPTKEYR = FLASH_KEY1;
  433.     FLASH->OPTKEYR = FLASH_KEY2;
  434.     FLASH->CR |= CR_OPTPG_Set;
  435.     if(WRP0_Data != 0xFF)
  436.     {
  437.       OB->WRP0 = WRP0_Data;
  438.       
  439.       /* Wait for last operation to be completed */
  440.       status = FLASH_WaitForLastOperation(ProgramTimeout);
  441.     }
  442.     if((status == FLASH_COMPLETE) && (WRP1_Data != 0xFF))
  443.     {
  444.       OB->WRP1 = WRP1_Data;
  445.       
  446.       /* Wait for last operation to be completed */
  447.       status = FLASH_WaitForLastOperation(ProgramTimeout);
  448.     }
  449.     if((status == FLASH_COMPLETE) && (WRP2_Data != 0xFF))
  450.     {
  451.       OB->WRP2 = WRP2_Data;
  452.       
  453.       /* Wait for last operation to be completed */
  454.       status = FLASH_WaitForLastOperation(ProgramTimeout);
  455.     }
  456.     
  457.     if((status == FLASH_COMPLETE)&& (WRP3_Data != 0xFF))
  458.     {
  459.       OB->WRP3 = WRP3_Data;
  460.      
  461.       /* Wait for last operation to be completed */
  462.       status = FLASH_WaitForLastOperation(ProgramTimeout);
  463.     }
  464.           
  465.     if(status != FLASH_BUSY)
  466.     {
  467.       /* if the program operation is completed, disable the OPTPG Bit */
  468.       FLASH->CR &= CR_OPTPG_Reset;
  469.     }
  470.   } 
  471.   /* Return the write protection operation Status */
  472.   return status;       
  473. }
  474. /*******************************************************************************
  475. * Function Name  : FLASH_ReadOutProtection
  476. * Description    : Enables or disables the read out protection
  477. * Input          : - Newstate: new state of the ReadOut Protection.
  478. *                    This parameter can be: ENABLE or DISABLE.
  479. * Output         : None
  480. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  481. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  482. *                  FLASH_TIMEOUT.
  483. *******************************************************************************/
  484. FLASH_Status FLASH_ReadOutProtection(FunctionalState NewState)
  485. {
  486.   FLASH_Status status = FLASH_COMPLETE;
  487.   /* Check the parameters */
  488.   assert(IS_FUNCTIONAL_STATE(NewState));
  489.   status = FLASH_WaitForLastOperation(EraseTimeout);
  490.   if(status == FLASH_COMPLETE)
  491.   {
  492.     /* Authorizes the small information block programming */
  493.     FLASH->OPTKEYR = FLASH_KEY1;
  494.     FLASH->OPTKEYR = FLASH_KEY2;
  495.     FLASH->CR |= CR_OPTER_Set;
  496.     FLASH->CR |= CR_STRT_Set;
  497.     /* Wait for last operation to be completed */
  498.     status = FLASH_WaitForLastOperation(EraseTimeout);
  499.     if(status == FLASH_COMPLETE)
  500.     {
  501.       /* if the erase operation is completed, disable the OPTER Bit */
  502.       FLASH->CR &= CR_OPTER_Reset;
  503.       /* Enable the Option Bytes Programming operation */
  504.       FLASH->CR |= CR_OPTPG_Set; 
  505.       if(NewState != DISABLE)
  506.       {
  507.         OB->RDP = 0x00;
  508.       }
  509.       else
  510.       {
  511.         OB->RDP = RDP_Key;  
  512.       }
  513.       /* Wait for last operation to be completed */
  514.       status = FLASH_WaitForLastOperation(EraseTimeout); 
  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.     else 
  523.     {
  524.       if(status != FLASH_BUSY)
  525.       {
  526.         /* Disable the OPTER Bit */
  527.         FLASH->CR &= CR_OPTER_Reset;
  528.       }
  529.     }
  530.   }
  531.   /* Return the protection operation Status */
  532.   return status;      
  533. }
  534.   
  535. /*******************************************************************************
  536. * Function Name  : FLASH_UserOptionByteConfig
  537. * Description    : Programs the FLASH User Option Byte: IWDG_SW / RST_STOP /
  538. *                  RST_STDBY.
  539. * Input          : - OB_IWDG: Selects the IWDG mode
  540. *                     This parameter can be one of the following values:
  541. *                     - OB_IWDG_SW: Software IWDG selected
  542. *                     - OB_IWDG_HW: Hardware IWDG selected
  543. *                  - OB_STOP: Reset event when entering STOP mode.
  544. *                     This parameter can be one of the following values:
  545. *                     - OB_STOP_NoRST: No reset generated when entering in STOP
  546. *                     - OB_STOP_RST: Reset generated when entering in STOP
  547. *                  - OB_STDBY: Reset event when entering Standby mode.
  548. *                    This parameter can be one of the following values:
  549. *                     - OB_STDBY_NoRST: No reset generated when entering in STANDBY
  550. *                     - OB_STDBY_RST: Reset generated when entering in STANDBY
  551. * Output         : None
  552. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  553. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  554. *                  FLASH_TIMEOUT.
  555. *******************************************************************************/
  556. FLASH_Status FLASH_UserOptionByteConfig(u16 OB_IWDG, u16 OB_STOP, u16 OB_STDBY)
  557. {
  558.   FLASH_Status status = FLASH_COMPLETE; 
  559.   /* Check the parameters */
  560.   assert(IS_OB_IWDG_SOURCE(OB_IWDG));
  561.   assert(IS_OB_STOP_SOURCE(OB_STOP));
  562.   assert(IS_OB_STDBY_SOURCE(OB_STDBY));
  563.   /* Authorize the small information block programming */
  564.   FLASH->OPTKEYR = FLASH_KEY1;
  565.   FLASH->OPTKEYR = FLASH_KEY2;
  566.   
  567.   /* Wait for last operation to be completed */
  568.   status = FLASH_WaitForLastOperation(ProgramTimeout);
  569.   
  570.   if(status == FLASH_COMPLETE)
  571.   {  
  572.     /* Enable the Option Bytes Programming operation */
  573.     FLASH->CR |= CR_OPTPG_Set; 
  574.            
  575.     OB->USER = ( OB_IWDG | OB_STOP |OB_STDBY) | (u16)0xF8; 
  576.   
  577.     /* Wait for last operation to be completed */
  578.     status = FLASH_WaitForLastOperation(ProgramTimeout);
  579.     if(status != FLASH_BUSY)
  580.     {
  581.       /* if the program operation is completed, disable the OPTPG Bit */
  582.       FLASH->CR &= CR_OPTPG_Reset;
  583.     }
  584.   }    
  585.   /* Return the Option Byte program Status */
  586.   return status;
  587. }
  588. /*******************************************************************************
  589. * Function Name  : FLASH_GetUserOptionByte
  590. * Description    : Returns the FLASH User Option Bytes values.
  591. * Input          : None
  592. * Output         : None
  593. * Return         : The FLASH User Option Bytes values:IWDG_SW(Bit0), RST_STOP(Bit1)
  594. *                  and RST_STDBY(Bit2).
  595. *******************************************************************************/
  596. u32 FLASH_GetUserOptionByte(void)
  597. {
  598.   /* Return the User Option Byte */
  599.   return (u32)(FLASH->OBR >> 2);
  600. }
  601. /*******************************************************************************
  602. * Function Name  : FLASH_GetWriteProtectionOptionByte
  603. * Description    : Returns the FLASH Write Protection Option Bytes Register value.
  604. * Input          : None
  605. * Output         : None
  606. * Return         : The FLASH Write Protection  Option Bytes Register value
  607. *******************************************************************************/
  608. u32 FLASH_GetWriteProtectionOptionByte(void)
  609. {
  610.   /* Return the Falsh write protection Register value */
  611.   return (u32)(FLASH->WRPR);
  612. }
  613. /*******************************************************************************
  614. * Function Name  : FLASH_GetReadOutProtectionStatus
  615. * Description    : Checks whether the FLASH Read Out Protection Status is set 
  616. *                  or not.
  617. * Input          : None
  618. * Output         : None
  619. * Return         : FLASH ReadOut Protection Status(SET or RESET)
  620. *******************************************************************************/
  621. FlagStatus FLASH_GetReadOutProtectionStatus(void)
  622. {
  623.   FlagStatus readoutstatus = RESET;
  624.   if ((FLASH->OBR & RDPRT_Mask) != (u32)RESET)
  625.   {
  626.     readoutstatus = SET;
  627.   }
  628.   else
  629.   {
  630.     readoutstatus = RESET;
  631.   }
  632.   return readoutstatus;
  633. }
  634. /*******************************************************************************
  635. * Function Name  : FLASH_GetPrefetchBufferStatus
  636. * Description    : Checks whether the FLASH Prefetch Buffer status is set or not.
  637. * Input          : None
  638. * Output         : None
  639. * Return         : FLASH Prefetch Buffer Status (SET or RESET).
  640. *******************************************************************************/
  641. FlagStatus FLASH_GetPrefetchBufferStatus(void)
  642. {
  643.   FlagStatus bitstatus = RESET;
  644.   
  645.   if ((FLASH->ACR & ACR_PRFTBS_Mask) != (u32)RESET)
  646.   {
  647.     bitstatus = SET;
  648.   }
  649.   else
  650.   {
  651.     bitstatus = RESET;
  652.   }
  653.   /* Return the new state of FLASH Prefetch Buffer Status (SET or RESET) */
  654.   return bitstatus; 
  655. }
  656. /*******************************************************************************
  657. * Function Name  : FLASH_ITConfig
  658. * Description    : Enables or disables the specified FLASH interrupts.
  659. * Input          : - FLASH_IT: specifies the FLASH interrupt sources to be 
  660. *                    enabled or disabled.
  661. *                    This parameter can be any combination of the following values:
  662. *                       - FLASH_IT_ERROR: FLASH Error Interrupt
  663. *                       - FLASH_IT_EOP: FLASH end of operation Interrupt
  664. * Output         : None
  665. * Return         : None 
  666. *******************************************************************************/
  667. void FLASH_ITConfig(u16 FLASH_IT, FunctionalState NewState)
  668. {
  669.   /* Check the parameters */
  670.   assert(IS_FLASH_IT(FLASH_IT)); 
  671.   assert(IS_FUNCTIONAL_STATE(NewState));
  672.   if(NewState != DISABLE)
  673.   {
  674.     /* Enable the interrupt sources */
  675.     FLASH->CR |= FLASH_IT;
  676.   }
  677.   else
  678.   {
  679.     /* Disable the interrupt sources */
  680.     FLASH->CR &= ~(u32)FLASH_IT;
  681.   }
  682. }
  683. /*******************************************************************************
  684. * Function Name  : FLASH_GetFlagStatus
  685. * Description    : Checks whether the specified FLASH flag is set or not.
  686. * Input          : - FLASH_FLAG: specifies the FLASH flag to check.
  687. *                     This parameter can be one of the following values:
  688. *                    - FLASH_FLAG_BSY: FLASH Busy flag           
  689. *                    - FLASH_FLAG_PGERR: FLASH Program error flag       
  690. *                    - FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      
  691. *                    - FLASH_FLAG_EOP: FLASH End of Operation flag           
  692. *                    - FLASH_FLAG_OPTERR:  FLASH Option Byte error flag     
  693. * Output         : None
  694. * Return         : The new state of FLASH_FLAG (SET or RESET).
  695. *******************************************************************************/
  696. FlagStatus FLASH_GetFlagStatus(u16 FLASH_FLAG)
  697. {
  698.   FlagStatus bitstatus = RESET;
  699.   /* Check the parameters */
  700.   assert(IS_FLASH_GET_FLAG(FLASH_FLAG)) ;
  701.   if(FLASH_FLAG == FLASH_FLAG_OPTERR) 
  702.   {
  703.     if((FLASH->OBR & FLASH_FLAG_OPTERR) != (u32)RESET)
  704.     {
  705.       bitstatus = SET;
  706.     }
  707.     else
  708.     {
  709.       bitstatus = RESET;
  710.     }
  711.   }
  712.   else
  713.   {
  714.    if((FLASH->SR & FLASH_FLAG) != (u32)RESET)
  715.     {
  716.       bitstatus = SET;
  717.     }
  718.     else
  719.     {
  720.       bitstatus = RESET;
  721.     }
  722.   }
  723.   /* Return the new state of FLASH_FLAG (SET or RESET) */
  724.   return bitstatus;
  725. }
  726. /*******************************************************************************
  727. * Function Name  : FLASH_ClearFlag
  728. * Description    : Clears the FLASH抯 pending flags.
  729. * Input          : - FLASH_FLAG: specifies the FLASH flags to clear.
  730. *                    This parameter can be any combination of the following values:
  731. *                    - FLASH_FLAG_BSY: FLASH Busy flag           
  732. *                    - FLASH_FLAG_PGERR: FLASH Program error flag       
  733. *                    - FLASH_FLAG_WRPRTERR: FLASH Write protected error flag      
  734. *                    - FLASH_FLAG_EOP: FLASH End of Operation flag           
  735. * Output         : None
  736. * Return         : None
  737. *******************************************************************************/
  738. void FLASH_ClearFlag(u16 FLASH_FLAG)
  739. {
  740.   /* Check the parameters */
  741.   assert(IS_FLASH_CLEAR_FLAG(FLASH_FLAG)) ;
  742.   
  743.   /* Clear the flags */
  744.   FLASH->SR = FLASH_FLAG;
  745. }
  746. /*******************************************************************************
  747. * Function Name  : FLASH_GetStatus
  748. * Description    : Returns the FLASH Status.
  749. * Input          : None
  750. * Output         : None
  751. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  752. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE
  753. *******************************************************************************/
  754. FLASH_Status FLASH_GetStatus(void)
  755. {
  756.   FLASH_Status flashstatus = FLASH_COMPLETE;
  757.   
  758.   if((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) 
  759.   {
  760.     flashstatus = FLASH_BUSY;
  761.   }
  762.   else 
  763.   {  
  764.     if(FLASH->SR & FLASH_FLAG_PGERR)
  765.     { 
  766.       flashstatus = FLASH_ERROR_PG;
  767.     }
  768.     else 
  769.     {
  770.       if(FLASH->SR & FLASH_FLAG_WRPRTERR)
  771.       {
  772.         flashstatus = FLASH_ERROR_WRP;
  773.       }
  774.       else
  775.       {
  776.         flashstatus = FLASH_COMPLETE;
  777.       }
  778.     }
  779.   }
  780.   /* Return the Flash Status */
  781.   return flashstatus;
  782. }
  783. /*******************************************************************************
  784. * Function Name  : FLASH_WaitForLastOperation
  785. * Description    : Waits for a Flash operation to complete or a TIMEOUT to occur.
  786. * Input          : - Timeout: FLASH progamming Timeout
  787. * Output         : None
  788. * Return         : FLASH Status: The returned value can be: FLASH_BUSY, 
  789. *                  FLASH_ERROR_PG or FLASH_ERROR_WRP or FLASH_COMPLETE or 
  790. *                  FLASH_TIMEOUT.
  791. *******************************************************************************/
  792. FLASH_Status FLASH_WaitForLastOperation(u32 Timeout)
  793.   FLASH_Status status = FLASH_COMPLETE;
  794.    
  795.   /* Check for the Flash Status */
  796.   status = FLASH_GetStatus();
  797.   /* Wait for a Flash operation to complete or a TIMEOUT to occur */
  798.   while((status == FLASH_BUSY) && (Timeout != 0x00))
  799.   {
  800.     delay();
  801.     status = FLASH_GetStatus();
  802.     Timeout--;
  803.   }
  804.   if(Timeout == 0x00 )
  805.   {
  806.     status = FLASH_TIMEOUT;
  807.   }
  808.   /* Return the operation status */
  809.   return status;
  810. }
  811. /*******************************************************************************
  812. * Function Name  : delay
  813. * Description    : Inserts a time delay.
  814. * Input          : None
  815. * Output         : None
  816. * Return         : None
  817. *******************************************************************************/
  818. static void delay(void)
  819. {
  820.   vu32 i = 0;
  821.   for(i = 0xFF; i != 0; i--)
  822.   {
  823.   }
  824. }
  825. #endif
  826. /******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/