Can't get C90TFS routines to write flash

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

Can't get C90TFS routines to write flash

756 Views
ee4hire
Contributor I

How to get the C90TFS driver to write flash in a KL03Z4 MCU. I simply copied each file for each command I wanted to execute into my project. Compiles and seems to execute fine. All commands complete with success return code, but as far as I can see, the flash never gets erased or written.

Here is the code I'm using:

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#include "SSD_FTFx.h"

#include "fsl_debug_console.h"
#include "board.h"
#include "constants.h"

#define FLASH_LOGGING_ENABLE 1

#if (FLASH_LOGGING_ENABLE == 1)
#define FLASH_LOG(...) debug_printf(__VA_ARGS__)
#else
#define FLASH_LOG(...)
#endif

#define FTFA_FSEC_ADDR 0x040c
#define FTFA_FOPT_ADDR 0x040d
uint8_t const *pFTFA_FSEC = (uint8_t *)FTFA_FSEC_ADDR;
uint8_t const *pFTFA_FOPT = (uint8_t *)FTFA_FOPT_ADDR;


void
flash_protection(void){

uint8_t config_FSEC = *pFTFA_FSEC;
uint8_t config_FOPT = *pFTFA_FOPT;
uint8_t buf[4] = { config_FSEC, config_FOPT, 0xff, 0xff };

uint32_t rc = 0;

FLASH_SSD_CONFIG flash_ssd_config = {
.ftfxRegBase = FTFA_BASE,
.PFlashBase = 0u,
.PFlashSize = 0x8000u,
.DebugEnable = true,
.CallBack = NULL,
};

debug_printf("\nFSEC=0x%02x, FOPT=0x%02x, flash protection (next reset):%s, buf = 0x%02x, 0x%02x\n",
*pFTFA_FSEC, *pFTFA_FOPT, *pFTFA_FSEC ? "DISABLED" : "enabled",
buf[0], buf[1] );

rc = FlashInit(&flash_ssd_config);
FLASH_LOG("FlashInit(): rc = 0x%02x\n", rc );

uint32_t protectStatus = 0;
rc = PFlashGetProtection(&flash_ssd_config, &protectStatus );
FLASH_LOG("\nPFlashGetProtection(): rc = 0x%02x, protectStatus = 0x%08x\n", rc, protectStatus );

uint8_t securityState;
rc = FlashGetSecurityState(&flash_ssd_config, &securityState);
FLASH_LOG("\nFlashGetSecurityState(): rc = 0x%02x, securityState = 0x%02x\n", rc, securityState );

rc = FlashEraseSector(&flash_ssd_config, 0x400, 1024, FlashCommandSequence );
FLASH_LOG("\nFlashEraseSector(): rc = 0x%02x\n", rc );
FLASH_LOG("FSEC=0x%02x, FOPT=0x%02x, flash protection (next reset):%s, buf = 0x%02x, 0x%02x\n",
*pFTFA_FSEC, *pFTFA_FOPT, *pFTFA_FSEC ? "DISABLED" : "enabled",
buf[0], buf[1] );

buf[0] |= 1;

rc = FlashProgram(&flash_ssd_config, FTFA_FSEC_ADDR, 4, buf, FlashCommandSequence );
FLASH_LOG("\nFlashProgramFlash(): rc = 0x%02x\n", rc );
FLASH_LOG("FSEC=0x%02x, FOPT=0x%02x, flash protection (next reset):%s, buf = 0x%02x, 0x%02x\n",
*pFTFA_FSEC, *pFTFA_FOPT, *pFTFA_FSEC ? "DISABLED" : "enabled",
buf[0], buf[1] );


}

and the results of its execution:

FSEC=0xfe, FOPT=0x1a, flash protection (next reset):DISABLED, buf = 0xfe, 0x1a
FlashInit(): rc = 0x00

PFlashGetProtection(): rc = 0x00, protectStatus = 0xffffffff

FlashGetSecurityState(): rc = 0x00, securityState = 0x01

FlashEraseSector(): rc = 0x00
FSEC=0xfe, FOPT=0x1a, flash protection (next reset):DISABLED, buf = 0xfe, 0x1a

FlashProgramFlash(): rc = 0x00
FSEC=0xfe, FOPT=0x1a, flash protection (next reset):DISABLED, buf = 0xff, 0x1a

Any suggestions as to why this doesn't write the flash would be much appreciated.

Labels (1)
0 Kudos
2 Replies

578 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hello William Fox,

     Actually, you can use our SDK_2.2_FRDM-KL03Z flash code directly, this code already be test, and it works OK on the FRDM-KL03 board.

   I think you can try the official code at first.

   Please download the KSDK code from this link:

Welcome to MCUXpresso | MCUXpresso Config Tools 

SDK Builder, choose the board as FRDM-KL03, generate the code and download it, you can find the flash code in folder:

SDK_2.2_FRDM-KL03Z\boards\frdmkl03z\driver_examples\flash\pflash

Please try it at first, if you still have question, please let me know!


Have a great day,
Kerry

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

578 Views
ee4hire
Contributor I

Kerry: Thanks.

I've brought in the DSDK 2.2 flash driver as well as copied some of the start up code from the demo examples. I get identical results to the 1.3, i.e., the routines complete with successful return status, but the flash never changes.
I'm beginning to suspect the problem my be in the 1.3 header files, such as fsl_device_registers.h, but I can't bring in the 2.2 version since all of my other code relies on 1.3.

Really bizarre such a simple piece of code can't work. Please have a look and see if I've done anything really stupid that keeps this from working.

Here's output from the version using the 2.2 driver:

@fffffffffffffffffffffffffe1affff
FLASH_Init(): rc = 0x00

PFlash Example Start

PFlash Information:
Flash block base address: 0x0
Total Program Flash Size: 32 KB, Hex: (0x8000)
Program Flash Sector Size: 1 KB, Hex: (0x400)

FLASH_Erase(): rc = 0x00

FSEC=0xfe, FOPT=0x1a, flash protection (next reset):DISABLED, buf = 0x00001afe
fffffffffffffffffffffffffe1affff
FLASH_VerifyErase(): rc = 0x00

FSEC=0xfe, FOPT=0x1a, flash protection (next reset):DISABLED, buf = 0x00001afe

FLASH_Program(): rc = 0x00

FSEC=0xfe, FOPT=0x1a, flash protection (next reset):DISABLED, buf = 0x00001afe
fffffffffffffffffffffffffe1affff

And the code (sorry the paste doesn't preserve indentation)

/* ----------------------------------------------------------------------------
Copyright (c) 2017, ZOS, Inc.

Author(s): Bill Fox <billfox@bct.com>

Target(s): ISO/IEC 9899:1999 (C99)
------------------------------------------------------------------------- */

#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#include "fsl_flash.h"

#include "fsl_debug_console.h"
#include "board.h"
#include "constants.h"
#include "delay.h"

#define FLASH_LOGGING_ENABLE 1

#if (FLASH_LOGGING_ENABLE == 1)
#define FLASH_LOG(...) debug_printf(__VA_ARGS__)
#else
#define FLASH_LOG(...)
#endif

#define FTFA_FSEC_ADDR 0x040c
#define FTFA_FOPT_ADDR 0x040d
uint8_t const *pFTFA_FSEC = (uint8_t *)FTFA_FSEC_ADDR;
uint8_t const *pFTFA_FOPT = (uint8_t *)FTFA_FOPT_ADDR;

static flash_config_t s_flashDriver;

static void printFLASH( uint32_t addr, size_t size ){
for(;size;size--){
FLASH_LOG("%02x", *(uint8_t*)addr++);
}
FLASH_LOG("\n");
}

void
flash_protection(void){

uint8_t config_FSEC = *pFTFA_FSEC;
uint8_t config_FOPT = *pFTFA_FOPT;
uint32_t buf = (config_FOPT << 8) | config_FSEC;

uint32_t rc = 0;

uint32_t pflashBlockBase = 0;
uint32_t pflashTotalSize = 0;
uint32_t pflashSectorSize = 0;

printFLASH( 0x400, 16 );

memset(&s_flashDriver, 0, sizeof(flash_config_t));
rc = FLASH_Init(&s_flashDriver);
FLASH_LOG("FLASH_Init(): rc = 0x%02x\n", rc );


FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashBlockBaseAddr, &pflashBlockBase);
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashTotalSize, &pflashTotalSize);
FLASH_GetProperty(&s_flashDriver, kFLASH_PropertyPflashSectorSize, &pflashSectorSize);

/* print welcome message */
debug_printf("\r\n PFlash Example Start \r\n");
/* Print flash information - PFlash. */
debug_printf("\r\n PFlash Information: ");
debug_printf("\r\n Flash block base address:\t0x%0x", pflashBlockBase);
debug_printf("\r\n Total Program Flash Size:\t%d KB, Hex: (0x%x)", (pflashTotalSize / 1024), pflashTotalSize);
debug_printf("\r\n Program Flash Sector Size:\t%d KB, Hex: (0x%x)\n\n", (pflashSectorSize / 1024), pflashSectorSize);

rc = FLASH_Erase(&s_flashDriver, 0x400, 1024, kFLASH_ApiEraseKey);
FLASH_LOG("FLASH_Erase(): rc = 0x%02x\n", rc );
FLASH_LOG("\nFSEC=0x%02x, FOPT=0x%02x, flash protection (next reset):%s, buf = 0x%08x\n",
*pFTFA_FSEC, *pFTFA_FOPT, *pFTFA_FSEC ? "DISABLED" : "enabled",
buf );

printFLASH( 0x400, 16 );

rc = FLASH_VerifyErase(&s_flashDriver, 0x400, 1024, kFLASH_MarginValueUser);
FLASH_LOG("FLASH_VerifyErase(): rc = 0x%02x\n", rc );
FLASH_LOG("\nFSEC=0x%02x, FOPT=0x%02x, flash protection (next reset):%s, buf = 0x%08x\n",
*pFTFA_FSEC, *pFTFA_FOPT, *pFTFA_FSEC ? "DISABLED" : "enabled",
buf );

rc = FLASH_Program(&s_flashDriver, FTFA_FSEC_ADDR, &buf, 4 );
FLASH_LOG("\nFLASH_Program(): rc = 0x%02x\n", rc );
FLASH_LOG("\nFSEC=0x%02x, FOPT=0x%02x, flash protection (next reset):%s, buf = 0x%08x\n",
*pFTFA_FSEC, *pFTFA_FOPT, *pFTFA_FSEC ? "DISABLED" : "enabled",
buf );

printFLASH( 0x400, 16 );
}

0 Kudos