TIDUF67 April 2024 – December 2024
CAN functionality can be added into the lab project to provide the user a communication bus for sending the start/stop command and getting the feedback running states. To utilize this, enable the pre-define symbol CMD_CAN in project build properties as shown in Figure 4-2. PCAN-View is used to simply monitor, transmit, and record CAN data traffic. Different type of command messages can be defined in motor_common.hfile. In this project as an example, sender specifies a starting command and defines a target speed value. The recipient then receives a message containing the target speed.
Please be noted that depending on whether you are using the LaunchPad or EVM in the kit, certain pin mux settings are required. These configurations are achieved using the mcanEnableTransceiver and tca6416ConfigOutput functions, as outlined in the following code:
#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
After launching "PCAN-View", make setup the CAN adaptor as Figure 4-34:
Double click to initiate the CAN data transmission as shown in Figure 4-35.
As shown in Figure 4-36, the motorVars_M1.flagEnableRunAndIdentify is configured with a value of 1, and the motorVars_M1.speedRef_Hz is set to 40Hz. This speed value is then transmitted back to the recipient through the CAN communication.