C

Qt Quick Ultralite map example

// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

#include "board_config.h"
#include "offline_tiles_provider/offlinetilefetcher.h"
#include "stmimagedecoder.h"
#include "stm32_mcu_specific.h"
#include "stm32f769i_discovery_sd.h"

#include <qul/application.h>

#include "fatfsfilesystem.h"

FIL JPEG_File; /* File object */
extern "C" const Diskio_drvTypeDef SD_Driver;

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

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

JPEG_HandleTypeDef JPEG_Handle;
extern SD_HandleTypeDef uSdHandle;

extern "C" {
void JPEG_InitColorTables();
}

uint8_t SD_initialize()
{
    uint8_t sd_state = MSD_OK;

    uSdHandle.Instance = SDMMC2;
    uSdHandle.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
    uSdHandle.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
    uSdHandle.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
    uSdHandle.Init.BusWide = SDMMC_BUS_WIDE_1B;
    uSdHandle.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
    uSdHandle.Init.ClockDiv = SDMMC_TRANSFER_CLK_DIV;

    BSP_SD_Detect_MspInit(&uSdHandle, NULL);
    if (BSP_SD_IsDetected() != SD_PRESENT) /* Check if SD card is present */
        return MSD_ERROR_SD_NOT_PRESENT;

    BSP_SD_MspInit(&uSdHandle, NULL);
    if (HAL_SD_Init(&uSdHandle) != HAL_OK)
        sd_state = MSD_ERROR;

    /* Configure SD Bus width */
    if (sd_state == MSD_OK) {
        /* Enable wide operation */
        if (HAL_SD_ConfigWideBusOperation(&uSdHandle, SDMMC_BUS_WIDE_4B) != HAL_OK)
            sd_state = MSD_ERROR;
        else
            sd_state = MSD_OK;
    }

    return sd_state;
}

void ConfigureBoard()
{
    /* Init The JPEG Look Up Tables used for YCbCr to RGB conversion*/
    JPEG_InitColorTables();

    /* Init the HAL JPEG driver */
    JPEG_Handle.Instance = JPEG;
    if (HAL_JPEG_Init(&JPEG_Handle) != HAL_OK)
        Qul::PlatformInterface::log("HAL_JPEG_Init failed\n");

    static FatFsFilesystem filesystem;

    static FATFS sdFatFs;
    static char sdPath[4];

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

    // initialize SD card
    uint8_t error = SD_initialize();
    switch (error) {
    case MSD_ERROR_SD_NOT_PRESENT: {
        Qul::PlatformInterface::log("SD card in missing!\n");
        return;
    }
    case MSD_ERROR: {
        Qul::PlatformInterface::log("HAL_SD_Init failed\n");
        return;
    }
    }

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

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

    static StmImageDecoder imagedecoder;
    Qul::Application::addImageDecoder(&imagedecoder);

    static OfflineTileFetcher tilefetcher;
    Qul::Application::addMapTileFetcher(&tilefetcher);
}