/********************************************************************************
* @file    Uart_LINMaster.c
* @author  SOC AE Team
* @version V1.1
* @date    2025-06-27
* @brief   UART2 LIN从机模式配置。
*          通过宏定义LIN_MODE选择UART2_LIN的从机模式：
*          0：SlaveSend  从机发送 1：SlaveRecive 从机接收
*          
*******************************************************************************/
#if defined(SC32f12xx) 
#include "sc32_conf.h"
#include "Function_Init.h"
/* UART_LIN从机模式配置 */
//(SlaveSend:LIN_MODE=0,SlaveRecive:LIN_MODE=1)
#define LIN_MODE 1

#if (TestNum == 37)
volatile FlagStatus UART_BkFlag = RESET;
volatile FlagStatus UART_RxFlag = RESET;
volatile FlagStatus UART_TxFlag = RESET;
volatile FlagStatus ReceivingFlag = RESET;      //正在接收数据标志位
static uint8_t PidTemp = 0;                     //暂存接收到PID数据

typedef struct
{
  uint16_t id;          //从机id号
  uint16_t LIN_TxSize;  //从机待发送数据大小（一帧数据不大于8字节）
  uint8_t LIN_TxBuf[9]; //发送缓存，最多发送8个字节和一个校验和
}LIN_SS_TypeDef;        //从机发送结构体

typedef struct
{
  uint16_t id;          //从机id号
  uint16_t LIN_RxSize;  //待接收数据大小（一帧数据不大于8字节）
  uint8_t LIN_RxBuf[9]; //发送缓存，最多接收8个字节和一个校验和
}LIN_SR_TypeDef;        //从机接收结构体


void UART_LINSlave_Function(void)
{
  //配置UART2外设时钟
  RCC_APB1Cmd(ENABLE);
  RCC_HCLKConfig(RCC_SYSCLK_Div1);
  RCC_APB1Config(RCC_HCLK_Div1);	
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2,ENABLE);
  UART_TXCmd(UART2,ENABLE);		//UART2 TX使能
  UART_RXCmd(UART2,ENABLE);             //UART2 RX使能
  UART_ITConfig(UART2,UART_IT_BK | UART_IT_RX | UART_IT_TX |UART_IT_EN|UART_IT_SY, ENABLE);
#if defined(SC32f10xx)||defined(SC32f12xx) 
  NVIC_EnableIRQ(UART0_2_4_IRQn);	//UART2中断使能

#endif 
  
  //UART2 LIN的初始化配置
  UART_InitTypeDef UART_InitStruct;
  UART_InitStruct.UART_BaudRate = 19200;	        //设置UART2波特率为19200
#if defined(SC32f12xx) 
  UART_InitStruct.UART_ClockFrequency = 32000000;	//APB时钟频率为32Mhz
#endif
  UART_InitStruct.UART_Mode = UART_Mode_10B;	        //UART2工作在模式1
  UART_Init(UART2,&UART_InitStruct);
  
  UART_LIN_MODE(UART2,UART_SLAVER);	                //设置UART2为LIN主机模式
  UART_LIN_BKSIZE(UART2,UART_BKSIZE_13);	        //13位长度发送间隔段
  UART_LIN_SLVARENE(UART2,ENABLE);	         
  #if (LIN_MODE == 0)
  //从机发送配置
  LIN_SS_TypeDef SlaveSend;                             //定义从机机发送变量
  SlaveSend.id = 0x26;
  SlaveSend.LIN_TxSize = 8;
  SlaveSend.LIN_TxBuf[0] = 0x18;
  SlaveSend.LIN_TxBuf[1] = 0x19;
  SlaveSend.LIN_TxBuf[2] = 0x21;
  SlaveSend.LIN_TxBuf[3] = 0x22;
  SlaveSend.LIN_TxBuf[4] = 0x23;
  SlaveSend.LIN_TxBuf[5] = 0x24;
  SlaveSend.LIN_TxBuf[6] = 0x25;
  SlaveSend.LIN_TxBuf[7] = 0x26;
  uint8_t pid = LIN_CalID(SlaveSend.id);               //计算PID
  SlaveSend.LIN_TxBuf[8] = LINCalChecksum(SlaveSend.id,SlaveSend.LIN_TxBuf,8);//存放8位数据的增强效验和
  #endif
  
  #if (LIN_MODE == 1)
  LIN_SR_TypeDef SlaveReceive;                         //定义从机发送变量
  SlaveReceive.id = 0x22;
  SlaveReceive.LIN_RxSize = 8;
  for (int i=0;i<=SlaveReceive.LIN_RxSize;i++)
  {
    SlaveReceive.LIN_RxBuf[i] = 0;                    //对接收数组初始化
  }
  uint8_t pid = LIN_CalID(SlaveReceive.id);           //计算PID
  #endif
  
  while(1)
  {
    #if (LIN_MODE == 0)
    if (pid == PidTemp)
    {
      ReceivingFlag = SET;
      for (int i=0;i<=SlaveSend.LIN_TxSize;i++)
      {
        UART_SendData(UART2,SlaveSend.LIN_TxBuf[i]);  //发送数据及增强校验和
        while(UART_TxFlag == RESET);             //等待发送完成
        UART_TxFlag = RESET;
      }
      ReceivingFlag = RESET;
      PidTemp = 0;
      printf("Sending Success!\r\n");
    }
    #endif
    #if (LIN_MODE == 1)
    if (pid == PidTemp)
    {
      ReceivingFlag = SET;
      for (int i=0;i<=SlaveReceive.LIN_RxSize;i++)
      {
        while(UART_RxFlag == RESET);             //等待接收数据
        UART_RxFlag = RESET;
        SlaveReceive.LIN_RxBuf[i] = UART_ReceiveData(UART2);//接收8位数据
      }
      ReceivingFlag = RESET;
      PidTemp = 0;
      for(int i=0;i<=SlaveReceive.LIN_RxSize;i++)
      {
        printf("Receivedata[%d]=%x\r\n", i,SlaveReceive.LIN_RxBuf[i]);
      }
    }
    #endif
  }
}

void UART_LINSlave_UART2Handler(void)
{
  if(UART_GetFlagStatus(UART2, UART_Flag_BK)) //break标志，发送/接收完break置起
  {
    UART_ClearFlag(UART2,UART_Flag_BK);
    UART_BkFlag = SET;
  }
  if(UART_GetFlagStatus(UART2,UART_Flag_SY)) //波特率同步完成标志,该位在同步域0x55后置起
  {
    UART_ClearFlag(UART2, UART_Flag_SY);
  }
  if(UART_GetFlagStatus(UART2,UART_Flag_TX)) //发送标志位，该位在发送一字节数据后置起
  {
    UART_ClearFlag(UART2,UART_Flag_TX);
    UART_TxFlag = SET;
  }
  if(UART_GetFlagStatus(UART2,UART_Flag_RX)) //接收标志位，该位在接收一字节数据后置起
  {
    UART_ClearFlag(UART2, UART_Flag_RX);
    UART_RxFlag = SET;
    if (ReceivingFlag == RESET)
    {
      UART_RxFlag = RESET;
      PidTemp = UART_ReceiveData(UART2);   
    }
  }
}
#endif
#endif

