/**
  *******************************************************************************************************
  * @file    fm33fr0xx_fl_aes.c
  * @author  FMSH Application Team
  * @brief   Src file of AES FL Module
  *******************************************************************************************************
  * @attention
  *
  * Copyright (c) [2021] [Fudan Microelectronics]
  * THIS SOFTWARE is licensed under Mulan PSL v2.
  * You can use this software according to the terms and conditions of the Mulan PSL v2.
  * You may obtain a copy of Mulan PSL v2 at:
  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  * See the Mulan PSL v2 for more details.
  *
  *******************************************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "fm33fr0xx_fl.h"

/** @addtogroup FM33FR0XX_FL_Driver
  * @{
  */

/** @addtogroup AES
  * @{
  */

#ifdef FL_AES_DRIVER_ENABLED

/* Private macros ------------------------------------------------------------*/
/** @addtogroup AES_FL_Private_Macros
  * @{
  */

#define         IS_FL_AES_INSTANCE(INSTANCE)                (((INSTANCE) == AES))

#define         IS_FL_AES_KEYLENTH(__VALUE__)               (((__VALUE__) == FL_AES_KEY_LENGTH_128B)||\
                                                             ((__VALUE__) == FL_AES_KEY_LENGTH_192B)||\
                                                             ((__VALUE__) == FL_AES_KEY_LENGTH_256B))

#define         IS_FL_AES_CIPHERMODE(__VALUE__)             (((__VALUE__) == FL_AES_CIPHER_ECB)||\
                                                             ((__VALUE__) == FL_AES_CIPHER_CBC)||\
                                                             ((__VALUE__) == FL_AES_CIPHER_CTR)||\
                                                             ((__VALUE__) == FL_AES_CIPHER_MULTH))

#define         IS_FL_AES_OPERATIONMODE(__VALUE__)          (((__VALUE__) == FL_AES_OPERATION_MODE_ENCRYPTION)||\
                                                             ((__VALUE__) == FL_AES_OPERATION_MODE_KEYDERIVATION)||\
                                                             ((__VALUE__) == FL_AES_OPERATION_MODE_DECRYPTION)||\
                                                             ((__VALUE__) == FL_AES_OPERATION_MODE_KEYDERIVATION_DECRYPTION))

#define         IS_FL_AES_DATATYPE(__VALUE__)               (((__VALUE__) == FL_AES_DATA_TYPE_32B)||\
                                                             ((__VALUE__) == FL_AES_DATA_TYPE_16B)||\
                                                             ((__VALUE__) == FL_AES_DATA_TYPE_8B)||\
                                                             ((__VALUE__) == FL_AES_DATA_TYPE_1B))

#define         IS_FL_AES_IVRSWAP(__VALUE__)                (((__VALUE__) == FL_AES_IVR_SWAP_32B)||\
                                                             ((__VALUE__) == FL_AES_IVR_SWAP_16B)||\
                                                             ((__VALUE__) == FL_AES_IVR_SWAP_8B)||\
                                                             ((__VALUE__) == FL_AES_IVR_SWAP_1B))



/**
  * @}
  */

/* Private consts ------------------------------------------------------------*/
/** @addtogroup AES_FL_Private_Consts
  * @{
  */



/**
  * @}
  */

/* Private function prototypes -----------------------------------------------*/



/* Exported functions --------------------------------------------------------*/
/** @addtogroup AES_FL_EF_Init
  * @{
  */

/**
  * @brief  复位AES 外设寄存器值为复位值
  *
  * @param  None
  *
  * @retval ErrorStatus枚举值
  *         -FL_PASS 配置成功
  *         -FL_FAIL 配置过程发生错误
  */
FL_ErrorStatus FL_AES_DeInit(void)
{
    /* 外设复位使能 */
    FL_RMU_EnablePeripheralReset(RMU);
    /* 复位AES */
    FL_RMU_EnableResetAPBPeripheral(RMU, FL_RMU_RSTAPB_AES);
    FL_RMU_DisableResetAPBPeripheral(RMU, FL_RMU_RSTAPB_AES);
    /* 关闭总线时钟 */
    FL_CMU_DisableGroup2BusClock(FL_CMU_GROUP2_BUSCLK_AES);
    /* 锁定外设复位功能 */
    FL_RMU_DisablePeripheralReset(RMU);
    return FL_PASS;
}
/**
  * @brief  根据 AES_InitStructer初始化对应外设入口地址的寄存器值.
  *
  * @param  AESx 外设入口地址
  * @param  AES_InitStructer 指向 @ref FL_AES_InitTypeDef 结构体的指针
  *
  * @retval ErrorStatus枚举值
  *         -FL_PASS 配置成功
  *         -FL_FAIL 配置过程发生错误
  */
FL_ErrorStatus FL_AES_Init(AES_Type *AESx, FL_AES_InitTypeDef *AES_InitStructer)
{
    FL_ErrorStatus status = FL_FAIL;
    if(AES_InitStructer != NULL)
    {
        /* 入口合法性检查 */
        assert_param(IS_FL_AES_INSTANCE(AESx));
        assert_param(IS_FL_AES_KEYLENTH(AES_InitStructer->keyLength));
        assert_param(IS_FL_AES_CIPHERMODE(AES_InitStructer->cipherMode));
        assert_param(IS_FL_AES_OPERATIONMODE(AES_InitStructer->operationMode));
        assert_param(IS_FL_AES_DATATYPE(AES_InitStructer->dataType));
        if(FL_AES_IsEnabled(AESx) == 0)
        {
            /* 开启总线时钟 */
            FL_CMU_EnableGroup2BusClock(FL_CMU_GROUP2_BUSCLK_AES);
            /* key长度 */
            FL_AES_SetKeySize(AESx, AES_InitStructer->keyLength);
            /* 数据流处理模式 */
            FL_AES_SetCipherMode(AESx, AES_InitStructer->cipherMode);
            /* 操作模式 */
            FL_AES_SetOperationMode(AESx, AES_InitStructer->operationMode);
            /* 数据类型 */
            FL_AES_SetDataType(AESx, AES_InitStructer->dataType);
            status = FL_PASS;
        }
        else
        {
            return FL_FAIL;
        }
    }
    return status;
}
/**
  * @brief  设置 AES_InitStruct 为默认配置
  *
  * @param  AES_InitStruct 指向需要将值设置为默认配置的结构体 @ref FL_AES_InitTypeDef 结构体
  *
  * @retval None
  */
void FL_AES_StructInit(FL_AES_InitTypeDef *AES_InitStructer)
{
    if(AES_InitStructer != NULL)
    {
        AES_InitStructer->keyLength     = FL_AES_KEY_LENGTH_128B;
        AES_InitStructer->cipherMode    = FL_AES_CIPHER_ECB;
        AES_InitStructer->operationMode = FL_AES_OPERATION_MODE_ENCRYPTION;
        AES_InitStructer->dataType      = FL_AES_DATA_TYPE_32B;
    }
}

/**
  * @}
  */

/* Private functions ---------------------------------------------------------*/
/** @defgroup AES_FL_Private_Functions AES Private Functions
  * @{
  */



/**
  * @}
  */

#endif /* FL_AES_DRIVER_ENABLED */

/**
  * @}
  */

/**
  * @}
  */

/*************************(C) COPYRIGHT Fudan Microelectronics **** END OF FILE*************************/
