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 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 237 of file mpu_util.cpp.

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

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 248 of file mpu_util.cpp.

248 {
249 return FLASH_ADDR;
250}

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 252 of file mpu_util.cpp.

252 {
253 return FLASH_ADDR_SECOND_COPY;
254}
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: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)
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: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

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

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