【中科昊芯HXS320F28025C】昊芯HXS320F28034数字信号处理器CAN调试
<div>昊芯HXS320F28034数字信号处理器DSP,其CAN模块以Mailbox信箱MBOXn控制与传输数据,实现DSP之间的控制指令高效收发,可更有效助力于工程师实现高效的多DSP控制指令收发,广泛应用于电动汽车、风力发电、轨道交通、机器人等高复杂度控制领域。</div><div>本章节以HX2000系列芯片调试 USB_CAN通信收发实例讲解CAN网络通信功能。</div>
<div>HX2000系列CAN模块原理如下图,通过CANMC使能进入初始化配置模式,等待CANMC置高写入CANBTC位配置波特率;等待CANMC拉低,完成初始化;</div>
<div>int main(void)</div>
<div>{</div>
<div>InitSysCtrl();//系统时钟初始化</div>
<div>CAN_Init();//CAN的初始化参数配置</div>
<div>InitECanaGpio();//CAN的Gpio引脚配置</div>
<div>EALLOW;</div>
<div>ECanaRegs.CANMIM.bit.MIM0=1;//打开接收掩码,接收到数据触发中断</div>
<div>ECanaRegs.CANMIL.bit.MIL0 = 0; // 选择EcanA中断0</div>
<div>ECanaRegs.CANGIM.bit.I0EN = 1; // 使能中断0</div>
<div>PieVectTable.ECAN0INTA = &eCanRxIsr; // CANA 0接收中断入口</div>
<div>EDIS;</div>
<div>PieCtrlRegs.PIEIER9.bit.INTx5 = 1; // 使能ECAN1中断</div>
<div>IER |= M_INT9; // Enable CPU INT9</div>
<div>EINT;</div>
<div>while(1){</div>
<div>CAN_Tx();//发送数据</div>
<div>}</div>
<div>return 0;</div>
<div>}</div>
<div>其中,CAN的初始化参数配置代码为:</div>
<div>void CAN_Init()</div>
<div>{</div>
<div>volatile struct ECAN_REGS ECanaShadow;</div>
<div>EALLOW;</div>
<div>/* 配置RX与TX引脚*/</div>
<div>ECanaShadow.CANTIOC.all = P_ECanaRegs->CANTIOC.all;</div>
<div>ECanaShadow.CANTIOC.bit.TXFUNC = 1;</div>
<div>P_ECanaRegs->CANTIOC.all = ECanaShadow.CANTIOC.all;</div>
<div>ECanaShadow.CANRIOC.all = P_ECanaRegs->CANRIOC.all;</div>
<div>ECanaShadow.CANRIOC.bit.RXFUNC = 1;</div>
<div>P_ECanaRegs->CANRIOC.all = ECanaShadow.CANRIOC.all;</div>
<div>/* 清RMPn, GIFn 位 */</div>
<div>P_ECanaRegs->CANRMP.all = 0xFFFFFFFF;</div>
<div>/* 清中断标志 */</div>
<div>P_ECanaRegs->CANGIF0.all = 0xFFFFFFFF;</div>
<div>P_ECanaRegs->CANGIF1.all = 0xFFFFFFFF;</div>
<div>/* 初始化配置,写入CAN波特率*/</div>
<div>ECanaShadow.CANMC.all = P_ECanaRegs->CANMC.all;</div>
<div>ECanaShadow.CANMC.bit.CCR = 1 ; // Set CCR = 1</div>
<div>P_ECanaRegs->CANMC.all = ECanaShadow.CANMC.all;</div>
<div>ECanaShadow.CANES.all = P_ECanaRegs->CANES.all;</div>
<div>do</div>
<div>{</div>
<div>ECanaShadow.CANES.all = P_ECanaRegs->CANES.all;</div>
<div>} while(ECanaShadow.CANES.bit.CCE != 1 ); // Wait for CCE bit to be set..</div>
<div>//Bit rate=SYSCLKOUT/2/{(BRP+1)*[(TSEG1+1)+(TSEG2+1)+1,</div>
<div>//TSEG1≥3,TSEG2≥2</div>
<div>ECanaShadow.CANBTC.bit.BRPREG = 2;</div>
<div>ECanaShadow.CANBTC.bit.TSEG2REG = 4;</div>
<div>ECanaShadow.CANBTC.bit.TSEG1REG = 13;</div>
<div>P_ECanaRegs->CANBTC.all = ECanaShadow.CANBTC.all;</div>
<div>ECanaShadow.CANMC.all = P_ECanaRegs->CANMC.all;</div>
<div>ECanaShadow.CANMC.bit.CCR = 0 ; // Set CCR = 0</div>
<div>P_ECanaRegs->CANMC.all = ECanaShadow.CANMC.all;</div>
<div>ECanaShadow.CANES.all = P_ECanaRegs->CANES.all;</div>
<div>do</div>
<div>{</div>
<div>ECanaShadow.CANES.all = P_ECanaRegs->CANES.all;</div>
<div>} while(ECanaShadow.CANES.bit.CCE != 0 ); // Wait for CCE bit to be cleared..</div>
<div>/* 屏蔽所有信箱寄存器*/</div>
<div>// Required before writing the MSGIDs</div>
<div>P_ECanaRegs->CANME.all = 0;</div>
<div>/* 配置MBOX1的MSGID*/</div>
<div>P_ECanaMboxes->MBOX1.MSGID.all = 0x00040000;</div>
<div>P_ECanaMboxes->MBOX0.MSGID.all = 0x00040000;</div>
<div>/* 配置 MBOX1/MBOX0的传输字节为2 */</div>
<div>ECanaMboxes.MBOX1.MSGCTRL.bit.DLC = 0x2;</div>
<div>ECanaMboxes.MBOX0.MSGCTRL.bit.DLC = 0x2;</div>
<div>/* 配置 MBOX0 为接收,MBOX1为发送 */</div>
<div>ECanaRegs.CANMD.bit.MD0=1;</div>
<div>ECanaRegs.CANMD.bit.MD1=0;</div>
<div>/*使能 MBOX0 and MBOX1 */</div>
<div>ECanaRegs.CANME.bit.ME0=1;</div>
<div>ECanaRegs.CANME.bit.ME1=1;</div>
<div>//挂起接收邮箱,以触发接收中断</div>
<div>if(ECanaRegs.CANRMP.bit.RMP0==0)</div>
<div>{</div>
<div>ECanaRegs.CANRMP.bit.RMP0=1;</div>
<div>}</div>
<div>EDIS;</div>
<div>return;</div>
<div>}</div>
<div>通过Mailbox邮箱MBOXn.MSGCTRL的RTR位配置远程传输请求,TPL位配置MBOXn传输优先级,DLC位配置传输0~8个字节,传输过程如下:</div>
<div>1.CAN总线通过CAN传输芯片向CAN发送数据,使能CANRIOC打开接收线,接收来自GPIOMUX所定义CANRX接收引脚上的数据,装到缓冲区Receive Buffer;</div>
<div>2.通过CANMD配置Mailbox邮箱MBOXn为接收,使能CANME打开邮箱MBOXn,接收来自缓冲区中的数据;</div>
<ol>
<li>全局中断标志的设置取决于CANGIM寄存器中GIL位的设置。如果设置了该位,则全局中断在CANGIF1寄存器中设置位;否则,在CANGIF0寄存器中设置。通过CANMIM配置接收掩码中断使能;通过CANMIL配置,将MBOXn接收中断,映射到中断ECAN0INTA或ECAN1INTA;使能CANGIM打开中断信号,MBOXn接收到数据时,将产生一个接收中断,使CANGIF0/1中相应接收邮箱MBOXn的邮箱号置位;通过PIE响应CPU执行接收中断程序:</li>
</ol>
<div>EEWORLDIMGTK0</div>
<div>(1)置位CANRMP挂起MBOXn邮箱,以防止瞬间接收到多组数据;</div>
<div>(2)通过CANGIF0/1,确认接收到数据邮箱为MBOXn,并读取接收的数据;</div>
<ol>
<li>置位CANRMP清除邮箱MBOXn挂起状态,准备接收下一组数据。</li>
</ol>
<div>CAN的发送程序代码为:</div>
<div>void CAN_Tx(void)</div>
<div>{</div>
<div>//等待MBOX0接收成功,读取消息</div>
<div>while(ECanaRegs.CANRMP.bit.RMP0!=1){}</div>
<div>ECanaMboxes.MBOX1.MDL.byte.BYTE0 = (receive_data&0xff);</div>
<div>ECanaMboxes.MBOX1.MDL.byte.BYTE1 = ((receive_data>>8)&0xff);</div>
<div>ECanaRegs.CANTRS.bit.TRS1 = 1;//发送MBOX1数据到MBOX0</div>
<div>} 作者:中科昊芯 https://www.bilibili.com/read/cv18742715/?spm_id_from=333.999.0.0 出处:bilibili</div>
<div>EEWORLDIMGTK1</div>
<div>4.通过CANMD配置另一Mailbox邮箱MBOXn为发送,使能CANME打开发送邮箱,使能CANTRS启动发送邮箱,发送数据到缓冲区 Transmit Buffer;</div>
<div>5.使能CANTIOC打开发送线,通过GPIOMUX所定义CANTX发送引脚将缓冲区Transmit Buffer数据发送到CAN总线。 </div>
页:
[1]