/******************************************************************************
 * @file     lin.h
 * @brief    LIN module header file.
 * @version  V4.0.0
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/


#ifndef LIN_H
#define LIN_H

#ifdef __cplusplus
extern "C" {
#endif


#if defined(SPC1168)
#include "spc1168.h"
#else
#error No defined SPC1168
#endif




/**
 *  @brief  LIN operation definition
 */
typedef enum
{
    LIN_READ                              = 0,    /* Receive LIN response data  */
    LIN_WRITE                             = 1     /* Transfer LIN response data */
} LIN_ReadWriteEnum;




/**
 *  @brief  LIN version
 */
#define LIN_VERSION                     0x220   /* 2.2A */




/**
 *  @brief  LIN frame identifier
 *          Values 0 to 59 (0x3B) are used for signal carrying frames,
 *          60 (0x3C) and 61 (0x3D) are used to carry diagnostic and configuration data,
 *          62 (0x3E) and 63 (0x3F) are reserved for future protocol enhancements
 */
#define LIN_ID_MASTER_REQ               (0x3C)
#define LIN_ID_SLAVE_ACK                (0x3D)




/**
 *  @brief  LIN frame timing definition
 */
#define RESPONSE_SPACE_TIME_MS          (  2)       /* Response space     */
#define INTER_BYTE_SPACE_TIME_MS        (  0)       /* Inter-byte space   */
#define INTER_FRAME_SPACE_TIME_MS       (200)       /* Inter-frame spaqce */




/**
 *  @brief  LIN error status definition
 */
typedef enum
{
    LIN_OK                                = 0x000U,
    LIN_TIMEOUT                           = 0x001U,
    LIN_BUS_OFFLINE                       = 0x002U,
    LIN_BUS_ERROR                         = 0x003U,
    LIN_ID_ERROR                          = 0x004U,

    LIN_MASTER_SYNC_FIELD_ERROR           = 0x100U,

    LIN_SLAVE_SYNC_FIELD_ERROR            = 0x200U,

    LIN_DATA_FIELD_SEND_DATA_FAILED       = 0x300U,
    LIN_DATA_FIELD_SEND_PARITY_FAILED     = 0x310U,
    LIN_DATA_FIELD_SEND_PARITY_ERROR      = 0x320U,

    LIN_DATA_FIELD_RECEIVE_DATA_FAILED    = 0x400U,
    LIN_DATA_FIELD_RECEIVE_PARITY_FAILED  = 0x410U,
    LIN_DATA_FIELD_RECEIVE_PARITY_ERROR   = 0x420U
} LIN_ErrorEnum;




/**
 *  @brief  LIN Public Function Declaration
 */
uint32_t LIN_SendHeader(uint32_t u32Baudrate, uint8_t u8ID, uint32_t u32Timeout);
uint32_t LIN_WaitHeader(uint8_t *u8ID, uint32_t u32Timeout);

uint32_t LIN_SendData(uint8_t u8ID, uint8_t *pu8Data, uint8_t u8Len, uint32_t u32Timeout);
uint32_t LIN_ReceiveData(uint8_t u8ID, uint8_t *pu8Data, uint8_t u8Len, uint32_t u32Timeout);

uint32_t LIN_MasterControl(LIN_ReadWriteEnum RnW, uint32_t u32Baudrate, uint8_t u8ID, uint8_t *pu8Data, uint8_t u8Len, uint32_t u32Timeout);
uint32_t LIN_SlaveControl(LIN_ReadWriteEnum RnW, uint8_t *pu8ID, uint8_t *pu8Data, uint8_t u8Len, uint32_t u32Timeout);


#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* LIN_H */


/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
