/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2022. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

#ifndef _AC780X_EFLASH_REG_H
#define _AC780X_EFLASH_REG_H
/*!
 * @file ac780x_eflash_reg.h
 *
 * @brief EFLASH access register inline function definition.
 *
 */
#ifdef __cplusplus
extern "C" {
#endif

/* ===========================================  Includes  =========================================== */
#include "ac780x_eflash.h"

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Set eflash key sequence register
 *
 * @param[in] keyValue: unlock eflash controler key value
 * @return none
 */
__STATIC_INLINE void EFLASH_SetKeyReg(uint32_t keyValue)
{
    EFLASH->UKR = keyValue;
}

/*!
 * @brief Get protect status
 *
 * @param[in] none
 * @return 0: not in read protection, 1: in read protection
 */
__STATIC_INLINE uint8_t EFLASH_GetReadProtectReg(void)
{
    return ((EFLASH->GSR & EFLASH_GSR_RPRT_Msk) >> EFLASH_GSR_RPRT_Pos);
}

/*!
 * @brief Get the can eflash controler lock bit
 *
 * param[in] none
 * @return 0: eFlash control is unlocked, 1: eFlash control is locked
 */
__STATIC_INLINE uint8_t EFLASH_GetCtrlLockBitReg(void)
{
    return (EFLASH->GSR & EFLASH_GSR_LOCK_Msk) >> EFLASH_GSR_LOCK_Pos;
}

/*!
 * @brief Lock eflash controler
 *
 * @param[in] none
 * @return none
 */
__STATIC_INLINE void EFLASH_LockCtrlReg(void)
{
    EFLASH->GSR |= EFLASH_GSR_LOCK_Msk;
}

/*!
 * @brief Set program or erase start address
 *
 * @param[in] startAddress: start address
 * @return none
 */
__STATIC_INLINE void EFLASH_SetStartAddressReg(uint32_t startAddress)
{
    EFLASH->CAR = startAddress;
}

/*!
 * @brief Set command
 *
 * @param[in] cmd: eFlash command
 * @return none
 */
__STATIC_INLINE void EFLASH_SetCommandReg(uint32_t cmd)
{
    MODIFY_REG32(EFLASH->CCR, EFLASH_CCR_CMD_Msk, EFLASH_CCR_CMD_Pos, cmd);
}

/*!
 * @brief Set eflash length register
 *
 * @param[in] len: length(unit is 32-bit)
 * @return none
 */
__STATIC_INLINE void EFLASH_SetLengthReg(uint32_t len)
{
    MODIFY_REG32(EFLASH->CCR, EFLASH_CCR_LEN_Msk, EFLASH_CCR_LEN_Pos, len);
}

/*!
 * @brief Set eflash data register
 *
 * @param[in] data: eflash data
 * @return none
 */
__STATIC_INLINE void EFLASH_SetDataReg(uint32_t data)
{
    EFLASH->CDR = data;
}

/*!
 * @brief Set eflash data msak register
 *
 * @param[in] mask: mask data
 * @return none
 */
__STATIC_INLINE void EFLASH_SetDataMaskReg(uint8_t mask)
{
    MODIFY_REG32(EFLASH->DMR, EFLASH_DMR_DATAMASK_Msk, EFLASH_DMR_DATAMASK_Pos, mask);
}

/*!
* @brief Trigger the command to start
*
* @param[in] none
* @return none
*/
__STATIC_INLINE void EFLASH_TrigCtrlCmdReg(void)
{
    EFLASH->CCR |= EFLASH_CCR_START_Msk;
}

/*!
* @brief  Get status that if program the last data
*
* @param[in]  none
* @return
   0: not last data
   1: last data
*/
__STATIC_INLINE uint8_t EFLASH_GetLastDataStatusReg(void)
{
    return ((EFLASH->CSR & EFLASH_CSR_DBUFLST_Msk) >> EFLASH_CSR_DBUFLST_Pos);
}

/*!
* @brief  Get status that if data buffer ready
*
* @param[in]  none
* @return
   0: data buffer not ready
   1: data buffer ready
*/
__STATIC_INLINE uint8_t EFLASH_GetDataBufferReg(void)
{
    return ((EFLASH->CSR & EFLASH_CSR_DBUFRDY_Msk) >> EFLASH_CSR_DBUFRDY_Pos);
}

/*!
 * @brief Set eflash command select register
 *
 * @param[in] sel: select P-Flash or D-Flash
    0: P-Flash
    1: D-Flash
 * @return none
 */
__STATIC_INLINE void EFLASH_SetSelectReg(uint8_t sel)
{
    MODIFY_REG32(EFLASH->CCR, EFLASH_CCR_SEL_Msk, EFLASH_CCR_SEL_Pos, sel);
}

/*!
 * @brief set option byte enable register
 *
 * @param[in] enable: enable/disable operation option byte area
    0: disable
    1: enable
 * @return none
 */
__STATIC_INLINE void EFLASH_EnableOptionReg(uint8_t enable)
{
    MODIFY_REG32(EFLASH->CCR, EFLASH_CCR_OPTEN_Msk, EFLASH_CCR_OPTEN_Pos, enable);
}

/*!
 * @brief Hardfault interrupt enable
 *
 * @param[in] none
 * @return none
 */
__STATIC_INLINE void EFLASH_EnableCtrlHardfault(void)
{
    EFLASH->GCR |= EFLASH_GCR_HDFEN_Msk;
}

/*!
 * @brief Hardfault interrupt Disable
 *
 * @param[in] none
 * @return none
 */
__STATIC_INLINE void EFLASH_DisableCtrlHardfault(void)
{
    EFLASH->GCR &= (~EFLASH_GCR_HDFEN_Msk);
}

/*!
* @brief  Get status that indicate whether any of the command operations is in process
*
* @param[in]  none
* @return
   0: all operations are not in process
   1: at least one operation in process
*/
__STATIC_INLINE uint8_t EFLASH_GetCmdBusyStatus(void)
{
    return ((EFLASH->CSR & EFLASH_CSR_CMDBUSY_Msk) >> EFLASH_CSR_CMDBUSY_Pos);
}

/*!
* @brief Get the error status for  main memory
*
* @param[in] none
* @return status value
*/
__STATIC_INLINE uint32_t EFLASH_GetStatusReg(void)
{
    return (EFLASH->CSR & EFLASH_MAIN_STAT_ERROR_BITS);
}

/*!
* @brief Get the error status for option memory
*
* @param[in] none
* @return status value
*/
__STATIC_INLINE uint32_t EFLASH_GetOptionStatusReg(void)
{
    return (EFLASH->CSR & EFLASH_OPTION_STAT_ERROR_BITS);
}

/*!
* @brief force the program command operation to be finished
*
* @param  none
* @return none
*/
__STATIC_INLINE void EFLASH_FlushOperation(void)
{
    MODIFY_REG32(EFLASH->CCR, EFLASH_CCR_ABORT_Msk, EFLASH_CCR_ABORT_Pos, 1);
}

/*!
* @brief Get P-Flash write protect register 0 value
*
* @param[in] none
* @return Write protect register 0 value
*/
__STATIC_INLINE uint32_t EFLASH_GetWriteProReg0(void)
{
    return (EFLASH->PWPR0);
}

/*!
* @brief Get P-Flash 2rite protect register 1 value
*
* @param[in] none
* @return Write protect register 1 value
*/
__STATIC_INLINE uint32_t EFLASH_GetWriteProReg1(void)
{
    return (EFLASH->PWPR1);
}

/*!
* @brief Get D-flash Write protect register value
*
* @param[in] none
* @return Write protect register 2 value
*/
__STATIC_INLINE uint32_t EFLASH_DFGetWriteProReg(void)
{
    return (EFLASH->DWPR);
}

#ifdef __cplusplus
}
#endif

#endif /* _AC780X_EFLASH_REG_H */

/* =============================================  EOF  ============================================== */
