MCXA153 flash writing problem

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MCXA153 flash writing problem

1,478 Views
_Ferrari_
Contributor V

Dear all,
I am developing an application based on the MCXA153 processor. The project uses an external 16MHz crystal, and I have modified the clock configuration, according to your suggestion, to obtain 96MHz as the Main clock.

_Ferrari__0-1761204691515.png

Also following your instructions, I used the information from the 'romapi_flashiap' project to be able to write to the flash.
Everything works fine, but after the first flash write operation, the Main clock completely changed.

In this post you can find the class and method that I used.


Have you ever had a similar problem?
How was it solved?
Thank you very much for your cooperation and your help.
Regards

 

 

class flash {
public:
	flash();
	virtual ~flash();
	void init();
	int32_t flashWrite(uint8_t *source, int32_t size);
	int32_t flashRead(uint8_t *pDest, int32_t size) ;
private:
    status_t status;
    uint32_t destAdrss; /* Address of the target location */
    uint32_t i, failedAddress, failedData;
    uint32_t pflashBlockBase  ;
    uint32_t pflashTotalSize  ;
    uint32_t pflashSectorSize ;
    uint32_t PflashPageSize   ;

    flash_config_t s_flashDriver;

    union _uBuffer buffer;

    void speculation_buffer_clear();
    void lpcac_clear();
};

 

void flash::init() {
	pflashBlockBase = 0U;
	pflashTotalSize = 0U;
	pflashSectorSize = 0U;
	PflashPageSize = 0U;

	FLASH_API->flash_init(&s_flashDriver);

	/* Get flash properties kFLASH_ApiEraseKey */
	FLASH_API->flash_get_property(&s_flashDriver, kFLASH_PropertyPflashBlockBaseAddr, &pflashBlockBase);
	FLASH_API->flash_get_property(&s_flashDriver, kFLASH_PropertyPflashSectorSize, &pflashSectorSize);
	FLASH_API->flash_get_property(&s_flashDriver, kFLASH_PropertyPflashTotalSize, &pflashTotalSize);
	FLASH_API->flash_get_property(&s_flashDriver, kFLASH_PropertyPflashPageSize, &PflashPageSize);

	destAdrss = pflashBlockBase + (pflashTotalSize - (SECTOR_INDEX_FROM_END * pflashSectorSize));
}

int32_t flash::flashWrite(uint8_t *source, int32_t size) {

	status = FLASH_API->flash_erase_sector(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_ApiEraseKey);
	if (status == kStatus_Success) {
		speculation_buffer_clear();
		lpcac_clear();
		status = FLASH_API->flash_verify_erase_sector(&s_flashDriver, destAdrss, pflashSectorSize);
		if (status == kStatus_Success) {
			status = FLASH_API->flash_program_page(&s_flashDriver, destAdrss, (uint8_t*) source, size);
		}
		speculation_buffer_clear();
		lpcac_clear();
	}
	return status;
}

 

0 Kudos
Reply
5 Replies

1,413 Views
_Ferrari_
Contributor V

I analyzed the problem in detail and would like to present the result of my measurements.

I developed two programs. The first uses the CTIMER0 timer interrupt to toggle a CPU pin with a period of 20 microseconds (10 microseconds OFF and 10 microseconds ON).
The clock source for CTIMER0 is the Main CLOCK at 96MHz. As seen in the image, the period does not change both before and after the write operation. (see the  A1-A2 and B1-B2 cursors)
This means that the 96MHz clock doesn't change, but, as can be noted, interrupts are disabled for approximately 500 microseconds during the flash write phase. (see the C1-C2 cursors)

// PROGRAM 1
uint8_t buffer[128]; 
volatile int32_t OnOff = 1;

int main(void) {

	/* Init board hardware. */

	BOARD_InitBootPins();
	BOARD_InitBootClocks();
	BOARD_InitBootPeripherals();
#ifndef BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL
	/* Init FSL debug console. */
	// BOARD_InitDebugConsole();
#endif
	memoryFlash.init();
	for(int i=0;i<128;i++) {
		buffer[i]=(uint8_t)i;
	}
	SDK_DelayAtLeastUs(100, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
	memoryFlash.flashWrite(buffer,128);
	SDK_DelayAtLeastUs(100, SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY);
	while(1);
	return 0;
}

// INTERRUPT ROUTINE
void TestBit(uint32_t flg)
{
	GPIO_PinWrite(BOARD_INITPINS_RS485_TX_GPIO,	BOARD_INITPINS_RS485_TX_GPIO_PIN, OnOff);
	OnOff = !OnOff;
}

Image 1

_Ferrari__0-1761556500148.png

The second program toggles the CPU pin 10 times using a dedicated routine (togglePin).
As seen in the image, before the write operation, the period is approximately 640 nanoseconds (320 OFF and 320 ON).

(see the A1-A2 cursor on the image 2)

After the flash write operation, the half-period lengthens by about three times (from 320 nanoseconds to 1.52 microseconds) (wait states are probably being added).  (see the B1-B2 cursor on the image 3)
This obviously slows down the program execution.

// PROGRAM 2

flash memoryFlash;
uint8_t buffer[128];
volatile int32_t OnOff = 1;


static void togglePin()
{
	for(int i=0;i<10;i++) {
		GPIO_PinWrite(BOARD_INITPINS_RS485_TX_GPIO,	BOARD_INITPINS_RS485_TX_GPIO_PIN, OnOff);
		OnOff = !OnOff;
	}
}

int main(void) {

	/* Init board hardware. */

	BOARD_InitBootPins();
	BOARD_InitBootClocks();
	BOARD_InitBootPeripherals();
#ifndef BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL
	/* Init FSL debug console. */
	// BOARD_InitDebugConsole();
#endif
	memoryFlash.init();
	for(int i=0;i<128;i++) {
		buffer[i]=(uint8_t)i;
	}
	togglePin();
	memoryFlash.flashWrite(buffer,128);
	togglePin();
	while(1);
	return 0;
}

Image 2

_Ferrari__1-1761557055673.png

 

Image 3

_Ferrari__2-1761557100481.png

Therefore:

1) How can I prevent interrupts from being disabled during the flash write operation?
2) How can I prevent the program from slowing down after the flash write operation?

Thank you very much for your help and cooperation

regards

 

 

0 Kudos
Reply

1,397 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @_Ferrari_ 

1) How can I prevent interrupts from being disabled during the flash write operation?

->>It is not recommended to keep interrupts enabled during flash write operations. Disabling interrupts helps prevent data corruption.

2) How can I prevent the program from slowing down after the flash write operation?

->>After the flash write is completed, the program should not run slowly—this is not related. Regarding the clock, you can measure it using the clock output pin.

 

BR

Alice

0 Kudos
Reply

1,354 Views
_Ferrari_
Contributor V

Thank you for your answer

You wrote:

 

->>It is not recommended to keep interrupts enabled during flash write operations. Disabling interrupts helps prevent data corruption.

However, I seem to recall that this is only true if, during the execution of an interrupt service routine, I try to read data or execute code that resides in the flash block I am currently erasing or writing.

->>After the flash write is completed, the program should not run slowly—this is not related. Regarding the clock, you can measure it using the clock output pin.

As you suggested, I followed the 'romapi_flashiap' example.
In the flash writing routine, you disable the microprocessor's cache memory but do not re-enable it at the end of the writing.
Therefore, after a flash write, the software's execution speed is lower.

(flashiap.c routine in romapi_flashiap example)

 PRINTF("\r\n Erase a sector of flash");
    status = FLASH_API->flash_erase_sector(&s_flashDriver, destAdrss, pflashSectorSize, kFLASH_ApiEraseKey);
    if (status != kStatus_Success)
    {
        error_trap();
    }

    /* Clear speculation buffer and lpcac. */
    speculation_buffer_clear();
    lpcac_clear();
// ------------------------
// CACHE MEMORY IS DISABLED
// ------------------------
    /* Verify if the given flash range is successfully erased. */
    PRINTF("\r\n Calling flash_verify_erase_sector() API.");
    status = FLASH_API->flash_verify_erase_sector(&s_flashDriver, destAdrss, pflashSectorSize);
    if (status == kStatus_Success)
    {
        PRINTF("\r\n Successfully erased sector: 0x%x -> 0x%x\r\n", destAdrss, (destAdrss + pflashSectorSize));
    }
    else
    {
        error_trap();
    }

    /* Prepare user buffer. */
    for (i = 0; i < BUFFER_LEN; i++)
    {
        s_buffer[i] = i;
    }

    /* Start programming specified flash region */
    PRINTF("\r\n Calling FLASH_Program() API.");
    status = FLASH_API->flash_program_page(&s_flashDriver, destAdrss, (uint8_t *)s_buffer, sizeof(s_buffer));
    if (status != kStatus_Success)
    {
        error_trap();
    }

    /* Clear speculation buffer and lpcac. */
    speculation_buffer_clear();
    lpcac_clear();
// ------------------------
// CACHE MEMORY IS DISABLED
// ------------------------

 

As you suggested I 

0 Kudos
Reply

1,317 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @_Ferrari_ 

 

Thanks for your reply.

Yes, you are right. You can  enable the cache after flash operation finished. 

 

BR

Alice

0 Kudos
Reply

1,447 Views
Alice_Yang
NXP TechSupport
NXP TechSupport

Hello @_Ferrari_ 

"but after the first flash write operation, the Main clock completely changed."

->> Do you mean that after running for a while, you noticed the main clock changed and is no longer 96 MHz?
And the main clock is 96 MHz when running romapi_flashiap?
How did you observe this?
Could you please share a screenshot or log to show the behavior?

 

BR

Alice

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2191272%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EMCXA153%20flash%20writing%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2191272%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EDear%20all%2C%3CBR%20%2F%3EI%20am%20developing%20an%20application%20based%20on%20the%20MCXA153%20processor.%20The%20project%20uses%20an%20external%2016MHz%20crystal%2C%20and%20I%20have%20modified%20the%20clock%20configuration%2C%20according%20to%20your%20suggestion%2C%20to%20obtain%2096MHz%20as%20the%20Main%20clock.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22_Ferrari__0-1761204691515.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22_Ferrari__0-1761204691515.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F362256i76A6855EAA8AA269%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22_Ferrari__0-1761204691515.png%22%20alt%3D%22_Ferrari__0-1761204691515.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EAlso%20following%20your%20instructions%2C%20I%20used%20the%20information%20from%20the%20'romapi_flashiap'%20project%20to%20be%20able%20to%20write%20to%20the%20flash.%3CBR%20%2F%3EEverything%20works%20fine%2C%20but%20after%20the%20first%20flash%20write%20operation%2C%20the%20Main%20clock%20completely%20changed.%3C%2FP%3E%3CP%3EIn%20this%20post%20you%20can%20find%20the%20class%20and%20method%20that%20I%20used.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EHave%20you%20ever%20had%20a%20similar%20problem%3F%3CBR%20%2F%3EHow%20was%20it%20solved%3F%3CBR%20%2F%3EThank%20you%20very%20much%20for%20your%20cooperation%20and%20your%20help.%3CBR%20%2F%3ERegards%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eclass%20flash%20%7B%0Apublic%3A%0A%09flash()%3B%0A%09virtual%20~flash()%3B%0A%09void%20init()%3B%0A%09int32_t%20flashWrite(uint8_t%20*source%2C%20int32_t%20size)%3B%0A%09int32_t%20flashRead(uint8_t%20*pDest%2C%20int32_t%20size)%20%3B%0Aprivate%3A%0A%20%20%20%20status_t%20status%3B%0A%20%20%20%20uint32_t%20destAdrss%3B%20%2F*%20Address%20of%20the%20target%20location%20*%2F%0A%20%20%20%20uint32_t%20i%2C%20failedAddress%2C%20failedData%3B%0A%20%20%20%20uint32_t%20pflashBlockBase%20%20%3B%0A%20%20%20%20uint32_t%20pflashTotalSize%20%20%3B%0A%20%20%20%20uint32_t%20pflashSectorSize%20%3B%0A%20%20%20%20uint32_t%20PflashPageSize%20%20%20%3B%0A%0A%20%20%20%20flash_config_t%20s_flashDriver%3B%0A%0A%20%20%20%20union%20_uBuffer%20buffer%3B%0A%0A%20%20%20%20void%20speculation_buffer_clear()%3B%0A%20%20%20%20void%20lpcac_clear()%3B%0A%7D%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Evoid%20flash%3A%3Ainit()%20%7B%0A%09pflashBlockBase%20%3D%200U%3B%0A%09pflashTotalSize%20%3D%200U%3B%0A%09pflashSectorSize%20%3D%200U%3B%0A%09PflashPageSize%20%3D%200U%3B%0A%0A%09FLASH_API-%26gt%3Bflash_init(%26amp%3Bs_flashDriver)%3B%0A%0A%09%2F*%20Get%20flash%20properties%20kFLASH_ApiEraseKey%20*%2F%0A%09FLASH_API-%26gt%3Bflash_get_property(%26amp%3Bs_flashDriver%2C%20kFLASH_PropertyPflashBlockBaseAddr%2C%20%26amp%3BpflashBlockBase)%3B%0A%09FLASH_API-%26gt%3Bflash_get_property(%26amp%3Bs_flashDriver%2C%20kFLASH_PropertyPflashSectorSize%2C%20%26amp%3BpflashSectorSize)%3B%0A%09FLASH_API-%26gt%3Bflash_get_property(%26amp%3Bs_flashDriver%2C%20kFLASH_PropertyPflashTotalSize%2C%20%26amp%3BpflashTotalSize)%3B%0A%09FLASH_API-%26gt%3Bflash_get_property(%26amp%3Bs_flashDriver%2C%20kFLASH_PropertyPflashPageSize%2C%20%26amp%3BPflashPageSize)%3B%0A%0A%09destAdrss%20%3D%20pflashBlockBase%20%2B%20(pflashTotalSize%20-%20(SECTOR_INDEX_FROM_END%20*%20pflashSectorSize))%3B%0A%7D%0A%0Aint32_t%20flash%3A%3AflashWrite(uint8_t%20*source%2C%20int32_t%20size)%20%7B%0A%0A%09status%20%3D%20FLASH_API-%26gt%3Bflash_erase_sector(%26amp%3Bs_flashDriver%2C%20destAdrss%2C%20pflashSectorSize%2C%20kFLASH_ApiEraseKey)%3B%0A%09if%20(status%20%3D%3D%20kStatus_Success)%20%7B%0A%09%09speculation_buffer_clear()%3B%0A%09%09lpcac_clear()%3B%0A%09%09status%20%3D%20FLASH_API-%26gt%3Bflash_verify_erase_sector(%26amp%3Bs_flashDriver%2C%20destAdrss%2C%20pflashSectorSize)%3B%0A%09%09if%20(status%20%3D%3D%20kStatus_Success)%20%7B%0A%09%09%09status%20%3D%20FLASH_API-%26gt%3Bflash_program_page(%26amp%3Bs_flashDriver%2C%20destAdrss%2C%20(uint8_t*)%20source%2C%20size)%3B%0A%09%09%7D%0A%09%09speculation_buffer_clear()%3B%0A%09%09lpcac_clear()%3B%0A%09%7D%0A%09return%20status%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2196430%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MCXA153%20flash%20writing%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2196430%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F202847%22%20target%3D%22_blank%22%3E%40_Ferrari_%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EThanks%20for%20your%20reply.%3C%2FP%3E%0A%3CP%3EYes%2C%20you%20are%20right.%20You%20can%26nbsp%3B%20enable%20the%20cache%20after%20flash%20operation%20finished.%26nbsp%3B%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EBR%3C%2FP%3E%0A%3CP%3EAlice%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2195803%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MCXA153%20flash%20writing%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2195803%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EThank%20you%20for%20your%20answer%3C%2FP%3E%3CP%3EYou%20wrote%3A%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E-%26gt%3B%26gt%3BIt%20is%20not%20recommended%20to%20keep%20interrupts%20enabled%20during%20flash%20write%20operations.%20Disabling%20interrupts%20helps%20prevent%20data%20corruption.%3C%2FP%3E%3CP%3EHowever%2C%20I%20seem%20to%20recall%20that%20this%20is%20only%20true%20if%2C%20during%20the%20execution%20of%20an%20interrupt%20service%20routine%2C%20I%20try%20to%20read%20data%20or%20execute%20code%20that%20resides%20in%20the%20flash%20block%20I%20am%20currently%20erasing%20or%20writing.%3C%2FP%3E%3CP%3E-%26gt%3B%26gt%3BAfter%20the%20flash%20write%20is%20completed%2C%20the%20program%20should%20not%20run%20slowly%E2%80%94this%20is%20not%20related.%20Regarding%20the%20clock%2C%20you%20can%20measure%20it%20using%20the%20clock%20output%20pin.%3C%2FP%3E%3CP%3EAs%20you%20suggested%2C%20I%20followed%20the%20'romapi_flashiap'%20example.%3CBR%20%2F%3EIn%20the%20flash%20writing%20routine%2C%20you%20disable%20the%20microprocessor's%20cache%20memory%20but%20do%20not%20re-enable%20it%20at%20the%20end%20of%20the%20writing.%3CBR%20%2F%3ETherefore%2C%20after%20a%20flash%20write%2C%20the%20software's%20execution%20speed%20is%20lower.%3C%2FP%3E%3CP%3E(flashiap.c%20routine%20in%20romapi_flashiap%20example)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%20PRINTF(%22%5Cr%5Cn%20Erase%20a%20sector%20of%20flash%22)%3B%0A%20%20%20%20status%20%3D%20FLASH_API-%26gt%3Bflash_erase_sector(%26amp%3Bs_flashDriver%2C%20destAdrss%2C%20pflashSectorSize%2C%20kFLASH_ApiEraseKey)%3B%0A%20%20%20%20if%20(status%20!%3D%20kStatus_Success)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20error_trap()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*%20Clear%20speculation%20buffer%20and%20lpcac.%20*%2F%0A%20%20%20%20speculation_buffer_clear()%3B%0A%20%20%20%20lpcac_clear()%3B%0A%2F%2F%20------------------------%0A%2F%2F%20CACHE%20MEMORY%20IS%20DISABLED%0A%2F%2F%20------------------------%0A%20%20%20%20%2F*%20Verify%20if%20the%20given%20flash%20range%20is%20successfully%20erased.%20*%2F%0A%20%20%20%20PRINTF(%22%5Cr%5Cn%20Calling%20flash_verify_erase_sector()%20API.%22)%3B%0A%20%20%20%20status%20%3D%20FLASH_API-%26gt%3Bflash_verify_erase_sector(%26amp%3Bs_flashDriver%2C%20destAdrss%2C%20pflashSectorSize)%3B%0A%20%20%20%20if%20(status%20%3D%3D%20kStatus_Success)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20PRINTF(%22%5Cr%5Cn%20Successfully%20erased%20sector%3A%200x%25x%20-%26gt%3B%200x%25x%5Cr%5Cn%22%2C%20destAdrss%2C%20(destAdrss%20%2B%20pflashSectorSize))%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20error_trap()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*%20Prepare%20user%20buffer.%20*%2F%0A%20%20%20%20for%20(i%20%3D%200%3B%20i%20%26lt%3B%20BUFFER_LEN%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20s_buffer%5Bi%5D%20%3D%20i%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*%20Start%20programming%20specified%20flash%20region%20*%2F%0A%20%20%20%20PRINTF(%22%5Cr%5Cn%20Calling%20FLASH_Program()%20API.%22)%3B%0A%20%20%20%20status%20%3D%20FLASH_API-%26gt%3Bflash_program_page(%26amp%3Bs_flashDriver%2C%20destAdrss%2C%20(uint8_t%20*)s_buffer%2C%20sizeof(s_buffer))%3B%0A%20%20%20%20if%20(status%20!%3D%20kStatus_Success)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20error_trap()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20%2F*%20Clear%20speculation%20buffer%20and%20lpcac.%20*%2F%0A%20%20%20%20speculation_buffer_clear()%3B%0A%20%20%20%20lpcac_clear()%3B%0A%2F%2F%20------------------------%0A%2F%2F%20CACHE%20MEMORY%20IS%20DISABLED%0A%2F%2F%20------------------------%3C%2FCODE%3E%3C%2FPRE%3E%3CBR%20%2F%3E%3CP%3EAs%20you%20suggested%20I%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2194268%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MCXA153%20flash%20writing%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2194268%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F202847%22%20target%3D%22_blank%22%3E%40_Ferrari_%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E1)%20How%20can%20I%20prevent%20interrupts%20from%20being%20disabled%20during%20the%20flash%20write%20operation%3F%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E-%26gt%3B%26gt%3BIt%20is%20not%20recommended%20to%20keep%20interrupts%20enabled%20during%20flash%20write%20operations.%20Disabling%20interrupts%20helps%20prevent%20data%20corruption.%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E2)%20How%20can%20I%20prevent%20the%20program%20from%20slowing%20down%20after%20the%20flash%20write%20operation%3F%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E-%26gt%3B%26gt%3BAfter%20the%20flash%20write%20is%20completed%2C%20the%20program%20should%20not%20run%20slowly%E2%80%94this%20is%20not%20related.%20Regarding%20the%20clock%2C%20you%20can%20measure%20it%20using%20the%20clock%20output%20pin.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3E%3CSPAN%3EBR%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAlice%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2193387%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MCXA153%20flash%20writing%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2193387%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EI%20analyzed%20the%20problem%20in%20detail%20and%20would%20like%20to%20present%20the%20result%20of%20my%20measurements.%3C%2FP%3E%3CP%3EI%20developed%20two%20programs.%20The%20first%20uses%20the%20CTIMER0%20timer%20interrupt%20to%20toggle%20a%20CPU%20pin%20with%20a%20period%20of%2020%20microseconds%20(10%20microseconds%20OFF%20and%2010%20microseconds%20ON).%3CBR%20%2F%3EThe%20clock%20source%20for%20CTIMER0%20is%20the%20Main%20CLOCK%20at%2096MHz.%20As%20seen%20in%20the%20image%2C%20the%20period%20does%20not%20change%20both%20before%20and%20after%20the%20write%20operation.%20(see%20the%26nbsp%3B%20A1-A2%20and%20B1-B2%20cursors)%3CBR%20%2F%3EThis%20means%20that%20the%2096MHz%20clock%20doesn't%20change%2C%20but%2C%20as%20can%20be%20noted%2C%20interrupts%20are%20disabled%20for%20approximately%20500%20microseconds%20during%20the%20flash%20write%20phase.%20(see%20the%20C1-C2%20cursors)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%2F%2F%20PROGRAM%201%0Auint8_t%20buffer%5B128%5D%3B%20%0Avolatile%20int32_t%20OnOff%20%3D%201%3B%0A%0Aint%20main(void)%20%7B%0A%0A%09%2F*%20Init%20board%20hardware.%20*%2F%0A%0A%09BOARD_InitBootPins()%3B%0A%09BOARD_InitBootClocks()%3B%0A%09BOARD_InitBootPeripherals()%3B%0A%23ifndef%20BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL%0A%09%2F*%20Init%20FSL%20debug%20console.%20*%2F%0A%09%2F%2F%20BOARD_InitDebugConsole()%3B%0A%23endif%0A%09memoryFlash.init()%3B%0A%09for(int%20i%3D0%3Bi%26lt%3B128%3Bi%2B%2B)%20%7B%0A%09%09buffer%5Bi%5D%3D(uint8_t)i%3B%0A%09%7D%0A%09SDK_DelayAtLeastUs(100%2C%20SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY)%3B%0A%09memoryFlash.flashWrite(buffer%2C128)%3B%0A%09SDK_DelayAtLeastUs(100%2C%20SDK_DEVICE_MAXIMUM_CPU_CLOCK_FREQUENCY)%3B%0A%09while(1)%3B%0A%09return%200%3B%0A%7D%0A%0A%2F%2F%20INTERRUPT%20ROUTINE%0Avoid%20TestBit(uint32_t%20flg)%0A%7B%0A%09GPIO_PinWrite(BOARD_INITPINS_RS485_TX_GPIO%2C%09BOARD_INITPINS_RS485_TX_GPIO_PIN%2C%20OnOff)%3B%0A%09OnOff%20%3D%20!OnOff%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EImage%201%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22_Ferrari__0-1761556500148.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22_Ferrari__0-1761556500148.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F362668i7B39D1D2ECABB59F%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22_Ferrari__0-1761556500148.png%22%20alt%3D%22_Ferrari__0-1761556500148.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EThe%20second%20program%20toggles%20the%20CPU%20pin%2010%20times%20using%20a%20dedicated%20routine%20(togglePin).%3CBR%20%2F%3EAs%20seen%20in%20the%20image%2C%20before%20the%20write%20operation%2C%20the%20period%20is%20approximately%20640%20nanoseconds%20(320%20OFF%20and%20320%20ON).%3C%2FP%3E%3CP%3E(see%20the%20A1-A2%20cursor%20on%20the%20image%202)%3C%2FP%3E%3CP%3EAfter%20the%20flash%20write%20operation%2C%20the%20half-period%20lengthens%20by%20about%20three%20times%20(from%20320%20nanoseconds%20to%201.52%20microseconds)%20(wait%20states%20are%20probably%20being%20added).%26nbsp%3B%26nbsp%3B(see%20the%20B1-B2%20cursor%20on%20the%20image%203)%3CBR%20%2F%3EThis%20obviously%20slows%20down%20the%20program%20execution.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%2F%2F%20PROGRAM%202%0A%0Aflash%20memoryFlash%3B%0Auint8_t%20buffer%5B128%5D%3B%0Avolatile%20int32_t%20OnOff%20%3D%201%3B%0A%0A%0Astatic%20void%20togglePin()%0A%7B%0A%09for(int%20i%3D0%3Bi%26lt%3B10%3Bi%2B%2B)%20%7B%0A%09%09GPIO_PinWrite(BOARD_INITPINS_RS485_TX_GPIO%2C%09BOARD_INITPINS_RS485_TX_GPIO_PIN%2C%20OnOff)%3B%0A%09%09OnOff%20%3D%20!OnOff%3B%0A%09%7D%0A%7D%0A%0Aint%20main(void)%20%7B%0A%0A%09%2F*%20Init%20board%20hardware.%20*%2F%0A%0A%09BOARD_InitBootPins()%3B%0A%09BOARD_InitBootClocks()%3B%0A%09BOARD_InitBootPeripherals()%3B%0A%23ifndef%20BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL%0A%09%2F*%20Init%20FSL%20debug%20console.%20*%2F%0A%09%2F%2F%20BOARD_InitDebugConsole()%3B%0A%23endif%0A%09memoryFlash.init()%3B%0A%09for(int%20i%3D0%3Bi%26lt%3B128%3Bi%2B%2B)%20%7B%0A%09%09buffer%5Bi%5D%3D(uint8_t)i%3B%0A%09%7D%0A%09togglePin()%3B%0A%09memoryFlash.flashWrite(buffer%2C128)%3B%0A%09togglePin()%3B%0A%09while(1)%3B%0A%09return%200%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EImage%202%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22_Ferrari__1-1761557055673.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22_Ferrari__1-1761557055673.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F362673i0A4AD5C059BC3954%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22_Ferrari__1-1761557055673.png%22%20alt%3D%22_Ferrari__1-1761557055673.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EImage%203%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22_Ferrari__2-1761557100481.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22_Ferrari__2-1761557100481.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F362675i30370536BDE5E344%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22_Ferrari__2-1761557100481.png%22%20alt%3D%22_Ferrari__2-1761557100481.png%22%20%2F%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3ETherefore%3A%3C%2FP%3E%3CP%3E1)%20How%20can%20I%20prevent%20interrupts%20from%20being%20disabled%20during%20the%20flash%20write%20operation%3F%3CBR%20%2F%3E2)%20How%20can%20I%20prevent%20the%20program%20from%20slowing%20down%20after%20the%20flash%20write%20operation%3F%3C%2FP%3E%3CP%3EThank%20you%20very%20much%20for%20your%20help%20and%20cooperation%3C%2FP%3E%3CP%3Eregards%3C%2FP%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2192223%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20MCXA153%20flash%20writing%20problem%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2192223%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHello%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F202847%22%20target%3D%22_blank%22%3E%40_Ferrari_%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%22%3CSPAN%3Ebut%20after%20the%20first%20flash%20write%20operation%2C%20the%20Main%20clock%20completely%20changed.%22%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E-%26gt%3B%26gt%3B%26nbsp%3B%3C%2FSPAN%3EDo%20you%20mean%20that%20after%20running%20for%20a%20while%2C%20you%20noticed%20the%20main%20clock%20changed%20and%20is%20no%20longer%2096%E2%80%AFMHz%3F%3CBR%20%2F%3EAnd%20the%20main%20clock%20is%2096%E2%80%AFMHz%20when%20running%20%3CCODE%3Eromapi_flashiap%3C%2FCODE%3E%3F%3CBR%20%2F%3EHow%20did%20you%20observe%20this%3F%3CBR%20%2F%3ECould%20you%20please%20share%20a%20screenshot%20or%20log%20to%20show%20the%20behavior%3F%3C%2FP%3E%0A%3CBR%20%2F%3E%0A%3CP%3EBR%3C%2FP%3E%0A%3CP%3EAlice%3C%2FP%3E%3C%2FLINGO-BODY%3E