Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. void UnlockFlash()
  2. {
  3.     FLASH->KEYR = 0x45670123;
  4.     FLASH->KEYR = 0xCDEF89AB;
  5. }
  6.  
  7.  
  8.  
  9. void LockFlash()
  10. {
  11.     SET_BIT(FLASH->CR, FLASH_CR_LOCK);
  12. }
  13.  
  14.  
  15.  
  16. void UserFlash_Erase()
  17. {
  18.     //Unlock FLASH register
  19.     UnlockFlash();
  20.    
  21.     //Check that no Flash memory operation is ongoing by checking the BSY bit in the FLASH_SR register
  22.     while (READ_BIT(FLASH->SR, FLASH_SR_BSY));
  23.    
  24.     //Set the SER bit
  25.     SET_BIT(FLASH->CR, FLASH_CR_SER);
  26.    
  27.     //Select the sector out of the 12 sectors
  28.     MODIFY_REG(FLASH->CR, FLASH_CR_SNB, USER_FLASH_SECTOR_INDEX << FLASH_CR_SNB_Pos);
  29.    
  30.     //Set the STRT bit in the FLASH_CR register
  31.     SET_BIT(FLASH->CR, FLASH_CR_STRT);
  32.    
  33.     //Wait for the BSY bit to be cleared
  34.     while(READ_BIT(FLASH->SR, FLASH_SR_BSY));
  35.    
  36.     //Lock FLASH register
  37.     LockFlash();
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement