L 1 "..\Apps\UART_LINMaster.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：MasterSend  主机发送 1：MaserRecive 主机接收
N*          
N*******************************************************************************/
N#if defined(SC32f12xx) 
X#if 0L 
S#include "sc32_conf.h"
S#include "Function_Init.h"
S
S/* UART_LIN主机模式配置 */
S//(MasterSend:LIN_MODE=0,MaserRecive:LIN_MODE=1)
S#define LIN_MODE 1
S
S#if (TestNum == 36)
Svolatile FlagStatus UART_BkFlag = RESET;
Svolatile FlagStatus UART_RxFlag = RESET;
Svolatile FlagStatus UART_TxFlag = RESET;
S
S
Stypedef struct
S{
S  uint16_t id;          //待发送的id号
S  uint16_t LIN_TxSize;  //待发送数据大小（一帧数据不大于8字节）
S  uint8_t LIN_TxBuf[9]; //发送缓存，最多发送8个字节和一个校验和
S}LIN_MS_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_MR_TypeDef;
S
Svoid UART_LINMaster_Function(void)
S{
S  //配置UART2外设时钟
S  RCC_APB1Cmd(ENABLE);
S  RCC_HCLKConfig(RCC_SYSCLK_Div1);
S  RCC_APB1Config(RCC_HCLK_Div1);	//LIN时钟为32Mhz
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, ENABLE);
S#if defined(SC32f10xx)||defined(SC32f12xx)
S  NVIC_EnableIRQ(UART0_2_4_IRQn);	//UART2中断使能
S#endif  
S  //UART2 LIN的初始化配置
S  UART_InitTypeDef UART_InitStruct;
S  UART_InitStruct.UART_BaudRate = 19200;	        //设置UART2波特率为19200
S#if defined(SC32f10xx)||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_MASTER);	                //设置UART2为LIN主机模式
S  UART_LIN_BKSIZE(UART2,UART_BKSIZE_13);	        //13位长度发送间隔段
S  
S  #if (LIN_MODE == 0)
S    //主机发送配置
S    LIN_MS_TypeDef MasterSend;                               //定义主机发送变量
S    MasterSend.id = 0x26;
S    MasterSend.LIN_TxSize = 8;
S    MasterSend.LIN_TxBuf[0] = 0x01;
S    MasterSend.LIN_TxBuf[1] = 0x02;
S    MasterSend.LIN_TxBuf[2] = 0x03;
S    MasterSend.LIN_TxBuf[3] = 0x04;
S    MasterSend.LIN_TxBuf[4] = 0x05;
S    MasterSend.LIN_TxBuf[5] = 0x06;
S    MasterSend.LIN_TxBuf[6] = 0x07;
S    MasterSend.LIN_TxBuf[7] = 0x08;
S    uint8_t pid = LIN_CalID(MasterSend.id);                 //计算PID
S    uint8_t CRC_sum = LINCalChecksum(MasterSend.id,MasterSend.LIN_TxBuf,8);//存放8位数据的增强效验和
S    
S    uint8_t i = 0;
S    UART_SendBreak();                          //发送间隔段
S    while(UART_BkFlag == RESET);              //等待发送完成
S    UART_BkFlag = RESET;
S    
S    UART_SendData(UART2,0x55);                //发送同步段
S    while(UART_TxFlag == RESET);             //等待发送完成
S    UART_TxFlag = RESET;
S    
S    UART_SendData(UART2,pid);                //发送PID
S    while(UART_TxFlag == RESET);             //等待发送完成
S    UART_TxFlag = RESET;
S    for (i=0;i<MasterSend.LIN_TxSize;i++)
S    {
S      UART_SendData(UART2,MasterSend.LIN_TxBuf[i]);//发送8位数据
S      while(UART_TxFlag == RESET);                //等待发送完成
S      UART_TxFlag = RESET;
S    }
S    
S    UART_SendData(UART2,CRC_sum);             //发送增强效验和
S    while(UART_TxFlag == RESET);             //等待发送完成
S    UART_TxFlag = RESET;
S    printf("Sending Success!\r\n");
S  #endif
S   
S  #if (LIN_MODE == 1)
S  //主机接收配置
S  LIN_MR_TypeDef MasterReceive;              //定义主机接收变量
S  MasterReceive.id = 0x27;
S  MasterReceive.LIN_RxSize = 8;
S  for (int i=0;i<9;i++)                     //清空接收缓存
S  {
S    MasterReceive.LIN_RxBuf[i] = 0;
S  }
S  
S  int8_t i = 0;
S  uint8_t pid = LIN_CalID(MasterReceive.id);    //计算PID
S  UART_SendBreak();                          //发送间隔段
S  while(UART_BkFlag == RESET);              //等待发送完成
S  UART_BkFlag = RESET;
S  
S  UART_SendData(UART2,0x55);                //发送同步段
S  while(UART_TxFlag == RESET);             //等待发送完成
S  UART_TxFlag = RESET;
S  while(UART_RxFlag == RESET);             //同步段发送时，主机同时也会接收同步段数据
S  UART_RxFlag = RESET;
S	
S  UART_SendData(UART2,pid);                //发送PID
S  while(UART_TxFlag == RESET);             //等待发送完成
S  UART_TxFlag = RESET;
S  while(UART_RxFlag == RESET);             //PID发送时，主机同时也会接收PID数据
S  UART_RxFlag = RESET;
S  for (i=0;i<=MasterReceive.LIN_RxSize;i++)
S  {
S    while(UART_RxFlag == RESET);             //等待接收数据
S    UART_RxFlag = RESET;
S    MasterReceive.LIN_RxBuf[i] = UART_ReceiveData(UART2);//接收8位数据及校验和
S  }
S  for(int i=0;i<MasterReceive.LIN_RxSize;i++)
S  {
S    printf("Receivedata[%d]=%X\r\n", i,MasterReceive.LIN_RxBuf[i]);
S  }
S#endif
S  while(1)
S  {
S  }
S}
S
Svoid UART_LINMaster_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  }
S}
S#endif
N#endif
