AnsweredAssumed Answered

Can't get C90TFS routines to write flash

Question asked by William Fox on Jul 19, 2017
Latest reply on Jul 20, 2017 by William Fox

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.

Outcomes