flexspi_config_t config;
auto AHBFreq = CLOCK_GetAhbFreq();
auto ahbCycleNs = 1.0f / AHBFreq;
ahbCycleNs *= 1.0e9;
auto totCycles = 800000 / ahbCycleNs;
auto ahbWriteInterval = totCycles / 512 ;
// WARNING: somehow the theory on this doesn't work out and we are not sure why.
// Although we divide by 512 here, it looks like we are actually 4 times too slow
// according to the oscilloscope. For this reason, we actually use 128 below for
// the AHBWriteWaitUnit.
#pragma diag_suppress = Pe2361 //Disable warning about precision loss with the static_casts below
flexspi_device_config_t deviceconfig = {
// .flexspiRootClk = 99000000,
.flexspiRootClk = 18750000,
.flashSize = FLASH_SIZE,
.CSIntervalUnit = kFLEXSPI_CsIntervalUnit1SckCycle,
.CSInterval = static_cast<uint32_t>(7 / ahbCycleNs) + 1,
.CSHoldTime = static_cast<uint32_t>(6 / ahbCycleNs) + 1,
.CSSetupTime = static_cast<uint32_t>(6 / ahbCycleNs) + 1,
.dataValidTime = static_cast<uint32_t>(10 / ahbCycleNs) + 1,
.columnspace = 8,
.enableWordAddress = 0,
.AWRSeqIndex = (uint8_t)(m_quadEnable ? WEL_WRITE_QUAD_IDX : WEL_WRITE_SINGLE_IDX),
.AWRSeqNumber = 2,
.ARDSeqIndex = (uint8_t)(m_quadEnable ? READ_QUAD_IDX : READ_SINGLE_IDX),
.ARDSeqNumber = 1,
.AHBWriteWaitUnit = kFLEXSPI_AhbWriteWaitUnit128AhbCycle,
.AHBWriteWaitInterval = static_cast<uint32_t>(ahbWriteInterval),
};
#pragma diag_default = Pe2361
FLEXSPI_Type* ptrs[] = FLEXSPI_BASE_PTRS;
auto base = const_cast<FLEXSPI_Type*>(ptrs[m_flexspiBase]);
#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
bool DCacheEnableFlag = false;
/* Disable D cache. */
if (SCB_CCR_DC_Msk == (SCB_CCR_DC_Msk & SCB->CCR))
{
SCB_DisableDCache();
DCacheEnableFlag = true;
}
#endif /* __DCACHE_PRESENT */
/*Get FLEXSPI default settings and configure the flexspi. */
FLEXSPI_GetDefaultConfig(&config);
// /*Set AHB buffer size for reading data through AHB bus. */
config.ahbConfig.enableAHBPrefetch = false;
config.ahbConfig.enableAHBBufferable = false;
config.ahbConfig.enableReadAddressOpt = false;
config.ahbConfig.enableAHBCachable = false;
FLEXSPI_Init(base, &config);
/* Configure flash settings according to serial flash feature. */
FLEXSPI_SetFlashConfig(base, &deviceconfig, kFLEXSPI_PortA1);
/* Update LUT table. */
FLEXSPI_UpdateLUT(base, 0, customLUT, CUSTOM_LUT_LENGTH);
/* Do software reset. */
FLEXSPI_SoftwareReset(base);
#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
if (DCacheEnableFlag)
{
/* Enable D cache. */
SCB_EnableDCache();
}
#endif /* __DCACHE_PRESENT */