bool CStorage::Initialize()

in Firmware/Platform_NordicSDK/EPXPlatform_CStorage.cpp [62:142]


bool CStorage::Initialize(uint16_t pinMISO, uint16_t pinMOSI, uint16_t pinSCLK, uint16_t pinCS, bool format)
{
	if (g_pSPIFlash == NULL)
	{		
		DEBUGLOGLN("Initializing flash storage");
		
		// Initialize SPI for flash IC
		g_pSPIFlash = new SPIClass(NRF_SPI0, pinMISO, pinSCLK, pinMOSI);
		g_pSPIFlash->begin();
		
		g_pFlashStorageDevice = new CFlashStorageDevice(pinCS, g_pSPIFlash);
		if(!g_pFlashStorageDevice->Initialize())
                  return false;
							
		//format = true;
		// If initialize has format request erase the chip before mounting
		if (format)
			FlashChip();
		
		// mount the filesystem		
		DEBUGLOGLN("\tMounting LittleFS...");
		memset(&g_lfsCFG, 0x00, sizeof(g_lfsCFG));
		g_lfsCFG.read = user_provided_block_device_read;
		g_lfsCFG.prog = user_provided_block_device_prog;
		g_lfsCFG.erase = user_provided_block_device_erase;
		g_lfsCFG.sync  = user_provided_block_device_sync;

		// block device configuration
		g_lfsCFG.read_size = LFS_DEVICE_READSIZE;
		g_lfsCFG.prog_size = LFS_DEVICE_READSIZE;
		g_lfsCFG.cache_size = LFS_BLOCK_SIZE,
		g_lfsCFG.block_size = LFS_BLOCK_SIZE;
		g_lfsCFG.block_cycles = 500,
		g_lfsCFG.lookahead_size = LFS_LOOK_AHEAD_SIZE;

		// Set the number of blocks based on the reported chip		
		g_lfsCFG.block_count = g_pFlashStorageDevice->BlockCount();
		g_deviceBlocks = g_lfsCFG.block_count;

		// Now mount the device
		int err = lfs_mount(&g_lfs, &g_lfsCFG);

		// reformat if we can't mount the filesystem
		// this should only happen on the first boot
		if(err) 
		{
			DEBUGLOGLN("\t**** FAILED to mount file system ****");
			FlashChip();
			
			DEBUGLOGLN("\tFormatting LFS");
			err = lfs_format(&g_lfs, &g_lfsCFG);
			if (err)
			{				
				DEBUGLOGLN("\t**** Format FAILURE ****");
			}
			DEBUGLOGLN("\tRe-Mounting LittleFS...");
			err = lfs_mount(&g_lfs, &g_lfsCFG);
			if (err)
			{
				DEBUGLOGLN("\t**** FAILED to re-mount file system ****");				
			}
			else
			{
				uint32_t freeSpace = UsedSpace();
				DEBUGLOGLN("\tMounted file system, %d/%d USED BLOCKS", freeSpace, g_deviceBlocks);
				g_lfsInitialized = true;
				return true;
			}
		}		
		else
		{
			uint32_t freeSpace = UsedSpace();
			DEBUGLOGLN("\tMounted file system, %d/%d USED BLOCKS", freeSpace, g_deviceBlocks);
			g_lfsInitialized = true;
			return true;
		}
	}
	else
		return true;
	return false;
}