C

Qt Quick Ultralite freertos_app_switch Example

/******************************************************************************
**
** Copyright (C) 2022 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Quick Ultralite module.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
******************************************************************************/

#include "fsl_gpio.h"
#include <fsl_iomuxc.h>
#include "fsl_common.h"
#include "fsl_elcdif.h"
#include "bg_image.h"
#include "string.h" //for memcpy and memset

extern int __BOOT_VAL_ADDR;
extern unsigned char *backBuffer();
extern void QUL_CleanInvalidateDCache_by_Addr(void *, int);
#define APP_IMG_WIDTH 480
#define APP_IMG_HEIGHT 272

#define BG_IMG_WIDTH 200
#define BG_IMG_HEIGHT BG_IMG_WIDTH

int readBootValue()
{
    int *ptr = &__BOOT_VAL_ADDR;
    return *ptr;
}

void writeBootValue(int app)
{
    int *ptr = &__BOOT_VAL_ADDR;
    *ptr = app;
    QUL_CleanInvalidateDCache_by_Addr((void *) ptr, 4);
}

void reset(int app)
{
    writeBootValue(app);
    NVIC_SystemReset();
}

#define LOGIC_LED_ON (0U)
#define LOGIC_LED_OFF (1U)
#ifndef BOARD_USER_LED_GPIO
#define BOARD_USER_LED_GPIO GPIO1
#endif
#ifndef BOARD_USER_LED_GPIO_PIN
#define BOARD_USER_LED_GPIO_PIN (9U)
#endif

void initLED()
{
    gpio_pin_config_t USER_LED_config = {.direction = kGPIO_DigitalOutput,
                                         .outputLogic = 0U,
                                         .interruptMode = kGPIO_NoIntmode};
    GPIO_PinInit(GPIO1, 9U, &USER_LED_config);

    IOMUXC_SetPinMux(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, 0U);
    IOMUXC_GPR->GPR26 = ((IOMUXC_GPR->GPR26 & (~(IOMUXC_GPR_GPR26_GPIO_MUX1_GPIO_SEL_MASK)))
                         | IOMUXC_GPR_GPR26_GPIO_MUX1_GPIO_SEL(0x00U));
    IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B0_09_GPIO1_IO09, 0x10B0U);

    GPIO_PinWrite(BOARD_USER_LED_GPIO, BOARD_USER_LED_GPIO_PIN, LOGIC_LED_OFF);
    BOARD_USER_LED_GPIO->GDIR |= (1U << BOARD_USER_LED_GPIO_PIN);
}

void toggleLED()
{
    GPIO_PinWrite(BOARD_USER_LED_GPIO,
                  BOARD_USER_LED_GPIO_PIN,
                  0x1 ^ GPIO_PinRead(BOARD_USER_LED_GPIO, BOARD_USER_LED_GPIO_PIN));
}

void displayApp2Background()
{
    //Init LCD
    ELCDIF_EnableInterrupts(LCDIF, kELCDIF_CurFrameDoneInterruptEnable | LCDIF_CTRL1_UNDERFLOW_IRQ_EN_MASK);
    ELCDIF_RgbModeStart(LCDIF);

    //Get framebuffer pointer
    unsigned char *fb = backBuffer();

    //Set white background (memset framebuffer to 255)
    const int width = APP_IMG_WIDTH;
    const int height = APP_IMG_HEIGHT;
    const int bpp = 2;
    memset((void *) fb, 255, width * height * bpp);

    //Copy the content of bg_image to the framebuffer line by line
    //Copy the content of bg_image to the framebuffer line by line
    const unsigned int h_offset = ((height - BG_IMG_HEIGHT) / 2) * width * bpp;
    const unsigned int w_offset = ((width - BG_IMG_WIDTH) / 2) * bpp;
    for (int i = 0; i < BG_IMG_HEIGHT; ++i) {
        unsigned char *dst = fb + h_offset + w_offset + i * width * bpp;
        unsigned char *src = &bg_image[i * BG_IMG_WIDTH * bpp];
        memcpy((void *) dst, (void *) src, BG_IMG_WIDTH * bpp);
    }
}