/*
 * 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 _FLS_H_
#define _FLS_H_

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

/*******************************************************************************
 * the includes
 ******************************************************************************/
#include <stdbool.h>
#include <stdint.h>
#include "mcu.h"
#include "bootloader_cfg.h"

#ifdef __cplusplus
extern "C" {
#endif

/*******************************************************************************
 * the defines
 ******************************************************************************/
/* One erase section bytes size is 1024 */
#define FLS_ERASE_SECTION_ADDR_ALIGNED_MASK (0x3FFu)
#define FLS_PGM_SECTION_ADDR_ALIGNED_MASK   (0x07u)
#define FLS_PGM_PHRASE_BYTE_SIZE            (0x08u)
#define FLS_ERASE_SECTION_SIZE              (FLS_ERASE_SECTION_ADDR_ALIGNED_MASK + 1u)
#define FLS_PGM_SECTION_SIZE                (FLS_ERASE_SECTION_SIZE)
#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

} Fls_StdRtnType;

#if(BOOTLOADER_CFG_FLS_CODE_GENERATE_EN != 0u)

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

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

/*! \brief Initializes the Flash driver module
 *
 * This function initializes Flash driver by setting the register
 * instance to it.
 *
 * \param[in] flashDrvInstance : pointer to Flash driver instance
 * \return void
 */
extern
    __attribute__((used, __section__(".ram_flash_driver"))) void
    Fls_Init(uint32_t flashDrvInstance);

/*! \brief Erased the pflash
 *
 * This function erase the length located in the pflash
 *
 * \param[in] targetAddress : pointer to pFlash address
 * \param[in] sectorNum : how many sector to be erased
 * \return Fls_StdRtnType
 */
extern
    __attribute__((used, __section__(".ram_flash_driver")))
    Fls_StdRtnType
    Fls_Erase(uint32_t targetAddress, uint32_t sectorNum);

/*! \brief Write datas to pflash
 *
 * This function wrtie the length bytes to pflash
 *
 * \param[in] targetAddress : the target address to begin to write in pflash
 * \param[in] sourceAddressPtr : pointer to write data buffer
 * \param[in] bytesNum : how many bytes to be written to pflash
 * \return Fls_StdRtnType
 */
extern
    __attribute__((used, __section__(".ram_flash_driver")))
    Fls_StdRtnType
    Fls_Write(uint32_t targetAddress, const uint8_t* sourceAddressPtr, uint32_t bytesNum);

/*! \brief Read datas from pflash
 *
 * This function Read the length bytes from pflash
 *
 * \param[in] targetAddress : the target address to begin to read in pflash
 * \param[in] sourceAddressPtr : pointer to read data buffer
 * \param[in] bytesNum : how many bytes to be read from pflash
 * \return Fls_StdRtnType
 */
extern
    __attribute__((used, __section__(".ram_flash_driver")))
    Fls_StdRtnType
    Fls_Read(uint32_t targetAddress, uint8_t* dataBuff, uint32_t bytesNum);
#else
/*! \brief Read datas from pflash
 *
 * This function Read the length bytes from pflash
 *
 * \param[in] flashDrvInstance : the address of flash driver instance
 * \param[in] targetAddress : the target address to begin to read in pflash
 * \param[in] sourceAddressPtr : pointer to read data buffer
 * \param[in] bytesNum : how many bytes to be read from pflash
 * \return Fls_StdRtnType
 */
extern Fls_StdRtnType
    Fls_Read(uint32_t flashDrvInstance, uint32_t targetAddress, uint8_t* dataBuff, uint32_t bytesNum);

#endif

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