C

Qt Quick Ultralite fileloading Example

// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial
#include "fatfs/fatfsfilesystem.h"

#include "board_config.h"

#include "ff.h"
#include "ff_gen_drv.h"

#include "fsl_sd.h"
#include "sdmmc_config.h"

#include <platforminterface/log.h>

#include <qul/application.h>
#include <platform/mem.h>

static sd_card_t card;

DSTATUS nxp_initialize(BYTE)
{
    return 0;
}

DSTATUS nxp_status(BYTE)
{
    return 0;
}

DRESULT nxp_read(BYTE, BYTE *buff, DWORD sector, UINT count)
{
    if (kStatus_Success != SD_ReadBlocks(&card, buff, sector, count))
        return RES_ERROR;

    return RES_OK;
}

const Diskio_drvTypeDef nxp_driver = {nxp_initialize, nxp_status, nxp_read};

#define BOARD_SDMMC_SD_HOST_IRQ_PRIORITY (5U)

void ConfigureBoard()
{
    static FatFsFilesystem filesystem;

    static FATFS nxpFatFs;
    static char nxpPath[4];

    BOARD_SD_Config(&card, NULL, BOARD_SDMMC_SD_HOST_IRQ_PRIORITY, NULL);

    if (SD_HostInit(&card) != kStatus_Success) {
        Qul::PlatformInterface::log("SD host init failed\r\n");
        return;
    }

    SD_PollingCardInsert(&card, kSD_Inserted);

    // Power cycle
    SD_SetCardPower(&card, false);
    SD_SetCardPower(&card, true);

    if (SD_CardInit(&card)) {
        Qul::PlatformInterface::log("SD card init failed\r\n");
        return;
    }

    if (FATFS_LinkDriver(&nxp_driver, nxpPath) != 0) {
        Qul::PlatformInterface::log("FATFS_LinkDriver failed\r\n");
        return;
    }

    if (f_mount(&nxpFatFs, nxpPath, 0) != 0) {
        Qul::PlatformInterface::log("Mounting filesystem failed!\r\n");
        return;
    }

    Qul::Application::addFilesystem(&filesystem);
}

extern "C" {

void *ff_memalloc(UINT msize)
{
    return Qul::Platform::qul_malloc(msize);
}

void ff_memfree(void *ptr)
{
    Qul::Platform::qul_free(ptr);
}
}