/*
 * Copyright (c) 2022, Shenzhen CVA Innovation CO.,LTD
 * All rights reserved.
 *
 * Shenzhen CVA Innovation CO.,LTD (CVA chip) is supplying this file for use
 * exclusively with CVA's microcontroller products. This file can be freely
 * distributed within development tools that are supporting such microcontroller
 * products.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 * CVA SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL,
 * OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 */

#ifndef _BOOTLOADER_CFG_H_
#define _BOOTLOADER_CFG_H_

/*! \brief Contains public interface to various functions related
 *         to the _BOOTLOADER_CFG module
 */

/*******************************************************************************
 * the includes
 ******************************************************************************/

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/
/* If want to Generate flash driver code in ram, enable the configuration; if not, disable the configuration */
#define BOOTLOADER_CFG_FLS_CODE_GENERATE_EN        (0u)
/* Copy flash driver from flash to ram to run if don't get flash driver from out side, only just for debug the flash driver */
#define BOOTLOADER_CFG_FLS_COPY_AUTO_EN            (0u)

/* Indication value with boot loader request from asw */
#define BOOTLOADER_CFG_REQ_ACTIVE                  (0x55AAAA55ul)
/* Asw code head id to show asw is not empty */
#define BOOTLOADER_CFG_HEAD_MASK                   (0xAABBCCDDul)

/* Once pgm block size bytes num to be written to flash */
#define BOOTLOADER_CFG_PGM_BYTES_SIZE              (2048)
/* The App flash start address, that stored the vector table for app */
#define BOOTLOADER_CFG_APP_START_ADDR              (0x00010000ul)
/* The App flash header id stored at this address */
#define BOOTLOADER_CFG_APP_HEAD_ADDR               (0x00010400ul)
/* The App flash code stored to be end at this address */
#define BOOTLOADER_CFG_APP_CODE_END_ADDR           (0x000FFFFBul)
/* The App flash crc stored at this address */
#define BOOTLOADER_CFG_APP_CRC_ADDR                (0x000FFFFCul)
/* Crc bytes num: 4bytes */
#define BOOTLOADER_CFG_APP_CRC_SIZE                (4u)
/* The total app code size exclude the crc-self */
#define BOOTLOADER_CFG_APP_SIZE                    (BOOTLOADER_CFG_APP_CODE_END_ADDR - BOOTLOADER_CFG_APP_START_ADDR + 1u)
/* All the app memory last byte address */
#define BOOTLOADER_CFG_APP_END_ADDR                (BOOTLOADER_CFG_APP_CRC_ADDR + BOOTLOADER_CFG_APP_CRC_SIZE - 1)

/* The start address of Flash driver copied into ram, the fisrt 16bytes is used to store the function address */
#define BOOTLOADER_CFG_FLS_DRV_RAM_START_ADDR      (0x1FFFF000)
#define BOOTLOADER_CFG_FLS_DRV_RAM_CRC_ADDR        (0x1FFFFFFC)
#define BOOTLOADER_CFG_FLS_DRV_RAM_CRC_SIZE        (4u)
#define BOOTLOADER_CFG_FLS_DRV_RAM_CODE_SIZE       (BOOTLOADER_CFG_FLS_DRV_RAM_CRC_ADDR - BOOTLOADER_CFG_FLS_DRV_RAM_START_ADDR)

/* The max time stay in boot before can jump to App, unit: ms */
#define BOOTLOADER_CFG_BOOT_DURATION_MS            (50u)

/* Erased all sector */
#define BOOTLOADER_CFG_ERASE_ALL                   (0xFFFFFFFF)
/* How many sector to be earsed once, recommand at least 2 sectors */
#define BOOTLOADER_CFG_ERASE_SECTOR_NUM            (BOOTLOADER_CFG_ERASE_ALL)

/* Define the intial value by calculate crc */
#define BOOTLOADER_CFG_INIT_CRC_VALUE              (0xFFFFFFFF)

/* One erase section bytes size is 1024 */
#define FLS_ERASE_SECTION_ADDR_ALIGNED_MASK        (0x7FFu)
#define FLS_PGM_SECTION_ADDR_ALIGNED_MASK          (0x07u)
#define FLS_PGM_PHRASE_BYTE_SIZE                   (0x08u)
#define FLS_ERASE_SECTION_SIZE                     (0x800)
#define FLS_FASTRAM_ADDR                           (0x14000000UL)

/*******************************************************************************
 * the typedefs
 ******************************************************************************/

typedef enum
{
    FLS_STD_RETURN_OK = 0u,
    FLS_STD_RETURN_ADDR_INVALID,
    FLS_STD_RETURN_DATA_INVALID,
    FLS_STD_RETURN_LEN_INVALID,
    FLS_STD_RETURN_FLASH_DRIVER_INVALID,
    FLS_STD_RETURN_PGM_NG,
    FLS_STD_RETURN_CHECK_PHASE_NG,
    FLS_STD_RETURN_ERASE_NG,
    FLS_STD_RETURN_PENDING,
    FLS_STD_RETURN_BUSY,
    FLS_STD_RETURN_TIMEOUT

} Bootloader_FlsStdRtnType;

typedef struct
{
    uint32_t memStartAddr;
    uint32_t memSize;
    uint32_t crcStartAddr;
    uint32_t crcSize;
} Bootloadder_CfgType;

typedef struct
{
    uint8_t bootVersionGeneration;
    uint8_t bootVersionMajor;
    uint8_t bootVersionMinor;
    uint8_t bootVersionRevision;
} Bootloade_CfgInfoType;

/*******************************************************************************
 * the globals
 ******************************************************************************/

/*******************************************************************************
 * the function prototypes
 ******************************************************************************/

#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* _BOOTLOADER_CFG_H_ */
