/* 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) 2023. 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.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

/*!
 *Routine Description:
 *
 *
 *
 */

/*!
 * @file canbusoff.c
 *
 * @brief This file is main.c.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "can.h"
#include "ac780x_can.h"
#include "ac780x_can_reg.h"
#include "canbusoff.h"
/* ============================================  Define  ============================================ */
#define CAN_FAST_RVCO_CNT       (5)     /* Fast recovery times */
#define CAN_FAST_RVCO_TIMER     (50)    /* fast recovery time (ms) */
#define CAN_SLOW_RVCO_TIMER     (200)   /* slow recovery time (ms) */
#define CAN_RVCO_TIMER          (50)    /* Exit fast and slow recovery time (ms) */
/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
CAN_BusoffConfig_t g_canBuoffCtrl;   

/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */
/*!
* @brief   busoff init
*
* @param[in] none
* @return  none
*/
void CAN_BusoffInit(void)
{
    g_canBuoffCtrl.busoffState = Busoff_check;
    g_canBuoffCtrl.canRecoverCnt = 0;
    g_canBuoffCtrl.canRecoverTimerCnt = 0;
    g_canBuoffCtrl.canTimerOut = 0;
    g_canBuoffCtrl.canState = CAN_NORMAL;
}

/*!
* @brief   Get the can status of the current busoff processing logic
*
* @param[in] none
* @return  CAN_State_t
*/
CAN_State_t CAN_GetBusoffState(void)
{
    return g_canBuoffCtrl.canState;
}

/*!
* @brief   Set CAN to busoff state
*
* @param[in] none
* @return  none
*/
void CAN_SetBusoffState(void)
{
    g_canBuoffCtrl.canState = CAN_BUSOFF;
}

/*!
* @brief   Time processing of busoff
*          It is recommended to place a timing interrupt of 1ms
*
* @param[in] none
* @return  none
*/
void CAN_BusoffTimerCnt(void)
{
    if(g_canBuoffCtrl.canRecoverCnt)
    {
        g_canBuoffCtrl.canRecoverTimerCnt++;
        g_canBuoffCtrl.canTimerOut++;
    }
}

/*!
* @brief   Busoff state switching processing
*
* @param[in] none
* @return  none
*/
void CAN_BusoffProcess(void)
{
    switch(g_canBuoffCtrl.busoffState)
    {
        /* Status detection of busoff,*/
        case Busoff_check:
        {
            if(g_canBuoffCtrl.canState == CAN_NORMAL)
            {
                /* Exit fast and slow recovery Process */
                if(g_canBuoffCtrl.canTimerOut > CAN_RVCO_TIMER) 
                {
                    g_canBuoffCtrl.canTimerOut = 0;
                    g_canBuoffCtrl.canRecoverCnt = 0;
                    g_canBuoffCtrl.canRecoverTimerCnt = 0;
                }
                else
                {
                    /* user confing*/
                }
            }
            else
            {
                g_canBuoffCtrl.canRecoverCnt++;
                g_canBuoffCtrl.canTimerOut = 0;
                if(g_canBuoffCtrl.canRecoverCnt < (CAN_FAST_RVCO_CNT +1 ))
                {
                    g_canBuoffCtrl.busoffState = Fast_Recover;
                }
                else
                {
                    g_canBuoffCtrl.canRecoverCnt = CAN_FAST_RVCO_CNT + 1;
                    g_canBuoffCtrl.busoffState = Slow_Recover;
                }
            }
        }
        break;
        
        /* fast recovery processing */
        case Fast_Recover:
        {
            /* Fast recovery timeout, CAN joins network communication */
            if(g_canBuoffCtrl.canRecoverTimerCnt > CAN_FAST_RVCO_TIMER)
            {
                printf("Fast_Recover\r\n");
                g_canBuoffCtrl.canTimerOut = 0;
                g_canBuoffCtrl.canRecoverTimerCnt = 0;
                
                /* Clear the CAN1 BUSOFF flag, Need to pay attention to which CAN sign */
                CAN_ExitBusOff(CAN1);
                
                /* Exit the reset state of CAN1, Need to pay attention to which CAN sign */
                CAN_SetReset(CAN1, DISABLE);
                
                g_canBuoffCtrl.canState = CAN_NORMAL;
                g_canBuoffCtrl.busoffState = Busoff_check;
            }
        }
        break;
        
        /* slow recovery processing */
        case Slow_Recover:
        {
            /* slow recovery timeout, CAN joins network communication */
            if(g_canBuoffCtrl.canRecoverTimerCnt > CAN_SLOW_RVCO_TIMER)
            {
                printf("Slow_Recover\r\n");
                g_canBuoffCtrl.canTimerOut = 0;
                g_canBuoffCtrl.canRecoverTimerCnt = 0;
                
                /* Clear the CAN1 BUSOFF flag, Need to pay attention to which CAN sign*/
                CAN_ExitBusOff(CAN1);
                
                /* Exit the reset state of CAN1, Need to pay attention to which CAN sign*/
                CAN_SetReset(CAN1,DISABLE);
                
                g_canBuoffCtrl.canState = CAN_NORMAL;
                g_canBuoffCtrl.busoffState = Busoff_check;
            }
        }
        break;
        
        default:
            break;
    }
}

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