/*
 * 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   (1u)
/* 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       (1u)
/* The value of unused flash space value to be filled */
#define BOOTLOADER_CFG_FLASH_FILL_PAD_VALUE   (0xFFFFFFFF)

/* 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)
/* Asw code tail id to show asw is not empty */
#define BOOTLOADER_CFG_TAIL_MASK              (0xDDCCBBAAul)
/* Once pgm block size bytes num to be written to flash */
#define BOOTLOADER_CFG_PGM_BYTES_SIZE         (1024)
/* 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          (0x000100C0ul)
/* The App flash code stored to be end at this address */
#define BOOTLOADER_CFG_APP_CODE_END_ADDR      (0x00037FFBul)
/* The App tail stored to be at this address */
#define BOOTLOADER_CFG_APP_TAIL_ADDR          (0x00037FF8ul)
/* The App flash crc stored at this address */
#define BOOTLOADER_CFG_APP_CRC_ADDR           (0x00037FFCul)
/* 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               ((*((uint32_t *)BOOTLOADER_CFG_APP_TAIL_ADDR)) - BOOTLOADER_CFG_APP_START_ADDR)
/* 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 (0x200033F0)
#define BOOTLOADER_CFG_FLS_DRV_RAM_CRC_ADDR   (0x20003FEC)
#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)

#define BOOTLOADER_CFG_NO_INIT_RAM_START_ADDR (0x20003FF0)
#define BOOTLOADER_CFG_NO_INIT_RAM_BYTE_SIZE  (16)

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

/* 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)

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

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

typedef struct
{
    uint8_t HWVersion[4];
    uint8_t BLVersion[8];
    uint8_t bootBuildTime[12];
    uint8_t bootBuildDate[16];
    uint8_t ECUName[8];
    uint8_t Reverse[16];
} Bootloade_CfgInfoType;//64 byte

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

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

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

#endif /* _BOOTLOADER_CFG_H_ */
