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

/* UART_LIN主机模式配置 */
//(MasterSend:LIN_MODE=0,MaserRecive:LIN_MODE=1)
#define LIN_MODE 1

#if (TestNum == 36)
volatile FlagStatus UART_BkFlag = RESET;
volatile FlagStatus UART_RxFlag = RESET;
volatile FlagStatus UART_TxFlag = RESET;


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

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

void UART_LINMaster_Function(void)
{
  //配置UART2外设时钟
  RCC_APB1Cmd(ENABLE);
  RCC_HCLKConfig(RCC_SYSCLK_Div1);
  RCC_APB1Config(RCC_HCLK_Div1);	//LIN时钟为32Mhz
  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, 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(SC32f10xx)||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_MASTER);	                //设置UART2为LIN主机模式
  UART_LIN_BKSIZE(UART2,UART_BKSIZE_13);	        //13位长度发送间隔段
  
  #if (LIN_MODE == 0)
    //主机发送配置
    LIN_MS_TypeDef MasterSend;                               //定义主机发送变量
    MasterSend.id = 0x26;
    MasterSend.LIN_TxSize = 8;
    MasterSend.LIN_TxBuf[0] = 0x01;
    MasterSend.LIN_TxBuf[1] = 0x02;
    MasterSend.LIN_TxBuf[2] = 0x03;
    MasterSend.LIN_TxBuf[3] = 0x04;
    MasterSend.LIN_TxBuf[4] = 0x05;
    MasterSend.LIN_TxBuf[5] = 0x06;
    MasterSend.LIN_TxBuf[6] = 0x07;
    MasterSend.LIN_TxBuf[7] = 0x08;
    uint8_t pid = LIN_CalID(MasterSend.id);                 //计算PID
    uint8_t CRC_sum = LINCalChecksum(MasterSend.id,MasterSend.LIN_TxBuf,8);//存放8位数据的增强效验和
    
    uint8_t i = 0;
    UART_SendBreak();                          //发送间隔段
    while(UART_BkFlag == RESET);              //等待发送完成
    UART_BkFlag = RESET;
    
    UART_SendData(UART2,0x55);                //发送同步段
    while(UART_TxFlag == RESET);             //等待发送完成
    UART_TxFlag = RESET;
    
    UART_SendData(UART2,pid);                //发送PID
    while(UART_TxFlag == RESET);             //等待发送完成
    UART_TxFlag = RESET;
    for (i=0;i<MasterSend.LIN_TxSize;i++)
    {
      UART_SendData(UART2,MasterSend.LIN_TxBuf[i]);//发送8位数据
      while(UART_TxFlag == RESET);                //等待发送完成
      UART_TxFlag = RESET;
    }
    
    UART_SendData(UART2,CRC_sum);             //发送增强效验和
    while(UART_TxFlag == RESET);             //等待发送完成
    UART_TxFlag = RESET;
    printf("Sending Success!\r\n");
  #endif
   
  #if (LIN_MODE == 1)
  //主机接收配置
  LIN_MR_TypeDef MasterReceive;              //定义主机接收变量
  MasterReceive.id = 0x27;
  MasterReceive.LIN_RxSize = 8;
  for (int i=0;i<9;i++)                     //清空接收缓存
  {
    MasterReceive.LIN_RxBuf[i] = 0;
  }
  
  int8_t i = 0;
  uint8_t pid = LIN_CalID(MasterReceive.id);    //计算PID
  UART_SendBreak();                          //发送间隔段
  while(UART_BkFlag == RESET);              //等待发送完成
  UART_BkFlag = RESET;
  
  UART_SendData(UART2,0x55);                //发送同步段
  while(UART_TxFlag == RESET);             //等待发送完成
  UART_TxFlag = RESET;
  while(UART_RxFlag == RESET);             //同步段发送时，主机同时也会接收同步段数据
  UART_RxFlag = RESET;
	
  UART_SendData(UART2,pid);                //发送PID
  while(UART_TxFlag == RESET);             //等待发送完成
  UART_TxFlag = RESET;
  while(UART_RxFlag == RESET);             //PID发送时，主机同时也会接收PID数据
  UART_RxFlag = RESET;
  for (i=0;i<=MasterReceive.LIN_RxSize;i++)
  {
    while(UART_RxFlag == RESET);             //等待接收数据
    UART_RxFlag = RESET;
    MasterReceive.LIN_RxBuf[i] = UART_ReceiveData(UART2);//接收8位数据及校验和
  }
  for(int i=0;i<MasterReceive.LIN_RxSize;i++)
  {
    printf("Receivedata[%d]=%X\r\n", i,MasterReceive.LIN_RxBuf[i]);
  }
#endif
  while(1)
  {
  }
}

void UART_LINMaster_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;
  }
}
#endif
#endif
