MCUXpresso SDK API Reference Manual  Rev. 0
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
FLASH: flash driver

Overview

The MCUXpresso SDK provides a peripheral driver for the flash driver module of MCUXpresso SDK devices.

Typical use case

Example use of FLASH API.

int main(void)
{
bool pass = true;
status_t result = 0;
uint32_t i = 0;
uint8_t data_buf[TEST_MEM_SIZE];
/* Board pin, clock, debug console init */
BOARD_InitHardware();
/* Configure Flash */
/*Flash module init*/
FLASH_Init(DEMO_FLASH_BASE, &config);
PRINTF("FLASH_Init\r\n");
/* erase test page*/
result = FLASH_PageErase(DEMO_FLASH_BASE, &config, FLASH_ADDR_TO_PAGE(TEST_START_ADDR));
if (result)
{
PRINTF("FLASH_PageErase error result = %d \r\n", result);
while (1)
;
}
/*set test data */
for (i = 0; i < TEST_MEM_SIZE; i++)
{
data_buf[i] = (uint8_t)i;
}
result = FLASH_Program(DEMO_FLASH_BASE, &config, TEST_START_ADDR, (uint8_t *)&data_buf[0], TEST_MEM_SIZE - 1);
if (result)
{
PRINTF("FLASH_Program error result = %d \r\n", result);
while (1)
;
}
if (memcmp((void *)TEST_START_ADDR, (uint8_t *)&data_buf[0], TEST_MEM_SIZE - 1))
{
pass = false;
}
/*show result*/
if (pass)
{
PRINTF("Passed!\r\n");
}
else
{
PRINTF("Failed!\r\n");
}
while (1)
;
}

Files

file  fsl_flash.h
 

Data Structures

struct  flash_config_t
 Flash configuration structure. More...
 
struct  flash_lock_bit_t
 Flash lock bit Structure definition. More...
 

Macros

#define FLASH_TIME_BASE(clk)
 Time base of FLASH timing, 2us: 64 cycles in 32 MHz/32 cycles in 16 MHz/16 cycles in 8 MHz.
 
#define FLASH_ERASE_TIME_BASE   16000
 Time base of FLASH timing, 2ms: 16000 cycles in 8 MHz(internal fixed clock)
 
#define FLASH_PROG_CYCLE   30
 Program times in normal write mode.
 
#define FLASH_ADDR_TO_PAGE(addr)   ((uint8_t)((addr) >> 11))
 FLASH address convert to page.
 

Enumerations

enum  flash_status_t {
  kStatus_FLASH_Success = MAKE_STATUS(kStatusGroup_Generic, 0),
  kStatus_FLASH_Fail = MAKE_STATUS(kStatusGroup_Generic, 1),
  kStatus_FLASH_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4),
  kStatus_FLASH_AddressError = MAKE_STATUS(kStatusGroup_FLASH, 0),
  kStatus_FLASH_EraseError = MAKE_STATUS(kStatusGroup_FLASH, 1),
  kStatus_FLASH_WriteError = MAKE_STATUS(kStatusGroup_FLASH, 2),
  kStatus_FLASH_ProtectionViolation,
  kStatus_FLASH_AHBError = MAKE_STATUS(kStatusGroup_FLASH, 4),
  kStatus_FLASH_Busy = MAKE_STATUS(kStatusGroup_FLASH, 5),
  kStatus_FLASH_WriteDmaIdle = MAKE_STATUS(kStatusGroup_FLASH, 6),
  kStatus_FLASH_ReadDmaIdle = MAKE_STATUS(kStatusGroup_FLASH, 7)
}
 
enum  flash_block_t {
  kFLASH_Block0 = 1U,
  kFLASH_Block1 = 2U
}
 

Functions

static uint32_t FLASH_GetStatusFlags (void)
 Get FLASH status flags. More...
 
static void FLASH_ClearStatusFlags (uint32_t mask)
 Clears status flags with the provided mask. More...
 
static void FLASH_EnableInterrupts (uint32_t mask)
 Enables the FLASH interrupt. More...
 
static void FLASH_DisableInterrupts (uint32_t mask)
 Disables the FLASH interrupt. More...
 
static uint32_t FLASH_GetBusyStatusFlags (void)
 Get FLASH busy status flags. More...
 
void FLASH_GetDefaultConfig (flash_config_t *config)
 Sets the FLASH configuration structure to default values. More...
 
status_t FLASH_Erase (flash_config_t *config, uint32_t start, uint32_t lengthInBytes)
 Erases flash pages encompassed by parameters passed into function. More...
 
status_t FLASH_PageErase (flash_config_t *config, uint8_t pageIdx)
 Erases a specified FLASH page. More...
 
status_t FLASH_BlockErase (flash_config_t *config, uint32_t block)
 Erases a specified FLASH block. More...
 
status_t FLASH_Program (flash_config_t *config, uint32_t start, uint32_t *src, uint32_t lengthInBytes)
 Writes n word data to a specified start address of the FLASH using a polling method. More...
 
status_t FLASH_GetLockBit (flash_config_t *config, flash_lock_bit_t *lockBit)
 Get FLASH page lock bit. More...
 
status_t FLASH_SetLockBit (flash_config_t *config, flash_lock_bit_t *lockBit)
 Set FLASH page lock bit. More...
 

Driver version

#define FSL_FLASH_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))
 FLASH driver version 2.0.0. More...
 

Data Structure Documentation

struct flash_config_t

Data Fields

uint32_t blockBase
 A base address of the first block.
 
uint32_t totalSize
 The total size of Flash. More...
 
uint32_t pageSize
 The size in bytes of a page of Flash. More...
 
uint8_t smartMaxEraseTime
 The maximum number of erase attempts for one Smart Erase operation. More...
 
uint8_t smartMaxWriteTime
 The maximum number of write attempts for one Smart Write operation. More...
 
uint16_t programCycle
 The maximum number of write operations in one flash page program. More...
 
uint32_t timeBase
 The amount of AHB clock cycles equal to 2 microseconds. More...
 
uint32_t eraseTimeBase
 The amount of internal 8 MHz clock cycles to use for the erase time.
 
bool smartWriteEnable
 The smart write configuration.
 

Field Documentation

uint32_t flash_config_t::totalSize
uint32_t flash_config_t::pageSize
uint8_t flash_config_t::smartMaxEraseTime
uint8_t flash_config_t::smartMaxWriteTime
uint16_t flash_config_t::programCycle
uint32_t flash_config_t::timeBase
struct flash_lock_bit_t

Data Fields

uint32_t lockCtrl0
 Flash page lock register0, . More...
 
uint32_t lockCtrl1
 Flash page lock register1. More...
 
uint32_t lockCtrl2
 Flash page lock register2. More...
 
uint32_t lockCtrl3
 Flash page lock register3. More...
 
uint32_t lockCtrl4
 Flash page lock register4, . More...
 
uint32_t lockCtrl5
 Flash page lock register5. More...
 
uint32_t lockCtrl6
 Flash page lock register6. More...
 
uint32_t lockCtrl7
 Flash page lock register7. More...
 
uint32_t lockCtrl8
 Flash page lock register8. More...
 

Field Documentation

uint32_t flash_lock_bit_t::lockCtrl0
uint32_t flash_lock_bit_t::lockCtrl1
uint32_t flash_lock_bit_t::lockCtrl2
uint32_t flash_lock_bit_t::lockCtrl3
uint32_t flash_lock_bit_t::lockCtrl4
uint32_t flash_lock_bit_t::lockCtrl5
uint32_t flash_lock_bit_t::lockCtrl6
uint32_t flash_lock_bit_t::lockCtrl7
uint32_t flash_lock_bit_t::lockCtrl8

Macro Definition Documentation

#define FSL_FLASH_DRIVER_VERSION   (MAKE_VERSION(2, 0, 0))

Enumeration Type Documentation

Enumerator
kStatus_FLASH_Success 

flash operation is successful

kStatus_FLASH_Fail 

flash operation is not successful

kStatus_FLASH_InvalidArgument 

Invalid argument.

kStatus_FLASH_AddressError 

Address is out of range.

kStatus_FLASH_EraseError 

The erase operation is error.

kStatus_FLASH_WriteError 

The erase operation is error.

kStatus_FLASH_ProtectionViolation 

The program/erase operation is requested to execute on protected areas.

kStatus_FLASH_AHBError 

The AHB operation is error.

kStatus_FLASH_Busy 

Flash is busy.

kStatus_FLASH_WriteDmaIdle 

Flash write finish.

kStatus_FLASH_ReadDmaIdle 

Flash read finish.

Enumerator
kFLASH_Block0 

The block 0 of Flash.

kFLASH_Block1 

The block 1 of Flash.

Function Documentation

static uint32_t FLASH_GetStatusFlags ( void  )
inlinestatic

This function get all FLASH status flags.

Returns
FLASH status flags.
static void FLASH_ClearStatusFlags ( uint32_t  mask)
inlinestatic

This function clears FLASH status flags with a provided mask.

Parameters
maskThe status flags to be cleared
static void FLASH_EnableInterrupts ( uint32_t  mask)
inlinestatic

This function enables the FLASH interrupt.

Parameters
maskinterrupt source.
static void FLASH_DisableInterrupts ( uint32_t  mask)
inlinestatic

This function disables the FLASH interrupt.

Parameters
maskinterrupt source.
static uint32_t FLASH_GetBusyStatusFlags ( void  )
inlinestatic

This function get all FLASH busy status flags.

Returns
FLASH busy status flags.
void FLASH_GetDefaultConfig ( flash_config_t config)

This function initializes the UART configuration structure to a default value.

* config->blockBase = FSL_FEATURE_FLASH_BASE_ADDR;
* config->totalSize = FSL_FEATURE_FLASH_SIZE_BYTES;
* config->pageSize = FSL_FEATURE_FLASH_PAGE_SIZE_BYTES;
* config->timeBase = FLASH_TIME_BASE(ahb_clk);
* config->smartMaxEraseTime = FLASH_SMART_MAX_ERASE_TIME;
* config->smartMaxWriteTime = FLASH_SMART_MAX_WRITE_TIME;
* config->smartWriteEnable = false;
*
Parameters
configpointer to flash config structure
Returns
none
status_t FLASH_Erase ( flash_config_t config,
uint32_t  start,
uint32_t  lengthInBytes 
)

This function erases the appropriate number of flash pages based on the desired start address and length.

Parameters
configpointer to configuration structure
startSpecifies the start address of the FLASH to be erased, the address should be aligned with 4 bytes
lengthInBytesThe length, given in bytes (not words or long-words) to be erased. Must be word aligned.
status_t FLASH_PageErase ( flash_config_t config,
uint8_t  pageIdx 
)

This function erases a page based on page_index.

Parameters
configpointer to configuration structure
pageIdxThe page index to be erased
Returns
status
status_t FLASH_BlockErase ( flash_config_t config,
uint32_t  block 
)

This function erases a block based on block.

Parameters
configpointer to configuration structure
blockSpecifies the block to be erased
Returns
status
status_t FLASH_Program ( flash_config_t config,
uint32_t  start,
uint32_t *  src,
uint32_t  lengthInBytes 
)

This function programs the flash memory with the desired data for a given flash area as determined by the addr and n_word.

Parameters
baseFLASH peripheral base address.
configpointer to configuration structure
startSpecifies the start address of the FLASH to be written, the address should be aligned with 4 bytes
srcPointer of the write data buffer
lengthInBytesThe size of data to be written
Returns
status
status_t FLASH_GetLockBit ( flash_config_t config,
flash_lock_bit_t lockBit 
)

This function get the flash page lock bit.

Parameters
baseFLASH peripheral base address.
configpointer to configuration structure
lockBitPointer of the lock bit configuration structure(0: lock; 1: unlock)
Returns
status
status_t FLASH_SetLockBit ( flash_config_t config,
flash_lock_bit_t lockBit 
)

This function set the flash page lock bit base based on lockBit.

Parameters
baseFLASH peripheral base address.
configpointer to configuration structure
lockBitPointer of the lock bit configuration structure(0: lock; 1: unlock)
Returns
status