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

/******************************************************************************
 * @version: V1.0.0  demo
 ******************************************************************************/

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

/* ===========================================  Includes  =========================================== */
#include <string.h>
#include "ac780x_uart.h"
#include "ac780x_uart_reg.h"
#include "ac780x_gpio.h"
#include "lin.h"
/* ============================================  Define  ============================================ */

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

/* ==========================================  Variables  =========================================== */

uint8_t g_LinDataTx[11] = {0};

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

/* ======================================  Functions define  ======================================== */


 
/**
 * UartLIN_Handler
 *
 * @param[in] device
 * @param[in] wpara
 * @param[in] lpara
 * @return : none
 *
 * @brief  Uart lin interrupt handle function
 */
void UartLIN_Handler(void *device, uint32_t wpara, uint32_t lpara)
{
    /*!Receive break field*/
    if (0 != (lpara & UART_LSR1_FBRK_Msk))
    {
		/*！<write 1 to clear break status*/
        UART_LIN->LSR1 |= UART_LSR1_FBRK_Msk;   
    }
    /*!Receive data*/
    if (0 != (wpara & UART_LSR0_DR_Msk))
    {

    }
    /*!Transmit data done*/
    if (0 != (wpara & UART_LSR0_THRE_Msk))
    {

    }

}

/**
 * SendSimpleLinMassage
 *
 * @param[in] : none
 * @return : none
 *
 * @brief  Uart lin Send Frame function
 */
void SendSimpleLinMessage(void)
{
    static uint16_t checksum = 0;
    static uint8_t i = 0, a = 0;
    static uint8_t j = 0;
    a++;

    UART_SendLINBreak(UART0);
    
    /*!LIN数据填充，包括同步场和校验场*/
    g_LinDataTx[0] = 0x55; 
    g_LinDataTx[1] = 0x50;
    g_LinDataTx[2] = a+1;
    g_LinDataTx[3] = a+2;
    g_LinDataTx[4] = a+3;
    g_LinDataTx[5] = a+4;
    g_LinDataTx[6] = a+5;
    g_LinDataTx[7] = a+6;
    g_LinDataTx[8] = a+7;
    g_LinDataTx[9] = a+8;

    /*！增强型校验和需要加上ID*/
    checksum = g_LinDataTx[1];
    for (i = 2; i < 10; i++)
    {
        checksum += g_LinDataTx[i];
        if (checksum >= 0x100)
        {
            checksum -= 0xFF;
        }
    }
    checksum ^= 0xFF;
    /*！数据校验和*/
    g_LinDataTx[10] = checksum;

    /*！发送同步域，ID，数据域，校验和*/
    for(j = 0;j < 11;j++)
    {
        UART_SendData(UART0, g_LinDataTx[j]);
    }
    checksum = 0;
}

/**
 * LIN_InitLin
 *
 * @param[in] mode: master or slave mode
 * @param[in] schTblIdx: schedule table index
 * @param[in] baudrate: baud rate
 * @return : none
 *
 * @brief  Init uart LIN module
 */ 
void LIN_InitLin(uint8_t mode, uint8_t schTblIdx, uint16_t baudrate)
{
    UART_ConfigType uartConfig;

    memset((void *)&uartConfig, 0, sizeof(UART_ConfigType));  

    /*！初始化LIN的引脚功能
    包括RX/TX及LIN的收发器引脚*/
    GPIO_SetFunc(SWLIN_RX, RXPinFunc);
    GPIO_SetFunc(SWLIN_TX, TXPinFunc); 
    GPIO_SetDir(SWLIN_SLP, GPIO_OUT);
    GPIO_SetPinLevel(SWLIN_SLP, GPIO_LEVEL_HIGH); 
    
    /*！初始化Uart_LIN及相关参数
    包括波特率及其他配置*/
    uartConfig.baudrate = baudrate;
    uartConfig.dataBits = UART_WORD_LEN_8BIT;
    uartConfig.stopBits = UART_STOP_1BIT;
    uartConfig.parity = UART_PARI_NO;
    uartConfig.fifoByteEn = DISABLE;
    uartConfig.sampleCnt = UART_SMP_CNT0;
    uartConfig.callBack = UartLIN_Handler;
    
    UART_Init(UART_LIN, &uartConfig);

	/*！配置LIN需要使能的功能*/
    UART_SetLIN(UART_LIN, UART_LINCR_LINEN_Msk | UART_LINCR_LBRKIE_Msk | UART_LINCR_LBRKDL_Msk); 
       
    /*！配置LIN模块相关中断*/
    UART_SetRXNEInterrupt(UART_LIN, DISABLE);
    UART_SetTXEInterrupt(UART_LIN, DISABLE);
    NVIC_ClearPendingIRQ(UART_LIN_IRQn);
    NVIC_EnableIRQ(UART_LIN_IRQn);
}
