when i try to erase or write to the program flash on my S32K146 i run into a Fault at the moment the FTFC should execute the command. Also the RDCOLLERR bit in the FTFC_STAT register is set.
BusFault: A precise (synchronous) data access error has occurred. and also instruction fetching error i got.
Strangely enough this does not happen, when i step through the program line by line. Then the flash gets programmed correctly.
This is my routine for erasing a flash sector:
status_t Flash_Write(const byte* pucSrc, ulong* pucDst, ushort usLen)
{
status_t ret = STATUS_N_OK; /* Return code variable */
/* Check CCIF to verify the previous command is completed */
if (0U == (FTFC->FSTAT & FTFC_FSTAT_CCIF_MASK))
{
ret = STATUS_N_OK; /* Returning NOK as STATUS_BUSY is not handled */
}
else /* No previous command is pending */
{
/* Clear RDCOLERR & ACCERR & FPVIOL flag in flash status register. Write 1 to clear */
FTFC->FSTAT = CLEAR_FTFC_FSTAT_ERROR_BITS;
/* Passing parameter to the command */
FTFC->FCCOB[3] = FTFC_PROGRAM_PHRASE;
FTFC->FCCOB[2] = GET_BIT_16_23(pucDst);
FTFC->FCCOB[1] = GET_BIT_8_15(pucDst);
FTFC->FCCOB[0] = GET_BIT_0_7(pucDst);
/* Calling flash command sequence function to execute the command */
ret = Flash_Command_Sequence();
}
return ret;
}
The error happens in Flash_Command_Sequence():
status_t Flash_Command_Sequence(void)
{
flash_drv_status_t Cmd_ret_Status = FTFx_N_OK; /* Command execution return code variable */
status_t ret = STATUS_OK; /* Return code variable, by default is OK */
/* Clear CCIF to launch command */
FTFC->FSTAT |= FTFC_FSTAT_CCIF_MASK;
while (0U == (FTFC->FSTAT & FTFC_FSTAT_CCIF_MASK))
{
/* Wait till CCIF bit is set indicating the requested command is completed*/
}
Cmd_ret_Status = (flash_drv_status_t)(FTFC->FSTAT & (FTFC_FSTAT_MGSTAT0_MASK | FTFC_FSTAT_FPVIOL_MASK \
| FTFC_FSTAT_ACCERR_MASK | FTFC_FSTAT_RDCOLERR_MASK));
if (Cmd_ret_Status != FTFx_OK)
{
/* Some error has occured */
ret = STATUS_N_OK;
}
return (ret);
}
As mentioned earlier, this only happens when NOT debugging step by step. I suspect this has something to do with the flash being busy, but i did not find anything that would help me understand.