<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>S32KのトピックRe: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
    <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403997#M13773</link>
    <description>&lt;P&gt;Hi Daniel,&lt;/P&gt;&lt;P&gt;no, the flash should not be write protected (I never set any flash region write protected in my program). The line is just a left over from my tests and I've already deleted it (but have still the same copy issues...).&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp; Arne&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jan 2022 14:02:06 GMT</pubDate>
    <dc:creator>ArneB</dc:creator>
    <dc:date>2022-01-24T14:02:06Z</dc:date>
    <item>
      <title>[S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403799#M13769</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I'm currently implementing an In-Application Programming (IAP) procedure for firmware updates in my project, but have some issues in the final step.&lt;/P&gt;&lt;P&gt;Note: I'm using S32 Design Studio for S32 platform,&amp;nbsp; Build 201217 (Update 3) and SDK for S32K144, version S32SDK_S32K1XX_RTM_4.0.2&lt;/P&gt;&lt;P&gt;What am I doing ?&lt;/P&gt;&lt;P&gt;1) Receive a new firmware via CAN and store it in the DFlash block (The FW size is ~25k, so this fits well into the 64k block of the DFlash). I've verified the content of the DFlash after receiving it and it's identical to the generated and uploaded .bin file, so I assume this step is working&lt;/P&gt;&lt;P&gt;2) Disable all interrupts and call a function located in RAM, which erases the PFlash block and copy the Dflash content to PFlash (starting at address 0).&lt;/P&gt;&lt;P&gt;3) Issue a SW reset request&lt;/P&gt;&lt;P&gt;The MCU stalls somewhere at step 2. The PFlash memory is corrupted, the MCU won't start. Unfortunately it's hard to debug, so I'm asking for any helpful hints &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/P&gt;&lt;P&gt;Here are the important snippets of my code:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#define  CODE_RAM  __attribute__ ((section(".code_ram")))
void CODE_RAM copy_firmware(void);&lt;/LI-CODE&gt;&lt;P&gt;I've checked the .map file and the copy_firmware() function is indeed placed into the .code_ram section.&lt;/P&gt;&lt;LI-CODE lang="c"&gt; *(.code_ram)
 .code_ram      0x1fff8ab8       0x38 ./SDK/platform/drivers/src/flash/flash_driver.o
 .code_ram      0x1fff8af0       0xcc ./src/can_operator.o
                0x1fff8af0                copy_firmware
                0x1fff8bbc                . = ALIGN (0x4)
                0x1fff8bbc                __code_end__ = .
                0x1fff8bbc                __code_ram_end__ = .
                0x0000644c                __CODE_END = (__CODE_ROM + (__code_end__ - __code_start__))
                0x0000644c                __CUSTOM_ROM = __CODE_END&lt;/LI-CODE&gt;&lt;P&gt;I'm calling the copy_firmware() function with these commands:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;FLASH_DRV_SetPFlashProtection(0xFFFFFFFF);
DISABLE_INTERRUPTS()
copy_firmware();&lt;/LI-CODE&gt;&lt;P&gt;...and here is the copy_firmware function. The length of the new firmware is stored in the FirmwareBlockBuffer.Current_Address variable (I've already verified that this value is correct)&lt;/P&gt;&lt;LI-CODE lang="c"&gt;void CODE_RAM copy_firmware() {
uint8_t *ptr8;
uint32_t regValue;
uint32_t i;

	// Erase PFlash
	while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0);
	FTFC-&amp;gt;FSTAT = FTFC_FSTAT_ACCERR_MASK | FTFC_FSTAT_FPVIOL_MASK;
	FTFC-&amp;gt;FCCOB[3] = 0x08; // Erase block cmd
	FTFC-&amp;gt;FCCOB[2] = 0;    // Addr = 0 (= PFlash block)
	FTFC-&amp;gt;FCCOB[1] = 0;
	FTFC-&amp;gt;FCCOB[0] = 0;
	FTFC-&amp;gt;FSTAT = FTFC_FSTAT_CCIF_MASK; //execute command
	while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0); //wait for done

// Program PFlash
	ptr8 = (uint8_t *)Flash_InitConfig0.DFlashBase;
	for(i=0;i&amp;lt;FirmwareBlockBuffer.Current_Address;i+=8) {
		FTFC-&amp;gt;FSTAT = FTFC_FSTAT_ACCERR_MASK | FTFC_FSTAT_FPVIOL_MASK;
		FTFC-&amp;gt;FCCOB[3]  = 0x07; // Program 8 bytes
		FTFC-&amp;gt;FCCOB[2]  = (uint8_t)((i &amp;amp; 0x00FF0000)&amp;gt;&amp;gt;16);
		FTFC-&amp;gt;FCCOB[1]  = (uint8_t)((i &amp;amp; 0x0000FF00)&amp;gt;&amp;gt;8);
		FTFC-&amp;gt;FCCOB[0]  = (uint8_t)( i &amp;amp; 0x000000FF);
		FTFC-&amp;gt;FCCOB[7]  = *ptr8;
		FTFC-&amp;gt;FCCOB[6]  = *(ptr8+1);
		FTFC-&amp;gt;FCCOB[5]  = *(ptr8+2);
		FTFC-&amp;gt;FCCOB[4]  = *(ptr8+3);
		FTFC-&amp;gt;FCCOB[11] = *(ptr8+4);
		FTFC-&amp;gt;FCCOB[10] = *(ptr8+5);
		FTFC-&amp;gt;FCCOB[9]  = *(ptr8+6);
		FTFC-&amp;gt;FCCOB[8]  = *(ptr8+7);
		FTFC-&amp;gt;FSTAT = FTFC_FSTAT_CCIF_MASK; //execute command
		while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0);
        ptr8 += 8;
	}

// Reset microcontroller to activate new firmware
    /* Read Application Interrupt and Reset Control Register */
    regValue = S32_SCB-&amp;gt;AIRCR;

    /* Clear register key */
    regValue &amp;amp;= ~( S32_SCB_AIRCR_VECTKEY_MASK);

    /* Configure System reset request bit and Register Key */
    regValue |= S32_SCB_AIRCR_VECTKEY(FEATURE_SCB_VECTKEY);
    regValue |= S32_SCB_AIRCR_SYSRESETREQ(0x1u);

    /* Write computed register value */
    S32_SCB-&amp;gt;AIRCR = regValue;
	while(1); // Wait for reset
}&lt;/LI-CODE&gt;&lt;P&gt;Any support/answer/explanation, why this code isn't working is highly appreciated, as I'm currently stuck...&lt;/P&gt;&lt;P&gt;Thank you very much &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 07:48:16 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403799#M13769</guid>
      <dc:creator>ArneB</dc:creator>
      <dc:date>2022-01-24T07:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403989#M13772</link>
      <description>&lt;P&gt;Hello &lt;a href="https://community.nxp.com/t5/user/viewprofilepage/user-id/196106"&gt;@ArneB&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;You call this function:&lt;/P&gt;
&lt;LI-CODE lang="c"&gt;FLASH_DRV_SetPFlashProtection(0xFFFFFFFF);&lt;/LI-CODE&gt;
&lt;P&gt;Does it mean the flash is protected out-of-reset?&lt;/P&gt;
&lt;P&gt;If so, this could be the root cause here.&lt;/P&gt;
&lt;P&gt;Becasue the protection can only be increased not descreased.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="danielmartynek_0-1643031778002.png" style="width: 771px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/168643i1831E3042A8E7E22/image-dimensions/771x293?v=v2" width="771" height="293" role="button" title="danielmartynek_0-1643031778002.png" alt="danielmartynek_0-1643031778002.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Please read the returned status value of the funtion and check for any FTFC errors in your custom functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;BR, Daniel&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 13:44:43 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403989#M13772</guid>
      <dc:creator>danielmartynek</dc:creator>
      <dc:date>2022-01-24T13:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403997#M13773</link>
      <description>&lt;P&gt;Hi Daniel,&lt;/P&gt;&lt;P&gt;no, the flash should not be write protected (I never set any flash region write protected in my program). The line is just a left over from my tests and I've already deleted it (but have still the same copy issues...).&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp; Arne&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 14:02:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1403997#M13773</guid>
      <dc:creator>ArneB</dc:creator>
      <dc:date>2022-01-24T14:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404006#M13774</link>
      <description>&lt;P&gt;Hi Arne,&lt;/P&gt;
&lt;P&gt;Thank you, I see.&lt;/P&gt;
&lt;P&gt;What about the FTFC error flags?&lt;/P&gt;
&lt;P&gt;Can you read it after CCIF= 1 for each operation?&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="danielmartynek_0-1643033423668.png" style="width: 669px;"&gt;&lt;img src="https://community.nxp.com/t5/image/serverpage/image-id/168647iFDC880CEE57A502E/image-dimensions/669x159?v=v2" width="669" height="159" role="button" title="danielmartynek_0-1643033423668.png" alt="danielmartynek_0-1643033423668.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;BR /&gt;Daniel&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 14:10:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404006#M13774</guid>
      <dc:creator>danielmartynek</dc:creator>
      <dc:date>2022-01-24T14:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404042#M13777</link>
      <description>&lt;P&gt;Hi Daniel,&lt;/P&gt;&lt;P&gt;I've set a breakpoint to to the erase PFlash command execution (first line below):&lt;/P&gt;&lt;LI-CODE lang="c"&gt;FTFC-&amp;gt;FSTAT = FTFC_FSTAT_CCIF_MASK; //execute command
while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0); //wait for done&lt;/LI-CODE&gt;&lt;P&gt;After executing the first line in the debugger (GDB &amp;amp; PE Multilink Universal) the FSTAT register shows a RDCOLERR (bit 6 is set). I'm unsure, if this is caused by the debugging process or if this is a real error...&lt;/P&gt;&lt;P&gt;The program flash is at least completely erased.&lt;/P&gt;&lt;P&gt;The first copy loop in the following paragraph results in a hard fault with the following console message in the IDE:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;BusFault: A precise (synchronous) data access error has occurred. &lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;Possible BusFault location: 0xFFFFFFFF. &lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;HardFault: A fault has been escalated to a hard fault.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;The program counter is stuck at the DefaultISR function:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;DefaultISR:&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;&amp;nbsp; b DefaultISR&lt;/EM&gt;&lt;BR /&gt;&lt;EM&gt;.size DefaultISR, . - DefaultISR&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 15:48:46 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404042#M13777</guid>
      <dc:creator>ArneB</dc:creator>
      <dc:date>2022-01-24T15:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404079#M13778</link>
      <description>&lt;P&gt;Just a quick update: I've found at least one root cause of this issue &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/P&gt;&lt;P&gt;It's this line in the copy function:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;ptr8 = (uint8_t *)Flash_InitConfig0.DFlashBase;&lt;/LI-CODE&gt;&lt;P&gt;The struct "Flash_InitConfig0" was declared as:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;const flash_user_config_t Flash_InitConfig0 =
{
    .PFlashBase = 0x0U,
    .PFlashSize = 0x80000U,
    .DFlashBase = 0x10000000U,
    .EERAMBase = 0x14000000U,
    .CallBack = NULL_CALLBACK
};&lt;/LI-CODE&gt;&lt;P&gt;Therefore it's located in PFlash. But as I just erased the whole PFlash, the members are all set to "0xFFFFFFFF" and are invalid.&lt;/P&gt;&lt;P&gt;Easy solution: Set the ptr8 variable to the DFlash memory address before the flash area is erased.&lt;/P&gt;&lt;P&gt;Now the copy proceeds as expected (I verified it with the memory debugger), but the new firmware won't run, even after an external reset. What am I missing ? Any checksum ?&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jan 2022 16:33:59 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404079#M13778</guid>
      <dc:creator>ArneB</dc:creator>
      <dc:date>2022-01-24T16:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: [S32K144] Copy DFlash to PFlash issue (In-Application Programming)</title>
      <link>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404404#M13786</link>
      <description>&lt;P&gt;Another quick update: I've found the last root cause for my issues and now everything is working as expected &lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/P&gt;&lt;P&gt;The firmware was stored in the DFlash area in the correct endian order, but the flash process reversed it...&lt;/P&gt;&lt;P&gt;For the archive and future users with similar issues I will post my working solution below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Necessary definitions:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;#define  CODE_RAM  __attribute__ ((section(".code_ram")))
void CODE_RAM copy_firmware(void);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Calling the copy function:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;DISABLE_INTERRUPTS()
copy_firmware();&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The copy function (located in RAM):&lt;/P&gt;&lt;LI-CODE lang="c"&gt;void CODE_RAM copy_firmware() {
uint32_t regValue;
uint8_t  *ptr8;
uint32_t i;

    ptr8 = (uint8_t *)Flash_InitConfig0.DFlashBase;

// Erase PFlash
	while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0);
	FTFC-&amp;gt;FSTAT = FTFC_FSTAT_ACCERR_MASK | FTFC_FSTAT_FPVIOL_MASK;
	FTFC-&amp;gt;FCCOB[3] = 0x08; // Erase block cmd
	FTFC-&amp;gt;FCCOB[2] = 0;    // Addr = 0 (= PFlash block)
	FTFC-&amp;gt;FCCOB[1] = 0;
	FTFC-&amp;gt;FCCOB[0] = 0;
	FTFC-&amp;gt;FSTAT = FTFC_FSTAT_CCIF_MASK; //execute command
	while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0); //wait for done

// Program PFlash
	for(i=0;i&amp;lt;(FirmwareBlockBuffer.Current_Address+FIRMWARE_BLOCK_BUFFER_LENGTH);i+=8) {
		FTFC-&amp;gt;FSTAT = FTFC_FSTAT_ACCERR_MASK | FTFC_FSTAT_FPVIOL_MASK;
		FTFC-&amp;gt;FCCOB[3]  = 0x07; // Program 8 bytes
		FTFC-&amp;gt;FCCOB[2]  = (uint8_t)((i &amp;amp; 0x00FF0000)&amp;gt;&amp;gt;16);
		FTFC-&amp;gt;FCCOB[1]  = (uint8_t)((i &amp;amp; 0x0000FF00)&amp;gt;&amp;gt;8);
		FTFC-&amp;gt;FCCOB[0]  = (uint8_t)( i &amp;amp; 0x000000FF);
		FTFC-&amp;gt;FCCOB[4]  = *ptr8;
		FTFC-&amp;gt;FCCOB[5]  = *(ptr8+1);
		FTFC-&amp;gt;FCCOB[6]  = *(ptr8+2);
		FTFC-&amp;gt;FCCOB[7]  = *(ptr8+3);
		FTFC-&amp;gt;FCCOB[8]  = *(ptr8+4);
		FTFC-&amp;gt;FCCOB[9]  = *(ptr8+5);
		FTFC-&amp;gt;FCCOB[10] = *(ptr8+6);
		FTFC-&amp;gt;FCCOB[11] = *(ptr8+7);
		FTFC-&amp;gt;FSTAT = FTFC_FSTAT_CCIF_MASK; //execute command
		while ((FTFC-&amp;gt;FSTAT &amp;amp; FTFC_FSTAT_CCIF_MASK) == 0);
        ptr8 += 8;
	}

// Reset microcontroller to activate new firmware
    /* Read Application Interrupt and Reset Control Register */
    regValue = S32_SCB-&amp;gt;AIRCR;

    /* Clear register key */
    regValue &amp;amp;= ~( S32_SCB_AIRCR_VECTKEY_MASK);

    /* Configure System reset request bit and Register Key */
    regValue |= S32_SCB_AIRCR_VECTKEY(FEATURE_SCB_VECTKEY);
    regValue |= S32_SCB_AIRCR_SYSRESETREQ(0x1u);

    /* Write computed register value */
    S32_SCB-&amp;gt;AIRCR = regValue;
	while(1); // Wait for reset
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you Daniel for your support !&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jan 2022 07:55:26 GMT</pubDate>
      <guid>https://community.nxp.com/t5/S32K/S32K144-Copy-DFlash-to-PFlash-issue-In-Application-Programming/m-p/1404404#M13786</guid>
      <dc:creator>ArneB</dc:creator>
      <dc:date>2022-01-25T07:55:26Z</dc:date>
    </item>
  </channel>
</rss>

