/**
  * @brief   Program byte, halfword, word or double word at a specified address  with interrupt enabled.
  * @param  TypeProgram:  Indicate the way to program at a specified address.
  *                           This parameter can be a value of @ref FLASH_Type_Program
  * @param  Address:  specifies the address to be programmed.
  * @param  Data: specifies the data to be programmed
  * 
  * @retval HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Program_IT(uint32_t TypeProgram, uint32_t Address, uint64_t Data)
{
  HAL_StatusTypeDef status = HAL_OK;
  
  /* Process Locked */
  __HAL_LOCK(&pFlash);

  /* Check the parameters */
  assert_param(IS_FLASH_TYPEPROGRAM(TypeProgram));

  /* Enable End of FLASH Operation interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_EOP);
  
  /* Enable Error source interrupt */
  __HAL_FLASH_ENABLE_IT(FLASH_IT_ERR);

  pFlash.ProcedureOnGoing = FLASH_PROC_PROGRAM;
  pFlash.Address = Address;

  if(TypeProgram == FLASH_TYPEPROGRAM_BYTE)
  {
    /*Program byte (8-bit) at a specified address.*/
      FLASH_Program_Byte(Address, (uint8_t) Data);
  }
  else if(TypeProgram == FLASH_TYPEPROGRAM_HALFWORD)
  {
    /*Program halfword (16-bit) at a specified address.*/
    FLASH_Program_HalfWord(Address, (uint16_t) Data);
  }
  else if(TypeProgram == FLASH_TYPEPROGRAM_WORD)
  {
    /*Program word (32-bit) at a specified address.*/
    FLASH_Program_Word(Address, (uint32_t) Data);
  }
  else
  {
    /*Program double word (64-bit) at a specified address.*/
    FLASH_Program_DoubleWord(Address, Data);
  }

  return status;
}
Exemplo n.º 2
0
OT_WEAK ot_u8  vprom_write(vaddr addr, ot_u16 value) {
/// 1. Resolve actual address from virtual address -- simple
/// 2. If the address is at the start of the block, erase the block
/// 3. Write the value to the place it must go
#if (VPROM_SIZE <= 0)
    return ~0;
#else
    static ot_u32   word;       // storage for word-write using halfwords
    ot_u32          paddr;
    FLASH_Status    err;
    ot_int          offset;
    
    err     = FLASH_COMPLETE;
    paddr   = _vprom_base + addr;
    
    // STM32L0 uses 128 byte flash pages.  Erase page if on page-start address
    if ((paddr & (FLASH_PAGE_SIZE-1)) == 0) {
        err = FLASH_Erase_Page(paddr);
    }
    
    offset = (paddr & 2);
    *(((ot_u8*)&word) + offset) = value;
    
    // Prepare the word and write it, but only when a full word is supplied
    // We also do unlocking and locking immediately around the write, because 
    // there is some importance in protecting flash memory.
    if (offset != 0) {
        FLASH->PRGKEYR  = FLASH_PRGKEY1;
        FLASH->PRGKEYR  = FLASH_PRGKEY2;  
        err             = FLASH_Program_Word(paddr, word);
        FLASH->PECR    |= FLASH_PECR_PRGLOCK;
    }
    
    return err;
#endif
}