rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
Typedefs | Functions
flash_int.h File Reference

Typedefs

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

Functions

size_t flashSectorSize (flashsector_t sector)
 Get the size of sector.
 
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.
 
bool intFlashIsErased (flashaddr_t address, size_t size)
 Check if the size bytes of flash memory starting at address are erased.
 
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.
 
int intFlashRead (flashaddr_t source, char *destination, size_t size)
 Copy data from the flash memory to a destination.
 
int intFlashWrite (flashaddr_t address, const char *buffer, size_t size)
 Copy data from a buffer to the flash memory.
 

Typedef Documentation

◆ flashaddr_t

typedef uintptr_t flashaddr_t

Address in the flash memory.

Definition at line 111 of file flash_int.h.

◆ flashdata_t

typedef uint8_t flashdata_t

Definition at line 64 of file flash_int.h.

◆ flashsector_t

typedef uint8_t flashsector_t

Index of a sector.

Definition at line 114 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 242 of file mpu_util.cpp.

242 {
243 // sectors 0..11 are the 1st memory bank (1Mb), and 12..23 are the 2nd (the same structure).
244 if (sector <= 3 || (sector >= 12 && sector <= 15))
245 return 16 * 1024;
246 else if (sector == 4 || sector == 16)
247 return 64 * 1024;
248 else if ((sector >= 5 && sector <= 11) || (sector >= 17 && sector <= 23))
249 return 128 * 1024;
250 return 0;
251}

Referenced by flashSectorSize(), 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 253 of file mpu_util.cpp.

253 {
254 return FLASH_ADDR;
255}

Referenced by getFlashAddrSecondCopy().

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

◆ getFlashAddrSecondCopy()

uintptr_t getFlashAddrSecondCopy ( void  )

Definition at line 257 of file mpu_util.cpp.

257 {
258 return FLASH_ADDR_SECOND_COPY;
259}
Here is the call 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:64
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)
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
static int alignToWord(int v)
Definition flash_int.cpp:43
uintptr_t flashaddr_t
Address in the flash memory.
Definition flash_int.h:111
@ 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

Referenced by backupRamFlush(), 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);
133}

Referenced by backupInit().

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 443 of file flash_int.cpp.

443 {
445 if (!intFlashGetPVDStatus()) {
446 return FLASH_RETURN_LOWVOLTAGEERROR;
447 }
448
449 int ret = FLASH_RETURN_SUCCESS;
450
451 /* Unlock flash for write access */
452 if (intFlashUnlock() == HAL_FAILED)
453 return FLASH_RETURN_NO_PERMISSION;
454
455 /* Wait for any busy flags */
456 intFlashWaitWhileBusy();
457
458 /* Setup parallelism before any program/erase */
459 FLASH->CR &= ~FLASH_CR_PSIZE_MASK;
460 FLASH->CR |= FLASH_CR_PSIZE_VALUE;
461
462 while (size) {
463 if (!intFlashGetPVDStatus()) {
464 intFlashLock();
465 return FLASH_RETURN_LOWVOLTAGEERROR;
466 }
467
468 /* Check if the flash address is correctly aligned */
469 size_t alignOffset = address % sizeof(flashdata_t);
470 //print("flash alignOffset=%d\r\n", alignOffset);
471 if (alignOffset != 0) {
472 /* Not aligned, thus we have to read the data in flash already present
473 * and update them with buffer's data */
474
475 /* Align the flash address correctly */
476 flashaddr_t alignedFlashAddress = address - alignOffset;
477
478 /* Read already present data */
479 flashdata_t tmp = *(volatile flashdata_t*) alignedFlashAddress;
480
481 /* Compute how much bytes one must update in the data read */
482 size_t chunkSize = sizeof(flashdata_t) - alignOffset;
483 if (chunkSize > size)
484 chunkSize = size; // this happens when both address and address + size are not aligned
485
486 /* Update the read data with buffer's data */
487 memcpy((char*) &tmp + alignOffset, buffer, chunkSize);
488
489 /* Write the new data in flash */
490 ret = intFlashWriteData(alignedFlashAddress, tmp);
491 if (ret != FLASH_RETURN_SUCCESS)
492 goto exit;
493
494 /* Advance */
495 address += chunkSize;
496 buffer += chunkSize;
497 size -= chunkSize;
498 } else if (size >= sizeof(flashdata_t)) {
499 /* Now, address is correctly aligned. One can copy data directly from
500 * buffer's data to flash memory until the size of the data remaining to be
501 * copied requires special treatment. */
502 //print("flash write size=%d\r\n", size);
503 ret = intFlashWriteData(address, *(const flashdata_t*) buffer);
504 if (ret != FLASH_RETURN_SUCCESS)
505 goto exit;
506 address += sizeof(flashdata_t);
507 buffer += sizeof(flashdata_t);
508 size -= sizeof(flashdata_t);
509 } else /* if (size > 0) */ {
510 /* Now, address is correctly aligned, but the remaining data are to
511 * small to fill a entier flashdata_t. Thus, one must read data already
512 * in flash and update them with buffer's data before writing an entire
513 * flashdata_t to flash memory. */
514 flashdata_t tmp = *(volatile flashdata_t*) address;
515 memcpy(&tmp, buffer, size);
516 ret = intFlashWriteData(address, tmp);
517 if (ret != FLASH_RETURN_SUCCESS)
518 goto exit;
519 size = 0;
520 }
521 }
522
523exit:
524 /* Lock flash again */
525 intFlashLock()
526 ;
527
528 return ret;
529}
static bool intFlashGetPVDStatus()
static void intFlashSetPVD()
static bool intFlashUnlock(void)
Unlock the flash memory for write access.
static int intFlashWriteData(flashaddr_t address, const flashdata_t data)

Referenced by backupInit(), backupRamFlush(), FlashBufferFlush(), 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.