ZHCUBZ4 April 2024
可以将 CAN 功能添加到实验工程中,为用户提供用于发送启动/停止命令并获取运行状态反馈的通信总线。要利用此功能,请在工程构建属性中启用预定义符号 CMD_CAN,如图 4-2 中所示。PCAN-View 用于简单地监控、发送和记录 CAN 数据流量。可以在 motor_common.h 文件中定义不同类型的命令消息。在本工程中,发送器指定了一个启动命令并定义了目标速度值。接收器随后会收到包含目标速度的邮件。
请注意,根据您使用的是套件中的 LaunchPad 还是 EVM,需要进行一些引脚多路复用设置。这些配置是使用 mcanEnableTransceiver 和 tca6416ConfigOutput 函数实现的,如以下代码所述:
#if defined(AM263_CC)
void tca6416ConfigOutput(uint16_t port, uint16_t pin, uint16_t level);
#endif // AM263_CC
#if defined(CMD_CAN)
#if defined(AM263_LP)
void mcanEnableTransceiver(void)
{
uint32_t gpioBaseAddr, pinNum;
gpioBaseAddr = (uint32_t)AddrTranslateP_getLocalAddr(MCAN_ENABLE_BASE_ADDR);
pinNum = MCAN_ENABLE_PIN;
GPIO_setDirMode(gpioBaseAddr, pinNum, GPIO_DIRECTION_OUTPUT);
GPIO_pinWriteLow(gpioBaseAddr, pinNum);
}
#endif // AM263_LP
#if defined(AM263_CC)
/* ========================================================================== */
/* Macros & Typedefs */
/* ========================================================================== */
/* Input status register */
#define TCA6416_REG_INPUT0 ((UInt8) 0x00U)
#define TCA6416_REG_INPUT1 ((UInt8) 0x01U)
/* Output register to change state of output BIT set to 1, output set HIGH */
#define TCA6416_REG_OUTPUT0 ((uint8_t) 0x02U)
#define TCA6416_REG_OUTPUT1 ((uint8_t) 0x03U)
/* Configuration register. BIT = '1' sets port to input, BIT = '0' sets
* port to output */
#define TCA6416_REG_CONFIG0 ((uint8_t) 0x06U)
#define TCA6416_REG_CONFIG1 ((uint8_t) 0x07U)
/* ========================================================================== */
/* Function Declarations */
/* ========================================================================== */
static void SetupI2CTransfer(I2C_Handle handle, uint32_t targetAddr,
uint8_t *writeData, uint32_t numWriteBytes,
uint8_t *readData, uint32_t numReadBytes);
void mcanEnableTransceiver(void)
{
I2C_Handle i2cHandle;
uint8_t dataToSlave[4];
i2cHandle = gI2cHandle[CONFIG_I2C0];
dataToSlave[0] = TCA6416_REG_CONFIG0;
dataToSlave[1] = 0x0U;
SetupI2CTransfer(i2cHandle, 0x20, &dataToSlave[0], 1, &dataToSlave[1], 1);
/* set the P00 to 0 make them output ports. */
dataToSlave[1] &= ~(0x1U);
SetupI2CTransfer(i2cHandle, 0x20, &dataToSlave[0], 2, NULL, 0);
/* Get the port values. */
dataToSlave[0] = TCA6416_REG_INPUT0;
dataToSlave[1] = 0x0U;
SetupI2CTransfer(i2cHandle, 0x20, &dataToSlave[0], 1, &dataToSlave[1], 1);
/* Set P10 and P11 to 0.
*/
dataToSlave[0] = TCA6416_REG_OUTPUT0;
dataToSlave[1] &= ~(0x1);
SetupI2CTransfer(i2cHandle, 0x20, &dataToSlave[0], 2, NULL, 0);
}
static void SetupI2CTransfer(I2C_Handle handle, uint32_t targetAddr,
uint8_t *writeData, uint32_t numWriteBytes,
uint8_t *readData, uint32_t numReadBytes)
{
int32_t status;
I2C_Transaction i2cTransaction;
/* Enable Transceiver */
I2C_Transaction_init(&i2cTransaction);
i2cTransaction.targetAddress = targetAddr;
i2cTransaction.writeBuf = (uint8_t *)&writeData[0];
i2cTransaction.writeCount = numWriteBytes;
i2cTransaction.readBuf = (uint8_t *)&readData[0];
i2cTransaction.readCount = numReadBytes;
status = I2C_transfer(handle, &i2cTransaction);
DebugP_assert(SystemP_SUCCESS == status);
}
#endif // AM263_CC
#endif // CMD_CAN
启动“PCAN-View”后,将 CAN 适配器设置为图 4-34:
双击启动 CAN 数据传输,如图 4-35 所示。
如图 4-36 所示,为 motorVars_M1.flagEnableRunAndIdentify 配置的值为 1,motorVars_M1.speedRef_Hz 设为 40Hz。然后,该速度值通过 CAN 通信传回接收器。