L 1 "..\Apps\UART_LINSlave.c"
N/********************************************************************************
N* @file    Uart_LINMaster.c
N* @author  SOC AE Team
N* @version V1.1
N* @date    2025-06-27
N* @brief   UART2 LIN从机模式配置。
N*          通过宏定义LIN_MODE选择UART2_LIN的从机模式：
N*          0：SlaveSend  从机发送 1：SlaveRecive 从机接收
N*          
N*******************************************************************************/
N#if defined(SC32f12xx) 
X#if 0L 
S#include "sc32_conf.h"
S#include "Function_Init.h"
S/* UART_LIN从机模式配置 */
S//(SlaveSend:LIN_MODE=0,SlaveRecive:LIN_MODE=1)
S#define LIN_MODE 1
S
S#if (TestNum == 37)
Svolatile FlagStatus UART_BkFlag = RESET;
Svolatile FlagStatus UART_RxFlag = RESET;
Svolatile FlagStatus UART_TxFlag = RESET;
Svolatile FlagStatus ReceivingFlag = RESET;      //正在接收数据标志位
Sstatic uint8_t PidTemp = 0;                     //暂存接收到PID数据
S
Stypedef struct
S{
S  uint16_t id;          //从机id号
S  uint16_t LIN_TxSize;  //从机待发送数据大小（一帧数据不大于8字节）
S  uint8_t LIN_TxBuf[9]; //发送缓存，最多发送8个字节和一个校验和
S}LIN_SS_TypeDef;        //从机发送结构体
S
Stypedef struct
S{
S  uint16_t id;          //从机id号
S  uint16_t LIN_RxSize;  //待接收数据大小（一帧数据不大于8字节）
S  uint8_t LIN_RxBuf[9]; //发送缓存，最多接收8个字节和一个校验和
S}LIN_SR_TypeDef;        //从机接收结构体
S
S
Svoid UART_LINSlave_Function(void)
S{
S  //配置UART2外设时钟
S  RCC_APB1Cmd(ENABLE);
S  RCC_HCLKConfig(RCC_SYSCLK_Div1);
S  RCC_APB1Config(RCC_HCLK_Div1);	
S  RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART2,ENABLE);
S  UART_TXCmd(UART2,ENABLE);		//UART2 TX使能
S  UART_RXCmd(UART2,ENABLE);             //UART2 RX使能
S  UART_ITConfig(UART2,UART_IT_BK | UART_IT_RX | UART_IT_TX |UART_IT_EN|UART_IT_SY, ENABLE);
S#if defined(SC32f10xx)||defined(SC32f12xx) 
S  NVIC_EnableIRQ(UART0_2_4_IRQn);	//UART2中断使能
S
S#endif 
S  
S  //UART2 LIN的初始化配置
S  UART_InitTypeDef UART_InitStruct;
S  UART_InitStruct.UART_BaudRate = 19200;	        //设置UART2波特率为19200
S#if defined(SC32f12xx) 
S  UART_InitStruct.UART_ClockFrequency = 32000000;	//APB时钟频率为32Mhz
S#endif
S  UART_InitStruct.UART_Mode = UART_Mode_10B;	        //UART2工作在模式1
S  UART_Init(UART2,&UART_InitStruct);
S  
S  UART_LIN_MODE(UART2,UART_SLAVER);	                //设置UART2为LIN主机模式
S  UART_LIN_BKSIZE(UART2,UART_BKSIZE_13);	        //13位长度发送间隔段
S  UART_LIN_SLVARENE(UART2,ENABLE);	         
S  #if (LIN_MODE == 0)
S  //从机发送配置
S  LIN_SS_TypeDef SlaveSend;                             //定义从机机发送变量
S  SlaveSend.id = 0x26;
S  SlaveSend.LIN_TxSize = 8;
S  SlaveSend.LIN_TxBuf[0] = 0x18;
S  SlaveSend.LIN_TxBuf[1] = 0x19;
S  SlaveSend.LIN_TxBuf[2] = 0x21;
S  SlaveSend.LIN_TxBuf[3] = 0x22;
S  SlaveSend.LIN_TxBuf[4] = 0x23;
S  SlaveSend.LIN_TxBuf[5] = 0x24;
S  SlaveSend.LIN_TxBuf[6] = 0x25;
S  SlaveSend.LIN_TxBuf[7] = 0x26;
S  uint8_t pid = LIN_CalID(SlaveSend.id);               //计算PID
S  SlaveSend.LIN_TxBuf[8] = LINCalChecksum(SlaveSend.id,SlaveSend.LIN_TxBuf,8);//存放8位数据的增强效验和
S  #endif
S  
S  #if (LIN_MODE == 1)
S  LIN_SR_TypeDef SlaveReceive;                         //定义从机发送变量
S  SlaveReceive.id = 0x22;
S  SlaveReceive.LIN_RxSize = 8;
S  for (int i=0;i<=SlaveReceive.LIN_RxSize;i++)
S  {
S    SlaveReceive.LIN_RxBuf[i] = 0;                    //对接收数组初始化
S  }
S  uint8_t pid = LIN_CalID(SlaveReceive.id);           //计算PID
S  #endif
S  
S  while(1)
S  {
S    #if (LIN_MODE == 0)
S    if (pid == PidTemp)
S    {
S      ReceivingFlag = SET;
S      for (int i=0;i<=SlaveSend.LIN_TxSize;i++)
S      {
S        UART_SendData(UART2,SlaveSend.LIN_TxBuf[i]);  //发送数据及增强校验和
S        while(UART_TxFlag == RESET);             //等待发送完成
S        UART_TxFlag = RESET;
S      }
S      ReceivingFlag = RESET;
S      PidTemp = 0;
S      printf("Sending Success!\r\n");
S    }
S    #endif
S    #if (LIN_MODE == 1)
S    if (pid == PidTemp)
S    {
S      ReceivingFlag = SET;
S      for (int i=0;i<=SlaveReceive.LIN_RxSize;i++)
S      {
S        while(UART_RxFlag == RESET);             //等待接收数据
S        UART_RxFlag = RESET;
S        SlaveReceive.LIN_RxBuf[i] = UART_ReceiveData(UART2);//接收8位数据
S      }
S      ReceivingFlag = RESET;
S      PidTemp = 0;
S      for(int i=0;i<=SlaveReceive.LIN_RxSize;i++)
S      {
S        printf("Receivedata[%d]=%x\r\n", i,SlaveReceive.LIN_RxBuf[i]);
S      }
S    }
S    #endif
S  }
S}
S
Svoid UART_LINSlave_UART2Handler(void)
S{
S  if(UART_GetFlagStatus(UART2, UART_Flag_BK)) //break标志，发送/接收完break置起
S  {
S    UART_ClearFlag(UART2,UART_Flag_BK);
S    UART_BkFlag = SET;
S  }
S  if(UART_GetFlagStatus(UART2,UART_Flag_SY)) //波特率同步完成标志,该位在同步域0x55后置起
S  {
S    UART_ClearFlag(UART2, UART_Flag_SY);
S  }
S  if(UART_GetFlagStatus(UART2,UART_Flag_TX)) //发送标志位，该位在发送一字节数据后置起
S  {
S    UART_ClearFlag(UART2,UART_Flag_TX);
S    UART_TxFlag = SET;
S  }
S  if(UART_GetFlagStatus(UART2,UART_Flag_RX)) //接收标志位，该位在接收一字节数据后置起
S  {
S    UART_ClearFlag(UART2, UART_Flag_RX);
S    UART_RxFlag = SET;
S    if (ReceivingFlag == RESET)
S    {
S      UART_RxFlag = RESET;
S      PidTemp = UART_ReceiveData(UART2);   
S    }
S  }
S}
S#endif
N#endif
N
