Documentation added to flash modules for all STM32 families.

This commit is contained in:
Ken Sarkies 2014-01-16 05:49:57 +10:30 committed by Frantisek Burian
parent e4f4845e0c
commit a54a12e1c9
16 changed files with 847 additions and 8 deletions

View File

@ -1,3 +1,11 @@
/** @addtogroup flash_defines
*
* @author @htmlonly © @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
*
*/
/*
* This file is part of the libopencm3 project.
*
@ -30,6 +38,7 @@
/** @endcond */
#ifndef LIBOPENCM3_FLASH_COMMON_F234_H
#define LIBOPENCM3_FLASH_COMMON_F234_H
/**@{*/
#include <libopencm3/cm3/common.h>
@ -75,6 +84,7 @@ void flash_clear_status_flags(void);
void flash_wait_for_last_operation(void);
END_DECLS
/**@}*/
#endif
/** @cond */

View File

@ -1,3 +1,11 @@
/** @addtogroup flash_defines
*
* @author @htmlonly &copy; @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
*
*/
/*
* This file is part of the libopencm3 project.
*
@ -30,6 +38,7 @@
/** @endcond */
#ifndef LIBOPENCM3_FLASH_COMMON_F24_H
#define LIBOPENCM3_FLASH_COMMON_F24_H
/**@{*/
#include <libopencm3/cm3/common.h>
#include <libopencm3/stm32/common/flash_common_f234.h>
@ -128,6 +137,7 @@ void flash_program(uint32_t address, uint8_t *data, uint32_t len);
void flash_program_option_bytes(uint32_t data);
END_DECLS
/**@}*/
#endif
/** @cond */

View File

@ -6,7 +6,10 @@
*
* @version 1.0.0
*
* @date 11 July 2013
* @author @htmlonly &copy; @endhtmlonly 2013
* Frantisek Burian <BuFran@seznam.cz>
*
* @date 14 January 2014
*
* LGPL License Terms @ref lgpl_license
*/
@ -31,6 +34,7 @@
#ifndef LIBOPENCM3_FLASH_H
#define LIBOPENCM3_FLASH_H
/**@{*/
#include <libopencm3/stm32/memorymap.h>
#include <libopencm3/cm3/common.h>
@ -137,5 +141,6 @@ void flash_erase_page(uint32_t page_address);
void flash_erase_all_pages(void);
END_DECLS
/**@}*/
#endif

View File

@ -1,3 +1,16 @@
/** @defgroup flash_defines FLASH Defines
*
* @ingroup STM32F1xx_defines
*
* @brief Defined Constants and Types for the STM32F1xx FLASH Memory
*
* @version 1.0.0
*
* @date 14 January 2014
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
@ -28,6 +41,8 @@
#ifndef LIBOPENCM3_FLASH_H
#define LIBOPENCM3_FLASH_H
/**@{*/
#include <libopencm3/stm32/memorymap.h>
#include <libopencm3/cm3/common.h>
@ -76,14 +91,14 @@
/* FLASH_OBR[25:18]: Data1 */
/* FLASH_OBR[17:10]: Data0 */
#define FLASH_OBR_NRST_STDBY (1 << 4)
#define FLASH_OBR_NRST_STOP (1 << 3)
#define FLASH_OBR_WDG_SW (1 << 2)
#define FLASH_OBR_NRST_STOP (1 << 3)
#define FLASH_OBR_WDG_SW (1 << 2)
#define FLASH_OBR_RDPRT (1 << 1)
#define FLASH_OBR_OPTERR (1 << 0)
#define FLASH_OBR_OPTERR (1 << 0)
/* --- FLASH Keys -----------------------------------------------------------*/
#define FLASH_RDP_KEY ((uint16_t)0x00a5)
#define FLASH_RDP_KEY ((uint16_t)0x00a5)
#define FLASH_KEYR_KEY1 ((uint32_t)0x45670123)
#define FLASH_KEYR_KEY2 ((uint32_t)0xcdef89ab)
@ -115,4 +130,6 @@ void flash_program_option_bytes(uint32_t address, uint16_t data);
END_DECLS
/**@}*/
#endif

View File

@ -1,3 +1,16 @@
/** @defgroup flash_defines FLASH Defines
*
* @ingroup STM32F2xx_defines
*
* @brief Defined Constants and Types for the STM32F2xx FLASH Memory
*
* @version 1.0.0
*
* @date 14 January 2014
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*

View File

@ -30,6 +30,7 @@
#ifndef LIBOPENCM3_FLASH_H
#define LIBOPENCM3_FLASH_H
/**@{*/
#include <libopencm3/stm32/memorymap.h>
#include <libopencm3/stm32/common/flash_common_f234.h>
@ -67,6 +68,7 @@
#define FLASH_CR_MER (1 << 2)
#define FLASH_CR_PER (1 << 1)
#define FLASH_CR_PG (1 << 0)
/**@}*/
#endif

View File

@ -1,3 +1,16 @@
/** @defgroup flash_defines FLASH Defines
*
* @ingroup STM32F4xx_defines
*
* @brief Defined Constants and Types for the STM32F4xx FLASH Memory
*
* @version 1.0.0
*
* @date 14 January 2014
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*

View File

@ -1,3 +1,19 @@
/** @defgroup flash_defines FLASH Defines
*
* @ingroup STM32L1xx_defines
*
* @brief Defined Constants and Types for the STM32L1xx FLASH Memory
*
* @version 1.0.0
*
* @author @htmlonly &copy; @endhtmlonly 2012
* Karl Palsson <karlp@tweak.net.au>
*
* @date 14 January 2014
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
@ -26,6 +42,7 @@
#ifndef LIBOPENCM3_FLASH_H
#define LIBOPENCM3_FLASH_H
/**@{*/
#include <libopencm3/stm32/memorymap.h>
#include <libopencm3/cm3/common.h>
@ -122,5 +139,6 @@ void flash_prefetch_disable(void);
void flash_set_ws(uint32_t ws);
END_DECLS
/**@}*/
#endif

View File

@ -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);
}
/**@}*/

View File

@ -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();
}
/**@}*/

View File

@ -1,3 +1,22 @@
/** @defgroup flash_file FLASH
*
* @ingroup STM32F0xx
*
* @brief <b>libopencm3 STM32F05x FLASH</b>
*
* @version 1.0.0
*
* @author @htmlonly &copy; @endhtmlonly 2013
* Frantisek Burian <BuFran@seznam.cz>
*
* @date 14 January 2014
*
* For the STM32F05x, accessing FLASH memory is described in
* section 3 of the STM32F05x Reference Manual.
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
@ -17,28 +36,77 @@
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**@{*/
#include <libopencm3/stm32/flash.h>
/*---------------------------------------------------------------------------*/
/** @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_buffer_enable(void)
{
FLASH_ACR |= FLASH_ACR_PRFTBE;
}
/*---------------------------------------------------------------------------*/
/** @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_buffer_disable(void)
{
FLASH_ACR &= ~FLASH_ACR_PRFTBE;
}
/*---------------------------------------------------------------------------*/
/** @brief Set the Number of Wait States
Used to match the system clock to the FLASH memory access time. See the
reference manual for more information on clock speed ranges for each wait state.
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 or 1 only.
*/
void flash_set_ws(uint32_t ws)
{
FLASH_ACR = (FLASH_ACR & ~FLASH_ACR_LATENCY) | ws;
}
/*---------------------------------------------------------------------------*/
/** @brief Wait for Busy Flag
This loops indefinitely until the busy flag is clear.
*/
void flash_wait_busy(void)
{
while ((FLASH_SR & FLASH_SR_BSY) != 0);
}
/*---------------------------------------------------------------------------*/
/** @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. Full address of flash word to be programmed.
@param[in] uint32_t data.
*/
void flash_program_u32(uint32_t address, uint32_t data)
{
flash_wait_busy();
@ -56,6 +124,17 @@ void flash_program_u32(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 Full address of flash half word to be programmed.
@param[in] uint16_t data.
*/
void flash_program_u16(uint32_t address, uint16_t data)
{
flash_wait_busy();
@ -69,6 +148,19 @@ void flash_program_u16(uint32_t address, uint16_t data)
FLASH_CR &= ~FLASH_CR_PG;
}
/*---------------------------------------------------------------------------*/
/** @brief Erase a Page of FLASH
This performs all operations necessary to erase a page in FLASH memory.
The page should be checked to ensure that it was properly erased. A page must
first be fully erased before attempting to program it.
Note that the page sizes differ between devices. See the reference manual or
the FLASH programming manual for details.
@param[in] uint32_t page_address. Full address of flash page to be erased.
*/
void flash_erase_page(uint32_t page_address)
{
flash_wait_busy();
@ -81,6 +173,13 @@ void flash_erase_page(uint32_t page_address)
FLASH_CR &= ~FLASH_CR_PER;
}
/*---------------------------------------------------------------------------*/
/** @brief Erase All FLASH
This performs all operations necessary to erase all user pages in the FLASH
memory. The information block is unaffected.
*/
void flash_erase_all_pages(void)
{
flash_wait_busy();
@ -91,3 +190,5 @@ void flash_erase_all_pages(void)
flash_wait_busy();
FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
}
/**@}*/

View File

@ -1,3 +1,32 @@
/** @defgroup flash_file FLASH
*
* @ingroup STM32F1xx
*
* @brief <b>libopencm3 STM32F1xx FLASH Memory</b>
*
* @version 1.0.0
*
* @author @htmlonly &copy; @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
*
* @date 14 January 2014
*
* For the STM32F1xx, accessing FLASH memory is described briefly in
* section 3.3.3 of the STM32F10x Reference Manual.
* For detailed programming information see:
* PM0075 programming manual: STM32F10xxx Flash programming
* August 2010, Doc ID 17863 Rev 1
* https://github.com/libopencm3/libopencm3-archive/blob/master/st_micro/CD00283419.pdf
*
* FLASH memory may be used for data storage as well as code, and may be
* programmatically modified. Note that for firmware upload the STM32F1xx
* provides a built-in bootloader in system memory that can be entered from a
* running program.
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
@ -18,28 +47,77 @@
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/stm32/f1/flash.h>
/**@{*/
#include <libopencm3/stm32/flash.h>
/*---------------------------------------------------------------------------*/
/** @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_buffer_enable(void)
{
FLASH_ACR |= FLASH_ACR_PRFTBE;
}
/*---------------------------------------------------------------------------*/
/** @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_buffer_disable(void)
{
FLASH_ACR &= ~FLASH_ACR_PRFTBE;
}
/*---------------------------------------------------------------------------*/
/** @brief Enable the FLASH Half Cycle Mode
This mode is used for power saving during read access. It is disabled by default
on reset.
Note carefully the clock restrictions under which the half cycle mode may be
enabled or disabled. This mode may only be used while the clock is running at
8MHz. See the reference manual for details.
*/
void flash_halfcycle_enable(void)
{
FLASH_ACR |= FLASH_ACR_HLFCYA;
}
/*---------------------------------------------------------------------------*/
/** @brief Disable the FLASH Half Cycle Mode
*/
void flash_halfcycle_disable(void)
{
FLASH_ACR &= ~FLASH_ACR_HLFCYA;
}
/*---------------------------------------------------------------------------*/
/** @brief Set the Number of Wait States
Used to match the system clock to the FLASH memory access time. See the
reference manual for more information on clock speed ranges for each wait state.
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, 1 or 2 only.
*/
void flash_set_ws(uint32_t ws)
{
uint32_t reg32;
@ -50,6 +128,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 state. */
@ -60,31 +145,63 @@ 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_pgerr_flag(void)
{
FLASH_SR |= FLASH_SR_PGERR;
}
/*---------------------------------------------------------------------------*/
/** @brief Clear the End of Operation Status Flag
*/
void flash_clear_eop_flag(void)
{
FLASH_SR |= FLASH_SR_EOP;
}
/*---------------------------------------------------------------------------*/
/** @brief Clear the Write Protect Error Status Flag
*/
void flash_clear_wrprterr_flag(void)
{
FLASH_SR |= FLASH_SR_WRPRTERR;
}
/*---------------------------------------------------------------------------*/
/** @brief Clear the Busy Status Flag
*/
void flash_clear_bsy_flag(void)
{
FLASH_SR &= ~FLASH_SR_BSY;
}
/*---------------------------------------------------------------------------*/
/** @brief Clear All Status Flags
Program error, end of operation, write protect error, busy.
*/
void flash_clear_status_flags(void)
{
flash_clear_pgerr_flag();
@ -93,6 +210,16 @@ void flash_clear_status_flags(void)
flash_clear_bsy_flag();
}
/*---------------------------------------------------------------------------*/
/** @brief Read All Status Flags
The programming error, end of operation, write protect error and busy flags
are returned in the order of appearance in the status register.
@returns uint32_t. bit 0: busy, bit 2: programming error, bit 4: write protect
error, bit 5: end of operation.
*/
uint32_t flash_get_status_flags(void)
{
return FLASH_SR &= (FLASH_SR_PGERR |
@ -101,6 +228,13 @@ uint32_t flash_get_status_flags(void)
FLASH_SR_BSY);
}
/*---------------------------------------------------------------------------*/
/** @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)
{
/* F1 uses same keys for flash and option */
@ -108,11 +242,29 @@ void flash_unlock_option_bytes(void)
FLASH_OPTKEYR = FLASH_KEYR_KEY2;
}
/*---------------------------------------------------------------------------*/
/** @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);
}
/*---------------------------------------------------------------------------*/
/** @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. Full address of flash word to be programmed.
@param[in] uint32_t data.
*/
void flash_program_word(uint32_t address, uint32_t data)
{
/* Ensure that all flash operations are complete. */
@ -137,6 +289,17 @@ void flash_program_word(uint32_t address, uint32_t data)
flash_wait_for_last_operation();
}
/*---------------------------------------------------------------------------*/
/** @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. Full address of flash half word to be programmed.
@param[in] uint16_t data.
*/
void flash_program_half_word(uint32_t address, uint16_t data)
{
flash_wait_for_last_operation();
@ -148,6 +311,19 @@ void flash_program_half_word(uint32_t address, uint16_t data)
flash_wait_for_last_operation();
}
/*---------------------------------------------------------------------------*/
/** @brief Erase a Page of FLASH
This performs all operations necessary to erase a page in FLASH memory.
The page should be checked to ensure that it was properly erased. A page must
first be fully erased before attempting to program it.
Note that the page sizes differ between devices. See the reference manual or
the FLASH programming manual for details.
@param[in] uint32_t page_address. Full address of flash psge to be erased.
*/
void flash_erase_page(uint32_t page_address)
{
flash_wait_for_last_operation();
@ -160,6 +336,13 @@ void flash_erase_page(uint32_t page_address)
FLASH_CR &= ~FLASH_CR_PER;
}
/*---------------------------------------------------------------------------*/
/** @brief Erase All FLASH
This performs all operations necessary to erase all user pages in the FLASH
memory. The information block is unaffected.
*/
void flash_erase_all_pages(void)
{
flash_wait_for_last_operation();
@ -171,6 +354,13 @@ void flash_erase_all_pages(void)
FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
}
/*---------------------------------------------------------------------------*/
/** @brief Erase All Option Bytes
This performs all operations necessary to erase the option bytes. These must
first be fully erased before attempting to program them.
*/
void flash_erase_option_bytes(void)
{
flash_wait_for_last_operation();
@ -185,6 +375,17 @@ void flash_erase_option_bytes(void)
FLASH_CR &= ~FLASH_CR_OPTER; /* Disable option byte 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 address. Full address of option byte to program.
@param[in] uint16_t data.
*/
void flash_program_option_bytes(uint32_t address, uint16_t data)
{
flash_wait_for_last_operation();
@ -198,3 +399,5 @@ void flash_program_option_bytes(uint32_t address, uint16_t data)
flash_wait_for_last_operation();
FLASH_CR &= ~FLASH_CR_OPTPG; /* Disable option byte programming. */
}
/**@}*/

53
lib/stm32/f2/flash.c Normal file
View File

@ -0,0 +1,53 @@
/** @defgroup flash_file FLASH
*
* @ingroup STM32F2xx
*
* @brief <b>libopencm3 STM32F2xx FLASH</b>
*
* @version 1.0.0
*
* @author @htmlonly &copy; @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
*
* @date 14 January 2014
*
* This library supports the FLASH memory controller in the STM32F2
* series of ARM Cortex Microcontrollers by ST Microelectronics.
*
* For the STM32F2xx, accessing FLASH memory is described briefly in
* section 2.3.3 of the STM32F2xx Reference Manual.
* For detailed programming information see:
* PM0059 programming manual: STM32F10xxx Flash programming
* June 2013, Doc ID DocID15687 Rev 5
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
* Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**@{*/
#include <libopencm3/stm32/flash.h>
/**@}*/

View File

@ -6,10 +6,22 @@
*
* @version 1.0.0
*
* @date 11 July 2013
* @author @htmlonly &copy; @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
*
* @date 14 January 2014
*
* This library supports the FLASH memory controller in the STM32F3
* series of ARM Cortex Microcontrollers by ST Microelectronics.
*
* For the STM32F3xx, accessing FLASH memory is described in
* section 3 of the STM32F3xx Reference Manual.
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
@ -30,11 +42,21 @@
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**@{*/
#include <libopencm3/stm32/flash.h>
/*---------------------------------------------------------------------------*/
/** @brief Clear All Status Flags
Clears program error, end of operation, busy flags.
*/
void flash_clear_status_flags(void)
{
flash_clear_pgperr_flag();
flash_clear_eop_flag();
flash_clear_bsy_flag();
}
/**@}*/

53
lib/stm32/f4/flash.c Normal file
View File

@ -0,0 +1,53 @@
/** @defgroup flash_file FLASH
*
* @ingroup STM32F4xx
*
* @brief <b>libopencm3 STM32F4xx FLASH</b>
*
* @version 1.0.0
*
* @author @htmlonly &copy; @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
*
* @date 14 January 2014
*
* This library supports the FLASH memory controller in the STM32F4
* series of ARM Cortex Microcontrollers by ST Microelectronics.
*
* For the STM32F4xx, accessing FLASH memory is described briefly in
* section 2.3.3 of the STM32F4xx Reference Manual.
* For detailed programming information see:
* PM0081 programming manual: STM32F10xxx Flash programming
* September 2011, Doc ID 018520 Rev 1
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
* Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/**@{*/
#include <libopencm3/stm32/flash.h>
/**@}*/

View File

@ -1,3 +1,29 @@
/** @defgroup flash_file FLASH
*
* @ingroup STM32L1xx
*
* @brief <b>libopencm3 STM32L1xx FLASH</b>
*
* @version 1.0.0
*
* @author @htmlonly &copy; @endhtmlonly 2010
* Thomas Otto <tommi@viadmin.org>
* @author @htmlonly &copy; @endhtmlonly 2010
* Mark Butler <mbutler@physics.otago.ac.nz>
* @author @htmlonly &copy; @endhtmlonly 2012
* Karl Palsson <karlp@tweak.net.au>
*
* @date 14 January 2014
*
* For the STM32L1xx, accessing FLASH memory is described briefly in
* section 2.3.3 of the STM32L1xx Reference Manual.
* For detailed programming information see:
* PM0062 programming manual: STM32L1xxxx Flash and EEPROM programming
* March 2012, Doc ID 16024 Rev 5
*
* LGPL License Terms @ref lgpl_license
*/
/*
* This file is part of the libopencm3 project.
*
@ -19,28 +45,72 @@
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/stm32/l1/flash.h>
/**@{*/
#include <libopencm3/stm32/flash.h>
/*---------------------------------------------------------------------------*/
/** @brief Enable 64 Bit Programming Mode
*/
void flash_64bit_enable(void)
{
FLASH_ACR |= FLASH_ACR_ACC64;
}
/*---------------------------------------------------------------------------*/
/** @brief Enable 32 Bit Programming Mode
This mode is a low power mode. It must be used at low frequencies and does not
allow prefetch or wait states to be used.
*/
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] uint32_t ws: values 0 or 1 only.
*/
void flash_set_ws(uint32_t ws)
{
uint32_t reg32;
@ -50,3 +120,5 @@ void flash_set_ws(uint32_t ws)
reg32 |= ws;
FLASH_ACR = reg32;
}
/**@}*/