AnsweredAssumed Answered

K64 Writing Flash - the last 8 byte missing

Question asked by Marco Giammarini on Dec 12, 2017
Latest reply on Dec 14, 2017 by Mark Butcher

Hello to everyone,

I wrote a small routine to write data into flash memory.  When I try to write an array of 24 byte, the last 8 byte doesn't write and I don't understand why! There is some error in my code?

 

Regards

Marco

static System_Errors Flash_sequenceCommand (Flash_DeviceHandle dev)
{
    // check CCIF bit of the flash status register
    // wait until CCIF bit is set
    while(0 == (FTFE_FSTAT_REG(dev->regMap) && FTFE_FSTAT_CCIF_MASK));

    // Clear RDCOLLERR, ACCERR and FPVIOL flag into status register
    FTFE_FSTAT_REG(dev->regMap) = FTFE_FSTAT_RDCOLERR_MASK |
                                  FTFE_FSTAT_ACCERR_MASK |
                                  FTFE_FSTAT_FPVIOL_MASK;

    FTFE_FCCOB0_REG(dev->regMap) = dev->commandArray[0];
    FTFE_FCCOB1_REG(dev->regMap) = dev->commandArray[1];
    FTFE_FCCOB2_REG(dev->regMap) = dev->commandArray[2];
    FTFE_FCCOB3_REG(dev->regMap) = dev->commandArray[3];
    FTFE_FCCOB4_REG(dev->regMap) = dev->commandArray[4];
    FTFE_FCCOB5_REG(dev->regMap) = dev->commandArray[5];
    FTFE_FCCOB6_REG(dev->regMap) = dev->commandArray[6];
    FTFE_FCCOB7_REG(dev->regMap) = dev->commandArray[7];
    FTFE_FCCOB8_REG(dev->regMap) = dev->commandArray[8];
    FTFE_FCCOB9_REG(dev->regMap) = dev->commandArray[9];
    FTFE_FCCOBA_REG(dev->regMap) = dev->commandArray[10];
    FTFE_FCCOBB_REG(dev->regMap) = dev->commandArray[11];

    // Clear CCIF flag into status register
    FTFE_FSTAT_REG(dev->regMap) |= FTFE_FSTAT_CCIF_MASK;

    if (FTFE_FSTAT_REG(dev->regMap) & FTFE_FSTAT_ACCERR_MASK)
        return ERRORS_FLASH_ACCESS;
    else if (FTFE_FSTAT_REG(dev->regMap) & FTFE_FSTAT_RDCOLERR_MASK)
        return ERRORS_FLASH_ACCESS;
    else if (FTFE_FSTAT_REG(dev->regMap) & FTFE_FSTAT_FPVIOL_MASK)
        return ERRORS_FLASH_PROTECTION_VIOLATION;
    else
        return ERRORS_NO_ERROR;
}

static System_Errors Flash_writeLongWord (Flash_DeviceHandle dev, uint32_t address, uint8_t* data)
{
    dev->commandArray[0] = FTFx_PROGRAM_PHRASE;
    dev->commandArray[1] = FLASH_GET_BIT_16_23(address);
    dev->commandArray[2] = FLASH_GET_BIT_8_15(address);
    dev->commandArray[3] = FLASH_GET_BIT_0_7(address);
    dev->commandArray[4] = FLASH_READ8((data + 3));
    dev->commandArray[5] = FLASH_READ8((data + 2));
    dev->commandArray[6] = FLASH_READ8((data + 1));
    dev->commandArray[7] = FLASH_READ8((data + 0));
    dev->commandArray[8] = FLASH_READ8((data + 7));
    dev->commandArray[9] = FLASH_READ8((data + 6));
    dev->commandArray[10] = FLASH_READ8((data + 5));
    dev->commandArray[11] = FLASH_READ8((data + 4));

    return Flash_sequenceCommand(dev);
}

System_Errors Flash_writeBuffer (Flash_DeviceHandle dev, uint8_t *buffer, uint32_t size)
{
    System_Errors error = ERRORS_NO_ERROR;
    uint32_t addr = dev->userStartAddr;

    while (size > 0x00U)
    {
        error = Flash_writeLongWord(dev,addr,buffer);

        if (error != ERRORS_NO_ERROR)
            return error;

        addr += dev->blockSize;
        size -= dev->blockSize;
        buffer += dev->blockSize;
    }
}

System_Errors Flash_init (Flash_DeviceHandle dev, uint8_t sectorNumbers)
{
    if (dev->isInit == TRUE) return ERRORS_FLASH_JUST_INIT;

    // Save memory address
    dev->startAddr = FSL_FEATURE_FLASH_PFLASH_START_ADDRESS;
    dev->flashSize = FSL_FEATURE_FLASH_PFLASH_BLOCK_SIZE * FSL_FEATURE_FLASH_PFLASH_BLOCK_COUNT;
    dev->sectorSize = FSL_FEATURE_FLASH_PFLASH_BLOCK_SECTOR_SIZE;

    dev->userStartAddr = dev->startAddr + dev->flashSize - (dev->sectorSize * sectorNumbers);
    dev->userStopAddr = dev->startAddr + dev->flashSize;

    dev->blockSize = FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE;

    // Init command array to 0x00
    for (uint8_t i = 0; i < 12; ++i)
        dev->commandArray[i] = 0x00;

    return ERRORS_NO_ERROR;
}

Outcomes