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

/*!
 * @file lin.c
 *
 * @brief This file contains the user lin function.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "lin.h"
#include "ac780x_gpio.h"
#include "ac7803x.h"
#include "ac780x_uart.h"
#include "ac780x_lin.h"

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
static lin_user_config_t s_linUserConfig = {0U}; /* LIN user configuration */
static lin_state_t s_linStateUser = {0U};        /* LIN state */

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

/* ======================================  Functions define  ======================================== */
/*!
 * @brief       LIN_InitGPIO
 *              Pin initialization
 * @param[in]  none
 *
 * @return int
 */
static void LIN_InitGPIO(void)
{
    /*LIN transceiver sleep pin initialization */
    GPIO_SetFunc(LIN_SLEEP_GPIO, LIN_SLEEP_PIN, GPIO_FUN0);
    GPIO_SetDir(LIN_SLEEP_GPIO, LIN_SLEEP_PIN, GPIO_OUT);
    GPIO_SetPinLevel(LIN_SLEEP_GPIO, LIN_SLEEP_PIN, GPIO_LEVEL_HIGH);

    /*LIN TX RX pin multiplexing function*/
    GPIO_SetFunc(LIN_TX_GPIO, LIN_TX_PIN, GPIO_FUN3);
    GPIO_SetFunc(LIN_RX_GPIO, LIN_RX_PIN, GPIO_FUN3);
}

/*!
 * @brief     LIN_IntCallback
 *            user LIN interrupt callback function.
 * @param[in] instance: LIN instance
 * @param[in] linState: pointer to LIN state
 * @return    none
 */
void LIN_IntCallback(uint8_t instance, lin_state_t *linState)
{
    uint8_t linfram[8] = {0x0, 0x1, 0x23, 0x3, 0x65, 0x48, 0x5, 0x24};

    lin_frame_info_t frameInfo = {0x01, LIN_DIR_TX, LIN_ENHANCED_CHECKSUM, 0x08, 0x01, linfram};

    switch (linState->currentEventId)
    {
    case LIN_RECV_BREAK_FIELD_OK:
        break;

    case LIN_PID_OK:
        (void)LIN_DRV_SendFrameData(instance, frameInfo.data, frameInfo.length);
        break;

    case LIN_RX_COMPLETED:
    case LIN_TX_COMPLETED:
        break;

    case LIN_SYNC_ERROR:
    case LIN_PID_ERROR:
    case LIN_FRAME_ERROR:
    case LIN_READBACK_ERROR:
    case LIN_CHECKSUM_ERROR:
    case LIN_RX_OVERRUN:
        break;

    case LIN_WAKEUP_SIGNAL:
        break;

    default:
        break;
    }
}

/*!
 * @brief       LIN_Init
 *              LIN module and pin initialization required for LIN module
 *              LIN is configured as master mode, and Baudrate is 19200
 * @param[in]   instance: LIN module instance
 * @param[in]   mode: Configure the lin node to either host mode or slave mode
 * @param[in]   baudrate: LIN module baudrate
 * @return      none
 *
 */
void LIN_Init(uint8_t instance, uint8_t mode, uint16_t baudrate)
{
    printf("[LIN%d]mode:%d baudrate:%d\r\n", instance, mode, baudrate);

    s_linUserConfig.nodeFunction = (bool)mode;            /*master mode*/
    s_linUserConfig.baudRate = baudrate;                  /*lin baudrate*/
    s_linUserConfig.breakLength = LIN_BREAK_LENGTH;       /*lin break length*/
    s_linUserConfig.breakThreshold = LIN_BREAK_THRESHOLD; /*lin break length detection bits*/

    s_linStateUser.Callback = (lin_callback_t)LIN_IntCallback; /*user lin callback function*/

    LIN_InitGPIO(); /*Pin initialization*/

    (void)LIN_DRV_Init(instance, &s_linUserConfig, &s_linStateUser);
}

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