概述
Lin總線在汽車電子中使用非常廣泛,下面簡單介紹一下NXP MPC574X系列PowerPC的Lin總線配置。由於Lin總線的通信速率比較低,所以一般在實際開發中會使用DMA外設進行數據的搬運,減少MCU的等待時間。
Lin總線配置
波特率配置
如下圖所示,LinFlexD模塊的Baud Clock時鐘源爲HALFSYS_CLK。
如下圖所示,HALFSYS_CLK爲PLL輸出系統時鐘SYS_CLK的一半。及如果配置SYS_CLK爲200MHz,HALFSYS_CLK則對應爲100MHz。
由下面代碼可知,首先使Lin模塊退出Sleep Mode,並進入Init Mode,然後等待Lin進入Init Mode。
MegaHertz表示Lin模塊的Baud Clock的頻率,BaudRate表示要設置的總線波特率。
uint16_t Fraction,Integer;
LINFlexD_0.LINCR1.B.SLEEP = 0; /* Exit Sleep Mode */
LINFlexD_0.LINCR1.B.INIT = 1; /* Put LINFlex hardware in init mode */
/* wait for the INIT mode */
while (0x1000 != (LINFlexD_0.LINSR.R & 0xF000)) {}
BaudRate = (MegaHertz * 1000000) / BaudRate;
Integer = BaudRate / 16;
Fraction = BaudRate - (Integer * 16);
LINFlexD_0.LINIBRR.B.IBR= Integer; /* Mantissa baud rate divider component */
/* Baud rate divider = 100 MHz LIN_CLK input / (16*19200 bps) ~= 326 */
LINFlexD_0.LINFBRR.B.FBR = Fraction; /* Fraction baud rate divider comonent */