stm32l1: flash: extract common code

Extracted all code that will be common with l0.  Compared with ref mans
for l0 and l4.  No functional change, just moving things getting ready.
This commit is contained in:
Karl Palsson
2017-05-01 22:24:02 +00:00
parent 9e36b8f29c
commit ce787c0f40
5 changed files with 331 additions and 250 deletions

View File

@@ -71,164 +71,4 @@ void flash_64bit_disable(void)
FLASH_ACR &= ~FLASH_ACR_ACC64;
}
/*---------------------------------------------------------------------------*/
/** @brief Enable the FLASH Prefetch Buffer
This buffer is used for instruction fetches and is enabled by default after
reset.
Note carefully the restrictions under which the prefetch buffer may be
enabled or disabled. Prefetch is only available when 64-bit
access is enabled.
*/
void flash_prefetch_enable(void)
{
FLASH_ACR |= FLASH_ACR_PRFTEN;
}
/*---------------------------------------------------------------------------*/
/** @brief Disable the FLASH Prefetch Buffer
Note carefully the restrictions under which the prefetch buffer may be
set to disabled. See the reference and programming manuals for details.
*/
void flash_prefetch_disable(void)
{
FLASH_ACR &= ~FLASH_ACR_PRFTEN;
}
/*---------------------------------------------------------------------------*/
/** @brief Set the Number of Wait States
Used to match the system clock to the FLASH memory access time. See the
programming manual for more information on clock speed and voltage ranges. The
latency must be changed to the appropriate value <b>before</b> any increase in
clock speed, or <b>after</b> any decrease in clock speed. A latency setting of
zero only applies if 64-bit mode is not used.
@param[in] ws values from @ref flash_latency.
*/
void flash_set_ws(uint32_t ws)
{
uint32_t reg32;
reg32 = FLASH_ACR;
reg32 &= ~(1 << 0);
reg32 |= ws;
FLASH_ACR = reg32;
}
/**
* Unlock primary access to the flash control/erase block
* You must call this before using any of the low level routines
* yourself.
* @sa flash_unlock
*/
void flash_unlock_pecr(void)
{
FLASH_PEKEYR = FLASH_PEKEYR_PEKEY1;
FLASH_PEKEYR = FLASH_PEKEYR_PEKEY2;
}
void flash_lock_pecr(void)
{
FLASH_PECR |= FLASH_PECR_PELOCK;
}
/**
* Unlock program memory itself.
* Writes the magic sequence to unlock the program memory
* you must have already unlocked access to this register!
* @sa flash_unlock_pecr
*/
void flash_unlock_progmem(void)
{
FLASH_PRGKEYR = FLASH_PRGKEYR_PRGKEY1;
FLASH_PRGKEYR = FLASH_PRGKEYR_PRGKEY2;
}
void flash_lock_progmem(void)
{
FLASH_PECR |= FLASH_PECR_PRGLOCK;
}
/**
* Unlock option bytes.
* Writes the magic sequence to unlock the option bytes,
* you must have already unlocked access to this register!
* @sa flash_unlock_pecr
*/
void flash_unlock_option_bytes(void)
{
FLASH_OPTKEYR = FLASH_OPTKEYR_OPTKEY1;
FLASH_OPTKEYR = FLASH_OPTKEYR_OPTKEY2;
}
void flash_lock_option_bytes(void)
{
FLASH_PECR |= FLASH_PECR_OPTLOCK;
}
/** @brief Unlock all segments of flash
*
*/
void flash_unlock(void)
{
flash_unlock_pecr();
flash_unlock_progmem();
flash_unlock_option_bytes();
}
/** @brief Lock all segments of flash
*
*/
void flash_lock(void)
{
flash_lock_option_bytes();
flash_lock_progmem();
flash_lock_pecr();
}
/** @brief Write a word to eeprom
*
* @param address assumed to be in the eeprom space, no checking
* @param data word to write
*/
void eeprom_program_word(uint32_t address, uint32_t data)
{
flash_unlock_pecr();
/* erase only if needed */
FLASH_PECR &= ~FLASH_PECR_FTDW;
MMIO32(address) = data;
flash_lock_pecr();
}
/** @brief Write a block of words to eeprom
*
* Writes a block of words to EEPROM at the requested address, erasing if
* necessary, and locking afterwards. Only wordwise writing is safe for
* writing any value
*
* @param[in] address must point to EEPROM space, no checking!
* @param[in] data pointer to data to write
* @param[in] length_in_words size of of data in WORDS!
*/
void eeprom_program_words(uint32_t address, uint32_t *data, int length_in_words)
{
int i;
flash_unlock_pecr();
while (FLASH_SR & FLASH_SR_BSY);
/* erase only if needed */
FLASH_PECR &= ~FLASH_PECR_FTDW;
for (i = 0; i < length_in_words; i++) {
MMIO32(address + (i * sizeof(uint32_t))) = *(data+i);
while (FLASH_SR & FLASH_SR_BSY);
}
flash_lock_pecr();
}
/**@}*/