EFM32 Gecko Software Documentation  efm32g-doc-5.1.2

Detailed Description

Memory System Controller API.

This module contain functions to control the MSC, primarily the Flash. The user can perform Flash memory write and erase operations as well as optimization of the CPU instruction fetch interface for the application. Available instruction fetch features depends on the MCU or SoC family, but features such as instruction pre-fetch, cache and configurable branch prediction are typically available.

Note
Flash wait-state configuration is handled by the CMU module. When the core clock configuration is changed by a calls to functions such as CMU_ClockSelectSet() or CMU_HFRCOBandSet(), then Flash wait-state configuration is also updated.

The MSC resets into a safe state. To initialize the instruction interface to recommended settings:

Note
The optimal configuration is highly application dependent. Performance benchmarking is supported by most families. See MSC_StartCacheMeasurement() and MSC_GetCacheMeasurement() for more details.

Support for Flash write and erase runs from RAM by default. This code may be allocated to Flash by defining EM_MSC_RUN_FROM_FLASH.

Note
Flash erase may add ms of delay to interrupt latency if executing from Flash.

Flash write and erase operations are supported by MSC_WriteWord(), MSC_WriteWordFast(), MSC_ErasePage() and MSC_MassErase(). Fast write is not supported for EFM32G and mass erase is supported for MCU and SoC families with larger Flash sizes.

Note
MSC_Init() must be called prior to any Flash write or erase operation.

The following steps are necessary to perform a page erase and write:

uint32_t * userDataPage = (uint32_t *) USERDATA_BASE;
uint32_t userData[] = {
0x01020304,
0x05060708
};
MSC_ErasePage(userDataPage);
MSC_WriteWord(userDataPage, userData, sizeof(userData));

Data Structures

struct  MSC_ExecConfig_TypeDef
 

Macros

#define EM_MSC_RUN_FROM_FLASH
 By compiling with the define EM_MSC_RUN_FROM_FLASH, the functions performing erase or write operations will remain in and execute from Flash. This is useful for targets that don't want to allocate RAM space to hold the flash functions. Without this define, code for Flash operations will be copied into RAM at startup. More...
 
#define MSC_EXECCONFIG_DEFAULT
 
#define MSC_PROGRAM_TIMEOUT   10000000ul
 The timeout used while waiting for the flash to become ready after a write. This number indicates the number of iterations to perform before issuing a timeout. More...
 
#define MSC_RAMFUNC_DECLARATOR
 Expands to SL_RAMFUNC_DECLARATOR if EM_MSC_RUN_FROM_FLASH is undefined and to nothing if EM_MSC_RUN_FROM_FLASH is defined.
 
#define MSC_RAMFUNC_DEFINITION_BEGIN
 Expands to SL_RAMFUNC_DEFINITION_BEGIN if EM_MSC_RUN_FROM_FLASH is undefined and to nothing if EM_MSC_RUN_FROM_FLASH is defined.
 
#define MSC_RAMFUNC_DEFINITION_END
 Expands to SL_RAMFUNC_DEFINITION_END if EM_MSC_RUN_FROM_FLASH is undefined and to nothing if EM_MSC_RUN_FROM_FLASH is defined.
 

Enumerations

enum  MSC_Status_TypeDef {
  mscReturnOk = 0,
  mscReturnInvalidAddr = -1,
  mscReturnLocked = -2,
  mscReturnTimeOut = -3,
  mscReturnUnaligned = -4
}
 

Functions

void MSC_Deinit (void)
 Disables the flash controller for writing.
 
MSC_RAMFUNC_DECLARATOR MSC_Status_TypeDef MSC_ErasePage (uint32_t *startAddress)
 Erases a page in flash memory. More...
 
void MSC_ExecConfigSet (MSC_ExecConfig_TypeDef *execConfig)
 Set MSC code execution configuration. More...
 
void MSC_Init (void)
 Enables the flash controller for writing. More...
 
__STATIC_INLINE void MSC_IntClear (uint32_t flags)
 Clear one or more pending MSC interrupts. More...
 
__STATIC_INLINE void MSC_IntDisable (uint32_t flags)
 Disable one or more MSC interrupts. More...
 
__STATIC_INLINE void MSC_IntEnable (uint32_t flags)
 Enable one or more MSC interrupts. More...
 
__STATIC_INLINE uint32_t MSC_IntGet (void)
 Get pending MSC interrupt flags. More...
 
__STATIC_INLINE uint32_t MSC_IntGetEnabled (void)
 Get enabled and pending MSC interrupt flags. Useful for handling more interrupt sources in the same interrupt handler. More...
 
__STATIC_INLINE void MSC_IntSet (uint32_t flags)
 Set one or more pending MSC interrupts from SW. More...
 
MSC_RAMFUNC_DECLARATOR MSC_Status_TypeDef MSC_WriteWord (uint32_t *address, void const *data, uint32_t numBytes)
 Writes data to flash memory. This function is interrupt safe, but slower than MSC_WriteWordFast(), which writes to flash with interrupts disabled. Write data must be aligned to words and contain a number of bytes that is divisable by four. More...
 

Macro Definition Documentation

#define EM_MSC_RUN_FROM_FLASH

By compiling with the define EM_MSC_RUN_FROM_FLASH, the functions performing erase or write operations will remain in and execute from Flash. This is useful for targets that don't want to allocate RAM space to hold the flash functions. Without this define, code for Flash operations will be copied into RAM at startup.

Note
This define is not present by default. The MSC controller API will run from RAM by default.

Definition at line 125 of file em_msc.h.

#define MSC_EXECCONFIG_DEFAULT
Value:
{ \
false, \
true, \
false, \
false, \
false, \
false, \
}

Default MSC ExecConfig initialization

Definition at line 166 of file em_msc.h.

#define MSC_PROGRAM_TIMEOUT   10000000ul

The timeout used while waiting for the flash to become ready after a write. This number indicates the number of iterations to perform before issuing a timeout.

Note
This timeout is set very large (in the order of 100x longer than necessary). This is to avoid any corner cases.

Definition at line 110 of file em_msc.h.

Referenced by MSC_ErasePage().

Enumeration Type Documentation

Return codes for writing/erasing the flash

Enumerator
mscReturnOk 

Flash write/erase successful.

mscReturnInvalidAddr 

Invalid address. Write to an address that is not flash.

mscReturnLocked 

Flash address is locked.

mscReturnTimeOut 

Timeout while writing to flash.

mscReturnUnaligned 

Unaligned access to flash.

Definition at line 133 of file em_msc.h.

Function Documentation

MSC_RAMFUNC_DEFINITION_BEGIN MSC_Status_TypeDef MSC_ErasePage ( uint32_t *  startAddress)

Erases a page in flash memory.

Note
It is recommended to run this code from RAM. On the Gecko family, it is required to run this function from RAM.

For IAR, Rowley, SimplicityStudio, Atollic and armgcc this will be achieved automatically by using attributes in the function proctype. For Keil uVision you must define a section called "ram_code" and place this manually in your project's scatter file.

Parameters
[in]startAddressPointer to the flash page to erase. Must be aligned to beginning of page boundary.
Returns
Returns the status of erase operation, MSC_Status_TypeDef
*   mscReturnOk - Operation completed successfully.
*   mscReturnInvalidAddr - Operation tried to erase a non-flash area.
*   mscReturnLocked - Operation tried to erase a locked area of the flash.
*   mscReturnTimeOut - Operation timed out waiting for flash operation
*       to complete.
* 

Definition at line 748 of file em_msc.c.

References EMU, FLASH_PAGE_SIZE, MSC, MSC_PROGRAM_TIMEOUT, MSC_STATUS_BUSY, MSC_STATUS_INVADDR, MSC_STATUS_LOCKED, MSC_WRITECMD_ERASEPAGE, MSC_WRITECMD_LADDRIM, MSC_WRITECTRL_WREN, mscReturnInvalidAddr, mscReturnLocked, mscReturnOk, and mscReturnTimeOut.

Referenced by BSP_TraceProfilerEnable(), and NVMHAL_PageErase().

void MSC_Init ( void  )

Enables the flash controller for writing.

Note
This function must be called before flash operations when AUXHFRCO clock has been changed from default band.
This function calls SystemCoreClockGet in order to set the global variable SystemCoreClock which is used in subseqent calls of MSC_WriteWord to make sure the frequency is sufficiently high for flash operations. If the clock frequency is changed then software is responsible for calling MSC_Init or SystemCoreClockGet in order to set the SystemCoreClock variable to the correct value.

Definition at line 157 of file em_msc.c.

References CMU_ClockFreqGet(), cmuClock_AUX, EMU, MSC, MSC_UNLOCK_CODE, MSC_WRITECTRL_WREN, and SystemCoreClockGet().

Referenced by BSP_TraceProfilerEnable(), and NVMHAL_Init().

__STATIC_INLINE void MSC_IntClear ( uint32_t  flags)

Clear one or more pending MSC interrupts.

Parameters
[in]flagsPending MSC intterupt source to clear. Use a bitwise logic OR combination of valid interrupt flags for the MSC module (MSC_IF_nnn).

Definition at line 191 of file em_msc.h.

References MSC.

__STATIC_INLINE void MSC_IntDisable ( uint32_t  flags)

Disable one or more MSC interrupts.

Parameters
[in]flagsMSC interrupt sources to disable. Use a bitwise logic OR combination of valid interrupt flags for the MSC module (MSC_IF_nnn).

Definition at line 204 of file em_msc.h.

References MSC.

__STATIC_INLINE void MSC_IntEnable ( uint32_t  flags)

Enable one or more MSC interrupts.

Note
Depending on the use, a pending interrupt may already be set prior to enabling the interrupt. Consider using MSC_IntClear() prior to enabling if such a pending interrupt should be ignored.
Parameters
[in]flagsMSC interrupt sources to enable. Use a bitwise logic OR combination of valid interrupt flags for the MSC module (MSC_IF_nnn).

Definition at line 223 of file em_msc.h.

References MSC.

__STATIC_INLINE uint32_t MSC_IntGet ( void  )

Get pending MSC interrupt flags.

Note
The event bits are not cleared by the use of this function.
Returns
MSC interrupt sources pending. A bitwise logic OR combination of valid interrupt flags for the MSC module (MSC_IF_nnn).

Definition at line 240 of file em_msc.h.

References MSC.

__STATIC_INLINE uint32_t MSC_IntGetEnabled ( void  )

Get enabled and pending MSC interrupt flags. Useful for handling more interrupt sources in the same interrupt handler.

Note
Interrupt flags are not cleared by the use of this function.
Returns
Pending and enabled MSC interrupt sources The return value is the bitwise AND of
  • the enabled interrupt sources in MSC_IEN and
  • the pending interrupt flags MSC_IF

Definition at line 260 of file em_msc.h.

References MSC.

__STATIC_INLINE void MSC_IntSet ( uint32_t  flags)

Set one or more pending MSC interrupts from SW.

Parameters
[in]flagsMSC interrupt sources to set to pending. Use a bitwise logic OR combination of valid interrupt flags for the MSC module (MSC_IF_nnn).

Definition at line 277 of file em_msc.h.

References MSC.

MSC_RAMFUNC_DEFINITION_END MSC_RAMFUNC_DEFINITION_BEGIN MSC_Status_TypeDef MSC_WriteWord ( uint32_t *  address,
void const *  data,
uint32_t  numBytes 
)

Writes data to flash memory. This function is interrupt safe, but slower than MSC_WriteWordFast(), which writes to flash with interrupts disabled. Write data must be aligned to words and contain a number of bytes that is divisable by four.

Note
It is recommended to erase the flash page before performing a write.

It is recommended to run this code from RAM. On the Gecko family, it is required to run this function from RAM.

For IAR, Rowley, SimplicityStudio, Atollic and armgcc this will be achieved automatically by using attributes in the function proctype. For Keil uVision you must define a section called "ram_code" and place this manually in your project's scatter file.

This function requires a system core clock at 1MHz or higher.

Parameters
[in]addressPointer to the flash word to write to. Must be aligned to words.
[in]dataData to write to flash.
[in]numBytesNumber of bytes to write from flash. NB: Must be divisable by four.
Returns
Returns the status of the write operation
*   flashReturnOk - Operation completed successfully.
*   flashReturnInvalidAddr - Operation tried to erase a non-flash area.
*   flashReturnLocked - Operation tried to erase a locked area of the flash.
*   flashReturnTimeOut - Operation timed out waiting for flash operation
*       to complete. Or the MSC timed out waiting for the software to write
*       the next word into the DWORD register.
* 

Definition at line 839 of file em_msc.c.

Referenced by BSP_TraceProfilerEnable(), and NVMHAL_Write().