rusEFI
The most advanced open source ECU
Typedefs | Functions
flash_int.h File Reference

Typedefs

typedef uint32_t flashdata_t
 
typedef uintptr_t flashaddr_t
 Address in the flash memory. More...
 
typedef uint8_t flashsector_t
 Index of a sector. More...
 

Functions

size_t flashSectorSize (flashsector_t sector)
 Get the size of sector. More...
 
uintptr_t getFlashAddrFirstCopy (void)
 
uintptr_t getFlashAddrSecondCopy (void)
 
int intFlashErase (flashaddr_t address, size_t size)
 Erase the sectors containing the span of size bytes starting at address. More...
 
bool intFlashIsErased (flashaddr_t address, size_t size)
 Check if the size bytes of flash memory starting at address are erased. More...
 
bool intFlashCompare (flashaddr_t address, const char *buffer, size_t size)
 Check if the data in buffer are identical to the one in flash memory. More...
 
int intFlashRead (flashaddr_t source, char *destination, size_t size)
 Copy data from the flash memory to a destination. More...
 
int intFlashWrite (flashaddr_t address, const char *buffer, size_t size)
 Copy data from a buffer to the flash memory. More...
 

Typedef Documentation

◆ flashaddr_t

typedef uintptr_t flashaddr_t

Address in the flash memory.

Definition at line 86 of file flash_int.h.

◆ flashdata_t

typedef uint8_t flashdata_t

Definition at line 54 of file flash_int.h.

◆ flashsector_t

typedef uint8_t flashsector_t

Index of a sector.

Definition at line 89 of file flash_int.h.

Function Documentation

◆ flashSectorSize()

size_t flashSectorSize ( flashsector_t  sector)

Get the size of sector.

Returns
sector size in bytes.

Definition at line 225 of file mpu_util.cpp.

225  {
226  // sectors 0..11 are the 1st memory bank (1Mb), and 12..23 are the 2nd (the same structure).
227  if (sector <= 3 || (sector >= 12 && sector <= 15))
228  return 16 * 1024;
229  else if (sector == 4 || sector == 16)
230  return 64 * 1024;
231  else if ((sector >= 5 && sector <= 11) || (sector >= 17 && sector <= 23))
232  return 128 * 1024;
233  return 0;
234 }

Referenced by flashSectorSize(), intFlashErase(), intFlashSectorBegin(), and intFlashSectorErase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFlashAddrFirstCopy()

uintptr_t getFlashAddrFirstCopy ( void  )

Flex Non Volatile Memory is faster than flash It also has smaller pages so it takes less time to erase

There is no remote access to FlexNVM meaning that we cannot erase settings externally

Definition at line 236 of file mpu_util.cpp.

236  {
237  return FLASH_ADDR;
238 }

Referenced by getFlashAddrSecondCopy(), readConfiguration(), readFromFlash(), and writeToFlashNow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFlashAddrSecondCopy()

uintptr_t getFlashAddrSecondCopy ( void  )

Definition at line 240 of file mpu_util.cpp.

240  {
241  return FLASH_ADDR_SECOND_COPY;
242 }

Referenced by readConfiguration(), readFromFlash(), and writeToFlashNow().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ intFlashCompare()

bool intFlashCompare ( flashaddr_t  address,
const char *  buffer,
size_t  size 
)

Check if the data in buffer are identical to the one in flash memory.

Parameters
addressFirst address in flash memory to be checked.
bufferBuffer containing the data to compare.
sizeSize of buffer in bytes.
Returns
TRUE if the flash memory and the buffer contain identical data.
FALSE if the flash memory and the buffer don't contain identical data.

Definition at line 109 of file flash_int.cpp.

109  {
110  /* For efficiency, compare flashdata_t values as much as possible,
111  * then, fallback to byte per byte comparison. */
112  while (size >= sizeof(flashdata_t)) {
113  if (*(volatile flashdata_t*) address != *(flashdata_t*) buffer)
114  return FALSE;
115  address += sizeof(flashdata_t);
116  buffer += sizeof(flashdata_t);
117  size -= sizeof(flashdata_t);
118  }
119  while (size > 0) {
120  if (*(volatile char*) address != *buffer)
121  return FALSE;
122  ++address;
123  ++buffer;
124  --size;
125  }
126 
127  return TRUE;
128 }
int size_t size
Definition: flash_int.cpp:51
uint32_t flashdata_t
Definition: flash_int.cpp:20
uint32_t flashdata_t
Definition: flash_int.h:54
static BigBufferHandle buffer
Here is the call graph for this function:

◆ intFlashErase()

int intFlashErase ( flashaddr_t  address,
size_t  size 
)

Erase the sectors containing the span of size bytes starting at address.

Warning
If address doesn't match the beginning of a sector, the data contained between the beginning of the sector and address will be erased too. The same applies for data contained at address + size up to the end of the sector.
Parameters
addressStarting address of the span in flash memory.
sizeSize of the span in bytes.
Returns
FLASH_RETURN_SUCCESS No error erasing the flash memory.
FLASH_RETURN_BAD_FLASH Flash cell error.
FLASH_RETURN_NO_PERMISSION Access denied.

Definition at line 115 of file flash_int.cpp.

115  {
116  if (!flashUnlock())
117  return FLASH_RETURN_NO_PERMISSION;
118 
119  flashaddr_t sizeAligned = alignToWord(size);
120  status_t status = FLEXNVM_DflashErase(&flashCfg, address, sizeAligned, kFTFx_ApiEraseKey);
121 
122  flashLock();
123 
124 #ifdef KINETIS_FLASH_DEBUG
125  debugLog("* flashErase(addr=%08x siz=%d sizeAligned=%d)=%d\r\n", address, size, sizeAligned, status);
126 #endif /* KINETIS_FLASH_DEBUG */
127 
128  if (status == kStatus_FTFx_Success)
129  return FLASH_RETURN_SUCCESS;
130  return -(int)status;
131 }
bool flashUnlock(void)
Definition: flash_int.cpp:33
bool flashLock(void)
Definition: flash_int.cpp:37
return FLASH_RETURN_SUCCESS
Definition: flash_int.cpp:80
uintptr_t flashaddr_t
Address in the flash memory.
Definition: flash_int.h:86
@ kStatus_FTFx_Success
@ kFTFx_ApiEraseKey
status_t FLEXNVM_DflashErase(flexnvm_config_t *config, uint32_t start, uint32_t lengthInBytes, uint32_t key)
Erases the Dflash sectors encompassed by parameters passed into function.
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:169
static flexnvm_config_t flashCfg
Definition: flash_int.cpp:24
static int alignToWord(int v)
Definition: flash_int.cpp:111

Referenced by backupRamFlush(), eraseAndFlashCopy(), and FlashErase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ intFlashIsErased()

bool intFlashIsErased ( flashaddr_t  address,
size_t  size 
)

Check if the size bytes of flash memory starting at address are erased.

Note
If the memory is erased, one can write data into it safely.
Parameters
addressFirst address in flash memory to be checked.
sizeSize of the memory space to be checked in bytes.
Returns
TRUE Memory is already erased.
FALSE Memory is not erased.

Definition at line 89 of file flash_int.cpp.

89  {
90  /* Check for default set bits in the flash memory
91  * For efficiency, compare flashdata_t values as much as possible,
92  * then, fallback to byte per byte comparison. */
93  while (size >= sizeof(flashdata_t)) {
94  if (*(volatile flashdata_t*) address != (flashdata_t) (-1)) // flashdata_t being unsigned, -1 is 0xFF..FF
95  return false;
96  address += sizeof(flashdata_t);
97  size -= sizeof(flashdata_t);
98  }
99  while (size > 0) {
100  if (*(char*) address != 0xFF)
101  return false;
102  ++address;
103  --size;
104  }
105 
106  return TRUE;
107 }

Referenced by FlashErase(), FlashVerifyChecksum(), and intFlashSectorErase().

Here is the caller graph for this function:

◆ intFlashRead()

int intFlashRead ( flashaddr_t  source,
char *  destination,
size_t  size 
)

Copy data from the flash memory to a destination.

Warning
The destination must be at least size bytes long.
Parameters
sourceFirst address of the flash memory to be copied.
destinationBuffer to copy to.
sizeSize of the data to be copied in bytes.
Returns
FLASH_RETURN_SUCCESS if successfully copied.

Definition at line 130 of file flash_int.cpp.

130  {
131  memcpy(destination, (char*) source, size);
132  return FLASH_RETURN_SUCCESS;
133 }

Referenced by backupInit(), and readOneConfigurationCopy().

Here is the caller graph for this function:

◆ intFlashWrite()

int intFlashWrite ( flashaddr_t  address,
const char *  buffer,
size_t  size 
)

Copy data from a buffer to the flash memory.

Warning
The flash memory area receiving the data must be erased.
The buffer must be at least size bytes long.
Parameters
addressFirst address in the flash memory where to copy the data to.
bufferBuffer containing the data to copy.
sizeSize of the data to be copied in bytes.
Returns
FLASH_RETURN_SUCCESS No error.
FLASH_RETURN_NO_PERMISSION Access denied.

Definition at line 365 of file flash_int.cpp.

365  {
366  int ret = FLASH_RETURN_SUCCESS;
367 
368  /* Unlock flash for write access */
369  if (intFlashUnlock() == HAL_FAILED)
370  return FLASH_RETURN_NO_PERMISSION;
371 
372  /* Wait for any busy flags */
373  intFlashWaitWhileBusy();
374 
375  /* Setup parallelism before any program/erase */
376  FLASH->CR &= ~FLASH_CR_PSIZE_MASK;
377  FLASH->CR |= FLASH_CR_PSIZE_VALUE;
378 
379  /* Check if the flash address is correctly aligned */
380  size_t alignOffset = address % sizeof(flashdata_t);
381 // print("flash alignOffset=%d\r\n", alignOffset);
382  if (alignOffset != 0) {
383  /* Not aligned, thus we have to read the data in flash already present
384  * and update them with buffer's data */
385 
386  /* Align the flash address correctly */
387  flashaddr_t alignedFlashAddress = address - alignOffset;
388 
389  /* Read already present data */
390  flashdata_t tmp = *(volatile flashdata_t*) alignedFlashAddress;
391 
392  /* Compute how much bytes one must update in the data read */
393  size_t chunkSize = sizeof(flashdata_t) - alignOffset;
394  if (chunkSize > size)
395  chunkSize = size; // this happens when both address and address + size are not aligned
396 
397  /* Update the read data with buffer's data */
398  memcpy((char*) &tmp + alignOffset, buffer, chunkSize);
399 
400  /* Write the new data in flash */
401  ret = intFlashWriteData(alignedFlashAddress, tmp);
402  if (ret != FLASH_RETURN_SUCCESS)
403  goto exit;
404 
405  /* Advance */
406  address += chunkSize;
407  buffer += chunkSize;
408  size -= chunkSize;
409  }
410 
411  /* Now, address is correctly aligned. One can copy data directly from
412  * buffer's data to flash memory until the size of the data remaining to be
413  * copied requires special treatment. */
414  while (size >= sizeof(flashdata_t)) {
415 // print("flash write size=%d\r\n", size);
416  ret = intFlashWriteData(address, *(const flashdata_t*) buffer);
417  if (ret != FLASH_RETURN_SUCCESS)
418  goto exit;
419  address += sizeof(flashdata_t);
420  buffer += sizeof(flashdata_t);
421  size -= sizeof(flashdata_t);
422  }
423 
424  /* Now, address is correctly aligned, but the remaining data are to
425  * small to fill a entier flashdata_t. Thus, one must read data already
426  * in flash and update them with buffer's data before writing an entire
427  * flashdata_t to flash memory. */
428  if (size > 0) {
429  flashdata_t tmp = *(volatile flashdata_t*) address;
430  memcpy(&tmp, buffer, size);
431  ret = intFlashWriteData(address, tmp);
432  if (ret != FLASH_RETURN_SUCCESS)
433  goto exit;
434  }
435 
436 exit:
437  /* Lock flash again */
438  intFlashLock()
439  ;
440 
441  return ret;
442 }
static bool intFlashUnlock(void)
Unlock the flash memory for write access.
Definition: flash_int.cpp:103
static int intFlashWriteData(flashaddr_t address, const flashdata_t data)
Definition: flash_int.cpp:340

Referenced by backupInit(), backupRamFlush(), eraseAndFlashCopy(), and FlashWrite().

Here is the call graph for this function:
Here is the caller graph for this function:

Go to the source code of this file.