上级目录TMS320F28335使用教程
CAN通讯协议——————待插入链接
CAN结构和特性介绍
Controller Area Network (CAN)
芯片框架和接口电路
模块架构:
有32个消息邮箱,传输消息时根据消息优先级进行传输,如果超过一个邮箱的消息有相同的优先级,则先传输高编号邮箱中的消息。
To initiate a data transfer, the transmission request bit (TRS.n) must be set.
The SCC(Standard CAN Controller) mode or the full featured eCAN mode is selected using the SCB bit (CANMC.13).
SCC模式:
The SCC Mode is a reduced functionality mode of the eCAN. Only 16 mailboxes (0 through 15) are available in this mode.
邮箱结构:
The mailbox module in the eCAN provides 32 message mailboxes of 8-byte data length, a 29-bit identifier,
and several control bits. Each mailbox can be configured as either transmit or receive. In the eCAN mode,
each mailbox has its individual acceptance mask.
寄存器访问应一次访问多少宽度的数据(8位、16位、32位):
The eCAN module has two different address segments mapped in the memory. The first segment is used
to access the control registers, the status registers, the acceptance masks, the time stamp, and the timeout of the message objects. The access to the control and status registers is limited to 32-bit wide
accesses. The local acceptance masks, the time stamp registers, and the time-out registers can be
accessed 8-bit, 16-bit and 32-bit wide. The second address segment is used to access the mailboxes.
This memory range can be accessed 8-bit, 16-bit and 32-bit wide.
上面提到一些寄存器只能32位读写,下面举例
当需要检查寄存器中某一位状态时应用do-while,而不要直接用while
CAN内存分布:
Each mailbox contains:
• The message identifier
– 29 bits for extended identifier
– 11 bits for standard identifier
• The identifier extension bit, IDE (MSGID.31)
• The acceptance mask enable bit, AME (MSGID.30)
• The auto answer mode bit, AAM (MSGID.29)
• The transmit priority level, TPL (MSGCTRL.12-8)
• The remote transmission request bit, RTR (MSGCTRL.4)
• The data length code, DLC (MSGCTRL.3-0)
• Up to eight bytes for the data field
每个消息邮箱可以设置成一下四种模式之一:
接受和回复消息可以是1对多连接,而请求数据和回复则是1对1.
发送消息特性
发送邮箱消息时,按照设定优先级开始发送,如果两个邮箱设定的优先级相同,则先发送高标号的邮箱中的消息。
发送邮箱消息时,有消息仲裁失败,此时有其他邮箱消息需要发送,且优先级更高,例如A邮箱的TRS位被置位,B邮箱的TRS位也被置位,分两种情况:
1.A邮箱仲裁失败前,B邮箱对应TRS置位,则会先发送B邮箱消息。
2.A邮箱仲裁失败后,B邮箱对应TRS置位,则会先发送A邮箱消息。
接收消息特性
RMP—receive-messagepending
RML—receive messagelost
OPC—overwrite-protection
接收消息后,会与接收邮箱比对标识符与控制位,如果相等,则会把数据存入对应邮箱中,对应的RMP[n]会置位,如果使能了接收中断则会产生中断,如果标识符与控制位没有匹配上,则不会存储数据。
比对标识符会先从高标号的消息邮箱开始,eCAN模式31到0,SCC模式15到0.
RMP[n]会在CPU读取对应消息后复位。
如果邮箱消息还没读取,此时来了第二条消息,对应的消息丢失位RML[n]会被置位,如果OPC[n]位被清除,第二条消息会覆盖第一条消息,否则,则会选中下一个邮箱(暂时未知选中下一个邮箱是什么意思)。
正常配置下的CAN模块
相同网络中的CAN模块应配置为相同的速率
CAN模块初始化流程
关于位时长的了解
CAN模块寄存器:
//官方文件
// eCAN Control & Status register file
//
struct ECAN_REGS {
union CANME_REG CANME; // Mailbox Enable
union CANMD_REG CANMD; // Mailbox Direction
union CANTRS_REG CANTRS; // Transmit Request Set
union CANTRR_REG CANTRR; // Transmit Request Reset
union CANTA_REG CANTA; // Transmit Acknowledge
union CANAA_REG CANAA; // Abort Acknowledge
union CANRMP_REG CANRMP; // Received Message Pending
union CANRML_REG CANRML; // Received Message Lost
union CANRFP_REG CANRFP; // Remote Frame Pending
union CANGAM_REG CANGAM; // Global Acceptance Mask
union CANMC_REG CANMC; // Master Control
union CANBTC_REG CANBTC; // Bit Timing
union CANES_REG CANES; // Error Status
union CANTEC_REG CANTEC; // Transmit Error Counter
union CANREC_REG CANREC; // Receive Error Counter
union CANGIF0_REG CANGIF0; // Global Interrupt Flag 0
union CANGIM_REG CANGIM; // Global Interrupt Mask 0
union CANGIF1_REG CANGIF1; // Global Interrupt Flag 1
union CANMIM_REG CANMIM; // Mailbox Interrupt Mask
union CANMIL_REG CANMIL; // Mailbox Interrupt Level
union CANOPC_REG CANOPC; // Overwrite Protection Control
union CANTIOC_REG CANTIOC; // TX I/O Control
union CANRIOC_REG CANRIOC; // RX I/O Control
Uint32 CANTSC; // Time-stamp counter
union CANTOC_REG CANTOC; // Time-out Control
union CANTOS_REG CANTOS; // Time-out Status
};
实验
本次实验实现CANA邮箱0发送,CANB邮箱0接收
开发板电路图
时钟参考:
TMS320F28335时钟和系统控制
比特率计算:SYSCLKOUT=150mhz
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx分界线xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
调整BRP的值,可以获得不同CAN模块的时钟,对应不同CAN总线速度。
设置CAN模块步骤
本次实验实现CANA邮箱0发送,CANB邮箱0接收
参考了下官方例程,感觉和技术手册不是很一样,所以我按照技术手册重写了下面的初始化的代码,代码中只是为了实现需要的功能,所以都是针对特定的功能实现的初始化,如果想要移植需要修改的地方比较多。实际应用中可以使用指针传参来初始化,这样移植和使用比较简单
step1.
SysCtrlRegs.PCLKCR0.bit.ECANAENCLK=1; // eCAN-A
SysCtrlRegs.PCLKCR0.bit.ECANBENCLK=1; // eCAN-B
step2
//复用GPIOInitECanaGpio(void)
{
EALLOW;
//
// Enable internal pull-up for the selected CAN pins
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.
//
GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pull-up for GPIO18 (CANRXA)
GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; //Enable pull-up for GPIO19 (CANTXA)
GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; //Enable pull-up for GPIO16(CANTXB)
GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; // Enable pull-up for GPIO17(CANRXB)
//
// Set qualification for selected CAN pins to asynch only
// Inputs are synchronized to SYSCLKOUT by default.
// This will select asynch (no qualification) for the selected pins.
//由参考手册得知,作为外设且异步输入,赋值3
GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch qual for GPIO18 (CANRXA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch qual for GPIO17 (CANRXB)
//
// Configure eCAN-A pins using GPIO regs
// This specifies which of the possible GPIO pins will be eCAN functional
// pins.
//
GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 3; // Configure GPIO18 for CANRXA
GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 3