Documentation added to flash modules for all STM32 families.
This commit is contained in:
committed by
Frantisek Burian
parent
e4f4845e0c
commit
a54a12e1c9
@@ -1,3 +1,7 @@
|
||||
/** @addtogroup flash_file
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
@@ -18,8 +22,21 @@
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**@{*/
|
||||
|
||||
#include <libopencm3/stm32/flash.h>
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @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 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.
|
||||
|
||||
@param[in] uint32_t ws: values 0-7 only.
|
||||
*/
|
||||
|
||||
void flash_set_ws(uint32_t ws)
|
||||
{
|
||||
uint32_t reg32;
|
||||
@@ -30,6 +47,13 @@ void flash_set_ws(uint32_t ws)
|
||||
FLASH_ACR = reg32;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Unlock the Flash Program and Erase Controller
|
||||
|
||||
This enables write access to the Flash memory. It is locked by default on
|
||||
reset.
|
||||
*/
|
||||
|
||||
void flash_unlock(void)
|
||||
{
|
||||
/* Clear the unlock sequence state. */
|
||||
@@ -40,28 +64,58 @@ void flash_unlock(void)
|
||||
FLASH_KEYR = FLASH_KEYR_KEY2;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Lock the Flash Program and Erase Controller
|
||||
|
||||
Used to prevent spurious writes to FLASH.
|
||||
*/
|
||||
|
||||
void flash_lock(void)
|
||||
{
|
||||
FLASH_CR |= FLASH_CR_LOCK;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear the Programming Error Status Flag
|
||||
|
||||
*/
|
||||
|
||||
void flash_clear_pgperr_flag(void)
|
||||
{
|
||||
FLASH_SR |= FLASH_SR_PGPERR;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear the End of Operation Status Flag
|
||||
|
||||
*/
|
||||
|
||||
void flash_clear_eop_flag(void)
|
||||
{
|
||||
FLASH_SR |= FLASH_SR_EOP;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear the Busy Status Flag
|
||||
|
||||
*/
|
||||
|
||||
void flash_clear_bsy_flag(void)
|
||||
{
|
||||
FLASH_SR &= ~FLASH_SR_BSY;
|
||||
}
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Wait until Last Operation has Ended
|
||||
|
||||
This loops indefinitely until an operation (write or erase) has completed by
|
||||
testing the busy flag.
|
||||
*/
|
||||
|
||||
void flash_wait_for_last_operation(void)
|
||||
{
|
||||
while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
/** @addtogroup flash_file
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is part of the libopencm3 project.
|
||||
*
|
||||
@@ -18,69 +22,154 @@
|
||||
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**@{*/
|
||||
|
||||
#include <libopencm3/stm32/flash.h>
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Set the Program Parallelism Size
|
||||
|
||||
Set the programming word width. Note carefully the power supply voltage
|
||||
restrictions under which the different word sizes may be used. See the
|
||||
programming manual for more information.
|
||||
|
||||
@param psize: 0 (8-bit), 1 (16-bit), 2 (32-bit), 3 (64-bit)
|
||||
*/
|
||||
|
||||
static inline void flash_set_program_size(uint32_t psize)
|
||||
{
|
||||
FLASH_CR &= ~(((1 << 0) | (1 << 1)) << 8);
|
||||
FLASH_CR |= psize;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Enable the Data Cache
|
||||
|
||||
*/
|
||||
|
||||
void flash_dcache_enable(void)
|
||||
{
|
||||
FLASH_ACR |= FLASH_ACR_DCE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Disable the Data Cache
|
||||
|
||||
*/
|
||||
|
||||
void flash_dcache_disable(void)
|
||||
{
|
||||
FLASH_ACR &= ~FLASH_ACR_DCE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Enable the Instruction Cache
|
||||
|
||||
*/
|
||||
|
||||
void flash_icache_enable(void)
|
||||
{
|
||||
FLASH_ACR |= FLASH_ACR_ICE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Disable the Instruction Cache
|
||||
|
||||
*/
|
||||
|
||||
void flash_icache_disable(void)
|
||||
{
|
||||
FLASH_ACR &= ~FLASH_ACR_ICE;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Enable the FLASH Prefetch Buffer
|
||||
|
||||
This buffer is used for instruction fetches and is enabled by default after
|
||||
reset.
|
||||
|
||||
Note carefully the clock restrictions under which the prefetch buffer may be
|
||||
enabled or disabled. Changes are normally made while the clock is running in
|
||||
the power-on low frequency mode before being set to a higher speed mode.
|
||||
See the reference manual for details.
|
||||
*/
|
||||
|
||||
void flash_prefetch_enable(void)
|
||||
{
|
||||
FLASH_ACR |= FLASH_ACR_PRFTEN;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Disable the FLASH Prefetch Buffer
|
||||
|
||||
Note carefully the clock restrictions under which the prefetch buffer may be
|
||||
set to disabled. See the reference manual for details.
|
||||
*/
|
||||
|
||||
void flash_prefetch_disable(void)
|
||||
{
|
||||
FLASH_ACR &= ~FLASH_ACR_PRFTEN;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Reset the Data Cache
|
||||
|
||||
The data cache must be disabled for this to have effect.
|
||||
*/
|
||||
|
||||
void flash_dcache_reset(void)
|
||||
{
|
||||
FLASH_ACR |= FLASH_ACR_DCRST;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Reset the Instruction Cache
|
||||
|
||||
The instruction cache must be disabled for this to have effect.
|
||||
*/
|
||||
|
||||
void flash_icache_reset(void)
|
||||
{
|
||||
FLASH_ACR |= FLASH_ACR_ICRST;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear the Programming Sequence Error Flag
|
||||
|
||||
This flag is set when incorrect programming configuration has been made.
|
||||
*/
|
||||
|
||||
void flash_clear_pgserr_flag(void)
|
||||
{
|
||||
FLASH_SR |= FLASH_SR_PGSERR;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear the Programming Alignment Error Flag
|
||||
|
||||
*/
|
||||
|
||||
void flash_clear_pgaerr_flag(void)
|
||||
{
|
||||
FLASH_SR |= FLASH_SR_PGAERR;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear the Write Protect Error Flag
|
||||
|
||||
*/
|
||||
|
||||
void flash_clear_wrperr_flag(void)
|
||||
{
|
||||
FLASH_SR |= FLASH_SR_WRPERR;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Clear All Status Flags
|
||||
|
||||
Program error, end of operation, write protect error, busy.
|
||||
*/
|
||||
|
||||
void flash_clear_status_flags(void)
|
||||
{
|
||||
flash_clear_pgserr_flag();
|
||||
@@ -91,6 +180,13 @@ void flash_clear_status_flags(void)
|
||||
flash_clear_bsy_flag();
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Unlock the Option Byte Access
|
||||
|
||||
This enables write access to the option bytes. It is locked by default on
|
||||
reset.
|
||||
*/
|
||||
|
||||
void flash_unlock_option_bytes(void)
|
||||
{
|
||||
/* Clear the unlock state. */
|
||||
@@ -101,11 +197,29 @@ void flash_unlock_option_bytes(void)
|
||||
FLASH_OPTKEYR = FLASH_OPTKEYR_KEY2;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Lock the Option Byte Access
|
||||
|
||||
This disables write access to the option bytes. It is locked by default on
|
||||
reset.
|
||||
*/
|
||||
|
||||
void flash_lock_option_bytes(void)
|
||||
{
|
||||
FLASH_OPTCR |= FLASH_OPTCR_OPTLOCK;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Program a 64 bit Word to FLASH
|
||||
|
||||
This performs all operations necessary to program a 64 bit word to FLASH memory.
|
||||
The program error flag should be checked separately for the event that memory
|
||||
was not properly erased.
|
||||
|
||||
@param[in] uint32_t address
|
||||
@param[in] uint64_t data.
|
||||
*/
|
||||
|
||||
void flash_program_double_word(uint32_t address, uint64_t data)
|
||||
{
|
||||
/* Ensure that all flash operations are complete. */
|
||||
@@ -125,6 +239,17 @@ void flash_program_double_word(uint32_t address, uint64_t data)
|
||||
FLASH_CR &= ~FLASH_CR_PG;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Program a 32 bit Word to FLASH
|
||||
|
||||
This performs all operations necessary to program a 32 bit word to FLASH memory.
|
||||
The program error flag should be checked separately for the event that memory
|
||||
was not properly erased.
|
||||
|
||||
@param[in] uint32_t address
|
||||
@param[in] uint32_t data.
|
||||
*/
|
||||
|
||||
void flash_program_word(uint32_t address, uint32_t data)
|
||||
{
|
||||
/* Ensure that all flash operations are complete. */
|
||||
@@ -144,6 +269,17 @@ void flash_program_word(uint32_t address, uint32_t data)
|
||||
FLASH_CR &= ~FLASH_CR_PG;
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Program a Half Word to FLASH
|
||||
|
||||
This performs all operations necessary to program a 16 bit word to FLASH memory.
|
||||
The program error flag should be checked separately for the event that memory
|
||||
was not properly erased.
|
||||
|
||||
@param[in] uint32_t address
|
||||
@param[in] uint16_t data.
|
||||
*/
|
||||
|
||||
void flash_program_half_word(uint32_t address, uint16_t data)
|
||||
{
|
||||
flash_wait_for_last_operation();
|
||||
@@ -158,6 +294,17 @@ void flash_program_half_word(uint32_t address, uint16_t data)
|
||||
FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Program an 8 bit Byte to FLASH
|
||||
|
||||
This performs all operations necessary to program an 8 bit byte to FLASH memory.
|
||||
The program error flag should be checked separately for the event that memory
|
||||
was not properly erased.
|
||||
|
||||
@param[in] uint32_t address
|
||||
@param[in] uint8_t data.
|
||||
*/
|
||||
|
||||
void flash_program_byte(uint32_t address, uint8_t data)
|
||||
{
|
||||
flash_wait_for_last_operation();
|
||||
@@ -172,6 +319,18 @@ void flash_program_byte(uint32_t address, uint8_t data)
|
||||
FLASH_CR &= ~FLASH_CR_PG; /* Disable the PG bit. */
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Program a Data Block to FLASH
|
||||
|
||||
This programs an arbitrary length data block to FLASH memory.
|
||||
The program error flag should be checked separately for the event that memory
|
||||
was not properly erased.
|
||||
|
||||
@param[in] uint32_t address. Starting address in Flash.
|
||||
@param[in] uint8_t *data. Pointer to start of data block.
|
||||
@param[in] uint32_t len. Length of data block.
|
||||
*/
|
||||
|
||||
void flash_program(uint32_t address, uint8_t *data, uint32_t len)
|
||||
{
|
||||
/* TODO: Use dword and word size program operations where possible for
|
||||
@@ -183,6 +342,19 @@ void flash_program(uint32_t address, uint8_t *data, uint32_t len)
|
||||
}
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Erase a Sector of FLASH
|
||||
|
||||
This performs all operations necessary to erase a sector in FLASH memory.
|
||||
The page should be checked to ensure that it was properly erased. A sector must
|
||||
first be fully erased before attempting to program it.
|
||||
|
||||
See the reference manual or the FLASH programming manual for details.
|
||||
|
||||
@param[in] uint32_t sector (0 - 11).
|
||||
@param program_size: 0 (8-bit), 1 (16-bit), 2 (32-bit), 3 (64-bit)
|
||||
*/
|
||||
|
||||
void flash_erase_sector(uint8_t sector, uint32_t program_size)
|
||||
{
|
||||
flash_wait_for_last_operation();
|
||||
@@ -198,6 +370,15 @@ void flash_erase_sector(uint8_t sector, uint32_t program_size)
|
||||
FLASH_CR &= ~(0xF << 3);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Erase All FLASH
|
||||
|
||||
This performs all operations necessary to erase all sectors in the FLASH
|
||||
memory.
|
||||
|
||||
@param program_size: 0 (8-bit), 1 (16-bit), 2 (32-bit), 3 (64-bit)
|
||||
*/
|
||||
|
||||
void flash_erase_all_sectors(uint32_t program_size)
|
||||
{
|
||||
flash_wait_for_last_operation();
|
||||
@@ -210,6 +391,16 @@ void flash_erase_all_sectors(uint32_t program_size)
|
||||
FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/** @brief Program the Option Bytes
|
||||
|
||||
This performs all operations necessary to program the option bytes.
|
||||
The write protect error flag should be checked separately for the event that
|
||||
an option byte was not properly erased.
|
||||
|
||||
@param[in] uint32_t data to be programmed.
|
||||
*/
|
||||
|
||||
void flash_program_option_bytes(uint32_t data)
|
||||
{
|
||||
flash_wait_for_last_operation();
|
||||
@@ -222,3 +413,5 @@ void flash_program_option_bytes(uint32_t data)
|
||||
FLASH_OPTCR |= FLASH_OPTCR_OPTSTRT; /* Enable option byte prog. */
|
||||
flash_wait_for_last_operation();
|
||||
}
|
||||
/**@}*/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user